Botan 3.12.0
Crypto and TLS for C&
ccm.cpp
Go to the documentation of this file.
1/*
2* CCM Mode Encryption
3* (C) 2013,2018 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/ccm.h>
10
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/loadstor.h>
16
17namespace Botan {
18
19// 128-bit cipher is intrinsic to CCM definition
20static const size_t CCM_BS = 16;
21
22/*
23* CCM_Mode Constructor
24*/
25CCM_Mode::CCM_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size, size_t L) :
26 m_tag_size(tag_size), m_L(L), m_cipher(std::move(cipher)) {
27 if(m_cipher->block_size() != CCM_BS) {
28 throw Invalid_Argument(m_cipher->name() + " cannot be used with CCM mode");
29 }
30
31 if(L < 2 || L > 8) {
32 throw Invalid_Argument(fmt("Invalid CCM L value {}", L));
33 }
34
35 if(tag_size < 4 || tag_size > 16 || tag_size % 2 != 0) {
36 throw Invalid_Argument(fmt("Invalid CCM tag length {}", tag_size));
37 }
38}
39
41 m_cipher->clear();
42 reset();
43}
44
46 m_nonce.clear();
47 m_msg_buf.clear();
48 m_ad_buf.clear();
49}
50
51std::string CCM_Mode::name() const {
52 return fmt("{}/CCM({},{})", m_cipher->name(), tag_size(), L());
53}
54
55bool CCM_Mode::valid_nonce_length(size_t length) const {
56 return (length == (15 - L()));
57}
58
60 return (15 - L());
61}
62
64 return 1;
65}
66
68 // Completely arbitrary
69 return m_cipher->parallel_bytes();
70}
71
73 return true;
74}
75
77 return m_cipher->key_spec();
78}
79
81 return m_cipher->has_keying_material();
82}
83
84void CCM_Mode::key_schedule(std::span<const uint8_t> key) {
85 m_cipher->set_key(key);
86}
87
88void CCM_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
89 BOTAN_ARG_CHECK(idx == 0, "CCM: cannot handle non-zero index in set_associated_data_n");
90
91 m_ad_buf.clear();
92
93 if(!ad.empty()) {
94 // FIXME: support larger AD using length encoding rules
95 BOTAN_ARG_CHECK(ad.size() < (0xFFFF - 0xFF), "Supported CCM AD length");
96
97 m_ad_buf.push_back(get_byte<0>(static_cast<uint16_t>(ad.size())));
98 m_ad_buf.push_back(get_byte<1>(static_cast<uint16_t>(ad.size())));
99 m_ad_buf.insert(m_ad_buf.end(), ad.begin(), ad.end());
100 while(m_ad_buf.size() % CCM_BS != 0) {
101 m_ad_buf.push_back(0); // pad with zeros to full block size
102 }
103 }
104}
105
106void CCM_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
107 if(!valid_nonce_length(nonce_len)) {
108 throw Invalid_IV_Length(name(), nonce_len);
109 }
110
111 m_nonce.assign(nonce, nonce + nonce_len);
112 m_msg_buf.clear();
113}
114
115size_t CCM_Mode::process_msg(uint8_t buf[], size_t sz) {
116 BOTAN_STATE_CHECK(!m_nonce.empty());
117 m_msg_buf.insert(m_msg_buf.end(), buf, buf + sz);
118
119 // CCM message length is limited to 2^(8*L) - 1 bytes
120 if(L() < 8) {
121 const uint64_t max_msg_len = (static_cast<uint64_t>(1) << (8 * L())) - 1;
122 if(m_msg_buf.size() > max_msg_len) {
123 throw Invalid_State("CCM message length exceeds the limit for L");
124 }
125 }
126
127 return 0; // no output until finished
128}
129
130void CCM_Mode::encode_length(uint64_t len, uint8_t out[]) {
131 const size_t len_bytes = L();
132
133 BOTAN_ASSERT_NOMSG(len_bytes >= 2 && len_bytes <= 8);
134
135 for(size_t i = 0; i != len_bytes; ++i) {
136 out[len_bytes - 1 - i] = get_byte_var(sizeof(uint64_t) - 1 - i, len);
137 }
138
139 if(len_bytes < 8 && (len >> (len_bytes * 8)) > 0) {
140 throw Encoding_Error("CCM message length too long to encode in L field");
141 }
142}
143
145 for(size_t i = 0; i != C.size(); ++i) {
146 uint8_t& b = C[C.size() - i - 1];
147 b += 1;
148 if(b > 0) {
149 break;
150 }
151 }
152}
153
155 if(m_nonce.size() != 15 - L()) {
156 throw Invalid_State("CCM mode must set nonce");
157 }
158 secure_vector<uint8_t> B0(CCM_BS);
159
160 const uint8_t b_flags =
161 static_cast<uint8_t>((!m_ad_buf.empty() ? 64 : 0) + (((tag_size() / 2) - 1) << 3) + (L() - 1));
162
163 B0[0] = b_flags;
164 copy_mem(&B0[1], m_nonce.data(), m_nonce.size());
165 encode_length(sz, &B0[m_nonce.size() + 1]);
166
167 return B0;
168}
169
171 if(m_nonce.size() != 15 - L()) {
172 throw Invalid_State("CCM mode must set nonce");
173 }
174 secure_vector<uint8_t> C(CCM_BS);
175
176 const uint8_t a_flags = static_cast<uint8_t>(L() - 1);
177
178 C[0] = a_flags;
179 copy_mem(&C[1], m_nonce.data(), m_nonce.size());
180
181 return C;
182}
183
184void CCM_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
185 BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
186
187 buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
188
189 const size_t sz = buffer.size() - offset;
190 uint8_t* buf = buffer.data() + offset;
191
192 const secure_vector<uint8_t>& ad = ad_buf();
193 BOTAN_ARG_CHECK(ad.size() % CCM_BS == 0, "AD is block size multiple");
194
195 const BlockCipher& E = cipher();
196
197 secure_vector<uint8_t> T(CCM_BS);
198 E.encrypt(format_b0(sz), T);
199
200 for(size_t i = 0; i != ad.size(); i += CCM_BS) {
201 xor_buf(T.data(), &ad[i], CCM_BS);
202 E.encrypt(T);
203 }
204
206 secure_vector<uint8_t> S0(CCM_BS);
207 E.encrypt(C, S0);
208 inc(C);
209
210 secure_vector<uint8_t> X(CCM_BS);
211
212 const uint8_t* buf_end = &buf[sz];
213
214 while(buf != buf_end) {
215 const size_t to_proc = std::min<size_t>(CCM_BS, buf_end - buf);
216
217 xor_buf(T.data(), buf, to_proc);
218 E.encrypt(T);
219
220 E.encrypt(C, X);
221 xor_buf(buf, X.data(), to_proc);
222 inc(C);
223
224 buf += to_proc;
225 }
226
227 T ^= S0;
228
229 buffer += std::make_pair(T.data(), tag_size());
230
231 reset();
232}
233
234void CCM_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
235 BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
236
237 buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
238
239 const size_t sz = buffer.size() - offset;
240 uint8_t* buf = buffer.data() + offset;
241
242 BOTAN_ARG_CHECK(sz >= tag_size(), "input did not include the tag");
243
244 const secure_vector<uint8_t>& ad = ad_buf();
245 BOTAN_ARG_CHECK(ad.size() % CCM_BS == 0, "AD is block size multiple");
246
247 const BlockCipher& E = cipher();
248
249 secure_vector<uint8_t> T(CCM_BS);
250 E.encrypt(format_b0(sz - tag_size()), T);
251
252 for(size_t i = 0; i != ad.size(); i += CCM_BS) {
253 xor_buf(T.data(), &ad[i], CCM_BS);
254 E.encrypt(T);
255 }
256
258
259 secure_vector<uint8_t> S0(CCM_BS);
260 E.encrypt(C, S0);
261 inc(C);
262
263 secure_vector<uint8_t> X(CCM_BS);
264
265 const uint8_t* buf_end = &buf[sz - tag_size()];
266
267 while(buf != buf_end) {
268 const size_t to_proc = std::min<size_t>(CCM_BS, buf_end - buf);
269
270 E.encrypt(C, X);
271 xor_buf(buf, X.data(), to_proc);
272 inc(C);
273
274 xor_buf(T.data(), buf, to_proc);
275 E.encrypt(T);
276
277 buf += to_proc;
278 }
279
280 T ^= S0;
281
282 if(!CT::is_equal(T.data(), buf_end, tag_size()).as_bool()) {
283 clear_mem(std::span{buffer}.subspan(offset, sz - tag_size()));
284 throw Invalid_Authentication_Tag("CCM tag check failed");
285 }
286
287 buffer.resize(buffer.size() - tag_size());
288
289 reset();
290}
291
292} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
void encrypt(const uint8_t in[], uint8_t out[]) const
size_t ideal_granularity() const final
Definition ccm.cpp:67
bool requires_entire_message() const final
Definition ccm.cpp:72
void reset() final
Definition ccm.cpp:45
static void inc(secure_vector< uint8_t > &C)
Definition ccm.cpp:144
size_t update_granularity() const final
Definition ccm.cpp:63
void clear() final
Definition ccm.cpp:40
secure_vector< uint8_t > & msg_buf()
Definition ccm.h:64
const BlockCipher & cipher() const
Definition ccm.h:56
void encode_length(uint64_t len, uint8_t out[])
Definition ccm.cpp:130
size_t L() const
Definition ccm.h:54
size_t tag_size() const final
Definition ccm.h:47
Key_Length_Specification key_spec() const final
Definition ccm.cpp:76
bool valid_nonce_length(size_t length) const final
Definition ccm.cpp:55
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) final
Definition ccm.cpp:88
secure_vector< uint8_t > format_c0()
Definition ccm.cpp:170
bool has_keying_material() const final
Definition ccm.cpp:80
std::string name() const final
Definition ccm.cpp:51
const secure_vector< uint8_t > & ad_buf() const
Definition ccm.h:62
CCM_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size, size_t L)
Definition ccm.cpp:25
secure_vector< uint8_t > format_b0(size_t msg_size)
Definition ccm.cpp:154
size_t default_nonce_length() const final
Definition ccm.cpp:59
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:798
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
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
constexpr uint8_t get_byte_var(size_t byte_num, T input)
Definition loadstor.h:69
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118