• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 24011819536

05 Apr 2026 10:23PM UTC coverage: 89.447% (-0.007%) from 89.454%
24011819536

Pull #5521

github

web-flow
Merge 8a2a3cca2 into 417709dd7
Pull Request #5521: Rollup of small fixes

105807 of 118290 relevant lines covered (89.45%)

11758282.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.79
/src/lib/modes/aead/ccm/ccm.cpp
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

16
namespace Botan {
17

18
// 128-bit cipher is intrinsic to CCM definition
19
static const size_t CCM_BS = 16;
20

21
/*
22
* CCM_Mode Constructor
23
*/
24
CCM_Mode::CCM_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size, size_t L) :
244✔
25
      m_tag_size(tag_size), m_L(L), m_cipher(std::move(cipher)) {
244✔
26
   if(m_cipher->block_size() != CCM_BS) {
244✔
27
      throw Invalid_Argument(m_cipher->name() + " cannot be used with CCM mode");
×
28
   }
29

30
   if(L < 2 || L > 8) {
244✔
31
      throw Invalid_Argument(fmt("Invalid CCM L value {}", L));
×
32
   }
33

34
   if(tag_size < 4 || tag_size > 16 || tag_size % 2 != 0) {
244✔
35
      throw Invalid_Argument(fmt("Invalid CCM tag length {}", tag_size));
×
36
   }
37
}
244✔
38

39
void CCM_Mode::clear() {
70✔
40
   m_cipher->clear();
70✔
41
   reset();
140✔
42
}
70✔
43

44
void CCM_Mode::reset() {
817✔
45
   m_nonce.clear();
382✔
46
   m_msg_buf.clear();
817✔
47
   m_ad_buf.clear();
817✔
48
}
312✔
49

50
std::string CCM_Mode::name() const {
35✔
51
   return fmt("{}/CCM({},{})", m_cipher->name(), tag_size(), L());
35✔
52
}
53

54
bool CCM_Mode::valid_nonce_length(size_t length) const {
645✔
55
   return (length == (15 - L()));
645✔
56
}
57

58
size_t CCM_Mode::default_nonce_length() const {
36✔
59
   return (15 - L());
36✔
60
}
61

62
size_t CCM_Mode::update_granularity() const {
456✔
63
   return 1;
456✔
64
}
65

66
size_t CCM_Mode::ideal_granularity() const {
216✔
67
   // Completely arbitrary
68
   return m_cipher->parallel_bytes();
216✔
69
}
70

71
bool CCM_Mode::requires_entire_message() const {
84✔
72
   return true;
84✔
73
}
74

75
Key_Length_Specification CCM_Mode::key_spec() const {
176✔
76
   return m_cipher->key_spec();
176✔
77
}
78

79
bool CCM_Mode::has_keying_material() const {
245✔
80
   return m_cipher->has_keying_material();
245✔
81
}
82

83
void CCM_Mode::key_schedule(std::span<const uint8_t> key) {
174✔
84
   m_cipher->set_key(key);
174✔
85
}
174✔
86

87
void CCM_Mode::set_associated_data_n(size_t idx, std::span<const uint8_t> ad) {
674✔
88
   BOTAN_ARG_CHECK(idx == 0, "CCM: cannot handle non-zero index in set_associated_data_n");
674✔
89

90
   m_ad_buf.clear();
674✔
91

92
   if(!ad.empty()) {
674✔
93
      // FIXME: support larger AD using length encoding rules
94
      BOTAN_ARG_CHECK(ad.size() < (0xFFFF - 0xFF), "Supported CCM AD length");
650✔
95

96
      m_ad_buf.push_back(get_byte<0>(static_cast<uint16_t>(ad.size())));
650✔
97
      m_ad_buf.push_back(get_byte<1>(static_cast<uint16_t>(ad.size())));
650✔
98
      m_ad_buf.insert(m_ad_buf.end(), ad.begin(), ad.end());
650✔
99
      while(m_ad_buf.size() % CCM_BS != 0) {
6,127✔
100
         m_ad_buf.push_back(0);  // pad with zeros to full block size
5,477✔
101
      }
102
   }
103
}
674✔
104

105
void CCM_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
610✔
106
   if(!valid_nonce_length(nonce_len)) {
610✔
107
      throw Invalid_IV_Length(name(), nonce_len);
×
108
   }
109

110
   m_nonce.assign(nonce, nonce + nonce_len);
610✔
111
   m_msg_buf.clear();
610✔
112
}
610✔
113

114
size_t CCM_Mode::process_msg(uint8_t buf[], size_t sz) {
1,894✔
115
   BOTAN_STATE_CHECK(!m_nonce.empty());
1,894✔
116
   m_msg_buf.insert(m_msg_buf.end(), buf, buf + sz);
1,754✔
117

118
   // CCM message length is limited to 2^(8*L) - 1 bytes
119
   if(L() < 8) {
1,754✔
120
      const uint64_t max_msg_len = (static_cast<uint64_t>(1) << (8 * L())) - 1;
1,692✔
121
      if(m_msg_buf.size() > max_msg_len) {
1,692✔
122
         throw Invalid_State("CCM message length exceeds the limit for L");
×
123
      }
124
   }
125

126
   return 0;  // no output until finished
1,754✔
127
}
128

129
void CCM_Mode::encode_length(uint64_t len, uint8_t out[]) {
540✔
130
   const size_t len_bytes = L();
540✔
131

132
   BOTAN_ASSERT_NOMSG(len_bytes >= 2 && len_bytes <= 8);
540✔
133

134
   for(size_t i = 0; i != len_bytes; ++i) {
2,163✔
135
      out[len_bytes - 1 - i] = get_byte_var(sizeof(uint64_t) - 1 - i, len);
1,623✔
136
   }
137

138
   if(len_bytes < 8 && (len >> (len_bytes * 8)) > 0) {
540✔
139
      throw Encoding_Error("CCM message length too long to encode in L field");
×
140
   }
141
}
540✔
142

143
void CCM_Mode::inc(secure_vector<uint8_t>& C) {
6,299✔
144
   for(size_t i = 0; i != C.size(); ++i) {
6,299✔
145
      uint8_t& b = C[C.size() - i - 1];
6,299✔
146
      b += 1;
6,299✔
147
      if(b > 0) {
6,299✔
148
         break;
149
      }
150
   }
151
}
×
152

153
secure_vector<uint8_t> CCM_Mode::format_b0(size_t sz) {
750✔
154
   if(m_nonce.size() != 15 - L()) {
750✔
155
      throw Invalid_State("CCM mode must set nonce");
210✔
156
   }
157
   secure_vector<uint8_t> B0(CCM_BS);
540✔
158

159
   const uint8_t b_flags =
540✔
160
      static_cast<uint8_t>((!m_ad_buf.empty() ? 64 : 0) + (((tag_size() / 2) - 1) << 3) + (L() - 1));
562✔
161

162
   B0[0] = b_flags;
540✔
163
   copy_mem(&B0[1], m_nonce.data(), m_nonce.size());
540✔
164
   encode_length(sz, &B0[m_nonce.size() + 1]);
540✔
165

166
   return B0;
540✔
167
}
×
168

169
secure_vector<uint8_t> CCM_Mode::format_c0() {
540✔
170
   if(m_nonce.size() != 15 - L()) {
540✔
171
      throw Invalid_State("CCM mode must set nonce");
×
172
   }
173
   secure_vector<uint8_t> C(CCM_BS);
540✔
174

175
   const uint8_t a_flags = static_cast<uint8_t>(L() - 1);
540✔
176

177
   C[0] = a_flags;
540✔
178
   copy_mem(&C[1], m_nonce.data(), m_nonce.size());
540✔
179

180
   return C;
540✔
181
}
×
182

183
void CCM_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
322✔
184
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
322✔
185

186
   buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
322✔
187

188
   const size_t sz = buffer.size() - offset;
322✔
189
   uint8_t* buf = buffer.data() + offset;
322✔
190

191
   const secure_vector<uint8_t>& ad = ad_buf();
322✔
192
   BOTAN_ARG_CHECK(ad.size() % CCM_BS == 0, "AD is block size multiple");
322✔
193

194
   const BlockCipher& E = cipher();
322✔
195

196
   secure_vector<uint8_t> T(CCM_BS);
322✔
197
   E.encrypt(format_b0(sz), T);
539✔
198

199
   for(size_t i = 0; i != ad.size(); i += CCM_BS) {
565✔
200
      xor_buf(T.data(), &ad[i], CCM_BS);
348✔
201
      E.encrypt(T);
696✔
202
   }
203

204
   secure_vector<uint8_t> C = format_c0();
217✔
205
   secure_vector<uint8_t> S0(CCM_BS);
217✔
206
   E.encrypt(C, S0);
217✔
207
   inc(C);
217✔
208

209
   secure_vector<uint8_t> X(CCM_BS);
217✔
210

211
   const uint8_t* buf_end = &buf[sz];
217✔
212

213
   while(buf != buf_end) {
2,993✔
214
      const size_t to_proc = std::min<size_t>(CCM_BS, buf_end - buf);
2,776✔
215

216
      xor_buf(T.data(), buf, to_proc);
2,776✔
217
      E.encrypt(T);
2,776✔
218

219
      E.encrypt(C, X);
2,776✔
220
      xor_buf(buf, X.data(), to_proc);
2,776✔
221
      inc(C);
2,776✔
222

223
      buf += to_proc;
2,776✔
224
   }
225

226
   T ^= S0;
217✔
227

228
   buffer += std::make_pair(T.data(), tag_size());
217✔
229

230
   reset();
434✔
231
}
868✔
232

233
void CCM_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
428✔
234
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
428✔
235

236
   buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
428✔
237

238
   const size_t sz = buffer.size() - offset;
428✔
239
   uint8_t* buf = buffer.data() + offset;
428✔
240

241
   BOTAN_ARG_CHECK(sz >= tag_size(), "input did not include the tag");
428✔
242

243
   const secure_vector<uint8_t>& ad = ad_buf();
428✔
244
   BOTAN_ARG_CHECK(ad.size() % CCM_BS == 0, "AD is block size multiple");
428✔
245

246
   const BlockCipher& E = cipher();
428✔
247

248
   secure_vector<uint8_t> T(CCM_BS);
428✔
249
   E.encrypt(format_b0(sz - tag_size()), T);
856✔
250

251
   for(size_t i = 0; i != ad.size(); i += CCM_BS) {
885✔
252
      xor_buf(T.data(), &ad[i], CCM_BS);
562✔
253
      E.encrypt(T);
1,124✔
254
   }
255

256
   secure_vector<uint8_t> C = format_c0();
323✔
257

258
   secure_vector<uint8_t> S0(CCM_BS);
428✔
259
   E.encrypt(C, S0);
323✔
260
   inc(C);
323✔
261

262
   secure_vector<uint8_t> X(CCM_BS);
428✔
263

264
   const uint8_t* buf_end = &buf[sz - tag_size()];
323✔
265

266
   while(buf != buf_end) {
3,306✔
267
      const size_t to_proc = std::min<size_t>(CCM_BS, buf_end - buf);
2,983✔
268

269
      E.encrypt(C, X);
2,983✔
270
      xor_buf(buf, X.data(), to_proc);
2,983✔
271
      inc(C);
2,983✔
272

273
      xor_buf(T.data(), buf, to_proc);
2,983✔
274
      E.encrypt(T);
2,983✔
275

276
      buf += to_proc;
2,983✔
277
   }
278

279
   T ^= S0;
323✔
280

281
   if(!CT::is_equal(T.data(), buf_end, tag_size()).as_bool()) {
323✔
282
      throw Invalid_Authentication_Tag("CCM tag check failed");
105✔
283
   }
284

285
   buffer.resize(buffer.size() - tag_size());
218✔
286

287
   reset();
436✔
288
}
1,187✔
289

290
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc