xref: /linux/lib/crypto/sha512.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SHA-384, SHA-512, HMAC-SHA384, and HMAC-SHA512 library functions
4  *
5  * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
6  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
7  * Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
8  * Copyright 2025 Google LLC
9  */
10 
11 #include <crypto/hmac.h>
12 #include <crypto/sha2.h>
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/overflow.h>
17 #include <linux/string.h>
18 #include <linux/unaligned.h>
19 #include <linux/wordpart.h>
20 #include "fips.h"
21 
22 static const struct sha512_block_state sha384_iv = {
23 	.h = {
24 		SHA384_H0, SHA384_H1, SHA384_H2, SHA384_H3,
25 		SHA384_H4, SHA384_H5, SHA384_H6, SHA384_H7,
26 	},
27 };
28 
29 static const struct sha512_block_state sha512_iv = {
30 	.h = {
31 		SHA512_H0, SHA512_H1, SHA512_H2, SHA512_H3,
32 		SHA512_H4, SHA512_H5, SHA512_H6, SHA512_H7,
33 	},
34 };
35 
36 static const u64 sha512_K[80] = {
37 	0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
38 	0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
39 	0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
40 	0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
41 	0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
42 	0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
43 	0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL,
44 	0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
45 	0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
46 	0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
47 	0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL,
48 	0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
49 	0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,
50 	0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
51 	0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
52 	0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
53 	0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,
54 	0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
55 	0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL,
56 	0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
57 	0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
58 	0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
59 	0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL,
60 	0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
61 	0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
62 	0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
63 	0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL,
64 };
65 
66 #define Ch(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
67 #define Maj(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
68 #define e0(x) (ror64((x), 28) ^ ror64((x), 34) ^ ror64((x), 39))
69 #define e1(x) (ror64((x), 14) ^ ror64((x), 18) ^ ror64((x), 41))
70 #define s0(x) (ror64((x), 1) ^ ror64((x), 8) ^ ((x) >> 7))
71 #define s1(x) (ror64((x), 19) ^ ror64((x), 61) ^ ((x) >> 6))
72 
73 static void sha512_block_generic(struct sha512_block_state *state,
74 				 const u8 *data)
75 {
76 	u64 a = state->h[0];
77 	u64 b = state->h[1];
78 	u64 c = state->h[2];
79 	u64 d = state->h[3];
80 	u64 e = state->h[4];
81 	u64 f = state->h[5];
82 	u64 g = state->h[6];
83 	u64 h = state->h[7];
84 	u64 t1, t2;
85 	u64 W[16];
86 
87 	for (int j = 0; j < 16; j++)
88 		W[j] = get_unaligned_be64(data + j * sizeof(u64));
89 
90 	for (int i = 0; i < 80; i += 8) {
91 		if ((i & 15) == 0 && i != 0) {
92 			for (int j = 0; j < 16; j++) {
93 				W[j & 15] += s1(W[(j - 2) & 15]) +
94 					     W[(j - 7) & 15] +
95 					     s0(W[(j - 15) & 15]);
96 			}
97 		}
98 		t1 = h + e1(e) + Ch(e, f, g) + sha512_K[i]   + W[(i & 15)];
99 		t2 = e0(a) + Maj(a, b, c);    d += t1;    h = t1 + t2;
100 		t1 = g + e1(d) + Ch(d, e, f) + sha512_K[i+1] + W[(i & 15) + 1];
101 		t2 = e0(h) + Maj(h, a, b);    c += t1;    g = t1 + t2;
102 		t1 = f + e1(c) + Ch(c, d, e) + sha512_K[i+2] + W[(i & 15) + 2];
103 		t2 = e0(g) + Maj(g, h, a);    b += t1;    f = t1 + t2;
104 		t1 = e + e1(b) + Ch(b, c, d) + sha512_K[i+3] + W[(i & 15) + 3];
105 		t2 = e0(f) + Maj(f, g, h);    a += t1;    e = t1 + t2;
106 		t1 = d + e1(a) + Ch(a, b, c) + sha512_K[i+4] + W[(i & 15) + 4];
107 		t2 = e0(e) + Maj(e, f, g);    h += t1;    d = t1 + t2;
108 		t1 = c + e1(h) + Ch(h, a, b) + sha512_K[i+5] + W[(i & 15) + 5];
109 		t2 = e0(d) + Maj(d, e, f);    g += t1;    c = t1 + t2;
110 		t1 = b + e1(g) + Ch(g, h, a) + sha512_K[i+6] + W[(i & 15) + 6];
111 		t2 = e0(c) + Maj(c, d, e);    f += t1;    b = t1 + t2;
112 		t1 = a + e1(f) + Ch(f, g, h) + sha512_K[i+7] + W[(i & 15) + 7];
113 		t2 = e0(b) + Maj(b, c, d);    e += t1;    a = t1 + t2;
114 	}
115 
116 	state->h[0] += a;
117 	state->h[1] += b;
118 	state->h[2] += c;
119 	state->h[3] += d;
120 	state->h[4] += e;
121 	state->h[5] += f;
122 	state->h[6] += g;
123 	state->h[7] += h;
124 }
125 
126 static void __maybe_unused
127 sha512_blocks_generic(struct sha512_block_state *state,
128 		      const u8 *data, size_t nblocks)
129 {
130 	do {
131 		sha512_block_generic(state, data);
132 		data += SHA512_BLOCK_SIZE;
133 	} while (--nblocks);
134 }
135 
136 #ifdef CONFIG_CRYPTO_LIB_SHA512_ARCH
137 #include "sha512.h" /* $(SRCARCH)/sha512.h */
138 #else
139 #define sha512_blocks sha512_blocks_generic
140 #endif
141 
142 static void __sha512_init(struct __sha512_ctx *ctx,
143 			  const struct sha512_block_state *iv,
144 			  u64 initial_bytecount)
145 {
146 	ctx->state = *iv;
147 	ctx->bytecount_lo = initial_bytecount;
148 	ctx->bytecount_hi = 0;
149 }
150 
151 void sha384_init(struct sha384_ctx *ctx)
152 {
153 	__sha512_init(&ctx->ctx, &sha384_iv, 0);
154 }
155 EXPORT_SYMBOL_GPL(sha384_init);
156 
157 void sha512_init(struct sha512_ctx *ctx)
158 {
159 	__sha512_init(&ctx->ctx, &sha512_iv, 0);
160 }
161 EXPORT_SYMBOL_GPL(sha512_init);
162 
163 void __sha512_update(struct __sha512_ctx *ctx, const u8 *data, size_t len)
164 {
165 	size_t partial = ctx->bytecount_lo % SHA512_BLOCK_SIZE;
166 
167 	if (check_add_overflow(ctx->bytecount_lo, len, &ctx->bytecount_lo))
168 		ctx->bytecount_hi++;
169 
170 	if (partial + len >= SHA512_BLOCK_SIZE) {
171 		size_t nblocks;
172 
173 		if (partial) {
174 			size_t l = SHA512_BLOCK_SIZE - partial;
175 
176 			memcpy(&ctx->buf[partial], data, l);
177 			data += l;
178 			len -= l;
179 
180 			sha512_blocks(&ctx->state, ctx->buf, 1);
181 		}
182 
183 		nblocks = len / SHA512_BLOCK_SIZE;
184 		len %= SHA512_BLOCK_SIZE;
185 
186 		if (nblocks) {
187 			sha512_blocks(&ctx->state, data, nblocks);
188 			data += nblocks * SHA512_BLOCK_SIZE;
189 		}
190 		partial = 0;
191 	}
192 	if (len)
193 		memcpy(&ctx->buf[partial], data, len);
194 }
195 EXPORT_SYMBOL_GPL(__sha512_update);
196 
197 static void __sha512_final(struct __sha512_ctx *ctx,
198 			   u8 *out, size_t digest_size)
199 {
200 	u64 bitcount_hi = (ctx->bytecount_hi << 3) | (ctx->bytecount_lo >> 61);
201 	u64 bitcount_lo = ctx->bytecount_lo << 3;
202 	size_t partial = ctx->bytecount_lo % SHA512_BLOCK_SIZE;
203 
204 	ctx->buf[partial++] = 0x80;
205 	if (partial > SHA512_BLOCK_SIZE - 16) {
206 		memset(&ctx->buf[partial], 0, SHA512_BLOCK_SIZE - partial);
207 		sha512_blocks(&ctx->state, ctx->buf, 1);
208 		partial = 0;
209 	}
210 	memset(&ctx->buf[partial], 0, SHA512_BLOCK_SIZE - 16 - partial);
211 	*(__be64 *)&ctx->buf[SHA512_BLOCK_SIZE - 16] = cpu_to_be64(bitcount_hi);
212 	*(__be64 *)&ctx->buf[SHA512_BLOCK_SIZE - 8] = cpu_to_be64(bitcount_lo);
213 	sha512_blocks(&ctx->state, ctx->buf, 1);
214 
215 	for (size_t i = 0; i < digest_size; i += 8)
216 		put_unaligned_be64(ctx->state.h[i / 8], out + i);
217 }
218 
219 void sha384_final(struct sha384_ctx *ctx, u8 out[SHA384_DIGEST_SIZE])
220 {
221 	__sha512_final(&ctx->ctx, out, SHA384_DIGEST_SIZE);
222 	memzero_explicit(ctx, sizeof(*ctx));
223 }
224 EXPORT_SYMBOL_GPL(sha384_final);
225 
226 void sha512_final(struct sha512_ctx *ctx, u8 out[SHA512_DIGEST_SIZE])
227 {
228 	__sha512_final(&ctx->ctx, out, SHA512_DIGEST_SIZE);
229 	memzero_explicit(ctx, sizeof(*ctx));
230 }
231 EXPORT_SYMBOL_GPL(sha512_final);
232 
233 void sha384(const u8 *data, size_t len, u8 out[SHA384_DIGEST_SIZE])
234 {
235 	struct sha384_ctx ctx;
236 
237 	sha384_init(&ctx);
238 	sha384_update(&ctx, data, len);
239 	sha384_final(&ctx, out);
240 }
241 EXPORT_SYMBOL_GPL(sha384);
242 
243 void sha512(const u8 *data, size_t len, u8 out[SHA512_DIGEST_SIZE])
244 {
245 	struct sha512_ctx ctx;
246 
247 	sha512_init(&ctx);
248 	sha512_update(&ctx, data, len);
249 	sha512_final(&ctx, out);
250 }
251 EXPORT_SYMBOL_GPL(sha512);
252 
253 static void __hmac_sha512_preparekey(struct sha512_block_state *istate,
254 				     struct sha512_block_state *ostate,
255 				     const u8 *raw_key, size_t raw_key_len,
256 				     const struct sha512_block_state *iv)
257 {
258 	union {
259 		u8 b[SHA512_BLOCK_SIZE];
260 		unsigned long w[SHA512_BLOCK_SIZE / sizeof(unsigned long)];
261 	} derived_key = { 0 };
262 
263 	if (unlikely(raw_key_len > SHA512_BLOCK_SIZE)) {
264 		if (iv == &sha384_iv)
265 			sha384(raw_key, raw_key_len, derived_key.b);
266 		else
267 			sha512(raw_key, raw_key_len, derived_key.b);
268 	} else {
269 		memcpy(derived_key.b, raw_key, raw_key_len);
270 	}
271 
272 	for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
273 		derived_key.w[i] ^= REPEAT_BYTE(HMAC_IPAD_VALUE);
274 	*istate = *iv;
275 	sha512_blocks(istate, derived_key.b, 1);
276 
277 	for (size_t i = 0; i < ARRAY_SIZE(derived_key.w); i++)
278 		derived_key.w[i] ^= REPEAT_BYTE(HMAC_OPAD_VALUE ^
279 						HMAC_IPAD_VALUE);
280 	*ostate = *iv;
281 	sha512_blocks(ostate, derived_key.b, 1);
282 
283 	memzero_explicit(&derived_key, sizeof(derived_key));
284 }
285 
286 void hmac_sha384_preparekey(struct hmac_sha384_key *key,
287 			    const u8 *raw_key, size_t raw_key_len)
288 {
289 	__hmac_sha512_preparekey(&key->key.istate, &key->key.ostate,
290 				 raw_key, raw_key_len, &sha384_iv);
291 }
292 EXPORT_SYMBOL_GPL(hmac_sha384_preparekey);
293 
294 void hmac_sha512_preparekey(struct hmac_sha512_key *key,
295 			    const u8 *raw_key, size_t raw_key_len)
296 {
297 	__hmac_sha512_preparekey(&key->key.istate, &key->key.ostate,
298 				 raw_key, raw_key_len, &sha512_iv);
299 }
300 EXPORT_SYMBOL_GPL(hmac_sha512_preparekey);
301 
302 void __hmac_sha512_init(struct __hmac_sha512_ctx *ctx,
303 			const struct __hmac_sha512_key *key)
304 {
305 	__sha512_init(&ctx->sha_ctx, &key->istate, SHA512_BLOCK_SIZE);
306 	ctx->ostate = key->ostate;
307 }
308 EXPORT_SYMBOL_GPL(__hmac_sha512_init);
309 
310 void hmac_sha384_init_usingrawkey(struct hmac_sha384_ctx *ctx,
311 				  const u8 *raw_key, size_t raw_key_len)
312 {
313 	__hmac_sha512_preparekey(&ctx->ctx.sha_ctx.state, &ctx->ctx.ostate,
314 				 raw_key, raw_key_len, &sha384_iv);
315 	ctx->ctx.sha_ctx.bytecount_lo = SHA512_BLOCK_SIZE;
316 	ctx->ctx.sha_ctx.bytecount_hi = 0;
317 }
318 EXPORT_SYMBOL_GPL(hmac_sha384_init_usingrawkey);
319 
320 void hmac_sha512_init_usingrawkey(struct hmac_sha512_ctx *ctx,
321 				  const u8 *raw_key, size_t raw_key_len)
322 {
323 	__hmac_sha512_preparekey(&ctx->ctx.sha_ctx.state, &ctx->ctx.ostate,
324 				 raw_key, raw_key_len, &sha512_iv);
325 	ctx->ctx.sha_ctx.bytecount_lo = SHA512_BLOCK_SIZE;
326 	ctx->ctx.sha_ctx.bytecount_hi = 0;
327 }
328 EXPORT_SYMBOL_GPL(hmac_sha512_init_usingrawkey);
329 
330 static void __hmac_sha512_final(struct __hmac_sha512_ctx *ctx,
331 				u8 *out, size_t digest_size)
332 {
333 	/* Generate the padded input for the outer hash in ctx->sha_ctx.buf. */
334 	__sha512_final(&ctx->sha_ctx, ctx->sha_ctx.buf, digest_size);
335 	memset(&ctx->sha_ctx.buf[digest_size], 0,
336 	       SHA512_BLOCK_SIZE - digest_size);
337 	ctx->sha_ctx.buf[digest_size] = 0x80;
338 	*(__be32 *)&ctx->sha_ctx.buf[SHA512_BLOCK_SIZE - 4] =
339 		cpu_to_be32(8 * (SHA512_BLOCK_SIZE + digest_size));
340 
341 	/* Compute the outer hash, which gives the HMAC value. */
342 	sha512_blocks(&ctx->ostate, ctx->sha_ctx.buf, 1);
343 	for (size_t i = 0; i < digest_size; i += 8)
344 		put_unaligned_be64(ctx->ostate.h[i / 8], out + i);
345 
346 	memzero_explicit(ctx, sizeof(*ctx));
347 }
348 
349 void hmac_sha384_final(struct hmac_sha384_ctx *ctx,
350 		       u8 out[SHA384_DIGEST_SIZE])
351 {
352 	__hmac_sha512_final(&ctx->ctx, out, SHA384_DIGEST_SIZE);
353 }
354 EXPORT_SYMBOL_GPL(hmac_sha384_final);
355 
356 void hmac_sha512_final(struct hmac_sha512_ctx *ctx,
357 		       u8 out[SHA512_DIGEST_SIZE])
358 {
359 	__hmac_sha512_final(&ctx->ctx, out, SHA512_DIGEST_SIZE);
360 }
361 EXPORT_SYMBOL_GPL(hmac_sha512_final);
362 
363 void hmac_sha384(const struct hmac_sha384_key *key,
364 		 const u8 *data, size_t data_len, u8 out[SHA384_DIGEST_SIZE])
365 {
366 	struct hmac_sha384_ctx ctx;
367 
368 	hmac_sha384_init(&ctx, key);
369 	hmac_sha384_update(&ctx, data, data_len);
370 	hmac_sha384_final(&ctx, out);
371 }
372 EXPORT_SYMBOL_GPL(hmac_sha384);
373 
374 void hmac_sha512(const struct hmac_sha512_key *key,
375 		 const u8 *data, size_t data_len, u8 out[SHA512_DIGEST_SIZE])
376 {
377 	struct hmac_sha512_ctx ctx;
378 
379 	hmac_sha512_init(&ctx, key);
380 	hmac_sha512_update(&ctx, data, data_len);
381 	hmac_sha512_final(&ctx, out);
382 }
383 EXPORT_SYMBOL_GPL(hmac_sha512);
384 
385 void hmac_sha384_usingrawkey(const u8 *raw_key, size_t raw_key_len,
386 			     const u8 *data, size_t data_len,
387 			     u8 out[SHA384_DIGEST_SIZE])
388 {
389 	struct hmac_sha384_ctx ctx;
390 
391 	hmac_sha384_init_usingrawkey(&ctx, raw_key, raw_key_len);
392 	hmac_sha384_update(&ctx, data, data_len);
393 	hmac_sha384_final(&ctx, out);
394 }
395 EXPORT_SYMBOL_GPL(hmac_sha384_usingrawkey);
396 
397 void hmac_sha512_usingrawkey(const u8 *raw_key, size_t raw_key_len,
398 			     const u8 *data, size_t data_len,
399 			     u8 out[SHA512_DIGEST_SIZE])
400 {
401 	struct hmac_sha512_ctx ctx;
402 
403 	hmac_sha512_init_usingrawkey(&ctx, raw_key, raw_key_len);
404 	hmac_sha512_update(&ctx, data, data_len);
405 	hmac_sha512_final(&ctx, out);
406 }
407 EXPORT_SYMBOL_GPL(hmac_sha512_usingrawkey);
408 
409 #if defined(sha512_mod_init_arch) || defined(CONFIG_CRYPTO_FIPS)
410 static int __init sha512_mod_init(void)
411 {
412 #ifdef sha512_mod_init_arch
413 	sha512_mod_init_arch();
414 #endif
415 	if (fips_enabled) {
416 		/*
417 		 * FIPS cryptographic algorithm self-test.  As per the FIPS
418 		 * Implementation Guidance, testing HMAC-SHA512 satisfies the
419 		 * test requirement for SHA-384, SHA-512, and HMAC-SHA384 too.
420 		 */
421 		u8 mac[SHA512_DIGEST_SIZE];
422 
423 		hmac_sha512_usingrawkey(fips_test_key, sizeof(fips_test_key),
424 					fips_test_data, sizeof(fips_test_data),
425 					mac);
426 		if (memcmp(fips_test_hmac_sha512_value, mac, sizeof(mac)) != 0)
427 			panic("sha512: FIPS self-test failed\n");
428 	}
429 	return 0;
430 }
431 subsys_initcall(sha512_mod_init);
432 
433 static void __exit sha512_mod_exit(void)
434 {
435 }
436 module_exit(sha512_mod_exit);
437 #endif
438 
439 MODULE_DESCRIPTION("SHA-384, SHA-512, HMAC-SHA384, and HMAC-SHA512 library functions");
440 MODULE_LICENSE("GPL");
441