rc4_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_RC4_FUNCTIONS_HPP
26 #define CRYPTO3_STREAM_RC4_FUNCTIONS_HPP
27 
29 
30 namespace nil {
31  namespace crypto3 {
32  namespace stream {
33  namespace detail {
34  template<std::size_t IVSize, std::size_t KeyBits>
35  struct rc4_functions : public rc4_policy<IVSize, KeyBits> {
37 
40 
41  constexpr static const std::size_t key_schedule_size = policy_type::key_schedule_size;
42  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
44 
45  constexpr static const std::size_t state_size = policy_type::state_size;
46  constexpr static const std::size_t state_bits = policy_type::state_bits;
47  typedef typename policy_type::state_type state_type;
48 
49  static void generate(key_schedule_type &schedule, state_type &state) {
50  std::uint8_t SX, SY;
51  for (std::size_t i = 0; i != state_size; i += 4) {
52  SX = schedule[state.x + 1];
53  state.y = (state.y + SX) % 256;
54  SY = schedule[state.y];
55  schedule[state.x + 1] = SY;
56  schedule[state.y] = SX;
57  state.data[i] = schedule[(SX + SY) % 256];
58 
59  SX = schedule[state.x + 2];
60  state.y = (state.y + SX) % 256;
61  SY = schedule[state.y];
62  schedule[state.x + 2] = SY;
63  schedule[state.y] = SX;
64  state.data[i + 1] = schedule[(SX + SY) % 256];
65 
66  SX = schedule[state.x + 3];
67  state.y = (state.y + SX) % 256;
68  SY = schedule[state.y];
69  schedule[state.x + 3] = SY;
70  schedule[state.y] = SX;
71  state.data[i + 2] = schedule[(SX + SY) % 256];
72 
73  state.x = (state.x + 4) % 256;
74  SX = schedule[state.x];
75  state.y = (state.y + SX) % 256;
76  SY = schedule[state.y];
77  schedule[state.x] = SY;
78  schedule[state.y] = SX;
79  state.data[i + 3] = schedule[(SX + SY) % 256];
80  }
81  }
82  };
83  } // namespace detail
84  } // namespace stream
85  } // namespace crypto3
86 } // namespace nil
87 
88 #endif // CRYPTO3_RC4_FUNCTIONS_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: rc4_functions.hpp:35
policy_type::word_type word_type
Definition: rc4_functions.hpp:39
constexpr static const std::size_t key_schedule_size
Definition: rc4_functions.hpp:41
rc4_policy< IVSize, KeyBits > policy_type
Definition: rc4_functions.hpp:36
policy_type::byte_type byte_type
Definition: rc4_functions.hpp:38
policy_type::state_type state_type
Definition: rc4_functions.hpp:47
constexpr static const std::size_t key_schedule_bits
Definition: rc4_functions.hpp:42
constexpr static const std::size_t state_size
Definition: rc4_functions.hpp:45
constexpr static const std::size_t state_bits
Definition: rc4_functions.hpp:46
policy_type::key_schedule_type key_schedule_type
Definition: rc4_functions.hpp:43
static void generate(key_schedule_type &schedule, state_type &state)
Definition: rc4_functions.hpp:49
Definition: rc4_policy.hpp:37
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
constexpr static const std::size_t state_size
Definition: rc4_policy.hpp:52
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
constexpr static const std::size_t key_schedule_size
Definition: rc4_policy.hpp:48
basic_functions< 32 >::word_type word_type
Definition: rc4_policy.hpp:39