Botan 3.6.1
Crypto and TLS for C&
Botan::Bcrypt_PBKDF Class Referencefinal

#include <bcrypt_pbkdf.h>

Inheritance diagram for Botan::Bcrypt_PBKDF:
Botan::PasswordHash

Public Member Functions

 Bcrypt_PBKDF (const Bcrypt_PBKDF &other)=default
 
 Bcrypt_PBKDF (size_t iterations)
 
void derive_key (uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len) const override
 
virtual void derive_key (uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len, const uint8_t ad[], size_t ad_len, const uint8_t key[], size_t key_len) const
 
void hash (std::span< uint8_t > out, std::string_view password, std::span< const uint8_t > salt) const
 
void hash (std::span< uint8_t > out, std::string_view password, std::span< const uint8_t > salt, std::span< const uint8_t > associated_data, std::span< const uint8_t > key) const
 
size_t iterations () const override
 
size_t memory_param () const override
 
Bcrypt_PBKDFoperator= (const Bcrypt_PBKDF &)=default
 
size_t parallelism () const override
 
virtual bool supports_associated_data () const
 
virtual bool supports_keyed_operation () const
 
std::string to_string () const override
 
size_t total_memory_usage () const override
 

Detailed Description

Bcrypt-PBKDF key derivation function

Definition at line 20 of file bcrypt_pbkdf.h.

Constructor & Destructor Documentation

◆ Bcrypt_PBKDF() [1/2]

Botan::Bcrypt_PBKDF::Bcrypt_PBKDF ( size_t iterations)

Definition at line 17 of file bcrypt_pbkdf.cpp.

17 : m_iterations(iterations) {
18 BOTAN_ARG_CHECK(m_iterations > 0, "Invalid Bcrypt-PBKDF iterations");
19}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
size_t iterations() const override

References BOTAN_ARG_CHECK.

◆ Bcrypt_PBKDF() [2/2]

Botan::Bcrypt_PBKDF::Bcrypt_PBKDF ( const Bcrypt_PBKDF & other)
default

Member Function Documentation

◆ derive_key() [1/2]

void Botan::Bcrypt_PBKDF::derive_key ( uint8_t out[],
size_t out_len,
const char * password,
size_t password_len,
const uint8_t salt[],
size_t salt_len ) const
overridevirtual

Derive a new key under the current Bcrypt-PBKDF parameter set

Implements Botan::PasswordHash.

Definition at line 120 of file bcrypt_pbkdf.cpp.

125 {
126 // No output desired, so we are all done already...
127 if(output_len == 0) {
128 return;
129 }
130
131 BOTAN_ARG_CHECK(output_len <= 10 * 1024 * 1024, "Too much output for Bcrypt PBKDF");
132
133 const size_t BCRYPT_BLOCK_SIZE = 32;
134 const size_t blocks = (output_len + BCRYPT_BLOCK_SIZE - 1) / BCRYPT_BLOCK_SIZE;
135
136 auto sha512 = HashFunction::create_or_throw("SHA-512");
137 const auto pass_hash = sha512->process(reinterpret_cast<const uint8_t*>(password), password_len);
138
139 secure_vector<uint8_t> salt_hash(sha512->output_length());
140
141 Blowfish blowfish;
142 secure_vector<uint8_t> out(BCRYPT_BLOCK_SIZE);
143 secure_vector<uint8_t> tmp(BCRYPT_BLOCK_SIZE);
144
145 for(size_t block = 0; block != blocks; ++block) {
146 clear_mem(out.data(), out.size());
147
148 sha512->update(salt, salt_len);
149 sha512->update_be(static_cast<uint32_t>(block + 1));
150 sha512->final(salt_hash.data());
151
152 bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
153
154 for(size_t r = 1; r < m_iterations; ++r) {
155 // Next salt is H(prev_output)
156 sha512->update(tmp);
157 sha512->final(salt_hash.data());
158
159 bcrypt_round(blowfish, pass_hash, salt_hash, out, tmp);
160 }
161
162 for(size_t i = 0; i != BCRYPT_BLOCK_SIZE; ++i) {
163 const size_t dest = i * blocks + block;
164 if(dest < output_len) {
165 output[dest] = out[i];
166 }
167 }
168 }
169}
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:298
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:120

References BOTAN_ARG_CHECK, Botan::clear_mem(), and Botan::HashFunction::create_or_throw().

◆ derive_key() [2/2]

void Botan::PasswordHash::derive_key ( uint8_t out[],
size_t out_len,
const char * password,
size_t password_len,
const uint8_t salt[],
size_t salt_len,
const uint8_t ad[],
size_t ad_len,
const uint8_t key[],
size_t key_len ) const
virtualinherited

Derive a key from a password plus additional data and/or a secret key

Currently this is only supported for Argon2. Using a non-empty AD or key with other algorithms will cause a Not_Implemented exception.

Parameters
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passwordthe password to derive the key from
password_lenthe length of password in bytes
salta randomly chosen salt
salt_lenlength of salt in bytes
adsome additional data
ad_lenlength of ad in bytes
keya secret key
key_lenlength of key in bytes

This function is const, but is not thread safe. Different threads should either use unique objects, or serialize all access.

Reimplemented in Botan::Argon2.

Definition at line 34 of file pwdhash.cpp.

43 {
44 BOTAN_UNUSED(ad, key);
45
46 if(ad_len == 0 && key_len == 0) {
47 return this->derive_key(out, out_len, password, password_len, salt, salt_len);
48 } else {
49 throw Not_Implemented("PasswordHash " + this->to_string() + " does not support AD or key");
50 }
51}
#define BOTAN_UNUSED
Definition assert.h:118
virtual void derive_key(uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len) const =0
virtual std::string to_string() const =0

References BOTAN_UNUSED, Botan::PasswordHash::derive_key(), and Botan::PasswordHash::to_string().

◆ hash() [1/2]

void Botan::PasswordHash::hash ( std::span< uint8_t > out,
std::string_view password,
std::span< const uint8_t > salt ) const
inlineinherited

Hash a password into a bitstring

Derive a key from the specified password and salt, placing it into out.

Parameters
outa span where the derived key will be placed
passwordthe password to derive the key from
salta randomly chosen salt

This function is const, but is not thread safe. Different threads should either use unique objects, or serialize all access.

Definition at line 84 of file pwdhash.h.

84 {
85 this->derive_key(out.data(), out.size(), password.data(), password.size(), salt.data(), salt.size());
86 }

◆ hash() [2/2]

void Botan::PasswordHash::hash ( std::span< uint8_t > out,
std::string_view password,
std::span< const uint8_t > salt,
std::span< const uint8_t > associated_data,
std::span< const uint8_t > key ) const
inlineinherited

Hash a password into a bitstring

Derive a key from the specified password, salt, associated_data, and secret key, placing it into out. The associated_data and key are both allowed to be empty. Currently non-empty AD/key is only supported with Argon2.

Parameters
outa span where the derived key will be placed
passwordthe password to derive the key from
salta randomly chosen salt
associated_datasome additional data
keya secret key

This function is const, but is not thread safe. Different threads should either use unique objects, or serialize all access.

Definition at line 105 of file pwdhash.h.

109 {
110 this->derive_key(out.data(),
111 out.size(),
112 password.data(),
113 password.size(),
114 salt.data(),
115 salt.size(),
116 associated_data.data(),
117 associated_data.size(),
118 key.data(),
119 key.size());
120 }

◆ iterations()

size_t Botan::Bcrypt_PBKDF::iterations ( ) const
inlineoverridevirtual

Most password hashes have some notion of iterations.

Implements Botan::PasswordHash.

Definition at line 39 of file bcrypt_pbkdf.h.

39{ return m_iterations; }

◆ memory_param()

size_t Botan::Bcrypt_PBKDF::memory_param ( ) const
inlineoverridevirtual

Some password hashing algorithms have a parameter which controls how much memory is used. If not supported by some algorithm, returns 0.

Reimplemented from Botan::PasswordHash.

Definition at line 43 of file bcrypt_pbkdf.h.

43{ return 0; }

◆ operator=()

Bcrypt_PBKDF & Botan::Bcrypt_PBKDF::operator= ( const Bcrypt_PBKDF & )
default

◆ parallelism()

size_t Botan::Bcrypt_PBKDF::parallelism ( ) const
inlineoverridevirtual

Some password hashing algorithms have a parallelism parameter. If the algorithm does not support this notion, then the function returns zero. This allows distinguishing between a password hash which just does not support parallel operation, vs one that does support parallel operation but which has been configured to use a single lane.

Reimplemented from Botan::PasswordHash.

Definition at line 41 of file bcrypt_pbkdf.h.

41{ return 0; }

◆ supports_associated_data()

virtual bool Botan::PasswordHash::supports_associated_data ( ) const
inlinevirtualinherited
Returns
true if this password hash supports supplying associated data

Reimplemented in Botan::Argon2.

Definition at line 69 of file pwdhash.h.

69{ return false; }

◆ supports_keyed_operation()

virtual bool Botan::PasswordHash::supports_keyed_operation ( ) const
inlinevirtualinherited
Returns
true if this password hash supports supplying a key

Reimplemented in Botan::Argon2.

Definition at line 64 of file pwdhash.h.

64{ return false; }

◆ to_string()

std::string Botan::Bcrypt_PBKDF::to_string ( ) const
overridevirtual

Implements Botan::PasswordHash.

Definition at line 21 of file bcrypt_pbkdf.cpp.

21 {
22 return fmt("Bcrypt-PBKDF({})", m_iterations);
23}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt().

◆ total_memory_usage()

size_t Botan::Bcrypt_PBKDF::total_memory_usage ( ) const
inlineoverridevirtual

Returns an estimate of the total number of bytes required to perform this key derivation.

If this algorithm uses a small and constant amount of memory, with no effort made towards being memory hard, this function returns 0.

Reimplemented from Botan::PasswordHash.

Definition at line 45 of file bcrypt_pbkdf.h.

45{ return 4096; }

The documentation for this class was generated from the following files: