Botan 3.9.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 mac().clear();
28}
29
31 BOTAN_STATE_CHECK(!m_key.empty());
32 mac().set_key(m_key);
33}
34
35std::string TLS_NULL_HMAC_AEAD_Mode::name() const {
36 return fmt("TLS_NULL({})", m_mac_name);
37}
38
40 return 1;
41}
42
44 return 1;
45}
46
48 return nl == 0;
49}
50
54
58
60 return m_mac_keylen;
61}
62
67
68void TLS_NULL_HMAC_AEAD_Mode::key_schedule(std::span<const uint8_t> key) {
69 if(key.size() != m_mac_keylen) {
70 throw Invalid_Key_Length(name(), key.size());
71 }
72 m_key.assign(key.begin(), key.end());
73 reset();
74}
75
76void TLS_NULL_HMAC_AEAD_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
77 BOTAN_UNUSED(nonce);
78
79 if(!valid_nonce_length(nonce_len)) {
80 throw Invalid_IV_Length(name(), nonce_len);
81 }
82}
83
84size_t TLS_NULL_HMAC_AEAD_Mode::process_msg(uint8_t buf[], size_t sz) {
85 mac().update(buf, sz);
86 return sz;
87}
88
89void TLS_NULL_HMAC_AEAD_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
90 BOTAN_ARG_CHECK(idx == 0, "TLS 1.2 NULL/HMAC: cannot handle non-zero index in set_associated_data_n");
91 BOTAN_ARG_CHECK(ad.size() == 13, "TLS 1.2 NULL/HMAC: invalid TLS AEAD associated data length");
92
93 mac().update(ad);
94}
95
99
100size_t TLS_NULL_HMAC_AEAD_Encryption::output_length(size_t input_length) const {
101 return input_length + tag_size();
102}
103
104void TLS_NULL_HMAC_AEAD_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
105 process(std::span{buffer}.subspan(offset));
106 buffer.resize(buffer.size() + tag_size());
107 mac().final(std::span{buffer}.last(tag_size()));
108}
109
110size_t TLS_NULL_HMAC_AEAD_Decryption::output_length(size_t input_length) const {
111 return input_length - tag_size();
112}
113
115 BOTAN_ARG_CHECK(buffer.size() >= tag_size() + offset,
116 "TLS_NULL_HMAC_AEAD_Decryption needs at least tag_size() bytes in final buffer");
117
118 const auto data_and_tag = std::span{buffer}.subspan(offset);
119 const auto data = data_and_tag.first(data_and_tag.size() - tag_size());
120 const auto tag = data_and_tag.subspan(data.size());
121
122 process(data);
123 if(!mac().verify_mac(tag)) {
124 throw TLS_Exception(Alert::BadRecordMac, "Message authentication failure");
125 }
126
127 buffer.resize(buffer.size() - tag_size());
128}
129
130} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#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:114
size_t output_length(size_t input_length) const override
Definition tls_null.cpp:110
size_t output_length(size_t input_length) const override
Definition tls_null.cpp:100
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_null.cpp:96
size_t update_granularity() const final
Definition tls_null.cpp:39
size_t ideal_granularity() const final
Definition tls_null.cpp:43
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) override
Definition tls_null.cpp:89
bool has_keying_material() const final
Definition tls_null.cpp:55
MessageAuthenticationCode & mac() const
Definition tls_null.cpp:63
Key_Length_Specification key_spec() const final
Definition tls_null.cpp:51
std::string name() const final
Definition tls_null.cpp:35
bool valid_nonce_length(size_t nl) const final
Definition tls_null.cpp:47
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:69