]> granicus.if.org Git - linux-pam/blob - modules/pam_pwdb/md5_crypt.c
Initial revision
[linux-pam] / modules / pam_pwdb / md5_crypt.c
1 /* $Id$
2  *
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  * ----------------------------------------------------------------------------
9  *
10  * Origin: Id: crypt.c,v 1.3 1995/05/30 05:42:22 rgrimes Exp
11  *
12  * $Log$
13  * Revision 1.1  2000/06/20 22:11:48  agmorgan
14  * Initial revision
15  *
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.]
20  *
21  * Revision 1.1.1.1  1998/07/12 05:17:17  morgan
22  * Linux PAM sources pre-0.66
23  *
24  * Revision 1.1  1996/09/05 06:43:31  morgan
25  * Initial revision
26  *
27  */
28
29 #include <string.h>
30 #include "md5.h"
31
32 static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
33         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
34
35 static void
36 to64(char *s, unsigned long v, int n)
37 {
38         while (--n >= 0) {
39                 *s++ = itoa64[v&0x3f];
40                 v >>= 6;
41         }
42 }
43
44 /*
45  * UNIX password
46  *
47  * Use MD5 for what it is best at...
48  */
49
50 char * MD5Name(crypt_md5)(const char *pw, const char *salt)
51 {
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];
58         int sl,pl,i,j;
59         MD5_CTX ctx,ctx1;
60         unsigned long l;
61
62         /* Refine the Salt first */
63         sp = salt;
64
65         /* If it starts with the magic string, then skip that */
66         if(!strncmp(sp,magic,strlen(magic)))
67                 sp += strlen(magic);
68
69         /* It stops at the first '$', max 8 chars */
70         for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
71                 continue;
72
73         /* get the length of the true salt */
74         sl = ep - sp;
75
76         MD5Name(MD5Init)(&ctx);
77
78         /* The password first, since that is what is most unknown */
79         MD5Name(MD5Update)(&ctx,(unsigned const char *)pw,strlen(pw));
80
81         /* Then our magic string */
82         MD5Name(MD5Update)(&ctx,(unsigned const char *)magic,strlen(magic));
83
84         /* Then the raw salt */
85         MD5Name(MD5Update)(&ctx,(unsigned const char *)sp,sl);
86
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);
95
96         /* Don't leave anything around in vm they could use. */
97         memset(final,0,sizeof final);
98
99         /* Then something really weird... */
100         for (j=0,i = strlen(pw); i ; i >>= 1)
101                 if(i&1)
102                     MD5Name(MD5Update)(&ctx, (unsigned const char *)final+j, 1);
103                 else
104                     MD5Name(MD5Update)(&ctx, (unsigned const char *)pw+j, 1);
105
106         /* Now make the output string */
107         strcpy(passwd,magic);
108         strncat(passwd,sp,sl);
109         strcat(passwd,"$");
110
111         MD5Name(MD5Final)(final,&ctx);
112
113         /*
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...
117          */
118         for(i=0;i<1000;i++) {
119                 MD5Name(MD5Init)(&ctx1);
120                 if(i & 1)
121                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
122                 else
123                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)final,16);
124
125                 if(i % 3)
126                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)sp,sl);
127
128                 if(i % 7)
129                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
130
131                 if(i & 1)
132                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)final,16);
133                 else
134                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
135                 MD5Name(MD5Final)(final,&ctx1);
136         }
137
138         p = passwd + strlen(passwd);
139
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;
146         *p = '\0';
147
148         /* Don't leave anything around in vm they could use. */
149         memset(final,0,sizeof final);
150
151         return passwd;
152 }
153