blob: e14d53ac1f20acf21bbbeefcafbd8327d61dd3ae [file] [log] [blame]
Damien Millerdd1c7ba1999-11-19 15:53:20 +11001/*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
Damien Millere7fb1032003-05-19 00:46:46 +10004 * <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
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
Darren Tucker3cb84e52003-05-30 16:58:22 +100016RCSID("$Id: md5crypt.c,v 1.7 2003/05/30 06:58:23 dtucker Exp $");
Damien Millerdd1c7ba1999-11-19 15:53:20 +110017
Damien Millere7fb1032003-05-19 00:46:46 +100018/* 0 ... 63 => ascii - 64 */
19static unsigned char itoa64[] =
20 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Damien Millerdd1c7ba1999-11-19 15:53:20 +110021
Damien Millere7fb1032003-05-19 00:46:46 +100022static char *magic = "$1$";
23
24static char *
25to64(unsigned long v, int n)
Damien Millerdd1c7ba1999-11-19 15:53:20 +110026{
Damien Millere7fb1032003-05-19 00:46:46 +100027 static char buf[5];
28 char *s = buf;
29
30 if (n > 4)
31 return (NULL);
32
33 memset(buf, '\0', sizeof(buf));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110034 while (--n >= 0) {
35 *s++ = itoa64[v&0x3f];
36 v >>= 6;
37 }
Damien Millere7fb1032003-05-19 00:46:46 +100038
39 return (buf);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110040}
41
42int
43is_md5_salt(const char *salt)
44{
Damien Millere7fb1032003-05-19 00:46:46 +100045 return (strncmp(salt, magic, strlen(magic)) == 0);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110046}
47
Damien Millerdd1c7ba1999-11-19 15:53:20 +110048char *
49md5_crypt(const char *pw, const char *salt)
50{
Damien Millere7fb1032003-05-19 00:46:46 +100051 static char passwd[120], salt_copy[9], *p;
52 static const char *sp, *ep;
53 unsigned char final[16];
54 int sl, pl, i, j;
55 MD5_CTX ctx, ctx1;
Damien Millerdd1c7ba1999-11-19 15:53:20 +110056 unsigned long l;
57
58 /* Refine the Salt first */
59 sp = salt;
60
61 /* If it starts with the magic string, then skip that */
Damien Millere7fb1032003-05-19 00:46:46 +100062 if(strncmp(sp, magic, strlen(magic)) == 0)
Damien Millerdd1c7ba1999-11-19 15:53:20 +110063 sp += strlen(magic);
64
65 /* It stops at the first '$', max 8 chars */
Damien Millere7fb1032003-05-19 00:46:46 +100066 for (ep = sp; *ep != '$'; ep++) {
67 if (*ep == '\0' || ep >= (sp + 8))
68 return (NULL);
69 }
Damien Millerdd1c7ba1999-11-19 15:53:20 +110070
71 /* get the length of the true salt */
72 sl = ep - sp;
73
Damien Millere7fb1032003-05-19 00:46:46 +100074 /* Stash the salt */
75 memcpy(salt_copy, sp, sl);
76 salt_copy[sl] = '\0';
77
Damien Millerdd1c7ba1999-11-19 15:53:20 +110078 MD5_Init(&ctx);
79
80 /* The password first, since that is what is most unknown */
Damien Millere7fb1032003-05-19 00:46:46 +100081 MD5_Update(&ctx, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110082
83 /* Then our magic string */
Damien Millere7fb1032003-05-19 00:46:46 +100084 MD5_Update(&ctx, magic, strlen(magic));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110085
86 /* Then the raw salt */
Damien Millere7fb1032003-05-19 00:46:46 +100087 MD5_Update(&ctx, sp, sl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110088
Damien Millere7fb1032003-05-19 00:46:46 +100089 /* Then just as many characters of the MD5(pw, salt, pw) */
Damien Millerdd1c7ba1999-11-19 15:53:20 +110090 MD5_Init(&ctx1);
Damien Millere7fb1032003-05-19 00:46:46 +100091 MD5_Update(&ctx1, pw, strlen(pw));
92 MD5_Update(&ctx1, sp, sl);
93 MD5_Update(&ctx1, pw, strlen(pw));
94 MD5_Final(final, &ctx1);
95
Damien Millerdd1c7ba1999-11-19 15:53:20 +110096 for(pl = strlen(pw); pl > 0; pl -= 16)
Damien Millere7fb1032003-05-19 00:46:46 +100097 MD5_Update(&ctx, final, pl > 16 ? 16 : pl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110098
99 /* Don't leave anything around in vm they could use. */
Damien Millere7fb1032003-05-19 00:46:46 +1000100 memset(final, '\0', sizeof final);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100101
102 /* Then something really weird... */
Damien Millere7fb1032003-05-19 00:46:46 +1000103 for (j = 0, i = strlen(pw); i != 0; i >>= 1)
104 if (i & 1)
105 MD5_Update(&ctx, final + j, 1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100106 else
Damien Millere7fb1032003-05-19 00:46:46 +1000107 MD5_Update(&ctx, pw + j, 1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100108
109 /* Now make the output string */
Damien Millere7fb1032003-05-19 00:46:46 +1000110 snprintf(passwd, sizeof(passwd), "%s%s$", magic, salt_copy);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100111
Damien Millere7fb1032003-05-19 00:46:46 +1000112 MD5_Final(final, &ctx);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100113
114 /*
115 * and now, just to make sure things don't run too fast
116 * On a 60 Mhz Pentium this takes 34 msec, so you would
117 * need 30 seconds to build a 1000 entry dictionary...
118 */
Damien Millere7fb1032003-05-19 00:46:46 +1000119 for(i = 0; i < 1000; i++) {
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100120 MD5_Init(&ctx1);
Damien Millere7fb1032003-05-19 00:46:46 +1000121 if (i & 1)
122 MD5_Update(&ctx1, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100123 else
Damien Millere7fb1032003-05-19 00:46:46 +1000124 MD5_Update(&ctx1, final, 16);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100125
Damien Millere7fb1032003-05-19 00:46:46 +1000126 if (i % 3)
127 MD5_Update(&ctx1, sp, sl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100128
Damien Millere7fb1032003-05-19 00:46:46 +1000129 if (i % 7)
130 MD5_Update(&ctx1, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100131
Damien Millere7fb1032003-05-19 00:46:46 +1000132 if (i & 1)
133 MD5_Update(&ctx1, final, 16);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100134 else
Damien Millere7fb1032003-05-19 00:46:46 +1000135 MD5_Update(&ctx1, pw, strlen(pw));
136
137 MD5_Final(final, &ctx1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100138 }
139
140 p = passwd + strlen(passwd);
141
Damien Millere7fb1032003-05-19 00:46:46 +1000142 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
143 strlcat(passwd, to64(l, 4), sizeof(passwd));
144 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
145 strlcat(passwd, to64(l, 4), sizeof(passwd));
146 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
147 strlcat(passwd, to64(l, 4), sizeof(passwd));
148 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
149 strlcat(passwd, to64(l, 4), sizeof(passwd));
150 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
151 strlcat(passwd, to64(l, 4), sizeof(passwd));
152 l = final[11] ;
153 strlcat(passwd, to64(l, 2), sizeof(passwd));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100154
155 /* Don't leave anything around in vm they could use. */
Damien Millere7fb1032003-05-19 00:46:46 +1000156 memset(final, 0, sizeof(final));
157 memset(salt_copy, 0, sizeof(salt_copy));
158 memset(&ctx, 0, sizeof(ctx));
159 memset(&ctx1, 0, sizeof(ctx1));
Darren Tucker3cb84e52003-05-30 16:58:22 +1000160 (void)to64(0, 4);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100161
Damien Millere7fb1032003-05-19 00:46:46 +1000162 return (passwd);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100163}
164
Damien Millerbeb4ba51999-12-28 15:09:35 +1100165#endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */