xref: /openbsd-src/lib/libc/crypt/bcrypt.c (revision d5b08a547186985b60d5227d433106ae9cadafcb)
1 /* $OpenBSD: bcrypt.c,v 1.2 1997/02/14 18:40:14 provos 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 Theo de Raadt.
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 := "OpenBSDbcrypthashfunc"
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 <time.h>
53 #include <sys/types.h>
54 #include <string.h>
55 #include <pwd.h>
56 #include <blf.h>
57 
58 /* This implementation is adaptable to current computing power.
59  * You can have up to 2^31 rounds which should be enough for some
60  * time to come.
61  */
62 
63 #define BCRYPT_VERSION '2'
64 #define BCRYPT_MAXSALT 16	/* Precomputation is just so nice */
65 #define BCRYPT_BLOCKS 6		/* Ciphertext blocks */
66 #define BCRYPT_MINROUNDS 16	/* we have log2(rounds) in salt */
67 
68 char   *bcrypt_gensalt __P((u_int8_t));
69 
70 static void encode_salt __P((char *, u_int8_t *, u_int16_t, u_int8_t));
71 static void encode_base64 __P((u_int8_t *, u_int8_t *, u_int16_t));
72 static void decode_base64 __P((u_int8_t *, u_int16_t, u_int8_t *));
73 
74 static char    encrypted[_PASSWORD_LEN];
75 static char    gsalt[BCRYPT_MAXSALT * 4 / 3 + 1];
76 static char    error[] = ":";
77 
78 const static u_int8_t Base64Code[] =
79 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
80 
81 const static u_int8_t index_64[128] =
82 {
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 
99 static void
100 decode_base64(buffer, len, data)
101 	u_int8_t *buffer;
102 	u_int16_t len;
103 	u_int8_t *data;
104 {
105 	u_int8_t *bp = buffer;
106 	u_int8_t *p = data;
107 	u_int8_t c1, c2, c3, c4;
108 	while (bp < buffer + len) {
109 		c1 = CHAR64(*p);
110 		c2 = CHAR64(*(p + 1));
111 
112 		/* Invalid data */
113 		if (c1 == 255 || c2 == 255)
114 			break;
115 
116 		*bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
117 		if (bp >= buffer + len)
118 			break;
119 
120 		c3 = CHAR64(*(p + 2));
121 		if (c3 == 255)
122 			break;
123 
124 		*bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
125 		if (bp >= buffer + len)
126 			break;
127 
128 		c4 = CHAR64(*(p + 3));
129 		if (c4 == 255)
130 			break;
131 		*bp++ = ((c3 & 0x03) << 6) | c4;
132 
133 		p += 4;
134 	}
135 }
136 
137 static void
138 encode_salt(salt, csalt, clen, logr)
139 	char   *salt;
140 	u_int8_t *csalt;
141 	u_int16_t clen;
142 	u_int8_t logr;
143 {
144 	salt[0] = '$';
145 	salt[1] = BCRYPT_VERSION;
146 	salt[2] = '$';
147 
148 	snprintf(salt + 3, 4, "%2.2u$", logr);
149 
150 	encode_base64((u_int8_t *) salt + 6, csalt, clen);
151 }
152 /* Generates a salt for this version of crypt.
153    Since versions may change. Keeping this here
154    seems sensible.
155  */
156 
157 char   *
158 bcrypt_gensalt(log_rounds)
159 	u_int8_t log_rounds;
160 {
161 	u_int8_t csalt[BCRYPT_MAXSALT];
162 	u_int16_t i;
163 	u_int32_t seed = 0;
164 	(void) srandom((int) time((time_t *) NULL));
165 	for (i = 0; i < BCRYPT_MAXSALT; i++) {
166 		if (i % 4 == 0)
167 			seed = random();
168 		csalt[i] = seed & 0xff;
169 		seed = seed >> 8;
170 	}
171 
172 	if (log_rounds < 4)
173 		log_rounds = 4;
174 
175 	encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds);
176 	return gsalt;
177 }
178 /* We handle $Vers$log2(NumRounds)$salt+passwd$
179    i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
180 
181 char   *
182 bcrypt(key, salt)
183 	char   *key;
184 	char   *salt;
185 {
186 	blf_ctx state;
187 	u_int32_t rounds, i, k;
188 	u_int16_t j;
189 	u_int8_t key_len, salt_len, logr;
190 	u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OpenBSDbcrypthashfunc";
191 	u_int8_t csalt[BCRYPT_MAXSALT];
192 	u_int32_t cdata[BCRYPT_BLOCKS];
193 	/* Discard "$" identifier */
194 	salt++;
195 
196 	if (*salt > BCRYPT_VERSION) {
197 		/* How do I handle errors ? Return ':' */
198 		return error;
199 	}
200 	/* Discard version + "$" identifier */
201 	salt += 2;
202 
203 	if (*(salt + 2) != '$')
204 		/* Out of sync with passwd entry */
205 		return error;
206 
207 	/* Computer power doesnt increase linear, 2^x should be fine */
208 	if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS)
209 		return error;
210 
211 	/* Discard num rounds + "$" identifier */
212 	salt += 3;
213 
214 	/* We dont want the base64 salt but the raw data */
215 	decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
216 	salt_len = BCRYPT_MAXSALT;
217 	key_len = strlen(key);
218 
219 	/* Setting up S-Boxes and Subkeys */
220 	Blowfish_initstate(&state);
221 	Blowfish_expandstate(&state, csalt, salt_len,
222 	    (u_int8_t *) key, key_len);
223 	for (k = 0; k < rounds; k++) {
224 		Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
225 		Blowfish_expand0state(&state, csalt, salt_len);
226 	}
227 
228 	/* This can be precomputed later */
229 	j = 0;
230 	for (i = 0; i < BCRYPT_BLOCKS; i++)
231 		cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
232 
233 	/* Now do the encryption */
234 	for (k = 0; k < 64; k++)
235 		blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
236 
237 	for (i = 0; i < BCRYPT_BLOCKS; i++) {
238 		ciphertext[4 * i + 3] = cdata[i] & 0xff;
239 		cdata[i] = cdata[i] >> 8;
240 		ciphertext[4 * i + 2] = cdata[i] & 0xff;
241 		cdata[i] = cdata[i] >> 8;
242 		ciphertext[4 * i + 1] = cdata[i] & 0xff;
243 		cdata[i] = cdata[i] >> 8;
244 		ciphertext[4 * i + 0] = cdata[i] & 0xff;
245 	}
246 
247 
248 	encrypted[0] = '$';
249 	encrypted[1] = BCRYPT_VERSION;
250 	encrypted[2] = '$';
251 
252 	snprintf(encrypted + 3, 4, "%2.2u$", logr);
253 
254 	encode_base64((u_int8_t *) encrypted + 6, csalt, BCRYPT_MAXSALT);
255 	encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
256 	    4 * BCRYPT_BLOCKS);
257 	return encrypted;
258 }
259 
260 static void
261 encode_base64(buffer, data, len)
262 	u_int8_t *buffer;
263 	u_int8_t *data;
264 	u_int16_t len;
265 {
266 	u_int8_t *bp = buffer;
267 	u_int8_t *p = data;
268 	u_int8_t c1, c2;
269 	while (p < data + len) {
270 		c1 = *p++;
271 		*bp++ = Base64Code[(c1 >> 2)];
272 		c1 = (c1 & 0x03) << 4;
273 		c2 = *p++;
274 		if (p >= data + len) {
275 			*bp++ = Base64Code[c1];
276 			break;
277 		}
278 		c1 |= (c2 >> 4) & 0x0f;
279 		*bp++ = Base64Code[c1];
280 		c1 = (c2 & 0x0f) << 2;
281 		c2 = *p++;
282 		if (p >= data + len) {
283 			*bp++ = Base64Code[c1];
284 			break;
285 		}
286 		c1 |= (c2 >> 6) & 0x03;
287 		*bp++ = Base64Code[c1];
288 		*bp++ = Base64Code[c2 & 0x3f];
289 	}
290 	*bp = '\0';
291 }
292 #ifdef TEST
293 void
294 main()
295 {
296 	char    blubber[73];
297 	char    salt[100];
298 	char   *p;
299 	salt[0] = '$';
300 	salt[1] = BCRYPT_VERSION;
301 	salt[2] = '$';
302 
303 	snprintf(salt + 3, 4, "%2.2u$", 5);
304 
305 	printf("24 bytes of salt: ");
306 	fgets(salt + 6, 94, stdin);
307 	salt[99] = 0;
308 	printf("72 bytes of password: ");
309 	fpurge(stdin);
310 	fgets(blubber, 73, stdin);
311 	blubber[72] = 0;
312 
313 	p = crypt(blubber, salt);
314 	printf("Passwd entry: %s\n\n", p);
315 
316 	p = bcrypt_gensalt(5);
317 	printf("Generated salt: %s\n", p);
318 	p = crypt(blubber, p);
319 	printf("Passwd entry: %s\n", p);
320 }
321 #endif
322