md5.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. $Id$
  3. MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  4. Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  5. rights reserved.
  6. License to copy and use this software is granted provided that it
  7. is identified as the "RSA Data Security, Inc. MD5 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. MD5 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 <string.h>
  22. #include "md5global.h"
  23. #include "md5.h"
  24. #define USE_MEM
  25. /* Constants for MD5Transform routine.
  26. */
  27. #define S11 7
  28. #define S12 12
  29. #define S13 17
  30. #define S14 22
  31. #define S21 5
  32. #define S22 9
  33. #define S23 14
  34. #define S24 20
  35. #define S31 4
  36. #define S32 11
  37. #define S33 16
  38. #define S34 23
  39. #define S41 6
  40. #define S42 10
  41. #define S43 15
  42. #define S44 21
  43. static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
  44. static void Encode PROTO_LIST
  45. ((unsigned char *, UINT4 *, unsigned int));
  46. static void Decode PROTO_LIST
  47. ((UINT4 *, unsigned char *, unsigned int));
  48. static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
  49. static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
  50. static unsigned char PADDING[64] = {
  51. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  54. };
  55. /* F, G, H and I are basic MD5 functions.
  56. */
  57. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  58. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  59. #define H(x, y, z) ((x) ^ (y) ^ (z))
  60. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  61. /* ROTATE_LEFT rotates x left n bits.
  62. */
  63. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  64. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  65. Rotation is separate from addition to prevent recomputation.
  66. */
  67. #define FF(a, b, c, d, x, s, ac) { \
  68. (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  69. (a) = ROTATE_LEFT ((a), (s)); \
  70. (a) += (b); \
  71. }
  72. #define GG(a, b, c, d, x, s, ac) { \
  73. (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  74. (a) = ROTATE_LEFT ((a), (s)); \
  75. (a) += (b); \
  76. }
  77. #define HH(a, b, c, d, x, s, ac) { \
  78. (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  79. (a) = ROTATE_LEFT ((a), (s)); \
  80. (a) += (b); \
  81. }
  82. #define II(a, b, c, d, x, s, ac) { \
  83. (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  84. (a) = ROTATE_LEFT ((a), (s)); \
  85. (a) += (b); \
  86. }
  87. /* MD5 initialization. Begins an MD5 operation, writing a new context.
  88. */
  89. void MD5Init (context)
  90. MD5_CTX *context; /* context */
  91. {
  92. context->count[0] = context->count[1] = 0;
  93. /* Load magic initialization constants.
  94. */
  95. context->state[0] = 0x67452301;
  96. context->state[1] = 0xefcdab89;
  97. context->state[2] = 0x98badcfe;
  98. context->state[3] = 0x10325476;
  99. }
  100. /* MD5 block update operation. Continues an MD5 message-digest
  101. operation, processing another message block, and updating the
  102. context.
  103. */
  104. void MD5Update (context, input, inputLen)
  105. MD5_CTX *context; /* context */
  106. unsigned char *input; /* input block */
  107. unsigned int inputLen; /* length of input block */
  108. {
  109. unsigned int i, index, partLen;
  110. /* Compute number of bytes mod 64 */
  111. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  112. /* Update number of bits */
  113. if ((context->count[0] += ((UINT4)inputLen << 3))
  114. < ((UINT4)inputLen << 3))
  115. context->count[1]++;
  116. context->count[1] += ((UINT4)inputLen >> 29);
  117. partLen = 64 - index;
  118. /* Transform as many times as possible.
  119. */
  120. if (inputLen >= partLen) {
  121. MD5_memcpy
  122. ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  123. MD5Transform (context->state, context->buffer);
  124. for (i = partLen; i + 63 < inputLen; i += 64)
  125. MD5Transform (context->state, &input[i]);
  126. index = 0;
  127. }
  128. else
  129. i = 0;
  130. /* Buffer remaining input */
  131. MD5_memcpy
  132. ((POINTER)&context->buffer[index], (POINTER)&input[i],
  133. inputLen-i);
  134. }
  135. /* MD5 finalization. Ends an MD5 message-digest operation, writing the
  136. the message digest and zeroizing the context.
  137. */
  138. void MD5Final (digest, context)
  139. unsigned char digest[16]; /* message digest */
  140. MD5_CTX *context; /* context */
  141. {
  142. unsigned char bits[8];
  143. unsigned int index, padLen;
  144. /* Save number of bits */
  145. Encode (bits, context->count, 8);
  146. /* Pad out to 56 mod 64.
  147. */
  148. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  149. padLen = (index < 56) ? (56 - index) : (120 - index);
  150. MD5Update (context, PADDING, padLen);
  151. /* Append length (before padding) */
  152. MD5Update (context, bits, 8);
  153. /* Store state in digest */
  154. Encode (digest, context->state, 16);
  155. /* Zeroize sensitive information.
  156. */
  157. MD5_memset ((POINTER)context, 0, sizeof (*context));
  158. }
  159. /* MD5 basic transformation. Transforms state based on block.
  160. */
  161. static void MD5Transform (state, block)
  162. UINT4 state[4];
  163. unsigned char block[64];
  164. {
  165. UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  166. Decode (x, block, 64);
  167. /* Round 1 */
  168. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  169. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  170. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  171. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  172. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  173. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  174. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  175. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  176. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  177. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  178. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  179. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  180. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  181. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  182. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  183. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  184. /* Round 2 */
  185. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  186. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  187. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  188. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  189. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  190. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  191. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  192. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  193. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  194. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  195. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  196. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  197. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  198. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  199. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  200. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  201. /* Round 3 */
  202. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  203. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  204. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  205. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  206. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  207. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  208. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  209. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  210. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  211. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  212. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  213. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  214. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  215. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  216. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  217. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  218. /* Round 4 */
  219. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  220. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  221. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  222. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  223. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  224. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  225. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  226. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  227. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  228. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  229. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  230. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  231. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  232. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  233. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  234. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  235. state[0] += a;
  236. state[1] += b;
  237. state[2] += c;
  238. state[3] += d;
  239. /* Zeroize sensitive information.
  240. */
  241. MD5_memset ((POINTER)x, 0, sizeof (x));
  242. }
  243. /* Encodes input (UINT4) into output (unsigned char). Assumes len is
  244. a multiple of 4.
  245. */
  246. static void Encode (output, input, len)
  247. unsigned char *output;
  248. UINT4 *input;
  249. unsigned int len;
  250. {
  251. unsigned int i, j;
  252. for (i = 0, j = 0; j < len; i++, j += 4) {
  253. output[j] = (unsigned char)(input[i] & 0xff);
  254. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  255. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  256. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  257. }
  258. }
  259. /* Decodes input (unsigned char) into output (UINT4). Assumes len is
  260. a multiple of 4.
  261. */
  262. static void Decode (output, input, len)
  263. UINT4 *output;
  264. unsigned char *input;
  265. unsigned int len;
  266. {
  267. unsigned int i, j;
  268. for (i = 0, j = 0; j < len; i++, j += 4)
  269. output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
  270. (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  271. }
  272. /* Note: Replace "for loop" with standard memcpy if possible.
  273. */
  274. static void MD5_memcpy (output, input, len)
  275. POINTER output;
  276. POINTER input;
  277. unsigned int len;
  278. {
  279. #ifndef USE_MEM
  280. unsigned int i;
  281. for (i = 0; i < len; i++)
  282. output[i] = input[i];
  283. #else
  284. memcpy( output, input, len );
  285. #endif
  286. }
  287. /* Note: Replace "for loop" with standard memset if possible.
  288. */
  289. static void MD5_memset (output, value, len)
  290. POINTER output;
  291. int value;
  292. unsigned int len;
  293. {
  294. #ifndef USE_MEM
  295. unsigned int i;
  296. for (i = 0; i < len; i++)
  297. ((char *)output)[i] = (char)value;
  298. #else
  299. memset( output, value, len );
  300. #endif
  301. }