]> granicus.if.org Git - linux-pam/blob - modules/pam_unix/md5_crypt.c
Initial revision
[linux-pam] / modules / pam_unix / md5_crypt.c
1 /*
2  * $Id$
3  *
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
9  * ----------------------------------------------------------------------------
10  *
11  * Origin: Id: crypt.c,v 1.3 1995/05/30 05:42:22 rgrimes Exp
12  *
13  */
14
15 #include <string.h>
16 #include "md5.h"
17
18 static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
19 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
20
21 static void to64(char *s, unsigned long v, int n)
22 {
23         while (--n >= 0) {
24                 *s++ = itoa64[v & 0x3f];
25                 v >>= 6;
26         }
27 }
28
29 /*
30  * UNIX password
31  *
32  * Use MD5 for what it is best at...
33  */
34
35 char *MD5Name(crypt_md5)(const char *pw, const char *salt)
36 {
37         const char *magic = "$1$";
38         /* This string is magic for this algorithm.  Having
39          * it this way, we can get get better later on */
40         static char passwd[120], *p;
41         static const char *sp, *ep;
42         unsigned char final[16];
43         int sl, pl, i, j;
44         MD5_CTX ctx, ctx1;
45         unsigned long l;
46
47         /* Refine the Salt first */
48         sp = salt;
49
50         /* If it starts with the magic string, then skip that */
51         if (!strncmp(sp, magic, strlen(magic)))
52                 sp += strlen(magic);
53
54         /* It stops at the first '$', max 8 chars */
55         for (ep = sp; *ep && *ep != '$' && ep < (sp + 8); ep++)
56                 continue;
57
58         /* get the length of the true salt */
59         sl = ep - sp;
60
61         MD5Name(MD5Init)(&ctx);
62
63         /* The password first, since that is what is most unknown */
64         MD5Name(MD5Update)(&ctx,(unsigned const char *)pw,strlen(pw));
65
66         /* Then our magic string */
67         MD5Name(MD5Update)(&ctx,(unsigned const char *)magic,strlen(magic));
68
69         /* Then the raw salt */
70         MD5Name(MD5Update)(&ctx,(unsigned const char *)sp,sl);
71
72         /* Then just as many characters of the MD5(pw,salt,pw) */
73         MD5Name(MD5Init)(&ctx1);
74         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
75         MD5Name(MD5Update)(&ctx1,(unsigned const char *)sp,sl);
76         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
77         MD5Name(MD5Final)(final,&ctx1);
78         for (pl = strlen(pw); pl > 0; pl -= 16)
79                 MD5Name(MD5Update)(&ctx,(unsigned const char *)final,pl>16 ? 16 : pl);
80
81         /* Don't leave anything around in vm they could use. */
82         memset(final, 0, sizeof final);
83
84         /* Then something really weird... */
85         for (j = 0, i = strlen(pw); i; i >>= 1)
86                 if (i & 1)
87                         MD5Name(MD5Update)(&ctx, (unsigned const char *)final+j, 1);
88                 else
89                         MD5Name(MD5Update)(&ctx, (unsigned const char *)pw+j, 1);
90
91         /* Now make the output string */
92         strcpy(passwd, magic);
93         strncat(passwd, sp, sl);
94         strcat(passwd, "$");
95
96         MD5Name(MD5Final)(final,&ctx);
97
98         /*
99          * and now, just to make sure things don't run too fast
100          * On a 60 Mhz Pentium this takes 34 msec, so you would
101          * need 30 seconds to build a 1000 entry dictionary...
102          */
103         for (i = 0; i < 1000; i++) {
104                 MD5Name(MD5Init)(&ctx1);
105                 if (i & 1)
106                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
107                 else
108                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)final,16);
109
110                 if (i % 3)
111                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)sp,sl);
112
113                 if (i % 7)
114                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
115
116                 if (i & 1)
117                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)final,16);
118                 else
119                         MD5Name(MD5Update)(&ctx1,(unsigned const char *)pw,strlen(pw));
120                 MD5Name(MD5Final)(final,&ctx1);
121         }
122
123         p = passwd + strlen(passwd);
124
125         l = (final[0] << 16) | (final[6] << 8) | final[12];
126         to64(p, l, 4);
127         p += 4;
128         l = (final[1] << 16) | (final[7] << 8) | final[13];
129         to64(p, l, 4);
130         p += 4;
131         l = (final[2] << 16) | (final[8] << 8) | final[14];
132         to64(p, l, 4);
133         p += 4;
134         l = (final[3] << 16) | (final[9] << 8) | final[15];
135         to64(p, l, 4);
136         p += 4;
137         l = (final[4] << 16) | (final[10] << 8) | final[5];
138         to64(p, l, 4);
139         p += 4;
140         l = final[11];
141         to64(p, l, 2);
142         p += 2;
143         *p = '\0';
144
145         /* Don't leave anything around in vm they could use. */
146         memset(final, 0, sizeof final);
147
148         return passwd;
149 }