Botan 3.3.0
Crypto and TLS for C&
prf_tls.cpp
Go to the documentation of this file.
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
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12
13namespace Botan {
14
15namespace {
16
17/*
18* TLS PRF P_hash function
19*/
20void P_hash(uint8_t out[],
21 size_t out_len,
22 MessageAuthenticationCode& mac,
23 const uint8_t secret[],
24 size_t secret_len,
25 const uint8_t salt[],
26 size_t salt_len) {
27 try {
28 mac.set_key(secret, secret_len);
29 } catch(Invalid_Key_Length&) {
30 throw Internal_Error(fmt("The premaster secret of {} bytes is too long for TLS-PRF", secret_len));
31 }
32
33 secure_vector<uint8_t> A(salt, salt + salt_len);
34 secure_vector<uint8_t> h;
35
36 size_t offset = 0;
37
38 while(offset != out_len) {
39 A = mac.process(A);
40
41 mac.update(A);
42 mac.update(salt, salt_len);
43 mac.final(h);
44
45 const size_t writing = std::min(h.size(), out_len - offset);
46 xor_buf(&out[offset], h.data(), writing);
47 offset += writing;
48 }
49}
50
51} // namespace
52
53std::string TLS_12_PRF::name() const {
54 return fmt("TLS-12-PRF({})", m_mac->name());
55}
56
57std::unique_ptr<KDF> TLS_12_PRF::new_object() const {
58 return std::make_unique<TLS_12_PRF>(m_mac->new_object());
59}
60
61void TLS_12_PRF::kdf(uint8_t key[],
62 size_t key_len,
63 const uint8_t secret[],
64 size_t secret_len,
65 const uint8_t salt[],
66 size_t salt_len,
67 const uint8_t label[],
68 size_t label_len) const {
70
71 msg.reserve(label_len + salt_len);
72 msg += std::make_pair(label, label_len);
73 msg += std::make_pair(salt, salt_len);
74
75 P_hash(key, key_len, *m_mac, secret, secret_len, msg.data(), msg.size());
76}
77
78} // namespace Botan
std::unique_ptr< KDF > new_object() const override
Definition prf_tls.cpp:57
void kdf(uint8_t key[], size_t key_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len) const override
Definition prf_tls.cpp:61
std::string name() const override
Definition prf_tls.cpp:53
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:340
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61