8 #include <botan/salsa20.h>
9 #include <botan/loadstor.h>
15 #define SALSA20_QUARTER_ROUND(x1, x2, x3, x4) \
17 x2 ^= rotate_left(x1 + x4, 7); \
18 x3 ^= rotate_left(x2 + x1, 9); \
19 x4 ^= rotate_left(x3 + x2, 13); \
20 x1 ^= rotate_left(x4 + x3, 18); \
26 void hsalsa20(uint32_t output[8],
const uint32_t input[16])
28 uint32_t x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
29 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
30 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
31 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
33 for(
size_t i = 0; i != 10; ++i)
59 void salsa20(uint8_t output[64],
const uint32_t input[16])
61 uint32_t x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
62 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
63 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
64 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
66 for(
size_t i = 0; i != 10; ++i)
79 store_le(x00 + input[ 0], output + 4 * 0);
80 store_le(x01 + input[ 1], output + 4 * 1);
81 store_le(x02 + input[ 2], output + 4 * 2);
82 store_le(x03 + input[ 3], output + 4 * 3);
83 store_le(x04 + input[ 4], output + 4 * 4);
84 store_le(x05 + input[ 5], output + 4 * 5);
85 store_le(x06 + input[ 6], output + 4 * 6);
86 store_le(x07 + input[ 7], output + 4 * 7);
87 store_le(x08 + input[ 8], output + 4 * 8);
88 store_le(x09 + input[ 9], output + 4 * 9);
89 store_le(x10 + input[10], output + 4 * 10);
90 store_le(x11 + input[11], output + 4 * 11);
91 store_le(x12 + input[12], output + 4 * 12);
92 store_le(x13 + input[13], output + 4 * 13);
93 store_le(x14 + input[14], output + 4 * 14);
94 store_le(x15 + input[15], output + 4 * 15);
99 #undef SALSA20_QUARTER_ROUND
106 while(length >= m_buffer.size() - m_position)
108 xor_buf(out, in, &m_buffer[m_position], m_buffer.size() - m_position);
109 length -= (m_buffer.size() - m_position);
110 in += (m_buffer.size() - m_position);
111 out += (m_buffer.size() - m_position);
112 salsa20(m_buffer.data(), m_state.data());
115 m_state[9] += (m_state[8] == 0);
120 xor_buf(out, in, &m_buffer[m_position], length);
122 m_position += length;
128 void Salsa20::key_schedule(
const uint8_t key[],
size_t length)
130 static const uint32_t TAU[] =
131 { 0x61707865, 0x3120646e, 0x79622d36, 0x6b206574 };
133 static const uint32_t SIGMA[] =
134 { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
136 const uint32_t* CONSTANTS = (length == 16) ? TAU : SIGMA;
141 m_state[0] = CONSTANTS[0];
142 m_state[5] = CONSTANTS[1];
143 m_state[10] = CONSTANTS[2];
144 m_state[15] = CONSTANTS[3];
193 hsalsa20(hsalsa.data(), m_state.data());
195 m_state[ 1] = hsalsa[0];
196 m_state[ 2] = hsalsa[1];
197 m_state[ 3] = hsalsa[2];
198 m_state[ 4] = hsalsa[3];
201 m_state[11] = hsalsa[4];
202 m_state[12] = hsalsa[5];
203 m_state[13] = hsalsa[6];
204 m_state[14] = hsalsa[7];
210 salsa20(m_buffer.data(), m_state.data());
212 m_state[9] += (m_state[8] == 0);
void xor_buf(T out[], const T in[], size_t length)
void zap(std::vector< T, Alloc > &vec)
std::string name() const override
uint32_t load_le< uint32_t >(const uint8_t in[], size_t off)
void seek(uint64_t offset) override
std::vector< T, secure_allocator< T >> secure_vector
#define SALSA20_QUARTER_ROUND(x1, x2, x3, x4)
bool valid_iv_length(size_t iv_len) const override
void set_iv(const uint8_t iv[], size_t iv_len) override
void store_le(uint16_t in, uint8_t out[2])
void cipher(const uint8_t in[], uint8_t out[], size_t length) override