md5.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 ver., 7/10 constant corr. **
  7. ***********************************************************************
  8. */
  9. /*
  10. ***********************************************************************
  11. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  12. ** **
  13. ** License to copy and use this software is granted provided that **
  14. ** it is identified as the "RSA Data Security, Inc. MD5 Message- **
  15. ** Digest Algorithm" in all material mentioning or referencing this **
  16. ** software or this function. **
  17. ** **
  18. ** License is also granted to make and use derivative works **
  19. ** provided that such works are identified as "derived from the RSA **
  20. ** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
  21. ** material mentioning or referencing the derived work. **
  22. ** **
  23. ** RSA Data Security, Inc. makes no representations concerning **
  24. ** either the merchantability of this software or the suitability **
  25. ** of this software for any particular purpose. It is provided "as **
  26. ** is" without express or implied warranty of any kind. **
  27. ** **
  28. ** These notices must be retained in any copies of any part of this **
  29. ** documentation and/or software. **
  30. ***********************************************************************
  31. */
  32. #include <string.h>
  33. #include "md5.h"
  34. /*
  35. ***********************************************************************
  36. ** Message-digest routines: **
  37. ** To form the message digest for a message M **
  38. ** (1) Initialize a context buffer mdContext using MD5_Init **
  39. ** (2) Call MD5_Update on mdContext and M **
  40. ** (3) Call MD5_Final on mdContext **
  41. ** The message digest is now in mdContext->digest[0...15] **
  42. ***********************************************************************
  43. */
  44. /* forward declaration */
  45. static void Transform ();
  46. static unsigned char PADDING[64] = {
  47. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  55. };
  56. /* F, G, H and I are basic MD5 functions */
  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. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  63. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  64. /* Rotation is separate from addition to prevent recomputation */
  65. #define FF(a, b, c, d, x, s, ac) \
  66. {(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
  67. (a) = ROTATE_LEFT ((a), (s)); \
  68. (a) += (b); \
  69. }
  70. #define GG(a, b, c, d, x, s, ac) \
  71. {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
  72. (a) = ROTATE_LEFT ((a), (s)); \
  73. (a) += (b); \
  74. }
  75. #define HH(a, b, c, d, x, s, ac) \
  76. {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
  77. (a) = ROTATE_LEFT ((a), (s)); \
  78. (a) += (b); \
  79. }
  80. #define II(a, b, c, d, x, s, ac) \
  81. {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
  82. (a) = ROTATE_LEFT ((a), (s)); \
  83. (a) += (b); \
  84. }
  85. #ifdef __STDC__
  86. #define UL(x) x##U
  87. #else
  88. #define UL(x) x
  89. #endif
  90. /* The routine MD5_Init initializes the message-digest context
  91. mdContext. All fields are set to zero.
  92. */
  93. void MD5_Init (mdContext)
  94. MD5_CTX *mdContext;
  95. {
  96. mdContext->i[0] = mdContext->i[1] = (UINT4)0;
  97. /* Load magic initialization constants.
  98. */
  99. mdContext->buf[0] = (UINT4)0x67452301;
  100. mdContext->buf[1] = (UINT4)0xefcdab89;
  101. mdContext->buf[2] = (UINT4)0x98badcfe;
  102. mdContext->buf[3] = (UINT4)0x10325476;
  103. }
  104. /* The routine MD5Update updates the message-digest context to
  105. account for the presence of each of the characters inBuf[0..inLen-1]
  106. in the message whose digest is being computed.
  107. */
  108. void MD5_Update (mdContext, inBuf, inLen)
  109. MD5_CTX *mdContext;
  110. unsigned char *inBuf;
  111. unsigned int inLen;
  112. {
  113. UINT4 in[16];
  114. int mdi;
  115. unsigned int i, ii;
  116. /* compute number of bytes mod 64 */
  117. mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  118. /* update number of bits */
  119. if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
  120. mdContext->i[1]++;
  121. mdContext->i[0] += ((UINT4)inLen << 3);
  122. mdContext->i[1] += ((UINT4)inLen >> 29);
  123. while (inLen--) {
  124. /* add new character to buffer, increment mdi */
  125. mdContext->in[mdi++] = *inBuf++;
  126. /* transform if necessary */
  127. if (mdi == 0x40) {
  128. for (i = 0, ii = 0; i < 16; i++, ii += 4)
  129. in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  130. (((UINT4)mdContext->in[ii+2]) << 16) |
  131. (((UINT4)mdContext->in[ii+1]) << 8) |
  132. ((UINT4)mdContext->in[ii]);
  133. Transform (mdContext->buf, in);
  134. mdi = 0;
  135. }
  136. }
  137. }
  138. /* The routine MD5Final terminates the message-digest computation and
  139. ends with the desired message digest in mdContext->digest[0...15].
  140. */
  141. void MD5_Final (hash, mdContext)
  142. unsigned char hash[];
  143. MD5_CTX *mdContext;
  144. {
  145. UINT4 in[16];
  146. int mdi;
  147. unsigned int i, ii;
  148. unsigned int padLen;
  149. /* save number of bits */
  150. in[14] = mdContext->i[0];
  151. in[15] = mdContext->i[1];
  152. /* compute number of bytes mod 64 */
  153. mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
  154. /* pad out to 56 mod 64 */
  155. padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
  156. MD5_Update (mdContext, PADDING, padLen);
  157. /* append length in bits and transform */
  158. for (i = 0, ii = 0; i < 14; i++, ii += 4)
  159. in[i] = (((UINT4)mdContext->in[ii+3]) << 24) |
  160. (((UINT4)mdContext->in[ii+2]) << 16) |
  161. (((UINT4)mdContext->in[ii+1]) << 8) |
  162. ((UINT4)mdContext->in[ii]);
  163. Transform (mdContext->buf, in);
  164. /* store buffer in digest */
  165. for (i = 0, ii = 0; i < 4; i++, ii += 4) {
  166. mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
  167. mdContext->digest[ii+1] =
  168. (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
  169. mdContext->digest[ii+2] =
  170. (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
  171. mdContext->digest[ii+3] =
  172. (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
  173. }
  174. memcpy(hash, mdContext->digest, 16);
  175. }
  176. /* Basic MD5 step. Transforms buf based on in.
  177. */
  178. static void Transform (buf, in)
  179. UINT4 *buf;
  180. UINT4 *in;
  181. {
  182. UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  183. /* Round 1 */
  184. #define S11 7
  185. #define S12 12
  186. #define S13 17
  187. #define S14 22
  188. FF ( a, b, c, d, in[ 0], S11, UL(3614090360)); /* 1 */
  189. FF ( d, a, b, c, in[ 1], S12, UL(3905402710)); /* 2 */
  190. FF ( c, d, a, b, in[ 2], S13, UL( 606105819)); /* 3 */
  191. FF ( b, c, d, a, in[ 3], S14, UL(3250441966)); /* 4 */
  192. FF ( a, b, c, d, in[ 4], S11, UL(4118548399)); /* 5 */
  193. FF ( d, a, b, c, in[ 5], S12, UL(1200080426)); /* 6 */
  194. FF ( c, d, a, b, in[ 6], S13, UL(2821735955)); /* 7 */
  195. FF ( b, c, d, a, in[ 7], S14, UL(4249261313)); /* 8 */
  196. FF ( a, b, c, d, in[ 8], S11, UL(1770035416)); /* 9 */
  197. FF ( d, a, b, c, in[ 9], S12, UL(2336552879)); /* 10 */
  198. FF ( c, d, a, b, in[10], S13, UL(4294925233)); /* 11 */
  199. FF ( b, c, d, a, in[11], S14, UL(2304563134)); /* 12 */
  200. FF ( a, b, c, d, in[12], S11, UL(1804603682)); /* 13 */
  201. FF ( d, a, b, c, in[13], S12, UL(4254626195)); /* 14 */
  202. FF ( c, d, a, b, in[14], S13, UL(2792965006)); /* 15 */
  203. FF ( b, c, d, a, in[15], S14, UL(1236535329)); /* 16 */
  204. /* Round 2 */
  205. #define S21 5
  206. #define S22 9
  207. #define S23 14
  208. #define S24 20
  209. GG ( a, b, c, d, in[ 1], S21, UL(4129170786)); /* 17 */
  210. GG ( d, a, b, c, in[ 6], S22, UL(3225465664)); /* 18 */
  211. GG ( c, d, a, b, in[11], S23, UL( 643717713)); /* 19 */
  212. GG ( b, c, d, a, in[ 0], S24, UL(3921069994)); /* 20 */
  213. GG ( a, b, c, d, in[ 5], S21, UL(3593408605)); /* 21 */
  214. GG ( d, a, b, c, in[10], S22, UL( 38016083)); /* 22 */
  215. GG ( c, d, a, b, in[15], S23, UL(3634488961)); /* 23 */
  216. GG ( b, c, d, a, in[ 4], S24, UL(3889429448)); /* 24 */
  217. GG ( a, b, c, d, in[ 9], S21, UL( 568446438)); /* 25 */
  218. GG ( d, a, b, c, in[14], S22, UL(3275163606)); /* 26 */
  219. GG ( c, d, a, b, in[ 3], S23, UL(4107603335)); /* 27 */
  220. GG ( b, c, d, a, in[ 8], S24, UL(1163531501)); /* 28 */
  221. GG ( a, b, c, d, in[13], S21, UL(2850285829)); /* 29 */
  222. GG ( d, a, b, c, in[ 2], S22, UL(4243563512)); /* 30 */
  223. GG ( c, d, a, b, in[ 7], S23, UL(1735328473)); /* 31 */
  224. GG ( b, c, d, a, in[12], S24, UL(2368359562)); /* 32 */
  225. /* Round 3 */
  226. #define S31 4
  227. #define S32 11
  228. #define S33 16
  229. #define S34 23
  230. HH ( a, b, c, d, in[ 5], S31, UL(4294588738)); /* 33 */
  231. HH ( d, a, b, c, in[ 8], S32, UL(2272392833)); /* 34 */
  232. HH ( c, d, a, b, in[11], S33, UL(1839030562)); /* 35 */
  233. HH ( b, c, d, a, in[14], S34, UL(4259657740)); /* 36 */
  234. HH ( a, b, c, d, in[ 1], S31, UL(2763975236)); /* 37 */
  235. HH ( d, a, b, c, in[ 4], S32, UL(1272893353)); /* 38 */
  236. HH ( c, d, a, b, in[ 7], S33, UL(4139469664)); /* 39 */
  237. HH ( b, c, d, a, in[10], S34, UL(3200236656)); /* 40 */
  238. HH ( a, b, c, d, in[13], S31, UL( 681279174)); /* 41 */
  239. HH ( d, a, b, c, in[ 0], S32, UL(3936430074)); /* 42 */
  240. HH ( c, d, a, b, in[ 3], S33, UL(3572445317)); /* 43 */
  241. HH ( b, c, d, a, in[ 6], S34, UL( 76029189)); /* 44 */
  242. HH ( a, b, c, d, in[ 9], S31, UL(3654602809)); /* 45 */
  243. HH ( d, a, b, c, in[12], S32, UL(3873151461)); /* 46 */
  244. HH ( c, d, a, b, in[15], S33, UL( 530742520)); /* 47 */
  245. HH ( b, c, d, a, in[ 2], S34, UL(3299628645)); /* 48 */
  246. /* Round 4 */
  247. #define S41 6
  248. #define S42 10
  249. #define S43 15
  250. #define S44 21
  251. II ( a, b, c, d, in[ 0], S41, UL(4096336452)); /* 49 */
  252. II ( d, a, b, c, in[ 7], S42, UL(1126891415)); /* 50 */
  253. II ( c, d, a, b, in[14], S43, UL(2878612391)); /* 51 */
  254. II ( b, c, d, a, in[ 5], S44, UL(4237533241)); /* 52 */
  255. II ( a, b, c, d, in[12], S41, UL(1700485571)); /* 53 */
  256. II ( d, a, b, c, in[ 3], S42, UL(2399980690)); /* 54 */
  257. II ( c, d, a, b, in[10], S43, UL(4293915773)); /* 55 */
  258. II ( b, c, d, a, in[ 1], S44, UL(2240044497)); /* 56 */
  259. II ( a, b, c, d, in[ 8], S41, UL(1873313359)); /* 57 */
  260. II ( d, a, b, c, in[15], S42, UL(4264355552)); /* 58 */
  261. II ( c, d, a, b, in[ 6], S43, UL(2734768916)); /* 59 */
  262. II ( b, c, d, a, in[13], S44, UL(1309151649)); /* 60 */
  263. II ( a, b, c, d, in[ 4], S41, UL(4149444226)); /* 61 */
  264. II ( d, a, b, c, in[11], S42, UL(3174756917)); /* 62 */
  265. II ( c, d, a, b, in[ 2], S43, UL( 718787259)); /* 63 */
  266. II ( b, c, d, a, in[ 9], S44, UL(3951481745)); /* 64 */
  267. buf[0] += a;
  268. buf[1] += b;
  269. buf[2] += c;
  270. buf[3] += d;
  271. }
  272. /*
  273. ***********************************************************************
  274. ** End of md5.c **
  275. ******************************** (cut) ********************************
  276. */