Merge tag 'x86-urgent-2025-01-28' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / arch / arm64 / crypto / sha1-ce-glue.c
blobcbd14f208f8301a627b36813cace10662f5162f7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
5 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
6 */
8 #include <asm/neon.h>
9 #include <asm/simd.h>
10 #include <linux/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>
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 MODULE_ALIAS_CRYPTO("sha1");
24 struct sha1_ce_state {
25 struct sha1_state sst;
26 u32 finalize;
29 extern const u32 sha1_ce_offsetof_count;
30 extern const u32 sha1_ce_offsetof_finalize;
32 asmlinkage int __sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
33 int blocks);
35 static void sha1_ce_transform(struct sha1_state *sst, u8 const *src,
36 int blocks)
38 while (blocks) {
39 int rem;
41 kernel_neon_begin();
42 rem = __sha1_ce_transform(container_of(sst,
43 struct sha1_ce_state,
44 sst), src, blocks);
45 kernel_neon_end();
46 src += (blocks - rem) * SHA1_BLOCK_SIZE;
47 blocks = rem;
51 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
52 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
54 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
55 unsigned int len)
57 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
59 if (!crypto_simd_usable())
60 return crypto_sha1_update(desc, data, len);
62 sctx->finalize = 0;
63 sha1_base_do_update(desc, data, len, sha1_ce_transform);
65 return 0;
68 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
69 unsigned int len, u8 *out)
71 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
72 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
74 if (!crypto_simd_usable())
75 return crypto_sha1_finup(desc, data, len, out);
78 * Allow the asm code to perform the finalization if there is no
79 * partial data and the input is a round multiple of the block size.
81 sctx->finalize = finalize;
83 sha1_base_do_update(desc, data, len, sha1_ce_transform);
84 if (!finalize)
85 sha1_base_do_finalize(desc, sha1_ce_transform);
86 return sha1_base_finish(desc, out);
89 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
91 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
93 if (!crypto_simd_usable())
94 return crypto_sha1_finup(desc, NULL, 0, out);
96 sctx->finalize = 0;
97 sha1_base_do_finalize(desc, sha1_ce_transform);
98 return sha1_base_finish(desc, out);
101 static int sha1_ce_export(struct shash_desc *desc, void *out)
103 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
105 memcpy(out, &sctx->sst, sizeof(struct sha1_state));
106 return 0;
109 static int sha1_ce_import(struct shash_desc *desc, const void *in)
111 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
113 memcpy(&sctx->sst, in, sizeof(struct sha1_state));
114 sctx->finalize = 0;
115 return 0;
118 static struct shash_alg alg = {
119 .init = sha1_base_init,
120 .update = sha1_ce_update,
121 .final = sha1_ce_final,
122 .finup = sha1_ce_finup,
123 .import = sha1_ce_import,
124 .export = sha1_ce_export,
125 .descsize = sizeof(struct sha1_ce_state),
126 .statesize = sizeof(struct sha1_state),
127 .digestsize = SHA1_DIGEST_SIZE,
128 .base = {
129 .cra_name = "sha1",
130 .cra_driver_name = "sha1-ce",
131 .cra_priority = 200,
132 .cra_blocksize = SHA1_BLOCK_SIZE,
133 .cra_module = THIS_MODULE,
137 static int __init sha1_ce_mod_init(void)
139 return crypto_register_shash(&alg);
142 static void __exit sha1_ce_mod_fini(void)
144 crypto_unregister_shash(&alg);
147 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
148 module_exit(sha1_ce_mod_fini);