Botan 3.9.0
Crypto and TLS for C&
Botan::Argon2 Class Referencefinal

#include <argon2.h>

Inheritance diagram for Botan::Argon2:
Botan::PasswordHash

Public Member Functions

 Argon2 (uint8_t family, size_t M, size_t t, size_t p)
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
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 override
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 M () const
size_t memory_param () const override
size_t p () const
size_t parallelism () const override
bool supports_associated_data () const override
bool supports_keyed_operation () const override
size_t t () const
std::string to_string () const override
size_t total_memory_usage () const override

Static Public Member Functions

static void blamka (uint64_t N[128], uint64_t T[128])

Detailed Description

Argon2 key derivation function

Definition at line 26 of file argon2.h.

Constructor & Destructor Documentation

◆ Argon2()

Botan::Argon2::Argon2 ( uint8_t family,
size_t M,
size_t t,
size_t p )

Definition at line 18 of file argon2pwhash.cpp.

18 : m_family(family), m_M(M), m_t(t), m_p(p) {
19 BOTAN_ARG_CHECK(m_p >= 1 && m_p <= 128, "Invalid Argon2 threads parameter");
20 BOTAN_ARG_CHECK(m_M >= 8 * m_p && m_M <= 8192 * 1024, "Invalid Argon2 M parameter");
21 BOTAN_ARG_CHECK(m_t >= 1 && m_t <= std::numeric_limits<uint32_t>::max(), "Invalid Argon2 t parameter");
22}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
size_t p() const
Definition argon2.h:57
size_t t() const
Definition argon2.h:55
size_t M() const
Definition argon2.h:53

References BOTAN_ARG_CHECK, M(), p(), and t().

Member Function Documentation

◆ blamka()

void Botan::Argon2::blamka ( uint64_t N[128],
uint64_t T[128] )
static

Argon2's BLAMKA function

Definition at line 169 of file argon2.cpp.

169 {
170#if defined(BOTAN_HAS_ARGON2_AVX2)
172 return Argon2::blamka_avx2(N, T);
173 }
174#endif
175
176#if defined(BOTAN_HAS_ARGON2_SSSE3)
178 return Argon2::blamka_ssse3(N, T);
179 }
180#endif
181
182 copy_mem(T, N, 128);
183
184 for(size_t i = 0; i != 128; i += 16) {
185 blamka_G(T[i + 0], T[i + 4], T[i + 8], T[i + 12]);
186 blamka_G(T[i + 1], T[i + 5], T[i + 9], T[i + 13]);
187 blamka_G(T[i + 2], T[i + 6], T[i + 10], T[i + 14]);
188 blamka_G(T[i + 3], T[i + 7], T[i + 11], T[i + 15]);
189
190 blamka_G(T[i + 0], T[i + 5], T[i + 10], T[i + 15]);
191 blamka_G(T[i + 1], T[i + 6], T[i + 11], T[i + 12]);
192 blamka_G(T[i + 2], T[i + 7], T[i + 8], T[i + 13]);
193 blamka_G(T[i + 3], T[i + 4], T[i + 9], T[i + 14]);
194 }
195
196 for(size_t i = 0; i != 128 / 8; i += 2) {
197 blamka_G(T[i + 0], T[i + 32], T[i + 64], T[i + 96]);
198 blamka_G(T[i + 1], T[i + 33], T[i + 65], T[i + 97]);
199 blamka_G(T[i + 16], T[i + 48], T[i + 80], T[i + 112]);
200 blamka_G(T[i + 17], T[i + 49], T[i + 81], T[i + 113]);
201
202 blamka_G(T[i + 0], T[i + 33], T[i + 80], T[i + 113]);
203 blamka_G(T[i + 1], T[i + 48], T[i + 81], T[i + 96]);
204 blamka_G(T[i + 16], T[i + 49], T[i + 64], T[i + 97]);
205 blamka_G(T[i + 17], T[i + 32], T[i + 65], T[i + 112]);
206 }
207
208 for(size_t i = 0; i != 128; ++i) {
209 N[i] ^= T[i];
210 }
211}
static bool has(CPUID::Feature feat)
Definition cpuid.h:94
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145

References Botan::CPUFeature::AVX2, Botan::copy_mem(), Botan::CPUID::has(), and Botan::CPUFeature::SSSE3.

◆ derive_key() [1/2]

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

Implements Botan::PasswordHash.

Definition at line 24 of file argon2pwhash.cpp.

29 {
30 argon2(output, output_len, password, password_len, salt, salt_len, nullptr, 0, nullptr, 0);
31}

◆ derive_key() [2/2]

void Botan::Argon2::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
overridevirtual

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 from Botan::PasswordHash.

Definition at line 33 of file argon2pwhash.cpp.

42 {
43 argon2(output, output_len, password, password_len, salt, salt_len, key, key_len, ad, ad_len);
44}

◆ 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 }
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

References derive_key().

Referenced by Botan::RFC4880_S2K::RFC4880_S2K().

◆ 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 }

References derive_key().

◆ iterations()

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

Most password hashes have some notion of iterations.

Implements Botan::PasswordHash.

Definition at line 63 of file argon2.h.

63{ return t(); }

References t().

◆ M()

size_t Botan::Argon2::M ( ) const
inline

Definition at line 53 of file argon2.h.

53{ return m_M; }

Referenced by Argon2(), memory_param(), and total_memory_usage().

◆ memory_param()

size_t Botan::Argon2::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 67 of file argon2.h.

67{ return M(); }

References M().

◆ p()

size_t Botan::Argon2::p ( ) const
inline

Definition at line 57 of file argon2.h.

57{ return m_p; }

Referenced by Argon2(), and parallelism().

◆ parallelism()

size_t Botan::Argon2::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 65 of file argon2.h.

65{ return p(); }

References p().

◆ supports_associated_data()

bool Botan::Argon2::supports_associated_data ( ) const
inlineoverridevirtual
Returns
true if this password hash supports supplying associated data

Reimplemented from Botan::PasswordHash.

Definition at line 61 of file argon2.h.

61{ return true; }

◆ supports_keyed_operation()

bool Botan::Argon2::supports_keyed_operation ( ) const
inlineoverridevirtual
Returns
true if this password hash supports supplying a key

Reimplemented from Botan::PasswordHash.

Definition at line 59 of file argon2.h.

59{ return true; }

◆ t()

size_t Botan::Argon2::t ( ) const
inline

Definition at line 55 of file argon2.h.

55{ return m_t; }

Referenced by Argon2(), and iterations().

◆ to_string()

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

Implements Botan::PasswordHash.

Definition at line 63 of file argon2pwhash.cpp.

63 {
64 return fmt("{}({},{},{})", argon2_family_name(m_family), m_M, m_t, m_p);
65}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53

References Botan::fmt().

◆ total_memory_usage()

size_t Botan::Argon2::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 69 of file argon2.h.

69{ return M() * 1024; }

References M().


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