MD4.cpp 8.1 KB

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