md5.c 13 KB

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