Home | History | Annotate | Line # | Download | only in libcrypt
bcrypt.c revision 1.2
      1 /*	$NetBSD: bcrypt.c,v 1.2 2003/04/17 00:31:04 thorpej Exp $	*/
      2 /*	$OpenBSD: bcrypt.c,v 1.16 2002/02/19 19:39:36 millert Exp $	*/
      3 
      4 /*
      5  * Copyright 1997 Niels Provos <provos (at) physnet.uni-hamburg.de>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     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.
     21  *
     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.
     32  */
     33 
     34 /* This password hashing algorithm was designed by David Mazieres
     35  * <dm (at) lcs.mit.edu> and works as follows:
     36  *
     37  * 1. state := InitState ()
     38  * 2. state := ExpandKey (state, salt, password) 3.
     39  * REPEAT rounds:
     40  *	state := ExpandKey (state, 0, salt)
     41  *      state := ExpandKey(state, 0, password)
     42  * 4. ctext := "OrpheanBeholderScryDoubt"
     43  * 5. REPEAT 64:
     44  * 	ctext := Encrypt_ECB (state, ctext);
     45  * 6. RETURN Concatenate (salt, ctext);
     46  *
     47  */
     48 
     49 #if 0
     50 #include <stdio.h>
     51 #endif
     52 
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <sys/types.h>
     56 #include <string.h>
     57 #include <pwd.h>
     58 
     59 #include "blowfish.c"
     60 
     61 /* This implementation is adaptable to current computing power.
     62  * You can have up to 2^31 rounds which should be enough for some
     63  * time to come.
     64  */
     65 
     66 #define BCRYPT_VERSION '2'
     67 #define BCRYPT_MAXSALT 16	/* Precomputation is just so nice */
     68 #define BCRYPT_BLOCKS 6		/* Ciphertext blocks */
     69 #define BCRYPT_MINROUNDS 16	/* we have log2(rounds) in salt */
     70 
     71 static void encode_salt(char *, u_int8_t *, u_int16_t, u_int8_t);
     72 static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t);
     73 static void decode_base64(u_int8_t *, u_int16_t, u_int8_t *);
     74 
     75 char *__bcrypt(const char *, const char *);	/* XXX */
     76 
     77 static char    encrypted[_PASSWORD_LEN];
     78 static char    gsalt[BCRYPT_MAXSALT * 4 / 3 + 1];
     79 static char    error[] = ":";
     80 
     81 const static u_int8_t Base64Code[] =
     82 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
     83 
     84 const static u_int8_t index_64[128] =
     85 {
     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, 255, 255, 255, 255,
     89 	255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
     90 	255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
     91 	56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
     92 	255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
     93 	7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
     94 	17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
     95 	255, 255, 255, 255, 255, 255, 28, 29, 30,
     96 	31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
     97 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
     98 	51, 52, 53, 255, 255, 255, 255, 255
     99 };
    100 #define CHAR64(c)  ( (c) > 127 ? 255 : index_64[(c)])
    101 
    102 static void
    103 decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data)
    104 {
    105 	u_int8_t *bp = buffer;
    106 	u_int8_t *p = data;
    107 	u_int8_t c1, c2, c3, c4;
    108 	while (bp < buffer + len) {
    109 		c1 = CHAR64(*p);
    110 		c2 = CHAR64(*(p + 1));
    111 
    112 		/* Invalid data */
    113 		if (c1 == 255 || c2 == 255)
    114 			break;
    115 
    116 		*bp++ = (c1 << 2) | ((c2 & 0x30) >> 4);
    117 		if (bp >= buffer + len)
    118 			break;
    119 
    120 		c3 = CHAR64(*(p + 2));
    121 		if (c3 == 255)
    122 			break;
    123 
    124 		*bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
    125 		if (bp >= buffer + len)
    126 			break;
    127 
    128 		c4 = CHAR64(*(p + 3));
    129 		if (c4 == 255)
    130 			break;
    131 		*bp++ = ((c3 & 0x03) << 6) | c4;
    132 
    133 		p += 4;
    134 	}
    135 }
    136 
    137 static void
    138 encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr)
    139 {
    140 	salt[0] = '$';
    141 	salt[1] = BCRYPT_VERSION;
    142 	salt[2] = 'a';
    143 	salt[3] = '$';
    144 
    145 	snprintf(salt + 4, 4, "%2.2u$", logr);
    146 
    147 	encode_base64((u_int8_t *) salt + 7, csalt, clen);
    148 }
    149 
    150 /* Generates a salt for this version of crypt.
    151    Since versions may change. Keeping this here
    152    seems sensible.
    153  */
    154 char *
    155 bcrypt_gensalt(u_int8_t log_rounds)
    156 {
    157 	u_int8_t csalt[BCRYPT_MAXSALT];
    158 	u_int16_t i;
    159 	u_int32_t seed = 0;
    160 
    161 	for (i = 0; i < BCRYPT_MAXSALT; i++) {
    162 		if (i % 4 == 0)
    163 			seed = arc4random();
    164 		csalt[i] = seed & 0xff;
    165 		seed = seed >> 8;
    166 	}
    167 
    168 	if (log_rounds < 4)
    169 		log_rounds = 4;
    170 
    171 	encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds);
    172 	return gsalt;
    173 }
    174 
    175 /* We handle $Vers$log2(NumRounds)$salt+passwd$
    176    i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */
    177 
    178 char   *
    179 __bcrypt(key, salt)
    180 	const char   *key;
    181 	const char   *salt;
    182 {
    183 	blf_ctx state;
    184 	u_int32_t rounds, i, k;
    185 	u_int16_t j;
    186 	u_int8_t key_len, salt_len, logr, minor;
    187 	u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt";
    188 	u_int8_t csalt[BCRYPT_MAXSALT];
    189 	u_int32_t cdata[BCRYPT_BLOCKS];
    190 
    191 	/* Discard "$" identifier */
    192 	salt++;
    193 
    194 	if (*salt > BCRYPT_VERSION) {
    195 		/* How do I handle errors ? Return ':' */
    196 		return error;
    197 	}
    198 
    199 	/* Check for minor versions */
    200 	if (salt[1] != '$') {
    201 		 switch (salt[1]) {
    202 		 case 'a':
    203 			 /* 'ab' should not yield the same as 'abab' */
    204 			 minor = salt[1];
    205 			 salt++;
    206 			 break;
    207 		 default:
    208 			 return error;
    209 		 }
    210 	} else
    211 		 minor = 0;
    212 
    213 	/* Discard version + "$" identifier */
    214 	salt += 2;
    215 
    216 	if (salt[2] != '$')
    217 		/* Out of sync with passwd entry */
    218 		return error;
    219 
    220 	/* Computer power doesn't increase linear, 2^x should be fine */
    221 	if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS)
    222 		return error;
    223 
    224 	/* Discard num rounds + "$" identifier */
    225 	salt += 3;
    226 
    227 	if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT)
    228 		return error;
    229 
    230 	/* We dont want the base64 salt but the raw data */
    231 	decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt);
    232 	salt_len = BCRYPT_MAXSALT;
    233 	key_len = strlen(key) + (minor >= 'a' ? 1 : 0);
    234 
    235 	/* Setting up S-Boxes and Subkeys */
    236 	Blowfish_initstate(&state);
    237 	Blowfish_expandstate(&state, csalt, salt_len,
    238 	    (u_int8_t *) key, key_len);
    239 	for (k = 0; k < rounds; k++) {
    240 		Blowfish_expand0state(&state, (u_int8_t *) key, key_len);
    241 		Blowfish_expand0state(&state, csalt, salt_len);
    242 	}
    243 
    244 	/* This can be precomputed later */
    245 	j = 0;
    246 	for (i = 0; i < BCRYPT_BLOCKS; i++)
    247 		cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j);
    248 
    249 	/* Now do the encryption */
    250 	for (k = 0; k < 64; k++)
    251 		blf_enc(&state, cdata, BCRYPT_BLOCKS / 2);
    252 
    253 	for (i = 0; i < BCRYPT_BLOCKS; i++) {
    254 		ciphertext[4 * i + 3] = cdata[i] & 0xff;
    255 		cdata[i] = cdata[i] >> 8;
    256 		ciphertext[4 * i + 2] = cdata[i] & 0xff;
    257 		cdata[i] = cdata[i] >> 8;
    258 		ciphertext[4 * i + 1] = cdata[i] & 0xff;
    259 		cdata[i] = cdata[i] >> 8;
    260 		ciphertext[4 * i + 0] = cdata[i] & 0xff;
    261 	}
    262 
    263 
    264 	i = 0;
    265 	encrypted[i++] = '$';
    266 	encrypted[i++] = BCRYPT_VERSION;
    267 	if (minor)
    268 		encrypted[i++] = minor;
    269 	encrypted[i++] = '$';
    270 
    271 	snprintf(encrypted + i, 4, "%2.2u$", logr);
    272 
    273 	encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT);
    274 	encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
    275 	    4 * BCRYPT_BLOCKS - 1);
    276 	return encrypted;
    277 }
    278 
    279 static void
    280 encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len)
    281 {
    282 	u_int8_t *bp = buffer;
    283 	u_int8_t *p = data;
    284 	u_int8_t c1, c2;
    285 	while (p < data + len) {
    286 		c1 = *p++;
    287 		*bp++ = Base64Code[(c1 >> 2)];
    288 		c1 = (c1 & 0x03) << 4;
    289 		if (p >= data + len) {
    290 			*bp++ = Base64Code[c1];
    291 			break;
    292 		}
    293 		c2 = *p++;
    294 		c1 |= (c2 >> 4) & 0x0f;
    295 		*bp++ = Base64Code[c1];
    296 		c1 = (c2 & 0x0f) << 2;
    297 		if (p >= data + len) {
    298 			*bp++ = Base64Code[c1];
    299 			break;
    300 		}
    301 		c2 = *p++;
    302 		c1 |= (c2 >> 6) & 0x03;
    303 		*bp++ = Base64Code[c1];
    304 		*bp++ = Base64Code[c2 & 0x3f];
    305 	}
    306 	*bp = '\0';
    307 }
    308 #if 0
    309 void
    310 main()
    311 {
    312 	char    blubber[73];
    313 	char    salt[100];
    314 	char   *p;
    315 	salt[0] = '$';
    316 	salt[1] = BCRYPT_VERSION;
    317 	salt[2] = '$';
    318 
    319 	snprintf(salt + 3, 4, "%2.2u$", 5);
    320 
    321 	printf("24 bytes of salt: ");
    322 	fgets(salt + 6, 94, stdin);
    323 	salt[99] = 0;
    324 	printf("72 bytes of password: ");
    325 	fpurge(stdin);
    326 	fgets(blubber, 73, stdin);
    327 	blubber[72] = 0;
    328 
    329 	p = crypt(blubber, salt);
    330 	printf("Passwd entry: %s\n\n", p);
    331 
    332 	p = bcrypt_gensalt(5);
    333 	printf("Generated salt: %s\n", p);
    334 	p = crypt(blubber, p);
    335 	printf("Passwd entry: %s\n", p);
    336 }
    337 #endif
    338