/src/botan/src/lib/kdf/kdf1_iso18033/kdf1_iso18033.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * KDF1 from ISO 18033-2 |
3 | | * (C) 2016 Philipp Weber |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/internal/kdf1_iso18033.h> |
9 | | #include <botan/exceptn.h> |
10 | | |
11 | | namespace Botan { |
12 | | |
13 | | void KDF1_18033::kdf(uint8_t key[], size_t key_len, |
14 | | const uint8_t secret[], size_t secret_len, |
15 | | const uint8_t salt[], size_t salt_len, |
16 | | const uint8_t label[], size_t label_len) const |
17 | 0 | { |
18 | 0 | if(key_len == 0) |
19 | 0 | return; |
20 | | |
21 | 0 | const size_t blocks_required = key_len / m_hash->output_length(); |
22 | |
|
23 | 0 | if(blocks_required >= 0xFFFFFFFE) |
24 | 0 | throw Invalid_Argument("KDF1-18033 maximum output length exceeeded"); |
25 | | |
26 | 0 | uint32_t counter = 0; |
27 | 0 | secure_vector<uint8_t> h; |
28 | |
|
29 | 0 | size_t offset = 0; |
30 | 0 | while(offset != key_len) |
31 | 0 | { |
32 | 0 | m_hash->update(secret, secret_len); |
33 | 0 | m_hash->update_be(counter++); |
34 | 0 | m_hash->update(label, label_len); |
35 | 0 | m_hash->update(salt, salt_len); |
36 | 0 | m_hash->final(h); |
37 | |
|
38 | 0 | const size_t added = std::min(h.size(), key_len - offset); |
39 | 0 | copy_mem(&key[offset], h.data(), added); |
40 | 0 | offset += added; |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | | } |