1 /* $OpenBSD: bcrypt.c,v 1.6 1997/07/01 20:12:43 provos Exp $ */ 2 /* 3 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Niels Provos. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* This password hashing algorithm was designed by David Mazieres 33 * <dm@lcs.mit.edu> and works as follows: 34 * 35 * 1. state := InitState () 36 * 2. state := ExpandKey (state, salt, password) 3. 37 * REPEAT rounds: 38 * state := ExpandKey (state, 0, salt) 39 * state := ExpandKey(state, 0, password) 40 * 4. ctext := "OrpheanBeholderScryDoubt" 41 * 5. REPEAT 64: 42 * ctext := Encrypt_ECB (state, ctext); 43 * 6. RETURN Concatenate (salt, ctext); 44 * 45 */ 46 47 #ifdef TEST 48 #include <stdio.h> 49 #endif 50 51 #include <stdlib.h> 52 #include <sys/types.h> 53 #include <string.h> 54 #include <pwd.h> 55 #include <blf.h> 56 57 /* This implementation is adaptable to current computing power. 58 * You can have up to 2^31 rounds which should be enough for some 59 * time to come. 60 */ 61 62 #define BCRYPT_VERSION '2' 63 #define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */ 64 #define BCRYPT_BLOCKS 6 /* Ciphertext blocks */ 65 #define BCRYPT_MINROUNDS 16 /* we have log2(rounds) in salt */ 66 67 char *bcrypt_gensalt __P((u_int8_t)); 68 69 static void encode_salt __P((char *, u_int8_t *, u_int16_t, u_int8_t)); 70 static void encode_base64 __P((u_int8_t *, u_int8_t *, u_int16_t)); 71 static void decode_base64 __P((u_int8_t *, u_int16_t, u_int8_t *)); 72 73 static char encrypted[_PASSWORD_LEN]; 74 static char gsalt[BCRYPT_MAXSALT * 4 / 3 + 1]; 75 static char error[] = ":"; 76 77 const static u_int8_t Base64Code[] = 78 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 79 80 const static u_int8_t index_64[128] = 81 { 82 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 83 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 84 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 85 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 86 255, 255, 255, 255, 255, 255, 0, 1, 54, 55, 87 56, 57, 58, 59, 60, 61, 62, 63, 255, 255, 88 255, 255, 255, 255, 255, 2, 3, 4, 5, 6, 89 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 90 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 91 255, 255, 255, 255, 255, 255, 28, 29, 30, 92 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 93 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 94 51, 52, 53, 255, 255, 255, 255, 255 95 }; 96 #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)]) 97 98 #if __STDC__ 99 static void 100 decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data) 101 #else 102 static void 103 decode_base64(buffer, len, data) 104 u_int8_t *buffer; 105 u_int16_t len; 106 u_int8_t *data; 107 #endif 108 { 109 u_int8_t *bp = buffer; 110 u_int8_t *p = data; 111 u_int8_t c1, c2, c3, c4; 112 while (bp < buffer + len) { 113 c1 = CHAR64(*p); 114 c2 = CHAR64(*(p + 1)); 115 116 /* Invalid data */ 117 if (c1 == 255 || c2 == 255) 118 break; 119 120 *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4); 121 if (bp >= buffer + len) 122 break; 123 124 c3 = CHAR64(*(p + 2)); 125 if (c3 == 255) 126 break; 127 128 *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2); 129 if (bp >= buffer + len) 130 break; 131 132 c4 = CHAR64(*(p + 3)); 133 if (c4 == 255) 134 break; 135 *bp++ = ((c3 & 0x03) << 6) | c4; 136 137 p += 4; 138 } 139 } 140 141 #if __STDC__ 142 static void 143 encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr) 144 #else 145 static void 146 encode_salt(salt, csalt, clen, logr) 147 char *salt; 148 u_int8_t *csalt; 149 u_int16_t clen; 150 u_int8_t logr; 151 #endif 152 { 153 salt[0] = '$'; 154 salt[1] = BCRYPT_VERSION; 155 salt[2] = 'a'; 156 salt[3] = '$'; 157 158 snprintf(salt + 4, 4, "%2.2u$", logr); 159 160 encode_base64((u_int8_t *) salt + 7, csalt, clen); 161 } 162 /* Generates a salt for this version of crypt. 163 Since versions may change. Keeping this here 164 seems sensible. 165 */ 166 167 #if __STDC__ 168 char * 169 bcrypt_gensalt(u_int8_t log_rounds) 170 #else 171 char * 172 bcrypt_gensalt(log_rounds) 173 u_int8_t log_rounds; 174 #endif 175 { 176 u_int8_t csalt[BCRYPT_MAXSALT]; 177 u_int16_t i; 178 u_int32_t seed = 0; 179 180 for (i = 0; i < BCRYPT_MAXSALT; i++) { 181 if (i % 4 == 0) 182 seed = arc4random(); 183 csalt[i] = seed & 0xff; 184 seed = seed >> 8; 185 } 186 187 if (log_rounds < 4) 188 log_rounds = 4; 189 190 encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds); 191 return gsalt; 192 } 193 /* We handle $Vers$log2(NumRounds)$salt+passwd$ 194 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */ 195 196 char * 197 bcrypt(key, salt) 198 const char *key; 199 const char *salt; 200 { 201 blf_ctx state; 202 u_int32_t rounds, i, k; 203 u_int16_t j; 204 u_int8_t key_len, salt_len, logr, minor; 205 u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt"; 206 u_int8_t csalt[BCRYPT_MAXSALT]; 207 u_int32_t cdata[BCRYPT_BLOCKS]; 208 209 /* Discard "$" identifier */ 210 salt++; 211 212 if (*salt > BCRYPT_VERSION) { 213 /* How do I handle errors ? Return ':' */ 214 return error; 215 } 216 217 /* Check for minor versions */ 218 if (salt[1] != '$') { 219 switch(salt[1]) { 220 case 'a': 221 /* 'ab' should not yield the same as 'abab' */ 222 minor = salt[1]; 223 salt++; 224 break; 225 default: 226 return error; 227 } 228 } else 229 minor = 0; 230 231 /* Discard version + "$" identifier */ 232 salt += 2; 233 234 if (salt[2] != '$') 235 /* Out of sync with passwd entry */ 236 return error; 237 238 /* Computer power doesnt increase linear, 2^x should be fine */ 239 if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS) 240 return error; 241 242 /* Discard num rounds + "$" identifier */ 243 salt += 3; 244 245 /* We dont want the base64 salt but the raw data */ 246 decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt); 247 salt_len = BCRYPT_MAXSALT; 248 key_len = strlen(key) + (minor >= 'a' ? 1 : 0); 249 250 /* Setting up S-Boxes and Subkeys */ 251 Blowfish_initstate(&state); 252 Blowfish_expandstate(&state, csalt, salt_len, 253 (u_int8_t *) key, key_len); 254 for (k = 0; k < rounds; k++) { 255 Blowfish_expand0state(&state, (u_int8_t *) key, key_len); 256 Blowfish_expand0state(&state, csalt, salt_len); 257 } 258 259 /* This can be precomputed later */ 260 j = 0; 261 for (i = 0; i < BCRYPT_BLOCKS; i++) 262 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j); 263 264 /* Now do the encryption */ 265 for (k = 0; k < 64; k++) 266 blf_enc(&state, cdata, BCRYPT_BLOCKS / 2); 267 268 for (i = 0; i < BCRYPT_BLOCKS; i++) { 269 ciphertext[4 * i + 3] = cdata[i] & 0xff; 270 cdata[i] = cdata[i] >> 8; 271 ciphertext[4 * i + 2] = cdata[i] & 0xff; 272 cdata[i] = cdata[i] >> 8; 273 ciphertext[4 * i + 1] = cdata[i] & 0xff; 274 cdata[i] = cdata[i] >> 8; 275 ciphertext[4 * i + 0] = cdata[i] & 0xff; 276 } 277 278 279 i = 0; 280 encrypted[i++] = '$'; 281 encrypted[i++] = BCRYPT_VERSION; 282 if (minor) 283 encrypted[i++] = minor; 284 encrypted[i++] = '$'; 285 286 snprintf(encrypted + i, 4, "%2.2u$", logr); 287 288 encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT); 289 encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext, 290 4 * BCRYPT_BLOCKS); 291 return encrypted; 292 } 293 294 #if __STDC__ 295 static void 296 encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) 297 #else 298 static void 299 encode_base64(buffer, data, len) 300 u_int8_t *buffer; 301 u_int8_t *data; 302 u_int16_t len; 303 #endif 304 { 305 u_int8_t *bp = buffer; 306 u_int8_t *p = data; 307 u_int8_t c1, c2; 308 while (p < data + len) { 309 c1 = *p++; 310 *bp++ = Base64Code[(c1 >> 2)]; 311 c1 = (c1 & 0x03) << 4; 312 c2 = *p++; 313 if (p >= data + len) { 314 *bp++ = Base64Code[c1]; 315 break; 316 } 317 c1 |= (c2 >> 4) & 0x0f; 318 *bp++ = Base64Code[c1]; 319 c1 = (c2 & 0x0f) << 2; 320 c2 = *p++; 321 if (p >= data + len) { 322 *bp++ = Base64Code[c1]; 323 break; 324 } 325 c1 |= (c2 >> 6) & 0x03; 326 *bp++ = Base64Code[c1]; 327 *bp++ = Base64Code[c2 & 0x3f]; 328 } 329 *bp = '\0'; 330 } 331 #ifdef TEST 332 void 333 main() 334 { 335 char blubber[73]; 336 char salt[100]; 337 char *p; 338 salt[0] = '$'; 339 salt[1] = BCRYPT_VERSION; 340 salt[2] = '$'; 341 342 snprintf(salt + 3, 4, "%2.2u$", 5); 343 344 printf("24 bytes of salt: "); 345 fgets(salt + 6, 94, stdin); 346 salt[99] = 0; 347 printf("72 bytes of password: "); 348 fpurge(stdin); 349 fgets(blubber, 73, stdin); 350 blubber[72] = 0; 351 352 p = crypt(blubber, salt); 353 printf("Passwd entry: %s\n\n", p); 354 355 p = bcrypt_gensalt(5); 356 printf("Generated salt: %s\n", p); 357 p = crypt(blubber, p); 358 printf("Passwd entry: %s\n", p); 359 } 360 #endif 361