Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

chacha.cpp
Go to the documentation of this file.
1 /*
2 * ChaCha
3 * (C) 2014 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/chacha.h>
9 #include <botan/loadstor.h>
10 #include <botan/cpuid.h>
11
12 namespace Botan {
13
14 ChaCha::ChaCha(size_t rounds) : m_rounds(rounds)
15 {
16 if(m_rounds != 8 && m_rounds != 12 && m_rounds != 20)
17 throw Invalid_Argument("ChaCha only supports 8, 12 or 20 rounds");
18 }
19
20 std::string ChaCha::provider() const
21 {
22 #if defined(BOTAN_HAS_CHACHA_SSE2)
23 if(CPUID::has_sse2())
24 {
25 return "sse2";
26 }
27 #endif
28
29 return "base";
30 }
31
32 //static
33 void ChaCha::chacha_x4(uint8_t output[64*4], uint32_t input[16], size_t rounds)
34 {
35 BOTAN_ASSERT(rounds % 2 == 0, "Valid rounds");
36
37 #if defined(BOTAN_HAS_CHACHA_SSE2)
38 if(CPUID::has_sse2())
39 {
40 return ChaCha::chacha_sse2_x4(output, input, rounds);
41 }
42 #endif
43
44 // TODO interleave rounds
45 for(size_t i = 0; i != 4; ++i)
46 {
47 uint32_t x00 = input[ 0], x01 = input[ 1], x02 = input[ 2], x03 = input[ 3],
48 x04 = input[ 4], x05 = input[ 5], x06 = input[ 6], x07 = input[ 7],
49 x08 = input[ 8], x09 = input[ 9], x10 = input[10], x11 = input[11],
50 x12 = input[12], x13 = input[13], x14 = input[14], x15 = input[15];
51
52 #define CHACHA_QUARTER_ROUND(a, b, c, d) \
53 do { \
54 a += b; d ^= a; d = rotate_left(d, 16); \
55 c += d; b ^= c; b = rotate_left(b, 12); \
56 a += b; d ^= a; d = rotate_left(d, 8); \
57 c += d; b ^= c; b = rotate_left(b, 7); \
58 } while(0)
59
60 for(size_t r = 0; r != rounds / 2; ++r)
61 {
62 CHACHA_QUARTER_ROUND(x00, x04, x08, x12);
63 CHACHA_QUARTER_ROUND(x01, x05, x09, x13);
64 CHACHA_QUARTER_ROUND(x02, x06, x10, x14);
65 CHACHA_QUARTER_ROUND(x03, x07, x11, x15);
66
67 CHACHA_QUARTER_ROUND(x00, x05, x10, x15);
68 CHACHA_QUARTER_ROUND(x01, x06, x11, x12);
69 CHACHA_QUARTER_ROUND(x02, x07, x08, x13);
70 CHACHA_QUARTER_ROUND(x03, x04, x09, x14);
71 }
72
73 #undef CHACHA_QUARTER_ROUND
74
75 x00 += input[0];
76 x01 += input[1];
77 x02 += input[2];
78 x03 += input[3];
79 x04 += input[4];
80 x05 += input[5];
81 x06 += input[6];
82 x07 += input[7];
83 x08 += input[8];
84 x09 += input[9];
85 x10 += input[10];
86 x11 += input[11];
87 x12 += input[12];
88 x13 += input[13];
89 x14 += input[14];
90 x15 += input[15];
91
92 store_le(x00, output + 64 * i + 4 * 0);
93 store_le(x01, output + 64 * i + 4 * 1);
94 store_le(x02, output + 64 * i + 4 * 2);
95 store_le(x03, output + 64 * i + 4 * 3);
96 store_le(x04, output + 64 * i + 4 * 4);
97 store_le(x05, output + 64 * i + 4 * 5);
98 store_le(x06, output + 64 * i + 4 * 6);
99 store_le(x07, output + 64 * i + 4 * 7);
100 store_le(x08, output + 64 * i + 4 * 8);
101 store_le(x09, output + 64 * i + 4 * 9);
102 store_le(x10, output + 64 * i + 4 * 10);
103 store_le(x11, output + 64 * i + 4 * 11);
104 store_le(x12, output + 64 * i + 4 * 12);
105 store_le(x13, output + 64 * i + 4 * 13);
106 store_le(x14, output + 64 * i + 4 * 14);
107 store_le(x15, output + 64 * i + 4 * 15);
108
109 input[12]++;
110 input[13] += input[12] < i; // carry?
111 }
112 }
113
114 /*
115 * Combine cipher stream with message
116 */
117 void ChaCha::cipher(const uint8_t in[], uint8_t out[], size_t length)
118 {
119 while(length >= m_buffer.size() - m_position)
120 {
121 xor_buf(out, in, &m_buffer[m_position], m_buffer.size() - m_position);
122 length -= (m_buffer.size() - m_position);
123 in += (m_buffer.size() - m_position);
124 out += (m_buffer.size() - m_position);
125 chacha_x4(m_buffer.data(), m_state.data(), m_rounds);
126 m_position = 0;
127 }
128
129 xor_buf(out, in, &m_buffer[m_position], length);
130
131 m_position += length;
132 }
133
134 /*
135 * ChaCha Key Schedule
136 */
137 void ChaCha::key_schedule(const uint8_t key[], size_t length)
138 {
139 static const uint32_t TAU[] =
140 { 0x61707865, 0x3120646e, 0x79622d36, 0x6b206574 };
141
142 static const uint32_t SIGMA[] =
143 { 0x61707865, 0x3320646e, 0x79622d32, 0x6b206574 };
144
145 const uint32_t* CONSTANTS = (length == 16) ? TAU : SIGMA;
146
147 // Repeat the key if 128 bits
148 const uint8_t* key2 = (length == 32) ? key + 16 : key;
149
150 m_position = 0;
151 m_state.resize(16);
152 m_buffer.resize(4*64);
153
154 m_state[0] = CONSTANTS[0];
155 m_state[1] = CONSTANTS[1];
156 m_state[2] = CONSTANTS[2];
157 m_state[3] = CONSTANTS[3];
158
159 m_state[4] = load_le<uint32_t>(key, 0);
160 m_state[5] = load_le<uint32_t>(key, 1);
161 m_state[6] = load_le<uint32_t>(key, 2);
162 m_state[7] = load_le<uint32_t>(key, 3);
163
164 m_state[8] = load_le<uint32_t>(key2, 0);
165 m_state[9] = load_le<uint32_t>(key2, 1);
166 m_state[10] = load_le<uint32_t>(key2, 2);
167 m_state[11] = load_le<uint32_t>(key2, 3);
168
169 // Default all-zero IV
170 const uint8_t ZERO[8] = { 0 };
171 set_iv(ZERO, sizeof(ZERO));
172 }
173
174 bool ChaCha::valid_iv_length(size_t iv_len) const
175 {
176 return (iv_len == 0 || iv_len == 8 || iv_len == 12);
177 }
178
179 void ChaCha::set_iv(const uint8_t iv[], size_t length)
180 {
181 if(!valid_iv_length(length))
182 throw Invalid_IV_Length(name(), length);
183
184 m_state[12] = 0;
185 m_state[13] = 0;
186
187 if(length == 0)
188 {
189 // Treat zero length IV same as an all-zero IV
190 m_state[14] = 0;
191 m_state[15] = 0;
192 }
193 else if(length == 8)
194 {
195 m_state[14] = load_le<uint32_t>(iv, 0);
196 m_state[15] = load_le<uint32_t>(iv, 1);
197 }
198 else if(length == 12)
199 {
200 m_state[13] = load_le<uint32_t>(iv, 0);
201 m_state[14] = load_le<uint32_t>(iv, 1);
202 m_state[15] = load_le<uint32_t>(iv, 2);
203 }
204
205 chacha_x4(m_buffer.data(), m_state.data(), m_rounds);
206 m_position = 0;
207 }
208
209 void ChaCha::clear()
210 {
211 zap(m_state);
212 zap(m_buffer);
213 m_position = 0;
214 }
215
216 std::string ChaCha::name() const
217 {
218 return "ChaCha(" + std::to_string(m_rounds) + ")";
219 }
220
221 void ChaCha::seek(uint64_t offset)
222 {
223 if (m_state.size() == 0 && m_buffer.size() == 0)
224 {
225 throw Invalid_State("You have to setup the stream cipher (key and iv)");
226 }
227
228 // Find the block offset
229 uint64_t counter = offset / 64;
230
231 uint8_t out[8];
232
233 store_le(counter, out);
234
235 m_state[12] = load_le<uint32_t>(out, 0);
236 m_state[13] += load_le<uint32_t>(out, 1);
237
238 chacha_x4(m_buffer.data(), m_state.data(), m_rounds);
239 m_position = offset % 64;
240 }
241 }
Botan::xor_buf
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
Botan::ChaCha::ChaCha
ChaCha(size_t rounds=20)
Definition: chacha.cpp:14
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::zap
void zap(std::vector< T, Alloc > &vec)
Definition: secmem.h:221
Botan::ChaCha::cipher
void cipher(const uint8_t in[], uint8_t out[], size_t length) override
Definition: chacha.cpp:117
Botan::load_le< uint32_t >
uint32_t load_le< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:204
Botan::Invalid_State
Definition: exceptn.h:61
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Botan::Invalid_IV_Length
Definition: exceptn.h:109
Botan::ChaCha::set_iv
void set_iv(const uint8_t iv[], size_t iv_len) override
Definition: chacha.cpp:179
Botan::ChaCha::clear
void clear() override
Definition: chacha.cpp:209
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
Botan::ChaCha::valid_iv_length
bool valid_iv_length(size_t iv_len) const override
Definition: chacha.cpp:174
Botan
Definition: alg_id.cpp:13
Botan::ChaCha::name
std::string name() const override
Definition: chacha.cpp:216
Botan::ChaCha::seek
void seek(uint64_t offset) override
Definition: chacha.cpp:221
CHACHA_QUARTER_ROUND
#define CHACHA_QUARTER_ROUND(a, b, c, d)
Botan::ChaCha::provider
std::string provider() const override
Definition: chacha.cpp:20
Botan::store_le
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:457