1 /* $NetBSD: bcrypt.c,v 1.19 2013/08/28 17:47:07 riastradh Exp $ */
2 /* $OpenBSD: bcrypt.c,v 1.16 2002/02/19 19:39:36 millert Exp $ */
5 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Niels Provos.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 /* This password hashing algorithm was designed by David Mazieres
35 * <dm@lcs.mit.edu> and works as follows:
37 * 1. state := InitState ()
38 * 2. state := ExpandKey (state, salt, password) 3.
40 * state := ExpandKey (state, 0, salt)
41 * state := ExpandKey(state, 0, password)
42 * 4. ctext := "OrpheanBeholderScryDoubt"
44 * ctext := Encrypt_ECB (state, ctext);
45 * 6. RETURN Concatenate (salt, ctext);
48 #include <sys/cdefs.h>
49 __RCSID("$NetBSD: bcrypt.c,v 1.19 2013/08/28 17:47:07 riastradh Exp $");
53 #include <sys/types.h>
62 /* This implementation is adaptable to current computing power.
63 * You can have up to 2^31 rounds which should be enough for some
67 #define BCRYPT_VERSION '2'
68 #define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */
69 #define BCRYPT_MAXSALTLEN (7 + (BCRYPT_MAXSALT * 4 + 2) / 3 + 1)
70 #define BCRYPT_BLOCKS 6 /* Ciphertext blocks */
71 #define BCRYPT_MINROUNDS 16 /* we have log2(rounds) in salt */
73 static void encode_salt(char *, u_int8_t
*, u_int16_t
, u_int8_t
);
74 static void encode_base64(u_int8_t
*, u_int8_t
*, u_int16_t
);
75 static void decode_base64(u_int8_t
*, u_int16_t
, const u_int8_t
*);
77 char *__bcrypt(const char *, const char *); /* XXX */
79 static char encrypted
[_PASSWORD_LEN
];
81 static const u_int8_t Base64Code
[] =
82 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
84 char *bcrypt_gensalt(u_int8_t
);
86 static const u_int8_t index_64
[128] =
88 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
89 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
90 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
91 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
92 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
93 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
94 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
95 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
96 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
97 255, 255, 255, 255, 255, 255, 28, 29, 30,
98 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
99 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
100 51, 52, 53, 255, 255, 255, 255, 255
102 #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
105 decode_base64(u_int8_t
*buffer
, u_int16_t len
, const u_int8_t
*data
)
107 u_int8_t
*bp
= buffer
;
108 const u_int8_t
*p
= data
;
109 u_int8_t c1
, c2
, c3
, c4
;
110 while (bp
< buffer
+ len
) {
112 c2
= CHAR64(*(p
+ 1));
115 if (c1
== 255 || c2
== 255)
118 *bp
++ = ((u_int32_t
)c1
<< 2) | (((u_int32_t
)c2
& 0x30) >> 4);
119 if (bp
>= buffer
+ len
)
122 c3
= CHAR64(*(p
+ 2));
126 *bp
++ = (((u_int32_t
)c2
& 0x0f) << 4) | (((uint32_t)c3
& 0x3c) >> 2);
127 if (bp
>= buffer
+ len
)
130 c4
= CHAR64(*(p
+ 3));
133 *bp
++ = ((c3
& 0x03) << 6) | c4
;
140 encode_salt(char *salt
, u_int8_t
*csalt
, u_int16_t clen
, u_int8_t logr
)
143 salt
[1] = BCRYPT_VERSION
;
147 snprintf(salt
+ 4, 4, "%2.2u$", logr
);
149 encode_base64((u_int8_t
*) salt
+ 7, csalt
, clen
);
153 __gensalt_blowfish(char *salt
, size_t saltlen
, const char *option
)
157 u_int8_t csalt
[BCRYPT_MAXSALT
];
158 unsigned long nrounds
;
161 if (saltlen
< BCRYPT_MAXSALTLEN
) {
165 if (option
== NULL
) {
169 nrounds
= strtoul(option
, &ep
, 0);
170 if (option
== ep
|| *ep
) {
174 if (errno
== ERANGE
&& nrounds
== ULONG_MAX
)
179 else if (nrounds
> 31)
182 for (i
= 0; i
< BCRYPT_MAXSALT
; i
++) {
185 csalt
[i
] = seed
& 0xff;
188 encode_salt(salt
, csalt
, BCRYPT_MAXSALT
, (u_int8_t
)nrounds
);
192 /* Generates a salt for this version of crypt.
193 Since versions may change. Keeping this here
198 bcrypt_gensalt(u_int8_t log_rounds
)
200 static char gsalt
[BCRYPT_MAXSALTLEN
];
203 (void)snprintf(num
, sizeof(num
), "%d", log_rounds
);
204 if (__gensalt_blowfish(gsalt
, sizeof(gsalt
), num
) == -1)
209 /* We handle $Vers$log2(NumRounds)$salt+passwd$
210 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
213 __bcrypt(const char *key
, const char *salt
)
216 u_int32_t rounds
, i
, k
;
218 u_int8_t key_len
, salt_len
, logr
, minor
;
219 u_int8_t ciphertext
[4 * BCRYPT_BLOCKS
] = "OrpheanBeholderScryDoubt";
220 u_int8_t csalt
[BCRYPT_MAXSALT
];
221 u_int32_t cdata
[BCRYPT_BLOCKS
];
225 /* Discard "$" identifier */
228 if (*salt
> BCRYPT_VERSION
)
231 /* Check for minor versions */
232 if (salt
[1] != '$') {
235 /* 'ab' should not yield the same as 'abab' */
245 /* Discard version + "$" identifier */
249 /* Out of sync with passwd entry */
252 /* Computer power doesn't increase linear, 2^x should be fine */
257 if ((rounds
= (u_int32_t
) 1 << logr
) < BCRYPT_MINROUNDS
)
260 /* Discard num rounds + "$" identifier */
263 if (strlen(salt
) * 3 / 4 < BCRYPT_MAXSALT
)
266 /* We dont want the base64 salt but the raw data */
267 decode_base64(csalt
, BCRYPT_MAXSALT
, (const u_int8_t
*)salt
);
268 salt_len
= BCRYPT_MAXSALT
;
273 key_len
= (uint8_t)len
;
274 key_len
+= minor
>= 'a' ? 1 : 0;
276 /* Setting up S-Boxes and Subkeys */
277 Blowfish_initstate(&state
);
278 Blowfish_expandstate(&state
, csalt
, salt_len
,
279 (const u_int8_t
*) key
, key_len
);
280 for (k
= 0; k
< rounds
; k
++) {
281 Blowfish_expand0state(&state
, (const u_int8_t
*) key
, key_len
);
282 Blowfish_expand0state(&state
, csalt
, salt_len
);
285 /* This can be precomputed later */
287 for (i
= 0; i
< BCRYPT_BLOCKS
; i
++)
288 cdata
[i
] = Blowfish_stream2word(ciphertext
, 4 * BCRYPT_BLOCKS
, &j
);
290 /* Now do the encryption */
291 for (k
= 0; k
< 64; k
++)
292 blf_enc(&state
, cdata
, BCRYPT_BLOCKS
/ 2);
294 for (i
= 0; i
< BCRYPT_BLOCKS
; i
++) {
295 ciphertext
[4 * i
+ 3] = cdata
[i
] & 0xff;
296 cdata
[i
] = cdata
[i
] >> 8;
297 ciphertext
[4 * i
+ 2] = cdata
[i
] & 0xff;
298 cdata
[i
] = cdata
[i
] >> 8;
299 ciphertext
[4 * i
+ 1] = cdata
[i
] & 0xff;
300 cdata
[i
] = cdata
[i
] >> 8;
301 ciphertext
[4 * i
+ 0] = cdata
[i
] & 0xff;
306 encrypted
[i
++] = '$';
307 encrypted
[i
++] = BCRYPT_VERSION
;
309 encrypted
[i
++] = minor
;
310 encrypted
[i
++] = '$';
312 snprintf(encrypted
+ i
, 4, "%2.2u$", logr
);
314 encode_base64((u_int8_t
*) encrypted
+ i
+ 3, csalt
, BCRYPT_MAXSALT
);
315 encode_base64((u_int8_t
*) encrypted
+ strlen(encrypted
), ciphertext
,
316 4 * BCRYPT_BLOCKS
- 1);
317 explicit_memset(&state
, 0, sizeof(state
));
322 encode_base64(u_int8_t
*buffer
, u_int8_t
*data
, u_int16_t len
)
324 u_int8_t
*bp
= buffer
;
327 while (p
< data
+ len
) {
329 *bp
++ = Base64Code
[((u_int32_t
)c1
>> 2)];
330 c1
= (c1
& 0x03) << 4;
331 if (p
>= data
+ len
) {
332 *bp
++ = Base64Code
[c1
];
336 c1
|= ((u_int32_t
)c2
>> 4) & 0x0f;
337 *bp
++ = Base64Code
[c1
];
338 c1
= (c2
& 0x0f) << 2;
339 if (p
>= data
+ len
) {
340 *bp
++ = Base64Code
[c1
];
344 c1
|= ((u_int32_t
)c2
>> 6) & 0x03;
345 *bp
++ = Base64Code
[c1
];
346 *bp
++ = Base64Code
[c2
& 0x3f];
358 salt
[1] = BCRYPT_VERSION
;
361 snprintf(salt
+ 3, 4, "%2.2u$", 5);
363 printf("24 bytes of salt: ");
364 fgets(salt
+ 6, 94, stdin
);
366 printf("72 bytes of password: ");
368 fgets(blubber
, 73, stdin
);
371 p
= crypt(blubber
, salt
);
372 printf("Passwd entry: %s\n\n", p
);
374 p
= bcrypt_gensalt(5);
375 printf("Generated salt: %s\n", p
);
376 p
= crypt(blubber
, p
);
377 printf("Passwd entry: %s\n", p
);