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