|
12 | 12 | #include <asm/unaligned.h> |
13 | 13 | #include <crypto/internal/hash.h> |
14 | 14 | #include <crypto/sha.h> |
| 15 | +#include <crypto/sha1_base.h> |
15 | 16 | #include <linux/cpufeature.h> |
16 | 17 | #include <linux/crypto.h> |
17 | 18 | #include <linux/module.h> |
18 | 19 |
|
| 20 | +#define ASM_EXPORT(sym, val) \ |
| 21 | + asm(".globl " #sym "; .set " #sym ", %0" :: "I"(val)); |
| 22 | + |
19 | 23 | MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions"); |
20 | 24 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); |
21 | 25 | MODULE_LICENSE("GPL v2"); |
22 | 26 |
|
23 | | -asmlinkage void sha1_ce_transform(int blocks, u8 const *src, u32 *state, |
24 | | - u8 *head, long bytes); |
| 27 | +struct sha1_ce_state { |
| 28 | + struct sha1_state sst; |
| 29 | + u32 finalize; |
| 30 | +}; |
25 | 31 |
|
26 | | -static int sha1_init(struct shash_desc *desc) |
27 | | -{ |
28 | | - struct sha1_state *sctx = shash_desc_ctx(desc); |
| 32 | +asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src, |
| 33 | + int blocks); |
29 | 34 |
|
30 | | - *sctx = (struct sha1_state){ |
31 | | - .state = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 }, |
32 | | - }; |
33 | | - return 0; |
34 | | -} |
35 | | - |
36 | | -static int sha1_update(struct shash_desc *desc, const u8 *data, |
37 | | - unsigned int len) |
| 35 | +static int sha1_ce_update(struct shash_desc *desc, const u8 *data, |
| 36 | + unsigned int len) |
38 | 37 | { |
39 | | - struct sha1_state *sctx = shash_desc_ctx(desc); |
40 | | - unsigned int partial = sctx->count % SHA1_BLOCK_SIZE; |
41 | | - |
42 | | - sctx->count += len; |
43 | | - |
44 | | - if ((partial + len) >= SHA1_BLOCK_SIZE) { |
45 | | - int blocks; |
46 | | - |
47 | | - if (partial) { |
48 | | - int p = SHA1_BLOCK_SIZE - partial; |
| 38 | + struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
49 | 39 |
|
50 | | - memcpy(sctx->buffer + partial, data, p); |
51 | | - data += p; |
52 | | - len -= p; |
53 | | - } |
54 | | - |
55 | | - blocks = len / SHA1_BLOCK_SIZE; |
56 | | - len %= SHA1_BLOCK_SIZE; |
57 | | - |
58 | | - kernel_neon_begin_partial(16); |
59 | | - sha1_ce_transform(blocks, data, sctx->state, |
60 | | - partial ? sctx->buffer : NULL, 0); |
61 | | - kernel_neon_end(); |
| 40 | + sctx->finalize = 0; |
| 41 | + kernel_neon_begin_partial(16); |
| 42 | + sha1_base_do_update(desc, data, len, |
| 43 | + (sha1_block_fn *)sha1_ce_transform); |
| 44 | + kernel_neon_end(); |
62 | 45 |
|
63 | | - data += blocks * SHA1_BLOCK_SIZE; |
64 | | - partial = 0; |
65 | | - } |
66 | | - if (len) |
67 | | - memcpy(sctx->buffer + partial, data, len); |
68 | 46 | return 0; |
69 | 47 | } |
70 | 48 |
|
71 | | -static int sha1_final(struct shash_desc *desc, u8 *out) |
| 49 | +static int sha1_ce_finup(struct shash_desc *desc, const u8 *data, |
| 50 | + unsigned int len, u8 *out) |
72 | 51 | { |
73 | | - static const u8 padding[SHA1_BLOCK_SIZE] = { 0x80, }; |
| 52 | + struct sha1_ce_state *sctx = shash_desc_ctx(desc); |
| 53 | + bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE); |
74 | 54 |
|
75 | | - struct sha1_state *sctx = shash_desc_ctx(desc); |
76 | | - __be64 bits = cpu_to_be64(sctx->count << 3); |
77 | | - __be32 *dst = (__be32 *)out; |
78 | | - int i; |
79 | | - |
80 | | - u32 padlen = SHA1_BLOCK_SIZE |
81 | | - - ((sctx->count + sizeof(bits)) % SHA1_BLOCK_SIZE); |
82 | | - |
83 | | - sha1_update(desc, padding, padlen); |
84 | | - sha1_update(desc, (const u8 *)&bits, sizeof(bits)); |
85 | | - |
86 | | - for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++) |
87 | | - put_unaligned_be32(sctx->state[i], dst++); |
88 | | - |
89 | | - *sctx = (struct sha1_state){}; |
90 | | - return 0; |
91 | | -} |
92 | | - |
93 | | -static int sha1_finup(struct shash_desc *desc, const u8 *data, |
94 | | - unsigned int len, u8 *out) |
95 | | -{ |
96 | | - struct sha1_state *sctx = shash_desc_ctx(desc); |
97 | | - __be32 *dst = (__be32 *)out; |
98 | | - int blocks; |
99 | | - int i; |
100 | | - |
101 | | - if (sctx->count || !len || (len % SHA1_BLOCK_SIZE)) { |
102 | | - sha1_update(desc, data, len); |
103 | | - return sha1_final(desc, out); |
104 | | - } |
| 55 | + ASM_EXPORT(sha1_ce_offsetof_count, |
| 56 | + offsetof(struct sha1_ce_state, sst.count)); |
| 57 | + ASM_EXPORT(sha1_ce_offsetof_finalize, |
| 58 | + offsetof(struct sha1_ce_state, finalize)); |
105 | 59 |
|
106 | 60 | /* |
107 | | - * Use a fast path if the input is a multiple of 64 bytes. In |
108 | | - * this case, there is no need to copy data around, and we can |
109 | | - * perform the entire digest calculation in a single invocation |
110 | | - * of sha1_ce_transform() |
| 61 | + * Allow the asm code to perform the finalization if there is no |
| 62 | + * partial data and the input is a round multiple of the block size. |
111 | 63 | */ |
112 | | - blocks = len / SHA1_BLOCK_SIZE; |
| 64 | + sctx->finalize = finalize; |
113 | 65 |
|
114 | 66 | kernel_neon_begin_partial(16); |
115 | | - sha1_ce_transform(blocks, data, sctx->state, NULL, len); |
| 67 | + sha1_base_do_update(desc, data, len, |
| 68 | + (sha1_block_fn *)sha1_ce_transform); |
| 69 | + if (!finalize) |
| 70 | + sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); |
116 | 71 | kernel_neon_end(); |
117 | | - |
118 | | - for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(__be32); i++) |
119 | | - put_unaligned_be32(sctx->state[i], dst++); |
120 | | - |
121 | | - *sctx = (struct sha1_state){}; |
122 | | - return 0; |
| 72 | + return sha1_base_finish(desc, out); |
123 | 73 | } |
124 | 74 |
|
125 | | -static int sha1_export(struct shash_desc *desc, void *out) |
| 75 | +static int sha1_ce_final(struct shash_desc *desc, u8 *out) |
126 | 76 | { |
127 | | - struct sha1_state *sctx = shash_desc_ctx(desc); |
128 | | - struct sha1_state *dst = out; |
129 | | - |
130 | | - *dst = *sctx; |
131 | | - return 0; |
132 | | -} |
133 | | - |
134 | | -static int sha1_import(struct shash_desc *desc, const void *in) |
135 | | -{ |
136 | | - struct sha1_state *sctx = shash_desc_ctx(desc); |
137 | | - struct sha1_state const *src = in; |
138 | | - |
139 | | - *sctx = *src; |
140 | | - return 0; |
| 77 | + kernel_neon_begin_partial(16); |
| 78 | + sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform); |
| 79 | + kernel_neon_end(); |
| 80 | + return sha1_base_finish(desc, out); |
141 | 81 | } |
142 | 82 |
|
143 | 83 | static struct shash_alg alg = { |
144 | | - .init = sha1_init, |
145 | | - .update = sha1_update, |
146 | | - .final = sha1_final, |
147 | | - .finup = sha1_finup, |
148 | | - .export = sha1_export, |
149 | | - .import = sha1_import, |
150 | | - .descsize = sizeof(struct sha1_state), |
| 84 | + .init = sha1_base_init, |
| 85 | + .update = sha1_ce_update, |
| 86 | + .final = sha1_ce_final, |
| 87 | + .finup = sha1_ce_finup, |
| 88 | + .descsize = sizeof(struct sha1_ce_state), |
151 | 89 | .digestsize = SHA1_DIGEST_SIZE, |
152 | | - .statesize = sizeof(struct sha1_state), |
153 | 90 | .base = { |
154 | 91 | .cra_name = "sha1", |
155 | 92 | .cra_driver_name = "sha1-ce", |
|
0 commit comments