md4.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <linux/init.h>
  24. #include <linux/crypto.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, sizeof(ctx->block) / sizeof(u32));
  136. md4_transform(ctx->hash, ctx->block);
  137. }
  138. static void md4_init(struct crypto_tfm *tfm)
  139. {
  140. struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
  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. }
  147. static void md4_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
  148. {
  149. struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
  150. const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  151. mctx->byte_count += len;
  152. if (avail > len) {
  153. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  154. data, len);
  155. return;
  156. }
  157. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  158. data, avail);
  159. md4_transform_helper(mctx);
  160. data += avail;
  161. len -= avail;
  162. while (len >= sizeof(mctx->block)) {
  163. memcpy(mctx->block, data, sizeof(mctx->block));
  164. md4_transform_helper(mctx);
  165. data += sizeof(mctx->block);
  166. len -= sizeof(mctx->block);
  167. }
  168. memcpy(mctx->block, data, len);
  169. }
  170. static void md4_final(struct crypto_tfm *tfm, u8 *out)
  171. {
  172. struct md4_ctx *mctx = crypto_tfm_ctx(tfm);
  173. const unsigned int offset = mctx->byte_count & 0x3f;
  174. char *p = (char *)mctx->block + offset;
  175. int padding = 56 - (offset + 1);
  176. *p++ = 0x80;
  177. if (padding < 0) {
  178. memset(p, 0x00, padding + sizeof (u64));
  179. md4_transform_helper(mctx);
  180. p = (char *)mctx->block;
  181. padding = 56;
  182. }
  183. memset(p, 0, padding);
  184. mctx->block[14] = mctx->byte_count << 3;
  185. mctx->block[15] = mctx->byte_count >> 29;
  186. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  187. sizeof(u64)) / sizeof(u32));
  188. md4_transform(mctx->hash, mctx->block);
  189. cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(u32));
  190. memcpy(out, mctx->hash, sizeof(mctx->hash));
  191. memset(mctx, 0, sizeof(*mctx));
  192. }
  193. static struct crypto_alg alg = {
  194. .cra_name = "md4",
  195. .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
  196. .cra_blocksize = MD4_HMAC_BLOCK_SIZE,
  197. .cra_ctxsize = sizeof(struct md4_ctx),
  198. .cra_module = THIS_MODULE,
  199. .cra_list = LIST_HEAD_INIT(alg.cra_list),
  200. .cra_u = { .digest = {
  201. .dia_digestsize = MD4_DIGEST_SIZE,
  202. .dia_init = md4_init,
  203. .dia_update = md4_update,
  204. .dia_final = md4_final } }
  205. };
  206. static int __init init(void)
  207. {
  208. return crypto_register_alg(&alg);
  209. }
  210. static void __exit fini(void)
  211. {
  212. crypto_unregister_alg(&alg);
  213. }
  214. module_init(init);
  215. module_exit(fini);
  216. MODULE_LICENSE("GPL");
  217. MODULE_DESCRIPTION("MD4 Message Digest Algorithm");