ddn.crypto.kdf.bcrypt_pbkdf

bcrypt_pbkdf: the OpenBSD bcrypt-based password-based key derivation function.

bcrypt_pbkdf is a PBKDF2-style construction around the "bcrypt hash" (EksBlowfish with a fixed cost) rather than HMAC. OpenSSH uses it to derive the cipher key and IV for encrypted openssh-key-v1 private key files; see OpenSSH PROTOCOL.key and bcrypt_pbkdf(3) from OpenBSD.

The bcrypt hash function is derived from the bcrypt password hashing function with the following modifications (quoting the OpenBSD reference):

  1. The input password and salt are preprocessed with SHA-512.
  2. The output length is expanded to 256 bits.
  3. The magic string to be encrypted is lengthened and modified

    to "OxychromaticBlowfishSwatDynamite".

  4. The hash function performs 64 rounds of initial state

    expansion.

One deviation from PBKDF2: instead of outputting key material linearly, the bytes of the per-block outputs are interleaved (striped) across the whole derived key, so assembling any subkey requires computing the entirety of the key material.

The Blowfish core here is a private EksBlowfish implementation, separate from the deprecated legacy Blowfish data-encryption cipher (ddn.crypto.legacy.cipher.blowfish): bcrypt uses Blowfish strictly as a one-way compression schedule over 64-bit blocks, which carries none of the 64-bit-block encryption risk that deprecates legacy Blowfish for data.

Constant-time note: the key schedule and Feistel rounds are branch-free; the only secret-dependent data flow is S-box indexing (memory access), which is inherent to Blowfish and identical in the OpenBSD reference. Secret intermediates are wiped with secureZero on every exit path.

Types 1

bcrypt_pbkdf implementation (OpenBSD bcrypt_pbkdf(3)).

Example:

auto kdf = new BcryptPbkdf(16);
auto keyMaterial = kdf.derive(passphraseBytes, saltBytes, 48);

Methods
ubyte[] derive(const(ubyte)[] secret, const(ubyte)[] salt, size_t length)Derives key material using the constructor's round count.
ubyte[] derive(const(ubyte)[] secret, const(ubyte)[] salt, size_t length, KDFParams params)Derives key material, letting params.iterations override the constructor's round count (zero means "unset").
Constructors
this(size_t rounds)Constructs a BcryptPbkdf with a fixed round count.

Variables 3

enumvarbcryptPbkdfBlock = 32

Hash size of the inner bcrypt hash and the KDF block size in bytes.

enumvarbcryptPbkdfMaxKey = 1024

Maximum derived key length in bytes (32 * 32, the reference bound; OpenSSH only ever needs 48).

enumvarbcryptPbkdfMaxSalt = 1 << 20

Maximum accepted salt length in bytes (the reference bound).