summaryrefslogtreecommitdiff
path: root/src/lib/libc/crypt/bcrypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/crypt/bcrypt.c')
-rw-r--r--src/lib/libc/crypt/bcrypt.c336
1 files changed, 336 insertions, 0 deletions
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c
new file mode 100644
index 0000000000..6e1ae04e1b
--- /dev/null
+++ b/src/lib/libc/crypt/bcrypt.c
@@ -0,0 +1,336 @@
1/* $OpenBSD: bcrypt.c,v 1.19 2004/12/22 17:33:25 otto Exp $ */
2
3/*
4 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Niels Provos.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/* This password hashing algorithm was designed by David Mazieres
34 * <dm@lcs.mit.edu> and works as follows:
35 *
36 * 1. state := InitState ()
37 * 2. state := ExpandKey (state, salt, password) 3.
38 * REPEAT rounds:
39 * state := ExpandKey (state, 0, salt)
40 * state := ExpandKey(state, 0, password)
41 * 4. ctext := "OrpheanBeholderScryDoubt"
42 * 5. REPEAT 64:
43 * ctext := Encrypt_ECB (state, ctext);
44 * 6. RETURN Concatenate (salt, ctext);
45 *
46 */
47
48#if 0
49#include <stdio.h>
50#endif
51
52#include <stdio.h>
53#include <stdlib.h>
54#include <sys/types.h>
55#include <string.h>
56#include <pwd.h>
57#include <blf.h>
58
59/* This implementation is adaptable to current computing power.
60 * You can have up to 2^31 rounds which should be enough for some
61 * time to come.
62 */
63
64#define BCRYPT_VERSION '2'
65#define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */
66#define BCRYPT_BLOCKS 6 /* Ciphertext blocks */
67#define BCRYPT_MINROUNDS 16 /* we have log2(rounds) in salt */
68
69char *bcrypt_gensalt(u_int8_t);
70
71static void encode_salt(char *, u_int8_t *, u_int16_t, u_int8_t);
72static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t);
73static void decode_base64(u_int8_t *, u_int16_t, u_int8_t *);
74
75static char encrypted[_PASSWORD_LEN];
76static char gsalt[BCRYPT_MAXSALT * 4 / 3 + 1];
77static char error[] = ":";
78
79const static u_int8_t Base64Code[] =
80"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
81
82const static u_int8_t index_64[128] = {
83 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
84 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
85 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
86 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
87 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
88 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
89 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
90 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
91 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
92 255, 255, 255, 255, 255, 255, 28, 29, 30,
93 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
94 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
95 51, 52, 53, 255, 255, 255, 255, 255
96};
97#define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
98
99static void
100decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
101{
102 u_int8_t *bp = buffer;
103 u_int8_t *p = data;
104 u_int8_t c1, c2, c3, c4;
105 while (bp < buffer + len) {
106 c1 = CHAR64(*p);
107 c2 = CHAR64(*(p + 1));
108
109 /* Invalid data */
110 if (c1 == 255 || c2 == 255)
111 break;
112
113 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
114 if (bp >= buffer + len)
115 break;
116
117 c3 = CHAR64(*(p + 2));
118 if (c3 == 255)
119 break;
120
121 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
122 if (bp >= buffer + len)
123 break;
124
125 c4 = CHAR64(*(p + 3));
126 if (c4 == 255)
127 break;
128 *bp++ = ((c3 & 0x03) << 6) | c4;
129
130 p += 4;
131 }
132}
133
134static void
135encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr)
136{
137 salt[0] = '$';
138 salt[1] = BCRYPT_VERSION;
139 salt[2] = 'a';
140 salt[3] = '$';
141
142 snprintf(salt + 4, 4, "%2.2u$", logr);
143
144 encode_base64((u_int8_t *) salt + 7, csalt, clen);
145}
146/* Generates a salt for this version of crypt.
147 Since versions may change. Keeping this here
148 seems sensible.
149 */
150
151char *
152bcrypt_gensalt(u_int8_t log_rounds)
153{
154 u_int8_t csalt[BCRYPT_MAXSALT];
155 u_int16_t i;
156 u_int32_t seed = 0;
157
158 for (i = 0; i < BCRYPT_MAXSALT; i++) {
159 if (i % 4 == 0)
160 seed = arc4random();
161 csalt[i] = seed & 0xff;
162 seed = seed >> 8;
163 }
164
165 if (log_rounds < 4)
166 log_rounds = 4;
167 else if (log_rounds > 31)
168 log_rounds = 31;
169
170 encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds);
171 return gsalt;
172}
173/* We handle $Vers$log2(NumRounds)$salt+passwd$
174 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
175
176char *
177bcrypt(const char *key, const char *salt)
178{
179 blf_ctx state;
180 u_int32_t rounds, i, k;
181 u_int16_t j;
182 u_int8_t key_len, salt_len, logr, minor;
183 u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt";
184 u_int8_t csalt[BCRYPT_MAXSALT];
185 u_int32_t cdata[BCRYPT_BLOCKS];
186
187 /* Discard "$" identifier */
188 salt++;
189
190 if (*salt > BCRYPT_VERSION) {
191 /* How do I handle errors ? Return ':' */
192 return error;
193 }
194
195 /* Check for minor versions */
196 if (salt[1] != '$') {
197 switch (salt[1]) {
198 case 'a':
199 /* 'ab' should not yield the same as 'abab' */
200 minor = salt[1];
201 salt++;
202 break;
203 default:
204 return error;
205 }
206 } else
207 minor = 0;
208
209 /* Discard version + "$" identifier */
210 salt += 2;
211
212 if (salt[2] != '$')
213 /* Out of sync with passwd entry */
214 return error;
215
216 /* Computer power doesn't increase linear, 2^x should be fine */
217 logr = atoi(salt);
218 if (logr > 31)
219 return error;
220 if ((rounds = (u_int32_t) 1 << logr) < BCRYPT_MINROUNDS)
221 return error;
222
223 /* Discard num rounds + "$" identifier */
224 salt += 3;
225
226 if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT)
227 return error;
228
229 /* We dont want the base64 salt but the raw data */
230 decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
231 salt_len = BCRYPT_MAXSALT;
232 key_len = strlen(key) + (minor >= 'a' ? 1 : 0);
233
234 /* Setting up S-Boxes and Subkeys */
235 Blowfish_initstate(&state);
236 Blowfish_expandstate(&state, csalt, salt_len,
237 (u_int8_t *) key, key_len);
238 for (k = 0; k < rounds; k++) {
239 Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
240 Blowfish_expand0state(&state, csalt, salt_len);
241 }
242
243 /* This can be precomputed later */
244 j = 0;
245 for (i = 0; i < BCRYPT_BLOCKS; i++)
246 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
247
248 /* Now do the encryption */
249 for (k = 0; k < 64; k++)
250 blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
251
252 for (i = 0; i < BCRYPT_BLOCKS; i++) {
253 ciphertext[4 * i + 3] = cdata[i] & 0xff;
254 cdata[i] = cdata[i] >> 8;
255 ciphertext[4 * i + 2] = cdata[i] & 0xff;
256 cdata[i] = cdata[i] >> 8;
257 ciphertext[4 * i + 1] = cdata[i] & 0xff;
258 cdata[i] = cdata[i] >> 8;
259 ciphertext[4 * i + 0] = cdata[i] & 0xff;
260 }
261
262
263 i = 0;
264 encrypted[i++] = '$';
265 encrypted[i++] = BCRYPT_VERSION;
266 if (minor)
267 encrypted[i++] = minor;
268 encrypted[i++] = '$';
269
270 snprintf(encrypted + i, 4, "%2.2u$", logr);
271
272 encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT);
273 encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
274 4 * BCRYPT_BLOCKS - 1);
275 return encrypted;
276}
277
278static void
279encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
280{
281 u_int8_t *bp = buffer;
282 u_int8_t *p = data;
283 u_int8_t c1, c2;
284 while (p < data + len) {
285 c1 = *p++;
286 *bp++ = Base64Code[(c1 >> 2)];
287 c1 = (c1 & 0x03) << 4;
288 if (p >= data + len) {
289 *bp++ = Base64Code[c1];
290 break;
291 }
292 c2 = *p++;
293 c1 |= (c2 >> 4) & 0x0f;
294 *bp++ = Base64Code[c1];
295 c1 = (c2 & 0x0f) << 2;
296 if (p >= data + len) {
297 *bp++ = Base64Code[c1];
298 break;
299 }
300 c2 = *p++;
301 c1 |= (c2 >> 6) & 0x03;
302 *bp++ = Base64Code[c1];
303 *bp++ = Base64Code[c2 & 0x3f];
304 }
305 *bp = '\0';
306}
307#if 0
308void
309main()
310{
311 char blubber[73];
312 char salt[100];
313 char *p;
314 salt[0] = '$';
315 salt[1] = BCRYPT_VERSION;
316 salt[2] = '$';
317
318 snprintf(salt + 3, 4, "%2.2u$", 5);
319
320 printf("24 bytes of salt: ");
321 fgets(salt + 6, 94, stdin);
322 salt[99] = 0;
323 printf("72 bytes of password: ");
324 fpurge(stdin);
325 fgets(blubber, 73, stdin);
326 blubber[72] = 0;
327
328 p = crypt(blubber, salt);
329 printf("Passwd entry: %s\n\n", p);
330
331 p = bcrypt_gensalt(5);
332 printf("Generated salt: %s\n", p);
333 p = crypt(blubber, p);
334 printf("Passwd entry: %s\n", p);
335}
336#endif