Botan 3.0.0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::Scrypt Class Referencefinal

#include <scrypt.h>

Inheritance diagram for Botan::Scrypt:
Botan::PasswordHash

Public Member Functions

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)
 
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)
 
size_t iterations () const override
 
size_t memory_param () const override
 
Scryptoperator= (const Scrypt &)=default
 
size_t parallelism () const override
 
 Scrypt (const Scrypt &other)=default
 
 Scrypt (size_t N, size_t r, size_t p)
 
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

Scrypt key derivation function (RFC 7914)

Definition at line 21 of file scrypt.h.

Constructor & Destructor Documentation

◆ Scrypt() [1/2]

Botan::Scrypt::Scrypt ( size_t  N,
size_t  r,
size_t  p 
)

Definition at line 144 of file scrypt.cpp.

144 :
145 m_N(N), m_r(r), m_p(p)
146 {
147 if(!is_power_of_2(N))
148 throw Invalid_Argument("Scrypt N parameter must be a power of 2");
149
150 if(p == 0 || p > 1024)
151 throw Invalid_Argument("Invalid or unsupported scrypt p");
152 if(r == 0 || r > 256)
153 throw Invalid_Argument("Invalid or unsupported scrypt r");
154 if(N < 1 || N > 4194304)
155 throw Invalid_Argument("Invalid or unsupported scrypt N");
156 }
constexpr bool is_power_of_2(T arg)
Definition: bit_ops.h:45

References Botan::is_power_of_2().

◆ Scrypt() [2/2]

Botan::Scrypt::Scrypt ( const Scrypt other)
default

Member Function Documentation

◆ derive_key() [1/2]

void Botan::Scrypt::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 Scrypt parameter set

Implements Botan::PasswordHash.

Definition at line 220 of file scrypt.cpp.

223 {
224 const size_t N = memory_param();
225 const size_t p = parallelism();
226 const size_t r = iterations();
227
228 const size_t S = 128 * r;
229 secure_vector<uint8_t> B(p * S);
230 // temp space
231 secure_vector<uint8_t> V((N+1) * S);
232
233 auto hmac_sha256 = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
234
235 try
236 {
237 hmac_sha256->set_key(cast_char_ptr_to_uint8(password), password_len);
238 }
239 catch(Invalid_Key_Length&)
240 {
241 throw Invalid_Argument("Scrypt cannot accept passphrases of the provided length");
242 }
243
244 pbkdf2(*hmac_sha256,
245 B.data(), B.size(),
246 salt, salt_len,
247 1);
248
249 // these can be parallel
250 for(size_t i = 0; i != p; ++i)
251 {
252 scryptROMmix(r, N, &B[128*r*i], V);
253 }
254
255 pbkdf2(*hmac_sha256,
256 output, output_len,
257 B.data(), B.size(),
258 1);
259 }
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition: mac.cpp:134
size_t memory_param() const override
Definition: scrypt.h:42
size_t iterations() const override
Definition: scrypt.h:38
size_t parallelism() const override
Definition: scrypt.h:40
size_t pbkdf2(MessageAuthenticationCode &prf, uint8_t out[], size_t out_len, std::string_view password, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec)
Definition: pbkdf2.cpp:81
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:183

References Botan::cast_char_ptr_to_uint8(), Botan::MessageAuthenticationCode::create_or_throw(), iterations(), memory_param(), parallelism(), and Botan::pbkdf2().

◆ 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 33 of file pwdhash.cpp.

38 {
39 BOTAN_UNUSED(ad, key);
40
41 if(ad_len == 0 && key_len == 0)
42 return this->derive_key(out, out_len,
43 password, password_len,
44 salt, salt_len);
45 else
46 throw Not_Implemented("PasswordHash " + this->to_string() + " does not support AD or key");
47 }
#define BOTAN_UNUSED(...)
Definition: assert.h:141
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 
)
inlineinherited

Hash a password into a bitstring

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 82 of file pwdhash.h.

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

◆ 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 
)
inlineinherited

Hash a password into a bitstring

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 103 of file pwdhash.h.

108 {
109 this->derive_key(out.data(), out.size(),
110 password.data(), password.size(),
111 salt.data(), salt.size(),
112 associated_data.data(), associated_data.size(),
113 key.data(), key.size());
114 }

◆ iterations()

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

Most password hashes have some notion of iterations.

Implements Botan::PasswordHash.

Definition at line 38 of file scrypt.h.

38{ return m_r; }

Referenced by derive_key(), and total_memory_usage().

◆ memory_param()

size_t Botan::Scrypt::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 42 of file scrypt.h.

42{ return m_N; }

Referenced by derive_key(), and total_memory_usage().

◆ operator=()

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

◆ parallelism()

size_t Botan::Scrypt::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 40 of file scrypt.h.

40{ return m_p; }

Referenced by derive_key(), and total_memory_usage().

◆ 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 70 of file pwdhash.h.

70{ 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 65 of file pwdhash.h.

65{ return false; }

◆ to_string()

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

Implements Botan::PasswordHash.

Definition at line 158 of file scrypt.cpp.

159 {
160 return fmt("Scrypt({},{},{})", m_N, m_r, m_p);
161 }
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60

References Botan::fmt().

◆ total_memory_usage()

size_t Botan::Scrypt::total_memory_usage ( ) const
overridevirtual

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 163 of file scrypt.cpp.

164 {
165 const size_t N = memory_param();
166 const size_t p = parallelism();
167 const size_t r = iterations();
168
169 return scrypt_memory_usage(N, r, p);
170 }

References iterations(), memory_param(), and parallelism().


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