/src/botan/src/lib/pbkdf/pbkdf1/pbkdf1.cpp
Line | Count | Source (jump to first uncovered line) |
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(uint8_t output_buf[], size_t output_len, |
14 | | const std::string& passphrase, |
15 | | const uint8_t salt[], size_t salt_len, |
16 | | size_t iterations, |
17 | | std::chrono::milliseconds msec) const |
18 | 0 | { |
19 | 0 | if(output_len > m_hash->output_length()) |
20 | 0 | throw Invalid_Argument("PKCS5_PBKDF1: Requested output length too long"); |
21 | 0 | |
22 | 0 | m_hash->update(passphrase); |
23 | 0 | m_hash->update(salt, salt_len); |
24 | 0 | secure_vector<uint8_t> key = m_hash->final(); |
25 | 0 |
|
26 | 0 | const auto start = std::chrono::high_resolution_clock::now(); |
27 | 0 | size_t iterations_performed = 1; |
28 | 0 |
|
29 | 0 | while(true) |
30 | 0 | { |
31 | 0 | if(iterations == 0) |
32 | 0 | { |
33 | 0 | if(iterations_performed % 10000 == 0) |
34 | 0 | { |
35 | 0 | auto time_taken = std::chrono::high_resolution_clock::now() - start; |
36 | 0 | auto msec_taken = std::chrono::duration_cast<std::chrono::milliseconds>(time_taken); |
37 | 0 | if(msec_taken > msec) |
38 | 0 | break; |
39 | 0 | } |
40 | 0 | } |
41 | 0 | else if(iterations_performed == iterations) |
42 | 0 | break; |
43 | 0 | |
44 | 0 | m_hash->update(key); |
45 | 0 | m_hash->final(key.data()); |
46 | 0 |
|
47 | 0 | ++iterations_performed; |
48 | 0 | } |
49 | 0 |
|
50 | 0 | copy_mem(output_buf, key.data(), output_len); |
51 | 0 | return iterations_performed; |
52 | 0 | } |
53 | | |
54 | | } |