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