Botan 3.12.0
Crypto and TLS for C&
tls_null.cpp
Go to the documentation of this file.
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
17namespace Botan::TLS {
18
19/*
20* TLS_NULL_HMAC_AEAD_Mode Constructor
21*/
22TLS_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
26 m_key.clear();
27 m_ad.clear();
28 mac().clear();
29}
30
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
40std::string TLS_NULL_HMAC_AEAD_Mode::name() const {
41 return fmt("TLS_NULL({})", m_mac_name);
42}
43
45 return 1;
46}
47
49 return 1;
50}
51
53 return nl == 0;
54}
55
59
63
65 return m_mac_keylen;
66}
67
72
73void 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
81void 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
99size_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
111void 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
120void TLS_NULL_HMAC_AEAD_Encryption::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
122}
123
124size_t TLS_NULL_HMAC_AEAD_Encryption::output_length(size_t input_length) const {
125 return input_length + tag_size();
126}
127
128void 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
134size_t TLS_NULL_HMAC_AEAD_Decryption::output_length(size_t input_length) const {
135 return input_length - tag_size();
136}
137
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
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:34
void final(uint8_t out[])
Definition buf_comp.h:69
size_t process(std::span< uint8_t > msg)
virtual size_t output_length(size_t input_length) const =0
virtual bool has_keying_material() const =0
virtual void clear()=0
void set_key(const OctetString &key)
Definition sym_algo.cpp:14
void finish_msg(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition tls_null.cpp:138
size_t output_length(size_t input_length) const override
Definition tls_null.cpp:134
size_t output_length(size_t input_length) const override
Definition tls_null.cpp:124
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_null.cpp:120
size_t update_granularity() const final
Definition tls_null.cpp:44
size_t ideal_granularity() const final
Definition tls_null.cpp:48
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_null.cpp:111
bool has_keying_material() const final
Definition tls_null.cpp:60
MessageAuthenticationCode & mac() const
Definition tls_null.cpp:68
Key_Length_Specification key_spec() const final
Definition tls_null.cpp:56
std::string name() const final
Definition tls_null.cpp:40
bool valid_nonce_length(size_t nl) const final
Definition tls_null.cpp:52
TLS_NULL_HMAC_AEAD_Mode(std::unique_ptr< MessageAuthenticationCode > mac, size_t mac_keylen)
Definition tls_null.cpp:22
size_t tag_size() const final
Definition tls_null.h:35
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68