11#include <botan/internal/tls_cbc.h>
13#include <botan/internal/cbc.h>
15#include <botan/tls_alert.h>
16#include <botan/tls_exceptn.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/loadstor.h>
19#include <botan/internal/rounding.h>
27 std::unique_ptr<BlockCipher> cipher,
28 std::unique_ptr<MessageAuthenticationCode> mac,
32 bool use_encrypt_then_mac) :
34 m_mac_name(mac->
name()),
35 m_cipher_keylen(cipher_keylen),
36 m_mac_keylen(mac_keylen),
37 m_use_encrypt_then_mac(use_encrypt_then_mac) {
39 m_block_size = cipher->block_size();
41 m_iv_size = m_block_size;
45 m_mac = std::move(
mac);
47 auto null_padding = std::make_unique<Null_Padding>();
49 m_cbc = std::make_unique<CBC_Encryption>(std::move(cipher), std::move(null_padding));
51 m_cbc = std::make_unique<CBC_Decryption>(std::move(cipher), std::move(null_padding));
68 return "TLS_CBC(" + m_cipher_name +
"," + m_mac_name +
")";
80 if(m_cbc_state.empty()) {
94void TLS_CBC_HMAC_AEAD_Mode::key_schedule(std::span<const uint8_t> key) {
97 if(key.size() != m_cipher_keylen + m_mac_keylen) {
102 cbc().
set_key(key.subspan(m_mac_keylen, m_cipher_keylen));
105void TLS_CBC_HMAC_AEAD_Mode::start_msg(
const uint8_t nonce[],
size_t nonce_len) {
107 throw Invalid_IV_Length(
name(), nonce_len);
113 m_cbc_state.assign(nonce, nonce + nonce_len);
117size_t TLS_CBC_HMAC_AEAD_Mode::process_msg(uint8_t buf[],
size_t sz) {
118 m_msg.insert(m_msg.end(), buf, buf + sz);
123 std::vector<uint8_t> ad = m_ad;
131 BOTAN_ARG_CHECK(idx == 0,
"TLS 1.2 CBC/HMAC: cannot handle non-zero index in set_associated_data_n");
132 if(ad.size() != 13) {
135 m_ad.assign(ad.begin(), ad.end());
153 size_t padding_length) {
157 buffer.resize(buffer.size() + padding_length);
159 const uint8_t padding_val =
static_cast<uint8_t
>(padding_length - 1);
165 const size_t last_block_starts = buffer.size() -
block_size();
166 const size_t padding_starts = buffer.size() - padding_length;
167 for(
size_t i = last_block_starts; i != buffer.size(); ++i) {
169 buffer[i] = add_padding.select(padding_val, buffer[i]);
177 cbc().
process(&buffer[offset], buffer.size() - offset);
179 cbc_state().assign(buffer.data() + (buffer.size() -
block_size()), buffer.data() + buffer.size());
190 const size_t msg_size =
msg().size();
196 const uint8_t padding_val =
static_cast<uint8_t
>(enc_size - input_size);
197 const size_t padding_length =
static_cast<size_t>(padding_val) + 1;
199 buffer.reserve(offset + msg_size + padding_length +
tag_size());
200 buffer.resize(offset + msg_size);
212 cbc_encrypt_record(buffer, offset, padding_length);
214 buffer.resize(buffer.size() +
tag_size());
220 buffer.resize(buffer.size() +
tag_size());
222 cbc_encrypt_record(buffer, offset, padding_length);
238 if(record_len == 0 || record_len > 0xFFFF) {
242 const uint16_t rec16 =
static_cast<uint16_t
>(record_len);
249 const uint16_t to_check = std::min<uint16_t>(256,
static_cast<uint16_t
>(record_len));
250 const uint8_t pad_byte = record[record_len - 1];
251 const uint16_t pad_bytes = 1 + pad_byte;
255 for(uint16_t i = rec16 - to_check; i != rec16; ++i) {
256 const uint16_t offset = rec16 - i;
259 pad_invalid |= in_pad_range & ~pad_correct;
262 return pad_invalid.if_not_set_return(pad_bytes);
265void TLS_CBC_HMAC_AEAD_Decryption::cbc_decrypt_record(uint8_t record_contents[],
size_t record_len) {
266 if(record_len == 0 || record_len %
block_size() != 0) {
267 throw Decoding_Error(
"Received TLS CBC ciphertext with invalid length");
271 cbc_state().assign(record_contents + record_len -
block_size(), record_contents + record_len);
325void TLS_CBC_HMAC_AEAD_Decryption::perform_additional_compressions(
size_t plen,
size_t padlen) {
327 uint16_t max_bytes_in_first_block;
328 if(
mac().
name() ==
"HMAC(SHA-384)") {
330 max_bytes_in_first_block = 111;
333 max_bytes_in_first_block = 55;
336 const uint16_t L1 =
static_cast<uint16_t
>(13 + plen -
tag_size());
340 const uint16_t L2 =
static_cast<uint16_t
>(13 + plen - padlen -
tag_size());
344 const uint16_t max_compresssions = ((L1 +
block_size - 1 - max_bytes_in_first_block) /
block_size);
345 const uint16_t current_compressions = ((L2 +
block_size - 1 - max_bytes_in_first_block) /
block_size);
347 const uint16_t add_compressions = max_compresssions - current_compressions;
352 const uint16_t data_len =
block_size * add_compressions + equal * max_bytes_in_first_block;
353 std::vector<uint8_t> data(data_len);
360 buffer.resize(offset);
362 const size_t record_len =
msg().size();
363 uint8_t* record_contents =
msg().data();
367 throw TLS_Exception(Alert::BadRecordMac,
"Message authentication failure");
371 const size_t enc_size = record_len -
tag_size();
372 const size_t enc_iv_size = enc_size +
iv_size();
382 std::vector<uint8_t> mac_buf(
tag_size());
385 const size_t mac_offset = enc_size;
389 if(!mac_ok.as_bool()) {
390 throw TLS_Exception(Alert::BadRecordMac,
"Message authentication failure");
393 cbc_decrypt_record(record_contents, enc_size);
400 throw TLS_Exception(Alert::BadRecordMac,
"Message authentication failure");
403 const uint8_t* plaintext_block = &record_contents[0];
404 const size_t plaintext_length = enc_size - pad_size;
406 buffer.insert(buffer.end(), plaintext_block, plaintext_block + plaintext_length);
408 cbc_decrypt_record(record_contents, record_len);
424 const auto size_ok_mask =
427 pad_size = size_ok_mask.if_set_return(pad_size);
437 const uint8_t* plaintext_block = &record_contents[0];
438 const uint16_t plaintext_length =
static_cast<uint16_t
>(record_len -
tag_size() - pad_size);
441 mac().
update(plaintext_block, plaintext_length);
443 std::vector<uint8_t> mac_buf(
tag_size());
446 const size_t mac_offset = record_len - (
tag_size() + pad_size);
454 if(ok_mask.as_bool()) {
455 buffer.insert(buffer.end(), plaintext_block, plaintext_block + plaintext_length);
457 perform_additional_compressions(record_len, pad_size);
467 throw TLS_Exception(Alert::BadRecordMac,
"Message authentication failure");
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
void update(const uint8_t in[], size_t length)
virtual size_t output_length() const =0
void final(uint8_t out[])
static constexpr Mask< T > is_lte(T x, T y)
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > is_equal(T x, T y)
static constexpr Mask< T > is_lt(T x, T y)
void start(std::span< const uint8_t > nonce)
size_t process(std::span< uint8_t > msg)
void set_key(const SymmetricKey &key)
virtual bool has_keying_material() const =0
bool is_datagram_protocol() const
size_t output_length(size_t input_length) const override
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
size_t output_length(size_t input_length) const override
size_t update_granularity() const final
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
size_t block_size() const
secure_vector< uint8_t > & cbc_state()
Key_Length_Specification key_spec() const final
bool has_keying_material() const final
size_t ideal_granularity() const final
std::vector< uint8_t > assoc_data_with_len(uint16_t len)
std::string name() const final
bool use_encrypt_then_mac() const
bool is_datagram_protocol() const
std::vector< uint8_t > & assoc_data()
secure_vector< uint8_t > & msg()
TLS_CBC_HMAC_AEAD_Mode(Cipher_Dir direction, std::unique_ptr< BlockCipher > cipher, std::unique_ptr< MessageAuthenticationCode > mac, size_t cipher_keylen, size_t mac_keylen, Protocol_Version version, bool use_encrypt_then_mac)
MessageAuthenticationCode & mac() const
Cipher_Mode & cbc() const
size_t tag_size() const final
bool valid_nonce_length(size_t nl) const final
std::string m_cipher_name
int(* update)(CTX *, const void *, CC_LONG len)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
uint16_t check_tls_cbc_padding(const uint8_t record[], size_t record_len)
constexpr uint8_t get_byte(T input)
constexpr size_t round_up(size_t n, size_t align_to)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)