1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions 4 * 5 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org> 6 */ 7 8 #include <asm/neon.h> 9 #include <asm/simd.h> 10 #include <asm/unaligned.h> 11 #include <crypto/internal/hash.h> 12 #include <crypto/internal/simd.h> 13 #include <crypto/sha.h> 14 #include <crypto/sha1_base.h> 15 #include <linux/cpufeature.h> 16 #include <linux/crypto.h> 17 #include <linux/module.h> 18 19 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions"); 20 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); 21 MODULE_LICENSE("GPL v2"); 22 23 struct sha1_ce_state { 24 struct sha1_state sst; 25 u32 finalize; 26 }; 27 28 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src, 29 int blocks); 30 31 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count); 32 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize); 33 34 static int sha1_ce_update(struct shash_desc *desc, const u8 *data, 35 unsigned int len) 36 { 37 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 38 39 if (!crypto_simd_usable()) 40 return crypto_sha1_update(desc, data, len); 41 42 sctx->finalize = 0; 43 kernel_neon_begin(); 44 sha1_base_do_update(desc, data, len, 45 (sha1_block_fn *)sha1_ce_transform); 46 kernel_neon_end(); 47 48 return 0; 49 } 50 51 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data, 52 unsigned int len, u8 *out) 53 { 54 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 55 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len; 56 57 if (!crypto_simd_usable()) 58 return crypto_sha1_finup(desc, data, len, out); 59 60 /* 61 * Allow the asm code to perform the finalization if there is no 62 * partial data and the input is a round multiple of the block size. 63 */ 64 sctx->finalize = finalize; 65 66 kernel_neon_begin(); 67 sha1_base_do_update(desc, data, len, 68 (sha1_block_fn *)sha1_ce_transform); 69 if (!finalize) 70 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); 71 kernel_neon_end(); 72 return sha1_base_finish(desc, out); 73 } 74 75 static int sha1_ce_final(struct shash_desc *desc, u8 *out) 76 { 77 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 78 79 if (!crypto_simd_usable()) 80 return crypto_sha1_finup(desc, NULL, 0, out); 81 82 sctx->finalize = 0; 83 kernel_neon_begin(); 84 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); 85 kernel_neon_end(); 86 return sha1_base_finish(desc, out); 87 } 88 89 static struct shash_alg alg = { 90 .init = sha1_base_init, 91 .update = sha1_ce_update, 92 .final = sha1_ce_final, 93 .finup = sha1_ce_finup, 94 .descsize = sizeof(struct sha1_ce_state), 95 .digestsize = SHA1_DIGEST_SIZE, 96 .base = { 97 .cra_name = "sha1", 98 .cra_driver_name = "sha1-ce", 99 .cra_priority = 200, 100 .cra_blocksize = SHA1_BLOCK_SIZE, 101 .cra_module = THIS_MODULE, 102 } 103 }; 104 105 static int __init sha1_ce_mod_init(void) 106 { 107 return crypto_register_shash(&alg); 108 } 109 110 static void __exit sha1_ce_mod_fini(void) 111 { 112 crypto_unregister_shash(&alg); 113 } 114 115 module_cpu_feature_match(SHA1, sha1_ce_mod_init); 116 module_exit(sha1_ce_mod_fini); 117