| 1 |
/* |
| 2 |
* TLS CBC+HMAC AEAD |
| 3 |
* (C) 2016 Jack Lloyd |
| 4 |
* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity |
| 5 |
* |
| 6 |
* Botan is released under the Simplified BSD License (see license.txt) |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef BOTAN_TLS_CBC_HMAC_AEAD_H_ |
| 10 |
#define BOTAN_TLS_CBC_HMAC_AEAD_H_ |
| 11 |
|
| 12 |
#include <botan/aead.h> |
| 13 |
|
| 14 |
namespace Botan { |
| 15 |
|
| 16 |
class BlockCipher; |
| 17 |
class MessageAuthenticationCode; |
| 18 |
|
| 19 |
} // namespace Botan |
| 20 |
|
| 21 |
namespace Botan::TLS { |
| 22 |
|
| 23 |
class Protocol_Version; |
| 24 |
|
| 25 |
/** |
| 26 |
* TLS CBC+HMAC AEAD base class (GenericBlockCipher in TLS spec) |
| 27 |
* This is the weird TLS-specific mode, not for general consumption. |
| 28 |
*/ |
| 29 |
class BOTAN_TEST_API TLS_CBC_HMAC_AEAD_Mode : public AEAD_Mode { |
| 30 |
public: |
| 31 |
std::string name() const final; |
| 32 |
|
| 33 |
void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) override; |
| 34 |
|
| 35 |
size_t update_granularity() const final; |
| 36 |
|
| 37 |
size_t ideal_granularity() const final; |
| 38 |
|
| 39 |
Key_Length_Specification key_spec() const final; |
| 40 |
|
| 41 |
bool valid_nonce_length(size_t nl) const final; |
| 42 |
|
| 43 |
size_t tag_size() const final { return m_tag_size; } |
| 44 |
|
| 45 |
size_t default_nonce_length() const final { return m_iv_size; } |
| 46 |
|
| 47 |
void clear() final; |
| 48 |
|
| 49 |
void reset() final; |
| 50 |
|
| 51 |
bool has_keying_material() const final; |
| 52 |
|
| 53 |
~TLS_CBC_HMAC_AEAD_Mode() override; |
| 54 |
|
| 55 |
TLS_CBC_HMAC_AEAD_Mode(const TLS_CBC_HMAC_AEAD_Mode& other) = delete; |
| 56 |
TLS_CBC_HMAC_AEAD_Mode(TLS_CBC_HMAC_AEAD_Mode&& other) = delete; |
| 57 |
|
| 58 |
TLS_CBC_HMAC_AEAD_Mode& operator=(const TLS_CBC_HMAC_AEAD_Mode& other) = delete; |
| 59 |
TLS_CBC_HMAC_AEAD_Mode& operator=(TLS_CBC_HMAC_AEAD_Mode&& other) = delete; |
| 60 |
|
| 61 |
protected: |
| 62 |
TLS_CBC_HMAC_AEAD_Mode(Cipher_Dir direction, |
| 63 |
std::unique_ptr<BlockCipher> cipher, |
| 64 |
std::unique_ptr<MessageAuthenticationCode> mac, |
| 65 |
size_t cipher_keylen, |
| 66 |
size_t mac_keylen, |
| 67 |
const Protocol_Version& version, |
| 68 |
bool use_encrypt_then_mac); |
| 69 |
|
| 70 |
size_t cipher_keylen() const { return m_cipher_keylen; } |
| 71 |
|
| 72 |
size_t mac_keylen() const { return m_mac_keylen; } |
| 73 |
|
| 74 |
size_t iv_size() const { return m_iv_size; } |
| 75 |
|
| 76 |
size_t block_size() const { return m_block_size; } |
| 77 |
|
| 78 |
bool use_encrypt_then_mac() const { return m_use_encrypt_then_mac; } |
| 79 |
|
| 80 |
bool is_datagram_protocol() const { return m_is_datagram; } |
| 81 |
|
| 82 |
Cipher_Mode& cbc() const { return *m_cbc; } |
| 83 |
|
| 84 |
MessageAuthenticationCode& mac() const { return *m_mac; } |
| 85 |
|
| 86 |
secure_vector<uint8_t>& cbc_state() { return m_cbc_state; } |
| 87 |
|
| 88 |
std::vector<uint8_t>& assoc_data() { return m_ad; } |
| 89 |
|
| 90 |
secure_vector<uint8_t>& msg() { return m_msg; } |
| 91 |
|
| 92 |
std::vector<uint8_t> assoc_data_with_len(uint16_t len); |
| 93 |
|
| 94 |
private: |
| 95 |
void start_msg(const uint8_t nonce[], size_t nonce_len) final; |
| 96 |
size_t process_msg(uint8_t buf[], size_t sz) final; |
| 97 |
|
| 98 |
void key_schedule(std::span<const uint8_t> key) final; |
| 99 |
|
| 100 |
std::unique_ptr<Cipher_Mode> m_cbc; |
| 101 |
std::unique_ptr<MessageAuthenticationCode> m_mac; |
| 102 |
|
| 103 |
const std::string m_cipher_name; |
| 104 |
const std::string m_mac_name; |
| 105 |
size_t m_cipher_keylen; |
| 106 |
size_t m_block_size; |
| 107 |
size_t m_iv_size; |
| 108 |
size_t m_mac_keylen; |
| 109 |
size_t m_tag_size; |
| 110 |
bool m_use_encrypt_then_mac; |
| 111 |
bool m_is_datagram; |
| 112 |
|
| 113 |
secure_vector<uint8_t> m_cbc_state; |
| 114 |
std::vector<uint8_t> m_ad; |
| 115 |
secure_vector<uint8_t> m_msg; |
| 116 |
}; |
| 117 |
|
| 118 |
/** |
| 119 |
* TLS_CBC_HMAC_AEAD Encryption |
| 120 |
*/ |
| 121 |
class BOTAN_TEST_API TLS_CBC_HMAC_AEAD_Encryption final : public TLS_CBC_HMAC_AEAD_Mode { |
| 122 |
public: |
| 123 |
/** |
| 124 |
*/ |
| 125 |
TLS_CBC_HMAC_AEAD_Encryption(std::unique_ptr<BlockCipher> cipher, |
| 126 |
std::unique_ptr<MessageAuthenticationCode> mac, |
| 127 |
size_t cipher_keylen, |
| 128 |
size_t mac_keylen, |
| 129 |
const Protocol_Version& version, |
| 130 |
bool use_encrypt_then_mac); |
| 131 |
|
| 132 |
void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) override; |
| 133 |
|
| 134 |
size_t output_length(size_t input_length) const override; |
| 135 |
|
| 136 |
size_t minimum_final_size() const override { return 0; } |
| 137 |
|
| 138 |
private: |
| 139 |
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
| 140 |
void cbc_encrypt_record(secure_vector<uint8_t>& buffer, size_t offset, size_t padding_length); |
| 141 |
}; |
| 142 |
|
| 143 |
/** |
| 144 |
* TLS_CBC_HMAC_AEAD Decryption |
| 145 |
*/ |
| 146 |
class BOTAN_TEST_API TLS_CBC_HMAC_AEAD_Decryption final : public TLS_CBC_HMAC_AEAD_Mode { |
| 147 |
public: |
| 148 |
/** |
| 149 |
*/ |
| 150 |
TLS_CBC_HMAC_AEAD_Decryption(std::unique_ptr<BlockCipher> cipher, |
| 151 |
std::unique_ptr<MessageAuthenticationCode> mac, |
| 152 |
size_t cipher_keylen, |
| 153 |
size_t mac_keylen, |
| 154 |
const Protocol_Version& version, |
| 155 |
bool use_encrypt_then_mac); |
| 156 |
|
| 157 |
size_t output_length(size_t input_length) const override; |
| 158 |
|
| 159 |
size_t minimum_final_size() const override { return tag_size(); } |
| 160 |
|
| 161 |
private: |
| 162 |
void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
| 163 |
|
| 164 |
void cbc_decrypt_record(uint8_t record_contents[], size_t record_len); |
| 165 |
|
| 166 |
void perform_additional_compressions(size_t plen, size_t padlen); |
| 167 |
}; |
| 168 |
|
| 169 |
/** |
| 170 |
* Check the TLS padding of a record |
| 171 |
* @param record the record bits |
| 172 |
* @param record_len length of record |
| 173 |
* @return 0 if padding is invalid, otherwise padding_bytes + 1 |
| 174 |
*/ |
| 175 |
BOTAN_TEST_API uint16_t check_tls_cbc_padding(const uint8_t record[], size_t record_len); |
| 176 |
|
| 177 |
} // namespace Botan::TLS |
| 178 |
|
| 179 |
#endif |