2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-03-10 08:47:46 +00:00
|
|
|
/*
|
|
|
|
* sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <crypto/internal/hash.h>
|
2019-03-13 05:12:49 +00:00
|
|
|
#include <crypto/internal/simd.h>
|
2020-11-13 05:20:21 +00:00
|
|
|
#include <crypto/sha2.h>
|
2015-04-09 10:55:43 +00:00
|
|
|
#include <crypto/sha256_base.h>
|
2017-05-21 10:23:39 +00:00
|
|
|
#include <linux/cpufeature.h>
|
2015-03-10 08:47:46 +00:00
|
|
|
#include <linux/crypto.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <asm/hwcap.h>
|
|
|
|
#include <asm/simd.h>
|
|
|
|
#include <asm/neon.h>
|
|
|
|
#include <asm/unaligned.h>
|
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
#include "sha256_glue.h"
|
|
|
|
|
2015-03-10 08:47:46 +00:00
|
|
|
MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
|
|
|
|
MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src,
|
|
|
|
int blocks);
|
2015-03-10 08:47:46 +00:00
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
static int sha2_ce_update(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len)
|
2015-03-10 08:47:46 +00:00
|
|
|
{
|
|
|
|
struct sha256_state *sctx = shash_desc_ctx(desc);
|
|
|
|
|
2019-03-13 05:12:49 +00:00
|
|
|
if (!crypto_simd_usable() ||
|
2015-04-09 10:55:43 +00:00
|
|
|
(sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
|
|
|
|
return crypto_sha256_arm_update(desc, data, len);
|
2015-03-10 08:47:46 +00:00
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
kernel_neon_begin();
|
|
|
|
sha256_base_do_update(desc, data, len,
|
|
|
|
(sha256_block_fn *)sha2_ce_transform);
|
|
|
|
kernel_neon_end();
|
2015-03-10 08:47:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
static int sha2_ce_finup(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len, u8 *out)
|
2015-03-10 08:47:46 +00:00
|
|
|
{
|
2019-03-13 05:12:49 +00:00
|
|
|
if (!crypto_simd_usable())
|
2015-04-09 10:55:43 +00:00
|
|
|
return crypto_sha256_arm_finup(desc, data, len, out);
|
2015-03-10 08:47:46 +00:00
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
kernel_neon_begin();
|
2015-03-10 08:47:46 +00:00
|
|
|
if (len)
|
2015-04-09 10:55:43 +00:00
|
|
|
sha256_base_do_update(desc, data, len,
|
|
|
|
(sha256_block_fn *)sha2_ce_transform);
|
|
|
|
sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
|
|
|
|
kernel_neon_end();
|
2015-03-10 08:47:46 +00:00
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
return sha256_base_finish(desc, out);
|
2015-03-10 08:47:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 10:55:43 +00:00
|
|
|
static int sha2_ce_final(struct shash_desc *desc, u8 *out)
|
2015-03-10 08:47:46 +00:00
|
|
|
{
|
2015-04-09 10:55:43 +00:00
|
|
|
return sha2_ce_finup(desc, NULL, 0, out);
|
2015-03-10 08:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct shash_alg algs[] = { {
|
2015-04-09 10:55:43 +00:00
|
|
|
.init = sha224_base_init,
|
|
|
|
.update = sha2_ce_update,
|
|
|
|
.final = sha2_ce_final,
|
|
|
|
.finup = sha2_ce_finup,
|
2015-03-10 08:47:46 +00:00
|
|
|
.descsize = sizeof(struct sha256_state),
|
|
|
|
.digestsize = SHA224_DIGEST_SIZE,
|
|
|
|
.base = {
|
|
|
|
.cra_name = "sha224",
|
|
|
|
.cra_driver_name = "sha224-ce",
|
2015-04-03 10:03:40 +00:00
|
|
|
.cra_priority = 300,
|
2015-03-10 08:47:46 +00:00
|
|
|
.cra_blocksize = SHA256_BLOCK_SIZE,
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
}
|
|
|
|
}, {
|
2015-04-09 10:55:43 +00:00
|
|
|
.init = sha256_base_init,
|
|
|
|
.update = sha2_ce_update,
|
|
|
|
.final = sha2_ce_final,
|
|
|
|
.finup = sha2_ce_finup,
|
2015-03-10 08:47:46 +00:00
|
|
|
.descsize = sizeof(struct sha256_state),
|
|
|
|
.digestsize = SHA256_DIGEST_SIZE,
|
|
|
|
.base = {
|
|
|
|
.cra_name = "sha256",
|
|
|
|
.cra_driver_name = "sha256-ce",
|
2015-04-03 10:03:40 +00:00
|
|
|
.cra_priority = 300,
|
2015-03-10 08:47:46 +00:00
|
|
|
.cra_blocksize = SHA256_BLOCK_SIZE,
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
}
|
|
|
|
} };
|
|
|
|
|
|
|
|
static int __init sha2_ce_mod_init(void)
|
|
|
|
{
|
|
|
|
return crypto_register_shashes(algs, ARRAY_SIZE(algs));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit sha2_ce_mod_fini(void)
|
|
|
|
{
|
|
|
|
crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
|
|
|
|
}
|
|
|
|
|
2017-05-21 10:23:39 +00:00
|
|
|
module_cpu_feature_match(SHA2, sha2_ce_mod_init);
|
2015-03-10 08:47:46 +00:00
|
|
|
module_exit(sha2_ce_mod_fini);
|