2 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
4 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
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/internal/simd.h>
16 #include <crypto/sha.h>
17 #include <crypto/sha1_base.h>
18 #include <linux/cpufeature.h>
19 #include <linux/crypto.h>
20 #include <linux/module.h>
22 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
26 struct sha1_ce_state {
27 struct sha1_state sst;
31 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
34 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
35 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
37 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
40 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
42 if (!crypto_simd_usable())
43 return crypto_sha1_update(desc, data, len);
47 sha1_base_do_update(desc, data, len,
48 (sha1_block_fn *)sha1_ce_transform);
54 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
55 unsigned int len, u8 *out)
57 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
58 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
60 if (!crypto_simd_usable())
61 return crypto_sha1_finup(desc, data, len, out);
64 * Allow the asm code to perform the finalization if there is no
65 * partial data and the input is a round multiple of the block size.
67 sctx->finalize = finalize;
70 sha1_base_do_update(desc, data, len,
71 (sha1_block_fn *)sha1_ce_transform);
73 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
75 return sha1_base_finish(desc, out);
78 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
80 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
82 if (!crypto_simd_usable())
83 return crypto_sha1_finup(desc, NULL, 0, out);
87 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
89 return sha1_base_finish(desc, out);
92 static struct shash_alg alg = {
93 .init = sha1_base_init,
94 .update = sha1_ce_update,
95 .final = sha1_ce_final,
96 .finup = sha1_ce_finup,
97 .descsize = sizeof(struct sha1_ce_state),
98 .digestsize = SHA1_DIGEST_SIZE,
101 .cra_driver_name = "sha1-ce",
103 .cra_blocksize = SHA1_BLOCK_SIZE,
104 .cra_module = THIS_MODULE,
108 static int __init sha1_ce_mod_init(void)
110 return crypto_register_shash(&alg);
113 static void __exit sha1_ce_mod_fini(void)
115 crypto_unregister_shash(&alg);
118 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
119 module_exit(sha1_ce_mod_fini);