md5.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /* XXX: this stuff can be optimized */
  27. static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
  28. {
  29. while (words--) {
  30. __le32_to_cpus(buf);
  31. buf++;
  32. }
  33. }
  34. static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
  35. {
  36. while (words--) {
  37. __cpu_to_le32s(buf);
  38. buf++;
  39. }
  40. }
  41. static inline void md5_transform_helper(struct md5_state *ctx)
  42. {
  43. le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
  44. md5_transform(ctx->hash, ctx->block);
  45. }
  46. static int md5_init(struct shash_desc *desc)
  47. {
  48. struct md5_state *mctx = shash_desc_ctx(desc);
  49. mctx->hash[0] = MD5_H0;
  50. mctx->hash[1] = MD5_H1;
  51. mctx->hash[2] = MD5_H2;
  52. mctx->hash[3] = MD5_H3;
  53. mctx->byte_count = 0;
  54. return 0;
  55. }
  56. static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len)
  57. {
  58. struct md5_state *mctx = shash_desc_ctx(desc);
  59. const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  60. mctx->byte_count += len;
  61. if (avail > len) {
  62. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  63. data, len);
  64. return 0;
  65. }
  66. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  67. data, avail);
  68. md5_transform_helper(mctx);
  69. data += avail;
  70. len -= avail;
  71. while (len >= sizeof(mctx->block)) {
  72. memcpy(mctx->block, data, sizeof(mctx->block));
  73. md5_transform_helper(mctx);
  74. data += sizeof(mctx->block);
  75. len -= sizeof(mctx->block);
  76. }
  77. memcpy(mctx->block, data, len);
  78. return 0;
  79. }
  80. static int md5_final(struct shash_desc *desc, u8 *out)
  81. {
  82. struct md5_state *mctx = shash_desc_ctx(desc);
  83. const unsigned int offset = mctx->byte_count & 0x3f;
  84. char *p = (char *)mctx->block + offset;
  85. int padding = 56 - (offset + 1);
  86. *p++ = 0x80;
  87. if (padding < 0) {
  88. memset(p, 0x00, padding + sizeof (u64));
  89. md5_transform_helper(mctx);
  90. p = (char *)mctx->block;
  91. padding = 56;
  92. }
  93. memset(p, 0, padding);
  94. mctx->block[14] = mctx->byte_count << 3;
  95. mctx->block[15] = mctx->byte_count >> 29;
  96. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  97. sizeof(u64)) / sizeof(u32));
  98. md5_transform(mctx->hash, mctx->block);
  99. cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
  100. memcpy(out, mctx->hash, sizeof(mctx->hash));
  101. memset(mctx, 0, sizeof(*mctx));
  102. return 0;
  103. }
  104. static int md5_export(struct shash_desc *desc, void *out)
  105. {
  106. struct md5_state *ctx = shash_desc_ctx(desc);
  107. memcpy(out, ctx, sizeof(*ctx));
  108. return 0;
  109. }
  110. static int md5_import(struct shash_desc *desc, const void *in)
  111. {
  112. struct md5_state *ctx = shash_desc_ctx(desc);
  113. memcpy(ctx, in, sizeof(*ctx));
  114. return 0;
  115. }
  116. static struct shash_alg alg = {
  117. .digestsize = MD5_DIGEST_SIZE,
  118. .init = md5_init,
  119. .update = md5_update,
  120. .final = md5_final,
  121. .export = md5_export,
  122. .import = md5_import,
  123. .descsize = sizeof(struct md5_state),
  124. .statesize = sizeof(struct md5_state),
  125. .base = {
  126. .cra_name = "md5",
  127. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  128. .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
  129. .cra_module = THIS_MODULE,
  130. }
  131. };
  132. static int __init md5_mod_init(void)
  133. {
  134. return crypto_register_shash(&alg);
  135. }
  136. static void __exit md5_mod_fini(void)
  137. {
  138. crypto_unregister_shash(&alg);
  139. }
  140. module_init(md5_mod_init);
  141. module_exit(md5_mod_fini);
  142. MODULE_LICENSE("GPL");
  143. MODULE_DESCRIPTION("MD5 Message Digest Algorithm");
  144. MODULE_ALIAS_CRYPTO("md5");