Botan  1.11.34
Crypto and TLS for C++11
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/pbkdf1.h>
9 #include <botan/exceptn.h>
10 
11 namespace Botan {
12 
13 size_t PKCS5_PBKDF1::pbkdf(byte output_buf[], size_t output_len,
14  const std::string& passphrase,
15  const byte salt[], size_t salt_len,
16  size_t iterations,
17  std::chrono::milliseconds msec) const
18  {
19  if(output_len > m_hash->output_length())
20  throw Invalid_Argument("PKCS5_PBKDF1: Requested output length too long");
21 
22  m_hash->update(passphrase);
23  m_hash->update(salt, salt_len);
24  secure_vector<byte> key = m_hash->final();
25 
26  const auto start = std::chrono::high_resolution_clock::now();
27  size_t iterations_performed = 1;
28 
29  while(true)
30  {
31  if(iterations == 0)
32  {
33  if(iterations_performed % 10000 == 0)
34  {
35  auto time_taken = std::chrono::high_resolution_clock::now() - start;
36  auto msec_taken = std::chrono::duration_cast<std::chrono::milliseconds>(time_taken);
37  if(msec_taken > msec)
38  break;
39  }
40  }
41  else if(iterations_performed == iterations)
42  break;
43 
44  m_hash->update(key);
45  m_hash->final(key.data());
46 
47  ++iterations_performed;
48  }
49 
50  copy_mem(output_buf, key.data(), output_len);
51  return iterations_performed;
52  }
53 
54 }
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:13
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:113
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Definition: alg_id.cpp:13
std::uint8_t byte
Definition: types.h:31