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/sha1.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 extern const u32 sha1_ce_offsetof_count; 29 extern const u32 sha1_ce_offsetof_finalize; 30 31 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src, 32 int blocks); 33 34 static void __sha1_ce_transform(struct sha1_state *sst, u8 const *src, 35 int blocks) 36 { 37 sha1_ce_transform(container_of(sst, struct sha1_ce_state, sst), src, 38 blocks); 39 } 40 41 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count); 42 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize); 43 44 static int sha1_ce_update(struct shash_desc *desc, const u8 *data, 45 unsigned int len) 46 { 47 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 48 49 if (!crypto_simd_usable()) 50 return crypto_sha1_update(desc, data, len); 51 52 sctx->finalize = 0; 53 kernel_neon_begin(); 54 sha1_base_do_update(desc, data, len, __sha1_ce_transform); 55 kernel_neon_end(); 56 57 return 0; 58 } 59 60 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data, 61 unsigned int len, u8 *out) 62 { 63 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 64 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len; 65 66 if (!crypto_simd_usable()) 67 return crypto_sha1_finup(desc, data, len, out); 68 69 /* 70 * Allow the asm code to perform the finalization if there is no 71 * partial data and the input is a round multiple of the block size. 72 */ 73 sctx->finalize = finalize; 74 75 kernel_neon_begin(); 76 sha1_base_do_update(desc, data, len, __sha1_ce_transform); 77 if (!finalize) 78 sha1_base_do_finalize(desc, __sha1_ce_transform); 79 kernel_neon_end(); 80 return sha1_base_finish(desc, out); 81 } 82 83 static int sha1_ce_final(struct shash_desc *desc, u8 *out) 84 { 85 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 86 87 if (!crypto_simd_usable()) 88 return crypto_sha1_finup(desc, NULL, 0, out); 89 90 sctx->finalize = 0; 91 kernel_neon_begin(); 92 sha1_base_do_finalize(desc, __sha1_ce_transform); 93 kernel_neon_end(); 94 return sha1_base_finish(desc, out); 95 } 96 97 static int sha1_ce_export(struct shash_desc *desc, void *out) 98 { 99 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 100 101 memcpy(out, &sctx->sst, sizeof(struct sha1_state)); 102 return 0; 103 } 104 105 static int sha1_ce_import(struct shash_desc *desc, const void *in) 106 { 107 struct sha1_ce_state *sctx = shash_desc_ctx(desc); 108 109 memcpy(&sctx->sst, in, sizeof(struct sha1_state)); 110 sctx->finalize = 0; 111 return 0; 112 } 113 114 static struct shash_alg alg = { 115 .init = sha1_base_init, 116 .update = sha1_ce_update, 117 .final = sha1_ce_final, 118 .finup = sha1_ce_finup, 119 .import = sha1_ce_import, 120 .export = sha1_ce_export, 121 .descsize = sizeof(struct sha1_ce_state), 122 .statesize = sizeof(struct sha1_state), 123 .digestsize = SHA1_DIGEST_SIZE, 124 .base = { 125 .cra_name = "sha1", 126 .cra_driver_name = "sha1-ce", 127 .cra_priority = 200, 128 .cra_blocksize = SHA1_BLOCK_SIZE, 129 .cra_module = THIS_MODULE, 130 } 131 }; 132 133 static int __init sha1_ce_mod_init(void) 134 { 135 return crypto_register_shash(&alg); 136 } 137 138 static void __exit sha1_ce_mod_fini(void) 139 { 140 crypto_unregister_shash(&alg); 141 } 142 143 module_cpu_feature_match(SHA1, sha1_ce_mod_init); 144 module_exit(sha1_ce_mod_fini); 145