2 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* $OpenBSD: bcrypt.c,v 1.16 2002/02/19 19:39:36 millert Exp $ */
9 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by Niels Provos.
23 * 4. The name of the author may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This password hashing algorithm was designed by David Mazieres
40 * <dm@lcs.mit.edu> and works as follows:
42 * 1. state := InitState ()
43 * 2. state := ExpandKey (state, salt, password) 3.
45 * state := ExpandKey (state, 0, salt)
46 * state := ExpandKey(state, 0, password)
47 * 4. ctext := "OrpheanBeholderScryDoubt"
49 * ctext := Encrypt_ECB (state, ctext);
50 * 6. RETURN Concatenate (salt, ctext);
60 #include <sys/types.h>
65 extern uint32_t arc4random();
68 * This implementation is adaptable to current computing power.
69 * You can have up to 2^31 rounds which should be enough for some
73 #define BCRYPT_VERSION '2'
74 #define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */
75 #define BCRYPT_BLOCKS 6 /* Ciphertext blocks */
76 #define BCRYPT_MINLOGROUNDS 4 /* we have log2(rounds) in salt */
77 #define BCRYPT_MAXLOGROUNDS 31
79 char *bcrypt_gensalt(uint8_t);
81 static void encode_salt(char *, uint8_t *, uint16_t, uint8_t);
82 static void encode_base64(uint8_t *, uint8_t *, uint16_t);
83 static void decode_base64(uint8_t *, uint16_t, uint8_t *);
85 static char encrypted
[128]; /* _PASSWORD_LEN in <pwd.h> on OpenBSD */
86 static char gsalt
[BCRYPT_MAXSALT
* 4 / 3 + 1];
87 static char error
[] = ":";
89 static uint8_t Base64Code
[] =
90 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
92 static uint8_t index_64
[128] =
94 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
95 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
96 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
97 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
98 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
99 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
100 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
101 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
102 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
103 255, 255, 255, 255, 255, 255, 28, 29, 30,
104 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
105 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
106 51, 52, 53, 255, 255, 255, 255, 255
108 #define CHAR64(c) ((c) > 127 ? 255 : index_64[(c)])
111 decode_base64(uint8_t *buffer
, uint16_t len
, uint8_t *data
)
113 uint8_t *bp
= buffer
;
115 uint8_t c1
, c2
, c3
, c4
;
116 while (bp
< buffer
+ len
) {
118 c2
= CHAR64(*(p
+ 1));
121 if (c1
== 255 || c2
== 255)
124 *bp
++ = (c1
<< 2) | ((c2
& 0x30) >> 4);
125 if (bp
>= buffer
+ len
)
128 c3
= CHAR64(*(p
+ 2));
132 *bp
++ = ((c2
& 0x0f) << 4) | ((c3
& 0x3c) >> 2);
133 if (bp
>= buffer
+ len
)
136 c4
= CHAR64(*(p
+ 3));
139 *bp
++ = ((c3
& 0x03) << 6) | c4
;
146 encode_salt(char *salt
, uint8_t *csalt
, uint16_t clen
, uint8_t logr
)
149 salt
[1] = BCRYPT_VERSION
;
153 (void) snprintf(salt
+ 4, 4, "%2.2u$", logr
);
155 encode_base64((uint8_t *)salt
+ 7, csalt
, clen
);
158 * Generates a salt for this version of crypt.
159 * Since versions may change. Keeping this here
164 bcrypt_gensalt(uint8_t log_rounds
)
166 uint8_t csalt
[BCRYPT_MAXSALT
];
170 for (i
= 0; i
< BCRYPT_MAXSALT
; i
++) {
173 csalt
[i
] = seed
& 0xff;
177 if (log_rounds
< BCRYPT_MINLOGROUNDS
)
178 log_rounds
= BCRYPT_MINLOGROUNDS
;
179 else if (log_rounds
> BCRYPT_MAXLOGROUNDS
)
180 log_rounds
= BCRYPT_MAXLOGROUNDS
;
182 encode_salt(gsalt
, csalt
, BCRYPT_MAXSALT
, log_rounds
);
186 * We handle $Vers$log2(NumRounds)$salt+passwd$
187 * i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou
196 uint32_t rounds
, i
, k
;
199 uint8_t salt_len
, logr
, minor
;
200 uint8_t ciphertext
[4 * BCRYPT_BLOCKS
] = "OrpheanBeholderScryDoubt";
201 uint8_t csalt
[BCRYPT_MAXSALT
];
202 uint32_t cdata
[BCRYPT_BLOCKS
];
205 /* Discard "$" identifier */
208 if (*salt
> BCRYPT_VERSION
) {
209 /* How do I handle errors ? Return ':' */
213 /* Check for minor versions */
214 if (salt
[1] != '$') {
216 case 'a': /* 'ab' should not yield the same as 'abab' */
217 case 'b': /* cap input length at 72 bytes */
227 /* Discard version + "$" identifier */
231 /* Out of sync with passwd entry */
234 (void) memcpy(arounds
, salt
, sizeof (arounds
));
235 if (arounds
[sizeof (arounds
) - 1] != '$')
237 if ((logr
= atoi(arounds
)) < BCRYPT_MINLOGROUNDS
||
238 logr
> BCRYPT_MAXLOGROUNDS
)
240 /* Computer power doesn't increase linear, 2^x should be fine */
243 /* Discard num rounds + "$" identifier */
246 if (strlen(salt
) * 3 / 4 < BCRYPT_MAXSALT
)
249 /* We dont want the base64 salt but the raw data */
250 decode_base64(csalt
, BCRYPT_MAXSALT
, (uint8_t *)salt
);
251 salt_len
= BCRYPT_MAXSALT
;
253 key_len
= (uint8_t)(strlen(key
) + (minor
>= 'a' ? 1 : 0));
256 * strlen() returns a size_t, but the function calls
257 * below result in implicit casts to a narrower integer
258 * type, so cap key_len at the actual maximum supported
259 * length here to avoid integer wraparound
261 key_len
= strlen(key
);
264 key_len
++; /* include the NUL */
267 /* Setting up S-Boxes and Subkeys */
268 Blowfish_initstate(&state
);
269 Blowfish_expandstate(&state
, csalt
, salt_len
,
270 (uint8_t *)key
, key_len
);
271 for (k
= 0; k
< rounds
; k
++) {
272 Blowfish_expand0state(&state
, (uint8_t *)key
, key_len
);
273 Blowfish_expand0state(&state
, csalt
, salt_len
);
276 /* This can be precomputed later */
278 for (i
= 0; i
< BCRYPT_BLOCKS
; i
++)
279 cdata
[i
] = Blowfish_stream2word(ciphertext
,
280 4 * BCRYPT_BLOCKS
, &j
);
282 /* Now do the encryption */
283 for (k
= 0; k
< 64; k
++)
284 blf_enc(&state
, cdata
, BCRYPT_BLOCKS
/ 2);
286 for (i
= 0; i
< BCRYPT_BLOCKS
; i
++) {
287 ciphertext
[4 * i
+ 3] = cdata
[i
] & 0xff;
288 cdata
[i
] = cdata
[i
] >> 8;
289 ciphertext
[4 * i
+ 2] = cdata
[i
] & 0xff;
290 cdata
[i
] = cdata
[i
] >> 8;
291 ciphertext
[4 * i
+ 1] = cdata
[i
] & 0xff;
292 cdata
[i
] = cdata
[i
] >> 8;
293 ciphertext
[4 * i
+ 0] = cdata
[i
] & 0xff;
298 encrypted
[i
++] = '$';
299 encrypted
[i
++] = BCRYPT_VERSION
;
301 encrypted
[i
++] = minor
;
302 encrypted
[i
++] = '$';
304 (void) snprintf(encrypted
+ i
, 4, "%2.2u$", logr
);
306 encode_base64((uint8_t *)encrypted
+ i
+ 3, csalt
, BCRYPT_MAXSALT
);
307 encode_base64((uint8_t *)encrypted
+ strlen(encrypted
), ciphertext
,
308 4 * BCRYPT_BLOCKS
- 1);
313 encode_base64(uint8_t *buffer
, uint8_t *data
, uint16_t len
)
315 uint8_t *bp
= buffer
;
318 while (p
< data
+ len
) {
320 *bp
++ = Base64Code
[(c1
>> 2)];
321 c1
= (c1
& 0x03) << 4;
322 if (p
>= data
+ len
) {
323 *bp
++ = Base64Code
[c1
];
327 c1
|= (c2
>> 4) & 0x0f;
328 *bp
++ = Base64Code
[c1
];
329 c1
= (c2
& 0x0f) << 2;
330 if (p
>= data
+ len
) {
331 *bp
++ = Base64Code
[c1
];
335 c1
|= (c2
>> 6) & 0x03;
336 *bp
++ = Base64Code
[c1
];
337 *bp
++ = Base64Code
[c2
& 0x3f];
349 salt
[1] = BCRYPT_VERSION
;
352 snprintf(salt
+ 3, 4, "%2.2u$", 5);
354 printf("24 bytes of salt: ");
355 fgets(salt
+ 6, 94, stdin
);
357 printf("72 bytes of password: ");
359 fgets(blubber
, 73, stdin
);
362 p
= crypt(blubber
, salt
);
363 printf("Passwd entry: %s\n\n", p
);
365 p
= bcrypt_gensalt(5);
366 printf("Generated salt: %s\n", p
);
367 p
= crypt(blubber
, p
);
368 printf("Passwd entry: %s\n", p
);