md4.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * md4c.c MD4 message-digest algorithm
  3. *
  4. * License to copy and use this software is granted provided that it
  5. * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  6. * Algorithm" in all material mentioning or referencing this software
  7. * or this function.
  8. *
  9. * License is also granted to make and use derivative works provided
  10. * that such works are identified as "derived from the RSA Data
  11. * Security, Inc. MD4 Message-Digest Algorithm" in all material
  12. * mentioning or referencing the derived work.
  13. *
  14. * RSA Data Security, Inc. makes no representations concerning either
  15. * the merchantability of this software or the suitability of this
  16. * software for any particular purpose. It is provided "as is"
  17. * without express or implied warranty of any kind.
  18. *
  19. * These notices must be retained in any copies of any part of this
  20. * documentation and/or software.
  21. *
  22. * Copyright 1990,1991,1992 RSA Data Security, Inc.
  23. */
  24. #include "md4.h"
  25. /* Constants for MD4Transform routine.
  26. */
  27. #define S11 3
  28. #define S12 7
  29. #define S13 11
  30. #define S14 19
  31. #define S21 3
  32. #define S22 5
  33. #define S23 9
  34. #define S24 13
  35. #define S31 3
  36. #define S32 9
  37. #define S33 11
  38. #define S34 15
  39. static void MD4Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  40. static void Encode PROTO_LIST
  41. ((unsigned char *, UINT4 *, unsigned int));
  42. static void Decode PROTO_LIST
  43. ((UINT4 *, unsigned char *, unsigned int));
  44. static void MD4_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  45. static void MD4_memset PROTO_LIST ((POINTER, int, unsigned int));
  46. static unsigned char PADDING[64] = {
  47. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  50. };
  51. /* F, G and H are basic MD4 functions.
  52. */
  53. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  54. #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  55. #define H(x, y, z) ((x) ^ (y) ^ (z))
  56. /* ROTATE_LEFT rotates x left n bits.
  57. */
  58. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  59. /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  60. /* Rotation is separate from addition to prevent recomputation */
  61. #define FF(a, b, c, d, x, s) { \
  62. (a) += F ((b), (c), (d)) + (x); \
  63. (a) = ROTATE_LEFT ((a), (s)); \
  64. }
  65. #define GG(a, b, c, d, x, s) { \
  66. (a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
  67. (a) = ROTATE_LEFT ((a), (s)); \
  68. }
  69. #define HH(a, b, c, d, x, s) { \
  70. (a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
  71. (a) = ROTATE_LEFT ((a), (s)); \
  72. }
  73. void md4_calc(output, input, inlen)
  74. unsigned char *output;
  75. unsigned char *input; /* input block */
  76. unsigned int inlen; /* length of input block */
  77. {
  78. MD4_CTX context;
  79. MD4Init(&context);
  80. MD4Update(&context, input, inlen);
  81. MD4Final(output, &context);
  82. }
  83. /* MD4 initialization. Begins an MD4 operation, writing a new context.
  84. */
  85. void MD4Init (context)
  86. MD4_CTX *context; /* context */
  87. {
  88. context->count[0] = context->count[1] = 0;
  89. /* Load magic initialization constants.
  90. */
  91. context->state[0] = 0x67452301;
  92. context->state[1] = 0xefcdab89;
  93. context->state[2] = 0x98badcfe;
  94. context->state[3] = 0x10325476;
  95. }
  96. /* MD4 block update operation. Continues an MD4 message-digest
  97. operation, processing another message block, and updating the
  98. context.
  99. */
  100. void MD4Update (context, input, inputLen)
  101. MD4_CTX *context; /* context */
  102. unsigned char *input; /* input block */
  103. unsigned int inputLen; /* length of input block */
  104. {
  105. unsigned int i, index, partLen;
  106. /* Compute number of bytes mod 64 */
  107. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  108. /* Update number of bits */
  109. if ((context->count[0] += ((UINT4)inputLen << 3))
  110. < ((UINT4)inputLen << 3))
  111. context->count[1]++;
  112. context->count[1] += ((UINT4)inputLen >> 29);
  113. partLen = 64 - index;
  114. /* Transform as many times as possible.
  115. */
  116. if (inputLen >= partLen) {
  117. MD4_memcpy
  118. ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  119. MD4Transform (context->state, context->buffer);
  120. for (i = partLen; i + 63 < inputLen; i += 64)
  121. MD4Transform (context->state, &input[i]);
  122. index = 0;
  123. }
  124. else
  125. i = 0;
  126. /* Buffer remaining input */
  127. MD4_memcpy
  128. ((POINTER)&context->buffer[index], (POINTER)&input[i],
  129. inputLen-i);
  130. }
  131. /* MD4 finalization. Ends an MD4 message-digest operation, writing the
  132. the message digest and zeroizing the context.
  133. */
  134. void MD4Final (digest, context)
  135. unsigned char digest[16]; /* message digest */
  136. MD4_CTX *context; /* context */
  137. {
  138. unsigned char bits[8];
  139. unsigned int index, padLen;
  140. /* Save number of bits */
  141. Encode (bits, context->count, 8);
  142. /* Pad out to 56 mod 64.
  143. */
  144. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  145. padLen = (index < 56) ? (56 - index) : (120 - index);
  146. MD4Update (context, PADDING, padLen);
  147. /* Append length (before padding) */
  148. MD4Update (context, bits, 8);
  149. /* Store state in digest */
  150. Encode (digest, context->state, 16);
  151. /* Zeroize sensitive information.
  152. */
  153. MD4_memset ((POINTER)context, 0, sizeof (*context));
  154. }
  155. /* MD4 basic transformation. Transforms state based on block.
  156. */
  157. static void MD4Transform (state, block)
  158. UINT4 state[4];
  159. unsigned char block[64];
  160. {
  161. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  162. Decode (x, block, 64);
  163. /* Round 1 */
  164. FF (a, b, c, d, x[ 0], S11); /* 1 */
  165. FF (d, a, b, c, x[ 1], S12); /* 2 */
  166. FF (c, d, a, b, x[ 2], S13); /* 3 */
  167. FF (b, c, d, a, x[ 3], S14); /* 4 */
  168. FF (a, b, c, d, x[ 4], S11); /* 5 */
  169. FF (d, a, b, c, x[ 5], S12); /* 6 */
  170. FF (c, d, a, b, x[ 6], S13); /* 7 */
  171. FF (b, c, d, a, x[ 7], S14); /* 8 */
  172. FF (a, b, c, d, x[ 8], S11); /* 9 */
  173. FF (d, a, b, c, x[ 9], S12); /* 10 */
  174. FF (c, d, a, b, x[10], S13); /* 11 */
  175. FF (b, c, d, a, x[11], S14); /* 12 */
  176. FF (a, b, c, d, x[12], S11); /* 13 */
  177. FF (d, a, b, c, x[13], S12); /* 14 */
  178. FF (c, d, a, b, x[14], S13); /* 15 */
  179. FF (b, c, d, a, x[15], S14); /* 16 */
  180. /* Round 2 */
  181. GG (a, b, c, d, x[ 0], S21); /* 17 */
  182. GG (d, a, b, c, x[ 4], S22); /* 18 */
  183. GG (c, d, a, b, x[ 8], S23); /* 19 */
  184. GG (b, c, d, a, x[12], S24); /* 20 */
  185. GG (a, b, c, d, x[ 1], S21); /* 21 */
  186. GG (d, a, b, c, x[ 5], S22); /* 22 */
  187. GG (c, d, a, b, x[ 9], S23); /* 23 */
  188. GG (b, c, d, a, x[13], S24); /* 24 */
  189. GG (a, b, c, d, x[ 2], S21); /* 25 */
  190. GG (d, a, b, c, x[ 6], S22); /* 26 */
  191. GG (c, d, a, b, x[10], S23); /* 27 */
  192. GG (b, c, d, a, x[14], S24); /* 28 */
  193. GG (a, b, c, d, x[ 3], S21); /* 29 */
  194. GG (d, a, b, c, x[ 7], S22); /* 30 */
  195. GG (c, d, a, b, x[11], S23); /* 31 */
  196. GG (b, c, d, a, x[15], S24); /* 32 */
  197. /* Round 3 */
  198. HH (a, b, c, d, x[ 0], S31); /* 33 */
  199. HH (d, a, b, c, x[ 8], S32); /* 34 */
  200. HH (c, d, a, b, x[ 4], S33); /* 35 */
  201. HH (b, c, d, a, x[12], S34); /* 36 */
  202. HH (a, b, c, d, x[ 2], S31); /* 37 */
  203. HH (d, a, b, c, x[10], S32); /* 38 */
  204. HH (c, d, a, b, x[ 6], S33); /* 39 */
  205. HH (b, c, d, a, x[14], S34); /* 40 */
  206. HH (a, b, c, d, x[ 1], S31); /* 41 */
  207. HH (d, a, b, c, x[ 9], S32); /* 42 */
  208. HH (c, d, a, b, x[ 5], S33); /* 43 */
  209. HH (b, c, d, a, x[13], S34); /* 44 */
  210. HH (a, b, c, d, x[ 3], S31); /* 45 */
  211. HH (d, a, b, c, x[11], S32); /* 46 */
  212. HH (c, d, a, b, x[ 7], S33); /* 47 */
  213. HH (b, c, d, a, x[15], S34); /* 48 */
  214. state[0] += a;
  215. state[1] += b;
  216. state[2] += c;
  217. state[3] += d;
  218. /* Zeroize sensitive information.
  219. */
  220. MD4_memset ((POINTER)x, 0, sizeof (x));
  221. }
  222. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  223. a multiple of 4.
  224. */
  225. static void Encode (output, input, len)
  226. unsigned char *output;
  227. UINT4 *input;
  228. unsigned int len;
  229. {
  230. unsigned int i, j;
  231. for (i = 0, j = 0; j < len; i++, j += 4) {
  232. output[j] = (unsigned char)(input[i] & 0xff);
  233. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  234. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  235. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  236. }
  237. }
  238. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  239. a multiple of 4.
  240. */
  241. static void Decode (output, input, len)
  242. UINT4 *output;
  243. unsigned char *input;
  244. unsigned int len;
  245. {
  246. unsigned int i, j;
  247. for (i = 0, j = 0; j < len; i++, j += 4)
  248. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  249. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  250. }
  251. /* Note: Replace "for loop" with standard memcpy if possible.
  252. */
  253. static void MD4_memcpy (output, input, len)
  254. POINTER output;
  255. POINTER input;
  256. unsigned int len;
  257. {
  258. unsigned int i;
  259. for (i = 0; i < len; i++)
  260. output[i] = input[i];
  261. }
  262. /* Note: Replace "for loop" with standard memset if possible.
  263. */
  264. static void MD4_memset (output, value, len)
  265. POINTER output;
  266. int value;
  267. unsigned int len;
  268. {
  269. unsigned int i;
  270. for (i = 0; i < len; i++)
  271. ((char *)output)[i] = (char)value;
  272. }