Coverage Report

Created: 2024-06-28 06:19

/src/botan/src/lib/kdf/kdf2/kdf2.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* KDF2
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/kdf2.h>
9
10
#include <botan/exceptn.h>
11
#include <botan/internal/fmt.h>
12
13
namespace Botan {
14
15
0
std::string KDF2::name() const {
16
0
   return fmt("KDF2({})", m_hash->name());
17
0
}
18
19
0
std::unique_ptr<KDF> KDF2::new_object() const {
20
0
   return std::make_unique<KDF2>(m_hash->new_object());
21
0
}
22
23
void KDF2::kdf(uint8_t key[],
24
               size_t key_len,
25
               const uint8_t secret[],
26
               size_t secret_len,
27
               const uint8_t salt[],
28
               size_t salt_len,
29
               const uint8_t label[],
30
0
               size_t label_len) const {
31
0
   if(key_len == 0) {
32
0
      return;
33
0
   }
34
35
0
   const size_t blocks_required = key_len / m_hash->output_length();
36
37
0
   if(blocks_required >= 0xFFFFFFFE) {
38
0
      throw Invalid_Argument("KDF2 maximum output length exceeeded");
39
0
   }
40
41
0
   uint32_t counter = 1;
42
0
   secure_vector<uint8_t> h;
43
44
0
   size_t offset = 0;
45
0
   while(offset != key_len) {
46
0
      m_hash->update(secret, secret_len);
47
0
      m_hash->update_be(counter);
48
0
      m_hash->update(label, label_len);
49
0
      m_hash->update(salt, salt_len);
50
0
      m_hash->final(h);
51
52
0
      const size_t added = std::min(h.size(), key_len - offset);
53
0
      copy_mem(&key[offset], h.data(), added);
54
0
      offset += added;
55
56
0
      counter += 1;
57
0
      BOTAN_ASSERT_NOMSG(counter != 0);  // no overflow
58
0
   }
59
0
}
60
61
}  // namespace Botan