1
2
3
4
5
6
7
8
9
10
11#include <asm/neon.h>
12#include <asm/simd.h>
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>
20
21MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
22MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23MODULE_LICENSE("GPL v2");
24
25struct sha1_ce_state {
26 struct sha1_state sst;
27 u32 finalize;
28};
29
30asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
31 int blocks);
32
33const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
34const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
35
36static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
37 unsigned int len)
38{
39 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
40
41 if (!may_use_simd())
42 return crypto_sha1_update(desc, data, len);
43
44 sctx->finalize = 0;
45 kernel_neon_begin();
46 sha1_base_do_update(desc, data, len,
47 (sha1_block_fn *)sha1_ce_transform);
48 kernel_neon_end();
49
50 return 0;
51}
52
53static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
54 unsigned int len, u8 *out)
55{
56 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
57 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
58
59 if (!may_use_simd())
60 return crypto_sha1_finup(desc, data, len, out);
61
62
63
64
65
66 sctx->finalize = finalize;
67
68 kernel_neon_begin();
69 sha1_base_do_update(desc, data, len,
70 (sha1_block_fn *)sha1_ce_transform);
71 if (!finalize)
72 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
73 kernel_neon_end();
74 return sha1_base_finish(desc, out);
75}
76
77static int sha1_ce_final(struct shash_desc *desc, u8 *out)
78{
79 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
80
81 if (!may_use_simd())
82 return crypto_sha1_finup(desc, NULL, 0, out);
83
84 sctx->finalize = 0;
85 kernel_neon_begin();
86 sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
87 kernel_neon_end();
88 return sha1_base_finish(desc, out);
89}
90
91static 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,
98 .base = {
99 .cra_name = "sha1",
100 .cra_driver_name = "sha1-ce",
101 .cra_priority = 200,
102 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
103 .cra_blocksize = SHA1_BLOCK_SIZE,
104 .cra_module = THIS_MODULE,
105 }
106};
107
108static int __init sha1_ce_mod_init(void)
109{
110 return crypto_register_shash(&alg);
111}
112
113static void __exit sha1_ce_mod_fini(void)
114{
115 crypto_unregister_shash(&alg);
116}
117
118module_cpu_feature_match(SHA1, sha1_ce_mod_init);
119module_exit(sha1_ce_mod_fini);
120