md5.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Crypto API support for MD5 and HMAC-MD5
  4. *
  5. * Copyright 2025 Google LLC
  6. */
  7. #include <crypto/internal/hash.h>
  8. #include <crypto/md5.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. /*
  12. * Export and import functions. crypto_shash wants a particular format that
  13. * matches that used by some legacy drivers. It currently is the same as the
  14. * library MD5 context, except the value in bytecount must be block-aligned and
  15. * the remainder must be stored in an extra u8 appended to the struct.
  16. */
  17. #define MD5_SHASH_STATE_SIZE (sizeof(struct md5_ctx) + 1)
  18. static_assert(sizeof(struct md5_ctx) == sizeof(struct md5_state));
  19. static_assert(offsetof(struct md5_ctx, state) == offsetof(struct md5_state, hash));
  20. static_assert(offsetof(struct md5_ctx, bytecount) == offsetof(struct md5_state, byte_count));
  21. static_assert(offsetof(struct md5_ctx, buf) == offsetof(struct md5_state, block));
  22. static int __crypto_md5_export(const struct md5_ctx *ctx0, void *out)
  23. {
  24. struct md5_ctx ctx = *ctx0;
  25. unsigned int partial;
  26. u8 *p = out;
  27. partial = ctx.bytecount % MD5_BLOCK_SIZE;
  28. ctx.bytecount -= partial;
  29. memcpy(p, &ctx, sizeof(ctx));
  30. p += sizeof(ctx);
  31. *p = partial;
  32. return 0;
  33. }
  34. static int __crypto_md5_import(struct md5_ctx *ctx, const void *in)
  35. {
  36. const u8 *p = in;
  37. memcpy(ctx, p, sizeof(*ctx));
  38. p += sizeof(*ctx);
  39. ctx->bytecount += *p;
  40. return 0;
  41. }
  42. static int __crypto_md5_export_core(const struct md5_ctx *ctx, void *out)
  43. {
  44. memcpy(out, ctx, offsetof(struct md5_ctx, buf));
  45. return 0;
  46. }
  47. static int __crypto_md5_import_core(struct md5_ctx *ctx, const void *in)
  48. {
  49. memcpy(ctx, in, offsetof(struct md5_ctx, buf));
  50. return 0;
  51. }
  52. const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = {
  53. 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
  54. 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
  55. };
  56. EXPORT_SYMBOL_GPL(md5_zero_message_hash);
  57. #define MD5_CTX(desc) ((struct md5_ctx *)shash_desc_ctx(desc))
  58. static int crypto_md5_init(struct shash_desc *desc)
  59. {
  60. md5_init(MD5_CTX(desc));
  61. return 0;
  62. }
  63. static int crypto_md5_update(struct shash_desc *desc,
  64. const u8 *data, unsigned int len)
  65. {
  66. md5_update(MD5_CTX(desc), data, len);
  67. return 0;
  68. }
  69. static int crypto_md5_final(struct shash_desc *desc, u8 *out)
  70. {
  71. md5_final(MD5_CTX(desc), out);
  72. return 0;
  73. }
  74. static int crypto_md5_digest(struct shash_desc *desc,
  75. const u8 *data, unsigned int len, u8 *out)
  76. {
  77. md5(data, len, out);
  78. return 0;
  79. }
  80. static int crypto_md5_export(struct shash_desc *desc, void *out)
  81. {
  82. return __crypto_md5_export(MD5_CTX(desc), out);
  83. }
  84. static int crypto_md5_import(struct shash_desc *desc, const void *in)
  85. {
  86. return __crypto_md5_import(MD5_CTX(desc), in);
  87. }
  88. static int crypto_md5_export_core(struct shash_desc *desc, void *out)
  89. {
  90. return __crypto_md5_export_core(MD5_CTX(desc), out);
  91. }
  92. static int crypto_md5_import_core(struct shash_desc *desc, const void *in)
  93. {
  94. return __crypto_md5_import_core(MD5_CTX(desc), in);
  95. }
  96. #define HMAC_MD5_KEY(tfm) ((struct hmac_md5_key *)crypto_shash_ctx(tfm))
  97. #define HMAC_MD5_CTX(desc) ((struct hmac_md5_ctx *)shash_desc_ctx(desc))
  98. static int crypto_hmac_md5_setkey(struct crypto_shash *tfm,
  99. const u8 *raw_key, unsigned int keylen)
  100. {
  101. hmac_md5_preparekey(HMAC_MD5_KEY(tfm), raw_key, keylen);
  102. return 0;
  103. }
  104. static int crypto_hmac_md5_init(struct shash_desc *desc)
  105. {
  106. hmac_md5_init(HMAC_MD5_CTX(desc), HMAC_MD5_KEY(desc->tfm));
  107. return 0;
  108. }
  109. static int crypto_hmac_md5_update(struct shash_desc *desc,
  110. const u8 *data, unsigned int len)
  111. {
  112. hmac_md5_update(HMAC_MD5_CTX(desc), data, len);
  113. return 0;
  114. }
  115. static int crypto_hmac_md5_final(struct shash_desc *desc, u8 *out)
  116. {
  117. hmac_md5_final(HMAC_MD5_CTX(desc), out);
  118. return 0;
  119. }
  120. static int crypto_hmac_md5_digest(struct shash_desc *desc,
  121. const u8 *data, unsigned int len, u8 *out)
  122. {
  123. hmac_md5(HMAC_MD5_KEY(desc->tfm), data, len, out);
  124. return 0;
  125. }
  126. static int crypto_hmac_md5_export(struct shash_desc *desc, void *out)
  127. {
  128. return __crypto_md5_export(&HMAC_MD5_CTX(desc)->hash_ctx, out);
  129. }
  130. static int crypto_hmac_md5_import(struct shash_desc *desc, const void *in)
  131. {
  132. struct hmac_md5_ctx *ctx = HMAC_MD5_CTX(desc);
  133. ctx->ostate = HMAC_MD5_KEY(desc->tfm)->ostate;
  134. return __crypto_md5_import(&ctx->hash_ctx, in);
  135. }
  136. static int crypto_hmac_md5_export_core(struct shash_desc *desc, void *out)
  137. {
  138. return __crypto_md5_export_core(&HMAC_MD5_CTX(desc)->hash_ctx, out);
  139. }
  140. static int crypto_hmac_md5_import_core(struct shash_desc *desc, const void *in)
  141. {
  142. struct hmac_md5_ctx *ctx = HMAC_MD5_CTX(desc);
  143. ctx->ostate = HMAC_MD5_KEY(desc->tfm)->ostate;
  144. return __crypto_md5_import_core(&ctx->hash_ctx, in);
  145. }
  146. static struct shash_alg algs[] = {
  147. {
  148. .base.cra_name = "md5",
  149. .base.cra_driver_name = "md5-lib",
  150. .base.cra_priority = 300,
  151. .base.cra_blocksize = MD5_BLOCK_SIZE,
  152. .base.cra_module = THIS_MODULE,
  153. .digestsize = MD5_DIGEST_SIZE,
  154. .init = crypto_md5_init,
  155. .update = crypto_md5_update,
  156. .final = crypto_md5_final,
  157. .digest = crypto_md5_digest,
  158. .export = crypto_md5_export,
  159. .import = crypto_md5_import,
  160. .export_core = crypto_md5_export_core,
  161. .import_core = crypto_md5_import_core,
  162. .descsize = sizeof(struct md5_ctx),
  163. .statesize = MD5_SHASH_STATE_SIZE,
  164. },
  165. {
  166. .base.cra_name = "hmac(md5)",
  167. .base.cra_driver_name = "hmac-md5-lib",
  168. .base.cra_priority = 300,
  169. .base.cra_blocksize = MD5_BLOCK_SIZE,
  170. .base.cra_ctxsize = sizeof(struct hmac_md5_key),
  171. .base.cra_module = THIS_MODULE,
  172. .digestsize = MD5_DIGEST_SIZE,
  173. .setkey = crypto_hmac_md5_setkey,
  174. .init = crypto_hmac_md5_init,
  175. .update = crypto_hmac_md5_update,
  176. .final = crypto_hmac_md5_final,
  177. .digest = crypto_hmac_md5_digest,
  178. .export = crypto_hmac_md5_export,
  179. .import = crypto_hmac_md5_import,
  180. .export_core = crypto_hmac_md5_export_core,
  181. .import_core = crypto_hmac_md5_import_core,
  182. .descsize = sizeof(struct hmac_md5_ctx),
  183. .statesize = MD5_SHASH_STATE_SIZE,
  184. },
  185. };
  186. static int __init crypto_md5_mod_init(void)
  187. {
  188. return crypto_register_shashes(algs, ARRAY_SIZE(algs));
  189. }
  190. module_init(crypto_md5_mod_init);
  191. static void __exit crypto_md5_mod_exit(void)
  192. {
  193. crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
  194. }
  195. module_exit(crypto_md5_mod_exit);
  196. MODULE_LICENSE("GPL");
  197. MODULE_DESCRIPTION("Crypto API support for MD5 and HMAC-MD5");
  198. MODULE_ALIAS_CRYPTO("md5");
  199. MODULE_ALIAS_CRYPTO("md5-lib");
  200. MODULE_ALIAS_CRYPTO("hmac(md5)");
  201. MODULE_ALIAS_CRYPTO("hmac-md5-lib");