md5.c 12 KB

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