xref: /freebsd/secure/lib/libcrypt/crypt-blowfish.c (revision 27c43fe1f3795622c5bd4bbfc465a29a800c0799)
1 /*	$OpenBSD: bcrypt.c,v 1.29 2014/02/24 19:45:43 tedu 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 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /* This password hashing algorithm was designed by David Mazieres
37  * <dm@lcs.mit.edu> and works as follows:
38  *
39  * 1. state := InitState ()
40  * 2. state := ExpandKey (state, salt, password)
41  * 3. REPEAT rounds:
42  *      state := ExpandKey (state, 0, password)
43  *	state := ExpandKey (state, 0, salt)
44  * 4. ctext := "OrpheanBeholderScryDoubt"
45  * 5. REPEAT 64:
46  * 	ctext := Encrypt_ECB (state, ctext);
47  * 6. RETURN Concatenate (salt, ctext);
48  *
49  */
50 
51 /*
52  * FreeBSD implementation by Paul Herman <pherman@frenchfries.net>
53  * and updated by Xin Li <delphij@FreeBSD.org>
54  */
55 
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <sys/types.h>
59 #include <string.h>
60 #include <pwd.h>
61 #include "blowfish.h"
62 #include "crypt.h"
63 
64 /* This implementation is adaptable to current computing power.
65  * You can have up to 2^31 rounds which should be enough for some
66  * time to come.
67  */
68 
69 #define BCRYPT_VERSION '2'
70 #define BCRYPT_MAXSALT 16	/* Precomputation is just so nice */
71 #define BCRYPT_BLOCKS 6		/* Ciphertext blocks */
72 #define BCRYPT_MINLOGROUNDS 4	/* we have log2(rounds) in salt */
73 
74 
75 static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t);
76 static void decode_base64(u_int8_t *, u_int16_t, const u_int8_t *);
77 
78 static char    encrypted[_PASSWORD_LEN];
79 
80 const static u_int8_t Base64Code[] =
81 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
82 
83 const static u_int8_t index_64[128] = {
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, 255, 255, 255, 255,
88 	255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
89 	56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
90 	255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
91 	7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
92 	17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
93 	255, 255, 255, 255, 255, 255, 28, 29, 30,
94 	31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
95 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
96 	51, 52, 53, 255, 255, 255, 255, 255
97 };
98 #define CHAR64(c)  ( (c) > 127 ? 255 : index_64[(c)])
99 
100 static void
101 decode_base64(u_int8_t *buffer, u_int16_t len, const u_int8_t *data)
102 {
103 	u_int8_t *bp = buffer;
104 	const u_int8_t *p = data;
105 	u_int8_t c1, c2, c3, c4;
106 	while (bp < buffer + len) {
107 		c1 = CHAR64(*p);
108 		c2 = CHAR64(*(p + 1));
109 
110 		/* Invalid data */
111 		if (c1 == 255 || c2 == 255)
112 			break;
113 
114 		*bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
115 		if (bp >= buffer + len)
116 			break;
117 
118 		c3 = CHAR64(*(p + 2));
119 		if (c3 == 255)
120 			break;
121 
122 		*bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
123 		if (bp >= buffer + len)
124 			break;
125 
126 		c4 = CHAR64(*(p + 3));
127 		if (c4 == 255)
128 			break;
129 		*bp++ = ((c3 & 0x03) << 6) | c4;
130 
131 		p += 4;
132 	}
133 }
134 
135 /* We handle $Vers$log2(NumRounds)$salt+passwd$
136    i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
137 
138 char   *
139 crypt_blowfish(const char *key, const char *salt)
140 {
141 	blf_ctx state;
142 	u_int32_t rounds, i, k;
143 	u_int16_t j;
144 	size_t key_len;
145 	u_int8_t salt_len, logr, minr;
146 	u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt";
147 	u_int8_t csalt[BCRYPT_MAXSALT];
148 	u_int32_t cdata[BCRYPT_BLOCKS];
149 	char arounds[3];
150 
151 	/* Defaults */
152 	minr = 'a';
153 	logr = BCRYPT_MINLOGROUNDS;
154 	rounds = 1U << logr;
155 
156 	if (*salt == '$') {
157 		/* Discard "$" identifier */
158 		salt++;
159 
160 		if (*salt > BCRYPT_VERSION) {
161 			/* How do I handle errors ? Return NULL */
162 			return NULL;
163 		}
164 
165 		/* Check for minor versions */
166 		if (salt[1] != '$') {
167 			 switch (salt[1]) {
168 			 case 'a':	/* 'ab' should not yield the same as 'abab' */
169 			 case 'b':	/* cap input length at 72 bytes */
170 				 minr = salt[1];
171 				 salt++;
172 				 break;
173 			 default:
174 				 return NULL;
175 			 }
176 		} else
177 			 minr = 0;
178 
179 		/* Discard version + "$" identifier */
180 		salt += 2;
181 
182 		if (salt[2] != '$')
183 			/* Out of sync with passwd entry */
184 			return NULL;
185 
186 		memcpy(arounds, salt, sizeof(arounds));
187 		if (arounds[sizeof(arounds) - 1] != '$')
188 			return NULL;
189 		arounds[sizeof(arounds) - 1] = 0;
190 		logr = strtonum(arounds, BCRYPT_MINLOGROUNDS, 31, NULL);
191 		if (logr == 0)
192 			return NULL;
193 		/* Computer power doesn't increase linearly, 2^x should be fine */
194 		rounds = 1U << logr;
195 
196 		/* Discard num rounds + "$" identifier */
197 		salt += 3;
198 	}
199 
200 	if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT)
201 		return NULL;
202 
203 	/* We dont want the base64 salt but the raw data */
204 	decode_base64(csalt, BCRYPT_MAXSALT, (const u_int8_t *) salt);
205 	salt_len = BCRYPT_MAXSALT;
206 	if (minr <= 'a')
207 		key_len = (u_int8_t)(strlen(key) + (minr >= 'a' ? 1 : 0));
208 	else {
209 		/* strlen() returns a size_t, but the function calls
210 		 * below result in implicit casts to a narrower integer
211 		 * type, so cap key_len at the actual maximum supported
212 		 * length here to avoid integer wraparound */
213 		key_len = strlen(key);
214 		if (key_len > 72)
215 			key_len = 72;
216 		key_len++; /* include the NUL */
217 	}
218 
219 	/* Setting up S-Boxes and Subkeys */
220 	Blowfish_initstate(&state);
221 	Blowfish_expandstate(&state, csalt, salt_len,
222 	    (const u_int8_t *) key, key_len);
223 	for (k = 0; k < rounds; k++) {
224 		Blowfish_expand0state(&state, (const 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 	i = 0;
249 	encrypted[i++] = '$';
250 	encrypted[i++] = BCRYPT_VERSION;
251 	if (minr)
252 		encrypted[i++] = minr;
253 	encrypted[i++] = '$';
254 
255 	snprintf(encrypted + i, 4, "%2.2u$", logr);
256 
257 	encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT);
258 	encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
259 	    4 * BCRYPT_BLOCKS - 1);
260 	memset(&state, 0, sizeof(state));
261 	memset(ciphertext, 0, sizeof(ciphertext));
262 	memset(csalt, 0, sizeof(csalt));
263 	memset(cdata, 0, sizeof(cdata));
264 	return encrypted;
265 }
266 
267 static void
268 encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
269 {
270 	u_int8_t *bp = buffer;
271 	u_int8_t *p = data;
272 	u_int8_t c1, c2;
273 	while (p < data + len) {
274 		c1 = *p++;
275 		*bp++ = Base64Code[(c1 >> 2)];
276 		c1 = (c1 & 0x03) << 4;
277 		if (p >= data + len) {
278 			*bp++ = Base64Code[c1];
279 			break;
280 		}
281 		c2 = *p++;
282 		c1 |= (c2 >> 4) & 0x0f;
283 		*bp++ = Base64Code[c1];
284 		c1 = (c2 & 0x0f) << 2;
285 		if (p >= data + len) {
286 			*bp++ = Base64Code[c1];
287 			break;
288 		}
289 		c2 = *p++;
290 		c1 |= (c2 >> 6) & 0x03;
291 		*bp++ = Base64Code[c1];
292 		*bp++ = Base64Code[c2 & 0x3f];
293 	}
294 	*bp = '\0';
295 }
296 #if 0
297 void
298 main()
299 {
300 	char    blubber[73];
301 	char    salt[100];
302 	char   *p;
303 	salt[0] = '$';
304 	salt[1] = BCRYPT_VERSION;
305 	salt[2] = '$';
306 
307 	snprintf(salt + 3, 4, "%2.2u$", 5);
308 
309 	printf("24 bytes of salt: ");
310 	fgets(salt + 6, sizeof(salt) - 6, stdin);
311 	salt[99] = 0;
312 	printf("72 bytes of password: ");
313 	fpurge(stdin);
314 	fgets(blubber, sizeof(blubber), stdin);
315 	blubber[72] = 0;
316 
317 	p = crypt(blubber, salt);
318 	printf("Passwd entry: %s\n\n", p);
319 
320 	p = bcrypt_gensalt(5);
321 	printf("Generated salt: %s\n", p);
322 	p = crypt(blubber, p);
323 	printf("Passwd entry: %s\n", p);
324 }
325 #endif
326