Botan  2.6.0
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | List of all members
Botan::PKCS5_PBKDF2 Class Referencefinal

#include <pbkdf2.h>

Inheritance diagram for Botan::PKCS5_PBKDF2:
Botan::PBKDF

Public Member Functions

PBKDFclone () const override
 
OctetString derive_key (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, 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, 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
 
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
 
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
 
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_iterations (size_t out_len, const std::string &passphrase, const uint8_t salt[], size_t salt_len, 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
 
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
 
 PKCS5_PBKDF2 (MessageAuthenticationCode *mac_fn)
 

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 PBKDF2

Definition at line 27 of file pbkdf2.h.

Constructor & Destructor Documentation

◆ PKCS5_PBKDF2()

Botan::PKCS5_PBKDF2::PKCS5_PBKDF2 ( MessageAuthenticationCode mac_fn)
inlineexplicit

Create a PKCS #5 instance using the specified message auth code

Parameters
mac_fnthe MAC object to use as PRF

Definition at line 50 of file pbkdf2.h.

50 : m_mac(mac_fn) {}

Member Function Documentation

◆ clone()

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

Implements Botan::PBKDF.

Definition at line 35 of file pbkdf2.h.

36  {
37  return new PKCS5_PBKDF2(m_mac->clone());
38  }
PKCS5_PBKDF2(MessageAuthenticationCode *mac_fn)
Definition: pbkdf2.h:50

◆ 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 25 of file pbkdf.cpp.

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(), and Botan::get_pbkdf().

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

◆ 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 73 of file pbkdf.cpp.

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

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

75  {
76  if(auto pbkdf = PBKDF::create(algo, provider))
77  {
78  return pbkdf;
79  }
80  throw Lookup_Error("PBKDF", algo, provider);
81  }
static std::unique_ptr< PBKDF > create(const std::string &algo_spec, const std::string &provider="")
Definition: pbkdf.cpp:25
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

◆ derive_key() [1/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 158 of file pbkdf.h.

References salt_len.

Referenced by Botan::check_passhash9().

162  {
163  return pbkdf_iterations(out_len, passphrase, salt, salt_len, iterations);
164  }
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:97
size_t salt_len
Definition: x509_obj.cpp:26

◆ 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,
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 174 of file pbkdf.h.

178  {
179  return pbkdf_iterations(out_len, passphrase, salt.data(), salt.size(), iterations);
180  }
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:97

◆ 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,
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 191 of file pbkdf.h.

References salt_len.

196  {
197  return pbkdf_timed(out_len, passphrase, salt, salt_len, msec, iterations);
198  }
size_t salt_len
Definition: x509_obj.cpp:26
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:88

◆ derive_key() [4/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 209 of file pbkdf.h.

214  {
215  return pbkdf_timed(out_len, passphrase, salt.data(), salt.size(), msec, iterations);
216  }
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:88

◆ name()

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

Implements Botan::PBKDF.

Definition at line 30 of file pbkdf2.h.

31  {
32  return "PBKDF2(" + m_mac->name() + ")";
33  }

◆ pbkdf()

size_t Botan::PKCS5_PBKDF2::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 107 of file pbkdf2.cpp.

References Botan::pbkdf2(), and salt_len.

112  {
113  return pbkdf2(*m_mac.get(), key, key_len, passphrase, salt, salt_len, iterations, msec);
114  }
size_t pbkdf2(MessageAuthenticationCode &prf, 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)
Definition: pbkdf2.cpp:14
size_t salt_len
Definition: x509_obj.cpp:26

◆ pbkdf_iterations() [1/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 97 of file pbkdf.cpp.

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

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

101  {
102  if(iterations == 0)
103  throw Invalid_Argument(name() + ": Invalid iteration count");
104 
105  const size_t iterations_run = pbkdf(out, out_len, passphrase,
106  salt, salt_len, iterations,
107  std::chrono::milliseconds(0));
108  BOTAN_ASSERT_EQUAL(iterations, iterations_run, "Expected PBKDF iterations");
109  }
virtual std::string name() const =0
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:56
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
size_t salt_len
Definition: x509_obj.cpp:26

◆ pbkdf_iterations() [2/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 111 of file pbkdf.cpp.

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

115  {
116  secure_vector<uint8_t> out(out_len);
117  pbkdf_iterations(out.data(), out_len, passphrase, salt, salt_len, iterations);
118  return out;
119  }
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:97
size_t salt_len
Definition: x509_obj.cpp:26

◆ pbkdf_timed() [1/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 88 of file pbkdf.cpp.

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

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

93  {
94  iterations = pbkdf(out, out_len, passphrase, salt, salt_len, 0, msec);
95  }
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
size_t salt_len
Definition: x509_obj.cpp:26

◆ pbkdf_timed() [2/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 121 of file pbkdf.cpp.

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

126  {
127  secure_vector<uint8_t> out(out_len);
128  pbkdf_timed(out.data(), out_len, passphrase, salt, salt_len, msec, iterations);
129  return out;
130  }
size_t salt_len
Definition: x509_obj.cpp:26
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:88

◆ 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 83 of file pbkdf.cpp.

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

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