Botan  1.11.15
tls_session_key.cpp
Go to the documentation of this file.
1 /*
2 * TLS Session Key
3 * (C) 2004-2006,2011 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/internal/tls_session_key.h>
9 #include <botan/internal/tls_handshake_state.h>
10 #include <botan/internal/tls_messages.h>
11 
12 namespace Botan {
13 
14 namespace TLS {
15 
16 /**
17 * Session_Keys Constructor
18 */
19 Session_Keys::Session_Keys(const Handshake_State* state,
20  const secure_vector<byte>& pre_master_secret,
21  bool resuming)
22  {
23  const size_t cipher_keylen = state->ciphersuite().cipher_keylen();
24  const size_t mac_keylen = state->ciphersuite().mac_keylen();
25  const size_t cipher_nonce_bytes = state->ciphersuite().nonce_bytes_from_handshake();
26 
27  const size_t prf_gen = 2 * (mac_keylen + cipher_keylen + cipher_nonce_bytes);
28 
29  const byte MASTER_SECRET_MAGIC[] = {
30  0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74 };
31 
32  const byte KEY_GEN_MAGIC[] = {
33  0x6B, 0x65, 0x79, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6E, 0x73, 0x69, 0x6F, 0x6E };
34 
35  std::unique_ptr<KDF> prf(state->protocol_specific_prf());
36 
37  if(resuming)
38  {
39  master_sec = pre_master_secret;
40  }
41  else
42  {
43  secure_vector<byte> salt;
44  salt += std::make_pair(MASTER_SECRET_MAGIC, sizeof(MASTER_SECRET_MAGIC));
45  salt += state->client_hello()->random();
46  salt += state->server_hello()->random();
47 
48  master_sec = prf->derive_key(48, pre_master_secret, salt);
49  }
50 
51  secure_vector<byte> salt;
52  salt += std::make_pair(KEY_GEN_MAGIC, sizeof(KEY_GEN_MAGIC));
53  salt += state->server_hello()->random();
54  salt += state->client_hello()->random();
55 
56  SymmetricKey keyblock = prf->derive_key(prf_gen, master_sec, salt);
57 
58  const byte* key_data = keyblock.begin();
59 
60  c_mac = SymmetricKey(key_data, mac_keylen);
61  key_data += mac_keylen;
62 
63  s_mac = SymmetricKey(key_data, mac_keylen);
64  key_data += mac_keylen;
65 
66  c_cipher = SymmetricKey(key_data, cipher_keylen);
67  key_data += cipher_keylen;
68 
69  s_cipher = SymmetricKey(key_data, cipher_keylen);
70  key_data += cipher_keylen;
71 
72  c_iv = InitializationVector(key_data, cipher_nonce_bytes);
73  key_data += cipher_nonce_bytes;
74 
75  s_iv = InitializationVector(key_data, cipher_nonce_bytes);
76  }
77 
78 }
79 
80 }
OctetString SymmetricKey
Definition: symkey.h:134
uint8_t byte
Definition: types.h:31
OctetString InitializationVector
Definition: symkey.h:139