/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* $OpenBSD: bcrypt.c,v 1.16 2002/02/19 19:39:36 millert Exp $ */
/*
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Niels Provos.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This password hashing algorithm was designed by David Mazieres
* <dm@lcs.mit.edu> and works as follows:
*
* 1. state := InitState ()
* 2. state := ExpandKey (state, salt, password) 3.
* REPEAT rounds:
* state := ExpandKey (state, 0, salt)
* state := ExpandKey(state, 0, password)
* 4. ctext := "OrpheanBeholderScryDoubt"
* 5. REPEAT 64:
* ctext := Encrypt_ECB (state, ctext);
* 6. RETURN Concatenate (salt, ctext);
*
*/
#if 0
#include <stdio.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <blf.h>
extern uint32_t arc4random();
/*
* This implementation is adaptable to current computing power.
* You can have up to 2^31 rounds which should be enough for some
* time to come.
*/
char *bcrypt_gensalt(uint8_t);
"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 0, 1, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63, 255, 255,
255, 255, 255, 255, 255, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
255, 255, 255, 255, 255, 255, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 255, 255, 255, 255, 255
};
static void
{
/* Invalid data */
break;
break;
if (c3 == 255)
break;
break;
if (c4 == 255)
break;
p += 4;
}
}
static void
{
salt[0] = '$';
}
/*
* Generates a salt for this version of crypt.
* Since versions may change. Keeping this here
* seems sensible.
*/
char *
{
uint16_t i;
for (i = 0; i < BCRYPT_MAXSALT; i++) {
if (i % 4 == 0)
seed = arc4random();
}
if (log_rounds < BCRYPT_MINLOGROUNDS)
else if (log_rounds > BCRYPT_MAXLOGROUNDS)
return (gsalt);
}
/*
* We handle $Vers$log2(NumRounds)$salt+passwd$
* i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou
*/
char *
const char *key;
const char *salt;
{
uint16_t j;
/* Discard "$" identifier */
salt++;
if (*salt > BCRYPT_VERSION) {
/* How do I handle errors ? Return ':' */
return (error);
}
/* Check for minor versions */
switch (salt[1]) {
case 'a': /* 'ab' should not yield the same as 'abab' */
case 'b': /* cap input length at 72 bytes */
salt++;
break;
default:
return (error);
}
} else
minor = 0;
/* Discard version + "$" identifier */
salt += 2;
/* Out of sync with passwd entry */
return (error);
return (error);
return (error);
/* Computer power doesn't increase linear, 2^x should be fine */
/* Discard num rounds + "$" identifier */
salt += 3;
return (error);
/* We dont want the base64 salt but the raw data */
if (minor <= 'a')
else {
/*
* strlen() returns a size_t, but the function calls
* below result in implicit casts to a narrower integer
* type, so cap key_len at the actual maximum supported
* length here to avoid integer wraparound
*/
if (key_len > 72)
key_len = 72;
key_len++; /* include the NUL */
}
/* Setting up S-Boxes and Subkeys */
for (k = 0; k < rounds; k++) {
}
/* This can be precomputed later */
j = 0;
for (i = 0; i < BCRYPT_BLOCKS; i++)
4 * BCRYPT_BLOCKS, &j);
/* Now do the encryption */
for (k = 0; k < 64; k++)
for (i = 0; i < BCRYPT_BLOCKS; i++) {
}
i = 0;
encrypted[i++] = '$';
encrypted[i++] = BCRYPT_VERSION;
if (minor)
encrypted[i++] = '$';
return (encrypted);
}
static void
{
c1 = *p++;
break;
}
c2 = *p++;
break;
}
c2 = *p++;
}
*bp = '\0';
}
#if 0
void
main()
{
char blubber[73];
char salt[100];
char *p;
salt[0] = '$';
printf("24 bytes of salt: ");
salt[99] = 0;
printf("72 bytes of password: ");
blubber[72] = 0;
printf("Passwd entry: %s\n\n", p);
p = bcrypt_gensalt(5);
printf("Generated salt: %s\n", p);
printf("Passwd entry: %s\n", p);
}
#endif