md4.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Cryptographic API.
  3. *
  4. * MD4 Message Digest Algorithm (RFC1320).
  5. *
  6. * Implementation derived from Andrew Tridgell and Steve French's
  7. * CIFS MD4 implementation, and the cryptoapi implementation
  8. * originally based on the public domain implementation written
  9. * by Colin Plumb in 1993.
  10. *
  11. * Copyright (c) Andrew Tridgell 1997-1998.
  12. * Modified by Steve French (sfrench@us.ibm.com) 2002
  13. * Copyright (c) Cryptoapi developers.
  14. * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  15. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. */
  23. #include <crypto/internal/hash.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/string.h>
  28. #include <linux/types.h>
  29. #include <asm/byteorder.h>
  30. #define MD4_DIGEST_SIZE 16
  31. #define MD4_HMAC_BLOCK_SIZE 64
  32. #define MD4_BLOCK_WORDS 16
  33. #define MD4_HASH_WORDS 4
  34. struct md4_ctx {
  35. u32 hash[MD4_HASH_WORDS];
  36. u32 block[MD4_BLOCK_WORDS];
  37. u64 byte_count;
  38. };
  39. static inline u32 lshift(u32 x, unsigned int s)
  40. {
  41. x &= 0xFFFFFFFF;
  42. return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
  43. }
  44. static inline u32 F(u32 x, u32 y, u32 z)
  45. {
  46. return (x & y) | ((~x) & z);
  47. }
  48. static inline u32 G(u32 x, u32 y, u32 z)
  49. {
  50. return (x & y) | (x & z) | (y & z);
  51. }
  52. static inline u32 H(u32 x, u32 y, u32 z)
  53. {
  54. return x ^ y ^ z;
  55. }
  56. #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
  57. #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s))
  58. #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s))
  59. static void md4_transform(u32 *hash, u32 const *in)
  60. {
  61. u32 a, b, c, d;
  62. a = hash[0];
  63. b = hash[1];
  64. c = hash[2];
  65. d = hash[3];
  66. ROUND1(a, b, c, d, in[0], 3);
  67. ROUND1(d, a, b, c, in[1], 7);
  68. ROUND1(c, d, a, b, in[2], 11);
  69. ROUND1(b, c, d, a, in[3], 19);
  70. ROUND1(a, b, c, d, in[4], 3);
  71. ROUND1(d, a, b, c, in[5], 7);
  72. ROUND1(c, d, a, b, in[6], 11);
  73. ROUND1(b, c, d, a, in[7], 19);
  74. ROUND1(a, b, c, d, in[8], 3);
  75. ROUND1(d, a, b, c, in[9], 7);
  76. ROUND1(c, d, a, b, in[10], 11);
  77. ROUND1(b, c, d, a, in[11], 19);
  78. ROUND1(a, b, c, d, in[12], 3);
  79. ROUND1(d, a, b, c, in[13], 7);
  80. ROUND1(c, d, a, b, in[14], 11);
  81. ROUND1(b, c, d, a, in[15], 19);
  82. ROUND2(a, b, c, d,in[ 0], 3);
  83. ROUND2(d, a, b, c, in[4], 5);
  84. ROUND2(c, d, a, b, in[8], 9);
  85. ROUND2(b, c, d, a, in[12], 13);
  86. ROUND2(a, b, c, d, in[1], 3);
  87. ROUND2(d, a, b, c, in[5], 5);
  88. ROUND2(c, d, a, b, in[9], 9);
  89. ROUND2(b, c, d, a, in[13], 13);
  90. ROUND2(a, b, c, d, in[2], 3);
  91. ROUND2(d, a, b, c, in[6], 5);
  92. ROUND2(c, d, a, b, in[10], 9);
  93. ROUND2(b, c, d, a, in[14], 13);
  94. ROUND2(a, b, c, d, in[3], 3);
  95. ROUND2(d, a, b, c, in[7], 5);
  96. ROUND2(c, d, a, b, in[11], 9);
  97. ROUND2(b, c, d, a, in[15], 13);
  98. ROUND3(a, b, c, d,in[ 0], 3);
  99. ROUND3(d, a, b, c, in[8], 9);
  100. ROUND3(c, d, a, b, in[4], 11);
  101. ROUND3(b, c, d, a, in[12], 15);
  102. ROUND3(a, b, c, d, in[2], 3);
  103. ROUND3(d, a, b, c, in[10], 9);
  104. ROUND3(c, d, a, b, in[6], 11);
  105. ROUND3(b, c, d, a, in[14], 15);
  106. ROUND3(a, b, c, d, in[1], 3);
  107. ROUND3(d, a, b, c, in[9], 9);
  108. ROUND3(c, d, a, b, in[5], 11);
  109. ROUND3(b, c, d, a, in[13], 15);
  110. ROUND3(a, b, c, d, in[3], 3);
  111. ROUND3(d, a, b, c, in[11], 9);
  112. ROUND3(c, d, a, b, in[7], 11);
  113. ROUND3(b, c, d, a, in[15], 15);
  114. hash[0] += a;
  115. hash[1] += b;
  116. hash[2] += c;
  117. hash[3] += d;
  118. }
  119. static inline void md4_transform_helper(struct md4_ctx *ctx)
  120. {
  121. le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
  122. md4_transform(ctx->hash, ctx->block);
  123. }
  124. static int md4_init(struct shash_desc *desc)
  125. {
  126. struct md4_ctx *mctx = shash_desc_ctx(desc);
  127. mctx->hash[0] = 0x67452301;
  128. mctx->hash[1] = 0xefcdab89;
  129. mctx->hash[2] = 0x98badcfe;
  130. mctx->hash[3] = 0x10325476;
  131. mctx->byte_count = 0;
  132. return 0;
  133. }
  134. static int md4_update(struct shash_desc *desc, const u8 *data, unsigned int len)
  135. {
  136. struct md4_ctx *mctx = shash_desc_ctx(desc);
  137. const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  138. mctx->byte_count += len;
  139. if (avail > len) {
  140. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  141. data, len);
  142. return 0;
  143. }
  144. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  145. data, avail);
  146. md4_transform_helper(mctx);
  147. data += avail;
  148. len -= avail;
  149. while (len >= sizeof(mctx->block)) {
  150. memcpy(mctx->block, data, sizeof(mctx->block));
  151. md4_transform_helper(mctx);
  152. data += sizeof(mctx->block);
  153. len -= sizeof(mctx->block);
  154. }
  155. memcpy(mctx->block, data, len);
  156. return 0;
  157. }
  158. static int md4_final(struct shash_desc *desc, u8 *out)
  159. {
  160. struct md4_ctx *mctx = shash_desc_ctx(desc);
  161. const unsigned int offset = mctx->byte_count & 0x3f;
  162. char *p = (char *)mctx->block + offset;
  163. int padding = 56 - (offset + 1);
  164. *p++ = 0x80;
  165. if (padding < 0) {
  166. memset(p, 0x00, padding + sizeof (u64));
  167. md4_transform_helper(mctx);
  168. p = (char *)mctx->block;
  169. padding = 56;
  170. }
  171. memset(p, 0, padding);
  172. mctx->block[14] = mctx->byte_count << 3;
  173. mctx->block[15] = mctx->byte_count >> 29;
  174. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  175. sizeof(u64)) / sizeof(u32));
  176. md4_transform(mctx->hash, mctx->block);
  177. cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
  178. memcpy(out, mctx->hash, sizeof(mctx->hash));
  179. memset(mctx, 0, sizeof(*mctx));
  180. return 0;
  181. }
  182. static struct shash_alg alg = {
  183. .digestsize = MD4_DIGEST_SIZE,
  184. .init = md4_init,
  185. .update = md4_update,
  186. .final = md4_final,
  187. .descsize = sizeof(struct md4_ctx),
  188. .base = {
  189. .cra_name = "md4",
  190. .cra_driver_name = "md4-generic",
  191. .cra_blocksize = MD4_HMAC_BLOCK_SIZE,
  192. .cra_module = THIS_MODULE,
  193. }
  194. };
  195. static int __init md4_mod_init(void)
  196. {
  197. return crypto_register_shash(&alg);
  198. }
  199. static void __exit md4_mod_fini(void)
  200. {
  201. crypto_unregister_shash(&alg);
  202. }
  203. subsys_initcall(md4_mod_init);
  204. module_exit(md4_mod_fini);
  205. MODULE_LICENSE("GPL");
  206. MODULE_DESCRIPTION("MD4 Message Digest Algorithm");
  207. MODULE_ALIAS_CRYPTO("md4");