3 * ----------------------------------------------------------------------------
4 * "THE BEER-WARE LICENSE" (Revision 42):
5 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
6 * can do whatever you want with this stuff. If we meet some day, and you think
7 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
8 * ----------------------------------------------------------------------------
10 * Origin: Id: crypt.c,v 1.3 1995/05/30 05:42:22 rgrimes Exp
13 * Revision 1.1 2000/06/20 22:11:48 agmorgan
16 * Revision 1.2 1999/07/04 23:22:38 morgan
17 * Andrey's MD5 (bigendian) work around + cleanup to address problems with
18 * applications that let an (ab)user kill them off without giving PAM the
19 * opportunity to end. [Problem report from Tani Hosokawa on bugtraq.]
21 * Revision 1.1.1.1 1998/07/12 05:17:17 morgan
22 * Linux PAM sources pre-0.66
24 * Revision 1.1 1996/09/05 06:43:31 morgan
32 static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
33 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
36 to64(char *s, unsigned long v, int n)
39 *s++ = itoa64[v&0x3f];
47 * Use MD5 for what it is best at...
50 char * MD5Name(crypt_md5)(const char *pw, const char *salt)
52 const char *magic = "$1$";
53 /* This string is magic for this algorithm. Having
54 * it this way, we can get get better later on */
55 static char passwd[120], *p;
56 static const char *sp,*ep;
57 unsigned char final[16];
62 /* Refine the Salt first */
65 /* If it starts with the magic string, then skip that */
66 if(!strncmp(sp,magic,strlen(magic)))
69 /* It stops at the first '$', max 8 chars */
70 for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
73 /* get the length of the true salt */
76 MD5Name(MD5Init)(&ctx);
78 /* The password first, since that is what is most unknown */
79 MD5Name(MD5Update)(&ctx,(unsigned const char *)pw,strlen(pw));
81 /* Then our magic string */
82 MD5Name(MD5Update)(&ctx,(unsigned const char *)magic,strlen(magic));
84 /* Then the raw salt */
85 MD5Name(MD5Update)(&ctx,(unsigned const char *)sp,sl);
87 /* Then just as many characters of the MD5(pw,salt,pw) */
88 MD5Name(MD5Init)(&ctx1);
89 MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
90 MD5Name(MD5Update)(&ctx1,(unsigned const char *)sp,sl);
91 MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
92 MD5Name(MD5Final)(final,&ctx1);
93 for(pl = strlen(pw); pl > 0; pl -= 16)
94 MD5Name(MD5Update)(&ctx,(unsigned const char *)final,pl>16 ? 16 : pl);
96 /* Don't leave anything around in vm they could use. */
97 memset(final,0,sizeof final);
99 /* Then something really weird... */
100 for (j=0,i = strlen(pw); i ; i >>= 1)
102 MD5Name(MD5Update)(&ctx, (unsigned const char *)final+j, 1);
104 MD5Name(MD5Update)(&ctx, (unsigned const char *)pw+j, 1);
106 /* Now make the output string */
107 strcpy(passwd,magic);
108 strncat(passwd,sp,sl);
111 MD5Name(MD5Final)(final,&ctx);
114 * and now, just to make sure things don't run too fast
115 * On a 60 Mhz Pentium this takes 34 msec, so you would
116 * need 30 seconds to build a 1000 entry dictionary...
118 for(i=0;i<1000;i++) {
119 MD5Name(MD5Init)(&ctx1);
121 MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
123 MD5Name(MD5Update)(&ctx1,(unsigned const char *)final,16);
126 MD5Name(MD5Update)(&ctx1,(unsigned const char *)sp,sl);
129 MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
132 MD5Name(MD5Update)(&ctx1,(unsigned const char *)final,16);
134 MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
135 MD5Name(MD5Final)(final,&ctx1);
138 p = passwd + strlen(passwd);
140 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
141 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
142 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
143 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
144 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
145 l = final[11] ; to64(p,l,2); p += 2;
148 /* Don't leave anything around in vm they could use. */
149 memset(final,0,sizeof final);