Botan 3.0.0-alpha0
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#include <botan/exceptn.h>
10
11namespace Botan {
12
13namespace {
14
15/*
16* TLS PRF P_hash function
17*/
18void 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 {
23 try
24 {
25 mac.set_key(secret, secret_len);
26 }
27 catch(Invalid_Key_Length&)
28 {
29 throw Internal_Error("The premaster secret of " +
30 std::to_string(secret_len) +
31 " bytes is too long for the PRF");
32 }
33
34 secure_vector<uint8_t> A(salt, salt + salt_len);
35 secure_vector<uint8_t> h;
36
37 size_t offset = 0;
38
39 while(offset != out_len)
40 {
41 A = mac.process(A);
42
43 mac.update(A);
44 mac.update(salt, salt_len);
45 mac.final(h);
46
47 const size_t writing = std::min(h.size(), out_len - offset);
48 xor_buf(&out[offset], h.data(), writing);
49 offset += writing;
50 }
51 }
52
53}
54
55void 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 {
61
62 msg.reserve(label_len + salt_len);
63 msg += std::make_pair(label, label_len);
64 msg += std::make_pair(salt, salt_len);
65
66 P_hash(key, key_len, *m_mac, secret, secret_len, msg.data(), msg.size());
67 }
68
69}
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:55
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:209
Definition: alg_id.cpp:13
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: mem_ops.h:255
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
size_t salt_len
Definition: x509_obj.cpp:25