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

#include <pbkdf1.h>

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

Public Member Functions

void clear ()
 
PBKDFclone () const
 
OctetString derive_key (size_t output_len, const std::string &passphrase, const byte salt[], size_t salt_len, 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
 
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)
 
 PKCS5_PBKDF1 (const PKCS5_PBKDF1 &other)
 
 ~PKCS5_PBKDF1 ()
 

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) {}
Botan::PKCS5_PBKDF1::PKCS5_PBKDF1 ( const PKCS5_PBKDF1 other)
inline

Copy constructor

Parameters
otherthe object to copy

Definition at line 34 of file pbkdf1.h.

35  :
PBKDF(), hash(other.hash->clone()) {}
Botan::PKCS5_PBKDF1::~PKCS5_PBKDF1 ( )
inline

Definition at line 37 of file pbkdf1.h.

37 { delete hash; }

Member Function Documentation

void Botan::PBKDF::clear ( )
inlinevirtualinherited

Zeroize internal state

Implements Botan::Algorithm.

Definition at line 31 of file pbkdf.h.

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

Implements Botan::PBKDF.

Definition at line 44 of file pbkdf1.h.

45  {
46  return new PKCS5_PBKDF1(hash->clone());
47  }
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::Algorithm::name().

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

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

References Botan::Buffered_Computation::final(), Botan::Buffered_Computation::output_length(), and Botan::Buffered_Computation::update().

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::string Botan::PKCS5_PBKDF1::name ( ) const
inlinevirtual
Returns
name of this algorithm

Implements Botan::Algorithm.

Definition at line 39 of file pbkdf1.h.

40  {
41  return "PBKDF1(" + hash->name() + ")";
42  }

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