3
0

hash_md5prime.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* This file is not used by busybox right now.
  2. * However, the code here seems to be a tiny bit smaller
  3. * than one in md5.c. Need to investigate which one
  4. * is better overall...
  5. * Hint: grep for md5prime to find places where you can switch
  6. * md5.c/md5prime.c
  7. */
  8. /*
  9. * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  10. *
  11. * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  12. * rights reserved.
  13. *
  14. * License to copy and use this software is granted provided that it
  15. * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  16. * Algorithm" in all material mentioning or referencing this software
  17. * or this function.
  18. *
  19. * License is also granted to make and use derivative works provided
  20. * that such works are identified as "derived from the RSA Data
  21. * Security, Inc. MD5 Message-Digest Algorithm" in all material
  22. * mentioning or referencing the derived work.
  23. *
  24. * RSA Data Security, Inc. makes no representations concerning either
  25. * the merchantability of this software or the suitability of this
  26. * software for any particular purpose. It is provided "as is"
  27. * without express or implied warranty of any kind.
  28. *
  29. * These notices must be retained in any copies of any part of this
  30. * documentation and/or software.
  31. *
  32. * $FreeBSD: src/lib/libmd/md5c.c,v 1.9.2.1 1999/08/29 14:57:12 peter Exp $
  33. *
  34. * This code is the same as the code published by RSA Inc. It has been
  35. * edited for clarity and style only.
  36. *
  37. * ----------------------------------------------------------------------------
  38. * The md5_crypt() function was taken from freeBSD's libcrypt and contains
  39. * this license:
  40. * "THE BEER-WARE LICENSE" (Revision 42):
  41. * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
  42. * can do whatever you want with this stuff. If we meet some day, and you think
  43. * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
  44. *
  45. * $FreeBSD: src/lib/libcrypt/crypt.c,v 1.7.2.1 1999/08/29 14:56:33 peter Exp $
  46. *
  47. * ----------------------------------------------------------------------------
  48. * On April 19th, 2001 md5_crypt() was modified to make it reentrant
  49. * by Erik Andersen <andersen@uclibc.org>
  50. *
  51. * June 28, 2001 Manuel Novoa III
  52. *
  53. * "Un-inlined" code using loops and static const tables in order to
  54. * reduce generated code size (on i386 from approx 4k to approx 2.5k).
  55. *
  56. * June 29, 2001 Manuel Novoa III
  57. *
  58. * Completely removed static PADDING array.
  59. *
  60. * Reintroduced the loop unrolling in md5_transform and added the
  61. * MD5_SMALL option for configurability. Define below as:
  62. * 0 fully unrolled loops
  63. * 1 partially unrolled (4 ops per loop)
  64. * 2 no unrolling -- introduces the need to swap 4 variables (slow)
  65. * 3 no unrolling and all 4 loops merged into one with switch
  66. * in each loop (glacial)
  67. * On i386, sizes are roughly (-Os -fno-builtin):
  68. * 0: 3k 1: 2.5k 2: 2.2k 3: 2k
  69. *
  70. * Since SuSv3 does not require crypt_r, modified again August 7, 2002
  71. * by Erik Andersen to remove reentrance stuff...
  72. */
  73. #include "libbb.h"
  74. /* 1: fastest, 3: smallest */
  75. #if CONFIG_MD5_SMALL < 1
  76. # define MD5_SMALL 1
  77. #elif CONFIG_MD5_SMALL > 3
  78. # define MD5_SMALL 3
  79. #else
  80. # define MD5_SMALL CONFIG_MD5_SMALL
  81. #endif
  82. #if BB_LITTLE_ENDIAN
  83. #define memcpy32_cpu2le memcpy
  84. #define memcpy32_le2cpu memcpy
  85. #else
  86. /* Encodes input (uint32_t) into output (unsigned char).
  87. * Assumes len is a multiple of 4. */
  88. static void
  89. memcpy32_cpu2le(unsigned char *output, uint32_t *input, unsigned len)
  90. {
  91. unsigned i, j;
  92. for (i = 0, j = 0; j < len; i++, j += 4) {
  93. output[j] = input[i];
  94. output[j+1] = (input[i] >> 8);
  95. output[j+2] = (input[i] >> 16);
  96. output[j+3] = (input[i] >> 24);
  97. }
  98. }
  99. /* Decodes input (unsigned char) into output (uint32_t).
  100. * Assumes len is a multiple of 4. */
  101. static void
  102. memcpy32_le2cpu(uint32_t *output, const unsigned char *input, unsigned len)
  103. {
  104. unsigned i, j;
  105. for (i = 0, j = 0; j < len; i++, j += 4)
  106. output[i] = ((uint32_t)input[j])
  107. | (((uint32_t)input[j+1]) << 8)
  108. | (((uint32_t)input[j+2]) << 16)
  109. | (((uint32_t)input[j+3]) << 24);
  110. }
  111. #endif /* i386 */
  112. /* F, G, H and I are basic MD5 functions. */
  113. #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
  114. #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
  115. #define H(x, y, z) ((x) ^ (y) ^ (z))
  116. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  117. /* rotl32 rotates x left n bits. */
  118. #define rotl32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
  119. /*
  120. * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  121. * Rotation is separate from addition to prevent recomputation.
  122. */
  123. #define FF(a, b, c, d, x, s, ac) { \
  124. (a) += F((b), (c), (d)) + (x) + (uint32_t)(ac); \
  125. (a) = rotl32((a), (s)); \
  126. (a) += (b); \
  127. }
  128. #define GG(a, b, c, d, x, s, ac) { \
  129. (a) += G((b), (c), (d)) + (x) + (uint32_t)(ac); \
  130. (a) = rotl32((a), (s)); \
  131. (a) += (b); \
  132. }
  133. #define HH(a, b, c, d, x, s, ac) { \
  134. (a) += H((b), (c), (d)) + (x) + (uint32_t)(ac); \
  135. (a) = rotl32((a), (s)); \
  136. (a) += (b); \
  137. }
  138. #define II(a, b, c, d, x, s, ac) { \
  139. (a) += I((b), (c), (d)) + (x) + (uint32_t)(ac); \
  140. (a) = rotl32((a), (s)); \
  141. (a) += (b); \
  142. }
  143. /* MD5 basic transformation. Transforms state based on block. */
  144. static void md5_transform(uint32_t state[4], const unsigned char block[64])
  145. {
  146. uint32_t a, b, c, d, x[16];
  147. #if MD5_SMALL > 1
  148. uint32_t temp;
  149. const unsigned char *ps;
  150. static const unsigned char S[] = {
  151. 7, 12, 17, 22,
  152. 5, 9, 14, 20,
  153. 4, 11, 16, 23,
  154. 6, 10, 15, 21
  155. };
  156. #endif /* MD5_SMALL > 1 */
  157. #if MD5_SMALL > 0
  158. const uint32_t *pc;
  159. const unsigned char *pp;
  160. int i;
  161. static const uint32_t C[] = {
  162. /* round 1 */
  163. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  164. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  165. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  166. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  167. /* round 2 */
  168. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  169. 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
  170. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  171. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  172. /* round 3 */
  173. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  174. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  175. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
  176. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  177. /* round 4 */
  178. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  179. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  180. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  181. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  182. };
  183. static const unsigned char P[] = {
  184. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
  185. 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
  186. 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
  187. 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
  188. };
  189. #endif /* MD5_SMALL > 0 */
  190. memcpy32_le2cpu(x, block, 64);
  191. a = state[0];
  192. b = state[1];
  193. c = state[2];
  194. d = state[3];
  195. #if MD5_SMALL > 2
  196. pc = C;
  197. pp = P;
  198. ps = S - 4;
  199. for (i = 0; i < 64; i++) {
  200. if ((i & 0x0f) == 0) ps += 4;
  201. temp = a;
  202. switch (i>>4) {
  203. case 0:
  204. temp += F(b, c, d);
  205. break;
  206. case 1:
  207. temp += G(b, c, d);
  208. break;
  209. case 2:
  210. temp += H(b, c, d);
  211. break;
  212. case 3:
  213. temp += I(b, c, d);
  214. break;
  215. }
  216. temp += x[*pp++] + *pc++;
  217. temp = rotl32(temp, ps[i & 3]);
  218. temp += b;
  219. a = d; d = c; c = b; b = temp;
  220. }
  221. #elif MD5_SMALL > 1
  222. pc = C;
  223. pp = P;
  224. ps = S;
  225. /* Round 1 */
  226. for (i = 0; i < 16; i++) {
  227. FF(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  228. temp = d; d = c; c = b; b = a; a = temp;
  229. }
  230. /* Round 2 */
  231. ps += 4;
  232. for (; i < 32; i++) {
  233. GG(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  234. temp = d; d = c; c = b; b = a; a = temp;
  235. }
  236. /* Round 3 */
  237. ps += 4;
  238. for (; i < 48; i++) {
  239. HH(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  240. temp = d; d = c; c = b; b = a; a = temp;
  241. }
  242. /* Round 4 */
  243. ps += 4;
  244. for (; i < 64; i++) {
  245. II(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  246. temp = d; d = c; c = b; b = a; a = temp;
  247. }
  248. #elif MD5_SMALL > 0
  249. pc = C;
  250. pp = P;
  251. /* Round 1 */
  252. for (i = 0; i < 4; i++) {
  253. FF(a, b, c, d, x[*pp], 7, *pc); pp++; pc++;
  254. FF(d, a, b, c, x[*pp], 12, *pc); pp++; pc++;
  255. FF(c, d, a, b, x[*pp], 17, *pc); pp++; pc++;
  256. FF(b, c, d, a, x[*pp], 22, *pc); pp++; pc++;
  257. }
  258. /* Round 2 */
  259. for (i = 0; i < 4; i++) {
  260. GG(a, b, c, d, x[*pp], 5, *pc); pp++; pc++;
  261. GG(d, a, b, c, x[*pp], 9, *pc); pp++; pc++;
  262. GG(c, d, a, b, x[*pp], 14, *pc); pp++; pc++;
  263. GG(b, c, d, a, x[*pp], 20, *pc); pp++; pc++;
  264. }
  265. /* Round 3 */
  266. for (i = 0; i < 4; i++) {
  267. HH(a, b, c, d, x[*pp], 4, *pc); pp++; pc++;
  268. HH(d, a, b, c, x[*pp], 11, *pc); pp++; pc++;
  269. HH(c, d, a, b, x[*pp], 16, *pc); pp++; pc++;
  270. HH(b, c, d, a, x[*pp], 23, *pc); pp++; pc++;
  271. }
  272. /* Round 4 */
  273. for (i = 0; i < 4; i++) {
  274. II(a, b, c, d, x[*pp], 6, *pc); pp++; pc++;
  275. II(d, a, b, c, x[*pp], 10, *pc); pp++; pc++;
  276. II(c, d, a, b, x[*pp], 15, *pc); pp++; pc++;
  277. II(b, c, d, a, x[*pp], 21, *pc); pp++; pc++;
  278. }
  279. #else
  280. /* Round 1 */
  281. #define S11 7
  282. #define S12 12
  283. #define S13 17
  284. #define S14 22
  285. FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  286. FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  287. FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  288. FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  289. FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  290. FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  291. FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  292. FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  293. FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  294. FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  295. FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  296. FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  297. FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  298. FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  299. FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  300. FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  301. /* Round 2 */
  302. #define S21 5
  303. #define S22 9
  304. #define S23 14
  305. #define S24 20
  306. GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  307. GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  308. GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  309. GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  310. GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  311. GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  312. GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  313. GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  314. GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  315. GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  316. GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  317. GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  318. GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  319. GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  320. GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  321. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  322. /* Round 3 */
  323. #define S31 4
  324. #define S32 11
  325. #define S33 16
  326. #define S34 23
  327. HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  328. HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  329. HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  330. HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  331. HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  332. HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  333. HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  334. HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  335. HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  336. HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  337. HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  338. HH(b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  339. HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  340. HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  341. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  342. HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  343. /* Round 4 */
  344. #define S41 6
  345. #define S42 10
  346. #define S43 15
  347. #define S44 21
  348. II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  349. II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  350. II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  351. II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  352. II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  353. II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  354. II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  355. II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  356. II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  357. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  358. II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  359. II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  360. II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  361. II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  362. II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  363. II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  364. #endif
  365. state[0] += a;
  366. state[1] += b;
  367. state[2] += c;
  368. state[3] += d;
  369. /* Zeroize sensitive information. */
  370. memset(x, 0, sizeof(x));
  371. }
  372. /* MD5 initialization. */
  373. void FAST_FUNC md5_begin(md5_ctx_t *context)
  374. {
  375. context->count[0] = context->count[1] = 0;
  376. /* Load magic initialization constants. */
  377. context->state[0] = 0x67452301;
  378. context->state[1] = 0xefcdab89;
  379. context->state[2] = 0x98badcfe;
  380. context->state[3] = 0x10325476;
  381. }
  382. /*
  383. * MD5 block update operation. Continues an MD5 message-digest
  384. * operation, processing another message block, and updating
  385. * the context.
  386. */
  387. void FAST_FUNC md5_hash(const void *buffer, size_t inputLen, md5_ctx_t *context)
  388. {
  389. unsigned i, idx, partLen;
  390. const unsigned char *input = buffer;
  391. /* Compute number of bytes mod 64 */
  392. idx = (context->count[0] >> 3) & 0x3F;
  393. /* Update number of bits */
  394. context->count[0] += (inputLen << 3);
  395. if (context->count[0] < (inputLen << 3))
  396. context->count[1]++;
  397. context->count[1] += (inputLen >> 29);
  398. /* Transform as many times as possible. */
  399. i = 0;
  400. partLen = 64 - idx;
  401. if (inputLen >= partLen) {
  402. memcpy(&context->buffer[idx], input, partLen);
  403. md5_transform(context->state, context->buffer);
  404. for (i = partLen; i + 63 < inputLen; i += 64)
  405. md5_transform(context->state, &input[i]);
  406. idx = 0;
  407. }
  408. /* Buffer remaining input */
  409. memcpy(&context->buffer[idx], &input[i], inputLen - i);
  410. }
  411. /*
  412. * MD5 finalization. Ends an MD5 message-digest operation,
  413. * writing the message digest.
  414. */
  415. unsigned FAST_FUNC md5_end(void *digest, md5_ctx_t *context)
  416. {
  417. unsigned idx, padLen;
  418. unsigned char bits[8];
  419. unsigned char padding[64];
  420. /* Add padding followed by original length. */
  421. memset(padding, 0, sizeof(padding));
  422. padding[0] = 0x80;
  423. /* save number of bits */
  424. memcpy32_cpu2le(bits, context->count, 8);
  425. /* pad out to 56 mod 64 */
  426. idx = (context->count[0] >> 3) & 0x3f;
  427. padLen = (idx < 56) ? (56 - idx) : (120 - idx);
  428. md5_hash(padding, padLen, context);
  429. /* append length (before padding) */
  430. md5_hash(bits, 8, context);
  431. /* Store state in digest */
  432. memcpy32_cpu2le(digest, context->state, 16);
  433. return 16;
  434. }