1 /* $NetBSD: bcrypt.c,v 1.10 2011/05/16 10:45:56 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.10 2011/05/16 10:45:56 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 (7 + (BCRYPT_MAXSALT * 4 + 2) / 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 < 4) 179 nrounds = 4; 180 else if (nrounds > 31) 181 nrounds = 31; 182 183 for (i = 0; i < BCRYPT_MAXSALT; i++) { 184 if (i % 4 == 0) 185 seed = arc4random(); 186 csalt[i] = seed & 0xff; 187 seed = seed >> 8; 188 } 189 encode_salt(salt, csalt, BCRYPT_MAXSALT, (u_int8_t)nrounds); 190 return 0; 191 } 192 193 /* Generates a salt for this version of crypt. 194 Since versions may change. Keeping this here 195 seems sensible. 196 XXX: compat. 197 */ 198 char * 199 bcrypt_gensalt(u_int8_t log_rounds) 200 { 201 static char gsalt[BCRYPT_MAXSALTLEN]; 202 char num[10]; 203 204 (void)snprintf(num, sizeof(num), "%d", log_rounds); 205 if (__gensalt_blowfish(gsalt, sizeof(gsalt), num) == -1) 206 return NULL; 207 return gsalt; 208 } 209 210 /* We handle $Vers$log2(NumRounds)$salt+passwd$ 211 i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */ 212 213 char * 214 __bcrypt(key, salt) 215 const char *key; 216 const char *salt; 217 { 218 blf_ctx state; 219 u_int32_t rounds, i, k; 220 u_int16_t j; 221 u_int8_t key_len, salt_len, logr, minor; 222 u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt"; 223 u_int8_t csalt[BCRYPT_MAXSALT]; 224 u_int32_t cdata[BCRYPT_BLOCKS]; 225 int n; 226 227 /* Discard "$" identifier */ 228 salt++; 229 230 if (*salt > BCRYPT_VERSION) { 231 /* How do I handle errors ? Return ':' */ 232 return error; 233 } 234 235 /* Check for minor versions */ 236 if (salt[1] != '$') { 237 switch (salt[1]) { 238 case 'a': 239 /* 'ab' should not yield the same as 'abab' */ 240 minor = salt[1]; 241 salt++; 242 break; 243 default: 244 return error; 245 } 246 } else 247 minor = 0; 248 249 /* Discard version + "$" identifier */ 250 salt += 2; 251 252 if (salt[2] != '$') 253 /* Out of sync with passwd entry */ 254 return error; 255 256 /* Computer power doesn't increase linear, 2^x should be fine */ 257 n = atoi(salt); 258 if (n > 31 || n < 0) 259 return error; 260 logr = (u_int8_t)n; 261 if ((rounds = (u_int32_t) 1 << logr) < BCRYPT_MINROUNDS) 262 return error; 263 264 /* Discard num rounds + "$" identifier */ 265 salt += 3; 266 267 if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT) 268 return error; 269 270 /* We dont want the base64 salt but the raw data */ 271 decode_base64(csalt, BCRYPT_MAXSALT, (const u_int8_t *)salt); 272 salt_len = BCRYPT_MAXSALT; 273 key_len = strlen(key) + (minor >= 'a' ? 1 : 0); 274 275 /* Setting up S-Boxes and Subkeys */ 276 Blowfish_initstate(&state); 277 Blowfish_expandstate(&state, csalt, salt_len, 278 (const u_int8_t *) key, key_len); 279 for (k = 0; k < rounds; k++) { 280 Blowfish_expand0state(&state, (const u_int8_t *) key, key_len); 281 Blowfish_expand0state(&state, csalt, salt_len); 282 } 283 284 /* This can be precomputed later */ 285 j = 0; 286 for (i = 0; i < BCRYPT_BLOCKS; i++) 287 cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j); 288 289 /* Now do the encryption */ 290 for (k = 0; k < 64; k++) 291 blf_enc(&state, cdata, BCRYPT_BLOCKS / 2); 292 293 for (i = 0; i < BCRYPT_BLOCKS; i++) { 294 ciphertext[4 * i + 3] = cdata[i] & 0xff; 295 cdata[i] = cdata[i] >> 8; 296 ciphertext[4 * i + 2] = cdata[i] & 0xff; 297 cdata[i] = cdata[i] >> 8; 298 ciphertext[4 * i + 1] = cdata[i] & 0xff; 299 cdata[i] = cdata[i] >> 8; 300 ciphertext[4 * i + 0] = cdata[i] & 0xff; 301 } 302 303 304 i = 0; 305 encrypted[i++] = '$'; 306 encrypted[i++] = BCRYPT_VERSION; 307 if (minor) 308 encrypted[i++] = minor; 309 encrypted[i++] = '$'; 310 311 snprintf(encrypted + i, 4, "%2.2u$", logr); 312 313 encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT); 314 encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext, 315 4 * BCRYPT_BLOCKS - 1); 316 memset(&state, 0, sizeof(state)); 317 return encrypted; 318 } 319 320 static void 321 encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) 322 { 323 u_int8_t *bp = buffer; 324 u_int8_t *p = data; 325 u_int8_t c1, c2; 326 while (p < data + len) { 327 c1 = *p++; 328 *bp++ = Base64Code[((u_int32_t)c1 >> 2)]; 329 c1 = (c1 & 0x03) << 4; 330 if (p >= data + len) { 331 *bp++ = Base64Code[c1]; 332 break; 333 } 334 c2 = *p++; 335 c1 |= ((u_int32_t)c2 >> 4) & 0x0f; 336 *bp++ = Base64Code[c1]; 337 c1 = (c2 & 0x0f) << 2; 338 if (p >= data + len) { 339 *bp++ = Base64Code[c1]; 340 break; 341 } 342 c2 = *p++; 343 c1 |= ((u_int32_t)c2 >> 6) & 0x03; 344 *bp++ = Base64Code[c1]; 345 *bp++ = Base64Code[c2 & 0x3f]; 346 } 347 *bp = '\0'; 348 } 349 #if 0 350 void 351 main() 352 { 353 char blubber[73]; 354 char salt[100]; 355 char *p; 356 salt[0] = '$'; 357 salt[1] = BCRYPT_VERSION; 358 salt[2] = '$'; 359 360 snprintf(salt + 3, 4, "%2.2u$", 5); 361 362 printf("24 bytes of salt: "); 363 fgets(salt + 6, 94, stdin); 364 salt[99] = 0; 365 printf("72 bytes of password: "); 366 fpurge(stdin); 367 fgets(blubber, 73, stdin); 368 blubber[72] = 0; 369 370 p = crypt(blubber, salt); 371 printf("Passwd entry: %s\n\n", p); 372 373 p = bcrypt_gensalt(5); 374 printf("Generated salt: %s\n", p); 375 p = crypt(blubber, p); 376 printf("Passwd entry: %s\n", p); 377 } 378 #endif 379