sha512.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto API support for SHA-384, SHA-512, HMAC-SHA384, and HMAC-SHA512
  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/internal/hash.h>
  11. #include <crypto/sha2.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. /*
  15. * Export and import functions. crypto_shash wants a particular format that
  16. * matches that used by some legacy drivers. It currently is the same as the
  17. * library SHA context, except the value in bytecount_lo must be block-aligned
  18. * and the remainder must be stored in an extra u8 appended to the struct.
  19. */
  20. #define SHA512_SHASH_STATE_SIZE 209
  21. static_assert(offsetof(struct __sha512_ctx, state) == 0);
  22. static_assert(offsetof(struct __sha512_ctx, bytecount_lo) == 64);
  23. static_assert(offsetof(struct __sha512_ctx, bytecount_hi) == 72);
  24. static_assert(offsetof(struct __sha512_ctx, buf) == 80);
  25. static_assert(sizeof(struct __sha512_ctx) + 1 == SHA512_SHASH_STATE_SIZE);
  26. static int __crypto_sha512_export(const struct __sha512_ctx *ctx0, void *out)
  27. {
  28. struct __sha512_ctx ctx = *ctx0;
  29. unsigned int partial;
  30. u8 *p = out;
  31. partial = ctx.bytecount_lo % SHA512_BLOCK_SIZE;
  32. ctx.bytecount_lo -= partial;
  33. memcpy(p, &ctx, sizeof(ctx));
  34. p += sizeof(ctx);
  35. *p = partial;
  36. return 0;
  37. }
  38. static int __crypto_sha512_import(struct __sha512_ctx *ctx, const void *in)
  39. {
  40. const u8 *p = in;
  41. memcpy(ctx, p, sizeof(*ctx));
  42. p += sizeof(*ctx);
  43. ctx->bytecount_lo += *p;
  44. return 0;
  45. }
  46. static int __crypto_sha512_export_core(const struct __sha512_ctx *ctx,
  47. void *out)
  48. {
  49. memcpy(out, ctx, offsetof(struct __sha512_ctx, buf));
  50. return 0;
  51. }
  52. static int __crypto_sha512_import_core(struct __sha512_ctx *ctx, const void *in)
  53. {
  54. memcpy(ctx, in, offsetof(struct __sha512_ctx, buf));
  55. return 0;
  56. }
  57. /* SHA-384 */
  58. const u8 sha384_zero_message_hash[SHA384_DIGEST_SIZE] = {
  59. 0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38,
  60. 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
  61. 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
  62. 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
  63. 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb,
  64. 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b
  65. };
  66. EXPORT_SYMBOL_GPL(sha384_zero_message_hash);
  67. #define SHA384_CTX(desc) ((struct sha384_ctx *)shash_desc_ctx(desc))
  68. static int crypto_sha384_init(struct shash_desc *desc)
  69. {
  70. sha384_init(SHA384_CTX(desc));
  71. return 0;
  72. }
  73. static int crypto_sha384_update(struct shash_desc *desc,
  74. const u8 *data, unsigned int len)
  75. {
  76. sha384_update(SHA384_CTX(desc), data, len);
  77. return 0;
  78. }
  79. static int crypto_sha384_final(struct shash_desc *desc, u8 *out)
  80. {
  81. sha384_final(SHA384_CTX(desc), out);
  82. return 0;
  83. }
  84. static int crypto_sha384_digest(struct shash_desc *desc,
  85. const u8 *data, unsigned int len, u8 *out)
  86. {
  87. sha384(data, len, out);
  88. return 0;
  89. }
  90. static int crypto_sha384_export(struct shash_desc *desc, void *out)
  91. {
  92. return __crypto_sha512_export(&SHA384_CTX(desc)->ctx, out);
  93. }
  94. static int crypto_sha384_import(struct shash_desc *desc, const void *in)
  95. {
  96. return __crypto_sha512_import(&SHA384_CTX(desc)->ctx, in);
  97. }
  98. static int crypto_sha384_export_core(struct shash_desc *desc, void *out)
  99. {
  100. return __crypto_sha512_export_core(&SHA384_CTX(desc)->ctx, out);
  101. }
  102. static int crypto_sha384_import_core(struct shash_desc *desc, const void *in)
  103. {
  104. return __crypto_sha512_import_core(&SHA384_CTX(desc)->ctx, in);
  105. }
  106. /* SHA-512 */
  107. const u8 sha512_zero_message_hash[SHA512_DIGEST_SIZE] = {
  108. 0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd,
  109. 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
  110. 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc,
  111. 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
  112. 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0,
  113. 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
  114. 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81,
  115. 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e
  116. };
  117. EXPORT_SYMBOL_GPL(sha512_zero_message_hash);
  118. #define SHA512_CTX(desc) ((struct sha512_ctx *)shash_desc_ctx(desc))
  119. static int crypto_sha512_init(struct shash_desc *desc)
  120. {
  121. sha512_init(SHA512_CTX(desc));
  122. return 0;
  123. }
  124. static int crypto_sha512_update(struct shash_desc *desc,
  125. const u8 *data, unsigned int len)
  126. {
  127. sha512_update(SHA512_CTX(desc), data, len);
  128. return 0;
  129. }
  130. static int crypto_sha512_final(struct shash_desc *desc, u8 *out)
  131. {
  132. sha512_final(SHA512_CTX(desc), out);
  133. return 0;
  134. }
  135. static int crypto_sha512_digest(struct shash_desc *desc,
  136. const u8 *data, unsigned int len, u8 *out)
  137. {
  138. sha512(data, len, out);
  139. return 0;
  140. }
  141. static int crypto_sha512_export(struct shash_desc *desc, void *out)
  142. {
  143. return __crypto_sha512_export(&SHA512_CTX(desc)->ctx, out);
  144. }
  145. static int crypto_sha512_import(struct shash_desc *desc, const void *in)
  146. {
  147. return __crypto_sha512_import(&SHA512_CTX(desc)->ctx, in);
  148. }
  149. static int crypto_sha512_export_core(struct shash_desc *desc, void *out)
  150. {
  151. return __crypto_sha512_export_core(&SHA512_CTX(desc)->ctx, out);
  152. }
  153. static int crypto_sha512_import_core(struct shash_desc *desc, const void *in)
  154. {
  155. return __crypto_sha512_import_core(&SHA512_CTX(desc)->ctx, in);
  156. }
  157. /* HMAC-SHA384 */
  158. #define HMAC_SHA384_KEY(tfm) ((struct hmac_sha384_key *)crypto_shash_ctx(tfm))
  159. #define HMAC_SHA384_CTX(desc) ((struct hmac_sha384_ctx *)shash_desc_ctx(desc))
  160. static int crypto_hmac_sha384_setkey(struct crypto_shash *tfm,
  161. const u8 *raw_key, unsigned int keylen)
  162. {
  163. hmac_sha384_preparekey(HMAC_SHA384_KEY(tfm), raw_key, keylen);
  164. return 0;
  165. }
  166. static int crypto_hmac_sha384_init(struct shash_desc *desc)
  167. {
  168. hmac_sha384_init(HMAC_SHA384_CTX(desc), HMAC_SHA384_KEY(desc->tfm));
  169. return 0;
  170. }
  171. static int crypto_hmac_sha384_update(struct shash_desc *desc,
  172. const u8 *data, unsigned int len)
  173. {
  174. hmac_sha384_update(HMAC_SHA384_CTX(desc), data, len);
  175. return 0;
  176. }
  177. static int crypto_hmac_sha384_final(struct shash_desc *desc, u8 *out)
  178. {
  179. hmac_sha384_final(HMAC_SHA384_CTX(desc), out);
  180. return 0;
  181. }
  182. static int crypto_hmac_sha384_digest(struct shash_desc *desc,
  183. const u8 *data, unsigned int len,
  184. u8 *out)
  185. {
  186. hmac_sha384(HMAC_SHA384_KEY(desc->tfm), data, len, out);
  187. return 0;
  188. }
  189. static int crypto_hmac_sha384_export(struct shash_desc *desc, void *out)
  190. {
  191. return __crypto_sha512_export(&HMAC_SHA384_CTX(desc)->ctx.sha_ctx, out);
  192. }
  193. static int crypto_hmac_sha384_import(struct shash_desc *desc, const void *in)
  194. {
  195. struct hmac_sha384_ctx *ctx = HMAC_SHA384_CTX(desc);
  196. ctx->ctx.ostate = HMAC_SHA384_KEY(desc->tfm)->key.ostate;
  197. return __crypto_sha512_import(&ctx->ctx.sha_ctx, in);
  198. }
  199. static int crypto_hmac_sha384_export_core(struct shash_desc *desc, void *out)
  200. {
  201. return __crypto_sha512_export_core(&HMAC_SHA384_CTX(desc)->ctx.sha_ctx,
  202. out);
  203. }
  204. static int crypto_hmac_sha384_import_core(struct shash_desc *desc,
  205. const void *in)
  206. {
  207. struct hmac_sha384_ctx *ctx = HMAC_SHA384_CTX(desc);
  208. ctx->ctx.ostate = HMAC_SHA384_KEY(desc->tfm)->key.ostate;
  209. return __crypto_sha512_import_core(&ctx->ctx.sha_ctx, in);
  210. }
  211. /* HMAC-SHA512 */
  212. #define HMAC_SHA512_KEY(tfm) ((struct hmac_sha512_key *)crypto_shash_ctx(tfm))
  213. #define HMAC_SHA512_CTX(desc) ((struct hmac_sha512_ctx *)shash_desc_ctx(desc))
  214. static int crypto_hmac_sha512_setkey(struct crypto_shash *tfm,
  215. const u8 *raw_key, unsigned int keylen)
  216. {
  217. hmac_sha512_preparekey(HMAC_SHA512_KEY(tfm), raw_key, keylen);
  218. return 0;
  219. }
  220. static int crypto_hmac_sha512_init(struct shash_desc *desc)
  221. {
  222. hmac_sha512_init(HMAC_SHA512_CTX(desc), HMAC_SHA512_KEY(desc->tfm));
  223. return 0;
  224. }
  225. static int crypto_hmac_sha512_update(struct shash_desc *desc,
  226. const u8 *data, unsigned int len)
  227. {
  228. hmac_sha512_update(HMAC_SHA512_CTX(desc), data, len);
  229. return 0;
  230. }
  231. static int crypto_hmac_sha512_final(struct shash_desc *desc, u8 *out)
  232. {
  233. hmac_sha512_final(HMAC_SHA512_CTX(desc), out);
  234. return 0;
  235. }
  236. static int crypto_hmac_sha512_digest(struct shash_desc *desc,
  237. const u8 *data, unsigned int len,
  238. u8 *out)
  239. {
  240. hmac_sha512(HMAC_SHA512_KEY(desc->tfm), data, len, out);
  241. return 0;
  242. }
  243. static int crypto_hmac_sha512_export(struct shash_desc *desc, void *out)
  244. {
  245. return __crypto_sha512_export(&HMAC_SHA512_CTX(desc)->ctx.sha_ctx, out);
  246. }
  247. static int crypto_hmac_sha512_import(struct shash_desc *desc, const void *in)
  248. {
  249. struct hmac_sha512_ctx *ctx = HMAC_SHA512_CTX(desc);
  250. ctx->ctx.ostate = HMAC_SHA512_KEY(desc->tfm)->key.ostate;
  251. return __crypto_sha512_import(&ctx->ctx.sha_ctx, in);
  252. }
  253. static int crypto_hmac_sha512_export_core(struct shash_desc *desc, void *out)
  254. {
  255. return __crypto_sha512_export_core(&HMAC_SHA512_CTX(desc)->ctx.sha_ctx,
  256. out);
  257. }
  258. static int crypto_hmac_sha512_import_core(struct shash_desc *desc,
  259. const void *in)
  260. {
  261. struct hmac_sha512_ctx *ctx = HMAC_SHA512_CTX(desc);
  262. ctx->ctx.ostate = HMAC_SHA512_KEY(desc->tfm)->key.ostate;
  263. return __crypto_sha512_import_core(&ctx->ctx.sha_ctx, in);
  264. }
  265. /* Algorithm definitions */
  266. static struct shash_alg algs[] = {
  267. {
  268. .base.cra_name = "sha384",
  269. .base.cra_driver_name = "sha384-lib",
  270. .base.cra_priority = 300,
  271. .base.cra_blocksize = SHA384_BLOCK_SIZE,
  272. .base.cra_module = THIS_MODULE,
  273. .digestsize = SHA384_DIGEST_SIZE,
  274. .init = crypto_sha384_init,
  275. .update = crypto_sha384_update,
  276. .final = crypto_sha384_final,
  277. .digest = crypto_sha384_digest,
  278. .export = crypto_sha384_export,
  279. .import = crypto_sha384_import,
  280. .export_core = crypto_sha384_export_core,
  281. .import_core = crypto_sha384_import_core,
  282. .descsize = sizeof(struct sha384_ctx),
  283. .statesize = SHA512_SHASH_STATE_SIZE,
  284. },
  285. {
  286. .base.cra_name = "sha512",
  287. .base.cra_driver_name = "sha512-lib",
  288. .base.cra_priority = 300,
  289. .base.cra_blocksize = SHA512_BLOCK_SIZE,
  290. .base.cra_module = THIS_MODULE,
  291. .digestsize = SHA512_DIGEST_SIZE,
  292. .init = crypto_sha512_init,
  293. .update = crypto_sha512_update,
  294. .final = crypto_sha512_final,
  295. .digest = crypto_sha512_digest,
  296. .export = crypto_sha512_export,
  297. .import = crypto_sha512_import,
  298. .export_core = crypto_sha512_export_core,
  299. .import_core = crypto_sha512_import_core,
  300. .descsize = sizeof(struct sha512_ctx),
  301. .statesize = SHA512_SHASH_STATE_SIZE,
  302. },
  303. {
  304. .base.cra_name = "hmac(sha384)",
  305. .base.cra_driver_name = "hmac-sha384-lib",
  306. .base.cra_priority = 300,
  307. .base.cra_blocksize = SHA384_BLOCK_SIZE,
  308. .base.cra_ctxsize = sizeof(struct hmac_sha384_key),
  309. .base.cra_module = THIS_MODULE,
  310. .digestsize = SHA384_DIGEST_SIZE,
  311. .setkey = crypto_hmac_sha384_setkey,
  312. .init = crypto_hmac_sha384_init,
  313. .update = crypto_hmac_sha384_update,
  314. .final = crypto_hmac_sha384_final,
  315. .digest = crypto_hmac_sha384_digest,
  316. .export = crypto_hmac_sha384_export,
  317. .import = crypto_hmac_sha384_import,
  318. .export_core = crypto_hmac_sha384_export_core,
  319. .import_core = crypto_hmac_sha384_import_core,
  320. .descsize = sizeof(struct hmac_sha384_ctx),
  321. .statesize = SHA512_SHASH_STATE_SIZE,
  322. },
  323. {
  324. .base.cra_name = "hmac(sha512)",
  325. .base.cra_driver_name = "hmac-sha512-lib",
  326. .base.cra_priority = 300,
  327. .base.cra_blocksize = SHA512_BLOCK_SIZE,
  328. .base.cra_ctxsize = sizeof(struct hmac_sha512_key),
  329. .base.cra_module = THIS_MODULE,
  330. .digestsize = SHA512_DIGEST_SIZE,
  331. .setkey = crypto_hmac_sha512_setkey,
  332. .init = crypto_hmac_sha512_init,
  333. .update = crypto_hmac_sha512_update,
  334. .final = crypto_hmac_sha512_final,
  335. .digest = crypto_hmac_sha512_digest,
  336. .export = crypto_hmac_sha512_export,
  337. .import = crypto_hmac_sha512_import,
  338. .export_core = crypto_hmac_sha512_export_core,
  339. .import_core = crypto_hmac_sha512_import_core,
  340. .descsize = sizeof(struct hmac_sha512_ctx),
  341. .statesize = SHA512_SHASH_STATE_SIZE,
  342. },
  343. };
  344. static int __init crypto_sha512_mod_init(void)
  345. {
  346. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  347. }
  348. module_init(crypto_sha512_mod_init);
  349. static void __exit crypto_sha512_mod_exit(void)
  350. {
  351. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  352. }
  353. module_exit(crypto_sha512_mod_exit);
  354. MODULE_LICENSE("GPL");
  355. MODULE_DESCRIPTION("Crypto API support for SHA-384, SHA-512, HMAC-SHA384, and HMAC-SHA512");
  356. MODULE_ALIAS_CRYPTO("sha384");
  357. MODULE_ALIAS_CRYPTO("sha384-lib");
  358. MODULE_ALIAS_CRYPTO("sha512");
  359. MODULE_ALIAS_CRYPTO("sha512-lib");
  360. MODULE_ALIAS_CRYPTO("hmac(sha384)");
  361. MODULE_ALIAS_CRYPTO("hmac-sha384-lib");
  362. MODULE_ALIAS_CRYPTO("hmac(sha512)");
  363. MODULE_ALIAS_CRYPTO("hmac-sha512-lib");