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