Coverage Report

Created: 2021-05-04 09:02

/src/botan/src/lib/kdf/prf_tls/prf_tls.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* TLSv1.2 PRF
3
* (C) 2004-2010 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/prf_tls.h>
9
#include <botan/exceptn.h>
10
11
namespace Botan {
12
13
namespace {
14
15
/*
16
* TLS PRF P_hash function
17
*/
18
void P_hash(uint8_t out[], size_t out_len,
19
            MessageAuthenticationCode& mac,
20
            const uint8_t secret[], size_t secret_len,
21
            const uint8_t salt[], size_t salt_len)
22
21.5k
   {
23
21.5k
   try
24
21.5k
      {
25
21.5k
      mac.set_key(secret, secret_len);
26
21.5k
      }
27
21.5k
   catch(Invalid_Key_Length&)
28
0
      {
29
0
      throw Internal_Error("The premaster secret of " +
30
0
                           std::to_string(secret_len) +
31
0
                           " bytes is too long for the PRF");
32
0
      }
33
34
21.5k
   secure_vector<uint8_t> A(salt, salt + salt_len);
35
21.5k
   secure_vector<uint8_t> h;
36
37
21.5k
   size_t offset = 0;
38
39
79.1k
   while(offset != out_len)
40
57.5k
      {
41
57.5k
      A = mac.process(A);
42
43
57.5k
      mac.update(A);
44
57.5k
      mac.update(salt, salt_len);
45
57.5k
      mac.final(h);
46
47
57.5k
      const size_t writing = std::min(h.size(), out_len - offset);
48
57.5k
      xor_buf(&out[offset], h.data(), writing);
49
57.5k
      offset += writing;
50
57.5k
      }
51
21.5k
   }
52
53
}
54
55
void TLS_12_PRF::kdf(uint8_t key[], size_t key_len,
56
                     const uint8_t secret[], size_t secret_len,
57
                     const uint8_t salt[], size_t salt_len,
58
                     const uint8_t label[], size_t label_len) const
59
21.5k
   {
60
21.5k
   secure_vector<uint8_t> msg;
61
62
21.5k
   msg.reserve(label_len + salt_len);
63
21.5k
   msg += std::make_pair(label, label_len);
64
21.5k
   msg += std::make_pair(salt, salt_len);
65
66
21.5k
   P_hash(key, key_len, *m_mac, secret, secret_len, msg.data(), msg.size());
67
21.5k
   }
68
69
}