blob: 22ef9893356eb0bcb65917002665415a4b68b599 [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 Millerded319c2006-09-01 15:38:36 +100014#include <sys/types.h>
15
16#include <string.h>
17
Damien Millerdd1c7ba1999-11-19 15:53:20 +110018#include <openssl/md5.h>
Damien Millerdd1c7ba1999-11-19 15:53:20 +110019
Damien Millere7fb1032003-05-19 00:46:46 +100020/* 0 ... 63 => ascii - 64 */
21static unsigned char itoa64[] =
22 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Damien Millerdd1c7ba1999-11-19 15:53:20 +110023
Damien Millere7fb1032003-05-19 00:46:46 +100024static char *magic = "$1$";
25
26static char *
27to64(unsigned long v, int n)
Damien Millerdd1c7ba1999-11-19 15:53:20 +110028{
Damien Millere7fb1032003-05-19 00:46:46 +100029 static char buf[5];
30 char *s = buf;
31
32 if (n > 4)
33 return (NULL);
34
35 memset(buf, '\0', sizeof(buf));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110036 while (--n >= 0) {
37 *s++ = itoa64[v&0x3f];
38 v >>= 6;
39 }
Damien Miller787b2ec2003-11-21 23:56:47 +110040
Damien Millere7fb1032003-05-19 00:46:46 +100041 return (buf);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110042}
43
44int
45is_md5_salt(const char *salt)
46{
Damien Millere7fb1032003-05-19 00:46:46 +100047 return (strncmp(salt, magic, strlen(magic)) == 0);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110048}
49
Damien Millerdd1c7ba1999-11-19 15:53:20 +110050char *
51md5_crypt(const char *pw, const char *salt)
52{
Damien Millere7fb1032003-05-19 00:46:46 +100053 static char passwd[120], salt_copy[9], *p;
54 static const char *sp, *ep;
55 unsigned char final[16];
56 int sl, pl, i, j;
57 MD5_CTX ctx, ctx1;
Damien Millerdd1c7ba1999-11-19 15:53:20 +110058 unsigned long l;
59
60 /* Refine the Salt first */
61 sp = salt;
62
63 /* If it starts with the magic string, then skip that */
Damien Millere7fb1032003-05-19 00:46:46 +100064 if(strncmp(sp, magic, strlen(magic)) == 0)
Damien Millerdd1c7ba1999-11-19 15:53:20 +110065 sp += strlen(magic);
66
67 /* It stops at the first '$', max 8 chars */
Damien Millere7fb1032003-05-19 00:46:46 +100068 for (ep = sp; *ep != '$'; ep++) {
69 if (*ep == '\0' || ep >= (sp + 8))
70 return (NULL);
71 }
Damien Millerdd1c7ba1999-11-19 15:53:20 +110072
73 /* get the length of the true salt */
74 sl = ep - sp;
75
Damien Millere7fb1032003-05-19 00:46:46 +100076 /* Stash the salt */
77 memcpy(salt_copy, sp, sl);
78 salt_copy[sl] = '\0';
79
Damien Millerdd1c7ba1999-11-19 15:53:20 +110080 MD5_Init(&ctx);
81
82 /* The password first, since that is what is most unknown */
Damien Millere7fb1032003-05-19 00:46:46 +100083 MD5_Update(&ctx, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110084
85 /* Then our magic string */
Damien Millere7fb1032003-05-19 00:46:46 +100086 MD5_Update(&ctx, magic, strlen(magic));
Damien Millerdd1c7ba1999-11-19 15:53:20 +110087
88 /* Then the raw salt */
Damien Millere7fb1032003-05-19 00:46:46 +100089 MD5_Update(&ctx, sp, sl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +110090
Damien Millere7fb1032003-05-19 00:46:46 +100091 /* Then just as many characters of the MD5(pw, salt, pw) */
Damien Millerdd1c7ba1999-11-19 15:53:20 +110092 MD5_Init(&ctx1);
Damien Millere7fb1032003-05-19 00:46:46 +100093 MD5_Update(&ctx1, pw, strlen(pw));
94 MD5_Update(&ctx1, sp, sl);
95 MD5_Update(&ctx1, pw, strlen(pw));
96 MD5_Final(final, &ctx1);
97
Damien Millerdd1c7ba1999-11-19 15:53:20 +110098 for(pl = strlen(pw); pl > 0; pl -= 16)
Damien Millere7fb1032003-05-19 00:46:46 +100099 MD5_Update(&ctx, final, pl > 16 ? 16 : pl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100100
101 /* Don't leave anything around in vm they could use. */
Damien Millere7fb1032003-05-19 00:46:46 +1000102 memset(final, '\0', sizeof final);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100103
104 /* Then something really weird... */
Damien Millere7fb1032003-05-19 00:46:46 +1000105 for (j = 0, i = strlen(pw); i != 0; i >>= 1)
106 if (i & 1)
107 MD5_Update(&ctx, final + j, 1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100108 else
Damien Millere7fb1032003-05-19 00:46:46 +1000109 MD5_Update(&ctx, pw + j, 1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100110
111 /* Now make the output string */
Damien Millere7fb1032003-05-19 00:46:46 +1000112 snprintf(passwd, sizeof(passwd), "%s%s$", magic, salt_copy);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100113
Damien Millere7fb1032003-05-19 00:46:46 +1000114 MD5_Final(final, &ctx);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100115
116 /*
117 * and now, just to make sure things don't run too fast
118 * On a 60 Mhz Pentium this takes 34 msec, so you would
119 * need 30 seconds to build a 1000 entry dictionary...
120 */
Damien Millere7fb1032003-05-19 00:46:46 +1000121 for(i = 0; i < 1000; i++) {
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100122 MD5_Init(&ctx1);
Damien Millere7fb1032003-05-19 00:46:46 +1000123 if (i & 1)
124 MD5_Update(&ctx1, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100125 else
Damien Millere7fb1032003-05-19 00:46:46 +1000126 MD5_Update(&ctx1, final, 16);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100127
Damien Millere7fb1032003-05-19 00:46:46 +1000128 if (i % 3)
129 MD5_Update(&ctx1, sp, sl);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100130
Damien Millere7fb1032003-05-19 00:46:46 +1000131 if (i % 7)
132 MD5_Update(&ctx1, pw, strlen(pw));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100133
Damien Millere7fb1032003-05-19 00:46:46 +1000134 if (i & 1)
135 MD5_Update(&ctx1, final, 16);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100136 else
Damien Millere7fb1032003-05-19 00:46:46 +1000137 MD5_Update(&ctx1, pw, strlen(pw));
138
139 MD5_Final(final, &ctx1);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100140 }
141
142 p = passwd + strlen(passwd);
143
Damien Millere7fb1032003-05-19 00:46:46 +1000144 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
145 strlcat(passwd, to64(l, 4), sizeof(passwd));
146 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
147 strlcat(passwd, to64(l, 4), sizeof(passwd));
148 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
149 strlcat(passwd, to64(l, 4), sizeof(passwd));
150 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
151 strlcat(passwd, to64(l, 4), sizeof(passwd));
152 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
153 strlcat(passwd, to64(l, 4), sizeof(passwd));
154 l = final[11] ;
155 strlcat(passwd, to64(l, 2), sizeof(passwd));
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100156
157 /* Don't leave anything around in vm they could use. */
Damien Millere7fb1032003-05-19 00:46:46 +1000158 memset(final, 0, sizeof(final));
159 memset(salt_copy, 0, sizeof(salt_copy));
160 memset(&ctx, 0, sizeof(ctx));
161 memset(&ctx1, 0, sizeof(ctx1));
Darren Tucker3cb84e52003-05-30 16:58:22 +1000162 (void)to64(0, 4);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100163
Damien Millere7fb1032003-05-19 00:46:46 +1000164 return (passwd);
Damien Millerdd1c7ba1999-11-19 15:53:20 +1100165}
166
Damien Millerbeb4ba51999-12-28 15:09:35 +1100167#endif /* defined(HAVE_MD5_PASSWORDS) && !defined(HAVE_MD5_CRYPT) */