8 #include <botan/salsa20.h>
9 #include <botan/loadstor.h>
10 #include <botan/rotate.h>
11 #include <botan/internal/xor_buf.h>
17 #define SALSA20_QUARTER_ROUND(x1, x2, x3, x4) \
19 x2 ^= rotate_left(x1 + x4, 7); \
20 x3 ^= rotate_left(x2 + x1, 9); \
21 x4 ^= rotate_left(x3 + x2, 13); \
22 x1 ^= rotate_left(x4 + x3, 18); \
30 u32bit x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
31 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
32 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
33 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
35 for(
size_t i = 0; i != 10; ++i)
61 void salsa20(
byte output[64],
const u32bit input[16])
63 u32bit x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
64 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
65 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
66 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
68 for(
size_t i = 0; i != 10; ++i)
81 store_le(x00 + input[ 0], output + 4 * 0);
82 store_le(x01 + input[ 1], output + 4 * 1);
83 store_le(x02 + input[ 2], output + 4 * 2);
84 store_le(x03 + input[ 3], output + 4 * 3);
85 store_le(x04 + input[ 4], output + 4 * 4);
86 store_le(x05 + input[ 5], output + 4 * 5);
87 store_le(x06 + input[ 6], output + 4 * 6);
88 store_le(x07 + input[ 7], output + 4 * 7);
89 store_le(x08 + input[ 8], output + 4 * 8);
90 store_le(x09 + input[ 9], output + 4 * 9);
91 store_le(x10 + input[10], output + 4 * 10);
92 store_le(x11 + input[11], output + 4 * 11);
93 store_le(x12 + input[12], output + 4 * 12);
94 store_le(x13 + input[13], output + 4 * 13);
95 store_le(x14 + input[14], output + 4 * 14);
96 store_le(x15 + input[15], output + 4 * 15);
106 while(length >= buffer.
size() - position)
108 xor_buf(out, in, &buffer[position], buffer.
size() - position);
109 length -= (buffer.
size() - position);
110 in += (buffer.
size() - position);
111 out += (buffer.
size() - position);
112 salsa20(&buffer[0], &state[0]);
121 xor_buf(out, in, &buffer[position], length);
129 void Salsa20::key_schedule(
const byte key[],
size_t length)
131 static const u32bit TAU[] =
132 { 0x61707865, 0x3120646e, 0x79622d36, 0x6b206574 };
134 static const u32bit SIGMA[] =
135 { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
154 else if(length == 32)
162 state[10] = SIGMA[2];
167 state[15] = SIGMA[3];
170 const byte ZERO[8] = { 0 };
171 set_iv(ZERO,
sizeof(ZERO));
197 hsalsa20(&hsalsa[0], &state[0]);
199 state[ 1] = hsalsa[0];
200 state[ 2] = hsalsa[1];
201 state[ 3] = hsalsa[2];
202 state[ 4] = hsalsa[3];
205 state[11] = hsalsa[4];
206 state[12] = hsalsa[5];
207 state[13] = hsalsa[6];
208 state[14] = hsalsa[7];
214 salsa20(&buffer[0], &state[0]);
void set_iv(const byte iv[], size_t iv_len)
void store_le(u16bit in, byte out[2])
bool valid_iv_length(size_t iv_len) const
u32bit load_le< u32bit >(const byte in[], size_t off)
#define SALSA20_QUARTER_ROUND(x1, x2, x3, x4)
void cipher(const byte in[], byte out[], size_t length)
void xor_buf(byte out[], const byte in[], size_t length)
void zeroise(MemoryRegion< T > &vec)