8#include <botan/salsa20.h>
9#include <botan/exceptn.h>
10#include <botan/loadstor.h>
11#include <botan/rotate.h>
15#define SALSA20_QUARTER_ROUND(x1, x2, x3, x4) \
17 x2 ^= rotl<7>(x1 + x4); \
18 x3 ^= rotl<9>(x2 + x1); \
19 x4 ^= rotl<13>(x3 + x2); \
20 x1 ^= rotl<18>(x4 + x3); \
29 uint32_t x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
30 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
31 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
32 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
34 for(
size_t i = 0; i != 10; ++i)
65 uint32_t x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
66 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
67 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
68 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
70 for(
size_t i = 0; i != rounds / 2; ++i)
83 store_le(x00 + input[ 0], output + 4 * 0);
84 store_le(x01 + input[ 1], output + 4 * 1);
85 store_le(x02 + input[ 2], output + 4 * 2);
86 store_le(x03 + input[ 3], output + 4 * 3);
87 store_le(x04 + input[ 4], output + 4 * 4);
88 store_le(x05 + input[ 5], output + 4 * 5);
89 store_le(x06 + input[ 6], output + 4 * 6);
90 store_le(x07 + input[ 7], output + 4 * 7);
91 store_le(x08 + input[ 8], output + 4 * 8);
92 store_le(x09 + input[ 9], output + 4 * 9);
93 store_le(x10 + input[10], output + 4 * 10);
94 store_le(x11 + input[11], output + 4 * 11);
95 store_le(x12 + input[12], output + 4 * 12);
96 store_le(x13 + input[13], output + 4 * 13);
97 store_le(x14 + input[14], output + 4 * 14);
98 store_le(x15 + input[15], output + 4 * 15);
101#undef SALSA20_QUARTER_ROUND
110 while(length >= m_buffer.size() - m_position)
112 const size_t available = m_buffer.size() - m_position;
114 xor_buf(out, in, &m_buffer[m_position], available);
115 salsa_core(m_buffer.data(), m_state.data(), 20);
118 m_state[9] += (m_state[8] == 0);
127 xor_buf(out, in, &m_buffer[m_position], length);
129 m_position += length;
132void Salsa20::initialize_state()
134 static const uint32_t TAU[] =
135 { 0x61707865, 0x3120646e, 0x79622d36, 0x6b206574 };
137 static const uint32_t SIGMA[] =
138 { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
140 m_state[1] = m_key[0];
141 m_state[2] = m_key[1];
142 m_state[3] = m_key[2];
143 m_state[4] = m_key[3];
145 if(m_key.size() == 4)
149 m_state[10] = TAU[2];
150 m_state[15] = TAU[3];
151 m_state[11] = m_key[0];
152 m_state[12] = m_key[1];
153 m_state[13] = m_key[2];
154 m_state[14] = m_key[3];
158 m_state[0] = SIGMA[0];
159 m_state[5] = SIGMA[1];
160 m_state[10] = SIGMA[2];
161 m_state[15] = SIGMA[3];
162 m_state[11] = m_key[4];
163 m_state[12] = m_key[5];
164 m_state[13] = m_key[6];
165 m_state[14] = m_key[7];
179void Salsa20::key_schedule(
const uint8_t key[],
size_t length)
181 m_key.resize(length / 4);
223 hsalsa20(hsalsa.data(), m_state.data());
225 m_state[ 1] = hsalsa[0];
226 m_state[ 2] = hsalsa[1];
227 m_state[ 3] = hsalsa[2];
228 m_state[ 4] = hsalsa[3];
231 m_state[11] = hsalsa[4];
232 m_state[12] = hsalsa[5];
233 m_state[13] = hsalsa[6];
234 m_state[14] = hsalsa[7];
240 salsa_core(m_buffer.data(), m_state.data(), 20);
242 m_state[9] += (m_state[8] == 0);
249 return (iv_len == 0 || iv_len == 8 || iv_len == 24);
288 const uint64_t counter = offset / 64;
295 salsa_core(m_buffer.data(), m_state.data(), 20);
298 m_state[9] += (m_state[8] == 0);
300 m_position = offset % 64;
#define BOTAN_ASSERT_NOMSG(expr)
void seek(uint64_t offset) override
static void hsalsa20(uint32_t output[8], const uint32_t input[16])
StreamCipher * clone() 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)
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
std::string name() const override
Key_Length_Specification key_spec() const override
void set_iv(const uint8_t iv[], size_t iv_len) override
void verify_key_set(bool cond) const
void zap(std::vector< T, Alloc > &vec)
uint32_t load_le< uint32_t >(const uint8_t in[], size_t off)
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
void store_le(uint16_t in, uint8_t out[2])
std::vector< T, secure_allocator< T > > secure_vector
#define SALSA20_QUARTER_ROUND(x1, x2, x3, x4)