Botan 3.9.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* (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/mem_ops.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/stl_util.h>
15
16namespace Botan {
17
18/*
19* TLS PRF P_hash function
20*/
21void TLS_12_PRF::perform_kdf(std::span<uint8_t> key,
22 std::span<const uint8_t> secret,
23 std::span<const uint8_t> salt,
24 std::span<const uint8_t> label) const {
25 try {
26 m_mac->set_key(secret);
27 } catch(Invalid_Key_Length&) {
28 throw Internal_Error(fmt("The premaster secret of {} bytes is too long for TLS-PRF", secret.size()));
29 }
30
31 auto A = concat<secure_vector<uint8_t>>(label, salt);
33
34 BufferStuffer o(key);
35 while(!o.full()) {
36 A = m_mac->process(A);
37
38 m_mac->update(A);
39 m_mac->update(label);
40 m_mac->update(salt);
41 m_mac->final(h);
42
43 const size_t writing = std::min(h.size(), o.remaining_capacity());
44 xor_buf(o.next(writing), std::span{h}.first(writing));
45 }
46}
47
48std::string TLS_12_PRF::name() const {
49 return fmt("TLS-12-PRF({})", m_mac->name());
50}
51
52std::unique_ptr<KDF> TLS_12_PRF::new_object() const {
53 return std::make_unique<TLS_12_PRF>(m_mac->new_object());
54}
55
56} // namespace Botan
std::unique_ptr< KDF > new_object() const override
Definition prf_tls.cpp:52
std::string name() const override
Definition prf_tls.cpp:48
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:255
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:69