8#include <botan/internal/salsa20.h>
9#include <botan/exceptn.h>
10#include <botan/internal/loadstor.h>
11#include <botan/internal/rotate.h>
17inline void salsa20_quarter_round(uint32_t& x1,
22 x2 ^= rotl<7>(x1 + x4);
23 x3 ^= rotl<9>(x2 + x1);
24 x4 ^= rotl<13>(x3 + x2);
25 x1 ^= rotl<18>(x4 + x3);
36 uint32_t x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
37 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
38 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
39 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
41 for(
size_t i = 0; i != 10; ++i)
43 salsa20_quarter_round(x00, x04, x08, x12);
44 salsa20_quarter_round(x05, x09, x13, x01);
45 salsa20_quarter_round(x10, x14, x02, x06);
46 salsa20_quarter_round(x15, x03, x07, x11);
48 salsa20_quarter_round(x00, x01, x02, x03);
49 salsa20_quarter_round(x05, x06, x07, x04);
50 salsa20_quarter_round(x10, x11, x08, x09);
51 salsa20_quarter_round(x15, x12, x13, x14);
72 uint32_t x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
73 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
74 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
75 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
77 for(
size_t i = 0; i != rounds / 2; ++i)
79 salsa20_quarter_round(x00, x04, x08, x12);
80 salsa20_quarter_round(x05, x09, x13, x01);
81 salsa20_quarter_round(x10, x14, x02, x06);
82 salsa20_quarter_round(x15, x03, x07, x11);
84 salsa20_quarter_round(x00, x01, x02, x03);
85 salsa20_quarter_round(x05, x06, x07, x04);
86 salsa20_quarter_round(x10, x11, x08, x09);
87 salsa20_quarter_round(x15, x12, x13, x14);
90 store_le(x00 + input[ 0], output + 4 * 0);
91 store_le(x01 + input[ 1], output + 4 * 1);
92 store_le(x02 + input[ 2], output + 4 * 2);
93 store_le(x03 + input[ 3], output + 4 * 3);
94 store_le(x04 + input[ 4], output + 4 * 4);
95 store_le(x05 + input[ 5], output + 4 * 5);
96 store_le(x06 + input[ 6], output + 4 * 6);
97 store_le(x07 + input[ 7], output + 4 * 7);
98 store_le(x08 + input[ 8], output + 4 * 8);
99 store_le(x09 + input[ 9], output + 4 * 9);
100 store_le(x10 + input[10], output + 4 * 10);
101 store_le(x11 + input[11], output + 4 * 11);
102 store_le(x12 + input[12], output + 4 * 12);
103 store_le(x13 + input[13], output + 4 * 13);
104 store_le(x14 + input[14], output + 4 * 14);
105 store_le(x15 + input[15], output + 4 * 15);
115 while(length >= m_buffer.size() - m_position)
117 const size_t available = m_buffer.size() - m_position;
119 xor_buf(out, in, &m_buffer[m_position], available);
120 salsa_core(m_buffer.data(), m_state.data(), 20);
123 m_state[9] += (m_state[8] == 0);
132 xor_buf(out, in, &m_buffer[m_position], length);
134 m_position += length;
137void Salsa20::initialize_state()
139 static const uint32_t TAU[] =
140 { 0x61707865, 0x3120646e, 0x79622d36, 0x6b206574 };
142 static const uint32_t SIGMA[] =
143 { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
145 m_state[1] = m_key[0];
146 m_state[2] = m_key[1];
147 m_state[3] = m_key[2];
148 m_state[4] = m_key[3];
150 if(m_key.size() == 4)
154 m_state[10] = TAU[2];
155 m_state[15] = TAU[3];
156 m_state[11] = m_key[0];
157 m_state[12] = m_key[1];
158 m_state[13] = m_key[2];
159 m_state[14] = m_key[3];
163 m_state[0] = SIGMA[0];
164 m_state[5] = SIGMA[1];
165 m_state[10] = SIGMA[2];
166 m_state[15] = SIGMA[3];
167 m_state[11] = m_key[4];
168 m_state[12] = m_key[5];
169 m_state[13] = m_key[6];
170 m_state[14] = m_key[7];
183 return !m_state.empty();
194void Salsa20::key_schedule(
const uint8_t key[],
size_t length)
196 m_key.resize(length / 4);
238 hsalsa20(hsalsa.data(), m_state.data());
240 m_state[ 1] = hsalsa[0];
241 m_state[ 2] = hsalsa[1];
242 m_state[ 3] = hsalsa[2];
243 m_state[ 4] = hsalsa[3];
246 m_state[11] = hsalsa[4];
247 m_state[12] = hsalsa[5];
248 m_state[13] = hsalsa[6];
249 m_state[14] = hsalsa[7];
255 salsa_core(m_buffer.data(), m_state.data(), 20);
257 m_state[9] += (m_state[8] == 0);
264 return (iv_len == 0 || iv_len == 8 || iv_len == 24);
279 return std::make_unique<Salsa20>();
303 const uint64_t counter = offset / 64;
310 salsa_core(m_buffer.data(), m_state.data(), 20);
313 m_state[9] += (m_state[8] == 0);
315 m_position = offset % 64;
#define BOTAN_ASSERT_NOMSG(expr)
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])
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 set_iv(const uint8_t iv[], size_t iv_len)
void assert_key_material_set() const
constexpr uint32_t load_le< uint32_t >(const uint8_t in[], size_t off)
constexpr void store_le(uint16_t in, uint8_t out[2])
void zap(std::vector< T, Alloc > &vec)
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
std::vector< T, secure_allocator< T > > secure_vector