rc4_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_RC4_POLICY_HPP
26 #define CRYPTO3_STREAM_RC4_POLICY_HPP
27 
28 #include <memory>
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace stream {
35  namespace detail {
36  template<std::size_t IVBits, std::size_t KeyBits, template<typename> class Allocator = std::allocator>
37  struct rc4_policy : public basic_functions<32> {
40 
41  constexpr static const std::size_t min_key_bits = CHAR_BIT;
42  constexpr static const std::size_t max_key_bits = 256 * CHAR_BIT;
43  constexpr static const std::size_t key_bits = KeyBits;
44  constexpr static const std::size_t key_size = key_bits / CHAR_BIT;
45  BOOST_STATIC_ASSERT(min_key_bits <= KeyBits && KeyBits <= max_key_bits);
46  typedef std::array<byte_type, key_size> key_type;
47 
48  constexpr static const std::size_t key_schedule_size = 256;
49  constexpr static const std::size_t key_schedule_bits = key_schedule_size * CHAR_BIT;
50  typedef std::array<byte_type, key_schedule_size> key_schedule_type;
51 
52  constexpr static const std::size_t state_size = 256;
53  constexpr static const std::size_t state_bits = state_size * CHAR_BIT;
54  struct state_type {
55  std::size_t x = 0, y = 0;
56  std::array<byte_type, state_size> data = {0};
57 
58  BOOST_FORCEINLINE void fill(const byte_type &v) {
59  data.fill(v);
60  }
61 
62  BOOST_FORCEINLINE byte_type &operator[](std::size_t i) {
63  return data[i];
64  }
65 
66  BOOST_FORCEINLINE const byte_type &operator[](std::size_t i) const {
67  return data[i];
68  }
69 
70  BOOST_FORCEINLINE std::size_t size() const {
71  return data.size();
72  }
73  };
74 
75  constexpr static const std::size_t iv_bits = IVBits;
76  constexpr static const std::size_t iv_size = IVBits / CHAR_BIT;
77  typedef std::array<byte_type, iv_size> iv_type;
78  };
79  } // namespace detail
80  } // namespace stream
81  } // namespace crypto3
82 } // namespace nil
83 
84 #endif // CRYPTO3_RC4_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
BOOST_FORCEINLINE std::size_t size() const
Definition: rc4_policy.hpp:70
BOOST_FORCEINLINE void fill(const byte_type &v)
Definition: rc4_policy.hpp:58
std::size_t x
Definition: rc4_policy.hpp:55
std::array< byte_type, state_size > data
Definition: rc4_policy.hpp:56
BOOST_FORCEINLINE byte_type & operator[](std::size_t i)
Definition: rc4_policy.hpp:62
BOOST_FORCEINLINE const byte_type & operator[](std::size_t i) const
Definition: rc4_policy.hpp:66
std::size_t y
Definition: rc4_policy.hpp:55
Definition: rc4_policy.hpp:37
constexpr static const std::size_t key_size
Definition: rc4_policy.hpp:44
std::array< byte_type, iv_size > iv_type
Definition: rc4_policy.hpp:77
constexpr static const std::size_t key_bits
Definition: rc4_policy.hpp:43
std::array< byte_type, key_schedule_size > key_schedule_type
Definition: rc4_policy.hpp:50
basic_functions< 32 >::byte_type byte_type
Definition: rc4_policy.hpp:38
BOOST_STATIC_ASSERT(min_key_bits<=KeyBits &&KeyBits<=max_key_bits)
constexpr static const std::size_t iv_bits
Definition: rc4_policy.hpp:75
constexpr static const std::size_t state_size
Definition: rc4_policy.hpp:52
constexpr static const std::size_t iv_size
Definition: rc4_policy.hpp:76
constexpr static const std::size_t key_schedule_bits
Definition: rc4_policy.hpp:49
constexpr static const std::size_t state_bits
Definition: rc4_policy.hpp:53
std::array< byte_type, key_size > key_type
Definition: rc4_policy.hpp:46
constexpr static const std::size_t max_key_bits
Definition: rc4_policy.hpp:42
constexpr static const std::size_t key_schedule_size
Definition: rc4_policy.hpp:48
constexpr static const std::size_t min_key_bits
Definition: rc4_policy.hpp:41
basic_functions< 32 >::word_type word_type
Definition: rc4_policy.hpp:39