/src/Botan-3.13.0/src/lib/modes/aead/eax/eax.cpp
Line | Count | Source |
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 | | #include <botan/internal/int_utils.h> |
18 | | |
19 | | namespace Botan { |
20 | | |
21 | | namespace { |
22 | | |
23 | | /* |
24 | | * EAX MAC-based PRF |
25 | | */ |
26 | | secure_vector<uint8_t> eax_prf( |
27 | 481k | uint8_t tag, size_t block_size, MessageAuthenticationCode& mac, const uint8_t in[], size_t length) { |
28 | 7.67M | for(size_t i = 0; i != block_size - 1; ++i) { |
29 | 7.19M | mac.update(0); |
30 | 7.19M | } |
31 | 481k | mac.update(tag); |
32 | 481k | mac.update(in, length); |
33 | 481k | return mac.final(); |
34 | 481k | } |
35 | | |
36 | | } // namespace |
37 | | |
38 | | /* |
39 | | * EAX_Mode Constructor |
40 | | */ |
41 | | EAX_Mode::EAX_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size) : |
42 | 5.43k | m_tag_size(tag_size), |
43 | 5.43k | m_cipher(std::move(cipher)), |
44 | 5.43k | m_ctr(std::make_unique<CTR_BE>(m_cipher->new_object())), |
45 | 5.43k | m_cmac(std::make_unique<CMAC>(m_cipher->new_object())) { |
46 | 5.43k | if(m_tag_size < 8 || m_tag_size > m_cmac->output_length()) { |
47 | 0 | throw Invalid_Argument(fmt("Tag size {} is not allowed for {}", tag_size, name())); |
48 | 0 | } |
49 | 5.43k | } |
50 | | |
51 | 0 | void EAX_Mode::clear() { |
52 | 0 | m_cipher->clear(); |
53 | 0 | m_ctr->clear(); |
54 | 0 | m_cmac->clear(); |
55 | 0 | m_ad_mac.clear(); |
56 | 0 | reset(); |
57 | 0 | } |
58 | | |
59 | 244k | void EAX_Mode::reset() { |
60 | 244k | m_nonce_mac.clear(); |
61 | | |
62 | | // Clear out any data added to the CMAC calculation |
63 | 244k | try { |
64 | 244k | m_cmac->final(); |
65 | 244k | } catch(Key_Not_Set&) {} |
66 | 244k | } |
67 | | |
68 | 0 | std::string EAX_Mode::name() const { |
69 | 0 | return (m_cipher->name() + "/EAX"); |
70 | 0 | } |
71 | | |
72 | 5.43k | size_t EAX_Mode::update_granularity() const { |
73 | 5.43k | return 1; |
74 | 5.43k | } |
75 | | |
76 | 10.8k | size_t EAX_Mode::ideal_granularity() const { |
77 | 10.8k | return m_cipher->parallel_bytes(); |
78 | 10.8k | } |
79 | | |
80 | 5.43k | Key_Length_Specification EAX_Mode::key_spec() const { |
81 | 5.43k | return m_ctr->key_spec(); |
82 | 5.43k | } |
83 | | |
84 | 0 | bool EAX_Mode::has_keying_material() const { |
85 | 0 | return m_ctr->has_keying_material() && m_cmac->has_keying_material(); |
86 | 0 | } |
87 | | |
88 | | /* |
89 | | * Set the EAX key |
90 | | */ |
91 | 5.43k | void EAX_Mode::key_schedule(std::span<const uint8_t> key) { |
92 | | /* |
93 | | * These could share the key schedule, which is one nice part of EAX, |
94 | | * but it's much easier to ignore that here... |
95 | | */ |
96 | 5.43k | m_ctr->set_key(key); |
97 | 5.43k | m_cmac->set_key(key); |
98 | | |
99 | | // m_ad_mac was precomputed under the previous CMAC key (if any). |
100 | | // Re-keying invalidates it; AD must be re-set after set_key. |
101 | 5.43k | m_ad_mac.clear(); |
102 | | |
103 | | // Also drop any per-message state. |
104 | 5.43k | reset(); |
105 | 5.43k | } |
106 | | |
107 | | /* |
108 | | * Set the EAX associated data |
109 | | */ |
110 | 240k | void EAX_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) { |
111 | 240k | BOTAN_ARG_CHECK(idx == 0, "EAX: cannot handle non-zero index in set_associated_data_n"); |
112 | 240k | if(!m_nonce_mac.empty()) { |
113 | 0 | throw Invalid_State("Cannot set AD for EAX while processing a message"); |
114 | 0 | } |
115 | 240k | m_ad_mac = eax_prf(1, block_size(), *m_cmac, ad.data(), ad.size()); |
116 | 240k | } |
117 | | |
118 | 240k | void EAX_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) { |
119 | 240k | BOTAN_STATE_CHECK(m_nonce_mac.empty()); |
120 | | |
121 | 240k | if(!valid_nonce_length(nonce_len)) { |
122 | 0 | throw Invalid_IV_Length(name(), nonce_len); |
123 | 0 | } |
124 | | |
125 | 240k | m_nonce_mac = eax_prf(0, block_size(), *m_cmac, nonce, nonce_len); |
126 | | |
127 | 240k | m_ctr->set_iv(m_nonce_mac.data(), m_nonce_mac.size()); |
128 | | |
129 | 3.83M | for(size_t i = 0; i != block_size() - 1; ++i) { |
130 | 3.59M | m_cmac->update(0); |
131 | 3.59M | } |
132 | 240k | m_cmac->update(2); |
133 | 240k | } |
134 | | |
135 | 0 | size_t EAX_Encryption::output_length(size_t input_length) const { |
136 | 0 | return add_or_throw(input_length, tag_size(), "EAX input too large"); |
137 | 0 | } |
138 | | |
139 | 360k | size_t EAX_Encryption::process_msg(uint8_t buf[], size_t sz) { |
140 | 360k | BOTAN_STATE_CHECK(!m_nonce_mac.empty()); |
141 | 360k | m_ctr->cipher(buf, buf, sz); |
142 | 360k | m_cmac->update(buf, sz); |
143 | 360k | return sz; |
144 | 360k | } |
145 | | |
146 | 163k | void EAX_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
147 | 163k | BOTAN_STATE_CHECK(!m_nonce_mac.empty()); |
148 | 163k | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
149 | 163k | update(buffer, offset); |
150 | | |
151 | 163k | secure_vector<uint8_t> data_mac = m_cmac->final(); |
152 | 163k | xor_buf(data_mac, m_nonce_mac, data_mac.size()); |
153 | | |
154 | 163k | if(m_ad_mac.empty()) { |
155 | 0 | m_ad_mac = eax_prf(1, block_size(), *m_cmac, nullptr, 0); |
156 | 0 | } |
157 | | |
158 | 163k | xor_buf(data_mac, m_ad_mac, data_mac.size()); |
159 | | |
160 | 163k | buffer += std::make_pair(data_mac.data(), tag_size()); |
161 | | |
162 | 163k | m_nonce_mac.clear(); |
163 | 163k | } |
164 | | |
165 | 0 | size_t EAX_Decryption::output_length(size_t input_length) const { |
166 | 0 | BOTAN_ARG_CHECK(input_length >= tag_size(), "Message too short to be valid"); |
167 | 0 | return input_length - tag_size(); |
168 | 0 | } |
169 | | |
170 | 622k | size_t EAX_Decryption::process_msg(uint8_t buf[], size_t sz) { |
171 | 622k | BOTAN_STATE_CHECK(!m_nonce_mac.empty()); |
172 | 622k | m_cmac->update(buf, sz); |
173 | 622k | m_ctr->cipher(buf, buf, sz); |
174 | 622k | return sz; |
175 | 622k | } |
176 | | |
177 | 76.3k | void EAX_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) { |
178 | 76.3k | BOTAN_STATE_CHECK(!m_nonce_mac.empty()); |
179 | 76.3k | BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range"); |
180 | 76.3k | const size_t sz = buffer.size() - offset; |
181 | 76.3k | uint8_t* buf = buffer.data() + offset; |
182 | | |
183 | 76.3k | BOTAN_ARG_CHECK(sz >= tag_size(), "input did not include the tag"); |
184 | | |
185 | 76.3k | const size_t remaining = sz - tag_size(); |
186 | | |
187 | 76.3k | if(remaining > 0) { |
188 | 75.4k | m_cmac->update(buf, remaining); |
189 | 75.4k | m_ctr->cipher(buf, buf, remaining); |
190 | 75.4k | } |
191 | | |
192 | 76.3k | const uint8_t* included_tag = &buf[remaining]; |
193 | | |
194 | 76.3k | secure_vector<uint8_t> mac = m_cmac->final(); |
195 | 76.3k | mac ^= m_nonce_mac; |
196 | | |
197 | 76.3k | if(m_ad_mac.empty()) { |
198 | 0 | m_ad_mac = eax_prf(1, block_size(), *m_cmac, nullptr, 0); |
199 | 0 | } |
200 | | |
201 | 76.3k | mac ^= m_ad_mac; |
202 | | |
203 | 76.3k | const bool accept_mac = CT::is_equal(mac.data(), included_tag, tag_size()).as_bool(); |
204 | | |
205 | 76.3k | buffer.resize(offset + remaining); |
206 | | |
207 | 76.3k | m_nonce_mac.clear(); |
208 | | |
209 | 76.3k | if(!accept_mac) { |
210 | 1.34k | clear_mem(std::span{buffer}.subspan(offset, remaining)); |
211 | 1.34k | throw Invalid_Authentication_Tag("EAX tag check failed"); |
212 | 1.34k | } |
213 | 76.3k | } |
214 | | |
215 | | } // namespace Botan |