MD5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* ===================================================================
  2. *
  3. * Copyright (c) 2018, Helder Eijs <helderijs@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. * ===================================================================
  30. */
  31. #include <stdio.h>
  32. #include "common.h"
  33. #include "endianess.h"
  34. FAKE_INIT(MD5)
  35. /**
  36. * MD5 as defined in RFC1321
  37. */
  38. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  39. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  40. #define H(x, y, z) ((x) ^ (y) ^ (z))
  41. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  42. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  43. #define FF(a, b, c, d, x, s, ac) { \
  44. (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  45. (a) = ROTATE_LEFT ((a), (s)); \
  46. (a) += (b); \
  47. }
  48. #define GG(a, b, c, d, x, s, ac) { \
  49. (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  50. (a) = ROTATE_LEFT ((a), (s)); \
  51. (a) += (b); \
  52. }
  53. #define HH(a, b, c, d, x, s, ac) { \
  54. (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  55. (a) = ROTATE_LEFT ((a), (s)); \
  56. (a) += (b); \
  57. }
  58. #define II(a, b, c, d, x, s, ac) { \
  59. (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  60. (a) = ROTATE_LEFT ((a), (s)); \
  61. (a) += (b); \
  62. }
  63. #define S11 7
  64. #define S12 12
  65. #define S13 17
  66. #define S14 22
  67. #define S21 5
  68. #define S22 9
  69. #define S23 14
  70. #define S24 20
  71. #define S31 4
  72. #define S32 11
  73. #define S33 16
  74. #define S34 23
  75. #define S41 6
  76. #define S42 10
  77. #define S43 15
  78. #define S44 21
  79. #define BLOCK_SIZE 64
  80. #define DIGEST_SIZE (128/8)
  81. typedef struct t_hash_state {
  82. uint32_t h[4];
  83. uint8_t buf[BLOCK_SIZE]; /** 64 bytes == 512 bits == sixteen 32-bit words **/
  84. unsigned curlen; /** Useful message bytes in buf[] (leftmost) **/
  85. uint64_t totbits; /** Total message length in bits **/
  86. } hash_state;
  87. static int add_bits(hash_state *hs, unsigned bits)
  88. {
  89. /** Maximum message length for MD5 is 2**64 bits **/
  90. hs->totbits += bits;
  91. return (hs->totbits < bits) ? ERR_MAX_DATA : 0;
  92. }
  93. static void md5_compress(hash_state * hs)
  94. {
  95. uint32_t a, b, c, d;
  96. uint32_t x[16];
  97. int i;
  98. /** Words flow in in little-endian mode **/
  99. for (i=0; i<16; i++) {
  100. x[i] = LOAD_U32_LITTLE(&hs->buf[i*4]);
  101. }
  102. a = hs->h[0];
  103. b = hs->h[1];
  104. c = hs->h[2];
  105. d = hs->h[3];
  106. /* Round 1 */
  107. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  108. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  109. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  110. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  111. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  112. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  113. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  114. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  115. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  116. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  117. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  118. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  119. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  120. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  121. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  122. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  123. /* Round 2 */
  124. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  125. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  126. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  127. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  128. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  129. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  130. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  131. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  132. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  133. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  134. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  135. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  136. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  137. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  138. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  139. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  140. /* Round 3 */
  141. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  142. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  143. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  144. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  145. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  146. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  147. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  148. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  149. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  150. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  151. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  152. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  153. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  154. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  155. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  156. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  157. /* Round 4 */
  158. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  159. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  160. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  161. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  162. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  163. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  164. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  165. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  166. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  167. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  168. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  169. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  170. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  171. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  172. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  173. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  174. /** compute new intermediate hash **/
  175. hs->h[0] += a;
  176. hs->h[1] += b;
  177. hs->h[2] += c;
  178. hs->h[3] += d;
  179. }
  180. EXPORT_SYM int MD5_init(hash_state **mdState)
  181. {
  182. hash_state *hs;
  183. if (NULL == mdState) {
  184. return ERR_NULL;
  185. }
  186. *mdState = hs = (hash_state*) calloc(1, sizeof(hash_state));
  187. if (NULL == hs)
  188. return ERR_MEMORY;
  189. hs->curlen = 0;
  190. hs->totbits = 0;
  191. /** Initial intermediate hash value **/
  192. hs->h[0] = 0x67452301;
  193. hs->h[1] = 0xefcdab89;
  194. hs->h[2] = 0x98badcfe;
  195. hs->h[3] = 0x10325476;
  196. return 0;
  197. }
  198. EXPORT_SYM int MD5_destroy (hash_state *mdState)
  199. {
  200. free(mdState);
  201. return 0;
  202. }
  203. EXPORT_SYM int MD5_update(hash_state *hs, const uint8_t *buf, size_t len)
  204. {
  205. if (NULL == hs || NULL == buf) {
  206. return ERR_NULL;
  207. }
  208. assert(hs->curlen < BLOCK_SIZE);
  209. while (len>0) {
  210. unsigned left, btc;
  211. left = BLOCK_SIZE - hs->curlen;
  212. btc = (unsigned)MIN(left, len);
  213. memcpy(&hs->buf[hs->curlen], buf, btc);
  214. buf += btc;
  215. hs->curlen += btc;
  216. len -= btc;
  217. if (hs->curlen == BLOCK_SIZE) {
  218. md5_compress(hs);
  219. hs->curlen = 0;
  220. if (add_bits(hs, BLOCK_SIZE*8)) {
  221. return ERR_MAX_DATA;
  222. }
  223. }
  224. }
  225. return 0;
  226. }
  227. static int md5_finalize(hash_state *hs, uint8_t *hash /** [DIGEST_SIZE] **/)
  228. {
  229. unsigned left, i;
  230. assert(hs->curlen < BLOCK_SIZE);
  231. /* remaining length of the message */
  232. if (add_bits(hs, hs->curlen*8)) {
  233. return ERR_MAX_DATA;
  234. }
  235. /* append the '1' bit */
  236. /* buf[] is guaranteed to have at least 1 byte free */
  237. hs->buf[hs->curlen++] = 0x80;
  238. /** if there are less then 64 bits lef, just pad with zeroes and compress **/
  239. left = BLOCK_SIZE - hs->curlen;
  240. if (left < 8) {
  241. memset(&hs->buf[hs->curlen], 0, left);
  242. md5_compress(hs);
  243. hs->curlen = 0;
  244. }
  245. /**
  246. * pad with zeroes and close the block with the bit length
  247. * encoded as 64-bit integer little endian.
  248. **/
  249. left = BLOCK_SIZE - hs->curlen;
  250. memset(&hs->buf[hs->curlen], 0, left);
  251. STORE_U64_LITTLE(&hs->buf[BLOCK_SIZE-8], hs->totbits);
  252. /** compress one last time **/
  253. md5_compress(hs);
  254. /** create final hash **/
  255. for (i=0; i<4; i++) {
  256. STORE_U32_LITTLE(&hash[i*4], hs->h[i]);
  257. }
  258. return 0;
  259. }
  260. EXPORT_SYM int MD5_digest(const hash_state *mdState, uint8_t digest[DIGEST_SIZE])
  261. {
  262. hash_state temp;
  263. if (NULL == mdState) {
  264. return ERR_NULL;
  265. }
  266. temp = *mdState;
  267. md5_finalize(&temp, digest);
  268. return 0;
  269. }
  270. EXPORT_SYM int MD5_copy(const hash_state *src, hash_state *dst)
  271. {
  272. if (NULL == src || NULL == dst) {
  273. return ERR_NULL;
  274. }
  275. *dst = *src;
  276. return 0;
  277. }
  278. /**
  279. * This is a specialized function to efficiently perform the inner loop of PBKDF2-HMAC.
  280. *
  281. * - inner, the hash after the inner padded secret has been absorbed
  282. * - outer, the hash after the outer padded secret has been absorbed
  283. * - first_hmac, the output of the first HMAC iteration (with salt and counter)
  284. * - result, the XOR of the HMACs from all iterations
  285. * - iterations, the total number of PBKDF2 iterations (>0)
  286. *
  287. * This function does not change the state of either hash.
  288. */
  289. EXPORT_SYM int MD5_pbkdf2_hmac_assist(const hash_state *inner, const hash_state *outer,
  290. const uint8_t first_hmac[DIGEST_SIZE],
  291. uint8_t result[DIGEST_SIZE],
  292. size_t iterations)
  293. {
  294. hash_state inner_temp, outer_temp;
  295. size_t i;
  296. uint8_t last_hmac[DIGEST_SIZE];
  297. if (NULL == inner || NULL == outer || NULL == first_hmac || NULL == result) {
  298. return ERR_NULL;
  299. }
  300. if (iterations == 0) {
  301. return ERR_NR_ROUNDS;
  302. }
  303. memcpy(result, first_hmac, DIGEST_SIZE);
  304. memcpy(last_hmac, first_hmac, DIGEST_SIZE);
  305. for (i=1; i<iterations; i++) {
  306. int j;
  307. inner_temp = *inner;
  308. outer_temp = *outer;
  309. MD5_update(&inner_temp, last_hmac, DIGEST_SIZE);
  310. md5_finalize(&inner_temp, last_hmac);
  311. /** last_hmac is now the intermediate digest **/
  312. MD5_update(&outer_temp, last_hmac, DIGEST_SIZE);
  313. md5_finalize(&outer_temp, last_hmac);
  314. for (j=0; j<DIGEST_SIZE; j++) {
  315. result[j] ^= last_hmac[j];
  316. }
  317. }
  318. return 0;
  319. }
  320. #ifdef MAIN
  321. int main(void)
  322. {
  323. hash_state *hs;
  324. const uint8_t tv[] = "The quick brown fox jumps over the lazy dog";
  325. uint8_t result[DIGEST_SIZE];
  326. int i;
  327. MD5_init(&hs);
  328. MD5_update(hs, tv, sizeof tv - 1);
  329. MD5_digest(hs, result);
  330. MD5_destroy(hs);
  331. for (i=0; i<sizeof result; i++) {
  332. printf("%02X", result[i]);
  333. }
  334. printf("\n");
  335. MD5_init(&hs);
  336. MD5_digest(hs, result);
  337. MD5_destroy(hs);
  338. for (i=0; i<sizeof result; i++) {
  339. printf("%02X", result[i]);
  340. }
  341. printf("\n");
  342. MD5_init(&hs);
  343. for (i=0; i<10000000; i++) {
  344. MD5_update(hs, tv, sizeof tv - 1);
  345. }
  346. MD5_destroy(hs);
  347. printf("\n");
  348. }
  349. #endif