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