Botan  1.10.10
pbkdf1.cpp
Go to the documentation of this file.
1 /*
2 * PBKDF1
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/internal/pbkdf_utils.h>
9 #include <botan/pbkdf1.h>
10 #include <botan/exceptn.h>
11 
12 namespace Botan {
13 
14 BOTAN_REGISTER_PBKDF_1HASH(PKCS5_PBKDF1, "PBKDF1");
15 
16 size_t PKCS5_PBKDF1::pbkdf(byte output_buf[], size_t output_len,
17  const std::string& passphrase,
18  const byte salt[], size_t salt_len,
19  size_t iterations,
20  std::chrono::milliseconds msec) const
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  }
56 
57 }
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
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
Definition: pbkdf1.cpp:16
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:93
BOTAN_REGISTER_PBKDF_1HASH(PKCS5_PBKDF1,"PBKDF1")
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:39
std::uint8_t byte
Definition: types.h:31