1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SHA-1 and HMAC-SHA1 library functions
4 */
5
6 #include <crypto/hmac.h>
7 #include <crypto/sha1.h>
8 #include <linux/bitops.h>
9 #include <linux/export.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/string.h>
13 #include <linux/unaligned.h>
14 #include <linux/wordpart.h>
15
16 static const struct sha1_block_state sha1_iv = {
17 .h = { SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4 },
18 };
19
20 /*
21 * If you have 32 registers or more, the compiler can (and should)
22 * try to change the array[] accesses into registers. However, on
23 * machines with less than ~25 registers, that won't really work,
24 * and at least gcc will make an unholy mess of it.
25 *
26 * So to avoid that mess which just slows things down, we force
27 * the stores to memory to actually happen (we might be better off
28 * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
29 * suggested by Artur Skawina - that will also make gcc unable to
30 * try to do the silly "optimize away loads" part because it won't
31 * see what the value will be).
32 *
33 * Ben Herrenschmidt reports that on PPC, the C version comes close
34 * to the optimized asm with this (ie on PPC you don't want that
35 * 'volatile', since there are lots of registers).
36 *
37 * On ARM we get the best code generation by forcing a full memory barrier
38 * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
39 * the stack frame size simply explode and performance goes down the drain.
40 */
41
42 #ifdef CONFIG_X86
43 #define setW(x, val) (*(volatile __u32 *)&W(x) = (val))
44 #elif defined(CONFIG_ARM)
45 #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
46 #else
47 #define setW(x, val) (W(x) = (val))
48 #endif
49
50 /* This "rolls" over the 512-bit array */
51 #define W(x) (array[(x)&15])
52
53 /*
54 * Where do we get the source from? The first 16 iterations get it from
55 * the input data, the next mix it from the 512-bit array.
56 */
57 #define SHA_SRC(t) get_unaligned_be32((__u32 *)data + t)
58 #define SHA_MIX(t) rol32(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
59
60 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
61 __u32 TEMP = input(t); setW(t, TEMP); \
62 E += TEMP + rol32(A,5) + (fn) + (constant); \
63 B = ror32(B, 2); \
64 TEMP = E; E = D; D = C; C = B; B = A; A = TEMP; } while (0)
65
66 #define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
67 #define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
68 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
69 #define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
70 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
71
72 /**
73 * sha1_transform - single block SHA1 transform (deprecated)
74 *
75 * @digest: 160 bit digest to update
76 * @data: 512 bits of data to hash
77 * @array: 16 words of workspace (see note)
78 *
79 * This function executes SHA-1's internal compression function. It updates the
80 * 160-bit internal state (@digest) with a single 512-bit data block (@data).
81 *
82 * Don't use this function. SHA-1 is no longer considered secure. And even if
83 * you do have to use SHA-1, this isn't the correct way to hash something with
84 * SHA-1 as this doesn't handle padding and finalization.
85 *
86 * Note: If the hash is security sensitive, the caller should be sure
87 * to clear the workspace. This is left to the caller to avoid
88 * unnecessary clears between chained hashing operations.
89 */
sha1_transform(__u32 * digest,const char * data,__u32 * array)90 void sha1_transform(__u32 *digest, const char *data, __u32 *array)
91 {
92 __u32 A, B, C, D, E;
93 unsigned int i = 0;
94
95 A = digest[0];
96 B = digest[1];
97 C = digest[2];
98 D = digest[3];
99 E = digest[4];
100
101 /* Round 1 - iterations 0-16 take their input from 'data' */
102 for (; i < 16; ++i)
103 T_0_15(i, A, B, C, D, E);
104
105 /* Round 1 - tail. Input from 512-bit mixing array */
106 for (; i < 20; ++i)
107 T_16_19(i, A, B, C, D, E);
108
109 /* Round 2 */
110 for (; i < 40; ++i)
111 T_20_39(i, A, B, C, D, E);
112
113 /* Round 3 */
114 for (; i < 60; ++i)
115 T_40_59(i, A, B, C, D, E);
116
117 /* Round 4 */
118 for (; i < 80; ++i)
119 T_60_79(i, A, B, C, D, E);
120
121 digest[0] += A;
122 digest[1] += B;
123 digest[2] += C;
124 digest[3] += D;
125 digest[4] += E;
126 }
127 EXPORT_SYMBOL(sha1_transform);
128
129 /**
130 * sha1_init_raw - initialize the vectors for a SHA1 digest
131 * @buf: vector to initialize
132 */
sha1_init_raw(__u32 * buf)133 void sha1_init_raw(__u32 *buf)
134 {
135 buf[0] = 0x67452301;
136 buf[1] = 0xefcdab89;
137 buf[2] = 0x98badcfe;
138 buf[3] = 0x10325476;
139 buf[4] = 0xc3d2e1f0;
140 }
141 EXPORT_SYMBOL(sha1_init_raw);
142
sha1_blocks_generic(struct sha1_block_state * state,const u8 * data,size_t nblocks)143 static void __maybe_unused sha1_blocks_generic(struct sha1_block_state *state,
144 const u8 *data, size_t nblocks)
145 {
146 u32 workspace[SHA1_WORKSPACE_WORDS];
147
148 do {
149 sha1_transform(state->h, data, workspace);
150 data += SHA1_BLOCK_SIZE;
151 } while (--nblocks);
152
153 memzero_explicit(workspace, sizeof(workspace));
154 }
155
156 #ifdef CONFIG_CRYPTO_LIB_SHA1_ARCH
157 #include "sha1.h" /* $(SRCARCH)/sha1.h */
158 #else
159 #define sha1_blocks sha1_blocks_generic
160 #endif
161
sha1_init(struct sha1_ctx * ctx)162 void sha1_init(struct sha1_ctx *ctx)
163 {
164 ctx->state = sha1_iv;
165 ctx->bytecount = 0;
166 }
167 EXPORT_SYMBOL_GPL(sha1_init);
168
sha1_update(struct sha1_ctx * ctx,const u8 * data,size_t len)169 void sha1_update(struct sha1_ctx *ctx, const u8 *data, size_t len)
170 {
171 size_t partial = ctx->bytecount % SHA1_BLOCK_SIZE;
172
173 ctx->bytecount += len;
174
175 if (partial + len >= SHA1_BLOCK_SIZE) {
176 size_t nblocks;
177
178 if (partial) {
179 size_t l = SHA1_BLOCK_SIZE - partial;
180
181 memcpy(&ctx->buf[partial], data, l);
182 data += l;
183 len -= l;
184
185 sha1_blocks(&ctx->state, ctx->buf, 1);
186 }
187
188 nblocks = len / SHA1_BLOCK_SIZE;
189 len %= SHA1_BLOCK_SIZE;
190
191 if (nblocks) {
192 sha1_blocks(&ctx->state, data, nblocks);
193 data += nblocks * SHA1_BLOCK_SIZE;
194 }
195 partial = 0;
196 }
197 if (len)
198 memcpy(&ctx->buf[partial], data, len);
199 }
200 EXPORT_SYMBOL_GPL(sha1_update);
201
__sha1_final(struct sha1_ctx * ctx,u8 out[SHA1_DIGEST_SIZE])202 static void __sha1_final(struct sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
203 {
204 u64 bitcount = ctx->bytecount << 3;
205 size_t partial = ctx->bytecount % SHA1_BLOCK_SIZE;
206
207 ctx->buf[partial++] = 0x80;
208 if (partial > SHA1_BLOCK_SIZE - 8) {
209 memset(&ctx->buf[partial], 0, SHA1_BLOCK_SIZE - partial);
210 sha1_blocks(&ctx->state, ctx->buf, 1);
211 partial = 0;
212 }
213 memset(&ctx->buf[partial], 0, SHA1_BLOCK_SIZE - 8 - partial);
214 *(__be64 *)&ctx->buf[SHA1_BLOCK_SIZE - 8] = cpu_to_be64(bitcount);
215 sha1_blocks(&ctx->state, ctx->buf, 1);
216
217 for (size_t i = 0; i < SHA1_DIGEST_SIZE; i += 4)
218 put_unaligned_be32(ctx->state.h[i / 4], out + i);
219 }
220
sha1_final(struct sha1_ctx * ctx,u8 out[SHA1_DIGEST_SIZE])221 void sha1_final(struct sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
222 {
223 __sha1_final(ctx, out);
224 memzero_explicit(ctx, sizeof(*ctx));
225 }
226 EXPORT_SYMBOL_GPL(sha1_final);
227
sha1(const u8 * data,size_t len,u8 out[SHA1_DIGEST_SIZE])228 void sha1(const u8 *data, size_t len, u8 out[SHA1_DIGEST_SIZE])
229 {
230 struct sha1_ctx ctx;
231
232 sha1_init(&ctx);
233 sha1_update(&ctx, data, len);
234 sha1_final(&ctx, out);
235 }
236 EXPORT_SYMBOL_GPL(sha1);
237
__hmac_sha1_preparekey(struct sha1_block_state * istate,struct sha1_block_state * ostate,const u8 * raw_key,size_t raw_key_len)238 static void __hmac_sha1_preparekey(struct sha1_block_state *istate,
239 struct sha1_block_state *ostate,
240 const u8 *raw_key, size_t raw_key_len)
241 {
242 union {
243 u8 b[SHA1_BLOCK_SIZE];
244 unsigned long w[SHA1_BLOCK_SIZE / sizeof(unsigned long)];
245 } derived_key = { 0 };
246
247 if (unlikely(raw_key_len > SHA1_BLOCK_SIZE))
248 sha1(raw_key, raw_key_len, derived_key.b);
249 else
250 memcpy(derived_key.b, raw_key, raw_key_len);
251
252 for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
253 derived_key.w[i] ^= REPEAT_BYTE(HMAC_IPAD_VALUE);
254 *istate = sha1_iv;
255 sha1_blocks(istate, derived_key.b, 1);
256
257 for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
258 derived_key.w[i] ^= REPEAT_BYTE(HMAC_OPAD_VALUE ^
259 HMAC_IPAD_VALUE);
260 *ostate = sha1_iv;
261 sha1_blocks(ostate, derived_key.b, 1);
262
263 memzero_explicit(&derived_key, sizeof(derived_key));
264 }
265
hmac_sha1_preparekey(struct hmac_sha1_key * key,const u8 * raw_key,size_t raw_key_len)266 void hmac_sha1_preparekey(struct hmac_sha1_key *key,
267 const u8 *raw_key, size_t raw_key_len)
268 {
269 __hmac_sha1_preparekey(&key->istate, &key->ostate,
270 raw_key, raw_key_len);
271 }
272 EXPORT_SYMBOL_GPL(hmac_sha1_preparekey);
273
hmac_sha1_init(struct hmac_sha1_ctx * ctx,const struct hmac_sha1_key * key)274 void hmac_sha1_init(struct hmac_sha1_ctx *ctx, const struct hmac_sha1_key *key)
275 {
276 ctx->sha_ctx.state = key->istate;
277 ctx->sha_ctx.bytecount = SHA1_BLOCK_SIZE;
278 ctx->ostate = key->ostate;
279 }
280 EXPORT_SYMBOL_GPL(hmac_sha1_init);
281
hmac_sha1_init_usingrawkey(struct hmac_sha1_ctx * ctx,const u8 * raw_key,size_t raw_key_len)282 void hmac_sha1_init_usingrawkey(struct hmac_sha1_ctx *ctx,
283 const u8 *raw_key, size_t raw_key_len)
284 {
285 __hmac_sha1_preparekey(&ctx->sha_ctx.state, &ctx->ostate,
286 raw_key, raw_key_len);
287 ctx->sha_ctx.bytecount = SHA1_BLOCK_SIZE;
288 }
289 EXPORT_SYMBOL_GPL(hmac_sha1_init_usingrawkey);
290
hmac_sha1_final(struct hmac_sha1_ctx * ctx,u8 out[SHA1_DIGEST_SIZE])291 void hmac_sha1_final(struct hmac_sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
292 {
293 /* Generate the padded input for the outer hash in ctx->sha_ctx.buf. */
294 __sha1_final(&ctx->sha_ctx, ctx->sha_ctx.buf);
295 memset(&ctx->sha_ctx.buf[SHA1_DIGEST_SIZE], 0,
296 SHA1_BLOCK_SIZE - SHA1_DIGEST_SIZE);
297 ctx->sha_ctx.buf[SHA1_DIGEST_SIZE] = 0x80;
298 *(__be32 *)&ctx->sha_ctx.buf[SHA1_BLOCK_SIZE - 4] =
299 cpu_to_be32(8 * (SHA1_BLOCK_SIZE + SHA1_DIGEST_SIZE));
300
301 /* Compute the outer hash, which gives the HMAC value. */
302 sha1_blocks(&ctx->ostate, ctx->sha_ctx.buf, 1);
303 for (size_t i = 0; i < SHA1_DIGEST_SIZE; i += 4)
304 put_unaligned_be32(ctx->ostate.h[i / 4], out + i);
305
306 memzero_explicit(ctx, sizeof(*ctx));
307 }
308 EXPORT_SYMBOL_GPL(hmac_sha1_final);
309
hmac_sha1(const struct hmac_sha1_key * key,const u8 * data,size_t data_len,u8 out[SHA1_DIGEST_SIZE])310 void hmac_sha1(const struct hmac_sha1_key *key,
311 const u8 *data, size_t data_len, u8 out[SHA1_DIGEST_SIZE])
312 {
313 struct hmac_sha1_ctx ctx;
314
315 hmac_sha1_init(&ctx, key);
316 hmac_sha1_update(&ctx, data, data_len);
317 hmac_sha1_final(&ctx, out);
318 }
319 EXPORT_SYMBOL_GPL(hmac_sha1);
320
hmac_sha1_usingrawkey(const u8 * raw_key,size_t raw_key_len,const u8 * data,size_t data_len,u8 out[SHA1_DIGEST_SIZE])321 void hmac_sha1_usingrawkey(const u8 *raw_key, size_t raw_key_len,
322 const u8 *data, size_t data_len,
323 u8 out[SHA1_DIGEST_SIZE])
324 {
325 struct hmac_sha1_ctx ctx;
326
327 hmac_sha1_init_usingrawkey(&ctx, raw_key, raw_key_len);
328 hmac_sha1_update(&ctx, data, data_len);
329 hmac_sha1_final(&ctx, out);
330 }
331 EXPORT_SYMBOL_GPL(hmac_sha1_usingrawkey);
332
333 #ifdef sha1_mod_init_arch
sha1_mod_init(void)334 static int __init sha1_mod_init(void)
335 {
336 sha1_mod_init_arch();
337 return 0;
338 }
339 subsys_initcall(sha1_mod_init);
340
sha1_mod_exit(void)341 static void __exit sha1_mod_exit(void)
342 {
343 }
344 module_exit(sha1_mod_exit);
345 #endif
346
347 MODULE_DESCRIPTION("SHA-1 and HMAC-SHA1 library functions");
348 MODULE_LICENSE("GPL");
349