Botan  2.1.0
Crypto and TLS for C++11
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 #include <botan/block_cipher.h>
14 #include <botan/mac.h>
15 
16 namespace Botan {
17 
18 namespace TLS {
19 
20 /**
21 * TLS CBC+HMAC AEAD base class (GenericBlockCipher in TLS spec)
22 * This is the weird TLS-specific mode, not for general consumption.
23 */
24 class BOTAN_DLL TLS_CBC_HMAC_AEAD_Mode : public AEAD_Mode
25  {
26  public:
27  size_t process(uint8_t buf[], size_t sz) override final;
28 
29  std::string name() const override final;
30 
31  void set_associated_data(const uint8_t ad[], size_t ad_len) override;
32 
33  size_t update_granularity() const override final;
34 
35  Key_Length_Specification key_spec() const override final;
36 
37  bool valid_nonce_length(size_t nl) const override final;
38 
39  size_t tag_size() const override final { return m_tag_size; }
40 
41  size_t default_nonce_length() const override final { return m_iv_size; }
42 
43  void clear() override final;
44 
45  void reset() override final;
46 
47  protected:
48  TLS_CBC_HMAC_AEAD_Mode(const std::string& cipher_name,
49  size_t cipher_keylen,
50  const std::string& mac_name,
51  size_t mac_keylen,
52  bool use_explicit_iv,
53  bool use_encrypt_then_mac);
54 
55  size_t cipher_keylen() const { return m_cipher_keylen; }
56  size_t mac_keylen() const { return m_mac_keylen; }
57  size_t iv_size() const { return m_iv_size; }
58  size_t block_size() const { return m_block_size; }
59 
60  bool use_encrypt_then_mac() const { return m_use_encrypt_then_mac; }
61 
63  {
64  BOTAN_ASSERT_NONNULL(m_cipher);
65  return *m_cipher;
66  }
67 
69  {
71  return *m_mac;
72  }
73 
74  secure_vector<uint8_t>& cbc_state() { return m_cbc_state; }
75  std::vector<uint8_t>& assoc_data() { return m_ad; }
76  secure_vector<uint8_t>& msg() { return m_msg; }
77 
78  std::vector<uint8_t> assoc_data_with_len(uint16_t len);
79 
80  private:
81  void start_msg(const uint8_t nonce[], size_t nonce_len) override final;
82 
83  void key_schedule(const uint8_t key[], size_t length) override final;
84 
85  const std::string m_cipher_name;
86  const std::string m_mac_name;
87  size_t m_cipher_keylen;
88  size_t m_mac_keylen;
89  size_t m_iv_size;
90  size_t m_tag_size;
91  size_t m_block_size;
92  bool m_use_encrypt_then_mac;
93 
94  std::unique_ptr<BlockCipher> m_cipher;
95  std::unique_ptr<MessageAuthenticationCode> m_mac;
96 
97  secure_vector<uint8_t> m_cbc_state;
98  std::vector<uint8_t> m_ad;
100  };
101 
102 /**
103 * TLS_CBC_HMAC_AEAD Encryption
104 */
106  {
107  public:
108  /**
109  */
110  TLS_CBC_HMAC_AEAD_Encryption(const std::string& cipher_algo,
111  const size_t cipher_keylen,
112  const std::string& mac_algo,
113  const size_t mac_keylen,
114  bool use_explicit_iv,
115  bool use_encrypt_then_mac) :
116  TLS_CBC_HMAC_AEAD_Mode(cipher_algo,
117  cipher_keylen,
118  mac_algo,
119  mac_keylen,
120  use_explicit_iv,
121  use_encrypt_then_mac)
122  {}
123 
124  void set_associated_data(const uint8_t ad[], size_t ad_len) override;
125 
126  size_t output_length(size_t input_length) const override;
127 
128  size_t minimum_final_size() const override { return 0; }
129 
130  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
131  private:
132  void cbc_encrypt_record(uint8_t record_contents[], size_t record_len);
133  };
134 
135 /**
136 * TLS_CBC_HMAC_AEAD Decryption
137 */
139  {
140  public:
141  /**
142  */
143  TLS_CBC_HMAC_AEAD_Decryption(const std::string& cipher_algo,
144  const size_t cipher_keylen,
145  const std::string& mac_algo,
146  const size_t mac_keylen,
147  bool use_explicit_iv,
148  bool use_encrypt_then_mac) :
149  TLS_CBC_HMAC_AEAD_Mode(cipher_algo,
150  cipher_keylen,
151  mac_algo,
152  mac_keylen,
153  use_explicit_iv,
154  use_encrypt_then_mac)
155  {}
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  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
162 
163  private:
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 
171 }
172 
173 #endif
BlockCipher & cipher() const
Definition: tls_cbc.h:62
std::unique_ptr< MessageAuthenticationCode > m_mac
Definition: fpe_fe1.cpp:88
std::string m_cipher_name
TLS_CBC_HMAC_AEAD_Decryption(const std::string &cipher_algo, const size_t cipher_keylen, const std::string &mac_algo, const size_t mac_keylen, bool use_explicit_iv, bool use_encrypt_then_mac)
Definition: tls_cbc.h:143
Definition: bigint.h:619
size_t tag_size() const overridefinal
Definition: tls_cbc.h:39
MessageAuthenticationCode & mac() const
Definition: tls_cbc.h:68
secure_vector< uint8_t > & cbc_state()
Definition: tls_cbc.h:74
size_t minimum_final_size() const override
Definition: tls_cbc.h:159
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:79
TLS_CBC_HMAC_AEAD_Encryption(const std::string &cipher_algo, const size_t cipher_keylen, const std::string &mac_algo, const size_t mac_keylen, bool use_explicit_iv, bool use_encrypt_then_mac)
Definition: tls_cbc.h:110
std::vector< uint8_t > & assoc_data()
Definition: tls_cbc.h:75
Definition: alg_id.cpp:13
size_t minimum_final_size() const override
Definition: tls_cbc.h:128
bool use_encrypt_then_mac() const
Definition: tls_cbc.h:60
secure_vector< uint8_t > & msg()
Definition: tls_cbc.h:76
size_t default_nonce_length() const overridefinal
Definition: tls_cbc.h:41