sha512.c 13 KB

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