Botan 2.17.3
Crypto and TLS for C&
Public Member Functions | Static Public Member Functions | List of all members
Botan::PKCS5_PBKDF1 Class Referencefinal

#include <pbkdf1.h>

Inheritance diagram for Botan::PKCS5_PBKDF1:
Botan::PBKDF

Public Member Functions

PBKDFclone () const override
 
template<typename Alloc >
OctetString derive_key (size_t out_len, const std::string &passphrase, const std::vector< uint8_t, Alloc > &salt, size_t iterations) const
 
template<typename Alloc >
OctetString derive_key (size_t out_len, const std::string &passphrase, const std::vector< uint8_t, Alloc > &salt, std::chrono::milliseconds msec, size_t &iterations) const
 
OctetString derive_key (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
 
OctetString derive_key (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
std::string name () const override
 
size_t pbkdf (uint8_t output_buf[], size_t output_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const override
 
secure_vector< uint8_t > pbkdf_iterations (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
 
void pbkdf_iterations (uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
 
secure_vector< uint8_t > pbkdf_timed (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
void pbkdf_timed (uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
 PKCS5_PBKDF1 (HashFunction *hash)
 

Static Public Member Functions

static std::unique_ptr< PBKDFcreate (const std::string &algo_spec, const std::string &provider="")
 
static std::unique_ptr< PBKDFcreate_or_throw (const std::string &algo_spec, const std::string &provider="")
 
static std::vector< std::string > providers (const std::string &algo_spec)
 

Detailed Description

PKCS #5 v1 PBKDF, aka PBKDF1 Can only generate a key up to the size of the hash output. Unless needed for backwards compatibility, use PKCS5_PBKDF2

Definition at line 23 of file pbkdf1.h.

Constructor & Destructor Documentation

◆ PKCS5_PBKDF1()

Botan::PKCS5_PBKDF1::PKCS5_PBKDF1 ( HashFunction hash)
inlineexplicit

Create a PKCS #5 instance using the specified hash function.

Parameters
hashpointer to a hash function object to use

Definition at line 30 of file pbkdf1.h.

30: m_hash(hash) {}
MechanismType hash

Member Function Documentation

◆ clone()

PBKDF * Botan::PKCS5_PBKDF1::clone ( ) const
inlineoverridevirtual
Returns
new instance of this same algorithm

Implements Botan::PBKDF.

Definition at line 37 of file pbkdf1.h.

38 {
39 return new PKCS5_PBKDF1(m_hash->clone());
40 }
PKCS5_PBKDF1(HashFunction *hash)
Definition: pbkdf1.h:30

◆ create()

std::unique_ptr< PBKDF > Botan::PBKDF::create ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
staticinherited

Create an instance based on a name If provider is empty then best available is chosen.

Parameters
algo_specalgorithm name
providerprovider implementation to choose
Returns
a null pointer if the algo/provider combination cannot be found

Definition at line 26 of file pbkdf.cpp.

28 {
29 const SCAN_Name req(algo_spec);
30
31#if defined(BOTAN_HAS_PBKDF2)
32 if(req.algo_name() == "PBKDF2")
33 {
34 // TODO OpenSSL
35
36 if(provider.empty() || provider == "base")
37 {
38 if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
39 return std::unique_ptr<PBKDF>(new PKCS5_PBKDF2(mac.release()));
40
41 if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")"))
42 return std::unique_ptr<PBKDF>(new PKCS5_PBKDF2(mac.release()));
43 }
44
45 return nullptr;
46 }
47#endif
48
49#if defined(BOTAN_HAS_PBKDF1)
50 if(req.algo_name() == "PBKDF1" && req.arg_count() == 1)
51 {
52 if(auto hash = HashFunction::create(req.arg(0)))
53 return std::unique_ptr<PBKDF>(new PKCS5_PBKDF1(hash.release()));
54
55 }
56#endif
57
58#if defined(BOTAN_HAS_PGP_S2K)
59 if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1)
60 {
61 if(auto hash = HashFunction::create(req.arg(0)))
62 return std::unique_ptr<PBKDF>(new OpenPGP_S2K(hash.release()));
63 }
64#endif
65
66 BOTAN_UNUSED(req);
67 BOTAN_UNUSED(provider);
68
69 return nullptr;
70 }
#define BOTAN_UNUSED(...)
Definition: assert.h:142
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:106
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:46

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_count(), BOTAN_UNUSED, Botan::HashFunction::create(), Botan::MessageAuthenticationCode::create(), and hash.

Referenced by Botan::PBKDF::create_or_throw().

◆ create_or_throw()

std::unique_ptr< PBKDF > Botan::PBKDF::create_or_throw ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
staticinherited

Create an instance based on a name, or throw if the algo/provider combination cannot be found. If provider is empty then best available is chosen.

Definition at line 74 of file pbkdf.cpp.

76 {
77 if(auto pbkdf = PBKDF::create(algo, provider))
78 {
79 return pbkdf;
80 }
81 throw Lookup_Error("PBKDF", algo, provider);
82 }
virtual size_t pbkdf(uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
static std::unique_ptr< PBKDF > create(const std::string &algo_spec, const std::string &provider="")
Definition: pbkdf.cpp:26

References Botan::PBKDF::create(), and Botan::PBKDF::pbkdf().

Referenced by Botan::CryptoBox::decrypt_bin(), Botan::CryptoBox::encrypt(), and Botan::get_pbkdf().

◆ derive_key() [1/4]

template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const std::vector< uint8_t, Alloc > &  salt,
size_t  iterations 
) const
inlineinherited

Derive a key from a passphrase

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
iterationsthe number of iterations to use (use 10K or more)

Definition at line 176 of file pbkdf.h.

180 {
181 return pbkdf_iterations(out_len, passphrase, salt.data(), salt.size(), iterations);
182 }
void pbkdf_iterations(uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
Definition: pbkdf.cpp:98

◆ derive_key() [2/4]

template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const std::vector< uint8_t, Alloc > &  salt,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inlineinherited

Derive a key from a passphrase using a certain amount of time

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
msecis how long to run the PBKDF
iterationsis set to the number of iterations used

Definition at line 211 of file pbkdf.h.

216 {
217 return pbkdf_timed(out_len, passphrase, salt.data(), salt.size(), msec, iterations);
218 }
void pbkdf_timed(uint8_t out[], size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
Definition: pbkdf.cpp:89

◆ derive_key() [3/4]

OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations 
) const
inlineinherited

Derive a key from a passphrase

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)

Definition at line 160 of file pbkdf.h.

164 {
165 return pbkdf_iterations(out_len, passphrase, salt, salt_len, iterations);
166 }
size_t salt_len
Definition: x509_obj.cpp:25

References salt_len.

Referenced by Botan::check_passhash9(), and Botan::generate_passhash9().

◆ derive_key() [4/4]

OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inlineinherited

Derive a key from a passphrase

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
msecis how long to run the PBKDF
iterationsis set to the number of iterations used

Definition at line 193 of file pbkdf.h.

198 {
199 return pbkdf_timed(out_len, passphrase, salt, salt_len, msec, iterations);
200 }

References salt_len.

◆ name()

std::string Botan::PKCS5_PBKDF1::name ( ) const
inlineoverridevirtual
Returns
name of this PBKDF

Implements Botan::PBKDF.

Definition at line 32 of file pbkdf1.h.

33 {
34 return "PBKDF1(" + m_hash->name() + ")";
35 }

◆ pbkdf()

size_t Botan::PKCS5_PBKDF1::pbkdf ( uint8_t  out[],
size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations,
std::chrono::milliseconds  msec 
) const
overridevirtual

Derive a key from a passphrase for a number of iterations specified by either iterations or if iterations == 0 then running until msec time has elapsed.

Parameters
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)
msecif iterations is zero, then instead the PBKDF is run until msec milliseconds has passed.
Returns
the number of iterations performed

Implements Botan::PBKDF.

Definition at line 13 of file pbkdf1.cpp.

18 {
19 if(output_len > m_hash->output_length())
20 throw Invalid_Argument("PKCS5_PBKDF1: Requested output length too long");
21
22 m_hash->update(passphrase);
23 m_hash->update(salt, salt_len);
24 secure_vector<uint8_t> key = m_hash->final();
25
26 const auto start = std::chrono::high_resolution_clock::now();
27 size_t iterations_performed = 1;
28
29 while(true)
30 {
31 if(iterations == 0)
32 {
33 if(iterations_performed % 10000 == 0)
34 {
35 auto time_taken = std::chrono::high_resolution_clock::now() - start;
36 auto msec_taken = std::chrono::duration_cast<std::chrono::milliseconds>(time_taken);
37 if(msec_taken > msec)
38 break;
39 }
40 }
41 else if(iterations_performed == iterations)
42 break;
43
44 m_hash->update(key);
45 m_hash->final(key.data());
46
47 ++iterations_performed;
48 }
49
50 copy_mem(output_buf, key.data(), output_len);
51 return iterations_performed;
52 }
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:133

References Botan::copy_mem(), and salt_len.

◆ pbkdf_iterations() [1/2]

secure_vector< uint8_t > Botan::PBKDF::pbkdf_iterations ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations 
) const
inherited

Derive a key from a passphrase for a number of iterations.

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)
Returns
the derived key

Definition at line 112 of file pbkdf.cpp.

116 {
117 secure_vector<uint8_t> out(out_len);
118 pbkdf_iterations(out.data(), out_len, passphrase, salt, salt_len, iterations);
119 return out;
120 }

References Botan::PBKDF::pbkdf_iterations(), and salt_len.

◆ pbkdf_iterations() [2/2]

void Botan::PBKDF::pbkdf_iterations ( uint8_t  out[],
size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations 
) const
inherited

Derive a key from a passphrase for a number of iterations.

Parameters
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
iterationsthe number of iterations to use (use 10K or more)

Definition at line 98 of file pbkdf.cpp.

102 {
103 if(iterations == 0)
104 throw Invalid_Argument(name() + ": Invalid iteration count");
105
106 const size_t iterations_run = pbkdf(out, out_len, passphrase,
107 salt, salt_len, iterations,
108 std::chrono::milliseconds(0));
109 BOTAN_ASSERT_EQUAL(iterations, iterations_run, "Expected PBKDF iterations");
110 }
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:81
virtual std::string name() const =0

References BOTAN_ASSERT_EQUAL, Botan::PBKDF::name(), Botan::PBKDF::pbkdf(), and salt_len.

Referenced by Botan::PBKDF::pbkdf_iterations().

◆ pbkdf_timed() [1/2]

secure_vector< uint8_t > Botan::PBKDF::pbkdf_timed ( size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inherited

Derive a key from a passphrase, running until msec time has elapsed.

Parameters
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
msecif iterations is zero, then instead the PBKDF is run until msec milliseconds has passed.
iterationsset to the number iterations executed
Returns
the derived key

Definition at line 122 of file pbkdf.cpp.

127 {
128 secure_vector<uint8_t> out(out_len);
129 pbkdf_timed(out.data(), out_len, passphrase, salt, salt_len, msec, iterations);
130 return out;
131 }

References Botan::PBKDF::pbkdf_timed(), and salt_len.

◆ pbkdf_timed() [2/2]

void Botan::PBKDF::pbkdf_timed ( uint8_t  out[],
size_t  out_len,
const std::string &  passphrase,
const uint8_t  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inherited

Derive a key from a passphrase, running until msec time has elapsed.

Parameters
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
msecif iterations is zero, then instead the PBKDF is run until msec milliseconds has passed.
iterationsset to the number iterations executed

Definition at line 89 of file pbkdf.cpp.

94 {
95 iterations = pbkdf(out, out_len, passphrase, salt, salt_len, 0, msec);
96 }

References Botan::PBKDF::pbkdf(), and salt_len.

Referenced by Botan::PBKDF::pbkdf_timed().

◆ providers()

std::vector< std::string > Botan::PBKDF::providers ( const std::string &  algo_spec)
staticinherited
Returns
list of available providers for this algorithm, empty if not available

Definition at line 84 of file pbkdf.cpp.

85 {
86 return probe_providers_of<PBKDF>(algo_spec, { "base", "openssl" });
87 }

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