2 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Niels Provos.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/secure/lib/libcrypt/crypt-blowfish.c,v 1.1.2.1 2001/05/24 12:20:03 markm Exp $
31 * $DragonFly: src/secure/lib/libcrypt/crypt-blowfish.c,v 1.2 2003/06/17 04:27:48 dillon Exp $
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);
50 * FreeBSD implementation by Paul Herman <pherman@frenchfries.net>
59 #include <sys/types.h>
64 /* This implementation is adaptable to current computing power.
65 * You can have up to 2^31 rounds which should be enough for some
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_MINROUNDS 16 /* we have log2(rounds) in salt */
74 char *bcrypt_gensalt __P((u_int8_t));
76 static void encode_salt __P((char *, u_int8_t *, u_int16_t, u_int8_t));
77 static void encode_base64 __P((u_int8_t *, u_int8_t *, u_int16_t));
78 static void decode_base64 __P((u_int8_t *, u_int16_t, u_int8_t *));
80 static char encrypted[_PASSWORD_LEN];
81 static char gsalt[BCRYPT_MAXSALT * 4 / 3 + 1];
82 static char error[] = ":";
84 const static u_int8_t Base64Code[] =
85 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
87 const static 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)])
107 decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
110 decode_base64(buffer, len, data)
116 u_int8_t *bp = buffer;
118 u_int8_t c1, c2, c3, c4;
119 while (bp < buffer + len) {
121 c2 = CHAR64(*(p + 1));
124 if (c1 == 255 || c2 == 255)
127 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
128 if (bp >= buffer + len)
131 c3 = CHAR64(*(p + 2));
135 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
136 if (bp >= buffer + len)
139 c4 = CHAR64(*(p + 3));
142 *bp++ = ((c3 & 0x03) << 6) | c4;
150 encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr)
153 encode_salt(salt, csalt, clen, logr)
161 salt[1] = BCRYPT_VERSION;
165 snprintf(salt + 4, 4, "%2.2u$", logr);
167 encode_base64((u_int8_t *) salt + 7, csalt, clen);
169 /* Generates a salt for this version of crypt.
170 Since versions may change. Keeping this here
176 bcrypt_gensalt(u_int8_t log_rounds)
179 bcrypt_gensalt(log_rounds)
183 u_int8_t csalt[BCRYPT_MAXSALT];
187 for (i = 0; i < BCRYPT_MAXSALT; i++) {
190 csalt[i] = seed & 0xff;
197 encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds);
200 /* We handle $Vers$log2(NumRounds)$salt+passwd$
201 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
204 crypt_blowfish(key, salt)
209 u_int32_t rounds, i, k;
211 u_int8_t key_len, salt_len, logr, minor;
212 u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt";
213 u_int8_t csalt[BCRYPT_MAXSALT];
214 u_int32_t cdata[BCRYPT_BLOCKS];
215 static char *magic = "$2a$04$";
222 /* If it starts with the magic string, then skip that */
223 if(!strncmp(salt, magic, strlen(magic))) {
224 salt += strlen(magic);
226 else if (*salt == '$') {
228 /* Discard "$" identifier */
231 if (*salt > BCRYPT_VERSION) {
232 /* How do I handle errors ? Return ':' */
236 /* Check for minor versions */
237 if (salt[1] != '$') {
240 /* 'ab' should not yield the same as 'abab' */
250 /* Discard version + "$" identifier */
254 /* Out of sync with passwd entry */
257 /* Computer power doesnt increase linear, 2^x should be fine */
258 if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS)
261 /* Discard num rounds + "$" identifier */
266 /* We dont want the base64 salt but the raw data */
267 decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
268 salt_len = BCRYPT_MAXSALT;
269 key_len = strlen(key) + (minor >= 'a' ? 1 : 0);
271 /* Setting up S-Boxes and Subkeys */
272 Blowfish_initstate(&state);
273 Blowfish_expandstate(&state, csalt, salt_len,
274 (u_int8_t *) key, key_len);
275 for (k = 0; k < rounds; k++) {
276 Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
277 Blowfish_expand0state(&state, csalt, salt_len);
280 /* This can be precomputed later */
282 for (i = 0; i < BCRYPT_BLOCKS; i++)
283 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
285 /* Now do the encryption */
286 for (k = 0; k < 64; k++)
287 blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
289 for (i = 0; i < BCRYPT_BLOCKS; i++) {
290 ciphertext[4 * i + 3] = cdata[i] & 0xff;
291 cdata[i] = cdata[i] >> 8;
292 ciphertext[4 * i + 2] = cdata[i] & 0xff;
293 cdata[i] = cdata[i] >> 8;
294 ciphertext[4 * i + 1] = cdata[i] & 0xff;
295 cdata[i] = cdata[i] >> 8;
296 ciphertext[4 * i + 0] = cdata[i] & 0xff;
301 encrypted[i++] = '$';
302 encrypted[i++] = BCRYPT_VERSION;
304 encrypted[i++] = minor;
305 encrypted[i++] = '$';
307 snprintf(encrypted + i, 4, "%2.2u$", logr);
309 encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT);
310 encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
311 4 * BCRYPT_BLOCKS - 1);
317 encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
320 encode_base64(buffer, data, len)
326 u_int8_t *bp = buffer;
329 while (p < data + len) {
331 *bp++ = Base64Code[(c1 >> 2)];
332 c1 = (c1 & 0x03) << 4;
333 if (p >= data + len) {
334 *bp++ = Base64Code[c1];
338 c1 |= (c2 >> 4) & 0x0f;
339 *bp++ = Base64Code[c1];
340 c1 = (c2 & 0x0f) << 2;
341 if (p >= data + len) {
342 *bp++ = Base64Code[c1];
346 c1 |= (c2 >> 6) & 0x03;
347 *bp++ = Base64Code[c1];
348 *bp++ = Base64Code[c2 & 0x3f];
360 salt[1] = BCRYPT_VERSION;
363 snprintf(salt + 3, 4, "%2.2u$", 5);
365 printf("24 bytes of salt: ");
366 fgets(salt + 6, 94, stdin);
368 printf("72 bytes of password: ");
370 fgets(blubber, 73, stdin);
373 p = crypt(blubber, salt);
374 printf("Passwd entry: %s\n\n", p);
376 p = bcrypt_gensalt(5);
377 printf("Generated salt: %s\n", p);
378 p = crypt(blubber, p);
379 printf("Passwd entry: %s\n", p);