cipher_key.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 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_BLOCK_CIPHER_KEY_HPP
26 #define CRYPTO3_BLOCK_CIPHER_KEY_HPP
27 
28 #include <boost/accumulators/framework/accumulator_set.hpp>
29 #include <boost/accumulators/framework/features.hpp>
30 
32 
33 namespace nil {
34  namespace crypto3 {
35  namespace block {
36  template<typename BlockCipher>
37  struct cipher_key {
39 
40  typedef typename cipher_type::endian_type endian_type;
41 
42  constexpr static const std::size_t key_bits = cipher_type::key_bits;
43  constexpr static const std::size_t key_value_bits =
44  sizeof(typename cipher_type::key_type::value_type) * CHAR_BIT;
45  typedef typename cipher_type::key_type key_type;
46 
47  template<typename SinglePassRange>
48  explicit cipher_key(const SinglePassRange &r) {
49  using namespace nil::crypto3::detail;
50 
51  BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
52 
53  typedef typename std::iterator_traits<typename SinglePassRange::iterator>::value_type value_type;
54 
55  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
56 
57  constexpr static const std::size_t value_bits =
58  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed;
59 
60  pack_to<endian_type, value_bits, key_value_bits>(r.begin(), r.end(), key.begin());
61  }
62 
63  template<typename InputIterator>
64  explicit cipher_key(InputIterator first, InputIterator last) {
65  using namespace nil::crypto3::detail;
66 
67  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
68 
69  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
70 
71  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
72 
73  constexpr static const std::size_t value_bits =
74  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed;
75 
76  pack_to<endian_type, value_bits, key_value_bits>(first, last, key.begin());
77  }
78 
80  };
81  } // namespace block
82  } // namespace crypto3
83 } // namespace nil
84 
85 #endif // CRYPTO3_BLOCK_CIPHER_KEY_HPP
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:33
boost::accumulators::accumulator_set< mac::digest< MessageAuthenticationCode::input_block_bits >, boost::accumulators::features< accumulators::tag::mac< MessageAuthenticationCode > >> BlockCipher
Definition: cbc_mac_state.hpp:40
Definition: pair.hpp:31
Definition: cipher_key.hpp:37
cipher_type::key_type key_type
Definition: cipher_key.hpp:45
constexpr static const std::size_t key_bits
Definition: cipher_key.hpp:42
cipher_key(InputIterator first, InputIterator last)
Definition: cipher_key.hpp:64
key_type key
Definition: cipher_key.hpp:79
BlockCipher cipher_type
Definition: cipher_key.hpp:38
cipher_type::endian_type endian_type
Definition: cipher_key.hpp:40
constexpr static const std::size_t key_value_bits
Definition: cipher_key.hpp:43
cipher_key(const SinglePassRange &r)
Definition: cipher_key.hpp:48