Botan 3.14.1
Crypto and TLS for C&
tls_cbc.h
Go to the documentation of this file.
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
14namespace Botan {
15
16class BlockCipher;
18
19} // namespace Botan
20
21namespace Botan::TLS {
22
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*/
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
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 // All processing happens when the record is finished
48 bool requires_entire_message() const final { return true; }
49
50 void clear() final;
51
52 void reset() final;
53
54 bool has_keying_material() const final;
55
57
60
61 TLS_CBC_HMAC_AEAD_Mode& operator=(const TLS_CBC_HMAC_AEAD_Mode& other) = delete;
62 TLS_CBC_HMAC_AEAD_Mode& operator=(TLS_CBC_HMAC_AEAD_Mode&& other) = delete;
63
64 protected:
66 std::unique_ptr<BlockCipher> cipher,
67 std::unique_ptr<MessageAuthenticationCode> mac,
68 size_t cipher_keylen,
69 size_t mac_keylen,
70 const Protocol_Version& version,
72
73 size_t cipher_keylen() const { return m_cipher_keylen; }
74
75 size_t mac_keylen() const { return m_mac_keylen; }
76
77 size_t iv_size() const { return m_iv_size; }
78
79 size_t block_size() const { return m_block_size; }
80
81 bool use_encrypt_then_mac() const { return m_use_encrypt_then_mac; }
82
83 bool is_datagram_protocol() const { return m_is_datagram; }
84
85 Cipher_Mode& cbc() const { return *m_cbc; }
86
87 MessageAuthenticationCode& mac() const { return *m_mac; }
88
89 secure_vector<uint8_t>& cbc_state() { return m_cbc_state; }
90
91 std::vector<uint8_t>& assoc_data() { return m_ad; }
92
93 secure_vector<uint8_t>& msg() { return m_msg; }
94
95 std::vector<uint8_t> assoc_data_with_len(uint16_t len);
96
97 private:
98 void start_msg(const uint8_t nonce[], size_t nonce_len) final;
99 size_t process_msg(uint8_t buf[], size_t sz) final;
100
101 void key_schedule(std::span<const uint8_t> key) final;
102
103 std::unique_ptr<Cipher_Mode> m_cbc;
104 std::unique_ptr<MessageAuthenticationCode> m_mac;
105
106 const std::string m_cipher_name;
107 const std::string m_mac_name;
108 size_t m_cipher_keylen;
109 size_t m_block_size;
110 size_t m_iv_size;
111 size_t m_mac_keylen;
112 size_t m_tag_size;
113 bool m_use_encrypt_then_mac;
114 bool m_is_datagram;
115
116 secure_vector<uint8_t> m_cbc_state;
117 std::vector<uint8_t> m_ad;
119};
120
121/**
122* TLS_CBC_HMAC_AEAD Encryption
123*/
125 public:
126 /**
127 */
128 TLS_CBC_HMAC_AEAD_Encryption(std::unique_ptr<BlockCipher> cipher,
129 std::unique_ptr<MessageAuthenticationCode> mac,
130 size_t cipher_keylen,
131 size_t mac_keylen,
132 const Protocol_Version& version,
134
135 void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) override;
136
137 size_t output_length(size_t input_length) const override;
138
139 size_t minimum_final_size() const override { return 0; }
140
141 private:
142 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
143 void cbc_encrypt_record(secure_vector<uint8_t>& buffer, size_t offset, size_t padding_length);
144};
145
146/**
147* TLS_CBC_HMAC_AEAD Decryption
148*/
150 public:
151 /**
152 */
153 TLS_CBC_HMAC_AEAD_Decryption(std::unique_ptr<BlockCipher> cipher,
154 std::unique_ptr<MessageAuthenticationCode> mac,
155 size_t cipher_keylen,
156 size_t mac_keylen,
157 const Protocol_Version& version,
159
160 size_t output_length(size_t input_length) const override;
161
162 size_t minimum_final_size() const override { return tag_size(); }
163
164 private:
165 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
166
167 void cbc_decrypt_record(uint8_t record_contents[], size_t record_len);
168
169 void perform_additional_compressions(size_t plen, size_t padlen);
170};
171
172/**
173* Check the TLS padding of a record
174* @param record the record bits
175* @param record_len length of record
176* @return 0 if padding is invalid, otherwise padding_bytes + 1
177*/
178BOTAN_TEST_API uint16_t check_tls_cbc_padding(const uint8_t record[], size_t record_len);
179
180} // namespace Botan::TLS
181
182#endif
#define BOTAN_TEST_API
Definition api.h:41
TLS_CBC_HMAC_AEAD_Decryption(std::unique_ptr< BlockCipher > cipher, std::unique_ptr< MessageAuthenticationCode > mac, size_t cipher_keylen, size_t mac_keylen, const Protocol_Version &version, bool use_encrypt_then_mac)
Definition tls_cbc.cpp:292
size_t minimum_final_size() const override
Definition tls_cbc.h:162
size_t output_length(size_t input_length) const override
Definition tls_cbc.cpp:317
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_cbc.cpp:156
size_t minimum_final_size() const override
Definition tls_cbc.h:139
size_t output_length(size_t input_length) const override
Definition tls_cbc.cpp:202
TLS_CBC_HMAC_AEAD_Encryption(std::unique_ptr< BlockCipher > cipher, std::unique_ptr< MessageAuthenticationCode > mac, size_t cipher_keylen, size_t mac_keylen, const Protocol_Version &version, bool use_encrypt_then_mac)
Definition tls_cbc.cpp:142
size_t update_granularity() const final
Definition tls_cbc.cpp:75
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_cbc.cpp:134
secure_vector< uint8_t > & cbc_state()
Definition tls_cbc.h:89
Key_Length_Specification key_spec() const final
Definition tls_cbc.cpp:90
size_t default_nonce_length() const final
Definition tls_cbc.h:45
size_t ideal_granularity() const final
Definition tls_cbc.cpp:79
bool requires_entire_message() const final
Definition tls_cbc.h:48
std::vector< uint8_t > & assoc_data()
Definition tls_cbc.h:91
secure_vector< uint8_t > & msg()
Definition tls_cbc.h:93
TLS_CBC_HMAC_AEAD_Mode(const TLS_CBC_HMAC_AEAD_Mode &other)=delete
MessageAuthenticationCode & mac() const
Definition tls_cbc.h:87
Cipher_Mode & cbc() const
Definition tls_cbc.h:85
size_t tag_size() const final
Definition tls_cbc.h:43
bool valid_nonce_length(size_t nl) const final
Definition tls_cbc.cpp:83
const char * name
Definition ffi_tls.h:59
uint16_t check_tls_cbc_padding(const uint8_t record[], size_t record_len)
Definition tls_cbc.cpp:264
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128