2 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 #include <asm/unaligned.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/sha.h>
16 #include <crypto/sha1_base.h>
17 #include <linux/cpufeature.h>
18 #include <linux/crypto.h>
19 #include <linux/module.h>
21 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
23 MODULE_LICENSE("GPL v2");
25 struct sha1_ce_state {
26 struct sha1_state sst;
30 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
33 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
34 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
36 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
39 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
42 return crypto_sha1_update(desc, data, len);
46 sha1_base_do_update(desc, data, len,
47 (sha1_block_fn *)sha1_ce_transform);
53 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
54 unsigned int len, u8 *out)
56 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
57 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
60 return crypto_sha1_finup(desc, data, len, out);
63 * Allow the asm code to perform the finalization if there is no
64 * partial data and the input is a round multiple of the block size.
66 sctx->finalize = finalize;
69 sha1_base_do_update(desc, data, len,
70 (sha1_block_fn *)sha1_ce_transform);
72 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
74 return sha1_base_finish(desc, out);
77 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
79 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
82 return crypto_sha1_finup(desc, NULL, 0, out);
86 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
88 return sha1_base_finish(desc, out);
91 static struct shash_alg alg = {
92 .init = sha1_base_init,
93 .update = sha1_ce_update,
94 .final = sha1_ce_final,
95 .finup = sha1_ce_finup,
96 .descsize = sizeof(struct sha1_ce_state),
97 .digestsize = SHA1_DIGEST_SIZE,
100 .cra_driver_name = "sha1-ce",
102 .cra_blocksize = SHA1_BLOCK_SIZE,
103 .cra_module = THIS_MODULE,
107 static int __init sha1_ce_mod_init(void)
109 return crypto_register_shash(&alg);
112 static void __exit sha1_ce_mod_fini(void)
114 crypto_unregister_shash(&alg);
117 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
118 module_exit(sha1_ce_mod_fini);