md5.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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@lineo.com>, <andersee@debian.org>
  43. *
  44. */
  45. #include <sys/types.h>
  46. #include <string.h>
  47. #include <unistd.h>
  48. #include <stdio.h>
  49. #include <crypt.h>
  50. #include <sys/cdefs.h>
  51. /* MD5 context. */
  52. typedef struct MD5Context {
  53. u_int32_t state[4]; /* state (ABCD) */
  54. u_int32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */
  55. unsigned char buffer[64]; /* input buffer */
  56. } MD5_CTX;
  57. void MD5Init (MD5_CTX *);
  58. void MD5Update (MD5_CTX *, const unsigned char *, unsigned int);
  59. void MD5Pad (MD5_CTX *);
  60. void MD5Final (unsigned char [16], MD5_CTX *);
  61. char * MD5End(MD5_CTX *, char *);
  62. char * MD5File(const char *, char *);
  63. char * MD5Data(const unsigned char *, unsigned int, char *);
  64. char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data);
  65. char *md5_magic = "$1$"; /* * This string is magic for this algorithm. Having
  66. it this way, we can get better later on */
  67. static const unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
  68. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  69. static void MD5Transform __P((u_int32_t [4], const unsigned char [64]));
  70. #ifdef KERNEL
  71. #define memset(x,y,z) bzero(x,z);
  72. #define memcpy(x,y,z) bcopy(y, x, z)
  73. #endif
  74. #ifdef i386
  75. #define Encode memcpy
  76. #define Decode memcpy
  77. #else /* i386 */
  78. /*
  79. * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
  80. * a multiple of 4.
  81. */
  82. static void
  83. Encode (output, input, len)
  84. unsigned char *output;
  85. u_int32_t *input;
  86. unsigned int len;
  87. {
  88. unsigned int i, j;
  89. for (i = 0, j = 0; j < len; i++, j += 4) {
  90. output[j] = (unsigned char)(input[i] & 0xff);
  91. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  92. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  93. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  94. }
  95. }
  96. /*
  97. * Decodes input (unsigned char) into output (u_int32_t). Assumes len is
  98. * a multiple of 4.
  99. */
  100. static void
  101. Decode (output, input, len)
  102. u_int32_t *output;
  103. const unsigned char *input;
  104. unsigned int len;
  105. {
  106. unsigned int i, j;
  107. for (i = 0, j = 0; j < len; i++, j += 4)
  108. output[i] = ((u_int32_t)input[j]) | (((u_int32_t)input[j+1]) << 8) |
  109. (((u_int32_t)input[j+2]) << 16) | (((u_int32_t)input[j+3]) << 24);
  110. }
  111. #endif /* i386 */
  112. static unsigned char PADDING[64] = {
  113. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  114. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  115. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  116. };
  117. /* F, G, H and I are basic MD5 functions. */
  118. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  119. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  120. #define H(x, y, z) ((x) ^ (y) ^ (z))
  121. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  122. /* ROTATE_LEFT rotates x left n bits. */
  123. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  124. /*
  125. * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  126. * Rotation is separate from addition to prevent recomputation.
  127. */
  128. #define FF(a, b, c, d, x, s, ac) { \
  129. (a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
  130. (a) = ROTATE_LEFT ((a), (s)); \
  131. (a) += (b); \
  132. }
  133. #define GG(a, b, c, d, x, s, ac) { \
  134. (a) += G ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
  135. (a) = ROTATE_LEFT ((a), (s)); \
  136. (a) += (b); \
  137. }
  138. #define HH(a, b, c, d, x, s, ac) { \
  139. (a) += H ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
  140. (a) = ROTATE_LEFT ((a), (s)); \
  141. (a) += (b); \
  142. }
  143. #define II(a, b, c, d, x, s, ac) { \
  144. (a) += I ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
  145. (a) = ROTATE_LEFT ((a), (s)); \
  146. (a) += (b); \
  147. }
  148. /* MD5 initialization. Begins an MD5 operation, writing a new context. */
  149. void MD5Init (MD5_CTX *context)
  150. {
  151. context->count[0] = context->count[1] = 0;
  152. /* Load magic initialization constants. */
  153. context->state[0] = 0x67452301;
  154. context->state[1] = 0xefcdab89;
  155. context->state[2] = 0x98badcfe;
  156. context->state[3] = 0x10325476;
  157. }
  158. /*
  159. * MD5 block update operation. Continues an MD5 message-digest
  160. * operation, processing another message block, and updating the
  161. * context.
  162. */
  163. void MD5Update ( MD5_CTX *context, const unsigned char *input, unsigned int inputLen)
  164. {
  165. unsigned int i, index, partLen;
  166. /* Compute number of bytes mod 64 */
  167. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  168. /* Update number of bits */
  169. if ((context->count[0] += ((u_int32_t)inputLen << 3))
  170. < ((u_int32_t)inputLen << 3))
  171. context->count[1]++;
  172. context->count[1] += ((u_int32_t)inputLen >> 29);
  173. partLen = 64 - index;
  174. /* Transform as many times as possible. */
  175. if (inputLen >= partLen) {
  176. memcpy((void *)&context->buffer[index], (const void *)input,
  177. partLen);
  178. MD5Transform (context->state, context->buffer);
  179. for (i = partLen; i + 63 < inputLen; i += 64)
  180. MD5Transform (context->state, &input[i]);
  181. index = 0;
  182. }
  183. else
  184. i = 0;
  185. /* Buffer remaining input */
  186. memcpy ((void *)&context->buffer[index], (const void *)&input[i],
  187. inputLen-i);
  188. }
  189. /*
  190. * MD5 padding. Adds padding followed by original length.
  191. */
  192. void MD5Pad ( MD5_CTX *context)
  193. {
  194. unsigned char bits[8];
  195. unsigned int index, padLen;
  196. /* Save number of bits */
  197. Encode (bits, context->count, 8);
  198. /* Pad out to 56 mod 64. */
  199. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  200. padLen = (index < 56) ? (56 - index) : (120 - index);
  201. MD5Update (context, PADDING, padLen);
  202. /* Append length (before padding) */
  203. MD5Update (context, bits, 8);
  204. }
  205. /*
  206. * MD5 finalization. Ends an MD5 message-digest operation, writing the
  207. * the message digest and zeroizing the context.
  208. */
  209. void MD5Final ( unsigned char digest[16], MD5_CTX *context)
  210. {
  211. /* Do padding. */
  212. MD5Pad (context);
  213. /* Store state in digest */
  214. Encode (digest, context->state, 16);
  215. /* Zeroize sensitive information. */
  216. memset ((void *)context, 0, sizeof (*context));
  217. }
  218. /* MD5 basic transformation. Transforms state based on block. */
  219. static void
  220. MD5Transform (state, block)
  221. u_int32_t state[4];
  222. const unsigned char block[64];
  223. {
  224. u_int32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  225. Decode (x, block, 64);
  226. /* Round 1 */
  227. #define S11 7
  228. #define S12 12
  229. #define S13 17
  230. #define S14 22
  231. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  232. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  233. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  234. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  235. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  236. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  237. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  238. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  239. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  240. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  241. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  242. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  243. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  244. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  245. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  246. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  247. /* Round 2 */
  248. #define S21 5
  249. #define S22 9
  250. #define S23 14
  251. #define S24 20
  252. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  253. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  254. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  255. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  256. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  257. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  258. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  259. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  260. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  261. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  262. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  263. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  264. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  265. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  266. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  267. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  268. /* Round 3 */
  269. #define S31 4
  270. #define S32 11
  271. #define S33 16
  272. #define S34 23
  273. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  274. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  275. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  276. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  277. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  278. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  279. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  280. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  281. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  282. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  283. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  284. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  285. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  286. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  287. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  288. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  289. /* Round 4 */
  290. #define S41 6
  291. #define S42 10
  292. #define S43 15
  293. #define S44 21
  294. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  295. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  296. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  297. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  298. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  299. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  300. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  301. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  302. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  303. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  304. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  305. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  306. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  307. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  308. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  309. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  310. state[0] += a;
  311. state[1] += b;
  312. state[2] += c;
  313. state[3] += d;
  314. /* Zeroize sensitive information. */
  315. memset ((void *)x, 0, sizeof (x));
  316. }
  317. static void to64( char *s, unsigned long v, int n)
  318. {
  319. while (--n >= 0) {
  320. *s++ = itoa64[v&0x3f];
  321. v >>= 6;
  322. }
  323. }
  324. /*
  325. * UNIX password
  326. *
  327. * Use MD5 for what it is best at...
  328. */
  329. char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
  330. {
  331. char *p = data->p;
  332. const char *sp = data->sp;
  333. const char *ep = data->ep;
  334. char *passwd = *data->KS;
  335. unsigned char final[16];
  336. int sl,pl,i;
  337. MD5_CTX ctx,ctx1;
  338. unsigned long l;
  339. /* Refine the Salt first */
  340. sp = salt;
  341. /* If it starts with the magic string, then skip that */
  342. if(!strncmp(sp,md5_magic,strlen(md5_magic)))
  343. sp += strlen(md5_magic);
  344. /* It stops at the first '$', max 8 chars */
  345. for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
  346. continue;
  347. /* get the length of the true salt */
  348. sl = ep - sp;
  349. MD5Init(&ctx);
  350. /* The password first, since that is what is most unknown */
  351. MD5Update(&ctx,pw,strlen(pw));
  352. /* Then our magic string */
  353. MD5Update(&ctx,md5_magic,strlen(md5_magic));
  354. /* Then the raw salt */
  355. MD5Update(&ctx,sp,sl);
  356. /* Then just as many characters of the MD5(pw,salt,pw) */
  357. MD5Init(&ctx1);
  358. MD5Update(&ctx1,pw,strlen(pw));
  359. MD5Update(&ctx1,sp,sl);
  360. MD5Update(&ctx1,pw,strlen(pw));
  361. MD5Final(final,&ctx1);
  362. for(pl = strlen(pw); pl > 0; pl -= 16)
  363. MD5Update(&ctx,final,pl>16 ? 16 : pl);
  364. /* Don't leave anything around in vm they could use. */
  365. memset(final,0,sizeof final);
  366. /* Then something really weird... */
  367. for (i = strlen(pw); i ; i >>= 1)
  368. if(i&1)
  369. MD5Update(&ctx, final, 1);
  370. else
  371. MD5Update(&ctx, pw, 1);
  372. /* Now make the output string */
  373. strcpy(passwd,md5_magic);
  374. strncat(passwd,sp,sl);
  375. strcat(passwd,"$");
  376. MD5Final(final,&ctx);
  377. /*
  378. * and now, just to make sure things don't run too fast
  379. * On a 60 Mhz Pentium this takes 34 msec, so you would
  380. * need 30 seconds to build a 1000 entry dictionary...
  381. */
  382. for(i=0;i<1000;i++) {
  383. MD5Init(&ctx1);
  384. if(i & 1)
  385. MD5Update(&ctx1,pw,strlen(pw));
  386. else
  387. MD5Update(&ctx1,final,16);
  388. if(i % 3)
  389. MD5Update(&ctx1,sp,sl);
  390. if(i % 7)
  391. MD5Update(&ctx1,pw,strlen(pw));
  392. if(i & 1)
  393. MD5Update(&ctx1,final,16);
  394. else
  395. MD5Update(&ctx1,pw,strlen(pw));
  396. MD5Final(final,&ctx1);
  397. }
  398. p = passwd + strlen(passwd);
  399. l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
  400. l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
  401. l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
  402. l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
  403. l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
  404. l = final[11] ; to64(p,l,2); p += 2;
  405. *p = '\0';
  406. /* Don't leave anything around in vm they could use. */
  407. memset(final,0,sizeof final);
  408. return passwd;
  409. }