Coverage Report

Created: 2021-05-04 09:02

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