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