md5crypt.c revision 1.4.2.2 1 1.4.2.2 he /* $NetBSD: md5crypt.c,v 1.4.2.2 2002/01/22 19:31:59 he Exp $ */
2 1.4.2.2 he
3 1.4.2.2 he /*
4 1.4.2.2 he * ----------------------------------------------------------------------------
5 1.4.2.2 he * "THE BEER-WARE LICENSE" (Revision 42):
6 1.4.2.2 he * <phk (at) login.dknet.dk> wrote this file. As long as you retain this notice you
7 1.4.2.2 he * can do whatever you want with this stuff. If we meet some day, and you think
8 1.4.2.2 he * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 1.4.2.2 he * ----------------------------------------------------------------------------
10 1.4.2.2 he *
11 1.4.2.2 he * from FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp
12 1.4.2.2 he * via OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp
13 1.4.2.2 he *
14 1.4.2.2 he */
15 1.4.2.2 he
16 1.4.2.2 he #include <sys/cdefs.h>
17 1.4.2.2 he #if !defined(lint)
18 1.4.2.2 he __RCSID("$NetBSD: md5crypt.c,v 1.4.2.2 2002/01/22 19:31:59 he Exp $");
19 1.4.2.2 he #endif /* not lint */
20 1.4.2.2 he
21 1.4.2.2 he #include <unistd.h>
22 1.4.2.2 he #include <stdio.h>
23 1.4.2.2 he #include <string.h>
24 1.4.2.2 he #include <md5.h>
25 1.4.2.2 he #include <string.h>
26 1.4.2.2 he
27 1.4.2.2 he #define MD5_MAGIC "$1$"
28 1.4.2.2 he #define MD5_MAGIC_LEN 3
29 1.4.2.2 he
30 1.4.2.2 he char *__md5crypt(const char *pw, const char *salt); /* XXX */
31 1.4.2.2 he
32 1.4.2.2 he static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
33 1.4.2.2 he "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
34 1.4.2.2 he
35 1.4.2.2 he static void to64(char *, u_int32_t, int);
36 1.4.2.2 he
37 1.4.2.2 he static void
38 1.4.2.2 he to64(char *s, u_int32_t v, int n)
39 1.4.2.2 he {
40 1.4.2.2 he
41 1.4.2.2 he while (--n >= 0) {
42 1.4.2.2 he *s++ = itoa64[v & 0x3f];
43 1.4.2.2 he v >>= 6;
44 1.4.2.2 he }
45 1.4.2.2 he }
46 1.4.2.2 he
47 1.4.2.2 he /*
48 1.4.2.2 he * MD5 password encryption.
49 1.4.2.2 he */
50 1.4.2.2 he char *
51 1.4.2.2 he __md5crypt(const char *pw, const char *salt)
52 1.4.2.2 he {
53 1.4.2.2 he static char passwd[120], *p;
54 1.4.2.2 he const char *sp, *ep;
55 1.4.2.2 he unsigned char final[16];
56 1.4.2.2 he unsigned int i, sl, pwl;
57 1.4.2.2 he MD5_CTX ctx, ctx1;
58 1.4.2.2 he u_int32_t l;
59 1.4.2.2 he int pl;
60 1.4.2.2 he
61 1.4.2.2 he pwl = strlen(pw);
62 1.4.2.2 he
63 1.4.2.2 he /* Refine the salt first */
64 1.4.2.2 he sp = salt;
65 1.4.2.2 he
66 1.4.2.2 he /* If it starts with the magic string, then skip that */
67 1.4.2.2 he if (strncmp(sp, MD5_MAGIC, MD5_MAGIC_LEN) == 0)
68 1.4.2.2 he sp += MD5_MAGIC_LEN;
69 1.4.2.2 he
70 1.4.2.2 he /* It stops at the first '$', max 8 chars */
71 1.4.2.2 he for (ep = sp; *ep != '\0' && *ep != '$' && ep < (sp + 8); ep++)
72 1.4.2.2 he continue;
73 1.4.2.2 he
74 1.4.2.2 he /* get the length of the true salt */
75 1.4.2.2 he sl = ep - sp;
76 1.4.2.2 he
77 1.4.2.2 he MD5Init(&ctx);
78 1.4.2.2 he
79 1.4.2.2 he /* The password first, since that is what is most unknown */
80 1.4.2.2 he MD5Update(&ctx, (const unsigned char *)pw, pwl);
81 1.4.2.2 he
82 1.4.2.2 he /* Then our magic string */
83 1.4.2.2 he MD5Update(&ctx, (const unsigned char *)MD5_MAGIC, MD5_MAGIC_LEN);
84 1.4.2.2 he
85 1.4.2.2 he /* Then the raw salt */
86 1.4.2.2 he MD5Update(&ctx, (const unsigned char *)sp, sl);
87 1.4.2.2 he
88 1.4.2.2 he /* Then just as many characters of the MD5(pw,salt,pw) */
89 1.4.2.2 he MD5Init(&ctx1);
90 1.4.2.2 he MD5Update(&ctx1, (const unsigned char *)pw, pwl);
91 1.4.2.2 he MD5Update(&ctx1, (const unsigned char *)sp, sl);
92 1.4.2.2 he MD5Update(&ctx1, (const unsigned char *)pw, pwl);
93 1.4.2.2 he MD5Final(final, &ctx1);
94 1.4.2.2 he
95 1.4.2.2 he for (pl = pwl; pl > 0; pl -= 16)
96 1.4.2.2 he MD5Update(&ctx, final, (unsigned int)(pl > 16 ? 16 : pl));
97 1.4.2.2 he
98 1.4.2.2 he /* Don't leave anything around in vm they could use. */
99 1.4.2.2 he memset(final, 0, sizeof(final));
100 1.4.2.2 he
101 1.4.2.2 he /* Then something really weird... */
102 1.4.2.2 he for (i = pwl; i != 0; i >>= 1)
103 1.4.2.2 he if ((i & 1) != 0)
104 1.4.2.2 he MD5Update(&ctx, final, 1);
105 1.4.2.2 he else
106 1.4.2.2 he MD5Update(&ctx, (const unsigned char *)pw, 1);
107 1.4.2.2 he
108 1.4.2.2 he /* Now make the output string */
109 1.4.2.2 he memcpy(passwd, MD5_MAGIC, MD5_MAGIC_LEN);
110 1.4.2.2 he strlcpy(passwd + MD5_MAGIC_LEN, sp, sl + 1);
111 1.4.2.2 he strcat(passwd, "$");
112 1.4.2.2 he
113 1.4.2.2 he MD5Final(final, &ctx);
114 1.4.2.2 he
115 1.4.2.2 he /*
116 1.4.2.2 he * And now, just to make sure things don't run too fast. On a 60 MHz
117 1.4.2.2 he * Pentium this takes 34 msec, so you would need 30 seconds to build
118 1.4.2.2 he * a 1000 entry dictionary...
119 1.4.2.2 he */
120 1.4.2.2 he for (i = 0; i < 1000; i++) {
121 1.4.2.2 he MD5Init(&ctx1);
122 1.4.2.2 he
123 1.4.2.2 he if ((i & 1) != 0)
124 1.4.2.2 he MD5Update(&ctx1, (const unsigned char *)pw, pwl);
125 1.4.2.2 he else
126 1.4.2.2 he MD5Update(&ctx1, final, 16);
127 1.4.2.2 he
128 1.4.2.2 he if ((i % 3) != 0)
129 1.4.2.2 he MD5Update(&ctx1, (const unsigned char *)sp, sl);
130 1.4.2.2 he
131 1.4.2.2 he if ((i % 7) != 0)
132 1.4.2.2 he MD5Update(&ctx1, (const unsigned char *)pw, pwl);
133 1.4.2.2 he
134 1.4.2.2 he if ((i & 1) != 0)
135 1.4.2.2 he MD5Update(&ctx1, final, 16);
136 1.4.2.2 he else
137 1.4.2.2 he MD5Update(&ctx1, (const unsigned char *)pw, pwl);
138 1.4.2.2 he
139 1.4.2.2 he MD5Final(final, &ctx1);
140 1.4.2.2 he }
141 1.4.2.2 he
142 1.4.2.2 he p = passwd + sl + MD5_MAGIC_LEN + 1;
143 1.4.2.2 he
144 1.4.2.2 he l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
145 1.4.2.2 he l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
146 1.4.2.2 he l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
147 1.4.2.2 he l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
148 1.4.2.2 he l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
149 1.4.2.2 he l = final[11] ; to64(p,l,2); p += 2;
150 1.4.2.2 he *p = '\0';
151 1.4.2.2 he
152 1.4.2.2 he /* Don't leave anything around in vm they could use. */
153 1.4.2.2 he memset(final, 0, sizeof(final));
154 1.4.2.2 he return (passwd);
155 1.4.2.2 he }
156