8#include <botan/internal/salsa20.h>
10#include <botan/exceptn.h>
11#include <botan/internal/loadstor.h>
12#include <botan/internal/rotate.h>
14#if defined(BOTAN_HAS_CPUID)
15 #include <botan/internal/cpuid.h>
22inline void salsa20_quarter_round(uint32_t& x1, uint32_t& x2, uint32_t& x3, uint32_t& x4) {
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];
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);
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);
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];
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);
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);
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);
129size_t Salsa20::parallelism() {
130#if defined(BOTAN_HAS_SALSA20_AVX512)
136#if defined(BOTAN_HAS_SALSA20_AVX2)
146#if defined(BOTAN_HAS_SALSA20_AVX512)
152#if defined(BOTAN_HAS_SALSA20_AVX2)
158#if defined(BOTAN_HAS_SALSA20_SIMD32)
168void Salsa20::salsa20(uint8_t output[],
size_t output_blocks, uint32_t state[16],
size_t rounds) {
171#if defined(BOTAN_HAS_SALSA20_AVX512)
173 while(output_blocks >= 16) {
174 Salsa20::salsa20_avx512_x16(output, state, rounds);
181#if defined(BOTAN_HAS_SALSA20_AVX2)
183 while(output_blocks >= 8) {
184 Salsa20::salsa20_avx2_x8(output, state, rounds);
191#if defined(BOTAN_HAS_SALSA20_SIMD32)
193 while(output_blocks >= 4) {
194 Salsa20::salsa20_simd32_x4(output, state, rounds);
201 for(
size_t i = 0; i != output_blocks; ++i) {
217 while(length >= m_buffer.size() - m_position) {
218 const size_t available = m_buffer.size() - m_position;
220 xor_buf(out, in, &m_buffer[m_position], available);
221 salsa20(m_buffer.data(), m_buffer.size() / 64, m_state.data(), 20);
230 xor_buf(out, in, &m_buffer[m_position], length);
232 m_position += length;
238 while(length >= m_buffer.size() - m_position) {
239 const size_t available = m_buffer.size() - m_position;
243 copy_mem(out, &m_buffer[m_position], available);
244 salsa20(m_buffer.data(), m_buffer.size() / 64, m_state.data(), 20);
251 copy_mem(out, &m_buffer[m_position], length);
253 m_position += length;
256void Salsa20::initialize_state() {
257 static const uint32_t TAU[] = {0x61707865, 0x3120646e, 0x79622d36, 0x6b206574};
259 static const uint32_t SIGMA[] = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574};
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];
266 if(m_key.size() == 4) {
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];
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];
295 return !m_state.empty();
305void Salsa20::key_schedule(std::span<const uint8_t> key) {
306 m_key.resize(key.size() / 4);
311 const size_t salsa_block = 64;
312 m_buffer.resize(parallelism() * salsa_block);
333 }
else if(length == 8) {
345 hsalsa20(hsalsa.data(), m_state.data());
347 m_state[1] = hsalsa[0];
348 m_state[2] = hsalsa[1];
349 m_state[3] = hsalsa[2];
350 m_state[4] = hsalsa[3];
353 m_state[11] = hsalsa[4];
354 m_state[12] = hsalsa[5];
355 m_state[13] = hsalsa[6];
356 m_state[14] = hsalsa[7];
362 salsa20(m_buffer.data(), m_buffer.size() / 64, m_state.data(), 20);
367 return (iv_len == 0 || iv_len == 8 || iv_len == 24);
379 return std::make_unique<Salsa20>();
399 const uint64_t counter = offset / 64;
401 m_state[8] =
static_cast<uint32_t
>(counter);
402 m_state[9] =
static_cast<uint32_t
>(counter >> 32);
404 salsa20(m_buffer.data(), m_buffer.size() / 64, m_state.data(), 20);
406 m_position = offset % 64;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT(expr, assertion_made)
static std::optional< std::string > check(CPUID::Feature feat)
static bool has(CPUID::Feature feat)
void seek(uint64_t offset) override
bool has_keying_material() const override
size_t buffer_size() const override
void set_iv_bytes(const uint8_t iv[], size_t iv_len) override
void cipher_bytes(const uint8_t in[], uint8_t out[], size_t length) override
static void hsalsa20(uint32_t output[8], const uint32_t input[16])
std::string provider() const override
bool valid_iv_length(size_t iv_len) const override
size_t default_iv_length() const override
static void salsa_core(uint8_t output[64], const uint32_t input[16], size_t rounds)
std::unique_ptr< StreamCipher > new_object() const override
std::string name() const override
Key_Length_Specification key_spec() const override
void generate_keystream(uint8_t out[], size_t len) override
void set_iv(const uint8_t iv[], size_t iv_len)
void assert_key_material_set() const
void zap(std::vector< T, Alloc > &vec)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr auto store_le(ParamTs &&... params)
BOTAN_FORCE_INLINE constexpr T rotl(T input)
constexpr auto load_le(ParamTs &&... params)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector