Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * sha512-ce-glue.c - SHA-384/SHA-512 using ARMv8 Crypto Extensions
0004  *
0005  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
0006  *
0007  * This program is free software; you can redistribute it and/or modify
0008  * it under the terms of the GNU General Public License version 2 as
0009  * published by the Free Software Foundation.
0010  */
0011 
0012 #include <asm/neon.h>
0013 #include <asm/simd.h>
0014 #include <asm/unaligned.h>
0015 #include <crypto/internal/hash.h>
0016 #include <crypto/internal/simd.h>
0017 #include <crypto/sha2.h>
0018 #include <crypto/sha512_base.h>
0019 #include <linux/cpufeature.h>
0020 #include <linux/crypto.h>
0021 #include <linux/module.h>
0022 
0023 MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions");
0024 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
0025 MODULE_LICENSE("GPL v2");
0026 MODULE_ALIAS_CRYPTO("sha384");
0027 MODULE_ALIAS_CRYPTO("sha512");
0028 
0029 asmlinkage int sha512_ce_transform(struct sha512_state *sst, u8 const *src,
0030                    int blocks);
0031 
0032 asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks);
0033 
0034 static void __sha512_ce_transform(struct sha512_state *sst, u8 const *src,
0035                   int blocks)
0036 {
0037     while (blocks) {
0038         int rem;
0039 
0040         kernel_neon_begin();
0041         rem = sha512_ce_transform(sst, src, blocks);
0042         kernel_neon_end();
0043         src += (blocks - rem) * SHA512_BLOCK_SIZE;
0044         blocks = rem;
0045     }
0046 }
0047 
0048 static void __sha512_block_data_order(struct sha512_state *sst, u8 const *src,
0049                       int blocks)
0050 {
0051     sha512_block_data_order(sst->state, src, blocks);
0052 }
0053 
0054 static int sha512_ce_update(struct shash_desc *desc, const u8 *data,
0055                 unsigned int len)
0056 {
0057     sha512_block_fn *fn = crypto_simd_usable() ? __sha512_ce_transform
0058                            : __sha512_block_data_order;
0059 
0060     sha512_base_do_update(desc, data, len, fn);
0061     return 0;
0062 }
0063 
0064 static int sha512_ce_finup(struct shash_desc *desc, const u8 *data,
0065                unsigned int len, u8 *out)
0066 {
0067     sha512_block_fn *fn = crypto_simd_usable() ? __sha512_ce_transform
0068                            : __sha512_block_data_order;
0069 
0070     sha512_base_do_update(desc, data, len, fn);
0071     sha512_base_do_finalize(desc, fn);
0072     return sha512_base_finish(desc, out);
0073 }
0074 
0075 static int sha512_ce_final(struct shash_desc *desc, u8 *out)
0076 {
0077     sha512_block_fn *fn = crypto_simd_usable() ? __sha512_ce_transform
0078                            : __sha512_block_data_order;
0079 
0080     sha512_base_do_finalize(desc, fn);
0081     return sha512_base_finish(desc, out);
0082 }
0083 
0084 static struct shash_alg algs[] = { {
0085     .init           = sha384_base_init,
0086     .update         = sha512_ce_update,
0087     .final          = sha512_ce_final,
0088     .finup          = sha512_ce_finup,
0089     .descsize       = sizeof(struct sha512_state),
0090     .digestsize     = SHA384_DIGEST_SIZE,
0091     .base.cra_name      = "sha384",
0092     .base.cra_driver_name   = "sha384-ce",
0093     .base.cra_priority  = 200,
0094     .base.cra_blocksize = SHA512_BLOCK_SIZE,
0095     .base.cra_module    = THIS_MODULE,
0096 }, {
0097     .init           = sha512_base_init,
0098     .update         = sha512_ce_update,
0099     .final          = sha512_ce_final,
0100     .finup          = sha512_ce_finup,
0101     .descsize       = sizeof(struct sha512_state),
0102     .digestsize     = SHA512_DIGEST_SIZE,
0103     .base.cra_name      = "sha512",
0104     .base.cra_driver_name   = "sha512-ce",
0105     .base.cra_priority  = 200,
0106     .base.cra_blocksize = SHA512_BLOCK_SIZE,
0107     .base.cra_module    = THIS_MODULE,
0108 } };
0109 
0110 static int __init sha512_ce_mod_init(void)
0111 {
0112     return crypto_register_shashes(algs, ARRAY_SIZE(algs));
0113 }
0114 
0115 static void __exit sha512_ce_mod_fini(void)
0116 {
0117     crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
0118 }
0119 
0120 module_cpu_feature_match(SHA512, sha512_ce_mod_init);
0121 module_exit(sha512_ce_mod_fini);