Botan 3.9.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 33 of file tls_record.cpp.

38 {
39 // NOLINTBEGIN(*-prefer-member-initializer)
40 m_nonce_format = suite.nonce_format();
41 m_nonce_bytes_from_record = suite.nonce_bytes_from_record(version);
42 m_nonce_bytes_from_handshake = suite.nonce_bytes_from_handshake();
43
44 const secure_vector<uint8_t>& aead_key = keys.aead_key(side);
45 m_nonce = keys.nonce(side);
46 // NOLINTEND(*-prefer-member-initializer)
47
48 BOTAN_ASSERT_NOMSG(m_nonce.size() == m_nonce_bytes_from_handshake);
49
51#if defined(BOTAN_HAS_TLS_CBC)
52 // legacy CBC+HMAC mode
53 auto mac = MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", suite.mac_algo()));
54 auto cipher = BlockCipher::create_or_throw(suite.cipher_algo());
55
56 if(our_side) {
57 m_aead = std::make_unique<TLS_CBC_HMAC_AEAD_Encryption>(std::move(cipher),
58 std::move(mac),
59 suite.cipher_keylen(),
60 suite.mac_keylen(),
61 version,
62 uses_encrypt_then_mac);
63 } else {
64 m_aead = std::make_unique<TLS_CBC_HMAC_AEAD_Decryption>(std::move(cipher),
65 std::move(mac),
66 suite.cipher_keylen(),
67 suite.mac_keylen(),
68 version,
69 uses_encrypt_then_mac);
70 }
71
72#else
73 BOTAN_UNUSED(uses_encrypt_then_mac);
74 throw Internal_Error("Negotiated disabled TLS CBC+HMAC ciphersuite");
75#endif
77#if defined(BOTAN_HAS_TLS_NULL)
78 auto mac = MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", suite.mac_algo()));
79
80 if(our_side) {
81 m_aead = std::make_unique<TLS_NULL_HMAC_AEAD_Encryption>(std::move(mac), suite.mac_keylen());
82 } else {
83 m_aead = std::make_unique<TLS_NULL_HMAC_AEAD_Decryption>(std::move(mac), suite.mac_keylen());
84 }
85#else
86 throw Internal_Error("Negotiated disabled TLS NULL ciphersuite");
87#endif
88 } else {
89 m_aead =
91 }
92
93 m_aead->set_key(aead_key);
94}
#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:44
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:148
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 129 of file tls_record.cpp.

129 {
130 switch(m_nonce_format) {
132 return std::vector<uint8_t>{};
133 }
135 if(nonce_bytes_from_record() == 0 && !m_nonce.empty()) {
136 std::vector<uint8_t> nonce;
137 nonce.swap(m_nonce);
138 return nonce;
139 }
140 if(record_len < nonce_bytes_from_record()) {
141 throw Decoding_Error("Invalid CBC packet too short to be valid");
142 }
143 std::vector<uint8_t> nonce(record, record + nonce_bytes_from_record());
144 return nonce;
145 }
147 std::vector<uint8_t> nonce(12);
148 store_be(seq, nonce.data() + 4);
149 xor_buf(nonce, m_nonce.data(), m_nonce.size());
150 return nonce;
151 }
153 BOTAN_ASSERT_NOMSG(m_nonce.size() == 4);
154 if(record_len < nonce_bytes_from_record()) {
155 throw Decoding_Error("Invalid AEAD packet too short to be valid");
156 }
157 std::vector<uint8_t> nonce(12);
158 copy_mem(&nonce[0], m_nonce.data(), 4); // NOLINT(*container-data-pointer)
160 return nonce;
161 }
162 }
163
164 throw Invalid_State("Unknown nonce format specified");
165}
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 96 of file tls_record.cpp.

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

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 167 of file tls_record.cpp.

170 {
171 std::vector<uint8_t> ad(13);
172
173 store_be(msg_sequence, &ad[0]); // NOLINT(*container-data-pointer)
174 ad[8] = static_cast<uint8_t>(msg_type);
175 ad[9] = version.major_version();
176 ad[10] = version.minor_version();
177 ad[11] = get_byte<0>(msg_length);
178 ad[12] = get_byte<1>(msg_length);
179
180 return ad;
181}
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: