md4-internal.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * MD4 hash implementation
  3. * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto.h"
  11. #define MD4_BLOCK_LENGTH 64
  12. #define MD4_DIGEST_LENGTH 16
  13. typedef struct MD4Context {
  14. u32 state[4]; /* state */
  15. u64 count; /* number of bits, mod 2^64 */
  16. u8 buffer[MD4_BLOCK_LENGTH]; /* input buffer */
  17. } MD4_CTX;
  18. static void MD4Init(MD4_CTX *ctx);
  19. static void MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len);
  20. static void MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx);
  21. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  22. {
  23. MD4_CTX ctx;
  24. size_t i;
  25. if (TEST_FAIL())
  26. return -1;
  27. MD4Init(&ctx);
  28. for (i = 0; i < num_elem; i++)
  29. MD4Update(&ctx, addr[i], len[i]);
  30. MD4Final(mac, &ctx);
  31. return 0;
  32. }
  33. /* ===== start - public domain MD4 implementation ===== */
  34. /* $OpenBSD: md4.c,v 1.7 2005/08/08 08:05:35 espie Exp $ */
  35. /*
  36. * This code implements the MD4 message-digest algorithm.
  37. * The algorithm is due to Ron Rivest. This code was
  38. * written by Colin Plumb in 1993, no copyright is claimed.
  39. * This code is in the public domain; do with it what you wish.
  40. * Todd C. Miller modified the MD5 code to do MD4 based on RFC 1186.
  41. *
  42. * Equivalent code is available from RSA Data Security, Inc.
  43. * This code has been tested against that, and is equivalent,
  44. * except that you don't need to include two pages of legalese
  45. * with every copy.
  46. *
  47. * To compute the message digest of a chunk of bytes, declare an
  48. * MD4Context structure, pass it to MD4Init, call MD4Update as
  49. * needed on buffers full of bytes, and then call MD4Final, which
  50. * will fill a supplied 16-byte array with the digest.
  51. */
  52. #define MD4_DIGEST_STRING_LENGTH (MD4_DIGEST_LENGTH * 2 + 1)
  53. static void
  54. MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH]);
  55. #define PUT_64BIT_LE(cp, value) do { \
  56. (cp)[7] = (value) >> 56; \
  57. (cp)[6] = (value) >> 48; \
  58. (cp)[5] = (value) >> 40; \
  59. (cp)[4] = (value) >> 32; \
  60. (cp)[3] = (value) >> 24; \
  61. (cp)[2] = (value) >> 16; \
  62. (cp)[1] = (value) >> 8; \
  63. (cp)[0] = (value); } while (0)
  64. #define PUT_32BIT_LE(cp, value) do { \
  65. (cp)[3] = (value) >> 24; \
  66. (cp)[2] = (value) >> 16; \
  67. (cp)[1] = (value) >> 8; \
  68. (cp)[0] = (value); } while (0)
  69. static u8 PADDING[MD4_BLOCK_LENGTH] = {
  70. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  71. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  73. };
  74. /*
  75. * Start MD4 accumulation.
  76. * Set bit count to 0 and buffer to mysterious initialization constants.
  77. */
  78. static void MD4Init(MD4_CTX *ctx)
  79. {
  80. ctx->count = 0;
  81. ctx->state[0] = 0x67452301;
  82. ctx->state[1] = 0xefcdab89;
  83. ctx->state[2] = 0x98badcfe;
  84. ctx->state[3] = 0x10325476;
  85. }
  86. /*
  87. * Update context to reflect the concatenation of another buffer full
  88. * of bytes.
  89. */
  90. static void MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len)
  91. {
  92. size_t have, need;
  93. /* Check how many bytes we already have and how many more we need. */
  94. have = (size_t)((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1));
  95. need = MD4_BLOCK_LENGTH - have;
  96. /* Update bitcount */
  97. ctx->count += (u64)len << 3;
  98. if (len >= need) {
  99. if (have != 0) {
  100. os_memcpy(ctx->buffer + have, input, need);
  101. MD4Transform(ctx->state, ctx->buffer);
  102. input += need;
  103. len -= need;
  104. have = 0;
  105. }
  106. /* Process data in MD4_BLOCK_LENGTH-byte chunks. */
  107. while (len >= MD4_BLOCK_LENGTH) {
  108. MD4Transform(ctx->state, input);
  109. input += MD4_BLOCK_LENGTH;
  110. len -= MD4_BLOCK_LENGTH;
  111. }
  112. }
  113. /* Handle any remaining bytes of data. */
  114. if (len != 0)
  115. os_memcpy(ctx->buffer + have, input, len);
  116. }
  117. /*
  118. * Pad pad to 64-byte boundary with the bit pattern
  119. * 1 0* (64-bit count of bits processed, MSB-first)
  120. */
  121. static void MD4Pad(MD4_CTX *ctx)
  122. {
  123. u8 count[8];
  124. size_t padlen;
  125. /* Convert count to 8 bytes in little endian order. */
  126. PUT_64BIT_LE(count, ctx->count);
  127. /* Pad out to 56 mod 64. */
  128. padlen = MD4_BLOCK_LENGTH -
  129. ((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1));
  130. if (padlen < 1 + 8)
  131. padlen += MD4_BLOCK_LENGTH;
  132. MD4Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
  133. MD4Update(ctx, count, 8);
  134. }
  135. /*
  136. * Final wrapup--call MD4Pad, fill in digest and zero out ctx.
  137. */
  138. static void MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx)
  139. {
  140. int i;
  141. MD4Pad(ctx);
  142. if (digest != NULL) {
  143. for (i = 0; i < 4; i++)
  144. PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
  145. os_memset(ctx, 0, sizeof(*ctx));
  146. }
  147. }
  148. /* The three core functions - F1 is optimized somewhat */
  149. /* #define F1(x, y, z) (x & y | ~x & z) */
  150. #define F1(x, y, z) (z ^ (x & (y ^ z)))
  151. #define F2(x, y, z) ((x & y) | (x & z) | (y & z))
  152. #define F3(x, y, z) (x ^ y ^ z)
  153. /* This is the central step in the MD4 algorithm. */
  154. #define MD4STEP(f, w, x, y, z, data, s) \
  155. ( w += f(x, y, z) + data, w = w<<s | w>>(32-s) )
  156. /*
  157. * The core of the MD4 algorithm, this alters an existing MD4 hash to
  158. * reflect the addition of 16 longwords of new data. MD4Update blocks
  159. * the data and converts bytes into longwords for this routine.
  160. */
  161. static void
  162. MD4Transform(u32 state[4], const u8 block[MD4_BLOCK_LENGTH])
  163. {
  164. u32 a, b, c, d, in[MD4_BLOCK_LENGTH / 4];
  165. #if BYTE_ORDER == LITTLE_ENDIAN
  166. os_memcpy(in, block, sizeof(in));
  167. #else
  168. for (a = 0; a < MD4_BLOCK_LENGTH / 4; a++) {
  169. in[a] = (u32)(
  170. (u32)(block[a * 4 + 0]) |
  171. (u32)(block[a * 4 + 1]) << 8 |
  172. (u32)(block[a * 4 + 2]) << 16 |
  173. (u32)(block[a * 4 + 3]) << 24);
  174. }
  175. #endif
  176. a = state[0];
  177. b = state[1];
  178. c = state[2];
  179. d = state[3];
  180. MD4STEP(F1, a, b, c, d, in[ 0], 3);
  181. MD4STEP(F1, d, a, b, c, in[ 1], 7);
  182. MD4STEP(F1, c, d, a, b, in[ 2], 11);
  183. MD4STEP(F1, b, c, d, a, in[ 3], 19);
  184. MD4STEP(F1, a, b, c, d, in[ 4], 3);
  185. MD4STEP(F1, d, a, b, c, in[ 5], 7);
  186. MD4STEP(F1, c, d, a, b, in[ 6], 11);
  187. MD4STEP(F1, b, c, d, a, in[ 7], 19);
  188. MD4STEP(F1, a, b, c, d, in[ 8], 3);
  189. MD4STEP(F1, d, a, b, c, in[ 9], 7);
  190. MD4STEP(F1, c, d, a, b, in[10], 11);
  191. MD4STEP(F1, b, c, d, a, in[11], 19);
  192. MD4STEP(F1, a, b, c, d, in[12], 3);
  193. MD4STEP(F1, d, a, b, c, in[13], 7);
  194. MD4STEP(F1, c, d, a, b, in[14], 11);
  195. MD4STEP(F1, b, c, d, a, in[15], 19);
  196. MD4STEP(F2, a, b, c, d, in[ 0] + 0x5a827999, 3);
  197. MD4STEP(F2, d, a, b, c, in[ 4] + 0x5a827999, 5);
  198. MD4STEP(F2, c, d, a, b, in[ 8] + 0x5a827999, 9);
  199. MD4STEP(F2, b, c, d, a, in[12] + 0x5a827999, 13);
  200. MD4STEP(F2, a, b, c, d, in[ 1] + 0x5a827999, 3);
  201. MD4STEP(F2, d, a, b, c, in[ 5] + 0x5a827999, 5);
  202. MD4STEP(F2, c, d, a, b, in[ 9] + 0x5a827999, 9);
  203. MD4STEP(F2, b, c, d, a, in[13] + 0x5a827999, 13);
  204. MD4STEP(F2, a, b, c, d, in[ 2] + 0x5a827999, 3);
  205. MD4STEP(F2, d, a, b, c, in[ 6] + 0x5a827999, 5);
  206. MD4STEP(F2, c, d, a, b, in[10] + 0x5a827999, 9);
  207. MD4STEP(F2, b, c, d, a, in[14] + 0x5a827999, 13);
  208. MD4STEP(F2, a, b, c, d, in[ 3] + 0x5a827999, 3);
  209. MD4STEP(F2, d, a, b, c, in[ 7] + 0x5a827999, 5);
  210. MD4STEP(F2, c, d, a, b, in[11] + 0x5a827999, 9);
  211. MD4STEP(F2, b, c, d, a, in[15] + 0x5a827999, 13);
  212. MD4STEP(F3, a, b, c, d, in[ 0] + 0x6ed9eba1, 3);
  213. MD4STEP(F3, d, a, b, c, in[ 8] + 0x6ed9eba1, 9);
  214. MD4STEP(F3, c, d, a, b, in[ 4] + 0x6ed9eba1, 11);
  215. MD4STEP(F3, b, c, d, a, in[12] + 0x6ed9eba1, 15);
  216. MD4STEP(F3, a, b, c, d, in[ 2] + 0x6ed9eba1, 3);
  217. MD4STEP(F3, d, a, b, c, in[10] + 0x6ed9eba1, 9);
  218. MD4STEP(F3, c, d, a, b, in[ 6] + 0x6ed9eba1, 11);
  219. MD4STEP(F3, b, c, d, a, in[14] + 0x6ed9eba1, 15);
  220. MD4STEP(F3, a, b, c, d, in[ 1] + 0x6ed9eba1, 3);
  221. MD4STEP(F3, d, a, b, c, in[ 9] + 0x6ed9eba1, 9);
  222. MD4STEP(F3, c, d, a, b, in[ 5] + 0x6ed9eba1, 11);
  223. MD4STEP(F3, b, c, d, a, in[13] + 0x6ed9eba1, 15);
  224. MD4STEP(F3, a, b, c, d, in[ 3] + 0x6ed9eba1, 3);
  225. MD4STEP(F3, d, a, b, c, in[11] + 0x6ed9eba1, 9);
  226. MD4STEP(F3, c, d, a, b, in[ 7] + 0x6ed9eba1, 11);
  227. MD4STEP(F3, b, c, d, a, in[15] + 0x6ed9eba1, 15);
  228. state[0] += a;
  229. state[1] += b;
  230. state[2] += c;
  231. state[3] += d;
  232. }
  233. /* ===== end - public domain MD4 implementation ===== */