Bcrypt
bcrypt is a password-hashing function designed by Niels Provos and David Mazières, based on the Blowfish cipher and presented at USENIX in 1999. Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with increasing computation power.
The bcrypt function is the default password hash algorithm for OpenBSD and other systems including some Linux distributions such as SUSE Linux.
There are implementations of bcrypt for C, C++, C#, Elixir, Go, Java, JavaScript, Perl, PHP, Python, Ruby, and other languages.
Background
Blowfish is notable among block ciphers for its expensive key setup phase. It starts off with subkeys in a standard state, then uses this state to perform a block encryption using part of the key, and uses the result of that encryption to replace some of the subkeys. Then it uses this modified state to encrypt another part of the key, and uses the result to replace more of the subkeys. It proceeds in this fashion, using a progressively modified state to hash the key and replace bits of state, until all subkeys have been set.Provos and Mazières took advantage of this, and took it further. They developed a new key setup algorithm for Blowfish, dubbing the resulting cipher "Eksblowfish". The key setup begins with a modified form of the standard Blowfish key setup, in which both the salt and password are used to set all subkeys. There are then a number of rounds in which the standard Blowfish keying algorithm is applied, using alternatively the salt and the password as the key, each round starting with the subkey state from the previous round. In theory, this is no stronger than the standard Blowfish key schedule, but the number of rekeying rounds is configurable; this process can therefore be made arbitrarily slow, which helps deter brute-force attacks upon the hash or salt.
Description
A bcrypt hash string is of the form:$2b$$
For example:
$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy
\__/\/ \____________________/\_____________________________/
Alg Cost Salt Hash
Where:
-
$2a$
: The hash algorithm identifier -
10
: Cost factor -
N9qo8uLOickgx2ZMRZoMye
: 16-byte salt, base64 encoded to 22 characters -
IjZAgcfl7p92ldGxad68LJZdL17lhWy
: 24-byte hash, base64 encoded to 31 characters
The rest of the hash string includes the cost parameter, a 128-bit salt, and 184 bits of the resulting hash value. The Radix-64 encoding uses the unix/crypt alphabet, and is not 'standard' Base-64. The cost parameter specifies a key expansion iteration count as a power of two, which is an input to the crypt algorithm.
For example, the shadow password record
$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy
specifies a cost parameter of 10, indicating 210 key expansion rounds. The salt is N9qo8uLOickgx2ZMRZoMye
and the resulting hash is IjZAgcfl7p92ldGxad68LJZdL17lhWy
. Per standard practice, the user's password itself is not stored.Versioning history
$2$The original Bcrypt specification defined a prefix of
$2$
. This follows the Modular Crypt Format format used when storing passwords in the OpenBSD password file:-
$1$
: MD5-based crypt -
$2$
: Blowfish-based crypt -
$sha1$
: SHA-1-based crypt -
$5$
: SHA-256-based crypt -
$6$
: SHA-512-based crypt
The original specification did not define how to handle non-ASCII character, nor how to handle a null terminator. The specification was revised to specify that when hashing strings:
- the string must be UTF-8 encoded
- the null terminator must be included
$2a$
$2x$, $2y$
In June 2011, a bug was discovered in crypt_blowfish, a PHP implementation of BCrypt. It was mis-handling characters with the 8th bit set. They suggested that system administrators update their existing password database, replacing
$2a$
with $2x$
, to indicate that those hashes are bad. They also suggested the idea of having crypt_blowfish emit $2y$
for hashes generated by the fixed algorithm.Nobody else, including canonical OpenBSD, adopted the idea of 2x/2y. This version marker change was limited to crypt_blowfish.
$2b$
A bug was discovered in the OpenBSD implementation of bcrypt. They were storing the length of their strings in an unsigned char. If a password was longer than 255 characters, it would overflow and wrap at 255.
BCrypt was created for OpenBSD. When they had a bug in their library, they decided to bump the version number.
Algorithm
The bcrypt algorithm is the result of encrypting the text "OrpheanBeholderScryDoubt" 64 times using Blowfish. In bcrypt the usual Blowfish key setup function is replaced with an expensive key setup function:Function bcrypt
Input:
cost: Number log2. e.g. 12 > 212 = 4,096 iterations
salt: array of Bytes random salt
password: array of Bytes UTF-8 encoded password
Output:
hash: array of Bytes
//Initialize Blowfish state with expensive key setup algorithm
//P: array of 18 subkeys
//S: Four substitution boxes, S0...S3. Each S-Box is 1,024 bytes
P, S EksBlowfishSetup
//Repeatedly encrypt the text "OrpheanBeholderScryDoubt" 64 times
ctext "OrpheanBeholderScryDoubt" //24 bytes > three 64-bit blocks
repeat
ctext EncryptECB //encrypt using standard Blowfish in ECB mode
//24-byte ctext is resulting password hash
return Concatenate
Expensive key setup
The bcrypt algorithm depends heavily on its "Eksblowfish" key setup algorithm, which runs as follows:Function EksBlowfishSetup
Input:
cost: Number log2. e.g. 12 > 212 = 4,096 iterations
salt: array of Bytes random salt
password: array of Bytes UTF-8 encoded password
Output:
state: opaque BlowFish state structure
P, S InitialState //P, and S are initialized with digits of pi
P, S ExpandKey
repeat
P, S ExpandKey
P, S ExpandKey
return P, S
InitialState works as in the original Blowfish algorithm, populating the P-array and S-box entries with the fractional part of in hexadecimal.
Expand key
The ExpandKey function does the following:Function ExpandKey
Input:
P: UInt32 Array of 18 subkeys
S: S0...S3 UInt32 Four 1 KB SBoxes
salt: array of Bytes random salt
password: array of Bytes UTF-8 encoded password
Output:
state: opaque BlowFish state structure
//Mix password into the internal P-array of state
for n 1 to 18 do
Pn Pn xor password //treat the password as cyclic
//Encrypt state using the lower 8 bytes of salt, and store the 8 byte result in P1|P2
block Encrypt
P1 block //lower 32-bits
P2 block //upper 32-bits
//Continue encrypting state with salt, and storing results in remaining P-array
for n 2 to 9 do
block Encrypt //encrypt using the current key schedule and treat the salt as cyclic
P2n-1 block //lower 32-bits
P2n block //upper 32-bits
//Mix encrypted state into the internal S-boxes of state
for i 1 to 4 do
for n 0 to 127 do
block Encrypt //as above
Si block //lower 32-bits
Si block //upper 32-bits
return state
Hence,
ExpandKey
is the same as regular Blowfish key schedule since all XORs with the all-zero salt value are ineffectual. ExpandKey
is similar, but uses the salt as a 128-bit key.User input
Many implementations of bcrypt truncate the password to the first 72 bytes, following the OpenBSD implementation.The mathematical algorithm itself requires initialization with 18 32-bit subkeys. The original specification of bcrypt does not mandate any one particular method for mapping text-based passwords from userland into numeric values for the algorithm. One brief comment in the text mentions, but does not mandate, the possibility of simply using the ASCII encoded value of a character string: "Finally, the key argument is a secret encryption key, which can be a user-chosen password of up to 56 bytes."
Note that the quote above mentions passwords "up to 56 bytes" even though the algorithm itself makes use of a 72 byte initial value. Although Provos and Mazières do not state the reason for the shorter restriction, they may have been motivated by the following statement from Bruce Schneier's original specification of Blowfish, "The 448 limit on the key size ensures that the every bit of every subkey depends on every bit of the key."
Implementations have varied in their approach of converting passwords into initial numeric values, including sometimes reducing the strength of passwords containing non-ASCII characters.