mymd5.c 12 KB

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