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