md4.c 9.0 KB

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