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