Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcrypt / crypt-md5.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.5.2.1 2001/05/24 12:20:02 markm Exp $
10  *
11  */
12
13 #if defined(LIBC_SCCS) && !defined(lint)
14 static const char rcsid[] = \
15 "$FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.5.2.1 2001/05/24 12:20:02 markm Exp $";
16 #endif /* LIBC_SCCS and not lint */
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <md5.h>
22 #include <err.h>
23 #include "crypt.h"
24
25 /*
26  * UNIX password
27  */
28
29 char *
30 crypt_md5(pw, salt)
31         const char *pw;
32         const char *salt;
33 {
34         static char     *magic = "$1$"; /*
35                                          * This string is magic for
36                                          * this algorithm.  Having
37                                          * it this way, we can get
38                                          * get better later on
39                                          */
40         static char     passwd[120], *p;
41         static const char *sp,*ep;
42         unsigned char   final[MD5_SIZE];
43         int sl,pl,i;
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         MD5Init(&ctx);
62
63         /* The password first, since that is what is most unknown */
64         MD5Update(&ctx,pw,strlen(pw));
65
66         /* Then our magic string */
67         MD5Update(&ctx,magic,strlen(magic));
68
69         /* Then the raw salt */
70         MD5Update(&ctx,sp,sl);
71
72         /* Then just as many characters of the MD5(pw,salt,pw) */
73         MD5Init(&ctx1);
74         MD5Update(&ctx1,pw,strlen(pw));
75         MD5Update(&ctx1,sp,sl);
76         MD5Update(&ctx1,pw,strlen(pw));
77         MD5Final(final,&ctx1);
78         for(pl = strlen(pw); pl > 0; pl -= MD5_SIZE)
79                 MD5Update(&ctx,final,pl>MD5_SIZE ? MD5_SIZE : 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 (i = strlen(pw); i ; i >>= 1)
86                 if(i&1)
87                     MD5Update(&ctx, final, 1);
88                 else
89                     MD5Update(&ctx, pw, 1);
90
91         /* Now make the output string */
92         strcpy(passwd,magic);
93         strncat(passwd,sp,sl);
94         strcat(passwd,"$");
95
96         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                 MD5Init(&ctx1);
105                 if(i & 1)
106                         MD5Update(&ctx1,pw,strlen(pw));
107                 else
108                         MD5Update(&ctx1,final,MD5_SIZE);
109
110                 if(i % 3)
111                         MD5Update(&ctx1,sp,sl);
112
113                 if(i % 7)
114                         MD5Update(&ctx1,pw,strlen(pw));
115
116                 if(i & 1)
117                         MD5Update(&ctx1,final,MD5_SIZE);
118                 else
119                         MD5Update(&ctx1,pw,strlen(pw));
120                 MD5Final(final,&ctx1);
121         }
122
123         p = passwd + strlen(passwd);
124
125         l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
126         _crypt_to64(p,l,4); p += 4;
127         l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
128         _crypt_to64(p,l,4); p += 4;
129         l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
130         _crypt_to64(p,l,4); p += 4;
131         l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
132         _crypt_to64(p,l,4); p += 4;
133         l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
134         _crypt_to64(p,l,4); p += 4;
135         l =                    final[11]                ;
136         _crypt_to64(p,l,2); p += 2;
137         *p = '\0';
138
139         /* Don't leave anything around in vm they could use. */
140         memset(final,0,sizeof final);
141
142         return passwd;
143 }
144