chacha_policy.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_CHACHA_POLICY_HPP
26 #define CRYPTO3_STREAM_CHACHA_POLICY_HPP
27 
28 #include <boost/endian/conversion.hpp>
29 
30 #include <boost/container/small_vector.hpp>
31 
32 #include <nil/crypto3/detail/inline_variable.hpp>
33 
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace stream {
39  namespace detail {
40  template<std::size_t Rounds, std::size_t IVBits, std::size_t KeyBits>
41  struct chacha_policy : public basic_functions<32> {
43 
44  constexpr static const std::size_t word_bits = basic_functions<32>::word_bits;
46 
47  constexpr static const std::size_t rounds = Rounds;
48  BOOST_STATIC_ASSERT(Rounds % 2 == 0);
49 
50  constexpr static const std::size_t value_bits = CHAR_BIT;
52 
53  constexpr static const std::size_t block_size = 64;
54  constexpr static const std::size_t block_bits = block_size * value_bits;
55  typedef std::array<byte_type, block_size> block_type;
56 
57  constexpr static const std::size_t min_key_bits = 16 * CHAR_BIT;
58  constexpr static const std::size_t max_key_bits = 32 * CHAR_BIT;
59  constexpr static const std::size_t key_bits = KeyBits;
60  constexpr static const std::size_t key_size = key_bits / CHAR_BIT;
63  typedef std::array<byte_type, key_size> key_type;
64 
65  constexpr static const std::size_t key_schedule_size = 16;
66  constexpr static const std::size_t key_schedule_bits = key_schedule_size * word_bits;
67  typedef std::array<word_type, key_schedule_size> key_schedule_type;
68 
69  constexpr static const std::size_t round_constants_size = 4;
70  typedef std::array<word_type, round_constants_size> round_constants_type;
71 
73  ({0x61707865, 0x3120646e, 0x79622d36, 0x6b206574}));
75  ({0x61707865, 0x3320646e, 0x79622d32, 0x6b206574}));
76 
77  constexpr static const std::size_t iv_bits = IVBits;
78  constexpr static const std::size_t iv_size = IVBits / CHAR_BIT;
79  typedef std::array<byte_type, iv_size> iv_type;
80  };
81  } // namespace detail
82  } // namespace stream
83  } // namespace crypto3
84 } // namespace nil
85 
86 #endif // CRYPTO3_CHACHA_POLICY_HPP
boost::mpl::apply< AccumulatorSet, tag::stream< Mode > >::type::result_type stream(const AccumulatorSet &acc)
Definition: accumulators/stream.hpp:175
Definition: pair.hpp:31
Definition: stream/include/nil/crypto3/stream/detail/basic_functions.hpp:38
Definition: chacha_policy.hpp:41
CRYPTO3_INLINE_VARIABLE(round_constants_type, sigma,({0x61707865, 0x3320646e, 0x79622d32, 0x6b206574}))
CRYPTO3_INLINE_VARIABLE(round_constants_type, tau,({0x61707865, 0x3120646e, 0x79622d36, 0x6b206574}))
basic_functions< 32 >::byte_type byte_type
Definition: chacha_policy.hpp:42
std::array< word_type, round_constants_size > round_constants_type
Definition: chacha_policy.hpp:70
constexpr static const std::size_t block_size
Definition: chacha_policy.hpp:53
constexpr static const std::size_t key_schedule_bits
Definition: chacha_policy.hpp:66
constexpr static const std::size_t rounds
Definition: chacha_policy.hpp:47
std::array< byte_type, key_size > key_type
Definition: chacha_policy.hpp:63
constexpr static const std::size_t word_bits
Definition: chacha_policy.hpp:44
byte_type value_type
Definition: chacha_policy.hpp:51
BOOST_STATIC_ASSERT(min_key_bits<=KeyBits<=max_key_bits)
constexpr static const std::size_t iv_bits
Definition: chacha_policy.hpp:77
constexpr static const std::size_t iv_size
Definition: chacha_policy.hpp:78
std::array< byte_type, iv_size > iv_type
Definition: chacha_policy.hpp:79
constexpr static const std::size_t min_key_bits
Definition: chacha_policy.hpp:57
constexpr static const std::size_t round_constants_size
Definition: chacha_policy.hpp:69
constexpr static const std::size_t max_key_bits
Definition: chacha_policy.hpp:58
basic_functions< 32 >::word_type word_type
Definition: chacha_policy.hpp:45
constexpr static const std::size_t block_bits
Definition: chacha_policy.hpp:54
constexpr static const std::size_t key_size
Definition: chacha_policy.hpp:60
constexpr static const std::size_t key_bits
Definition: chacha_policy.hpp:59
std::array< word_type, key_schedule_size > key_schedule_type
Definition: chacha_policy.hpp:67
std::array< byte_type, block_size > block_type
Definition: chacha_policy.hpp:55
constexpr static const std::size_t key_schedule_size
Definition: chacha_policy.hpp:65
constexpr static const std::size_t value_bits
Definition: chacha_policy.hpp:50