3
0

md5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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[] = {
  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[] = {
  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. { \
  192. a += FF (b, c, d) + (*cwp++ = SWAP_LE32(*words)) + T; \
  193. ++words; \
  194. CYCLIC (a, s); \
  195. a += b; \
  196. } \
  197. while (0)
  198. /* It is unfortunate that C does not provide an operator for
  199. cyclic rotation. Hope the C compiler is smart enough. */
  200. /* gcc 2.95.4 seems to be --aaronl */
  201. # define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
  202. /* Before we start, one word to the strange constants.
  203. They are defined in RFC 1321 as
  204. T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
  205. */
  206. # if MD5_SIZE_VS_SPEED == 1
  207. const uint32_t *pc;
  208. const char *pp;
  209. int i;
  210. # endif /* MD5_SIZE_VS_SPEED */
  211. /* Round 1. */
  212. # if MD5_SIZE_VS_SPEED == 1
  213. pc = C_array;
  214. for (i = 0; i < 4; i++) {
  215. OP(A, B, C, D, 7, *pc++);
  216. OP(D, A, B, C, 12, *pc++);
  217. OP(C, D, A, B, 17, *pc++);
  218. OP(B, C, D, A, 22, *pc++);
  219. }
  220. # else
  221. OP(A, B, C, D, 7, 0xd76aa478);
  222. OP(D, A, B, C, 12, 0xe8c7b756);
  223. OP(C, D, A, B, 17, 0x242070db);
  224. OP(B, C, D, A, 22, 0xc1bdceee);
  225. OP(A, B, C, D, 7, 0xf57c0faf);
  226. OP(D, A, B, C, 12, 0x4787c62a);
  227. OP(C, D, A, B, 17, 0xa8304613);
  228. OP(B, C, D, A, 22, 0xfd469501);
  229. OP(A, B, C, D, 7, 0x698098d8);
  230. OP(D, A, B, C, 12, 0x8b44f7af);
  231. OP(C, D, A, B, 17, 0xffff5bb1);
  232. OP(B, C, D, A, 22, 0x895cd7be);
  233. OP(A, B, C, D, 7, 0x6b901122);
  234. OP(D, A, B, C, 12, 0xfd987193);
  235. OP(C, D, A, B, 17, 0xa679438e);
  236. OP(B, C, D, A, 22, 0x49b40821);
  237. # endif /* MD5_SIZE_VS_SPEED == 1 */
  238. /* For the second to fourth round we have the possibly swapped words
  239. in CORRECT_WORDS. Redefine the macro to take an additional first
  240. argument specifying the function to use. */
  241. # undef OP
  242. # define OP(f, a, b, c, d, k, s, T) \
  243. do \
  244. { \
  245. a += f (b, c, d) + correct_words[k] + T; \
  246. CYCLIC (a, s); \
  247. a += b; \
  248. } \
  249. while (0)
  250. /* Round 2. */
  251. # if MD5_SIZE_VS_SPEED == 1
  252. pp = P_array;
  253. for (i = 0; i < 4; i++) {
  254. OP(FG, A, B, C, D, (int) (*pp++), 5, *pc++);
  255. OP(FG, D, A, B, C, (int) (*pp++), 9, *pc++);
  256. OP(FG, C, D, A, B, (int) (*pp++), 14, *pc++);
  257. OP(FG, B, C, D, A, (int) (*pp++), 20, *pc++);
  258. }
  259. # else
  260. OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
  261. OP(FG, D, A, B, C, 6, 9, 0xc040b340);
  262. OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
  263. OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
  264. OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
  265. OP(FG, D, A, B, C, 10, 9, 0x02441453);
  266. OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
  267. OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
  268. OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
  269. OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
  270. OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
  271. OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
  272. OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
  273. OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
  274. OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
  275. OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
  276. # endif /* MD5_SIZE_VS_SPEED == 1 */
  277. /* Round 3. */
  278. # if MD5_SIZE_VS_SPEED == 1
  279. for (i = 0; i < 4; i++) {
  280. OP(FH, A, B, C, D, (int) (*pp++), 4, *pc++);
  281. OP(FH, D, A, B, C, (int) (*pp++), 11, *pc++);
  282. OP(FH, C, D, A, B, (int) (*pp++), 16, *pc++);
  283. OP(FH, B, C, D, A, (int) (*pp++), 23, *pc++);
  284. }
  285. # else
  286. OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
  287. OP(FH, D, A, B, C, 8, 11, 0x8771f681);
  288. OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
  289. OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
  290. OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
  291. OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
  292. OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
  293. OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
  294. OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
  295. OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
  296. OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
  297. OP(FH, B, C, D, A, 6, 23, 0x04881d05);
  298. OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
  299. OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
  300. OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
  301. OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
  302. # endif /* MD5_SIZE_VS_SPEED == 1 */
  303. /* Round 4. */
  304. # if MD5_SIZE_VS_SPEED == 1
  305. for (i = 0; i < 4; i++) {
  306. OP(FI, A, B, C, D, (int) (*pp++), 6, *pc++);
  307. OP(FI, D, A, B, C, (int) (*pp++), 10, *pc++);
  308. OP(FI, C, D, A, B, (int) (*pp++), 15, *pc++);
  309. OP(FI, B, C, D, A, (int) (*pp++), 21, *pc++);
  310. }
  311. # else
  312. OP(FI, A, B, C, D, 0, 6, 0xf4292244);
  313. OP(FI, D, A, B, C, 7, 10, 0x432aff97);
  314. OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
  315. OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
  316. OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
  317. OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
  318. OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
  319. OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
  320. OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
  321. OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
  322. OP(FI, C, D, A, B, 6, 15, 0xa3014314);
  323. OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
  324. OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
  325. OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
  326. OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
  327. OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
  328. # endif /* MD5_SIZE_VS_SPEED == 1 */
  329. # endif /* MD5_SIZE_VS_SPEED > 1 */
  330. /* Add the starting values of the context. */
  331. A += A_save;
  332. B += B_save;
  333. C += C_save;
  334. D += D_save;
  335. /* Put checksum in context given as argument. */
  336. ctx->A = A;
  337. ctx->B = B;
  338. ctx->C = C;
  339. ctx->D = D;
  340. }
  341. /* Feed data through a temporary buffer to call md5_hash_aligned_block()
  342. * with chunks of data that are 4-byte aligned and a multiple of 64 bytes.
  343. * This function's internal buffer remembers previous data until it has 64
  344. * bytes worth to pass on. Call md5_end() to flush this buffer. */
  345. void md5_hash(const void *buffer, size_t len, md5_ctx_t *ctx)
  346. {
  347. char *buf=(char *)buffer;
  348. /* RFC 1321 specifies the possible length of the file up to 2^64 bits,
  349. * Here we only track the number of bytes. */
  350. ctx->total += len;
  351. // Process all input.
  352. while (len) {
  353. int i = 64 - ctx->buflen;
  354. // Copy data into aligned buffer.
  355. if (i > len) i = len;
  356. memcpy(ctx->buffer + ctx->buflen, buf, i);
  357. len -= i;
  358. ctx->buflen += i;
  359. buf += i;
  360. // When buffer fills up, process it.
  361. if (ctx->buflen == 64) {
  362. md5_hash_block(ctx->buffer, ctx);
  363. ctx->buflen = 0;
  364. }
  365. }
  366. }
  367. /* Process the remaining bytes in the buffer and put result from CTX
  368. * in first 16 bytes following RESBUF. The result is always in little
  369. * endian byte order, so that a byte-wise output yields to the wanted
  370. * ASCII representation of the message digest.
  371. *
  372. * IMPORTANT: On some systems it is required that RESBUF is correctly
  373. * aligned for a 32 bits value.
  374. */
  375. void *md5_end(void *resbuf, md5_ctx_t *ctx)
  376. {
  377. char *buf = ctx->buffer;
  378. int i;
  379. /* Pad data to block size. */
  380. buf[ctx->buflen++] = 0x80;
  381. memset(buf + ctx->buflen, 0, 128 - ctx->buflen);
  382. /* Put the 64-bit file length in *bits* at the end of the buffer. */
  383. ctx->total <<= 3;
  384. if (ctx->buflen > 56) buf += 64;
  385. for (i = 0; i < 8; i++) buf[56 + i] = ctx->total >> (i*8);
  386. /* Process last bytes. */
  387. if (buf != ctx->buffer) md5_hash_block(ctx->buffer, ctx);
  388. md5_hash_block(buf, ctx);
  389. /* Put result from CTX in first 16 bytes following RESBUF. The result is
  390. * always in little endian byte order, so that a byte-wise output yields
  391. * to the wanted ASCII representation of the message digest.
  392. *
  393. * IMPORTANT: On some systems it is required that RESBUF is correctly
  394. * aligned for a 32 bits value.
  395. */
  396. ((uint32_t *) resbuf)[0] = SWAP_LE32(ctx->A);
  397. ((uint32_t *) resbuf)[1] = SWAP_LE32(ctx->B);
  398. ((uint32_t *) resbuf)[2] = SWAP_LE32(ctx->C);
  399. ((uint32_t *) resbuf)[3] = SWAP_LE32(ctx->D);
  400. return resbuf;
  401. }