Botan 3.9.0
Crypto and TLS for C&
eax.cpp
Go to the documentation of this file.
1/*
2* EAX Mode Encryption
3* (C) 1999-2007 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#include <botan/internal/eax.h>
10
11#include <botan/mem_ops.h>
12#include <botan/internal/cmac.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/ctr.h>
15#include <botan/internal/fmt.h>
16
17namespace Botan {
18
19namespace {
20
21/*
22* EAX MAC-based PRF
23*/
25 uint8_t tag, size_t block_size, MessageAuthenticationCode& mac, const uint8_t in[], size_t length) {
26 for(size_t i = 0; i != block_size - 1; ++i) {
27 mac.update(0);
28 }
29 mac.update(tag);
30 mac.update(in, length);
31 return mac.final();
32}
33
34} // namespace
35
36/*
37* EAX_Mode Constructor
38*/
39EAX_Mode::EAX_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size) :
41 m_cipher(std::move(cipher)),
42 m_ctr(std::make_unique<CTR_BE>(m_cipher->new_object())),
43 m_cmac(std::make_unique<CMAC>(m_cipher->new_object())) {
44 if(m_tag_size < 8 || m_tag_size > m_cmac->output_length()) {
45 throw Invalid_Argument(fmt("Tag size {} is not allowed for {}", tag_size, name()));
46 }
47}
48
50 m_cipher->clear();
51 m_ctr->clear();
52 m_cmac->clear();
53 reset();
54}
55
57 m_ad_mac.clear();
58 m_nonce_mac.clear();
59
60 // Clear out any data added to the CMAC calculation
61 try {
62 m_cmac->final();
63 } catch(Key_Not_Set&) {}
64}
65
66std::string EAX_Mode::name() const {
67 return (m_cipher->name() + "/EAX");
68}
69
71 return 1;
72}
73
75 return m_cipher->parallel_bytes();
76}
77
79 return m_ctr->key_spec();
80}
81
83 return m_ctr->has_keying_material() && m_cmac->has_keying_material();
84}
85
86/*
87* Set the EAX key
88*/
89void EAX_Mode::key_schedule(std::span<const uint8_t> key) {
90 /*
91 * These could share the key schedule, which is one nice part of EAX,
92 * but it's much easier to ignore that here...
93 */
94 m_ctr->set_key(key);
95 m_cmac->set_key(key);
96}
97
98/*
99* Set the EAX associated data
100*/
101void EAX_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
102 BOTAN_ARG_CHECK(idx == 0, "EAX: cannot handle non-zero index in set_associated_data_n");
103 if(!m_nonce_mac.empty()) {
104 throw Invalid_State("Cannot set AD for EAX while processing a message");
105 }
106 m_ad_mac = eax_prf(1, block_size(), *m_cmac, ad.data(), ad.size());
107}
108
109void EAX_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
110 if(!valid_nonce_length(nonce_len)) {
111 throw Invalid_IV_Length(name(), nonce_len);
112 }
113
114 m_nonce_mac = eax_prf(0, block_size(), *m_cmac, nonce, nonce_len);
115
116 m_ctr->set_iv(m_nonce_mac.data(), m_nonce_mac.size());
117
118 for(size_t i = 0; i != block_size() - 1; ++i) {
119 m_cmac->update(0);
120 }
121 m_cmac->update(2);
122}
123
124size_t EAX_Encryption::process_msg(uint8_t buf[], size_t sz) {
126 m_ctr->cipher(buf, buf, sz);
127 m_cmac->update(buf, sz);
128 return sz;
129}
130
131void EAX_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
133 update(buffer, offset);
134
135 secure_vector<uint8_t> data_mac = m_cmac->final();
136 xor_buf(data_mac, m_nonce_mac, data_mac.size());
137
138 if(m_ad_mac.empty()) {
139 m_ad_mac = eax_prf(1, block_size(), *m_cmac, nullptr, 0);
140 }
141
142 xor_buf(data_mac, m_ad_mac, data_mac.size());
143
144 buffer += std::make_pair(data_mac.data(), tag_size());
145
146 m_nonce_mac.clear();
147}
148
149size_t EAX_Decryption::process_msg(uint8_t buf[], size_t sz) {
151 m_cmac->update(buf, sz);
152 m_ctr->cipher(buf, buf, sz);
153 return sz;
154}
155
156void EAX_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
158 BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
159 const size_t sz = buffer.size() - offset;
160 uint8_t* buf = buffer.data() + offset;
161
162 BOTAN_ARG_CHECK(sz >= tag_size(), "input did not include the tag");
163
164 const size_t remaining = sz - tag_size();
165
166 if(remaining > 0) {
167 m_cmac->update(buf, remaining);
168 m_ctr->cipher(buf, buf, remaining);
169 }
170
171 const uint8_t* included_tag = &buf[remaining];
172
173 secure_vector<uint8_t> mac = m_cmac->final();
174 mac ^= m_nonce_mac;
175
176 if(m_ad_mac.empty()) {
177 m_ad_mac = eax_prf(1, block_size(), *m_cmac, nullptr, 0);
178 }
179
180 mac ^= m_ad_mac;
181
182 const bool accept_mac = CT::is_equal(mac.data(), included_tag, tag_size()).as_bool();
183
184 buffer.resize(offset + remaining);
185
186 m_nonce_mac.clear();
187
188 if(!accept_mac) {
189 throw Invalid_Authentication_Tag("EAX tag check failed");
190 }
191}
192
193} // namespace Botan
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
void update(T &buffer, size_t offset=0)
size_t tag_size() const final
Definition eax.h:39
size_t update_granularity() const final
Definition eax.cpp:70
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) final
Definition eax.cpp:101
size_t block_size() const
Definition eax.h:54
size_t ideal_granularity() const final
Definition eax.cpp:74
void clear() final
Definition eax.cpp:49
bool valid_nonce_length(size_t) const final
Definition eax.h:37
std::unique_ptr< BlockCipher > m_cipher
Definition eax.h:58
bool has_keying_material() const final
Definition eax.cpp:82
EAX_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size)
Definition eax.cpp:39
std::unique_ptr< StreamCipher > m_ctr
Definition eax.h:59
std::unique_ptr< MessageAuthenticationCode > m_cmac
Definition eax.h:60
Key_Length_Specification key_spec() const final
Definition eax.cpp:78
secure_vector< uint8_t > m_nonce_mac
Definition eax.h:64
size_t m_tag_size
Definition eax.h:56
void reset() final
Definition eax.cpp:56
std::string name() const final
Definition eax.cpp:66
secure_vector< uint8_t > m_ad_mac
Definition eax.h:62
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:826
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:342
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69