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