md4.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Free implementation of the MD4 hash algorithm
  2. // Original header in MD4C.C and MD4.h:
  3. // MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
  4. /*
  5. Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
  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. License is also granted to make and use derivative works provided
  11. that such works are identified as "derived from the RSA Data
  12. Security, Inc. MD4 Message-Digest Algorithm" in all material
  13. mentioning or referencing the derived work.
  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. These notices must be retained in any copies of any part of this
  19. documentation and/or software.
  20. */
  21. #include <memory.h>
  22. #include "MD4.h"
  23. // Constants for MD4_Transform routine.
  24. #define MD4_S11 3
  25. #define MD4_S12 7
  26. #define MD4_S13 11
  27. #define MD4_S14 19
  28. #define MD4_S21 3
  29. #define MD4_S22 5
  30. #define MD4_S23 9
  31. #define MD4_S24 13
  32. #define MD4_S31 3
  33. #define MD4_S32 9
  34. #define MD4_S33 11
  35. #define MD4_S34 15
  36. static void MD4_Transform(UINT4 *state, unsigned char *block);
  37. static void MD4_Encode(unsigned char *output, UINT4 *input, unsigned int len);
  38. static void MD4_Decode(UINT4 *output, unsigned char *input, unsigned int len);
  39. static unsigned char MD4_PADDING[64] = {
  40. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  43. };
  44. // MD4F, MD4G and MD4H are basic MD4 functions.
  45. #define MD4F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  46. #define MD4G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  47. #define MD4H(x, y, z) ((x) ^ (y) ^ (z))
  48. // MD4_ROTL rotates x left n bits.
  49. #define MD4_ROTL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  50. // MD4FF, MD4GG and MD4HH are transformations for rounds 1, 2 and 3
  51. // Rotation is separate from addition to prevent recomputation
  52. #define MD4FF(a, b, c, d, x, s) { \
  53. (a) += MD4F ((b), (c), (d)) + (x); \
  54. (a) = MD4_ROTL ((a), (s)); \
  55. }
  56. #define MD4GG(a, b, c, d, x, s) { \
  57. (a) += MD4G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
  58. (a) = MD4_ROTL ((a), (s)); \
  59. }
  60. #define MD4HH(a, b, c, d, x, s) { \
  61. (a) += MD4H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
  62. (a) = MD4_ROTL ((a), (s)); \
  63. }
  64. // MD4 initialization. Begins an MD4 operation, writing a new context
  65. void MD4Init(MD4_CTX *context)
  66. {
  67. context->count[0] = context->count[1] = 0;
  68. // Load magic initialization constants
  69. context->state[0] = 0x67452301;
  70. context->state[1] = 0xefcdab89;
  71. context->state[2] = 0x98badcfe;
  72. context->state[3] = 0x10325476;
  73. }
  74. // MD4 block update operation. Continues an MD4 message-digest
  75. // operation, processing another message block, and updating the
  76. // context
  77. void MD4Update(MD4_CTX *context, unsigned char *input, unsigned int inputLen)
  78. {
  79. unsigned int i = 0, index = 0, partLen = 0;
  80. // Compute number of bytes mod 64
  81. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  82. // Update number of bits
  83. if ((context->count[0] += ((UINT4)inputLen << 3))
  84. < ((UINT4)inputLen << 3))
  85. context->count[1]++;
  86. context->count[1] += ((UINT4)inputLen >> 29);
  87. partLen = 64 - index;
  88. // Transform as many times as possible
  89. if (inputLen >= partLen)
  90. {
  91. memcpy((MD4_POINTER)&context->buffer[index], (MD4_POINTER)input, partLen);
  92. MD4_Transform(context->state, context->buffer);
  93. for (i = partLen; i + 63 < inputLen; i += 64)
  94. MD4_Transform (context->state, &input[i]);
  95. index = 0;
  96. }
  97. else i = 0;
  98. // Buffer remaining input
  99. memcpy((MD4_POINTER)&context->buffer[index], (MD4_POINTER)&input[i], inputLen - i);
  100. }
  101. // MD4 finalization. Ends an MD4 message-digest operation, writing the
  102. // the message digest and zeroizing the context.
  103. void MD4Final(unsigned char *digest, MD4_CTX *context)
  104. {
  105. unsigned char bits[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  106. unsigned int index = 0, padLen = 0;
  107. // Save number of bits
  108. MD4_Encode (bits, context->count, 8);
  109. // Pad out to 56 mod 64.
  110. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  111. padLen = (index < 56) ? (56 - index) : (120 - index);
  112. MD4Update(context, MD4_PADDING, padLen);
  113. // Append length (before padding)
  114. MD4Update(context, bits, 8);
  115. // Store state in digest
  116. MD4_Encode(digest, context->state, 16);
  117. // Zeroize sensitive information
  118. memset((MD4_POINTER)context, 0, sizeof(MD4_CTX));
  119. }
  120. // MD4 basic transformation. Transforms state based on block.
  121. static void MD4_Transform(UINT4 *state, unsigned char *block)
  122. {
  123. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  124. MD4_Decode(x, block, 64);
  125. // Round 1
  126. MD4FF (a, b, c, d, x[ 0], MD4_S11); /* 1 */
  127. MD4FF (d, a, b, c, x[ 1], MD4_S12); /* 2 */
  128. MD4FF (c, d, a, b, x[ 2], MD4_S13); /* 3 */
  129. MD4FF (b, c, d, a, x[ 3], MD4_S14); /* 4 */
  130. MD4FF (a, b, c, d, x[ 4], MD4_S11); /* 5 */
  131. MD4FF (d, a, b, c, x[ 5], MD4_S12); /* 6 */
  132. MD4FF (c, d, a, b, x[ 6], MD4_S13); /* 7 */
  133. MD4FF (b, c, d, a, x[ 7], MD4_S14); /* 8 */
  134. MD4FF (a, b, c, d, x[ 8], MD4_S11); /* 9 */
  135. MD4FF (d, a, b, c, x[ 9], MD4_S12); /* 10 */
  136. MD4FF (c, d, a, b, x[10], MD4_S13); /* 11 */
  137. MD4FF (b, c, d, a, x[11], MD4_S14); /* 12 */
  138. MD4FF (a, b, c, d, x[12], MD4_S11); /* 13 */
  139. MD4FF (d, a, b, c, x[13], MD4_S12); /* 14 */
  140. MD4FF (c, d, a, b, x[14], MD4_S13); /* 15 */
  141. MD4FF (b, c, d, a, x[15], MD4_S14); /* 16 */
  142. // Round 2
  143. MD4GG (a, b, c, d, x[ 0], MD4_S21); /* 17 */
  144. MD4GG (d, a, b, c, x[ 4], MD4_S22); /* 18 */
  145. MD4GG (c, d, a, b, x[ 8], MD4_S23); /* 19 */
  146. MD4GG (b, c, d, a, x[12], MD4_S24); /* 20 */
  147. MD4GG (a, b, c, d, x[ 1], MD4_S21); /* 21 */
  148. MD4GG (d, a, b, c, x[ 5], MD4_S22); /* 22 */
  149. MD4GG (c, d, a, b, x[ 9], MD4_S23); /* 23 */
  150. MD4GG (b, c, d, a, x[13], MD4_S24); /* 24 */
  151. MD4GG (a, b, c, d, x[ 2], MD4_S21); /* 25 */
  152. MD4GG (d, a, b, c, x[ 6], MD4_S22); /* 26 */
  153. MD4GG (c, d, a, b, x[10], MD4_S23); /* 27 */
  154. MD4GG (b, c, d, a, x[14], MD4_S24); /* 28 */
  155. MD4GG (a, b, c, d, x[ 3], MD4_S21); /* 29 */
  156. MD4GG (d, a, b, c, x[ 7], MD4_S22); /* 30 */
  157. MD4GG (c, d, a, b, x[11], MD4_S23); /* 31 */
  158. MD4GG (b, c, d, a, x[15], MD4_S24); /* 32 */
  159. // Round 3
  160. MD4HH (a, b, c, d, x[ 0], MD4_S31); /* 33 */
  161. MD4HH (d, a, b, c, x[ 8], MD4_S32); /* 34 */
  162. MD4HH (c, d, a, b, x[ 4], MD4_S33); /* 35 */
  163. MD4HH (b, c, d, a, x[12], MD4_S34); /* 36 */
  164. MD4HH (a, b, c, d, x[ 2], MD4_S31); /* 37 */
  165. MD4HH (d, a, b, c, x[10], MD4_S32); /* 38 */
  166. MD4HH (c, d, a, b, x[ 6], MD4_S33); /* 39 */
  167. MD4HH (b, c, d, a, x[14], MD4_S34); /* 40 */
  168. MD4HH (a, b, c, d, x[ 1], MD4_S31); /* 41 */
  169. MD4HH (d, a, b, c, x[ 9], MD4_S32); /* 42 */
  170. MD4HH (c, d, a, b, x[ 5], MD4_S33); /* 43 */
  171. MD4HH (b, c, d, a, x[13], MD4_S34); /* 44 */
  172. MD4HH (a, b, c, d, x[ 3], MD4_S31); /* 45 */
  173. MD4HH (d, a, b, c, x[11], MD4_S32); /* 46 */
  174. MD4HH (c, d, a, b, x[ 7], MD4_S33); /* 47 */
  175. MD4HH (b, c, d, a, x[15], MD4_S34); /* 48 */
  176. state[0] += a;
  177. state[1] += b;
  178. state[2] += c;
  179. state[3] += d;
  180. // Zeroize sensitive information.
  181. memset ((MD4_POINTER)x, 0, sizeof(x));
  182. }
  183. // MD4_Encodes input (UINT4) into output (unsigned char). Assumes len is
  184. // a multiple of 4.
  185. static void MD4_Encode(unsigned char *output, UINT4 *input, unsigned int len)
  186. {
  187. unsigned int i = 0, j = 0;
  188. for (i = 0, j = 0; j < len; i++, j += 4)
  189. {
  190. output[j] = (unsigned char)(input[i] & 0xff);
  191. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  192. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  193. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  194. }
  195. }
  196. // MD4_Decodes input (unsigned char) into output (UINT4). Assumes len is
  197. // a multiple of 4.
  198. static void MD4_Decode(UINT4 *output, unsigned char *input, unsigned int len)
  199. {
  200. unsigned int i = 0, j = 0;
  201. for (i = 0, j = 0; j < len; i++, j += 4)
  202. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  203. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  204. }