Botan  1.11.17
Public Types | Public Member Functions | List of all members
Botan::PKCS5_PBKDF1 Class Reference

#include <pbkdf1.h>

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

Public Types

typedef SCAN_Name Spec
 

Public Member Functions

PBKDFclone () const
 
OctetString derive_key (size_t out_len, const std::string &passphrase, const byte 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< byte, Alloc > &salt, size_t iterations) const
 
OctetString derive_key (size_t out_len, const std::string &passphrase, const byte 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< byte, Alloc > &salt, std::chrono::milliseconds msec, size_t &iterations) const
 
std::string name () const
 
size_t pbkdf (byte output_buf[], size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const override
 
void pbkdf_iterations (byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
 
secure_vector< bytepbkdf_iterations (size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
 
void pbkdf_timed (byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
secure_vector< bytepbkdf_timed (size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
 
 PKCS5_PBKDF1 (HashFunction *hash)
 

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 compatability, use PKCS5_PBKDF2

Definition at line 21 of file pbkdf1.h.

Member Typedef Documentation

typedef SCAN_Name Botan::PBKDF::Spec
inherited

Definition at line 28 of file pbkdf.h.

Constructor & Destructor Documentation

Botan::PKCS5_PBKDF1::PKCS5_PBKDF1 ( HashFunction hash)
inline

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

Parameters
hashpointer to a hash function object to use

Definition at line 28 of file pbkdf1.h.

28 : m_hash(hash) {}
std::string m_hash
Definition: dsa.cpp:102

Member Function Documentation

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

Implements Botan::PBKDF.

Definition at line 35 of file pbkdf1.h.

References m_hash.

36  {
37  return new PKCS5_PBKDF1(m_hash->clone());
38  }
PKCS5_PBKDF1(HashFunction *hash)
Definition: pbkdf1.h:28
std::string m_hash
Definition: dsa.cpp:102
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const byte  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 89 of file pbkdf.h.

Referenced by Botan::check_passhash9(), Botan::CryptoBox::decrypt(), Botan::CryptoBox::encrypt(), and Botan::generate_passhash9().

93  {
94  return pbkdf_iterations(out_len, passphrase, salt, salt_len, iterations);
95  }
void pbkdf_iterations(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
Definition: pbkdf.cpp:22
template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const std::vector< byte, 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 105 of file pbkdf.h.

109  {
110  return pbkdf_iterations(out_len, passphrase, salt.data(), salt.size(), iterations);
111  }
void pbkdf_iterations(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
Definition: pbkdf.cpp:22
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const byte  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 122 of file pbkdf.h.

127  {
128  return pbkdf_timed(out_len, passphrase, salt, salt_len, msec, iterations);
129  }
void pbkdf_timed(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
Definition: pbkdf.cpp:13
template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  out_len,
const std::string &  passphrase,
const std::vector< byte, 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 140 of file pbkdf.h.

145  {
146  return pbkdf_timed(out_len, passphrase, salt.data(), salt.size(), msec, iterations);
147  }
void pbkdf_timed(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
Definition: pbkdf.cpp:13
std::string Botan::PKCS5_PBKDF1::name ( ) const
inlinevirtual

Implements Botan::PBKDF.

Definition at line 30 of file pbkdf1.h.

References m_hash.

31  {
32  return "PBKDF1(" + m_hash->name() + ")";
33  }
std::string m_hash
Definition: dsa.cpp:102
size_t Botan::PKCS5_PBKDF1::pbkdf ( byte  out[],
size_t  out_len,
const std::string &  passphrase,
const byte  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 seconds 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
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 16 of file pbkdf1.cpp.

References Botan::copy_mem(), and m_hash.

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

Definition at line 22 of file pbkdf.cpp.

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

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

26  {
27  if(iterations == 0)
28  throw std::invalid_argument(name() + ": Invalid iteration count");
29 
30  const size_t iterations_run = pbkdf(out, out_len, passphrase,
31  salt, salt_len, iterations,
32  std::chrono::milliseconds(0));
33  BOTAN_ASSERT_EQUAL(iterations, iterations_run, "Expected PBKDF iterations");
34  }
virtual size_t pbkdf(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
virtual std::string name() const =0
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:40
secure_vector< byte > Botan::PBKDF::pbkdf_iterations ( size_t  out_len,
const std::string &  passphrase,
const byte  salt[],
size_t  salt_len,
size_t  iterations 
) const
inherited

Definition at line 36 of file pbkdf.cpp.

References Botan::PBKDF::pbkdf_iterations().

40  {
41  secure_vector<byte> out(out_len);
42  pbkdf_iterations(out.data(), out_len, passphrase, salt, salt_len, iterations);
43  return out;
44  }
void pbkdf_iterations(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
Definition: pbkdf.cpp:22
void Botan::PBKDF::pbkdf_timed ( byte  out[],
size_t  out_len,
const std::string &  passphrase,
const byte  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inherited

Definition at line 13 of file pbkdf.cpp.

References Botan::PBKDF::pbkdf().

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

18  {
19  iterations = pbkdf(out, out_len, passphrase, salt, salt_len, 0, msec);
20  }
virtual size_t pbkdf(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
secure_vector< byte > Botan::PBKDF::pbkdf_timed ( size_t  out_len,
const std::string &  passphrase,
const byte  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inherited

Definition at line 46 of file pbkdf.cpp.

References Botan::PBKDF::pbkdf_timed().

51  {
52  secure_vector<byte> out(out_len);
53  pbkdf_timed(out.data(), out_len, passphrase, salt, salt_len, msec, iterations);
54  return out;
55  }
void pbkdf_timed(byte out[], size_t out_len, const std::string &passphrase, const byte salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
Definition: pbkdf.cpp:13

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