]> andersk Git - openssh.git/blame - md5crypt.c
- jmc@cvs.openbsd.org 2006/07/18 07:56:28
[openssh.git] / md5crypt.c
CommitLineData
caf3bc51 1/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
aff51935 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
1bbcb4cd 7 * return. Poul-Henning Kamp
caf3bc51 8 * ----------------------------------------------------------------------------
9 */
10
0b202697 11#include "includes.h"
12
d94aa2ae 13#if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
caf3bc51 14#include <openssl/md5.h>
caf3bc51 15
1bbcb4cd 16/* 0 ... 63 => ascii - 64 */
17static unsigned char itoa64[] =
18 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
caf3bc51 19
1bbcb4cd 20static char *magic = "$1$";
caf3bc51 21
1bbcb4cd 22static char *
23to64(unsigned long v, int n)
caf3bc51 24{
1bbcb4cd 25 static char buf[5];
26 char *s = buf;
27
28 if (n > 4)
29 return (NULL);
30
31 memset(buf, '\0', sizeof(buf));
caf3bc51 32 while (--n >= 0) {
33 *s++ = itoa64[v&0x3f];
34 v >>= 6;
35 }
b6453d99 36
1bbcb4cd 37 return (buf);
caf3bc51 38}
39
40int
41is_md5_salt(const char *salt)
42{
1bbcb4cd 43 return (strncmp(salt, magic, strlen(magic)) == 0);
caf3bc51 44}
45
caf3bc51 46char *
47md5_crypt(const char *pw, const char *salt)
48{
1bbcb4cd 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;
caf3bc51 54 unsigned long l;
55
56 /* Refine the Salt first */
57 sp = salt;
58
59 /* If it starts with the magic string, then skip that */
1bbcb4cd 60 if(strncmp(sp, magic, strlen(magic)) == 0)
caf3bc51 61 sp += strlen(magic);
62
63 /* It stops at the first '$', max 8 chars */
1bbcb4cd 64 for (ep = sp; *ep != '$'; ep++) {
65 if (*ep == '\0' || ep >= (sp + 8))
66 return (NULL);
67 }
caf3bc51 68
69 /* get the length of the true salt */
70 sl = ep - sp;
71
1bbcb4cd 72 /* Stash the salt */
73 memcpy(salt_copy, sp, sl);
74 salt_copy[sl] = '\0';
75
caf3bc51 76 MD5_Init(&ctx);
77
78 /* The password first, since that is what is most unknown */
1bbcb4cd 79 MD5_Update(&ctx, pw, strlen(pw));
caf3bc51 80
81 /* Then our magic string */
1bbcb4cd 82 MD5_Update(&ctx, magic, strlen(magic));
caf3bc51 83
84 /* Then the raw salt */
1bbcb4cd 85 MD5_Update(&ctx, sp, sl);
caf3bc51 86
1bbcb4cd 87 /* Then just as many characters of the MD5(pw, salt, pw) */
caf3bc51 88 MD5_Init(&ctx1);
1bbcb4cd 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);
93
caf3bc51 94 for(pl = strlen(pw); pl > 0; pl -= 16)
1bbcb4cd 95 MD5_Update(&ctx, final, pl > 16 ? 16 : pl);
caf3bc51 96
97 /* Don't leave anything around in vm they could use. */
1bbcb4cd 98 memset(final, '\0', sizeof final);
caf3bc51 99
100 /* Then something really weird... */
1bbcb4cd 101 for (j = 0, i = strlen(pw); i != 0; i >>= 1)
102 if (i & 1)
103 MD5_Update(&ctx, final + j, 1);
caf3bc51 104 else
1bbcb4cd 105 MD5_Update(&ctx, pw + j, 1);
caf3bc51 106
107 /* Now make the output string */
1bbcb4cd 108 snprintf(passwd, sizeof(passwd), "%s%s$", magic, salt_copy);
caf3bc51 109
1bbcb4cd 110 MD5_Final(final, &ctx);
caf3bc51 111
112 /*
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...
116 */
1bbcb4cd 117 for(i = 0; i < 1000; i++) {
caf3bc51 118 MD5_Init(&ctx1);
1bbcb4cd 119 if (i & 1)
120 MD5_Update(&ctx1, pw, strlen(pw));
caf3bc51 121 else
1bbcb4cd 122 MD5_Update(&ctx1, final, 16);
caf3bc51 123
1bbcb4cd 124 if (i % 3)
125 MD5_Update(&ctx1, sp, sl);
caf3bc51 126
1bbcb4cd 127 if (i % 7)
128 MD5_Update(&ctx1, pw, strlen(pw));
caf3bc51 129
1bbcb4cd 130 if (i & 1)
131 MD5_Update(&ctx1, final, 16);
caf3bc51 132 else
1bbcb4cd 133 MD5_Update(&ctx1, pw, strlen(pw));
134
135 MD5_Final(final, &ctx1);
caf3bc51 136 }
137
138 p = passwd + strlen(passwd);
139
1bbcb4cd 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));
caf3bc51 152
153 /* Don't leave anything around in vm they could use. */
1bbcb4cd 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));
4f178be8 158 (void)to64(0, 4);
caf3bc51 159
1bbcb4cd 160 return (passwd);
caf3bc51 161}
162
d94aa2ae 163#endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */
This page took 0.244497 seconds and 5 git commands to generate.