md5.c 14 KB

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