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