Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions
0004  *
0005  * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
0006  */
0007 
0008 #include <crypto/internal/hash.h>
0009 #include <crypto/internal/simd.h>
0010 #include <crypto/sha2.h>
0011 #include <crypto/sha256_base.h>
0012 #include <linux/cpufeature.h>
0013 #include <linux/crypto.h>
0014 #include <linux/module.h>
0015 
0016 #include <asm/hwcap.h>
0017 #include <asm/simd.h>
0018 #include <asm/neon.h>
0019 #include <asm/unaligned.h>
0020 
0021 #include "sha256_glue.h"
0022 
0023 MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions");
0024 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
0025 MODULE_LICENSE("GPL v2");
0026 
0027 asmlinkage void sha2_ce_transform(struct sha256_state *sst, u8 const *src,
0028                   int blocks);
0029 
0030 static int sha2_ce_update(struct shash_desc *desc, const u8 *data,
0031               unsigned int len)
0032 {
0033     struct sha256_state *sctx = shash_desc_ctx(desc);
0034 
0035     if (!crypto_simd_usable() ||
0036         (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
0037         return crypto_sha256_arm_update(desc, data, len);
0038 
0039     kernel_neon_begin();
0040     sha256_base_do_update(desc, data, len,
0041                   (sha256_block_fn *)sha2_ce_transform);
0042     kernel_neon_end();
0043 
0044     return 0;
0045 }
0046 
0047 static int sha2_ce_finup(struct shash_desc *desc, const u8 *data,
0048              unsigned int len, u8 *out)
0049 {
0050     if (!crypto_simd_usable())
0051         return crypto_sha256_arm_finup(desc, data, len, out);
0052 
0053     kernel_neon_begin();
0054     if (len)
0055         sha256_base_do_update(desc, data, len,
0056                       (sha256_block_fn *)sha2_ce_transform);
0057     sha256_base_do_finalize(desc, (sha256_block_fn *)sha2_ce_transform);
0058     kernel_neon_end();
0059 
0060     return sha256_base_finish(desc, out);
0061 }
0062 
0063 static int sha2_ce_final(struct shash_desc *desc, u8 *out)
0064 {
0065     return sha2_ce_finup(desc, NULL, 0, out);
0066 }
0067 
0068 static struct shash_alg algs[] = { {
0069     .init           = sha224_base_init,
0070     .update         = sha2_ce_update,
0071     .final          = sha2_ce_final,
0072     .finup          = sha2_ce_finup,
0073     .descsize       = sizeof(struct sha256_state),
0074     .digestsize     = SHA224_DIGEST_SIZE,
0075     .base           = {
0076         .cra_name       = "sha224",
0077         .cra_driver_name    = "sha224-ce",
0078         .cra_priority       = 300,
0079         .cra_blocksize      = SHA256_BLOCK_SIZE,
0080         .cra_module     = THIS_MODULE,
0081     }
0082 }, {
0083     .init           = sha256_base_init,
0084     .update         = sha2_ce_update,
0085     .final          = sha2_ce_final,
0086     .finup          = sha2_ce_finup,
0087     .descsize       = sizeof(struct sha256_state),
0088     .digestsize     = SHA256_DIGEST_SIZE,
0089     .base           = {
0090         .cra_name       = "sha256",
0091         .cra_driver_name    = "sha256-ce",
0092         .cra_priority       = 300,
0093         .cra_blocksize      = SHA256_BLOCK_SIZE,
0094         .cra_module     = THIS_MODULE,
0095     }
0096 } };
0097 
0098 static int __init sha2_ce_mod_init(void)
0099 {
0100     return crypto_register_shashes(algs, ARRAY_SIZE(algs));
0101 }
0102 
0103 static void __exit sha2_ce_mod_fini(void)
0104 {
0105     crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
0106 }
0107 
0108 module_cpu_feature_match(SHA2, sha2_ce_mod_init);
0109 module_exit(sha2_ce_mod_fini);