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 $ */ 3 4 /* 5 * Copyright 1997 Niels Provos <provos@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@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 #include <sys/cdefs.h> 49 __RCSID("$NetBSD: bcrypt.c,v 1.9 2006/10/27 19:39:11 drochner Exp $"); 50 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <sys/types.h> 54 #include <string.h> 55 #include <pwd.h> 56 #include <errno.h> 57 #include <limits.h> 58 59 #include "crypt.h" 60 #include "blowfish.c" 61 62 /* This implementation is adaptable to current computing power. 63 * You can have up to 2^31 rounds which should be enough for some 64 * time to come. 65 */ 66 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 */ 72 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 *); 76 77 char *__bcrypt(const char *, const char *); /* XXX */ 78 79 static char encrypted[_PASSWORD_LEN]; 80 static char error[] = ":"; 81 82 static const u_int8_t Base64Code[] = 83 "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 84 85 char *bcrypt_gensalt(u_int8_t); 86 87 static const u_int8_t index_64[128] = 88 { 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 102 }; 103 #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)]) 104 105 static void 106 decode_base64(u_int8_t *buffer, u_int16_t len, const u_int8_t *data) 107 { 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) { 112 c1 = CHAR64(*p); 113 c2 = CHAR64(*(p + 1)); 114 115 /* Invalid data */ 116 if (c1 == 255 || c2 == 255) 117 break; 118 119 *bp++ = ((u_int32_t)c1 << 2) | (((u_int32_t)c2 & 0x30) >> 4); 120 if (bp >= buffer + len) 121 break; 122 123 c3 = CHAR64(*(p + 2)); 124 if (c3 == 255) 125 break; 126 127 *bp++ = (((u_int32_t)c2 & 0x0f) << 4) | (((uint32_t)c3 & 0x3c) >> 2); 128 if (bp >= buffer + len) 129 break; 130 131 c4 = CHAR64(*(p + 3)); 132 if (c4 == 255) 133 break; 134 *bp++ = ((c3 & 0x03) << 6) | c4; 135 136 p += 4; 137 } 138 } 139 140 static void 141 encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr) 142 { 143 salt[0] = '$'; 144 salt[1] = BCRYPT_VERSION; 145 salt[2] = 'a'; 146 salt[3] = '$'; 147 148 snprintf(salt + 4, 4, "%2.2u$", logr); 149 150 encode_base64((u_int8_t *) salt + 7, csalt, clen); 151 } 152 153 int 154 __gensalt_blowfish(char *salt, size_t saltlen, const char *option) 155 { 156 size_t i; 157 u_int32_t seed = 0; 158 u_int8_t csalt[BCRYPT_MAXSALT]; 159 unsigned long nrounds; 160 char *ep; 161 162 if (saltlen < BCRYPT_MAXSALTLEN) { 163 errno = ENOSPC; 164 return -1; 165 } 166 if (option == NULL) { 167 errno = EINVAL; 168 return -1; 169 } 170 nrounds = strtoul(option, &ep, 0); 171 if (option == ep || *ep) { 172 errno = EINVAL; 173 return -1; 174 } 175 if (errno == ERANGE && nrounds == ULONG_MAX) 176 return -1; 177 178 if (nrounds > 255) { 179 errno = EINVAL; 180 return -1; 181 } 182 183 if (nrounds < 4) 184 nrounds = 4; 185 186 for (i = 0; i < BCRYPT_MAXSALT; i++) { 187 if (i % 4 == 0) 188 seed = arc4random(); 189 csalt[i] = seed & 0xff; 190 seed = seed >> 8; 191 } 192 encode_salt(salt, csalt, BCRYPT_MAXSALT, (u_int8_t)nrounds); 193 return 0; 194 } 195 196 /* Generates a salt for this version of crypt. 197 Since versions may change. Keeping this here 198 seems sensible. 199 XXX: compat. 200 */ 201 char * 202 bcrypt_gensalt(u_int8_t log_rounds) 203 { 204 static char gsalt[BCRYPT_MAXSALTLEN]; 205 char num[10]; 206 207 (void)snprintf(num, sizeof(num), "%d", log_rounds); 208 if (__gensalt_blowfish(gsalt, sizeof(gsalt), num) == -1) 209 return NULL; 210 return gsalt; 211 } 212 213 /* We handle $Vers$log2(NumRounds)$salt+passwd$ 214 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */ 215 216 char * 217 __bcrypt(key, salt) 218 const char *key; 219 const char *salt; 220 { 221 blf_ctx state; 222 u_int32_t rounds, i, k; 223 u_int16_t j; 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]; 228 229 /* Discard "$" identifier */ 230 salt++; 231 232 if (*salt > BCRYPT_VERSION) { 233 /* How do I handle errors ? Return ':' */ 234 return error; 235 } 236 237 /* Check for minor versions */ 238 if (salt[1] != '$') { 239 switch (salt[1]) { 240 case 'a': 241 /* 'ab' should not yield the same as 'abab' */ 242 minor = salt[1]; 243 salt++; 244 break; 245 default: 246 return error; 247 } 248 } else 249 minor = 0; 250 251 /* Discard version + "$" identifier */ 252 salt += 2; 253 254 if (salt[2] != '$') 255 /* Out of sync with passwd entry */ 256 return error; 257 258 /* Computer power doesn't increase linear, 2^x should be fine */ 259 if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS) 260 return error; 261 262 /* Discard num rounds + "$" identifier */ 263 salt += 3; 264 265 if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT) 266 return error; 267 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); 272 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); 280 } 281 282 /* This can be precomputed later */ 283 j = 0; 284 for (i = 0; i < BCRYPT_BLOCKS; i++) 285 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j); 286 287 /* Now do the encryption */ 288 for (k = 0; k < 64; k++) 289 blf_enc(&state, cdata, BCRYPT_BLOCKS / 2); 290 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; 299 } 300 301 302 i = 0; 303 encrypted[i++] = '$'; 304 encrypted[i++] = BCRYPT_VERSION; 305 if (minor) 306 encrypted[i++] = minor; 307 encrypted[i++] = '$'; 308 309 snprintf(encrypted + i, 4, "%2.2u$", logr); 310 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); 314 return encrypted; 315 } 316 317 static void 318 encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) 319 { 320 u_int8_t *bp = buffer; 321 u_int8_t *p = data; 322 u_int8_t c1, c2; 323 while (p < data + len) { 324 c1 = *p++; 325 *bp++ = Base64Code[((u_int32_t)c1 >> 2)]; 326 c1 = (c1 & 0x03) << 4; 327 if (p >= data + len) { 328 *bp++ = Base64Code[c1]; 329 break; 330 } 331 c2 = *p++; 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]; 337 break; 338 } 339 c2 = *p++; 340 c1 |= ((u_int32_t)c2 >> 6) & 0x03; 341 *bp++ = Base64Code[c1]; 342 *bp++ = Base64Code[c2 & 0x3f]; 343 } 344 *bp = '\0'; 345 } 346 #if 0 347 void 348 main() 349 { 350 char blubber[73]; 351 char salt[100]; 352 char *p; 353 salt[0] = '$'; 354 salt[1] = BCRYPT_VERSION; 355 salt[2] = '$'; 356 357 snprintf(salt + 3, 4, "%2.2u$", 5); 358 359 printf("24 bytes of salt: "); 360 fgets(salt + 6, 94, stdin); 361 salt[99] = 0; 362 printf("72 bytes of password: "); 363 fpurge(stdin); 364 fgets(blubber, 73, stdin); 365 blubber[72] = 0; 366 367 p = crypt(blubber, salt); 368 printf("Passwd entry: %s\n\n", p); 369 370 p = bcrypt_gensalt(5); 371 printf("Generated salt: %s\n", p); 372 p = crypt(blubber, p); 373 printf("Passwd entry: %s\n", p); 374 } 375 #endif 376