Debian Codegraph @

src/lib/tls/tls12/tls_null/tls_null.cpp

raw
1 /*
2 * TLS Null Cipher Handling
3 * (C) 2024 Sebastian Ahrens, Dirk Dobkowitz, André Schomburg (Volkswagen AG)
4 * (C) 2024 Lars Dürkop (CARIAD SE)
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9 #include <botan/internal/tls_null.h>
10
11 #include <botan/assert.h>
12 #include <botan/tls_alert.h>
13 #include <botan/tls_exceptn.h>
14 #include <botan/internal/fmt.h>
15 #include <botan/internal/loadstor.h>
16
17 namespace Botan::TLS {
18
19 /*
20 * TLS_NULL_HMAC_AEAD_Mode Constructor
21 */
22 TLS_NULL_HMAC_AEAD_Mode::TLS_NULL_HMAC_AEAD_Mode(std::unique_ptr<MessageAuthenticationCode> mac, size_t mac_keylen) :
23 m_mac_name(mac->name()), m_mac_keylen(mac_keylen), m_tag_size(mac->output_length()), m_mac(std::move(mac)) {}
24
25 void TLS_NULL_HMAC_AEAD_Mode::clear() {
26 m_key.clear();
27 m_ad.clear();
28 mac().clear();
29 }
30
31 void TLS_NULL_HMAC_AEAD_Mode::reset() {
32 m_ad.clear();
33 // The base AEAD_Mode contract permits reset() before the first key has
34 // been set; only re-key the MAC if there is a key to re-key with.
35 if(!m_key.empty()) {
36 mac().set_key(m_key);
37 }
38 }
39
40 std::string TLS_NULL_HMAC_AEAD_Mode::name() const {
41 return fmt("TLS_NULL({})", m_mac_name);
42 }
43
44 size_t TLS_NULL_HMAC_AEAD_Mode::update_granularity() const {
45 return 1;
46 }
47
48 size_t TLS_NULL_HMAC_AEAD_Mode::ideal_granularity() const {
49 return 1;
50 }
51
52 bool TLS_NULL_HMAC_AEAD_Mode::valid_nonce_length(size_t nl) const {
53 return nl == 0;
54 }
55
56 Key_Length_Specification TLS_NULL_HMAC_AEAD_Mode::key_spec() const {
57 return Key_Length_Specification(m_mac_keylen);
58 }
59
60 bool TLS_NULL_HMAC_AEAD_Mode::has_keying_material() const {
61 return mac().has_keying_material();
62 }
63
64 size_t TLS_NULL_HMAC_AEAD_Mode::mac_keylen() const {
65 return m_mac_keylen;
66 }
67
68 MessageAuthenticationCode& TLS_NULL_HMAC_AEAD_Mode::mac() const {
69 BOTAN_ASSERT_NONNULL(m_mac);
70 return *m_mac;
71 }
72
73 void TLS_NULL_HMAC_AEAD_Mode::key_schedule(std::span<const uint8_t> key) {
74 if(key.size() != m_mac_keylen) {
75 throw Invalid_Key_Length(name(), key.size());
76 }
77 m_key.assign(key.begin(), key.end());
78 reset();
79 }
80
81 void TLS_NULL_HMAC_AEAD_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
82 BOTAN_UNUSED(nonce);
83
84 if(!valid_nonce_length(nonce_len)) {
85 throw Invalid_IV_Length(name(), nonce_len);
86 }
87
88 m_processed = false;
89
90 // AEAD_Mode contract: AD set via set_associated_data persists across
91 // messages until reset. finish_msg calls mac().final() which clears the
92 // internal state, so we re-feed the cached AD at the start of each
93 // message rather than once at set_associated_data time.
94 if(!m_ad.empty()) {
95 mac().update(m_ad);
96 }
97 }
98
99 size_t TLS_NULL_HMAC_AEAD_Mode::process_msg(uint8_t buf[], size_t sz) {
100 // The TLS record code path MACs each record in a single call (via
101 // finish_msg -> process). A second invocation between start_msg and
102 // finish_msg would feed additional bytes into the same HMAC instance,
103 // producing a tag covering more than the intended record body.
104 BOTAN_ASSERT_NOMSG(!m_processed);
105 m_processed = true;
106
107 mac().update(buf, sz);
108 return sz;
109 }
110
111 void TLS_NULL_HMAC_AEAD_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
112 BOTAN_ARG_CHECK(idx == 0, "TLS 1.2 NULL/HMAC: cannot handle non-zero index in set_associated_data_n");
113 BOTAN_ARG_CHECK(ad.size() == 13, "TLS 1.2 NULL/HMAC: invalid TLS AEAD associated data length");
114
115 // Cache the AD; the actual MAC update happens at start_msg so the AD
116 // persists across messages per the AEAD_Mode contract.
117 m_ad.assign(ad.begin(), ad.end());
118 }
119
120 void TLS_NULL_HMAC_AEAD_Encryption::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
121 TLS_NULL_HMAC_AEAD_Mode::set_associated_data_n(idx, ad);
122 }
123
124 size_t TLS_NULL_HMAC_AEAD_Encryption::output_length(size_t input_length) const {
125 return input_length + tag_size();
126 }
127
128 void TLS_NULL_HMAC_AEAD_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
129 process(std::span{buffer}.subspan(offset));
130 buffer.resize(buffer.size() + tag_size());
131 mac().final(std::span{buffer}.last(tag_size()));
132 }
133
134 size_t TLS_NULL_HMAC_AEAD_Decryption::output_length(size_t input_length) const {
135 return input_length - tag_size();
136 }
137
138 void TLS_NULL_HMAC_AEAD_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
139 BOTAN_ARG_CHECK(buffer.size() >= tag_size() + offset,
140 "TLS_NULL_HMAC_AEAD_Decryption needs at least tag_size() bytes in final buffer");
141
142 const auto data_and_tag = std::span{buffer}.subspan(offset);
143 const auto data = data_and_tag.first(data_and_tag.size() - tag_size());
144 const auto tag = data_and_tag.subspan(data.size());
145
146 process(data);
147 if(!mac().verify_mac(tag)) {
148 throw TLS_Exception(Alert::BadRecordMac, "Message authentication failure");
149 }
150
151 buffer.resize(buffer.size() - tag_size());
152 }
153
154 } // namespace Botan::TLS