1 /* $NetBSD: bcrypt.c,v 1.9 2006/10/27 19:39:11 drochner 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.9 2006/10/27 19:39:11 drochner 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 (BCRYPT_MAXSALT * 4 / 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
];
80 static char error
[] = ":";
82 static const u_int8_t Base64Code
[] =
83 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
85 char *bcrypt_gensalt(u_int8_t
);
87 static const u_int8_t index_64
[128] =
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, 255, 255, 255, 255,
93 255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
94 56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
95 255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
96 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
97 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
98 255, 255, 255, 255, 255, 255, 28, 29, 30,
99 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
100 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
101 51, 52, 53, 255, 255, 255, 255, 255
103 #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)])
106 decode_base64(u_int8_t
*buffer
, u_int16_t len
, const u_int8_t
*data
)
108 u_int8_t
*bp
= buffer
;
109 const u_int8_t
*p
= data
;
110 u_int8_t c1
, c2
, c3
, c4
;
111 while (bp
< buffer
+ len
) {
113 c2
= CHAR64(*(p
+ 1));
116 if (c1
== 255 || c2
== 255)
119 *bp
++ = ((u_int32_t
)c1
<< 2) | (((u_int32_t
)c2
& 0x30) >> 4);
120 if (bp
>= buffer
+ len
)
123 c3
= CHAR64(*(p
+ 2));
127 *bp
++ = (((u_int32_t
)c2
& 0x0f) << 4) | (((uint32_t)c3
& 0x3c) >> 2);
128 if (bp
>= buffer
+ len
)
131 c4
= CHAR64(*(p
+ 3));
134 *bp
++ = ((c3
& 0x03) << 6) | c4
;
141 encode_salt(char *salt
, u_int8_t
*csalt
, u_int16_t clen
, u_int8_t logr
)
144 salt
[1] = BCRYPT_VERSION
;
148 snprintf(salt
+ 4, 4, "%2.2u$", logr
);
150 encode_base64((u_int8_t
*) salt
+ 7, csalt
, clen
);
154 __gensalt_blowfish(char *salt
, size_t saltlen
, const char *option
)
158 u_int8_t csalt
[BCRYPT_MAXSALT
];
159 unsigned long nrounds
;
162 if (saltlen
< BCRYPT_MAXSALTLEN
) {
166 if (option
== NULL
) {
170 nrounds
= strtoul(option
, &ep
, 0);
171 if (option
== ep
|| *ep
) {
175 if (errno
== ERANGE
&& nrounds
== ULONG_MAX
)
186 for (i
= 0; i
< BCRYPT_MAXSALT
; i
++) {
189 csalt
[i
] = seed
& 0xff;
192 encode_salt(salt
, csalt
, BCRYPT_MAXSALT
, (u_int8_t
)nrounds
);
196 /* Generates a salt for this version of crypt.
197 Since versions may change. Keeping this here
202 bcrypt_gensalt(u_int8_t log_rounds
)
204 static char gsalt
[BCRYPT_MAXSALTLEN
];
207 (void)snprintf(num
, sizeof(num
), "%d", log_rounds
);
208 if (__gensalt_blowfish(gsalt
, sizeof(gsalt
), num
) == -1)
213 /* We handle $Vers$log2(NumRounds)$salt+passwd$
214 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
222 u_int32_t rounds
, i
, k
;
224 u_int8_t key_len
, salt_len
, logr
, minor
;
225 u_int8_t ciphertext
[4 * BCRYPT_BLOCKS
] = "OrpheanBeholderScryDoubt";
226 u_int8_t csalt
[BCRYPT_MAXSALT
];
227 u_int32_t cdata
[BCRYPT_BLOCKS
];
229 /* Discard "$" identifier */
232 if (*salt
> BCRYPT_VERSION
) {
233 /* How do I handle errors ? Return ':' */
237 /* Check for minor versions */
238 if (salt
[1] != '$') {
241 /* 'ab' should not yield the same as 'abab' */
251 /* Discard version + "$" identifier */
255 /* Out of sync with passwd entry */
258 /* Computer power doesn't increase linear, 2^x should be fine */
259 if ((rounds
= (u_int32_t
) 1 << (logr
= atoi(salt
))) < BCRYPT_MINROUNDS
)
262 /* Discard num rounds + "$" identifier */
265 if (strlen(salt
) * 3 / 4 < BCRYPT_MAXSALT
)
268 /* We dont want the base64 salt but the raw data */
269 decode_base64(csalt
, BCRYPT_MAXSALT
, (const u_int8_t
*)salt
);
270 salt_len
= BCRYPT_MAXSALT
;
271 key_len
= strlen(key
) + (minor
>= 'a' ? 1 : 0);
273 /* Setting up S-Boxes and Subkeys */
274 Blowfish_initstate(&state
);
275 Blowfish_expandstate(&state
, csalt
, salt_len
,
276 (const u_int8_t
*) key
, key_len
);
277 for (k
= 0; k
< rounds
; k
++) {
278 Blowfish_expand0state(&state
, (const u_int8_t
*) key
, key_len
);
279 Blowfish_expand0state(&state
, csalt
, salt_len
);
282 /* This can be precomputed later */
284 for (i
= 0; i
< BCRYPT_BLOCKS
; i
++)
285 cdata
[i
] = Blowfish_stream2word(ciphertext
, 4 * BCRYPT_BLOCKS
, &j
);
287 /* Now do the encryption */
288 for (k
= 0; k
< 64; k
++)
289 blf_enc(&state
, cdata
, BCRYPT_BLOCKS
/ 2);
291 for (i
= 0; i
< BCRYPT_BLOCKS
; i
++) {
292 ciphertext
[4 * i
+ 3] = cdata
[i
] & 0xff;
293 cdata
[i
] = cdata
[i
] >> 8;
294 ciphertext
[4 * i
+ 2] = cdata
[i
] & 0xff;
295 cdata
[i
] = cdata
[i
] >> 8;
296 ciphertext
[4 * i
+ 1] = cdata
[i
] & 0xff;
297 cdata
[i
] = cdata
[i
] >> 8;
298 ciphertext
[4 * i
+ 0] = cdata
[i
] & 0xff;
303 encrypted
[i
++] = '$';
304 encrypted
[i
++] = BCRYPT_VERSION
;
306 encrypted
[i
++] = minor
;
307 encrypted
[i
++] = '$';
309 snprintf(encrypted
+ i
, 4, "%2.2u$", logr
);
311 encode_base64((u_int8_t
*) encrypted
+ i
+ 3, csalt
, BCRYPT_MAXSALT
);
312 encode_base64((u_int8_t
*) encrypted
+ strlen(encrypted
), ciphertext
,
313 4 * BCRYPT_BLOCKS
- 1);
318 encode_base64(u_int8_t
*buffer
, u_int8_t
*data
, u_int16_t len
)
320 u_int8_t
*bp
= buffer
;
323 while (p
< data
+ len
) {
325 *bp
++ = Base64Code
[((u_int32_t
)c1
>> 2)];
326 c1
= (c1
& 0x03) << 4;
327 if (p
>= data
+ len
) {
328 *bp
++ = Base64Code
[c1
];
332 c1
|= ((u_int32_t
)c2
>> 4) & 0x0f;
333 *bp
++ = Base64Code
[c1
];
334 c1
= (c2
& 0x0f) << 2;
335 if (p
>= data
+ len
) {
336 *bp
++ = Base64Code
[c1
];
340 c1
|= ((u_int32_t
)c2
>> 6) & 0x03;
341 *bp
++ = Base64Code
[c1
];
342 *bp
++ = Base64Code
[c2
& 0x3f];
354 salt
[1] = BCRYPT_VERSION
;
357 snprintf(salt
+ 3, 4, "%2.2u$", 5);
359 printf("24 bytes of salt: ");
360 fgets(salt
+ 6, 94, stdin
);
362 printf("72 bytes of password: ");
364 fgets(blubber
, 73, stdin
);
367 p
= crypt(blubber
, salt
);
368 printf("Passwd entry: %s\n\n", p
);
370 p
= bcrypt_gensalt(5);
371 printf("Generated salt: %s\n", p
);
372 p
= crypt(blubber
, p
);
373 printf("Passwd entry: %s\n", p
);