md4.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/string.h>
  27. #include <linux/types.h>
  28. #include <asm/byteorder.h>
  29. #define MD4_DIGEST_SIZE 16
  30. #define MD4_HMAC_BLOCK_SIZE 64
  31. #define MD4_BLOCK_WORDS 16
  32. #define MD4_HASH_WORDS 4
  33. struct md4_ctx {
  34. u32 hash[MD4_HASH_WORDS];
  35. u32 block[MD4_BLOCK_WORDS];
  36. u64 byte_count;
  37. };
  38. static inline u32 lshift(u32 x, unsigned int s)
  39. {
  40. x &= 0xFFFFFFFF;
  41. return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
  42. }
  43. static inline u32 F(u32 x, u32 y, u32 z)
  44. {
  45. return (x & y) | ((~x) & z);
  46. }
  47. static inline u32 G(u32 x, u32 y, u32 z)
  48. {
  49. return (x & y) | (x & z) | (y & z);
  50. }
  51. static inline u32 H(u32 x, u32 y, u32 z)
  52. {
  53. return x ^ y ^ z;
  54. }
  55. #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
  56. #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (u32)0x5A827999,s))
  57. #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (u32)0x6ED9EBA1,s))
  58. /* XXX: this stuff can be optimized */
  59. static inline void le32_to_cpu_array(u32 *buf, unsigned int words)
  60. {
  61. while (words--) {
  62. __le32_to_cpus(buf);
  63. buf++;
  64. }
  65. }
  66. static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
  67. {
  68. while (words--) {
  69. __cpu_to_le32s(buf);
  70. buf++;
  71. }
  72. }
  73. static void md4_transform(u32 *hash, u32 const *in)
  74. {
  75. u32 a, b, c, d;
  76. a = hash[0];
  77. b = hash[1];
  78. c = hash[2];
  79. d = hash[3];
  80. ROUND1(a, b, c, d, in[0], 3);
  81. ROUND1(d, a, b, c, in[1], 7);
  82. ROUND1(c, d, a, b, in[2], 11);
  83. ROUND1(b, c, d, a, in[3], 19);
  84. ROUND1(a, b, c, d, in[4], 3);
  85. ROUND1(d, a, b, c, in[5], 7);
  86. ROUND1(c, d, a, b, in[6], 11);
  87. ROUND1(b, c, d, a, in[7], 19);
  88. ROUND1(a, b, c, d, in[8], 3);
  89. ROUND1(d, a, b, c, in[9], 7);
  90. ROUND1(c, d, a, b, in[10], 11);
  91. ROUND1(b, c, d, a, in[11], 19);
  92. ROUND1(a, b, c, d, in[12], 3);
  93. ROUND1(d, a, b, c, in[13], 7);
  94. ROUND1(c, d, a, b, in[14], 11);
  95. ROUND1(b, c, d, a, in[15], 19);
  96. ROUND2(a, b, c, d,in[ 0], 3);
  97. ROUND2(d, a, b, c, in[4], 5);
  98. ROUND2(c, d, a, b, in[8], 9);
  99. ROUND2(b, c, d, a, in[12], 13);
  100. ROUND2(a, b, c, d, in[1], 3);
  101. ROUND2(d, a, b, c, in[5], 5);
  102. ROUND2(c, d, a, b, in[9], 9);
  103. ROUND2(b, c, d, a, in[13], 13);
  104. ROUND2(a, b, c, d, in[2], 3);
  105. ROUND2(d, a, b, c, in[6], 5);
  106. ROUND2(c, d, a, b, in[10], 9);
  107. ROUND2(b, c, d, a, in[14], 13);
  108. ROUND2(a, b, c, d, in[3], 3);
  109. ROUND2(d, a, b, c, in[7], 5);
  110. ROUND2(c, d, a, b, in[11], 9);
  111. ROUND2(b, c, d, a, in[15], 13);
  112. ROUND3(a, b, c, d,in[ 0], 3);
  113. ROUND3(d, a, b, c, in[8], 9);
  114. ROUND3(c, d, a, b, in[4], 11);
  115. ROUND3(b, c, d, a, in[12], 15);
  116. ROUND3(a, b, c, d, in[2], 3);
  117. ROUND3(d, a, b, c, in[10], 9);
  118. ROUND3(c, d, a, b, in[6], 11);
  119. ROUND3(b, c, d, a, in[14], 15);
  120. ROUND3(a, b, c, d, in[1], 3);
  121. ROUND3(d, a, b, c, in[9], 9);
  122. ROUND3(c, d, a, b, in[5], 11);
  123. ROUND3(b, c, d, a, in[13], 15);
  124. ROUND3(a, b, c, d, in[3], 3);
  125. ROUND3(d, a, b, c, in[11], 9);
  126. ROUND3(c, d, a, b, in[7], 11);
  127. ROUND3(b, c, d, a, in[15], 15);
  128. hash[0] += a;
  129. hash[1] += b;
  130. hash[2] += c;
  131. hash[3] += d;
  132. }
  133. static inline void md4_transform_helper(struct md4_ctx *ctx)
  134. {
  135. le32_to_cpu_array(ctx->block, ARRAY_SIZE(ctx->block));
  136. md4_transform(ctx->hash, ctx->block);
  137. }
  138. static int md4_init(struct shash_desc *desc)
  139. {
  140. struct md4_ctx *mctx = shash_desc_ctx(desc);
  141. mctx->hash[0] = 0x67452301;
  142. mctx->hash[1] = 0xefcdab89;
  143. mctx->hash[2] = 0x98badcfe;
  144. mctx->hash[3] = 0x10325476;
  145. mctx->byte_count = 0;
  146. return 0;
  147. }
  148. static int md4_update(struct shash_desc *desc, const u8 *data, unsigned int len)
  149. {
  150. struct md4_ctx *mctx = shash_desc_ctx(desc);
  151. const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  152. mctx->byte_count += len;
  153. if (avail > len) {
  154. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  155. data, len);
  156. return 0;
  157. }
  158. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  159. data, avail);
  160. md4_transform_helper(mctx);
  161. data += avail;
  162. len -= avail;
  163. while (len >= sizeof(mctx->block)) {
  164. memcpy(mctx->block, data, sizeof(mctx->block));
  165. md4_transform_helper(mctx);
  166. data += sizeof(mctx->block);
  167. len -= sizeof(mctx->block);
  168. }
  169. memcpy(mctx->block, data, len);
  170. return 0;
  171. }
  172. static int md4_final(struct shash_desc *desc, u8 *out)
  173. {
  174. struct md4_ctx *mctx = shash_desc_ctx(desc);
  175. const unsigned int offset = mctx->byte_count & 0x3f;
  176. char *p = (char *)mctx->block + offset;
  177. int padding = 56 - (offset + 1);
  178. *p++ = 0x80;
  179. if (padding < 0) {
  180. memset(p, 0x00, padding + sizeof (u64));
  181. md4_transform_helper(mctx);
  182. p = (char *)mctx->block;
  183. padding = 56;
  184. }
  185. memset(p, 0, padding);
  186. mctx->block[14] = mctx->byte_count << 3;
  187. mctx->block[15] = mctx->byte_count >> 29;
  188. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  189. sizeof(u64)) / sizeof(u32));
  190. md4_transform(mctx->hash, mctx->block);
  191. cpu_to_le32_array(mctx->hash, ARRAY_SIZE(mctx->hash));
  192. memcpy(out, mctx->hash, sizeof(mctx->hash));
  193. memset(mctx, 0, sizeof(*mctx));
  194. return 0;
  195. }
  196. static struct shash_alg alg = {
  197. .digestsize = MD4_DIGEST_SIZE,
  198. .init = md4_init,
  199. .update = md4_update,
  200. .final = md4_final,
  201. .descsize = sizeof(struct md4_ctx),
  202. .base = {
  203. .cra_name = "md4",
  204. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  205. .cra_blocksize = MD4_HMAC_BLOCK_SIZE,
  206. .cra_module = THIS_MODULE,
  207. }
  208. };
  209. static int __init md4_mod_init(void)
  210. {
  211. return crypto_register_shash(&alg);
  212. }
  213. static void __exit md4_mod_fini(void)
  214. {
  215. crypto_unregister_shash(&alg);
  216. }
  217. module_init(md4_mod_init);
  218. module_exit(md4_mod_fini);
  219. MODULE_LICENSE("GPL");
  220. MODULE_DESCRIPTION("MD4 Message Digest Algorithm");