Botan 3.7.1
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* (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/prf_tls.h>
10
11#include <botan/exceptn.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/stl_util.h>
14
15namespace Botan {
16
17/*
18* TLS PRF P_hash function
19*/
20void TLS_12_PRF::perform_kdf(std::span<uint8_t> key,
21 std::span<const uint8_t> secret,
22 std::span<const uint8_t> salt,
23 std::span<const uint8_t> label) const {
24 try {
25 m_mac->set_key(secret);
26 } catch(Invalid_Key_Length&) {
27 throw Internal_Error(fmt("The premaster secret of {} bytes is too long for TLS-PRF", secret.size()));
28 }
29
30 auto A = concat<secure_vector<uint8_t>>(label, salt);
32
33 BufferStuffer o(key);
34 while(!o.full()) {
35 A = m_mac->process(A);
36
37 m_mac->update(A);
38 m_mac->update(label);
39 m_mac->update(salt);
40 m_mac->final(h);
41
42 const size_t writing = std::min(h.size(), o.remaining_capacity());
43 xor_buf(o.next(writing), std::span{h}.first(writing));
44 }
45}
46
47std::string TLS_12_PRF::name() const {
48 return fmt("TLS-12-PRF({})", m_mac->name());
49}
50
51std::unique_ptr<KDF> TLS_12_PRF::new_object() const {
52 return std::make_unique<TLS_12_PRF>(m_mac->new_object());
53}
54
55} // namespace Botan
std::unique_ptr< KDF > new_object() const override
Definition prf_tls.cpp:51
std::string name() const override
Definition prf_tls.cpp:47
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:263
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:342
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61