md4.c 8.2 KB

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