1 // SPDX-License-Identifier: GPL-2.0-only
3 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
5 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
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>
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;
29 extern const u32 sha1_ce_offsetof_count;
30 extern const u32 sha1_ce_offsetof_finalize;
32 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
35 static void __sha1_ce_transform(struct sha1_state *sst, u8 const *src,
38 sha1_ce_transform(container_of(sst, struct sha1_ce_state, sst), src,
42 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
43 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
45 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
48 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
50 if (!crypto_simd_usable())
51 return crypto_sha1_update(desc, data, len);
55 sha1_base_do_update(desc, data, len, __sha1_ce_transform);
61 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
62 unsigned int len, u8 *out)
64 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
65 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
67 if (!crypto_simd_usable())
68 return crypto_sha1_finup(desc, data, len, out);
71 * Allow the asm code to perform the finalization if there is no
72 * partial data and the input is a round multiple of the block size.
74 sctx->finalize = finalize;
77 sha1_base_do_update(desc, data, len, __sha1_ce_transform);
79 sha1_base_do_finalize(desc, __sha1_ce_transform);
81 return sha1_base_finish(desc, out);
84 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
86 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
88 if (!crypto_simd_usable())
89 return crypto_sha1_finup(desc, NULL, 0, out);
93 sha1_base_do_finalize(desc, __sha1_ce_transform);
95 return sha1_base_finish(desc, out);
98 static int sha1_ce_export(struct shash_desc *desc, void *out)
100 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
102 memcpy(out, &sctx->sst, sizeof(struct sha1_state));
106 static int sha1_ce_import(struct shash_desc *desc, const void *in)
108 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
110 memcpy(&sctx->sst, in, sizeof(struct sha1_state));
115 static struct shash_alg alg = {
116 .init = sha1_base_init,
117 .update = sha1_ce_update,
118 .final = sha1_ce_final,
119 .finup = sha1_ce_finup,
120 .import = sha1_ce_import,
121 .export = sha1_ce_export,
122 .descsize = sizeof(struct sha1_ce_state),
123 .statesize = sizeof(struct sha1_state),
124 .digestsize = SHA1_DIGEST_SIZE,
127 .cra_driver_name = "sha1-ce",
129 .cra_blocksize = SHA1_BLOCK_SIZE,
130 .cra_module = THIS_MODULE,
134 static int __init sha1_ce_mod_init(void)
136 return crypto_register_shash(&alg);
139 static void __exit sha1_ce_mod_fini(void)
141 crypto_unregister_shash(&alg);
144 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
145 module_exit(sha1_ce_mod_fini);