md5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * md5.c - Compute MD5 checksum of strings according to the
  4. * definition of MD5 in RFC 1321 from April 1992.
  5. *
  6. * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  7. *
  8. * Copyright (C) 1995-1999 Free Software Foundation, Inc.
  9. * Copyright (C) 2001 Manuel Novoa III
  10. * Copyright (C) 2003 Glenn L. McGrath
  11. * Copyright (C) 2003 Erik Andersen
  12. *
  13. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  14. */
  15. #include "libbb.h"
  16. #if CONFIG_MD5_SIZE_VS_SPEED < 0 || CONFIG_MD5_SIZE_VS_SPEED > 3
  17. # define MD5_SIZE_VS_SPEED 2
  18. #else
  19. # define MD5_SIZE_VS_SPEED CONFIG_MD5_SIZE_VS_SPEED
  20. #endif
  21. /* Initialize structure containing state of computation.
  22. * (RFC 1321, 3.3: Step 3)
  23. */
  24. void md5_begin(md5_ctx_t *ctx)
  25. {
  26. ctx->A = 0x67452301;
  27. ctx->B = 0xefcdab89;
  28. ctx->C = 0x98badcfe;
  29. ctx->D = 0x10325476;
  30. ctx->total = 0;
  31. ctx->buflen = 0;
  32. }
  33. /* These are the four functions used in the four steps of the MD5 algorithm
  34. * and defined in the RFC 1321. The first function is a little bit optimized
  35. * (as found in Colin Plumbs public domain implementation).
  36. * #define FF(b, c, d) ((b & c) | (~b & d))
  37. */
  38. # define FF(b, c, d) (d ^ (b & (c ^ d)))
  39. # define FG(b, c, d) FF (d, b, c)
  40. # define FH(b, c, d) (b ^ c ^ d)
  41. # define FI(b, c, d) (c ^ (b | ~d))
  42. /* Hash a single block, 64 bytes long and 4-byte aligned. */
  43. static void md5_hash_block(const void *buffer, md5_ctx_t *ctx)
  44. {
  45. uint32_t correct_words[16];
  46. const uint32_t *words = buffer;
  47. # if MD5_SIZE_VS_SPEED > 0
  48. static const uint32_t C_array[] = {
  49. /* round 1 */
  50. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  51. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  52. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  53. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  54. /* round 2 */
  55. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  56. 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
  57. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  58. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  59. /* round 3 */
  60. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  61. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  62. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
  63. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  64. /* round 4 */
  65. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  66. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  67. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  68. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  69. };
  70. static const char P_array[] ALIGN1 = {
  71. # if MD5_SIZE_VS_SPEED > 1
  72. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
  73. # endif /* MD5_SIZE_VS_SPEED > 1 */
  74. 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
  75. 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
  76. 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
  77. };
  78. # if MD5_SIZE_VS_SPEED > 1
  79. static const char S_array[] ALIGN1 = {
  80. 7, 12, 17, 22,
  81. 5, 9, 14, 20,
  82. 4, 11, 16, 23,
  83. 6, 10, 15, 21
  84. };
  85. # endif /* MD5_SIZE_VS_SPEED > 1 */
  86. # endif
  87. uint32_t A = ctx->A;
  88. uint32_t B = ctx->B;
  89. uint32_t C = ctx->C;
  90. uint32_t D = ctx->D;
  91. /* Process all bytes in the buffer with 64 bytes in each round of
  92. the loop. */
  93. uint32_t *cwp = correct_words;
  94. uint32_t A_save = A;
  95. uint32_t B_save = B;
  96. uint32_t C_save = C;
  97. uint32_t D_save = D;
  98. # if MD5_SIZE_VS_SPEED > 1
  99. # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  100. const uint32_t *pc;
  101. const char *pp;
  102. const char *ps;
  103. int i;
  104. uint32_t temp;
  105. for (i = 0; i < 16; i++) {
  106. cwp[i] = SWAP_LE32(words[i]);
  107. }
  108. words += 16;
  109. # if MD5_SIZE_VS_SPEED > 2
  110. pc = C_array;
  111. pp = P_array;
  112. ps = S_array - 4;
  113. for (i = 0; i < 64; i++) {
  114. if ((i & 0x0f) == 0)
  115. ps += 4;
  116. temp = A;
  117. switch (i >> 4) {
  118. case 0:
  119. temp += FF(B, C, D);
  120. break;
  121. case 1:
  122. temp += FG(B, C, D);
  123. break;
  124. case 2:
  125. temp += FH(B, C, D);
  126. break;
  127. case 3:
  128. temp += FI(B, C, D);
  129. }
  130. temp += cwp[(int) (*pp++)] + *pc++;
  131. CYCLIC(temp, ps[i & 3]);
  132. temp += B;
  133. A = D;
  134. D = C;
  135. C = B;
  136. B = temp;
  137. }
  138. # else
  139. pc = C_array;
  140. pp = P_array;
  141. ps = S_array;
  142. for (i = 0; i < 16; i++) {
  143. temp = A + FF(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  144. CYCLIC(temp, ps[i & 3]);
  145. temp += B;
  146. A = D;
  147. D = C;
  148. C = B;
  149. B = temp;
  150. }
  151. ps += 4;
  152. for (i = 0; i < 16; i++) {
  153. temp = A + FG(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  154. CYCLIC(temp, ps[i & 3]);
  155. temp += B;
  156. A = D;
  157. D = C;
  158. C = B;
  159. B = temp;
  160. }
  161. ps += 4;
  162. for (i = 0; i < 16; i++) {
  163. temp = A + FH(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  164. CYCLIC(temp, ps[i & 3]);
  165. temp += B;
  166. A = D;
  167. D = C;
  168. C = B;
  169. B = temp;
  170. }
  171. ps += 4;
  172. for (i = 0; i < 16; i++) {
  173. temp = A + FI(B, C, D) + cwp[(int) (*pp++)] + *pc++;
  174. CYCLIC(temp, ps[i & 3]);
  175. temp += B;
  176. A = D;
  177. D = C;
  178. C = B;
  179. B = temp;
  180. }
  181. # endif /* MD5_SIZE_VS_SPEED > 2 */
  182. # else
  183. /* First round: using the given function, the context and a constant
  184. the next context is computed. Because the algorithms processing
  185. unit is a 32-bit word and it is determined to work on words in
  186. little endian byte order we perhaps have to change the byte order
  187. before the computation. To reduce the work for the next steps
  188. we store the swapped words in the array CORRECT_WORDS. */
  189. # define OP(a, b, c, d, s, T) \
  190. do { \
  191. a += FF (b, c, d) + (*cwp++ = SWAP_LE32(*words)) + T; \
  192. ++words; \
  193. CYCLIC (a, s); \
  194. a += b; \
  195. } while (0)
  196. /* It is unfortunate that C does not provide an operator for
  197. cyclic rotation. Hope the C compiler is smart enough. */
  198. /* gcc 2.95.4 seems to be --aaronl */
  199. # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  200. /* Before we start, one word to the strange constants.
  201. They are defined in RFC 1321 as
  202. T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  203. */
  204. # if MD5_SIZE_VS_SPEED == 1
  205. const uint32_t *pc;
  206. const char *pp;
  207. int i;
  208. # endif /* MD5_SIZE_VS_SPEED */
  209. /* Round 1. */
  210. # if MD5_SIZE_VS_SPEED == 1
  211. pc = C_array;
  212. for (i = 0; i < 4; i++) {
  213. OP(A, B, C, D, 7, *pc++);
  214. OP(D, A, B, C, 12, *pc++);
  215. OP(C, D, A, B, 17, *pc++);
  216. OP(B, C, D, A, 22, *pc++);
  217. }
  218. # else
  219. OP(A, B, C, D, 7, 0xd76aa478);
  220. OP(D, A, B, C, 12, 0xe8c7b756);
  221. OP(C, D, A, B, 17, 0x242070db);
  222. OP(B, C, D, A, 22, 0xc1bdceee);
  223. OP(A, B, C, D, 7, 0xf57c0faf);
  224. OP(D, A, B, C, 12, 0x4787c62a);
  225. OP(C, D, A, B, 17, 0xa8304613);
  226. OP(B, C, D, A, 22, 0xfd469501);
  227. OP(A, B, C, D, 7, 0x698098d8);
  228. OP(D, A, B, C, 12, 0x8b44f7af);
  229. OP(C, D, A, B, 17, 0xffff5bb1);
  230. OP(B, C, D, A, 22, 0x895cd7be);
  231. OP(A, B, C, D, 7, 0x6b901122);
  232. OP(D, A, B, C, 12, 0xfd987193);
  233. OP(C, D, A, B, 17, 0xa679438e);
  234. OP(B, C, D, A, 22, 0x49b40821);
  235. # endif /* MD5_SIZE_VS_SPEED == 1 */
  236. /* For the second to fourth round we have the possibly swapped words
  237. in CORRECT_WORDS. Redefine the macro to take an additional first
  238. argument specifying the function to use. */
  239. # undef OP
  240. # define OP(f, a, b, c, d, k, s, T) \
  241. do { \
  242. a += f (b, c, d) + correct_words[k] + T; \
  243. CYCLIC (a, s); \
  244. a += b; \
  245. } while (0)
  246. /* Round 2. */
  247. # if MD5_SIZE_VS_SPEED == 1
  248. pp = P_array;
  249. for (i = 0; i < 4; i++) {
  250. OP(FG, A, B, C, D, (int) (*pp++), 5, *pc++);
  251. OP(FG, D, A, B, C, (int) (*pp++), 9, *pc++);
  252. OP(FG, C, D, A, B, (int) (*pp++), 14, *pc++);
  253. OP(FG, B, C, D, A, (int) (*pp++), 20, *pc++);
  254. }
  255. # else
  256. OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
  257. OP(FG, D, A, B, C, 6, 9, 0xc040b340);
  258. OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
  259. OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
  260. OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
  261. OP(FG, D, A, B, C, 10, 9, 0x02441453);
  262. OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
  263. OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
  264. OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
  265. OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
  266. OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
  267. OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
  268. OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
  269. OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
  270. OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
  271. OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
  272. # endif /* MD5_SIZE_VS_SPEED == 1 */
  273. /* Round 3. */
  274. # if MD5_SIZE_VS_SPEED == 1
  275. for (i = 0; i < 4; i++) {
  276. OP(FH, A, B, C, D, (int) (*pp++), 4, *pc++);
  277. OP(FH, D, A, B, C, (int) (*pp++), 11, *pc++);
  278. OP(FH, C, D, A, B, (int) (*pp++), 16, *pc++);
  279. OP(FH, B, C, D, A, (int) (*pp++), 23, *pc++);
  280. }
  281. # else
  282. OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
  283. OP(FH, D, A, B, C, 8, 11, 0x8771f681);
  284. OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
  285. OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
  286. OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
  287. OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
  288. OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
  289. OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
  290. OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
  291. OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
  292. OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
  293. OP(FH, B, C, D, A, 6, 23, 0x04881d05);
  294. OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
  295. OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
  296. OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
  297. OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
  298. # endif /* MD5_SIZE_VS_SPEED == 1 */
  299. /* Round 4. */
  300. # if MD5_SIZE_VS_SPEED == 1
  301. for (i = 0; i < 4; i++) {
  302. OP(FI, A, B, C, D, (int) (*pp++), 6, *pc++);
  303. OP(FI, D, A, B, C, (int) (*pp++), 10, *pc++);
  304. OP(FI, C, D, A, B, (int) (*pp++), 15, *pc++);
  305. OP(FI, B, C, D, A, (int) (*pp++), 21, *pc++);
  306. }
  307. # else
  308. OP(FI, A, B, C, D, 0, 6, 0xf4292244);
  309. OP(FI, D, A, B, C, 7, 10, 0x432aff97);
  310. OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
  311. OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
  312. OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
  313. OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
  314. OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
  315. OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
  316. OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
  317. OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
  318. OP(FI, C, D, A, B, 6, 15, 0xa3014314);
  319. OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
  320. OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
  321. OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
  322. OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
  323. OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
  324. # endif /* MD5_SIZE_VS_SPEED == 1 */
  325. # endif /* MD5_SIZE_VS_SPEED > 1 */
  326. /* Add the starting values of the context. */
  327. A += A_save;
  328. B += B_save;
  329. C += C_save;
  330. D += D_save;
  331. /* Put checksum in context given as argument. */
  332. ctx->A = A;
  333. ctx->B = B;
  334. ctx->C = C;
  335. ctx->D = D;
  336. }
  337. /* Feed data through a temporary buffer to call md5_hash_aligned_block()
  338. * with chunks of data that are 4-byte aligned and a multiple of 64 bytes.
  339. * This function's internal buffer remembers previous data until it has 64
  340. * bytes worth to pass on. Call md5_end() to flush this buffer. */
  341. void md5_hash(const void *buffer, size_t len, md5_ctx_t *ctx)
  342. {
  343. char *buf=(char *)buffer;
  344. /* RFC 1321 specifies the possible length of the file up to 2^64 bits,
  345. * Here we only track the number of bytes. */
  346. ctx->total += len;
  347. // Process all input.
  348. while (len) {
  349. unsigned i = 64 - ctx->buflen;
  350. // Copy data into aligned buffer.
  351. if (i > len) i = len;
  352. memcpy(ctx->buffer + ctx->buflen, buf, i);
  353. len -= i;
  354. ctx->buflen += i;
  355. buf += i;
  356. // When buffer fills up, process it.
  357. if (ctx->buflen == 64) {
  358. md5_hash_block(ctx->buffer, ctx);
  359. ctx->buflen = 0;
  360. }
  361. }
  362. }
  363. /* Process the remaining bytes in the buffer and put result from CTX
  364. * in first 16 bytes following RESBUF. The result is always in little
  365. * endian byte order, so that a byte-wise output yields to the wanted
  366. * ASCII representation of the message digest.
  367. *
  368. * IMPORTANT: On some systems it is required that RESBUF is correctly
  369. * aligned for a 32 bits value.
  370. */
  371. void *md5_end(void *resbuf, md5_ctx_t *ctx)
  372. {
  373. char *buf = ctx->buffer;
  374. int i;
  375. /* Pad data to block size. */
  376. buf[ctx->buflen++] = 0x80;
  377. memset(buf + ctx->buflen, 0, 128 - ctx->buflen);
  378. /* Put the 64-bit file length in *bits* at the end of the buffer. */
  379. ctx->total <<= 3;
  380. if (ctx->buflen > 56) buf += 64;
  381. for (i = 0; i < 8; i++) buf[56 + i] = ctx->total >> (i*8);
  382. /* Process last bytes. */
  383. if (buf != ctx->buffer) md5_hash_block(ctx->buffer, ctx);
  384. md5_hash_block(buf, ctx);
  385. /* Put result from CTX in first 16 bytes following RESBUF. The result is
  386. * always in little endian byte order, so that a byte-wise output yields
  387. * to the wanted ASCII representation of the message digest.
  388. *
  389. * IMPORTANT: On some systems it is required that RESBUF is correctly
  390. * aligned for a 32 bits value.
  391. */
  392. ((uint32_t *) resbuf)[0] = SWAP_LE32(ctx->A);
  393. ((uint32_t *) resbuf)[1] = SWAP_LE32(ctx->B);
  394. ((uint32_t *) resbuf)[2] = SWAP_LE32(ctx->C);
  395. ((uint32_t *) resbuf)[3] = SWAP_LE32(ctx->D);
  396. return resbuf;
  397. }