salsa20.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_HPP
26 #define CRYPTO3_STREAM_SALSA20_HPP
27 
29 
30 namespace nil {
31  namespace crypto3 {
32  namespace stream {
33  template<std::size_t IVBits, std::size_t KeyBits, std::size_t Rounds = 20>
36 
37  public:
38  constexpr static const std::size_t rounds = policy_type::rounds;
39 
41 
42  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
43  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
45 
46  constexpr static const std::size_t iv_bits = policy_type::iv_bits;
47  typedef typename policy_type::iv_type iv_type;
48 
49  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
50  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
51  constexpr static const std::size_t key_bits = policy_type::key_bits;
52  typedef typename policy_type::key_type key_type;
53 
54  template<typename InputRange, typename OutputRange>
55  void process(OutputRange &out, InputRange &in, key_schedule_type &schedule, block_type &block) {
56  xor_buf(out, in, block, block.size());
57  }
58  };
66  template<std::size_t IVBits, std::size_t KeyBits, std::size_t Rounds = 20>
67  class salsa20 {
69 
70  public:
71  constexpr static const std::size_t rounds = policy_type::rounds;
72 
74 
75  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
76  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
78 
79  constexpr static const std::size_t iv_bits = policy_type::iv_bits;
80  typedef typename policy_type::iv_type iv_type;
81 
82  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
83  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
84  constexpr static const std::size_t key_bits = policy_type::key_bits;
85  typedef typename policy_type::key_type key_type;
86 
87  salsa20(key_schedule_type &schedule, const key_type &key, const iv_type &iv = iv_type()) {
88  policy_type::schedule_key(schedule, key);
89  policy_type::schedule_iv(schedule, iv);
90  }
91 
92  template<typename InputRange, typename OutputRange>
93  void process(OutputRange &out, InputRange &in, key_schedule_type &schedule, block_type &block) {
94  xor_buf(out, in, block, block.size());
95  policy_type::salsa_core(block, schedule);
96 
97  ++schedule[8];
98  schedule[9] += (schedule[8] == 0);
99  }
100 
101  void seek(key_schedule_type &schedule, block_type &block, std::size_t offset) {
102  // Find the block offset
103  const uint64_t counter = offset / 64;
104  uint8_t counter8[8];
105  boost::endian::store_little_u64(counter8, counter);
106 
107  schedule[8] = boost::endian::store_little_u32(counter8, 0);
108  schedule[9] += boost::endian::store_little_u32(counter8, 1);
109 
110  salsa_core(block, schedule);
111 
112  ++schedule[8];
113  schedule[9] += (schedule[8] == 0);
114  }
115  };
116  } // namespace stream
117  } // namespace crypto3
118 } // namespace nil
119 
120 #endif
Definition: salsa20.hpp:34
constexpr static const std::size_t rounds
Definition: salsa20.hpp:38
constexpr static const std::size_t key_schedule_bits
Definition: salsa20.hpp:42
policy_type::key_type key_type
Definition: salsa20.hpp:52
policy_type::block_type block_type
Definition: salsa20.hpp:40
void process(OutputRange &out, InputRange &in, key_schedule_type &schedule, block_type &block)
Definition: salsa20.hpp:55
constexpr static const std::size_t iv_bits
Definition: salsa20.hpp:46
constexpr static const std::size_t max_key_bits
Definition: salsa20.hpp:50
constexpr static const std::size_t key_bits
Definition: salsa20.hpp:51
constexpr static const std::size_t min_key_bits
Definition: salsa20.hpp:49
policy_type::key_schedule_type key_schedule_type
Definition: salsa20.hpp:44
constexpr static const std::size_t key_schedule_size
Definition: salsa20.hpp:43
policy_type::iv_type iv_type
Definition: salsa20.hpp:47
Definition: salsa20.hpp:67
void seek(key_schedule_type &schedule, block_type &block, std::size_t offset)
Definition: salsa20.hpp:101
constexpr static const std::size_t key_bits
Definition: salsa20.hpp:84
constexpr static const std::size_t key_schedule_bits
Definition: salsa20.hpp:75
constexpr static const std::size_t max_key_bits
Definition: salsa20.hpp:83
policy_type::iv_type iv_type
Definition: salsa20.hpp:80
constexpr static const std::size_t rounds
Definition: salsa20.hpp:71
policy_type::key_type key_type
Definition: salsa20.hpp:85
constexpr static const std::size_t iv_bits
Definition: salsa20.hpp:79
constexpr static const std::size_t min_key_bits
Definition: salsa20.hpp:82
void process(OutputRange &out, InputRange &in, key_schedule_type &schedule, block_type &block)
Definition: salsa20.hpp:93
salsa20(key_schedule_type &schedule, const key_type &key, const iv_type &iv=iv_type())
Definition: salsa20.hpp:87
policy_type::block_type block_type
Definition: salsa20.hpp:73
policy_type::key_schedule_type key_schedule_type
Definition: salsa20.hpp:77
constexpr static const std::size_t key_schedule_size
Definition: salsa20.hpp:76
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
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: memory_operations.hpp:245
Definition: pair.hpp:31
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
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
constexpr static const std::size_t iv_bits
Definition: salsa20_functions.hpp:65
static void salsa_core(block_type &block, const key_schedule_type &input)
Definition: salsa20_policy.hpp:115