blob: 253fdfdf27656a6f2d67f58466996cd8327b1957 [file] [log] [blame]
Damien Millerdd1c7ba1999-11-19 15:53:20 +11001/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
Damien Millera8e06ce2003-11-21 23:48:55 +11004 * <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
Damien Millere7fb1032003-05-19 00:46:46 +10007 * return. Poul-Henning Kamp
Damien Millerdd1c7ba1999-11-19 15:53:20 +11008 * ----------------------------------------------------------------------------
9 */
10
Damien Millere9cf3572001-02-09 12:55:35 +110011#include "includes.h"
12
Damien Millerbeb4ba51999-12-28 15:09:35 +110013#if defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT)
Damien Millerdd1c7ba1999-11-19 15:53:20 +110014#include <openssl/md5.h>
Damien Millerdd1c7ba1999-11-19 15:53:20 +110015
Damien Millere7fb1032003-05-19 00:46:46 +100016/* 0 ... 63 => ascii - 64 */
17static unsigned char itoa64[] =
18 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Damien Millerdd1c7ba1999-11-19 15:53:20 +110019
Damien Millere7fb1032003-05-19 00:46:46 +100020static char *magic = "$1$";
21
22static char *
23to64(unsigned long v, int n)
Damien Millerdd1c7ba1999-11-19 15:53:20 +110024{
Damien Millere7fb1032003-05-19 00:46:46 +100025 static char buf[5];
26 char *s = buf;
27
28 if (n > 4)
29 return (NULL);
30
31 memset(buf, '\0', sizeof(buf));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110032 while (--n >= 0) {
33 *s++ = itoa64[v&0x3f];
34 v >>= 6;
35 }
Damien Miller787b2ec2003-11-21 23:56:47 +110036
Damien Millere7fb1032003-05-19 00:46:46 +100037 return (buf);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110038}
39
40int
41is_md5_salt(const char *salt)
42{
Damien Millere7fb1032003-05-19 00:46:46 +100043 return (strncmp(salt, magic, strlen(magic)) == 0);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110044}
45
Damien Millerdd1c7ba1999-11-19 15:53:20 +110046char *
47md5_crypt(const char *pw, const char *salt)
48{
Damien Millere7fb1032003-05-19 00:46:46 +100049 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;
Damien Millerdd1c7ba1999-11-19 15:53:20 +110054 unsigned long l;
55
56 /* Refine the Salt first */
57 sp = salt;
58
59 /* If it starts with the magic string, then skip that */
Damien Millere7fb1032003-05-19 00:46:46 +100060 if(strncmp(sp, magic, strlen(magic)) == 0)
Damien Millerdd1c7ba1999-11-19 15:53:20 +110061 sp += strlen(magic);
62
63 /* It stops at the first '$', max 8 chars */
Damien Millere7fb1032003-05-19 00:46:46 +100064 for (ep = sp; *ep != '$'; ep++) {
65 if (*ep == '\0' || ep >= (sp + 8))
66 return (NULL);
67 }
Damien Millerdd1c7ba1999-11-19 15:53:20 +110068
69 /* get the length of the true salt */
70 sl = ep - sp;
71
Damien Millere7fb1032003-05-19 00:46:46 +100072 /* Stash the salt */
73 memcpy(salt_copy, sp, sl);
74 salt_copy[sl] = '\0';
75
Damien Millerdd1c7ba1999-11-19 15:53:20 +110076 MD5_Init(&ctx);
77
78 /* The password first, since that is what is most unknown */
Damien Millere7fb1032003-05-19 00:46:46 +100079 MD5_Update(&ctx, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110080
81 /* Then our magic string */
Damien Millere7fb1032003-05-19 00:46:46 +100082 MD5_Update(&ctx, magic, strlen(magic));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110083
84 /* Then the raw salt */
Damien Millere7fb1032003-05-19 00:46:46 +100085 MD5_Update(&ctx, sp, sl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110086
Damien Millere7fb1032003-05-19 00:46:46 +100087 /* Then just as many characters of the MD5(pw, salt, pw) */
Damien Millerdd1c7ba1999-11-19 15:53:20 +110088 MD5_Init(&ctx1);
Damien Millere7fb1032003-05-19 00:46:46 +100089 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
Damien Millerdd1c7ba1999-11-19 15:53:20 +110094 for(pl = strlen(pw); pl > 0; pl -= 16)
Damien Millere7fb1032003-05-19 00:46:46 +100095 MD5_Update(&ctx, final, pl > 16 ? 16 : pl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110096
97 /* Don't leave anything around in vm they could use. */
Damien Millere7fb1032003-05-19 00:46:46 +100098 memset(final, '\0', sizeof final);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110099
100 /* Then something really weird... */
Damien Millere7fb1032003-05-19 00:46:46 +1000101 for (j = 0, i = strlen(pw); i != 0; i >>= 1)
102 if (i & 1)
103 MD5_Update(&ctx, final + j, 1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100104 else
Damien Millere7fb1032003-05-19 00:46:46 +1000105 MD5_Update(&ctx, pw + j, 1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100106
107 /* Now make the output string */
Damien Millere7fb1032003-05-19 00:46:46 +1000108 snprintf(passwd, sizeof(passwd), "%s%s$", magic, salt_copy);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100109
Damien Millere7fb1032003-05-19 00:46:46 +1000110 MD5_Final(final, &ctx);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100111
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 */
Damien Millere7fb1032003-05-19 00:46:46 +1000117 for(i = 0; i < 1000; i++) {
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100118 MD5_Init(&ctx1);
Damien Millere7fb1032003-05-19 00:46:46 +1000119 if (i & 1)
120 MD5_Update(&ctx1, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100121 else
Damien Millere7fb1032003-05-19 00:46:46 +1000122 MD5_Update(&ctx1, final, 16);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100123
Damien Millere7fb1032003-05-19 00:46:46 +1000124 if (i % 3)
125 MD5_Update(&ctx1, sp, sl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100126
Damien Millere7fb1032003-05-19 00:46:46 +1000127 if (i % 7)
128 MD5_Update(&ctx1, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100129
Damien Millere7fb1032003-05-19 00:46:46 +1000130 if (i & 1)
131 MD5_Update(&ctx1, final, 16);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100132 else
Damien Millere7fb1032003-05-19 00:46:46 +1000133 MD5_Update(&ctx1, pw, strlen(pw));
134
135 MD5_Final(final, &ctx1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100136 }
137
138 p = passwd + strlen(passwd);
139
Damien Millere7fb1032003-05-19 00:46:46 +1000140 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));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100152
153 /* Don't leave anything around in vm they could use. */
Damien Millere7fb1032003-05-19 00:46:46 +1000154 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));
Darren Tucker3cb84e52003-05-30 16:58:22 +1000158 (void)to64(0, 4);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100159
Damien Millere7fb1032003-05-19 00:46:46 +1000160 return (passwd);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100161}
162
Damien Millerbeb4ba51999-12-28 15:09:35 +1100163#endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */