Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * $DragonFly: src/lib/libcrypt/crypt-md5.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
11  *
12  * $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.5.2.1 2001/05/24 12:20:02 markm Exp $
13  */
14
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <md5.h>
19 #include <err.h>
20 #include "crypt.h"
21
22 /*
23  * UNIX password
24  */
25
26 char *
27 crypt_md5(pw, salt)
28         const char *pw;
29         const char *salt;
30 {
31         static char     *magic = "$1$"; /*
32                                          * This string is magic for
33                                          * this algorithm.  Having
34                                          * it this way, we can get
35                                          * get better later on
36                                          */
37         static char     passwd[120], *p;
38         static const char *sp,*ep;
39         unsigned char   final[MD5_SIZE];
40         int sl,pl,i;
41         MD5_CTX ctx,ctx1;
42         unsigned long l;
43
44         /* Refine the Salt first */
45         sp = salt;
46
47         /* If it starts with the magic string, then skip that */
48         if(!strncmp(sp,magic,strlen(magic)))
49                 sp += strlen(magic);
50
51         /* It stops at the first '$', max 8 chars */
52         for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
53                 continue;
54
55         /* get the length of the true salt */
56         sl = ep - sp;
57
58         MD5Init(&ctx);
59
60         /* The password first, since that is what is most unknown */
61         MD5Update(&ctx,pw,strlen(pw));
62
63         /* Then our magic string */
64         MD5Update(&ctx,magic,strlen(magic));
65
66         /* Then the raw salt */
67         MD5Update(&ctx,sp,sl);
68
69         /* Then just as many characters of the MD5(pw,salt,pw) */
70         MD5Init(&ctx1);
71         MD5Update(&ctx1,pw,strlen(pw));
72         MD5Update(&ctx1,sp,sl);
73         MD5Update(&ctx1,pw,strlen(pw));
74         MD5Final(final,&ctx1);
75         for(pl = strlen(pw); pl > 0; pl -= MD5_SIZE)
76                 MD5Update(&ctx,final,pl>MD5_SIZE ? MD5_SIZE : pl);
77
78         /* Don't leave anything around in vm they could use. */
79         memset(final,0,sizeof final);
80
81         /* Then something really weird... */
82         for (i = strlen(pw); i ; i >>= 1)
83                 if(i&1)
84                     MD5Update(&ctx, final, 1);
85                 else
86                     MD5Update(&ctx, pw, 1);
87
88         /* Now make the output string */
89         strcpy(passwd,magic);
90         strncat(passwd,sp,sl);
91         strcat(passwd,"$");
92
93         MD5Final(final,&ctx);
94
95         /*
96          * and now, just to make sure things don't run too fast
97          * On a 60 Mhz Pentium this takes 34 msec, so you would
98          * need 30 seconds to build a 1000 entry dictionary...
99          */
100         for(i=0;i<1000;i++) {
101                 MD5Init(&ctx1);
102                 if(i & 1)
103                         MD5Update(&ctx1,pw,strlen(pw));
104                 else
105                         MD5Update(&ctx1,final,MD5_SIZE);
106
107                 if(i % 3)
108                         MD5Update(&ctx1,sp,sl);
109
110                 if(i % 7)
111                         MD5Update(&ctx1,pw,strlen(pw));
112
113                 if(i & 1)
114                         MD5Update(&ctx1,final,MD5_SIZE);
115                 else
116                         MD5Update(&ctx1,pw,strlen(pw));
117                 MD5Final(final,&ctx1);
118         }
119
120         p = passwd + strlen(passwd);
121
122         l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
123         _crypt_to64(p,l,4); p += 4;
124         l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
125         _crypt_to64(p,l,4); p += 4;
126         l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
127         _crypt_to64(p,l,4); p += 4;
128         l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
129         _crypt_to64(p,l,4); p += 4;
130         l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
131         _crypt_to64(p,l,4); p += 4;
132         l =                    final[11]                ;
133         _crypt_to64(p,l,2); p += 2;
134         *p = '\0';
135
136         /* Don't leave anything around in vm they could use. */
137         memset(final,0,sizeof final);
138
139         return passwd;
140 }
141