Coverage Report

Created: 2025-04-11 06:34

/src/botan/src/lib/stream/chacha/chacha.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* ChaCha
3
* (C) 2014,2018,2023 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/chacha.h>
9
10
#include <botan/exceptn.h>
11
#include <botan/internal/fmt.h>
12
#include <botan/internal/loadstor.h>
13
#include <botan/internal/rotate.h>
14
15
#if defined(BOTAN_HAS_CPUID)
16
   #include <botan/internal/cpuid.h>
17
#endif
18
19
namespace Botan {
20
21
namespace {
22
23
0
inline void chacha_quarter_round(uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) {
24
0
   a += b;
25
0
   d ^= a;
26
0
   d = rotl<16>(d);
27
0
   c += d;
28
0
   b ^= c;
29
0
   b = rotl<12>(b);
30
0
   a += b;
31
0
   d ^= a;
32
0
   d = rotl<8>(d);
33
0
   c += d;
34
0
   b ^= c;
35
0
   b = rotl<7>(b);
36
0
}
37
38
/*
39
* Generate HChaCha cipher stream (for XChaCha IV setup)
40
*/
41
0
void hchacha(uint32_t output[8], const uint32_t input[16], size_t rounds) {
42
0
   BOTAN_ASSERT(rounds % 2 == 0, "Valid rounds");
43
44
0
   uint32_t x00 = input[0], x01 = input[1], x02 = input[2], x03 = input[3], x04 = input[4], x05 = input[5],
45
0
            x06 = input[6], x07 = input[7], x08 = input[8], x09 = input[9], x10 = input[10], x11 = input[11],
46
0
            x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
47
48
0
   for(size_t i = 0; i != rounds / 2; ++i) {
49
0
      chacha_quarter_round(x00, x04, x08, x12);
50
0
      chacha_quarter_round(x01, x05, x09, x13);
51
0
      chacha_quarter_round(x02, x06, x10, x14);
52
0
      chacha_quarter_round(x03, x07, x11, x15);
53
54
0
      chacha_quarter_round(x00, x05, x10, x15);
55
0
      chacha_quarter_round(x01, x06, x11, x12);
56
0
      chacha_quarter_round(x02, x07, x08, x13);
57
0
      chacha_quarter_round(x03, x04, x09, x14);
58
0
   }
59
60
0
   output[0] = x00;
61
0
   output[1] = x01;
62
0
   output[2] = x02;
63
0
   output[3] = x03;
64
0
   output[4] = x12;
65
0
   output[5] = x13;
66
0
   output[6] = x14;
67
0
   output[7] = x15;
68
0
}
69
70
}  // namespace
71
72
7
ChaCha::ChaCha(size_t rounds) : m_rounds(rounds) {
73
7
   BOTAN_ARG_CHECK(m_rounds == 8 || m_rounds == 12 || m_rounds == 20, "ChaCha only supports 8, 12 or 20 rounds");
74
7
}
75
76
14
size_t ChaCha::parallelism() {
77
14
#if defined(BOTAN_HAS_CHACHA_AVX512)
78
14
   if(CPUID::has(CPUID::Feature::AVX512)) {
79
0
      return 16;
80
0
   }
81
14
#endif
82
83
14
#if defined(BOTAN_HAS_CHACHA_AVX2)
84
14
   if(CPUID::has(CPUID::Feature::AVX2)) {
85
14
      return 8;
86
14
   }
87
0
#endif
88
89
0
   return 4;
90
14
}
91
92
0
std::string ChaCha::provider() const {
93
0
#if defined(BOTAN_HAS_CHACHA_AVX512)
94
0
   if(CPUID::has(CPUID::Feature::AVX512)) {
95
0
      return "avx512";
96
0
   }
97
0
#endif
98
99
0
#if defined(BOTAN_HAS_CHACHA_AVX2)
100
0
   if(CPUID::has(CPUID::Feature::AVX2)) {
101
0
      return "avx2";
102
0
   }
103
0
#endif
104
105
0
#if defined(BOTAN_HAS_CHACHA_SIMD32)
106
0
   if(CPUID::has_simd_4x32()) {
107
0
      return "simd32";
108
0
   }
109
0
#endif
110
111
0
   return "base";
112
0
}
113
114
12.7k
void ChaCha::chacha(uint8_t output[], size_t output_blocks, uint32_t state[16], size_t rounds) {
115
12.7k
   BOTAN_ASSERT(rounds % 2 == 0, "Valid rounds");
116
117
12.7k
#if defined(BOTAN_HAS_CHACHA_AVX512)
118
12.7k
   if(CPUID::has(CPUID::Feature::AVX512)) {
119
0
      while(output_blocks >= 16) {
120
0
         ChaCha::chacha_avx512_x16(output, state, rounds);
121
0
         output += 16 * 64;
122
0
         output_blocks -= 16;
123
0
      }
124
0
   }
125
12.7k
#endif
126
127
12.7k
#if defined(BOTAN_HAS_CHACHA_AVX2)
128
12.7k
   if(CPUID::has(CPUID::Feature::AVX2)) {
129
25.5k
      while(output_blocks >= 8) {
130
12.7k
         ChaCha::chacha_avx2_x8(output, state, rounds);
131
12.7k
         output += 8 * 64;
132
12.7k
         output_blocks -= 8;
133
12.7k
      }
134
12.7k
   }
135
12.7k
#endif
136
137
12.7k
#if defined(BOTAN_HAS_CHACHA_SIMD32)
138
12.7k
   if(CPUID::has_simd_4x32()) {
139
12.7k
      while(output_blocks >= 4) {
140
0
         ChaCha::chacha_simd32_x4(output, state, rounds);
141
0
         output += 4 * 64;
142
0
         output_blocks -= 4;
143
0
      }
144
12.7k
   }
145
12.7k
#endif
146
147
   // TODO interleave rounds
148
12.7k
   for(size_t i = 0; i != output_blocks; ++i) {
149
0
      uint32_t x00 = state[0], x01 = state[1], x02 = state[2], x03 = state[3], x04 = state[4], x05 = state[5],
150
0
               x06 = state[6], x07 = state[7], x08 = state[8], x09 = state[9], x10 = state[10], x11 = state[11],
151
0
               x12 = state[12], x13 = state[13], x14 = state[14], x15 = state[15];
152
153
0
      for(size_t r = 0; r != rounds / 2; ++r) {
154
0
         chacha_quarter_round(x00, x04, x08, x12);
155
0
         chacha_quarter_round(x01, x05, x09, x13);
156
0
         chacha_quarter_round(x02, x06, x10, x14);
157
0
         chacha_quarter_round(x03, x07, x11, x15);
158
159
0
         chacha_quarter_round(x00, x05, x10, x15);
160
0
         chacha_quarter_round(x01, x06, x11, x12);
161
0
         chacha_quarter_round(x02, x07, x08, x13);
162
0
         chacha_quarter_round(x03, x04, x09, x14);
163
0
      }
164
165
0
      x00 += state[0];
166
0
      x01 += state[1];
167
0
      x02 += state[2];
168
0
      x03 += state[3];
169
0
      x04 += state[4];
170
0
      x05 += state[5];
171
0
      x06 += state[6];
172
0
      x07 += state[7];
173
0
      x08 += state[8];
174
0
      x09 += state[9];
175
0
      x10 += state[10];
176
0
      x11 += state[11];
177
0
      x12 += state[12];
178
0
      x13 += state[13];
179
0
      x14 += state[14];
180
0
      x15 += state[15];
181
182
0
      store_le(x00, output + 64 * i + 4 * 0);
183
0
      store_le(x01, output + 64 * i + 4 * 1);
184
0
      store_le(x02, output + 64 * i + 4 * 2);
185
0
      store_le(x03, output + 64 * i + 4 * 3);
186
0
      store_le(x04, output + 64 * i + 4 * 4);
187
0
      store_le(x05, output + 64 * i + 4 * 5);
188
0
      store_le(x06, output + 64 * i + 4 * 6);
189
0
      store_le(x07, output + 64 * i + 4 * 7);
190
0
      store_le(x08, output + 64 * i + 4 * 8);
191
0
      store_le(x09, output + 64 * i + 4 * 9);
192
0
      store_le(x10, output + 64 * i + 4 * 10);
193
0
      store_le(x11, output + 64 * i + 4 * 11);
194
0
      store_le(x12, output + 64 * i + 4 * 12);
195
0
      store_le(x13, output + 64 * i + 4 * 13);
196
0
      store_le(x14, output + 64 * i + 4 * 14);
197
0
      store_le(x15, output + 64 * i + 4 * 15);
198
199
0
      state[12]++;
200
0
      state[13] += (state[12] == 0);
201
0
   }
202
12.7k
}
203
204
/*
205
* Combine cipher stream with message
206
*/
207
0
void ChaCha::cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) {
208
0
   assert_key_material_set();
209
210
0
   while(length >= m_buffer.size() - m_position) {
211
0
      const size_t available = m_buffer.size() - m_position;
212
213
0
      xor_buf(out, in, &m_buffer[m_position], available);
214
0
      chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
215
216
0
      length -= available;
217
0
      in += available;
218
0
      out += available;
219
0
      m_position = 0;
220
0
   }
221
222
0
   xor_buf(out, in, &m_buffer[m_position], length);
223
224
0
   m_position += length;
225
0
}
226
227
165k
void ChaCha::generate_keystream(uint8_t out[], size_t length) {
228
165k
   assert_key_material_set();
229
230
178k
   while(length >= m_buffer.size() - m_position) {
231
12.7k
      const size_t available = m_buffer.size() - m_position;
232
233
      // TODO: this could write directly to the output buffer
234
      // instead of bouncing it through m_buffer first
235
12.7k
      copy_mem(out, &m_buffer[m_position], available);
236
12.7k
      chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
237
238
12.7k
      length -= available;
239
12.7k
      out += available;
240
12.7k
      m_position = 0;
241
12.7k
   }
242
243
165k
   copy_mem(out, &m_buffer[m_position], length);
244
245
165k
   m_position += length;
246
165k
}
247
248
14
void ChaCha::initialize_state() {
249
14
   static const uint32_t TAU[] = {0x61707865, 0x3120646e, 0x79622d36, 0x6b206574};
250
251
14
   static const uint32_t SIGMA[] = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574};
252
253
14
   m_state[4] = m_key[0];
254
14
   m_state[5] = m_key[1];
255
14
   m_state[6] = m_key[2];
256
14
   m_state[7] = m_key[3];
257
258
14
   if(m_key.size() == 4) {
259
0
      m_state[0] = TAU[0];
260
0
      m_state[1] = TAU[1];
261
0
      m_state[2] = TAU[2];
262
0
      m_state[3] = TAU[3];
263
264
0
      m_state[8] = m_key[0];
265
0
      m_state[9] = m_key[1];
266
0
      m_state[10] = m_key[2];
267
0
      m_state[11] = m_key[3];
268
14
   } else {
269
14
      m_state[0] = SIGMA[0];
270
14
      m_state[1] = SIGMA[1];
271
14
      m_state[2] = SIGMA[2];
272
14
      m_state[3] = SIGMA[3];
273
274
14
      m_state[8] = m_key[4];
275
14
      m_state[9] = m_key[5];
276
14
      m_state[10] = m_key[6];
277
14
      m_state[11] = m_key[7];
278
14
   }
279
280
14
   m_state[12] = 0;
281
14
   m_state[13] = 0;
282
14
   m_state[14] = 0;
283
14
   m_state[15] = 0;
284
285
14
   m_position = 0;
286
14
}
287
288
165k
bool ChaCha::has_keying_material() const {
289
165k
   return !m_state.empty();
290
165k
}
291
292
0
size_t ChaCha::buffer_size() const {
293
0
   return 64;
294
0
}
295
296
/*
297
* ChaCha Key Schedule
298
*/
299
14
void ChaCha::key_schedule(std::span<const uint8_t> key) {
300
14
   m_key.resize(key.size() / 4);
301
14
   load_le<uint32_t>(m_key.data(), key.data(), m_key.size());
302
303
14
   m_state.resize(16);
304
305
14
   const size_t chacha_block = 64;
306
14
   m_buffer.resize(parallelism() * chacha_block);
307
308
14
   set_iv(nullptr, 0);
309
14
}
310
311
0
size_t ChaCha::default_iv_length() const {
312
0
   return 24;
313
0
}
314
315
14
Key_Length_Specification ChaCha::key_spec() const {
316
14
   return Key_Length_Specification(16, 32, 16);
317
14
}
318
319
0
std::unique_ptr<StreamCipher> ChaCha::new_object() const {
320
0
   return std::make_unique<ChaCha>(m_rounds);
321
0
}
322
323
14
bool ChaCha::valid_iv_length(size_t iv_len) const {
324
14
   return (iv_len == 0 || iv_len == 8 || iv_len == 12 || iv_len == 24);
325
14
}
326
327
14
void ChaCha::set_iv_bytes(const uint8_t iv[], size_t length) {
328
14
   assert_key_material_set();
329
330
14
   if(!valid_iv_length(length)) {
331
0
      throw Invalid_IV_Length(name(), length);
332
0
   }
333
334
14
   initialize_state();
335
336
14
   if(length == 0) {
337
      // Treat zero length IV same as an all-zero IV
338
14
      m_state[14] = 0;
339
14
      m_state[15] = 0;
340
14
   } else if(length == 8) {
341
0
      m_state[14] = load_le<uint32_t>(iv, 0);
342
0
      m_state[15] = load_le<uint32_t>(iv, 1);
343
0
   } else if(length == 12) {
344
0
      m_state[13] = load_le<uint32_t>(iv, 0);
345
0
      m_state[14] = load_le<uint32_t>(iv, 1);
346
0
      m_state[15] = load_le<uint32_t>(iv, 2);
347
0
   } else if(length == 24) {
348
0
      m_state[12] = load_le<uint32_t>(iv, 0);
349
0
      m_state[13] = load_le<uint32_t>(iv, 1);
350
0
      m_state[14] = load_le<uint32_t>(iv, 2);
351
0
      m_state[15] = load_le<uint32_t>(iv, 3);
352
353
0
      secure_vector<uint32_t> hc(8);
354
0
      hchacha(hc.data(), m_state.data(), m_rounds);
355
356
0
      m_state[4] = hc[0];
357
0
      m_state[5] = hc[1];
358
0
      m_state[6] = hc[2];
359
0
      m_state[7] = hc[3];
360
0
      m_state[8] = hc[4];
361
0
      m_state[9] = hc[5];
362
0
      m_state[10] = hc[6];
363
0
      m_state[11] = hc[7];
364
0
      m_state[12] = 0;
365
0
      m_state[13] = 0;
366
0
      m_state[14] = load_le<uint32_t>(iv, 4);
367
0
      m_state[15] = load_le<uint32_t>(iv, 5);
368
0
   }
369
370
14
   chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
371
14
   m_position = 0;
372
14
}
373
374
0
void ChaCha::clear() {
375
0
   zap(m_key);
376
0
   zap(m_state);
377
0
   zap(m_buffer);
378
0
   m_position = 0;
379
0
}
380
381
0
std::string ChaCha::name() const {
382
0
   return fmt("ChaCha({})", m_rounds);
383
0
}
384
385
0
void ChaCha::seek(uint64_t offset) {
386
0
   assert_key_material_set();
387
388
   // Find the block offset
389
0
   const uint64_t counter = offset / 64;
390
391
0
   uint8_t out[8];
392
393
0
   store_le(counter, out);
394
395
0
   m_state[12] = load_le<uint32_t>(out, 0);
396
0
   m_state[13] += load_le<uint32_t>(out, 1);
397
398
0
   chacha(m_buffer.data(), m_buffer.size() / 64, m_state.data(), m_rounds);
399
0
   m_position = offset % 64;
400
0
}
401
}  // namespace Botan