SDL_test_md5.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. ***********************************************************************
  20. ** RSA Data Security, Inc. MD5 Message-Digest Algorithm **
  21. ** Created: 2/17/90 RLR **
  22. ** Revised: 1/91 SRD,AJ,BSK,JT Reference C ver., 7/10 constant corr. **
  23. ***********************************************************************
  24. */
  25. /*
  26. ***********************************************************************
  27. ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
  28. ** **
  29. ** License to copy and use this software is granted provided that **
  30. ** it is identified as the "RSA Data Security, Inc. MD5 Message- **
  31. ** Digest Algorithm" in all material mentioning or referencing this **
  32. ** software or this function. **
  33. ** **
  34. ** License is also granted to make and use derivative works **
  35. ** provided that such works are identified as "derived from the RSA **
  36. ** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
  37. ** material mentioning or referencing the derived work. **
  38. ** **
  39. ** RSA Data Security, Inc. makes no representations concerning **
  40. ** either the merchantability of this software or the suitability **
  41. ** of this software for any particular purpose. It is provided "as **
  42. ** is" without express or implied warranty of any kind. **
  43. ** **
  44. ** These notices must be retained in any copies of any part of this **
  45. ** documentation and/or software. **
  46. ***********************************************************************
  47. */
  48. #include "SDL_config.h"
  49. #include "SDL_test.h"
  50. /* Forward declaration of static helper function */
  51. static void SDLTest_Md5Transform(MD5UINT4 * buf, const MD5UINT4 * in);
  52. static unsigned char MD5PADDING[64] = {
  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. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  69. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
  70. /* Rotation is separate from addition to prevent recomputation */
  71. #define FF(a, b, c, d, x, s, ac) \
  72. {(a) += F ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
  73. (a) = ROTATE_LEFT ((a), (s)); \
  74. (a) += (b); \
  75. }
  76. #define GG(a, b, c, d, x, s, ac) \
  77. {(a) += G ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
  78. (a) = ROTATE_LEFT ((a), (s)); \
  79. (a) += (b); \
  80. }
  81. #define HH(a, b, c, d, x, s, ac) \
  82. {(a) += H ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
  83. (a) = ROTATE_LEFT ((a), (s)); \
  84. (a) += (b); \
  85. }
  86. #define II(a, b, c, d, x, s, ac) \
  87. {(a) += I ((b), (c), (d)) + (x) + (MD5UINT4)(ac); \
  88. (a) = ROTATE_LEFT ((a), (s)); \
  89. (a) += (b); \
  90. }
  91. /*
  92. The routine MD5Init initializes the message-digest context
  93. mdContext. All fields are set to zero.
  94. */
  95. void SDLTest_Md5Init(SDLTest_Md5Context * mdContext)
  96. {
  97. if (mdContext==NULL) return;
  98. mdContext->i[0] = mdContext->i[1] = (MD5UINT4) 0;
  99. /*
  100. * Load magic initialization constants.
  101. */
  102. mdContext->buf[0] = (MD5UINT4) 0x67452301;
  103. mdContext->buf[1] = (MD5UINT4) 0xefcdab89;
  104. mdContext->buf[2] = (MD5UINT4) 0x98badcfe;
  105. mdContext->buf[3] = (MD5UINT4) 0x10325476;
  106. }
  107. /*
  108. The routine MD5Update updates the message-digest context to
  109. account for the presence of each of the characters inBuf[0..inLen-1]
  110. in the message whose digest is being computed.
  111. */
  112. void SDLTest_Md5Update(SDLTest_Md5Context * mdContext, unsigned char *inBuf,
  113. unsigned int inLen)
  114. {
  115. MD5UINT4 in[16];
  116. int mdi;
  117. unsigned int i, ii;
  118. if (mdContext == NULL) return;
  119. if (inBuf == NULL || inLen < 1) return;
  120. /*
  121. * compute number of bytes mod 64
  122. */
  123. mdi = (int) ((mdContext->i[0] >> 3) & 0x3F);
  124. /*
  125. * update number of bits
  126. */
  127. if ((mdContext->i[0] + ((MD5UINT4) inLen << 3)) < mdContext->i[0])
  128. mdContext->i[1]++;
  129. mdContext->i[0] += ((MD5UINT4) inLen << 3);
  130. mdContext->i[1] += ((MD5UINT4) inLen >> 29);
  131. while (inLen--) {
  132. /*
  133. * add new character to buffer, increment mdi
  134. */
  135. mdContext->in[mdi++] = *inBuf++;
  136. /*
  137. * transform if necessary
  138. */
  139. if (mdi == 0x40) {
  140. for (i = 0, ii = 0; i < 16; i++, ii += 4)
  141. in[i] = (((MD5UINT4) mdContext->in[ii + 3]) << 24) |
  142. (((MD5UINT4) mdContext->in[ii + 2]) << 16) |
  143. (((MD5UINT4) mdContext->in[ii + 1]) << 8) |
  144. ((MD5UINT4) mdContext->in[ii]);
  145. SDLTest_Md5Transform(mdContext->buf, in);
  146. mdi = 0;
  147. }
  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 SDLTest_Md5Final(SDLTest_Md5Context * mdContext)
  155. {
  156. MD5UINT4 in[16];
  157. int mdi;
  158. unsigned int i, ii;
  159. unsigned int padLen;
  160. if (mdContext == NULL) return;
  161. /*
  162. * save number of bits
  163. */
  164. in[14] = mdContext->i[0];
  165. in[15] = mdContext->i[1];
  166. /*
  167. * compute number of bytes mod 64
  168. */
  169. mdi = (int) ((mdContext->i[0] >> 3) & 0x3F);
  170. /*
  171. * pad out to 56 mod 64
  172. */
  173. padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
  174. SDLTest_Md5Update(mdContext, MD5PADDING, padLen);
  175. /*
  176. * append length in bits and transform
  177. */
  178. for (i = 0, ii = 0; i < 14; i++, ii += 4)
  179. in[i] = (((MD5UINT4) mdContext->in[ii + 3]) << 24) |
  180. (((MD5UINT4) mdContext->in[ii + 2]) << 16) |
  181. (((MD5UINT4) mdContext->in[ii + 1]) << 8) |
  182. ((MD5UINT4) mdContext->in[ii]);
  183. SDLTest_Md5Transform(mdContext->buf, in);
  184. /*
  185. * store buffer in digest
  186. */
  187. for (i = 0, ii = 0; i < 4; i++, ii += 4) {
  188. mdContext->digest[ii] = (unsigned char) (mdContext->buf[i] & 0xFF);
  189. mdContext->digest[ii + 1] =
  190. (unsigned char) ((mdContext->buf[i] >> 8) & 0xFF);
  191. mdContext->digest[ii + 2] =
  192. (unsigned char) ((mdContext->buf[i] >> 16) & 0xFF);
  193. mdContext->digest[ii + 3] =
  194. (unsigned char) ((mdContext->buf[i] >> 24) & 0xFF);
  195. }
  196. }
  197. /* Basic MD5 step. Transforms buf based on in.
  198. */
  199. static void SDLTest_Md5Transform(MD5UINT4 * buf, const MD5UINT4 * in)
  200. {
  201. MD5UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  202. /*
  203. * Round 1
  204. */
  205. #define S11 7
  206. #define S12 12
  207. #define S13 17
  208. #define S14 22
  209. FF(a, b, c, d, in[0], S11, 3614090360u); /* 1 */
  210. FF(d, a, b, c, in[1], S12, 3905402710u); /* 2 */
  211. FF(c, d, a, b, in[2], S13, 606105819u); /* 3 */
  212. FF(b, c, d, a, in[3], S14, 3250441966u); /* 4 */
  213. FF(a, b, c, d, in[4], S11, 4118548399u); /* 5 */
  214. FF(d, a, b, c, in[5], S12, 1200080426u); /* 6 */
  215. FF(c, d, a, b, in[6], S13, 2821735955u); /* 7 */
  216. FF(b, c, d, a, in[7], S14, 4249261313u); /* 8 */
  217. FF(a, b, c, d, in[8], S11, 1770035416u); /* 9 */
  218. FF(d, a, b, c, in[9], S12, 2336552879u); /* 10 */
  219. FF(c, d, a, b, in[10], S13, 4294925233u); /* 11 */
  220. FF(b, c, d, a, in[11], S14, 2304563134u); /* 12 */
  221. FF(a, b, c, d, in[12], S11, 1804603682u); /* 13 */
  222. FF(d, a, b, c, in[13], S12, 4254626195u); /* 14 */
  223. FF(c, d, a, b, in[14], S13, 2792965006u); /* 15 */
  224. FF(b, c, d, a, in[15], S14, 1236535329u); /* 16 */
  225. /*
  226. * Round 2
  227. */
  228. #define S21 5
  229. #define S22 9
  230. #define S23 14
  231. #define S24 20
  232. GG(a, b, c, d, in[1], S21, 4129170786u); /* 17 */
  233. GG(d, a, b, c, in[6], S22, 3225465664u); /* 18 */
  234. GG(c, d, a, b, in[11], S23, 643717713u); /* 19 */
  235. GG(b, c, d, a, in[0], S24, 3921069994u); /* 20 */
  236. GG(a, b, c, d, in[5], S21, 3593408605u); /* 21 */
  237. GG(d, a, b, c, in[10], S22, 38016083u); /* 22 */
  238. GG(c, d, a, b, in[15], S23, 3634488961u); /* 23 */
  239. GG(b, c, d, a, in[4], S24, 3889429448u); /* 24 */
  240. GG(a, b, c, d, in[9], S21, 568446438u); /* 25 */
  241. GG(d, a, b, c, in[14], S22, 3275163606u); /* 26 */
  242. GG(c, d, a, b, in[3], S23, 4107603335u); /* 27 */
  243. GG(b, c, d, a, in[8], S24, 1163531501u); /* 28 */
  244. GG(a, b, c, d, in[13], S21, 2850285829u); /* 29 */
  245. GG(d, a, b, c, in[2], S22, 4243563512u); /* 30 */
  246. GG(c, d, a, b, in[7], S23, 1735328473u); /* 31 */
  247. GG(b, c, d, a, in[12], S24, 2368359562u); /* 32 */
  248. /*
  249. * Round 3
  250. */
  251. #define S31 4
  252. #define S32 11
  253. #define S33 16
  254. #define S34 23
  255. HH(a, b, c, d, in[5], S31, 4294588738u); /* 33 */
  256. HH(d, a, b, c, in[8], S32, 2272392833u); /* 34 */
  257. HH(c, d, a, b, in[11], S33, 1839030562u); /* 35 */
  258. HH(b, c, d, a, in[14], S34, 4259657740u); /* 36 */
  259. HH(a, b, c, d, in[1], S31, 2763975236u); /* 37 */
  260. HH(d, a, b, c, in[4], S32, 1272893353u); /* 38 */
  261. HH(c, d, a, b, in[7], S33, 4139469664u); /* 39 */
  262. HH(b, c, d, a, in[10], S34, 3200236656u); /* 40 */
  263. HH(a, b, c, d, in[13], S31, 681279174u); /* 41 */
  264. HH(d, a, b, c, in[0], S32, 3936430074u); /* 42 */
  265. HH(c, d, a, b, in[3], S33, 3572445317u); /* 43 */
  266. HH(b, c, d, a, in[6], S34, 76029189u); /* 44 */
  267. HH(a, b, c, d, in[9], S31, 3654602809u); /* 45 */
  268. HH(d, a, b, c, in[12], S32, 3873151461u); /* 46 */
  269. HH(c, d, a, b, in[15], S33, 530742520u); /* 47 */
  270. HH(b, c, d, a, in[2], S34, 3299628645u); /* 48 */
  271. /*
  272. * Round 4
  273. */
  274. #define S41 6
  275. #define S42 10
  276. #define S43 15
  277. #define S44 21
  278. II(a, b, c, d, in[0], S41, 4096336452u); /* 49 */
  279. II(d, a, b, c, in[7], S42, 1126891415u); /* 50 */
  280. II(c, d, a, b, in[14], S43, 2878612391u); /* 51 */
  281. II(b, c, d, a, in[5], S44, 4237533241u); /* 52 */
  282. II(a, b, c, d, in[12], S41, 1700485571u); /* 53 */
  283. II(d, a, b, c, in[3], S42, 2399980690u); /* 54 */
  284. II(c, d, a, b, in[10], S43, 4293915773u); /* 55 */
  285. II(b, c, d, a, in[1], S44, 2240044497u); /* 56 */
  286. II(a, b, c, d, in[8], S41, 1873313359u); /* 57 */
  287. II(d, a, b, c, in[15], S42, 4264355552u); /* 58 */
  288. II(c, d, a, b, in[6], S43, 2734768916u); /* 59 */
  289. II(b, c, d, a, in[13], S44, 1309151649u); /* 60 */
  290. II(a, b, c, d, in[4], S41, 4149444226u); /* 61 */
  291. II(d, a, b, c, in[11], S42, 3174756917u); /* 62 */
  292. II(c, d, a, b, in[2], S43, 718787259u); /* 63 */
  293. II(b, c, d, a, in[9], S44, 3951481745u); /* 64 */
  294. buf[0] += a;
  295. buf[1] += b;
  296. buf[2] += c;
  297. buf[3] += d;
  298. }
  299. /* vi: set ts=4 sw=4 expandtab: */