salsa20_functions.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2019 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_STREAM_SALSA20_FUNCTIONS_HPP
26 #define CRYPTO3_STREAM_SALSA20_FUNCTIONS_HPP
27 
29 
30 #define SALSA20_QUARTER_ROUND(x1, x2, x3, x4) \
31  do { \
32  x2 ^= policy_type::rotl<7>(x1 + x4); \
33  x3 ^= policy_type::rotl<9>(x2 + x1); \
34  x4 ^= policy_type::rotl<13>(x3 + x2); \
35  x1 ^= policy_type::rotl<18>(x4 + x3); \
36  } while (0)
37 
38 namespace nil {
39  namespace crypto3 {
40  namespace stream {
41  namespace detail {
42  template<std::size_t IVSize, std::size_t KeyBits, std::size_t Rounds>
43  struct salsa20_functions : public salsa20_policy<IVSize, KeyBits, Rounds> {
45 
46  constexpr static const std::size_t word_bits = policy_type::word_bits;
47  constexpr static const std::size_t word_bytes = word_bits / CHAR_BIT;
49 
50  constexpr static const std::size_t rounds = policy_type::rounds;
51 
52  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
53  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
55 
56  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
57  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
58  constexpr static const std::size_t key_bits = policy_type::key_bits;
59  typedef typename policy_type::key_type key_type;
60 
61  constexpr static const std::size_t block_bits = policy_type::block_bits;
62  constexpr static const std::size_t block_size = policy_type::block_size;
64 
65  constexpr static const std::size_t iv_bits = policy_type::iv_bits;
66  typedef typename policy_type::iv_type iv_type;
67 
68  void schedule_iv(block_type &block, key_schedule_type &schedule, const iv_type &iv) {
69  // XSalsa20
70 
71  for (std::uint8_t itr = 0; itr < 4; itr++) {
72  schedule[itr + 6] = boost::endian::native_to_little(
73  make_uint_t(iv[word_bytes * itr], iv[word_bytes * itr + 1], iv[word_bytes * itr + 2],
74  iv[word_bytes * itr + 3]));
75  }
76 
77  std::array<word_type, 8> hsalsa;
78  policy_type::hsalsa20(hsalsa.data(), schedule);
79 
80  schedule[1] = hsalsa[0];
81  schedule[2] = hsalsa[1];
82  schedule[3] = hsalsa[2];
83  schedule[4] = hsalsa[3];
84  schedule[6] = boost::endian::native_to_little(
85  make_uint_t(iv[word_bytes * 4], iv[word_bytes * 4 + 1], iv[word_bytes * 4 + 2],
86  iv[word_bytes * 4 + 3]));
87  schedule[7] = boost::endian::native_to_little(
88  make_uint_t(iv[word_bytes * 5], iv[word_bytes * 5 + 1], iv[word_bytes * 5 + 2],
89  iv[word_bytes * 5 + 3]));
90  schedule[11] = hsalsa[4];
91  schedule[12] = hsalsa[5];
92  schedule[13] = hsalsa[6];
93  schedule[14] = hsalsa[7];
94 
95  schedule[8] = 0;
96  schedule[9] = 0;
97 
98  policy_type::salsa_core(block, schedule);
99  ++schedule[8];
100  schedule[9] += (schedule[8] == 0);
101  }
102  };
103 
104  template<std::size_t Rounds>
105  struct salsa20_functions<64, 128, Rounds> : public salsa20_policy<64, 128, Rounds> {
107 
108  constexpr static const std::size_t word_bits = policy_type::word_bits;
109  constexpr static const std::size_t word_bytes = word_bits / CHAR_BIT;
111 
112  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
113  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
115 
116  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
117  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
118  constexpr static const std::size_t key_bits = policy_type::key_bits;
119  typedef typename policy_type::key_type key_type;
120 
121  constexpr static const std::size_t block_bits = policy_type::block_bits;
122  constexpr static const std::size_t block_size = policy_type::block_size;
124 
125  constexpr static const std::size_t iv_bits = policy_type::iv_bits;
126  typedef typename policy_type::iv_type iv_type;
127 
128  void schedule_key(key_schedule_type &schedule, const key_type &key) {
129  schedule[0] = policy_type::tau()[0];
130  schedule[5] = policy_type::tau()[1];
131  schedule[10] = policy_type::tau()[2];
132  schedule[15] = policy_type::tau()[3];
133 
134 
135  for (std::uint8_t itr = 0; itr < 4; itr++) {
136  schedule[itr + 1] = boost::endian::native_to_little(
137  make_uint_t(key[4 * itr], key[4 * itr + 1], key[4 * itr + 2], key[4 * itr + 3]));
138  schedule[itr + 11] = boost::endian::native_to_little(
139  make_uint_t(key[4 * itr], key[4 * itr + 1], key[4 * itr + 2], key[4 * itr + 3]));
140  }
141  }
142 
143  void schedule_iv(block_type &block, key_schedule_type &schedule, const iv_type &iv) {
144  // Salsa20
145  schedule[6] = boost::endian::native_to_little(make_uint_t(iv[0], iv[1], iv[2], iv[3]));
146  schedule[7] = boost::endian::native_to_little(make_uint_t(iv[4], iv[5], iv[6], iv[7]));
147  schedule[8] = 0;
148  schedule[9] = 0;
149 
150  policy_type::salsa_core(block, schedule);
151  ++schedule[8];
152  schedule[9] += (schedule[8] == 0);
153  }
154  };
155 
156  template<std::size_t Rounds>
157  struct salsa20_functions<96, 128, Rounds> : public salsa20_policy<96, 128, Rounds> {
159 
160  constexpr static const std::size_t word_bits = policy_type::word_bits;
161  constexpr static const std::size_t word_bytes = word_bits / CHAR_BIT;
163 
164  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
165  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
167 
168  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
169  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
170  constexpr static const std::size_t key_bits = policy_type::key_bits;
171  typedef typename policy_type::key_type key_type;
172 
173  constexpr static const std::size_t block_bits = policy_type::block_bits;
174  constexpr static const std::size_t block_size = policy_type::block_size;
176 
177  constexpr static const std::size_t iv_bits = policy_type::iv_bits;
178  typedef typename policy_type::iv_type iv_type;
179 
180  void schedule_key(key_schedule_type &schedule, const key_type &key) {
181  schedule[0] = policy_type::tau()[0];
182  schedule[5] = policy_type::tau()[1];
183  schedule[10] = policy_type::tau()[2];
184  schedule[15] = policy_type::tau()[3];
185 
186 
187  for (std::uint8_t itr = 0; itr < 4; itr++) {
188  schedule[itr + 1] = boost::endian::native_to_little(
189  make_uint_t(key[4 * itr], key[4 * itr + 1], key[4 * itr + 2], key[4 * itr + 3]));
190  schedule[itr + 11] = boost::endian::native_to_little(
191  make_uint_t(key[4 * itr], key[4 * itr + 1], key[4 * itr + 2], key[4 * itr + 3]));
192  }
193  }
194 
196  // XSalsa20
197 
198  for (std::uint8_t itr = 0; itr < 4; itr++) {
199  state[itr + 6] = boost::endian::native_to_little(
200  make_uint_t(iv[4 * itr], iv[4 * itr + 1], iv[4 * itr + 2], iv[4 * itr + 3]));
201  }
202 
203  std::array<word_type, 8> hsalsa;
204  policy_type::hsalsa20(hsalsa.data(), state);
205 
206  state[1] = hsalsa[0];
207  state[2] = hsalsa[1];
208  state[3] = hsalsa[2];
209  state[4] = hsalsa[3];
210  state[6] = boost::endian::native_to_little(
211  make_uint_t(iv[word_bytes * 4], iv[word_bytes * 4 + 1], iv[word_bytes * 4 + 2],
212  iv[word_bytes * 4 + 3]));
213  state[7] = boost::endian::native_to_little(
214  make_uint_t(iv[word_bytes * 5], iv[word_bytes * 5 + 1], iv[word_bytes * 5 + 2],
215  iv[word_bytes * 5 + 3]));
216  state[11] = hsalsa[4];
217  state[12] = hsalsa[5];
218  state[13] = hsalsa[6];
219  state[14] = hsalsa[7];
220 
221  hsalsa.fill(0);
222 
223  state[8] = 0;
224  state[9] = 0;
225 
227  ++state[8];
228  state[9] += (state[8] == 0);
229  }
230  };
231 
232  template<std::size_t Rounds>
233  struct salsa20_functions<64, 256, Rounds> : public salsa20_policy<64, 256, Rounds> {
235 
236  constexpr static const std::size_t word_bits = policy_type::word_bits;
237  constexpr static const std::size_t word_bytes = word_bits / CHAR_BIT;
239 
240  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
241  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
243 
244  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
245  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
246  constexpr static const std::size_t key_bits = policy_type::key_bits;
247  typedef typename policy_type::key_type key_type;
248 
249  constexpr static const std::size_t block_bits = policy_type::block_bits;
250  constexpr static const std::size_t block_size = policy_type::block_size;
252 
253  constexpr static const std::size_t iv_bits = policy_type::iv_bits;
254  typedef typename policy_type::iv_type iv_type;
255 
256  void schedule_key(key_schedule_type &state, const key_type &key) {
257  state[0] = policy_type::sigma()[0];
258  state[5] = policy_type::sigma()[1];
259  state[10] = policy_type::sigma()[2];
260  state[15] = policy_type::sigma()[3];
261 
262 
263  for (std::uint8_t itr = 0; itr < 4; itr++) {
264  state[itr + 1] = boost::endian::native_to_little(
265  make_uint_t(key[4 * itr], key[4 * itr + 1], key[4 * itr + 2], key[4 * itr + 3]));
266  state[itr + 11] = boost::endian::native_to_little(
267  make_uint_t(key[4 * itr + 16], key[4 * itr + 1 + 16], key[4 * itr + 2 + 16],
268  key[4 * itr + 3 + 16]));
269  }
270  }
271 
272  void schedule_iv(block_type &block, key_schedule_type &schedule, const iv_type &iv) {
273  // Salsa20
274  schedule[6] = boost::endian::native_to_little(make_uint_t(iv[0], iv[1], iv[2], iv[3]));
275  schedule[7] = boost::endian::native_to_little(make_uint_t(iv[4], iv[5], iv[6], iv[7]));
276  schedule[8] = 0;
277  schedule[9] = 0;
278 
279  policy_type::salsa_core(block, schedule);
280  ++schedule[8];
281  schedule[9] += (schedule[8] == 0);
282  }
283  };
284 
285  template<std::size_t Rounds>
286  struct salsa20_functions<96, 256, Rounds> : public salsa20_policy<96, 256, Rounds> {
288 
289  constexpr static const std::size_t word_bits = policy_type::word_bits;
290  constexpr static const std::size_t word_bytes = word_bits / CHAR_BIT;
292 
293  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
294  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
296 
297  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
298  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
299  constexpr static const std::size_t key_bits = policy_type::key_bits;
300  typedef typename policy_type::key_type key_type;
301 
302  constexpr static const std::size_t block_bits = policy_type::block_bits;
303  constexpr static const std::size_t block_size = policy_type::block_size;
305 
306  constexpr static const std::size_t iv_bits = policy_type::iv_bits;
307  typedef typename policy_type::iv_type iv_type;
308 
309  void schedule_key(key_schedule_type &state, const key_type &key) {
310  state[0] = policy_type::sigma()[0];
311  state[5] = policy_type::sigma()[1];
312  state[10] = policy_type::sigma()[2];
313  state[15] = policy_type::sigma()[3];
314 
315 
316  for (std::uint8_t itr = 0; itr < 4; itr++) {
317  state[itr + 1] = boost::endian::native_to_little(
318  make_uint_t(key[4 * itr], key[4 * itr + 1], key[4 * itr + 2], key[4 * itr + 3]));
319  state[itr + 11] = boost::endian::native_to_little(
320  make_uint_t(key[4 * itr + 16], key[4 * itr + 1 + 16], key[4 * itr + 2 + 16],
321  key[4 * itr + 3 + 16]));
322  }
323  }
324 
326  // XSalsa20
327 
328  for (std::uint8_t itr = 0; itr < 4; itr++) {
329  state[itr + 6] = boost::endian::native_to_little(
330  make_uint_t(iv[4 * itr], iv[4 * itr + 1], iv[4 * itr + 2], iv[4 * itr + 3]));
331  }
332 
333  std::array<word_type, 8> hsalsa;
334  policy_type::hsalsa20(hsalsa.data(), state);
335 
336  state[1] = hsalsa[0];
337  state[2] = hsalsa[1];
338  state[3] = hsalsa[2];
339  state[4] = hsalsa[3];
340  state[6] = boost::endian::native_to_little(
341  make_uint_t(iv[word_bytes * 4], iv[word_bytes * 4 + 1], iv[word_bytes * 4 + 2],
342  iv[word_bytes * 4 + 3]));
343  state[7] = boost::endian::native_to_little(
344  make_uint_t(iv[word_bytes * 5], iv[word_bytes * 5 + 1], iv[word_bytes * 5 + 2],
345  iv[word_bytes * 5 + 3]));
346  state[11] = hsalsa[4];
347  state[12] = hsalsa[5];
348  state[13] = hsalsa[6];
349  state[14] = hsalsa[7];
350 
351  state[8] = 0;
352  state[9] = 0;
353 
355  ++state[8];
356  state[9] += (state[8] == 0);
357  }
358  };
359  } // namespace detail
360  } // namespace stream
361  } // namespace crypto3
362 } // namespace nil
363 
364 #endif // CRYPTO3_SALSA20_FUNCTIONS_HPP
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
boost::mpl::apply< AccumulatorSet, tag::stream< Mode > >::type::result_type stream(const AccumulatorSet &acc)
Definition: accumulators/stream.hpp:175
Definition: pair.hpp:31
constexpr static const std::size_t word_bits
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:41
policy_type::key_schedule_type key_schedule_type
Definition: salsa20_functions.hpp:114
policy_type::block_type block_type
Definition: salsa20_functions.hpp:123
void schedule_iv(block_type &block, key_schedule_type &schedule, const iv_type &iv)
Definition: salsa20_functions.hpp:143
policy_type::word_type word_type
Definition: salsa20_functions.hpp:110
policy_type::key_type key_type
Definition: salsa20_functions.hpp:119
void schedule_key(key_schedule_type &schedule, const key_type &key)
Definition: salsa20_functions.hpp:128
policy_type::iv_type iv_type
Definition: salsa20_functions.hpp:126
salsa20_policy< 64, 128, Rounds > policy_type
Definition: salsa20_functions.hpp:106
void schedule_iv(block_type &block, key_schedule_type &schedule, const iv_type &iv)
Definition: salsa20_functions.hpp:272
policy_type::iv_type iv_type
Definition: salsa20_functions.hpp:254
policy_type::word_type word_type
Definition: salsa20_functions.hpp:238
policy_type::key_type key_type
Definition: salsa20_functions.hpp:247
void schedule_key(key_schedule_type &state, const key_type &key)
Definition: salsa20_functions.hpp:256
policy_type::key_schedule_type key_schedule_type
Definition: salsa20_functions.hpp:242
policy_type::block_type block_type
Definition: salsa20_functions.hpp:251
salsa20_policy< 64, 256, Rounds > policy_type
Definition: salsa20_functions.hpp:234
salsa20_policy< 96, 128, Rounds > policy_type
Definition: salsa20_functions.hpp:158
policy_type::word_type word_type
Definition: salsa20_functions.hpp:162
void schedule_iv(block_type &block, key_schedule_type &state, const iv_type &iv)
Definition: salsa20_functions.hpp:195
void schedule_key(key_schedule_type &schedule, const key_type &key)
Definition: salsa20_functions.hpp:180
policy_type::key_type key_type
Definition: salsa20_functions.hpp:171
policy_type::iv_type iv_type
Definition: salsa20_functions.hpp:178
policy_type::block_type block_type
Definition: salsa20_functions.hpp:175
policy_type::key_schedule_type key_schedule_type
Definition: salsa20_functions.hpp:166
policy_type::word_type word_type
Definition: salsa20_functions.hpp:291
void schedule_iv(block_type &block, key_schedule_type &state, const iv_type &iv)
Definition: salsa20_functions.hpp:325
salsa20_policy< 96, 256, Rounds > policy_type
Definition: salsa20_functions.hpp:287
policy_type::key_type key_type
Definition: salsa20_functions.hpp:300
policy_type::key_schedule_type key_schedule_type
Definition: salsa20_functions.hpp:295
policy_type::iv_type iv_type
Definition: salsa20_functions.hpp:307
policy_type::block_type block_type
Definition: salsa20_functions.hpp:304
void schedule_key(key_schedule_type &state, const key_type &key)
Definition: salsa20_functions.hpp:309
Definition: salsa20_functions.hpp:43
constexpr static const std::size_t rounds
Definition: salsa20_functions.hpp:50
constexpr static const std::size_t key_schedule_size
Definition: salsa20_functions.hpp:53
policy_type::block_type block_type
Definition: salsa20_functions.hpp:63
policy_type::iv_type iv_type
Definition: salsa20_functions.hpp:66
constexpr static const std::size_t block_bits
Definition: salsa20_functions.hpp:61
constexpr static const std::size_t word_bytes
Definition: salsa20_functions.hpp:47
constexpr static const std::size_t word_bits
Definition: salsa20_functions.hpp:46
policy_type::key_type key_type
Definition: salsa20_functions.hpp:59
constexpr static const std::size_t key_bits
Definition: salsa20_functions.hpp:58
policy_type::key_schedule_type key_schedule_type
Definition: salsa20_functions.hpp:54
constexpr static const std::size_t key_schedule_bits
Definition: salsa20_functions.hpp:52
constexpr static const std::size_t min_key_bits
Definition: salsa20_functions.hpp:56
void schedule_iv(block_type &block, key_schedule_type &schedule, const iv_type &iv)
Definition: salsa20_functions.hpp:68
constexpr static const std::size_t max_key_bits
Definition: salsa20_functions.hpp:57
salsa20_policy< IVSize, KeyBits, Rounds > policy_type
Definition: salsa20_functions.hpp:44
constexpr static const std::size_t block_size
Definition: salsa20_functions.hpp:62
policy_type::word_type word_type
Definition: salsa20_functions.hpp:48
constexpr static const std::size_t iv_bits
Definition: salsa20_functions.hpp:65
Definition: salsa20_policy.hpp:47
constexpr static const std::size_t key_schedule_bits
Definition: salsa20_policy.hpp:72
std::array< byte_type, key_size > key_type
Definition: salsa20_policy.hpp:69
static void hsalsa20(word_type output[8], const key_schedule_type input)
Definition: salsa20_policy.hpp:87
std::array< word_type, key_schedule_size > key_schedule_type
Definition: salsa20_policy.hpp:73
static void salsa_core(block_type &block, const key_schedule_type &input)
Definition: salsa20_policy.hpp:115
constexpr static const std::size_t rounds
Definition: salsa20_policy.hpp:53
constexpr static const std::size_t min_key_bits
Definition: salsa20_policy.hpp:63
policy_type::word_type word_type
Definition: salsa20_policy.hpp:51
constexpr static const std::size_t iv_bits
Definition: salsa20_policy.hpp:83
constexpr static const std::size_t key_bits
Definition: salsa20_policy.hpp:65
std::array< byte_type, block_size > block_type
Definition: salsa20_policy.hpp:61
constexpr static const std::size_t key_schedule_size
Definition: salsa20_policy.hpp:71
std::array< byte_type, iv_size > iv_type
Definition: salsa20_policy.hpp:85
constexpr static const std::size_t block_bits
Definition: salsa20_policy.hpp:60
constexpr static const std::size_t max_key_bits
Definition: salsa20_policy.hpp:64
constexpr static const std::size_t block_size
Definition: salsa20_policy.hpp:59