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