xref: /openbsd-src/lib/libc/crypt/bcrypt.c (revision 6d84a8a155ebcb16a9347e393b3a25c1396681a9)
1 /* $OpenBSD: bcrypt.c,v 1.1 1997/02/13 16:31:16 provos Exp $ */
2 /*
3  * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
4  *
5  * Modification and redistribution in source and binary forms is
6  * permitted provided that due credit is given to the author and the
7  * OpenBSD project (for instance by leaving this copyright notice
8  * intact).
9  */
10 
11 /* This password hashing algorithm was designed by David Mazieres
12  * <dm@lcs.mit.edu> and works as follows:
13  *
14  * 1. state := InitState ()
15  * 2. state := ExpandKey (state, salt, password) 3.
16  * REPEAT rounds:
17  *	state := ExpandKey (state, 0, salt)
18  *      state := ExpandKey(state, 0, password)
19  * 4. ctext := "OpenBSDbcrypthashfunc"
20  * 5. REPEAT 64:
21  * 	ctext := Encrypt_ECB (state, ctext);
22  * 6. RETURN Concatenate (salt, ctext);
23  *
24  */
25 
26 #ifdef TEST
27 #include <stdio.h>
28 #endif
29 
30 #include <stdlib.h>
31 #include <time.h>
32 #include <sys/types.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <blf.h>
36 
37 /* This implementation is adaptable to current computing power.
38  * You can have up to 2^31 rounds which should be enough for some
39  * time to come.
40  */
41 
42 #define BCRYPT_VERSION '2'
43 #define BCRYPT_MAXSALT 16	/* Precomputation is just so nice */
44 #define BCRYPT_BLOCKS 6		/* Ciphertext blocks */
45 #define BCRYPT_MINROUNDS 16	/* we have log2(rounds) in salt */
46 
47 char   *bcrypt_gensalt __P((u_int8_t));
48 
49 static void encode_salt __P((char *, u_int8_t *, u_int16_t, u_int8_t));
50 static void encode_base64 __P((u_int8_t *, u_int8_t *, u_int16_t));
51 static void decode_base64 __P((u_int8_t *, u_int16_t, u_int8_t *));
52 
53 static char    encrypted[_PASSWORD_LEN];
54 static char    gsalt[BCRYPT_MAXSALT * 4 / 3 + 1];
55 static char    error[] = ":";
56 
57 const static u_int8_t Base64Code[] =
58 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
59 
60 const static u_int8_t index_64[128] =
61 {
62 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
63 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
64 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
65 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
66 	255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
67 	56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
68 	255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
69 	7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
70 	17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
71 	255, 255, 255, 255, 255, 255, 28, 29, 30,
72 	31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
73 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
74 	51, 52, 53, 255, 255, 255, 255, 255
75 };
76 #define CHAR64(c)  ( (c) > 127 ? 255 : index_64[(c)])
77 
78 static void
79 decode_base64(buffer, len, data)
80 	u_int8_t *buffer;
81 	u_int16_t len;
82 	u_int8_t *data;
83 {
84 	u_int8_t *bp = buffer;
85 	u_int8_t *p = data;
86 	u_int8_t c1, c2, c3, c4;
87 	while (bp < buffer + len) {
88 		c1 = CHAR64(*p);
89 		c2 = CHAR64(*(p + 1));
90 
91 		/* Invalid data */
92 		if (c1 == 255 || c2 == 255)
93 			break;
94 
95 		*bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
96 		if (bp >= buffer + len)
97 			break;
98 
99 		c3 = CHAR64(*(p + 2));
100 		if (c3 == 255)
101 			break;
102 
103 		*bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
104 		if (bp >= buffer + len)
105 			break;
106 
107 		c4 = CHAR64(*(p + 3));
108 		if (c4 == 255)
109 			break;
110 		*bp++ = ((c3 & 0x03) << 6) | c4;
111 
112 		p += 4;
113 	}
114 }
115 
116 static void
117 encode_salt(salt, csalt, clen, logr)
118 	char   *salt;
119 	u_int8_t *csalt;
120 	u_int16_t clen;
121 	u_int8_t logr;
122 {
123 	salt[0] = '$';
124 	salt[1] = BCRYPT_VERSION;
125 	salt[2] = '$';
126 
127 	snprintf(salt + 3, 4, "%2.2u$", logr);
128 
129 	encode_base64((u_int8_t *) salt + 6, csalt, clen);
130 }
131 /* Generates a salt for this version of crypt.
132    Since versions may change. Keeping this here
133    seems sensible.
134  */
135 
136 char   *
137 bcrypt_gensalt(log_rounds)
138 	u_int8_t log_rounds;
139 {
140 	u_int8_t csalt[BCRYPT_MAXSALT];
141 	u_int16_t i;
142 	u_int32_t seed = 0;
143 	(void) srandom((int) time((time_t *) NULL));
144 	for (i = 0; i < BCRYPT_MAXSALT; i++) {
145 		if (i % 4 == 0)
146 			seed = random();
147 		csalt[i] = seed & 0xff;
148 		seed = seed >> 8;
149 	}
150 
151 	if (log_rounds < 4)
152 		log_rounds = 4;
153 
154 	encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds);
155 	return gsalt;
156 }
157 /* We handle $Vers$log2(NumRounds)$salt+passwd$
158    i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
159 
160 char   *
161 bcrypt(key, salt)
162 	char   *key;
163 	char   *salt;
164 {
165 	blf_ctx state;
166 	u_int32_t rounds, i, k;
167 	u_int16_t j;
168 	u_int8_t key_len, salt_len, logr;
169 	u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OpenBSDbcrypthashfunc";
170 	u_int8_t csalt[BCRYPT_MAXSALT];
171 	u_int32_t cdata[BCRYPT_BLOCKS];
172 	/* Discard "$" identifier */
173 	salt++;
174 
175 	if (*salt > BCRYPT_VERSION) {
176 		/* How do I handle errors ? Return ':' */
177 		return error;
178 	}
179 	/* Discard version + "$" identifier */
180 	salt += 2;
181 
182 	if (*(salt + 2) != '$')
183 		/* Out of sync with passwd entry */
184 		return error;
185 
186 	/* Computer power doesnt increase linear, 2^x should be fine */
187 	if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS)
188 		return error;
189 
190 	/* Discard num rounds + "$" identifier */
191 	salt += 3;
192 
193 	/* We dont want the base64 salt but the raw data */
194 	decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
195 	salt_len = BCRYPT_MAXSALT;
196 	key_len = strlen(key);
197 
198 	/* Setting up S-Boxes and Subkeys */
199 	Blowfish_initstate(&state);
200 	Blowfish_expandstate(&state, csalt, salt_len,
201 	    (u_int8_t *) key, key_len);
202 	for (k = 0; k < rounds; k++) {
203 		Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
204 		Blowfish_expand0state(&state, csalt, salt_len);
205 	}
206 
207 	/* This can be precomputed later */
208 	j = 0;
209 	for (i = 0; i < BCRYPT_BLOCKS; i++)
210 		cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
211 
212 	/* Now do the encryption */
213 	for (k = 0; k < 64; k++)
214 		blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
215 
216 	for (i = 0; i < BCRYPT_BLOCKS; i++) {
217 		ciphertext[4 * i + 3] = cdata[i] & 0xff;
218 		cdata[i] = cdata[i] >> 8;
219 		ciphertext[4 * i + 2] = cdata[i] & 0xff;
220 		cdata[i] = cdata[i] >> 8;
221 		ciphertext[4 * i + 1] = cdata[i] & 0xff;
222 		cdata[i] = cdata[i] >> 8;
223 		ciphertext[4 * i + 0] = cdata[i] & 0xff;
224 	}
225 
226 
227 	encrypted[0] = '$';
228 	encrypted[1] = BCRYPT_VERSION;
229 	encrypted[2] = '$';
230 
231 	snprintf(encrypted + 3, 4, "%2.2u$", logr);
232 
233 	encode_base64((u_int8_t *) encrypted + 6, csalt, BCRYPT_MAXSALT);
234 	encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
235 	    4 * BCRYPT_BLOCKS);
236 	return encrypted;
237 }
238 
239 static void
240 encode_base64(buffer, data, len)
241 	u_int8_t *buffer;
242 	u_int8_t *data;
243 	u_int16_t len;
244 {
245 	u_int8_t *bp = buffer;
246 	u_int8_t *p = data;
247 	u_int8_t c1, c2;
248 	while (p < data + len) {
249 		c1 = *p++;
250 		*bp++ = Base64Code[(c1 >> 2)];
251 		c1 = (c1 & 0x03) << 4;
252 		c2 = *p++;
253 		if (p >= data + len) {
254 			*bp++ = Base64Code[c1];
255 			break;
256 		}
257 		c1 |= (c2 >> 4) & 0x0f;
258 		*bp++ = Base64Code[c1];
259 		c1 = (c2 & 0x0f) << 2;
260 		c2 = *p++;
261 		if (p >= data + len) {
262 			*bp++ = Base64Code[c1];
263 			break;
264 		}
265 		c1 |= (c2 >> 6) & 0x03;
266 		*bp++ = Base64Code[c1];
267 		*bp++ = Base64Code[c2 & 0x3f];
268 	}
269 	*bp = '\0';
270 }
271 #ifdef TEST
272 void
273 main()
274 {
275 	char    blubber[73];
276 	char    salt[100];
277 	char   *p;
278 	salt[0] = '$';
279 	salt[1] = BCRYPT_VERSION;
280 	salt[2] = '$';
281 
282 	snprintf(salt + 3, 4, "%2.2u$", 5);
283 
284 	printf("24 bytes of salt: ");
285 	fgets(salt + 6, 94, stdin);
286 	salt[99] = 0;
287 	printf("72 bytes of password: ");
288 	fpurge(stdin);
289 	fgets(blubber, 73, stdin);
290 	blubber[72] = 0;
291 
292 	p = crypt(blubber, salt);
293 	printf("Passwd entry: %s\n\n", p);
294 
295 	p = bcrypt_gensalt(5);
296 	printf("Generated salt: %s\n", p);
297 	p = crypt(blubber, p);
298 	printf("Passwd entry: %s\n", p);
299 }
300 #endif
301