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

#include <pbkdf1.h>

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

Public Member Functions

PBKDFclone () const
 
OctetString derive_key (size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
 
template<typename Alloc >
OctetString derive_key (size_t output_len, const std::string &passphrase, const std::vector< byte, Alloc > &salt, size_t iterations) const
 
OctetString derive_key (size_t output_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 output_len, const std::string &passphrase, const std::vector< byte, Alloc > &salt, std::chrono::milliseconds msec, size_t &iterations) const
 
std::pair< size_t, OctetStringkey_derivation (size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const override
 
std::string name () const
 
 PKCS5_PBKDF1 (HashFunction *hash_in)
 

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.

Constructor & Destructor Documentation

Botan::PKCS5_PBKDF1::PKCS5_PBKDF1 ( HashFunction hash_in)
inline

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

Parameters
hash_inpointer to a hash function object to use

Definition at line 28 of file pbkdf1.h.

28 : hash(hash_in) {}

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.

36  {
37  return new PKCS5_PBKDF1(hash->clone());
38  }
PKCS5_PBKDF1(HashFunction *hash_in)
Definition: pbkdf1.h:28
OctetString Botan::PBKDF::derive_key ( size_t  output_len,
const std::string &  passphrase,
const byte  salt[],
size_t  salt_len,
size_t  iterations 
) const
inherited

Derive a key from a passphrase

Parameters
output_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 13 of file pbkdf.cpp.

References BOTAN_ASSERT, Botan::PBKDF::key_derivation(), and Botan::PBKDF::name().

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

17  {
18  if(iterations == 0)
19  throw std::invalid_argument(name() + ": Invalid iteration count");
20 
21  auto derived = key_derivation(output_len, passphrase,
22  salt, salt_len, iterations,
23  std::chrono::milliseconds(0));
24 
25  BOTAN_ASSERT(derived.first == iterations,
26  "PBKDF used the correct number of iterations");
27 
28  return derived.second;
29  }
virtual std::string name() const =0
virtual std::pair< size_t, OctetString > key_derivation(size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
std::string passphrase
Definition: pkcs8.cpp:251
template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  output_len,
const std::string &  passphrase,
const std::vector< byte, Alloc > &  salt,
size_t  iterations 
) const
inlineinherited

Derive a key from a passphrase

Parameters
output_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 55 of file pbkdf.h.

59  {
60  return derive_key(output_len, passphrase, &salt[0], salt.size(), iterations);
61  }
OctetString derive_key(size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
Definition: pbkdf.cpp:13
std::string passphrase
Definition: pkcs8.cpp:251
OctetString Botan::PBKDF::derive_key ( size_t  output_len,
const std::string &  passphrase,
const byte  salt[],
size_t  salt_len,
std::chrono::milliseconds  msec,
size_t &  iterations 
) const
inherited

Derive a key from a passphrase

Parameters
output_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 31 of file pbkdf.cpp.

References Botan::PBKDF::key_derivation().

36  {
37  auto derived = key_derivation(output_len, passphrase, salt, salt_len, 0, ms);
38 
39  iterations = derived.first;
40 
41  return derived.second;
42  }
virtual std::pair< size_t, OctetString > key_derivation(size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
std::string passphrase
Definition: pkcs8.cpp:251
template<typename Alloc >
OctetString Botan::PBKDF::derive_key ( size_t  output_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
output_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 87 of file pbkdf.h.

92  {
93  return derive_key(output_len, passphrase, &salt[0], salt.size(), msec, iterations);
94  }
OctetString derive_key(size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, size_t iterations) const
Definition: pbkdf.cpp:13
std::string passphrase
Definition: pkcs8.cpp:251
std::pair< size_t, OctetString > Botan::PKCS5_PBKDF1::key_derivation ( size_t  output_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
output_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 and the derived key

Implements Botan::PBKDF.

Definition at line 17 of file pbkdf1.cpp.

22  {
23  if(key_len > hash->output_length())
24  throw Invalid_Argument("PKCS5_PBKDF1: Requested output length too long");
25 
26  hash->update(passphrase);
27  hash->update(salt, salt_len);
28  secure_vector<byte> key = hash->final();
29 
30  const auto start = std::chrono::high_resolution_clock::now();
31  size_t iterations_performed = 1;
32 
33  while(true)
34  {
35  if(iterations == 0)
36  {
37  if(iterations_performed % 10000 == 0)
38  {
39  auto time_taken = std::chrono::high_resolution_clock::now() - start;
40  auto msec_taken = std::chrono::duration_cast<std::chrono::milliseconds>(time_taken);
41  if(msec_taken > msec)
42  break;
43  }
44  }
45  else if(iterations_performed == iterations)
46  break;
47 
48  hash->update(key);
49  hash->final(&key[0]);
50 
51  ++iterations_performed;
52  }
53 
54  return std::make_pair(iterations_performed,
55  OctetString(&key[0], std::min(key_len, key.size())));
56  }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
std::string passphrase
Definition: pkcs8.cpp:251
std::string Botan::PKCS5_PBKDF1::name ( ) const
inlinevirtual

Implements Botan::PBKDF.

Definition at line 30 of file pbkdf1.h.

31  {
32  return "PBKDF1(" + hash->name() + ")";
33  }

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