md5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. ***********************************************************************
  3. ** md5.c -- the source code for MD5 routines **
  4. ** RSA Data Security, Inc. MD5 Message-Digest Algorithm **
  5. ** Created: 2/17/90 RLR **
  6. ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
  7. ** Revised (for MD5): RLR 4/27/91 **
  8. ** -- G modified to have y&~z instead of y&z **
  9. ** -- FF, GG, HH modified to add in last register done **
  10. ** -- Access pattern: round 2 works mod 5, round 3 works mod 3 **
  11. ** -- distinct additive constant for each step **
  12. ** -- round 4 added, working mod 7 **
  13. ***********************************************************************
  14. */
  15. /*
  16. ***********************************************************************
  17. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  18. ** **
  19. ** License to copy and use this software is granted provided that **
  20. ** it is identified as the "RSA Data Security, Inc. MD5 Message- **
  21. ** Digest Algorithm" in all material mentioning or referencing this **
  22. ** software or this function. **
  23. ** **
  24. ** License is also granted to make and use derivative works **
  25. ** provided that such works are identified as "derived from the RSA **
  26. ** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
  27. ** material mentioning or referencing the derived work. **
  28. ** **
  29. ** RSA Data Security, Inc. makes no representations concerning **
  30. ** either the merchantability of this software or the suitability **
  31. ** of this software for any particular purpose. It is provided "as **
  32. ** is" without express or implied warranty of any kind. **
  33. ** **
  34. ** These notices must be retained in any copies of any part of this **
  35. ** documentation and/or software. **
  36. ***********************************************************************
  37. */
  38. #include "md5.h"
  39. /*
  40. ***********************************************************************
  41. ** Message-digest routines: **
  42. ** To form the message digest for a message M **
  43. ** (1) Initialize a context buffer mdContext using MD5Init **
  44. ** (2) Call MD5Update on mdContext and M **
  45. ** (3) Call MD5Final on mdContext **
  46. ** The message digest is now in mdContext->digest[0...15] **
  47. ***********************************************************************
  48. */
  49. /* forward declaration */
  50. static void Transform (UINT4 *, UINT4 *);
  51. #ifdef __STDC__
  52. static const
  53. #else
  54. static
  55. #endif
  56. unsigned char PADDING[64] = {
  57. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  65. };
  66. /* F, G, H and I are basic MD5 functions */
  67. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  68. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  69. #define H(x, y, z) ((x) ^ (y) ^ (z))
  70. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  71. /* ROTATE_LEFT rotates x left n bits */
  72. #if defined(FAST_MD5) && defined(__GNUC__) && defined(mc68000)
  73. /*
  74. * If we're on a 68000 based CPU and using a GNU C compiler with
  75. * inline assembly code, we can speed this up a bit.
  76. */
  77. inline UINT4 ROTATE_LEFT(UINT4 x, int n)
  78. {
  79. asm("roll %2,%0" : "=d" (x) : "0" (x), "Ir" (n));
  80. return x;
  81. }
  82. #else
  83. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  84. #endif
  85. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  86. /* Rotation is separate from addition to prevent recomputation */
  87. #define FF(a, b, c, d, x, s, ac) \
  88. {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  89. (a) = ROTATE_LEFT ((a), (s)); \
  90. (a) += (b); \
  91. }
  92. #define GG(a, b, c, d, x, s, ac) \
  93. {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  94. (a) = ROTATE_LEFT ((a), (s)); \
  95. (a) += (b); \
  96. }
  97. #define HH(a, b, c, d, x, s, ac) \
  98. {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  99. (a) = ROTATE_LEFT ((a), (s)); \
  100. (a) += (b); \
  101. }
  102. #define II(a, b, c, d, x, s, ac) \
  103. {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  104. (a) = ROTATE_LEFT ((a), (s)); \
  105. (a) += (b); \
  106. }
  107. /* The routine MD5Init initializes the message-digest context
  108. mdContext. All fields are set to zero.
  109. */
  110. void MD5Init (mdContext)
  111. MD5_CTX *mdContext;
  112. {
  113. mdContext->i[0] = mdContext->i[1] = (UINT4)0;
  114. /* Load magic initialization constants.
  115. */
  116. mdContext->buf[0] = (UINT4)0x67452301;
  117. mdContext->buf[1] = (UINT4)0xefcdab89;
  118. mdContext->buf[2] = (UINT4)0x98badcfe;
  119. mdContext->buf[3] = (UINT4)0x10325476;
  120. }
  121. /* The routine MD5Update updates the message-digest context to
  122. account for the presence of each of the characters inBuf[0..inLen-1]
  123. in the message whose digest is being computed.
  124. */
  125. void MD5Update (mdContext, inBuf, inLen)
  126. MD5_CTX *mdContext;
  127. unsigned const char *inBuf;
  128. unsigned int inLen;
  129. {
  130. UINT4 in[16];
  131. int mdi;
  132. unsigned int i, ii;
  133. /* compute number of bytes mod 64 */
  134. mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  135. /* update number of bits */
  136. if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
  137. mdContext->i[1]++;
  138. mdContext->i[0] += ((UINT4)inLen << 3);
  139. mdContext->i[1] += ((UINT4)inLen >> 29);
  140. while (inLen--) {
  141. /* add new character to buffer, increment mdi */
  142. mdContext->in[mdi++] = *inBuf++;
  143. /* transform if necessary */
  144. if (mdi == 0x40) {
  145. for (i = 0, ii = 0; i < 16; i++, ii += 4)
  146. in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  147. (((UINT4)mdContext->in[ii+2]) << 16) |
  148. (((UINT4)mdContext->in[ii+1]) << 8) |
  149. ((UINT4)mdContext->in[ii]);
  150. Transform (mdContext->buf, in);
  151. mdi = 0;
  152. }
  153. }
  154. }
  155. /* The routine MD5Final terminates the message-digest computation and
  156. ends with the desired message digest in mdContext->digest[0...15].
  157. */
  158. void MD5Final (mdContext)
  159. MD5_CTX *mdContext;
  160. {
  161. UINT4 in[16];
  162. int mdi;
  163. unsigned int i, ii;
  164. unsigned int padLen;
  165. /* save number of bits */
  166. in[14] = mdContext->i[0];
  167. in[15] = mdContext->i[1];
  168. /* compute number of bytes mod 64 */
  169. mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  170. /* pad out to 56 mod 64 */
  171. padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
  172. MD5Update (mdContext, PADDING, padLen);
  173. /* append length in bits and transform */
  174. for (i = 0, ii = 0; i < 14; i++, ii += 4)
  175. in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  176. (((UINT4)mdContext->in[ii+2]) << 16) |
  177. (((UINT4)mdContext->in[ii+1]) << 8) |
  178. ((UINT4)mdContext->in[ii]);
  179. Transform (mdContext->buf, in);
  180. /* store buffer in digest */
  181. for (i = 0, ii = 0; i < 4; i++, ii += 4) {
  182. mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
  183. mdContext->digest[ii+1] =
  184. (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
  185. mdContext->digest[ii+2] =
  186. (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
  187. mdContext->digest[ii+3] =
  188. (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
  189. }
  190. }
  191. /* Basic MD5 step. Transforms buf based on in.
  192. */
  193. static void Transform (buf, in)
  194. UINT4 *buf;
  195. UINT4 *in;
  196. {
  197. UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  198. /* Round 1 */
  199. #define S11 7
  200. #define S12 12
  201. #define S13 17
  202. #define S14 22
  203. FF ( a, b, c, d, in[ 0], S11, 0xd76aa478); /* 1 */
  204. FF ( d, a, b, c, in[ 1], S12, 0xe8c7b756); /* 2 */
  205. FF ( c, d, a, b, in[ 2], S13, 0x242070db); /* 3 */
  206. FF ( b, c, d, a, in[ 3], S14, 0xc1bdceee); /* 4 */
  207. FF ( a, b, c, d, in[ 4], S11, 0xf57c0faf); /* 5 */
  208. FF ( d, a, b, c, in[ 5], S12, 0x4787c62a); /* 6 */
  209. FF ( c, d, a, b, in[ 6], S13, 0xa8304613); /* 7 */
  210. FF ( b, c, d, a, in[ 7], S14, 0xfd469501); /* 8 */
  211. FF ( a, b, c, d, in[ 8], S11, 0x698098d8); /* 9 */
  212. FF ( d, a, b, c, in[ 9], S12, 0x8b44f7af); /* 10 */
  213. FF ( c, d, a, b, in[10], S13, 0xffff5bb1); /* 11 */
  214. FF ( b, c, d, a, in[11], S14, 0x895cd7be); /* 12 */
  215. FF ( a, b, c, d, in[12], S11, 0x6b901122); /* 13 */
  216. FF ( d, a, b, c, in[13], S12, 0xfd987193); /* 14 */
  217. FF ( c, d, a, b, in[14], S13, 0xa679438e); /* 15 */
  218. FF ( b, c, d, a, in[15], S14, 0x49b40821); /* 16 */
  219. /* Round 2 */
  220. #define S21 5
  221. #define S22 9
  222. #define S23 14
  223. #define S24 20
  224. GG ( a, b, c, d, in[ 1], S21, 0xf61e2562); /* 17 */
  225. GG ( d, a, b, c, in[ 6], S22, 0xc040b340); /* 18 */
  226. GG ( c, d, a, b, in[11], S23, 0x265e5a51); /* 19 */
  227. GG ( b, c, d, a, in[ 0], S24, 0xe9b6c7aa); /* 20 */
  228. GG ( a, b, c, d, in[ 5], S21, 0xd62f105d); /* 21 */
  229. GG ( d, a, b, c, in[10], S22, 0x2441453); /* 22 */
  230. GG ( c, d, a, b, in[15], S23, 0xd8a1e681); /* 23 */
  231. GG ( b, c, d, a, in[ 4], S24, 0xe7d3fbc8); /* 24 */
  232. GG ( a, b, c, d, in[ 9], S21, 0x21e1cde6); /* 25 */
  233. GG ( d, a, b, c, in[14], S22, 0xc33707d6); /* 26 */
  234. GG ( c, d, a, b, in[ 3], S23, 0xf4d50d87); /* 27 */
  235. GG ( b, c, d, a, in[ 8], S24, 0x455a14ed); /* 28 */
  236. GG ( a, b, c, d, in[13], S21, 0xa9e3e905); /* 29 */
  237. GG ( d, a, b, c, in[ 2], S22, 0xfcefa3f8); /* 30 */
  238. GG ( c, d, a, b, in[ 7], S23, 0x676f02d9); /* 31 */
  239. GG ( b, c, d, a, in[12], S24, 0x8d2a4c8a); /* 32 */
  240. /* Round 3 */
  241. #define S31 4
  242. #define S32 11
  243. #define S33 16
  244. #define S34 23
  245. HH ( a, b, c, d, in[ 5], S31, 0xfffa3942); /* 33 */
  246. HH ( d, a, b, c, in[ 8], S32, 0x8771f681); /* 34 */
  247. HH ( c, d, a, b, in[11], S33, 0x6d9d6122); /* 35 */
  248. HH ( b, c, d, a, in[14], S34, 0xfde5380c); /* 36 */
  249. HH ( a, b, c, d, in[ 1], S31, 0xa4beea44); /* 37 */
  250. HH ( d, a, b, c, in[ 4], S32, 0x4bdecfa9); /* 38 */
  251. HH ( c, d, a, b, in[ 7], S33, 0xf6bb4b60); /* 39 */
  252. HH ( b, c, d, a, in[10], S34, 0xbebfbc70); /* 40 */
  253. HH ( a, b, c, d, in[13], S31, 0x289b7ec6); /* 41 */
  254. HH ( d, a, b, c, in[ 0], S32, 0xeaa127fa); /* 42 */
  255. HH ( c, d, a, b, in[ 3], S33, 0xd4ef3085); /* 43 */
  256. HH ( b, c, d, a, in[ 6], S34, 0x4881d05); /* 44 */
  257. HH ( a, b, c, d, in[ 9], S31, 0xd9d4d039); /* 45 */
  258. HH ( d, a, b, c, in[12], S32, 0xe6db99e5); /* 46 */
  259. HH ( c, d, a, b, in[15], S33, 0x1fa27cf8); /* 47 */
  260. HH ( b, c, d, a, in[ 2], S34, 0xc4ac5665); /* 48 */
  261. /* Round 4 */
  262. #define S41 6
  263. #define S42 10
  264. #define S43 15
  265. #define S44 21
  266. II ( a, b, c, d, in[ 0], S41, 0xf4292244); /* 49 */
  267. II ( d, a, b, c, in[ 7], S42, 0x432aff97); /* 50 */
  268. II ( c, d, a, b, in[14], S43, 0xab9423a7); /* 51 */
  269. II ( b, c, d, a, in[ 5], S44, 0xfc93a039); /* 52 */
  270. II ( a, b, c, d, in[12], S41, 0x655b59c3); /* 53 */
  271. II ( d, a, b, c, in[ 3], S42, 0x8f0ccc92); /* 54 */
  272. II ( c, d, a, b, in[10], S43, 0xffeff47d); /* 55 */
  273. II ( b, c, d, a, in[ 1], S44, 0x85845dd1); /* 56 */
  274. II ( a, b, c, d, in[ 8], S41, 0x6fa87e4f); /* 57 */
  275. II ( d, a, b, c, in[15], S42, 0xfe2ce6e0); /* 58 */
  276. II ( c, d, a, b, in[ 6], S43, 0xa3014314); /* 59 */
  277. II ( b, c, d, a, in[13], S44, 0x4e0811a1); /* 60 */
  278. II ( a, b, c, d, in[ 4], S41, 0xf7537e82); /* 61 */
  279. II ( d, a, b, c, in[11], S42, 0xbd3af235); /* 62 */
  280. II ( c, d, a, b, in[ 2], S43, 0x2ad7d2bb); /* 63 */
  281. II ( b, c, d, a, in[ 9], S44, 0xeb86d391); /* 64 */
  282. buf[0] += a;
  283. buf[1] += b;
  284. buf[2] += c;
  285. buf[3] += d;
  286. }
  287. /*
  288. ***********************************************************************
  289. ** End of md5.c **
  290. ******************************** (cut) ********************************
  291. */