Botan 3.14.1
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/int_utils.h>
16#include <botan/internal/loadstor.h>
17
18namespace Botan::TLS {
19
20/*
21* TLS_NULL_HMAC_AEAD_Mode Constructor
22*/
23TLS_NULL_HMAC_AEAD_Mode::TLS_NULL_HMAC_AEAD_Mode(std::unique_ptr<MessageAuthenticationCode> mac, size_t mac_keylen) :
24 m_mac_name(mac->name()), m_mac_keylen(mac_keylen), m_tag_size(mac->output_length()), m_mac(std::move(mac)) {
25 // An empty m_key means that no key was set
26 BOTAN_ARG_CHECK(m_mac_keylen > 0, "TLS 1.2 NULL/HMAC: MAC key length must not be zero");
27}
28
30 m_key.clear();
31 m_ad.clear();
32 mac().clear();
33}
34
36 m_ad.clear();
37 // The base AEAD_Mode contract permits reset() before the first key has
38 // been set; only re-key the MAC if there is a key to re-key with.
39 if(!m_key.empty()) {
40 mac().set_key(m_key);
41 }
42}
43
44std::string TLS_NULL_HMAC_AEAD_Mode::name() const {
45 return fmt("TLS_NULL({})", m_mac_name);
46}
47
49 return 1;
50}
51
53 return 1;
54}
55
57 return nl == 0;
58}
59
63
67
69 return m_mac_keylen;
70}
71
76
77void TLS_NULL_HMAC_AEAD_Mode::key_schedule(std::span<const uint8_t> key) {
78 if(key.size() != m_mac_keylen) {
79 throw Invalid_Key_Length(name(), key.size());
80 }
81 m_key.assign(key.begin(), key.end());
82 reset();
83}
84
85void TLS_NULL_HMAC_AEAD_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
86 BOTAN_UNUSED(nonce);
87
88 if(!valid_nonce_length(nonce_len)) {
89 throw Invalid_IV_Length(name(), nonce_len);
90 }
91
92 // The MAC always covers the record header
93 if(m_ad.empty()) {
94 throw Invalid_State("TLS 1.2 NULL/HMAC: associated data must be set before starting a message");
95 }
96
97 // AEAD_Mode contract: AD set via set_associated_data persists across
98 // messages until reset. finish_msg calls mac().final() which clears the
99 // internal state, so we re-feed the cached AD at the start of each
100 // message rather than once at set_associated_data time.
101 mac().update(m_ad);
102}
103
104size_t TLS_NULL_HMAC_AEAD_Mode::process_msg(uint8_t buf[], size_t sz) {
105 mac().update(buf, sz);
106 return sz;
107}
108
109void TLS_NULL_HMAC_AEAD_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
110 BOTAN_ARG_CHECK(idx == 0, "TLS 1.2 NULL/HMAC: cannot handle non-zero index in set_associated_data_n");
111 BOTAN_ARG_CHECK(ad.size() == 13, "TLS 1.2 NULL/HMAC: invalid TLS AEAD associated data length");
112
113 // Cache the AD; the actual MAC update happens at start_msg so the AD
114 // persists across messages per the AEAD_Mode contract.
115 m_ad.assign(ad.begin(), ad.end());
116}
117
118void TLS_NULL_HMAC_AEAD_Encryption::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
120}
121
122size_t TLS_NULL_HMAC_AEAD_Encryption::output_length(size_t input_length) const {
123 return add_or_throw(input_length, tag_size(), "TLS NULL input too large");
124}
125
126void TLS_NULL_HMAC_AEAD_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
127 BOTAN_ARG_CHECK(offset <= buffer.size(), "Invalid offset");
128 process(std::span{buffer}.subspan(offset));
129 const size_t output_size = add_or_throw(buffer.size(), tag_size(), "TLS NULL input too large");
130 buffer.resize(output_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 BOTAN_ARG_CHECK(input_length >= tag_size(), "Message too short to be valid");
136 return input_length - tag_size();
137}
138
140 BOTAN_ARG_CHECK(offset <= buffer.size(), "Invalid offset");
141 BOTAN_ARG_CHECK(buffer.size() - offset >= tag_size(),
142 "TLS_NULL_HMAC_AEAD_Decryption needs at least tag_size() bytes in final buffer");
143
144 const auto data_and_tag = std::span{buffer}.subspan(offset);
145 const auto data = data_and_tag.first(data_and_tag.size() - tag_size());
146 const auto tag = data_and_tag.subspan(data.size());
147
148 process(data);
149 if(!mac().verify_mac(tag)) {
150 throw TLS_Exception(Alert::BadRecordMac, "Message authentication failure");
151 }
152
153 buffer.resize(buffer.size() - tag_size());
154}
155
156} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:35
void final(uint8_t out[])
Definition buf_comp.h:97
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:139
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:122
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_null.cpp:118
size_t update_granularity() const final
Definition tls_null.cpp:48
size_t ideal_granularity() const final
Definition tls_null.cpp:52
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_null.cpp:109
bool has_keying_material() const final
Definition tls_null.cpp:64
MessageAuthenticationCode & mac() const
Definition tls_null.cpp:72
Key_Length_Specification key_spec() const final
Definition tls_null.cpp:60
std::string name() const final
Definition tls_null.cpp:44
bool valid_nonce_length(size_t nl) const final
Definition tls_null.cpp:56
TLS_NULL_HMAC_AEAD_Mode(std::unique_ptr< MessageAuthenticationCode > mac, size_t mac_keylen)
Definition tls_null.cpp:23
size_t tag_size() const final
Definition tls_null.h:34
constexpr T add_or_throw(T a, T b, std::string_view msg)
Definition int_utils.h:66
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:128