md5.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Cryptographic API.
  3. *
  4. * MD5 Message Digest Algorithm (RFC1321).
  5. *
  6. * Derived from cryptoapi implementation, originally based on the
  7. * public domain implementation written by Colin Plumb in 1993.
  8. *
  9. * Copyright (c) Cryptoapi developers.
  10. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. *
  17. */
  18. #include <crypto/internal/hash.h>
  19. #include <crypto/md5.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/string.h>
  23. #include <linux/types.h>
  24. #include <linux/cryptohash.h>
  25. #include <asm/byteorder.h>
  26. const u8 md5_zero_message_hash[MD5_DIGEST_SIZE] = {
  27. 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
  28. 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e,
  29. };
  30. EXPORT_SYMBOL_GPL(md5_zero_message_hash);
  31. /* XXX: this stuff can be optimized */
  32. static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
  33. {
  34. while (words--) {
  35. __le32_to_cpus(buf);
  36. buf++;
  37. }
  38. }
  39. static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
  40. {
  41. while (words--) {
  42. __cpu_to_le32s(buf);
  43. buf++;
  44. }
  45. }
  46. static inline void md5_transform_helper(struct md5_state *ctx)
  47. {
  48. le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
  49. md5_transform(ctx->hash, ctx->block);
  50. }
  51. static int md5_init(struct shash_desc *desc)
  52. {
  53. struct md5_state *mctx = shash_desc_ctx(desc);
  54. mctx->hash[0] = MD5_H0;
  55. mctx->hash[1] = MD5_H1;
  56. mctx->hash[2] = MD5_H2;
  57. mctx->hash[3] = MD5_H3;
  58. mctx->byte_count = 0;
  59. return 0;
  60. }
  61. static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
  62. {
  63. struct md5_state *mctx = shash_desc_ctx(desc);
  64. const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  65. mctx->byte_count += len;
  66. if (avail > len) {
  67. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  68. data, len);
  69. return 0;
  70. }
  71. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  72. data, avail);
  73. md5_transform_helper(mctx);
  74. data += avail;
  75. len -= avail;
  76. while (len >= sizeof(mctx->block)) {
  77. memcpy(mctx->block, data, sizeof(mctx->block));
  78. md5_transform_helper(mctx);
  79. data += sizeof(mctx->block);
  80. len -= sizeof(mctx->block);
  81. }
  82. memcpy(mctx->block, data, len);
  83. return 0;
  84. }
  85. static int md5_final(struct shash_desc *desc, u8 *out)
  86. {
  87. struct md5_state *mctx = shash_desc_ctx(desc);
  88. const unsigned int offset = mctx->byte_count & 0x3f;
  89. char *p = (char *)mctx->block + offset;
  90. int padding = 56 - (offset + 1);
  91. *p++ = 0x80;
  92. if (padding < 0) {
  93. memset(p, 0x00, padding + sizeof (u64));
  94. md5_transform_helper(mctx);
  95. p = (char *)mctx->block;
  96. padding = 56;
  97. }
  98. memset(p, 0, padding);
  99. mctx->block[14] = mctx->byte_count << 3;
  100. mctx->block[15] = mctx->byte_count >> 29;
  101. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  102. sizeof(u64)) / sizeof(u32));
  103. md5_transform(mctx->hash, mctx->block);
  104. cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
  105. memcpy(out, mctx->hash, sizeof(mctx->hash));
  106. memset(mctx, 0, sizeof(*mctx));
  107. return 0;
  108. }
  109. static int md5_export(struct shash_desc *desc, void *out)
  110. {
  111. struct md5_state *ctx = shash_desc_ctx(desc);
  112. memcpy(out, ctx, sizeof(*ctx));
  113. return 0;
  114. }
  115. static int md5_import(struct shash_desc *desc, const void *in)
  116. {
  117. struct md5_state *ctx = shash_desc_ctx(desc);
  118. memcpy(ctx, in, sizeof(*ctx));
  119. return 0;
  120. }
  121. static struct shash_alg alg = {
  122. .digestsize = MD5_DIGEST_SIZE,
  123. .init = md5_init,
  124. .update = md5_update,
  125. .final = md5_final,
  126. .export = md5_export,
  127. .import = md5_import,
  128. .descsize = sizeof(struct md5_state),
  129. .statesize = sizeof(struct md5_state),
  130. .base = {
  131. .cra_name = "md5",
  132. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  133. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  134. .cra_module = THIS_MODULE,
  135. }
  136. };
  137. static int __init md5_mod_init(void)
  138. {
  139. return crypto_register_shash(&alg);
  140. }
  141. static void __exit md5_mod_fini(void)
  142. {
  143. crypto_unregister_shash(&alg);
  144. }
  145. module_init(md5_mod_init);
  146. module_exit(md5_mod_fini);
  147. MODULE_LICENSE("GPL");
  148. MODULE_DESCRIPTION("MD5 Message Digest Algorithm");
  149. MODULE_ALIAS_CRYPTO("md5");