pbkdf_value.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_PBKDF_VALUE_HPP
26 #define CRYPTO3_PBKDF_VALUE_HPP
27 
28 #include <type_traits>
29 #include <iterator>
30 
31 #include <boost/assert.hpp>
32 #include <boost/concept_check.hpp>
33 
34 #include <boost/range/concepts.hpp>
35 
36 namespace nil {
37  namespace crypto3 {
38  namespace mac {
39  namespace detail {
40  template<typename MacAccumulator>
41  struct ref_cipher_impl {
42  typedef MacAccumulator accumulator_set_type;
43  typedef
44  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
45 
46  typedef typename MacAccumulator::mode_type mode_type;
47  typedef typename mode_type::encoder_type cipher_type;
48 
50  }
51 
53  };
54 
55  template<typename MacAccumulator>
56  struct value_cipher_impl {
57  typedef MacAccumulator accumulator_set_type;
58  typedef
59  typename boost::mpl::front<typename accumulator_set_type::features_type>::type accumulator_type;
60 
61  typedef typename MacAccumulator::mode_type mode_type;
62  typedef typename mode_type::encoder_type cipher_type;
63 
65  }
66 
68  };
69 
70  template<typename MacStateImpl>
71  struct range_cipher_impl : public MacStateImpl {
72  typedef MacStateImpl cipher_state_impl_type;
73 
74  typedef typename cipher_state_impl_type::accumulator_type accumulator_type;
75  typedef typename cipher_state_impl_type::accumulator_set_type accumulator_set_type;
76 
77  typedef typename cipher_state_impl_type::mode_type mode_type;
78  typedef typename cipher_state_impl_type::cipher_type cipher_type;
79 
80  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
82 
83  template<typename SinglePassRange>
84  range_cipher_impl(const SinglePassRange &range, const accumulator_set_type &ise) :
85  MacStateImpl(ise) {
86  BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
87 
88  typedef
89  typename std::iterator_traits<typename SinglePassRange::iterator>::value_type value_type;
90  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
91  typedef typename cipher_type::template stream_processor<
93  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
94  stream_processor;
95 
96  stream_processor(this->accumulator_set)(range.begin(), range.end());
97  }
98 
99  template<typename InputIterator>
100  range_cipher_impl(InputIterator first, InputIterator last, const accumulator_set_type &ise) :
101  MacStateImpl(ise) {
102  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
103 
104  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
105  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
106  typedef typename cipher_type::template stream_processor<
108  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
109  stream_processor;
110 
111  stream_processor(this->accumulator_set)(first, last);
112  }
113 
114  template<typename T, std::size_t Size>
115  inline operator std::array<T, Size>() const {
116  result_type result =
117  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
118  std::array<T, Size> out;
119  std::copy(result.begin(), result.end(), out.end());
120  return out;
121  }
122 
123  template<typename T, std::size_t Size>
124  inline operator boost::array<T, Size>() const {
125  result_type result =
126  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
127  boost::array<T, Size> out;
128  std::copy(result.begin(), result.end(), out.end());
129  return out;
130  }
131 
132  template<typename OutputRange>
133  operator OutputRange() const {
134  result_type result =
135  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
136  return OutputRange(result.cbegin(), result.cend());
137  }
138 
139  operator result_type() const {
140  return boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
141  }
142 
143  operator accumulator_set_type() const {
144  return this->accumulator_set;
145  }
146 
147 #ifdef CRYPTO3_ASCII_STRING_CODEC_OUTPUT
148 
149  template<typename Char, typename CharTraits, typename Alloc>
150  operator std::basic_string<Char, CharTraits, Alloc>() const {
151  return std::to_string(
152  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set));
153  }
154 
155 #endif
156  };
157 
158  template<typename MacStateImpl, typename OutputIterator>
159  struct itr_cipher_impl : public MacStateImpl {
160  private:
161  mutable OutputIterator out;
162 
163  public:
164  typedef MacStateImpl cipher_state_impl_type;
165 
166  typedef typename cipher_state_impl_type::accumulator_type accumulator_type;
167  typedef typename cipher_state_impl_type::accumulator_set_type accumulator_set_type;
168 
169  typedef typename cipher_state_impl_type::mode_type mode_type;
170  typedef typename cipher_state_impl_type::cipher_type cipher_type;
171 
172  typedef typename boost::mpl::apply<accumulator_set_type, accumulator_type>::type::result_type
174 
175  template<typename SinglePassRange>
176  itr_cipher_impl(const SinglePassRange &range, OutputIterator out, const accumulator_set_type &ise) :
177  MacStateImpl(ise), out(std::move(out)) {
178  BOOST_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const SinglePassRange>));
179 
180  typedef
181  typename std::iterator_traits<typename SinglePassRange::iterator>::value_type value_type;
182  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
183  typedef typename cipher_type::template stream_processor<
185  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
186  stream_processor;
187 
188  stream_processor(this->accumulator_set)(range.begin(), range.end());
189  }
190 
191  template<typename InputIterator>
192  itr_cipher_impl(InputIterator first, InputIterator last, OutputIterator out,
193  const accumulator_set_type &ise) :
194  MacStateImpl(ise),
195  out(std::move(out)) {
196  BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<InputIterator>));
197 
198  typedef typename std::iterator_traits<InputIterator>::value_type value_type;
199  BOOST_STATIC_ASSERT(std::numeric_limits<value_type>::is_specialized);
200  typedef typename cipher_type::template stream_processor<
202  std::numeric_limits<value_type>::digits + std::numeric_limits<value_type>::is_signed>::type
203  stream_processor;
204 
205  stream_processor(this->accumulator_set)(first, last);
206  }
207 
208  operator OutputIterator() const {
209  result_type result =
210  boost::accumulators::extract_result<accumulator_type>(this->accumulator_set);
211 
212  return std::move(result.cbegin(), result.cend(), out);
213  }
214  };
215  } // namespace detail
216  } // namespace mac
217  } // namespace crypto3
218 } // namespace nil
219 
220 #endif // CRYPTO3_CODEC_POSTPROCESSOR_HPP
boost::accumulators::accumulator_set< digest< ProcessingMode::block_bits >, boost::accumulators::features< accumulators::tag::block< ProcessingMode > >, std::size_t > accumulator_set
Accumulator set with pre-defined block cipher accumulator params.
Definition: block/include/nil/crypto3/block/cipher_state.hpp:51
boost::mpl::apply< AccumulatorSet, tag::mac< ProcessingPolicy > >::type::result_type mac(const AccumulatorSet &acc)
Definition: accumulators/mac.hpp:99
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: pair.hpp:31
Definition: hash_state.hpp:43
cipher_state_impl_type::cipher_type cipher_type
Definition: pbkdf_value.hpp:170
cipher_state_impl_type::accumulator_type accumulator_type
Definition: pbkdf_value.hpp:166
itr_cipher_impl(InputIterator first, InputIterator last, OutputIterator out, const accumulator_set_type &ise)
Definition: pbkdf_value.hpp:192
cipher_state_impl_type::accumulator_set_type accumulator_set_type
Definition: pbkdf_value.hpp:167
itr_cipher_impl(const SinglePassRange &range, OutputIterator out, const accumulator_set_type &ise)
Definition: pbkdf_value.hpp:176
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: pbkdf_value.hpp:173
MacStateImpl cipher_state_impl_type
Definition: pbkdf_value.hpp:164
cipher_state_impl_type::mode_type mode_type
Definition: pbkdf_value.hpp:169
boost::mpl::apply< accumulator_set_type, accumulator_type >::type::result_type result_type
Definition: pbkdf_value.hpp:81
cipher_state_impl_type::cipher_type cipher_type
Definition: pbkdf_value.hpp:78
cipher_state_impl_type::accumulator_type accumulator_type
Definition: pbkdf_value.hpp:74
cipher_state_impl_type::mode_type mode_type
Definition: pbkdf_value.hpp:77
range_cipher_impl(const SinglePassRange &range, const accumulator_set_type &ise)
Definition: pbkdf_value.hpp:84
range_cipher_impl(InputIterator first, InputIterator last, const accumulator_set_type &ise)
Definition: pbkdf_value.hpp:100
MacStateImpl cipher_state_impl_type
Definition: pbkdf_value.hpp:72
cipher_state_impl_type::accumulator_set_type accumulator_set_type
Definition: pbkdf_value.hpp:75
ref_cipher_impl(const accumulator_set_type &acc)
Definition: pbkdf_value.hpp:49
accumulator_set_type & accumulator_set
Definition: passhash_value.hpp:52
MacAccumulator accumulator_set_type
Definition: pbkdf_value.hpp:42
MacAccumulator::mode_type mode_type
Definition: pbkdf_value.hpp:46
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: pbkdf_value.hpp:44
mode_type::encoder_type cipher_type
Definition: pbkdf_value.hpp:47
boost::mpl::front< typename accumulator_set_type::features_type >::type accumulator_type
Definition: pbkdf_value.hpp:59
MacAccumulator accumulator_set_type
Definition: pbkdf_value.hpp:57
MacAccumulator::mode_type mode_type
Definition: pbkdf_value.hpp:61
accumulator_set_type accumulator_set
Definition: passhash_value.hpp:67
value_cipher_impl(const accumulator_set_type &acc)
Definition: pbkdf_value.hpp:64
mode_type::encoder_type cipher_type
Definition: pbkdf_value.hpp:62