- stevesk@cvs.openbsd.org 2006/07/22 19:08:54
[openssh-git.git] / md5crypt.c
blob253fdfdf27656a6f2d67f58466996cd8327b1957
1 /*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file. As long as you retain this
5 * notice you can do whatever you want with this stuff. If we meet some
6 * day, and you think this stuff is worth it, you can buy me a beer in
7 * return. Poul-Henning Kamp
8 * ----------------------------------------------------------------------------
9 */
11 #include "includes.h"
13 #if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
14 #include <openssl/md5.h>
16 /* 0 ... 63 => ascii - 64 */
17 static unsigned char itoa64[] =
18 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
20 static char *magic = "$1$";
22 static char *
23 to64(unsigned long v, int n)
25 static char buf[5];
26 char *s = buf;
28 if (n > 4)
29 return (NULL);
31 memset(buf, '\0', sizeof(buf));
32 while (--n >= 0) {
33 *s++ = itoa64[v&0x3f];
34 v >>= 6;
37 return (buf);
40 int
41 is_md5_salt(const char *salt)
43 return (strncmp(salt, magic, strlen(magic)) == 0);
46 char *
47 md5_crypt(const char *pw, const char *salt)
49 static char passwd[120], salt_copy[9], *p;
50 static const char *sp, *ep;
51 unsigned char final[16];
52 int sl, pl, i, j;
53 MD5_CTX ctx, ctx1;
54 unsigned long l;
56 /* Refine the Salt first */
57 sp = salt;
59 /* If it starts with the magic string, then skip that */
60 if(strncmp(sp, magic, strlen(magic)) == 0)
61 sp += strlen(magic);
63 /* It stops at the first '$', max 8 chars */
64 for (ep = sp; *ep != '$'; ep++) {
65 if (*ep == '\0' || ep >= (sp + 8))
66 return (NULL);
69 /* get the length of the true salt */
70 sl = ep - sp;
72 /* Stash the salt */
73 memcpy(salt_copy, sp, sl);
74 salt_copy[sl] = '\0';
76 MD5_Init(&ctx);
78 /* The password first, since that is what is most unknown */
79 MD5_Update(&ctx, pw, strlen(pw));
81 /* Then our magic string */
82 MD5_Update(&ctx, magic, strlen(magic));
84 /* Then the raw salt */
85 MD5_Update(&ctx, sp, sl);
87 /* Then just as many characters of the MD5(pw, salt, pw) */
88 MD5_Init(&ctx1);
89 MD5_Update(&ctx1, pw, strlen(pw));
90 MD5_Update(&ctx1, sp, sl);
91 MD5_Update(&ctx1, pw, strlen(pw));
92 MD5_Final(final, &ctx1);
94 for(pl = strlen(pw); pl > 0; pl -= 16)
95 MD5_Update(&ctx, final, pl > 16 ? 16 : pl);
97 /* Don't leave anything around in vm they could use. */
98 memset(final, '\0', sizeof final);
100 /* Then something really weird... */
101 for (j = 0, i = strlen(pw); i != 0; i >>= 1)
102 if (i & 1)
103 MD5_Update(&ctx, final + j, 1);
104 else
105 MD5_Update(&ctx, pw + j, 1);
107 /* Now make the output string */
108 snprintf(passwd, sizeof(passwd), "%s%s$", magic, salt_copy);
110 MD5_Final(final, &ctx);
113 * and now, just to make sure things don't run too fast
114 * On a 60 Mhz Pentium this takes 34 msec, so you would
115 * need 30 seconds to build a 1000 entry dictionary...
117 for(i = 0; i < 1000; i++) {
118 MD5_Init(&ctx1);
119 if (i & 1)
120 MD5_Update(&ctx1, pw, strlen(pw));
121 else
122 MD5_Update(&ctx1, final, 16);
124 if (i % 3)
125 MD5_Update(&ctx1, sp, sl);
127 if (i % 7)
128 MD5_Update(&ctx1, pw, strlen(pw));
130 if (i & 1)
131 MD5_Update(&ctx1, final, 16);
132 else
133 MD5_Update(&ctx1, pw, strlen(pw));
135 MD5_Final(final, &ctx1);
138 p = passwd + strlen(passwd);
140 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
141 strlcat(passwd, to64(l, 4), sizeof(passwd));
142 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
143 strlcat(passwd, to64(l, 4), sizeof(passwd));
144 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
145 strlcat(passwd, to64(l, 4), sizeof(passwd));
146 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
147 strlcat(passwd, to64(l, 4), sizeof(passwd));
148 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
149 strlcat(passwd, to64(l, 4), sizeof(passwd));
150 l = final[11] ;
151 strlcat(passwd, to64(l, 2), sizeof(passwd));
153 /* Don't leave anything around in vm they could use. */
154 memset(final, 0, sizeof(final));
155 memset(salt_copy, 0, sizeof(salt_copy));
156 memset(&ctx, 0, sizeof(ctx));
157 memset(&ctx1, 0, sizeof(ctx1));
158 (void)to64(0, 4);
160 return (passwd);
163 #endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */