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