Botan 3.10.0
Crypto and TLS for C&
Botan::TLS::Connection_Cipher_State Class Referencefinal

#include <tls_record.h>

Public Member Functions

AEAD_Modeaead ()
std::vector< uint8_t > aead_nonce (const uint8_t record[], size_t record_len, uint64_t seq)
std::vector< uint8_t > aead_nonce (uint64_t seq, RandomNumberGenerator &rng)
 Connection_Cipher_State (Protocol_Version version, Connection_Side which_side, bool is_our_side, const Ciphersuite &suite, const Session_Keys &keys, bool uses_encrypt_then_mac)
std::vector< uint8_t > format_ad (uint64_t seq, Record_Type type, Protocol_Version version, uint16_t ptext_length)
size_t nonce_bytes_from_handshake () const
size_t nonce_bytes_from_record () const
Nonce_Format nonce_format () const

Detailed Description

TLS Cipher State

Definition at line 32 of file tls_record.h.

Constructor & Destructor Documentation

◆ Connection_Cipher_State()

Botan::TLS::Connection_Cipher_State::Connection_Cipher_State ( Protocol_Version version,
Connection_Side which_side,
bool is_our_side,
const Ciphersuite & suite,
const Session_Keys & keys,
bool uses_encrypt_then_mac )

Initialize a new cipher state

Definition at line 30 of file tls_record.cpp.

35 {
36 // NOLINTBEGIN(*-prefer-member-initializer)
37 m_nonce_format = suite.nonce_format();
38 m_nonce_bytes_from_record = suite.nonce_bytes_from_record(version);
39 m_nonce_bytes_from_handshake = suite.nonce_bytes_from_handshake();
40
41 const secure_vector<uint8_t>& aead_key = keys.aead_key(side);
42 m_nonce = keys.nonce(side);
43 // NOLINTEND(*-prefer-member-initializer)
44
45 BOTAN_ASSERT_NOMSG(m_nonce.size() == m_nonce_bytes_from_handshake);
46
48#if defined(BOTAN_HAS_TLS_CBC)
49 // legacy CBC+HMAC mode
50 auto mac = MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", suite.mac_algo()));
51 auto cipher = BlockCipher::create_or_throw(suite.cipher_algo());
52
53 if(our_side) {
54 m_aead = std::make_unique<TLS_CBC_HMAC_AEAD_Encryption>(std::move(cipher),
55 std::move(mac),
56 suite.cipher_keylen(),
57 suite.mac_keylen(),
58 version,
59 uses_encrypt_then_mac);
60 } else {
61 m_aead = std::make_unique<TLS_CBC_HMAC_AEAD_Decryption>(std::move(cipher),
62 std::move(mac),
63 suite.cipher_keylen(),
64 suite.mac_keylen(),
65 version,
66 uses_encrypt_then_mac);
67 }
68
69#else
70 BOTAN_UNUSED(uses_encrypt_then_mac);
71 throw Internal_Error("Negotiated disabled TLS CBC+HMAC ciphersuite");
72#endif
74#if defined(BOTAN_HAS_TLS_NULL)
75 auto mac = MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", suite.mac_algo()));
76
77 if(our_side) {
78 m_aead = std::make_unique<TLS_NULL_HMAC_AEAD_Encryption>(std::move(mac), suite.mac_keylen());
79 } else {
80 m_aead = std::make_unique<TLS_NULL_HMAC_AEAD_Decryption>(std::move(mac), suite.mac_keylen());
81 }
82#else
83 throw Internal_Error("Negotiated disabled TLS NULL ciphersuite");
84#endif
85 } else {
86 m_aead =
88 }
89
90 m_aead->set_key(aead_key);
91}
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:48
static std::unique_ptr< BlockCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:147
Nonce_Format nonce_format() const
Definition tls_record.h:59
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69

References Botan::TLS::Session_Keys::aead_key(), BOTAN_ASSERT_NOMSG, BOTAN_UNUSED, Botan::TLS::CBC_MODE, Botan::TLS::Ciphersuite::cipher_algo(), Botan::TLS::Ciphersuite::cipher_keylen(), Botan::AEAD_Mode::create_or_throw(), Botan::BlockCipher::create_or_throw(), Botan::MessageAuthenticationCode::create_or_throw(), Botan::Decryption, Botan::Encryption, Botan::fmt(), Botan::TLS::Ciphersuite::mac_algo(), Botan::TLS::Ciphersuite::mac_keylen(), Botan::TLS::Session_Keys::nonce(), Botan::TLS::Ciphersuite::nonce_bytes_from_handshake(), Botan::TLS::Ciphersuite::nonce_bytes_from_record(), Botan::TLS::Ciphersuite::nonce_format(), nonce_format(), and Botan::TLS::NULL_CIPHER.

Member Function Documentation

◆ aead()

AEAD_Mode & Botan::TLS::Connection_Cipher_State::aead ( )
inline

Definition at line 44 of file tls_record.h.

44 {
45 BOTAN_ASSERT_NONNULL(m_aead.get());
46 return *m_aead;
47 }
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114

References BOTAN_ASSERT_NONNULL.

Referenced by Botan::TLS::write_record().

◆ aead_nonce() [1/2]

std::vector< uint8_t > Botan::TLS::Connection_Cipher_State::aead_nonce ( const uint8_t record[],
size_t record_len,
uint64_t seq )

Definition at line 126 of file tls_record.cpp.

126 {
127 switch(m_nonce_format) {
129 return std::vector<uint8_t>{};
130 }
132 if(nonce_bytes_from_record() == 0 && !m_nonce.empty()) {
133 std::vector<uint8_t> nonce;
134 nonce.swap(m_nonce);
135 return nonce;
136 }
137 if(record_len < nonce_bytes_from_record()) {
138 throw Decoding_Error("Invalid CBC packet too short to be valid");
139 }
140 std::vector<uint8_t> nonce(record, record + nonce_bytes_from_record());
141 return nonce;
142 }
144 std::vector<uint8_t> nonce(12);
145 store_be(seq, nonce.data() + 4);
146 xor_buf(nonce, m_nonce.data(), m_nonce.size());
147 return nonce;
148 }
150 BOTAN_ASSERT_NOMSG(m_nonce.size() == 4);
151 if(record_len < nonce_bytes_from_record()) {
152 throw Decoding_Error("Invalid AEAD packet too short to be valid");
153 }
154 std::vector<uint8_t> nonce(12);
155 copy_mem(&nonce[0], m_nonce.data(), 4); // NOLINT(*container-data-pointer)
157 return nonce;
158 }
159 }
160
161 throw Invalid_State("Unknown nonce format specified");
162}
size_t nonce_bytes_from_handshake() const
Definition tls_record.h:55
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:145
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:342
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745

References Botan::TLS::AEAD_IMPLICIT_4, Botan::TLS::AEAD_XOR_12, BOTAN_ASSERT_NOMSG, Botan::TLS::CBC_MODE, Botan::copy_mem(), nonce_bytes_from_handshake(), nonce_bytes_from_record(), Botan::TLS::NULL_CIPHER, Botan::store_be(), and Botan::xor_buf().

◆ aead_nonce() [2/2]

std::vector< uint8_t > Botan::TLS::Connection_Cipher_State::aead_nonce ( uint64_t seq,
RandomNumberGenerator & rng )

Definition at line 93 of file tls_record.cpp.

93 {
94 switch(m_nonce_format) {
96 return std::vector<uint8_t>{};
97 }
99 if(!m_nonce.empty()) {
100 std::vector<uint8_t> nonce;
101 nonce.swap(m_nonce);
102 return nonce;
103 }
104 std::vector<uint8_t> nonce(nonce_bytes_from_record());
105 rng.randomize(nonce.data(), nonce.size());
106 return nonce;
107 }
109 std::vector<uint8_t> nonce(12);
110 store_be(seq, nonce.data() + 4);
111 xor_buf(nonce, m_nonce.data(), m_nonce.size());
112 return nonce;
113 }
115 BOTAN_ASSERT_NOMSG(m_nonce.size() == 4);
116 std::vector<uint8_t> nonce(12);
117 copy_mem(&nonce[0], m_nonce.data(), 4); // NOLINT(*container-data-pointer)
118 store_be(seq, &nonce[nonce_bytes_from_handshake()]);
119 return nonce;
120 }
121 }
122
123 throw Invalid_State("Unknown nonce format specified");
124}

References Botan::TLS::AEAD_IMPLICIT_4, Botan::TLS::AEAD_XOR_12, BOTAN_ASSERT_NOMSG, Botan::TLS::CBC_MODE, Botan::copy_mem(), nonce_bytes_from_handshake(), nonce_bytes_from_record(), Botan::TLS::NULL_CIPHER, Botan::RandomNumberGenerator::randomize(), Botan::store_be(), and Botan::xor_buf().

Referenced by Botan::TLS::write_record().

◆ format_ad()

std::vector< uint8_t > Botan::TLS::Connection_Cipher_State::format_ad ( uint64_t seq,
Record_Type type,
Protocol_Version version,
uint16_t ptext_length )

Definition at line 164 of file tls_record.cpp.

167 {
168 std::vector<uint8_t> ad(13);
169
170 store_be(msg_sequence, &ad[0]); // NOLINT(*container-data-pointer)
171 ad[8] = static_cast<uint8_t>(msg_type);
172 ad[9] = version.major_version();
173 ad[10] = version.minor_version();
174 ad[11] = get_byte<0>(msg_length);
175 ad[12] = get_byte<1>(msg_length);
176
177 return ad;
178}
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79

References Botan::get_byte(), Botan::TLS::Protocol_Version::major_version(), Botan::TLS::Protocol_Version::minor_version(), and Botan::store_be().

Referenced by Botan::TLS::write_record().

◆ nonce_bytes_from_handshake()

size_t Botan::TLS::Connection_Cipher_State::nonce_bytes_from_handshake ( ) const
inline

Definition at line 55 of file tls_record.h.

55{ return m_nonce_bytes_from_handshake; }

Referenced by aead_nonce(), aead_nonce(), and Botan::TLS::write_record().

◆ nonce_bytes_from_record()

size_t Botan::TLS::Connection_Cipher_State::nonce_bytes_from_record ( ) const
inline

Definition at line 57 of file tls_record.h.

57{ return m_nonce_bytes_from_record; }

Referenced by aead_nonce(), aead_nonce(), and Botan::TLS::write_record().

◆ nonce_format()

Nonce_Format Botan::TLS::Connection_Cipher_State::nonce_format ( ) const
inline

Definition at line 59 of file tls_record.h.

59{ return m_nonce_format; }

Referenced by Connection_Cipher_State(), and Botan::TLS::write_record().


The documentation for this class was generated from the following files: