]> mj.ucw.cz Git - eval.git/blob - mop/admin/md5crypt.c
Adapted to reflect changes in libucw.
[eval.git] / mop / admin / md5crypt.c
1 /*
2  * md5crypt based on lib/md5crypt.c from Linux shadow package.
3  * adapted by Martin Mares <mj@ucw.cz> in June 2004
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
12 #include "ucw/lib.h"
13 #include "ucw/md5.h"
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
21         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
22
23 static void
24 to64(char *s, unsigned int v, int n)
25 {
26   while (--n >= 0)
27     {
28       *s++ = itoa64[v&0x3f];
29       v >>= 6;
30     }
31 }
32
33 static char *
34 libshadow_md5_crypt(const char *pw, const char *salt)
35 {
36         static char     *magic = "$1$"; /*
37                                                  * This string is magic for
38                                                  * this algorithm.  Having
39                                                  * it this way, we can get
40                                                  * get better later on
41                                                  */
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_context 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         md5_init(&ctx);
64
65         /* The password first, since that is what is most unknown */
66         md5_update(&ctx,pw,strlen(pw));
67
68         /* Then our magic string */
69         md5_update(&ctx,magic,strlen(magic));
70
71         /* Then the raw salt */
72         md5_update(&ctx,sp,sl);
73
74         /* Then just as many characters of the MD5(pw,salt,pw) */
75         md5_init(&ctx1);
76         md5_update(&ctx1,pw,strlen(pw));
77         md5_update(&ctx1,sp,sl);
78         md5_update(&ctx1,pw,strlen(pw));
79         memcpy(final, md5_final(&ctx1), 16);
80         for(pl = strlen(pw); pl > 0; pl -= 16)
81                 md5_update(&ctx,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                     md5_update(&ctx, final+j, 1);
90                 else
91                     md5_update(&ctx, pw+j, 1);
92
93         /* Now make the output string */
94         strcpy(passwd,magic);
95         strncat(passwd,sp,sl);
96         strcat(passwd,"$");
97
98         memcpy(final, md5_final(&ctx), 16);
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                 md5_init(&ctx1);
107                 if(i & 1)
108                         md5_update(&ctx1,pw,strlen(pw));
109                 else
110                         md5_update(&ctx1,final,16);
111
112                 if(i % 3)
113                         md5_update(&ctx1,sp,sl);
114
115                 if(i % 7)
116                         md5_update(&ctx1,pw,strlen(pw));
117
118                 if(i & 1)
119                         md5_update(&ctx1,final,16);
120                 else
121                         md5_update(&ctx1,pw,strlen(pw));
122                 memcpy(final, md5_final(&ctx1), 16);
123         }
124
125         p = passwd + strlen(passwd);
126
127         l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
128         l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
129         l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
130         l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
131         l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
132         l =                    final[11]                ; to64(p,l,2); p += 2;
133         *p = '\0';
134
135         /* Don't leave anything around in vm they could use. */
136         memset(final,0,sizeof final);
137
138         return passwd;
139 }
140
141 int main(void)
142 {
143   char pass[256], salt[10], rand[8];
144   int fd = open("/dev/urandom", O_RDONLY);
145   int n;
146   if (fd < 0)
147     {
148       fprintf(stderr, "unable to open /dev/urandom: %m\n");
149       return 1;
150     }
151   while (fgets(pass, sizeof(pass), stdin))
152     {
153       char *c = strchr(pass, '\n');
154       if (c)
155         *c = 0;
156       if (read(fd, rand, sizeof(rand)) != sizeof(rand))
157         {
158           fprintf(stderr, "Error reading /dev/urandom: %m\n");
159           return 1;
160         }
161       for (n=0; n<2; n++)
162         to64(salt+4*n, *(u32 *)(rand+4*n), 4);
163       salt[8] = 0;
164       printf("%s\n", libshadow_md5_crypt(pass, salt));
165     }
166   return 0;
167 }