pw_encrypt_md5.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  3. *
  4. * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  5. * rights reserved.
  6. *
  7. * License to copy and use this software is granted provided that it
  8. * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  9. * Algorithm" in all material mentioning or referencing this software
  10. * or this function.
  11. *
  12. * License is also granted to make and use derivative works provided
  13. * that such works are identified as "derived from the RSA Data
  14. * Security, Inc. MD5 Message-Digest Algorithm" in all material
  15. * mentioning or referencing the derived work.
  16. *
  17. * RSA Data Security, Inc. makes no representations concerning either
  18. * the merchantability of this software or the suitability of this
  19. * software for any particular purpose. It is provided "as is"
  20. * without express or implied warranty of any kind.
  21. *
  22. * These notices must be retained in any copies of any part of this
  23. * documentation and/or software.
  24. *
  25. * $FreeBSD: src/lib/libmd/md5c.c,v 1.9.2.1 1999/08/29 14:57:12 peter Exp $
  26. *
  27. * This code is the same as the code published by RSA Inc. It has been
  28. * edited for clarity and style only.
  29. *
  30. * ----------------------------------------------------------------------------
  31. * The md5_crypt() function was taken from freeBSD's libcrypt and contains
  32. * this license:
  33. * "THE BEER-WARE LICENSE" (Revision 42):
  34. * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
  35. * can do whatever you want with this stuff. If we meet some day, and you think
  36. * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
  37. *
  38. * $FreeBSD: src/lib/libcrypt/crypt.c,v 1.7.2.1 1999/08/29 14:56:33 peter Exp $
  39. *
  40. * ----------------------------------------------------------------------------
  41. * On April 19th, 2001 md5_crypt() was modified to make it reentrant
  42. * by Erik Andersen <andersen@uclibc.org>
  43. *
  44. *
  45. * June 28, 2001 Manuel Novoa III
  46. *
  47. * "Un-inlined" code using loops and static const tables in order to
  48. * reduce generated code size (on i386 from approx 4k to approx 2.5k).
  49. *
  50. * June 29, 2001 Manuel Novoa III
  51. *
  52. * Completely removed static PADDING array.
  53. *
  54. * Reintroduced the loop unrolling in MD5_Transform and added the
  55. * MD5_SIZE_OVER_SPEED option for configurability. Define below as:
  56. * 0 fully unrolled loops
  57. * 1 partially unrolled (4 ops per loop)
  58. * 2 no unrolling -- introduces the need to swap 4 variables (slow)
  59. * 3 no unrolling and all 4 loops merged into one with switch
  60. * in each loop (glacial)
  61. * On i386, sizes are roughly (-Os -fno-builtin):
  62. * 0: 3k 1: 2.5k 2: 2.2k 3: 2k
  63. *
  64. *
  65. * Since SuSv3 does not require crypt_r, modified again August 7, 2002
  66. * by Erik Andersen to remove reentrance stuff...
  67. */
  68. /*
  69. * Valid values are 1 (fastest/largest) to 3 (smallest/slowest).
  70. */
  71. #define MD5_SIZE_OVER_SPEED 3
  72. /**********************************************************************/
  73. /* MD5 context. */
  74. struct MD5Context {
  75. uint32_t state[4]; /* state (ABCD) */
  76. uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
  77. unsigned char buffer[64]; /* input buffer */
  78. };
  79. static void __md5_Init(struct MD5Context *);
  80. static void __md5_Update(struct MD5Context *, const unsigned char *, unsigned int);
  81. static void __md5_Pad(struct MD5Context *);
  82. static void __md5_Final(unsigned char [16], struct MD5Context *);
  83. static void __md5_Transform(uint32_t [4], const unsigned char [64]);
  84. #define MD5_MAGIC_STR "$1$"
  85. #define MD5_MAGIC_LEN (sizeof(MD5_MAGIC_STR) - 1)
  86. static const unsigned char __md5__magic[] = MD5_MAGIC_STR;
  87. #ifdef i386
  88. #define __md5_Encode memcpy
  89. #define __md5_Decode memcpy
  90. #else /* i386 */
  91. /*
  92. * __md5_Encodes input (uint32_t) into output (unsigned char). Assumes len is
  93. * a multiple of 4.
  94. */
  95. static void
  96. __md5_Encode(unsigned char *output, uint32_t *input, unsigned int len)
  97. {
  98. unsigned int i, j;
  99. for (i = 0, j = 0; j < len; i++, j += 4) {
  100. output[j] = input[i];
  101. output[j+1] = (input[i] >> 8);
  102. output[j+2] = (input[i] >> 16);
  103. output[j+3] = (input[i] >> 24);
  104. }
  105. }
  106. /*
  107. * __md5_Decodes input (unsigned char) into output (uint32_t). Assumes len is
  108. * a multiple of 4.
  109. */
  110. static void
  111. __md5_Decode(uint32_t *output, const unsigned char *input, unsigned int len)
  112. {
  113. unsigned int i, j;
  114. for (i = 0, j = 0; j < len; i++, j += 4)
  115. output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
  116. (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
  117. }
  118. #endif /* i386 */
  119. /* F, G, H and I are basic MD5 functions. */
  120. #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
  121. #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
  122. #define H(x, y, z) ((x) ^ (y) ^ (z))
  123. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  124. /* ROTATE_LEFT rotates x left n bits. */
  125. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  126. /*
  127. * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  128. * Rotation is separate from addition to prevent recomputation.
  129. */
  130. #define FF(a, b, c, d, x, s, ac) { \
  131. (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  132. (a) = ROTATE_LEFT((a), (s)); \
  133. (a) += (b); \
  134. }
  135. #define GG(a, b, c, d, x, s, ac) { \
  136. (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  137. (a) = ROTATE_LEFT((a), (s)); \
  138. (a) += (b); \
  139. }
  140. #define HH(a, b, c, d, x, s, ac) { \
  141. (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  142. (a) = ROTATE_LEFT((a), (s)); \
  143. (a) += (b); \
  144. }
  145. #define II(a, b, c, d, x, s, ac) { \
  146. (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  147. (a) = ROTATE_LEFT((a), (s)); \
  148. (a) += (b); \
  149. }
  150. /* MD5 initialization. Begins an MD5 operation, writing a new context. */
  151. static void __md5_Init(struct MD5Context *context)
  152. {
  153. context->count[0] = context->count[1] = 0;
  154. /* Load magic initialization constants. */
  155. context->state[0] = 0x67452301;
  156. context->state[1] = 0xefcdab89;
  157. context->state[2] = 0x98badcfe;
  158. context->state[3] = 0x10325476;
  159. }
  160. /*
  161. * MD5 block update operation. Continues an MD5 message-digest
  162. * operation, processing another message block, and updating the
  163. * context.
  164. */
  165. static void __md5_Update(struct MD5Context *context, const unsigned char *input, unsigned int inputLen)
  166. {
  167. unsigned int i, idx, partLen;
  168. /* Compute number of bytes mod 64 */
  169. idx = (context->count[0] >> 3) & 0x3F;
  170. /* Update number of bits */
  171. context->count[0] += (inputLen << 3);
  172. if (context->count[0] < (inputLen << 3))
  173. context->count[1]++;
  174. context->count[1] += (inputLen >> 29);
  175. partLen = 64 - idx;
  176. /* Transform as many times as possible. */
  177. if (inputLen >= partLen) {
  178. memcpy(&context->buffer[idx], input, partLen);
  179. __md5_Transform(context->state, context->buffer);
  180. for (i = partLen; i + 63 < inputLen; i += 64)
  181. __md5_Transform(context->state, &input[i]);
  182. idx = 0;
  183. } else
  184. i = 0;
  185. /* Buffer remaining input */
  186. memcpy(&context->buffer[idx], &input[i], inputLen - i);
  187. }
  188. /*
  189. * MD5 padding. Adds padding followed by original length.
  190. */
  191. static void __md5_Pad(struct MD5Context *context)
  192. {
  193. unsigned char bits[8];
  194. unsigned int idx, padLen;
  195. unsigned char PADDING[64];
  196. memset(PADDING, 0, sizeof(PADDING));
  197. PADDING[0] = 0x80;
  198. /* Save number of bits */
  199. __md5_Encode(bits, context->count, 8);
  200. /* Pad out to 56 mod 64. */
  201. idx = (context->count[0] >> 3) & 0x3f;
  202. padLen = (idx < 56) ? (56 - idx) : (120 - idx);
  203. __md5_Update(context, PADDING, padLen);
  204. /* Append length (before padding) */
  205. __md5_Update(context, bits, 8);
  206. }
  207. /*
  208. * MD5 finalization. Ends an MD5 message-digest operation, writing the
  209. * the message digest and zeroizing the context.
  210. */
  211. static void __md5_Final(unsigned char digest[16], struct MD5Context *context)
  212. {
  213. /* Do padding. */
  214. __md5_Pad(context);
  215. /* Store state in digest */
  216. __md5_Encode(digest, context->state, 16);
  217. /* Zeroize sensitive information. */
  218. memset(context, 0, sizeof(*context));
  219. }
  220. /* MD5 basic transformation. Transforms state based on block. */
  221. static void __md5_Transform(uint32_t state[4], const unsigned char block[64])
  222. {
  223. uint32_t a, b, c, d, x[16];
  224. #if MD5_SIZE_OVER_SPEED > 1
  225. uint32_t temp;
  226. const unsigned char *ps;
  227. static const unsigned char S[] = {
  228. 7, 12, 17, 22,
  229. 5, 9, 14, 20,
  230. 4, 11, 16, 23,
  231. 6, 10, 15, 21
  232. };
  233. #endif /* MD5_SIZE_OVER_SPEED > 1 */
  234. #if MD5_SIZE_OVER_SPEED > 0
  235. const uint32_t *pc;
  236. const unsigned char *pp;
  237. int i;
  238. static const uint32_t C[] = {
  239. /* round 1 */
  240. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  241. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  242. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  243. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  244. /* round 2 */
  245. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  246. 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8,
  247. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  248. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  249. /* round 3 */
  250. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  251. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  252. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
  253. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  254. /* round 4 */
  255. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  256. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  257. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  258. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
  259. };
  260. static const unsigned char P[] = {
  261. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
  262. 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
  263. 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
  264. 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
  265. };
  266. #endif /* MD5_SIZE_OVER_SPEED > 0 */
  267. __md5_Decode(x, block, 64);
  268. a = state[0]; b = state[1]; c = state[2]; d = state[3];
  269. #if MD5_SIZE_OVER_SPEED > 2
  270. pc = C; pp = P; ps = S - 4;
  271. for (i = 0; i < 64; i++) {
  272. if ((i & 0x0f) == 0) ps += 4;
  273. temp = a;
  274. switch (i>>4) {
  275. case 0:
  276. temp += F(b, c, d);
  277. break;
  278. case 1:
  279. temp += G(b, c, d);
  280. break;
  281. case 2:
  282. temp += H(b, c, d);
  283. break;
  284. case 3:
  285. temp += I(b, c, d);
  286. break;
  287. }
  288. temp += x[*pp++] + *pc++;
  289. temp = ROTATE_LEFT(temp, ps[i & 3]);
  290. temp += b;
  291. a = d; d = c; c = b; b = temp;
  292. }
  293. #elif MD5_SIZE_OVER_SPEED > 1
  294. pc = C; pp = P; ps = S;
  295. /* Round 1 */
  296. for (i = 0; i < 16; i++) {
  297. FF(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  298. temp = d; d = c; c = b; b = a; a = temp;
  299. }
  300. /* Round 2 */
  301. ps += 4;
  302. for (; i < 32; i++) {
  303. GG(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  304. temp = d; d = c; c = b; b = a; a = temp;
  305. }
  306. /* Round 3 */
  307. ps += 4;
  308. for (; i < 48; i++) {
  309. HH(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  310. temp = d; d = c; c = b; b = a; a = temp;
  311. }
  312. /* Round 4 */
  313. ps += 4;
  314. for (; i < 64; i++) {
  315. II(a, b, c, d, x[*pp], ps[i & 0x3], *pc); pp++; pc++;
  316. temp = d; d = c; c = b; b = a; a = temp;
  317. }
  318. #elif MD5_SIZE_OVER_SPEED > 0
  319. pc = C; pp = P;
  320. /* Round 1 */
  321. for (i = 0; i < 4; i++) {
  322. FF(a, b, c, d, x[*pp], 7, *pc); pp++; pc++;
  323. FF(d, a, b, c, x[*pp], 12, *pc); pp++; pc++;
  324. FF(c, d, a, b, x[*pp], 17, *pc); pp++; pc++;
  325. FF(b, c, d, a, x[*pp], 22, *pc); pp++; pc++;
  326. }
  327. /* Round 2 */
  328. for (i = 0; i < 4; i++) {
  329. GG(a, b, c, d, x[*pp], 5, *pc); pp++; pc++;
  330. GG(d, a, b, c, x[*pp], 9, *pc); pp++; pc++;
  331. GG(c, d, a, b, x[*pp], 14, *pc); pp++; pc++;
  332. GG(b, c, d, a, x[*pp], 20, *pc); pp++; pc++;
  333. }
  334. /* Round 3 */
  335. for (i = 0; i < 4; i++) {
  336. HH(a, b, c, d, x[*pp], 4, *pc); pp++; pc++;
  337. HH(d, a, b, c, x[*pp], 11, *pc); pp++; pc++;
  338. HH(c, d, a, b, x[*pp], 16, *pc); pp++; pc++;
  339. HH(b, c, d, a, x[*pp], 23, *pc); pp++; pc++;
  340. }
  341. /* Round 4 */
  342. for (i = 0; i < 4; i++) {
  343. II(a, b, c, d, x[*pp], 6, *pc); pp++; pc++;
  344. II(d, a, b, c, x[*pp], 10, *pc); pp++; pc++;
  345. II(c, d, a, b, x[*pp], 15, *pc); pp++; pc++;
  346. II(b, c, d, a, x[*pp], 21, *pc); pp++; pc++;
  347. }
  348. #else
  349. /* Round 1 */
  350. #define S11 7
  351. #define S12 12
  352. #define S13 17
  353. #define S14 22
  354. FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  355. FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  356. FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  357. FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  358. FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  359. FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  360. FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  361. FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  362. FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  363. FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  364. FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  365. FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  366. FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  367. FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  368. FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  369. FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  370. /* Round 2 */
  371. #define S21 5
  372. #define S22 9
  373. #define S23 14
  374. #define S24 20
  375. GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  376. GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  377. GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  378. GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  379. GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  380. GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  381. GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  382. GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  383. GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  384. GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  385. GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  386. GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  387. GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  388. GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  389. GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  390. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  391. /* Round 3 */
  392. #define S31 4
  393. #define S32 11
  394. #define S33 16
  395. #define S34 23
  396. HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  397. HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  398. HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  399. HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  400. HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  401. HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  402. HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  403. HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  404. HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  405. HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  406. HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  407. HH(b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  408. HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  409. HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  410. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  411. HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  412. /* Round 4 */
  413. #define S41 6
  414. #define S42 10
  415. #define S43 15
  416. #define S44 21
  417. II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  418. II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  419. II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  420. II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  421. II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  422. II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  423. II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  424. II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  425. II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  426. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  427. II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  428. II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  429. II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  430. II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  431. II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  432. II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  433. #endif
  434. state[0] += a;
  435. state[1] += b;
  436. state[2] += c;
  437. state[3] += d;
  438. /* Zeroize sensitive information. */
  439. memset(x, 0, sizeof(x));
  440. }
  441. static char*
  442. __md5_to64(char *s, unsigned v, int n)
  443. {
  444. while (--n >= 0) {
  445. *s++ = ascii64[v & 0x3f];
  446. v >>= 6;
  447. }
  448. return s;
  449. }
  450. /*
  451. * UNIX password
  452. *
  453. * Use MD5 for what it is best at...
  454. */
  455. #define MD5_OUT_BUFSIZE 36
  456. static char *
  457. NOINLINE
  458. md5_crypt(char passwd[MD5_OUT_BUFSIZE], const unsigned char *pw, const unsigned char *salt)
  459. {
  460. const unsigned char *sp, *ep;
  461. char *p;
  462. unsigned char final[17]; /* final[16] exists only to aid in looping */
  463. int sl, pl, i, pw_len;
  464. struct MD5Context ctx, ctx1;
  465. /* Refine the Salt first */
  466. sp = salt;
  467. // always true for bbox
  468. // /* If it starts with the magic string, then skip that */
  469. // if (!strncmp(sp, __md5__magic, MD5_MAGIC_LEN))
  470. sp += MD5_MAGIC_LEN;
  471. /* It stops at the first '$', max 8 chars */
  472. for (ep = sp; *ep && *ep != '$' && ep < (sp+8); ep++)
  473. continue;
  474. /* get the length of the true salt */
  475. sl = ep - sp;
  476. __md5_Init(&ctx);
  477. /* The password first, since that is what is most unknown */
  478. pw_len = strlen((char*)pw);
  479. __md5_Update(&ctx, pw, pw_len);
  480. /* Then our magic string */
  481. __md5_Update(&ctx, __md5__magic, MD5_MAGIC_LEN);
  482. /* Then the raw salt */
  483. __md5_Update(&ctx, sp, sl);
  484. /* Then just as many characters of the MD5(pw, salt, pw) */
  485. __md5_Init(&ctx1);
  486. __md5_Update(&ctx1, pw, pw_len);
  487. __md5_Update(&ctx1, sp, sl);
  488. __md5_Update(&ctx1, pw, pw_len);
  489. __md5_Final(final, &ctx1);
  490. for (pl = pw_len; pl > 0; pl -= 16)
  491. __md5_Update(&ctx, final, pl > 16 ? 16 : pl);
  492. /* Don't leave anything around in vm they could use. */
  493. //TODO: the above comment seems to be wrong. final is used later.
  494. memset(final, 0, sizeof(final));
  495. /* Then something really weird... */
  496. for (i = pw_len; i; i >>= 1) {
  497. __md5_Update(&ctx, ((i & 1) ? final : (const unsigned char *) pw), 1);
  498. }
  499. /* Now make the output string */
  500. passwd[0] = '$';
  501. passwd[1] = '1';
  502. passwd[2] = '$';
  503. strncpy(passwd + 3, (char*)sp, sl);
  504. passwd[sl + 3] = '$';
  505. __md5_Final(final, &ctx);
  506. /*
  507. * and now, just to make sure things don't run too fast
  508. * On a 60 Mhz Pentium this takes 34 msec, so you would
  509. * need 30 seconds to build a 1000 entry dictionary...
  510. */
  511. for (i = 0; i < 1000; i++) {
  512. __md5_Init(&ctx1);
  513. if (i & 1)
  514. __md5_Update(&ctx1, pw, pw_len);
  515. else
  516. __md5_Update(&ctx1, final, 16);
  517. if (i % 3)
  518. __md5_Update(&ctx1, sp, sl);
  519. if (i % 7)
  520. __md5_Update(&ctx1, pw, pw_len);
  521. if (i & 1)
  522. __md5_Update(&ctx1, final, 16);
  523. else
  524. __md5_Update(&ctx1, pw, pw_len);
  525. __md5_Final(final, &ctx1);
  526. }
  527. p = passwd + sl + 4; /* 12 bytes max (sl is up to 8 bytes) */
  528. /* Add 5*4+2 = 22 bytes of hash, + NUL byte. */
  529. final[16] = final[5];
  530. for (i = 0; i < 5; i++) {
  531. unsigned l = (final[i] << 16) | (final[i+6] << 8) | final[i+12];
  532. p = __md5_to64(p, l, 4);
  533. }
  534. p = __md5_to64(p, final[11], 2);
  535. *p = '\0';
  536. /* Don't leave anything around in vm they could use. */
  537. memset(final, 0, sizeof(final));
  538. return passwd;
  539. }
  540. #undef MD5_SIZE_OVER_SPEED
  541. #undef MD5_MAGIC_STR
  542. #undef MD5_MAGIC_LEN
  543. #undef __md5_Encode
  544. #undef __md5_Decode
  545. #undef F
  546. #undef G
  547. #undef H
  548. #undef I
  549. #undef ROTATE_LEFT
  550. #undef FF
  551. #undef GG
  552. #undef HH
  553. #undef II
  554. #undef S11
  555. #undef S12
  556. #undef S13
  557. #undef S14
  558. #undef S21
  559. #undef S22
  560. #undef S23
  561. #undef S24
  562. #undef S31
  563. #undef S32
  564. #undef S33
  565. #undef S34
  566. #undef S41
  567. #undef S42
  568. #undef S43
  569. #undef S44