Botan  1.11.10
pbkdf1.cpp
Go to the documentation of this file.
1 /*
2 * PBKDF1
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/pbkdf1.h>
9 #include <botan/exceptn.h>
10 
11 namespace Botan {
12 
13 /*
14 * Return a PKCS#5 PBKDF1 derived key
15 */
16 std::pair<size_t, OctetString>
18  const std::string& passphrase,
19  const byte salt[], size_t salt_len,
20  size_t iterations,
21  std::chrono::milliseconds msec) const
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  }
57 
58 }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:92
uint8_t byte
Definition: types.h:30
Definition: buf_comp.h:15
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 override
Definition: pbkdf1.cpp:17
std::string passphrase
Definition: pkcs8.cpp:245