block/include/nil/crypto3/block/detail/block_stream_processor.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
4 //
5 // MIT License
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 //---------------------------------------------------------------------------//
25 
26 #ifndef CRYPTO3_BLOCK_BLOCK_STATE_PREPROCESSOR_HPP
27 #define CRYPTO3_BLOCK_BLOCK_STATE_PREPROCESSOR_HPP
28 
29 #include <array>
30 #include <iterator>
31 #include <climits>
32 
33 #include <nil/crypto3/detail/pack.hpp>
34 #include <nil/crypto3/detail/digest.hpp>
35 
38 
39 #include <boost/integer.hpp>
40 #include <boost/cstdint.hpp>
41 #include <boost/static_assert.hpp>
42 #include <boost/utility/enable_if.hpp>
43 
44 #include <boost/range/algorithm/copy.hpp>
45 
46 namespace nil {
47  namespace crypto3 {
48  namespace block {
49  template<typename Mode, typename StateAccumulator, typename Params>
51  private:
52  typedef Mode mode_type;
53  typedef StateAccumulator accumulator_type;
54  typedef Params params_type;
55 
56  typedef typename mode_type::block_type block_type;
57  constexpr static const std::size_t block_bits = mode_type::block_bits;
58 
59  constexpr static const std::size_t word_bits = mode_type::word_bits;
60 
61  constexpr static const std::size_t actual_bits = sizeof(typename block_type::value_type) * CHAR_BIT;
62 
63  public:
64  typedef typename mode_type::endian_type endian_type;
65 
66  constexpr static const std::size_t value_bits = params_type::value_bits;
67  typedef typename boost::uint_t<value_bits>::least value_type;
68  BOOST_STATIC_ASSERT(block_bits % value_bits == 0);
69  constexpr static const std::size_t block_values = block_bits / value_bits;
70  typedef std::array<value_type, block_values> cache_type;
71 
72  private:
73  constexpr static const std::size_t length_bits = params_type::length_bits;
74  // FIXME: do something more intelligent than capping at sizeof(boost::uintmax_t) * CHAR_BIT
75  constexpr static const std::size_t length_type_bits = length_bits < word_bits ?
76  word_bits :
77  length_bits >
78  sizeof(boost::uintmax_t) * CHAR_BIT ?
79  sizeof(boost::uintmax_t) * CHAR_BIT :
80  length_bits;
81  typedef typename boost::uint_t<length_type_bits>::least length_type;
82 
83  BOOST_STATIC_ASSERT(!length_bits || length_bits % word_bits == 0);
84  BOOST_STATIC_ASSERT(block_bits % value_bits == 0);
85 
86  BOOST_STATIC_ASSERT(!length_bits || value_bits <= length_bits);
87 
88  inline void process_block(std::size_t block_seen = block_bits) {
89  using namespace nil::crypto3::detail;
90  // Convert the input into words
91  block_type block;
92  pack_to<endian_type, value_bits, actual_bits>(cache.begin(), cache.end(), block.begin());
93  // Process the block
94  acc(block, accumulators::bits = block_seen);
95  }
96 
97  public:
98  inline void update_one(value_type value) {
99  cache[cache_seen] = value;
100  ++cache_seen;
101  if (cache_seen == block_values) {
102  // Process the completed block
103  process_block();
104  cache_seen = 0;
105  }
106  }
107 
108  template<typename InputIterator>
109  inline void update_n(InputIterator p, size_t n) {
110  for (; n; --n) {
111  update_one(*p++);
112  }
113  }
114 
115  template<typename InputIterator>
116  inline void update_n(InputIterator first, InputIterator last) {
117  std::size_t n = std::distance(first, last);
118  update_n(first, n);
119  }
120 
121  public:
122  block_stream_processor(StateAccumulator &s) : acc(s), cache(cache_type()), cache_seen(0) {
123  }
124 
126  if (cache_seen != 0) {
127  process_block(cache_seen * value_bits);
128  cache_seen = 0;
129  }
130  }
131 
132  template<typename InputIterator>
133  inline void operator()(InputIterator b, InputIterator e, std::random_access_iterator_tag) {
134  return update_n(b, e);
135  }
136 
137  template<typename InputIterator, typename Category>
138  inline void operator()(InputIterator first, InputIterator last, Category) {
139 
140  while (first != last) {
141  update_one(*first++);
142  }
143  }
144 
145  template<typename ValueType>
146  inline void operator()(const ValueType &value) {
147  return update_one(value);
148  }
149 
150  template<typename InputIterator>
151  inline void operator()(InputIterator b, InputIterator e) {
152  typedef typename std::iterator_traits<InputIterator>::iterator_category cat;
153  return operator()(b, e, cat());
154  }
155 
156  template<typename ValueType>
157  inline void operator()(const std::initializer_list<ValueType> &il) {
158  return operator()(il.begin(), il.end());
159  }
160 
161  void reset() {
162  cache_seen = 0;
163  }
164 
165  StateAccumulator &acc;
166 
167  length_type cache_seen;
169  };
170  } // namespace block
171  } // namespace crypto3
172 } // namespace nil
173 
174 #endif // CRYPTO3_BLOCK_BLOCK_STATE_PREPROCESSOR_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
typename std::iterator_traits< Iterator >::value_type ValueType
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:50
Definition: pair.hpp:31
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:50
void update_one(value_type value)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:98
void operator()(const std::initializer_list< ValueType > &il)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:157
BOOST_STATIC_ASSERT(block_bits % value_bits==0)
void operator()(InputIterator b, InputIterator e, std::random_access_iterator_tag)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:133
void operator()(const ValueType &value)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:146
void update_n(InputIterator p, size_t n)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:109
StateAccumulator & acc
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:165
constexpr static const std::size_t value_bits
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:66
block_stream_processor(StateAccumulator &s)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:122
void update_n(InputIterator first, InputIterator last)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:116
void reset()
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:161
cache_type cache
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:168
virtual ~block_stream_processor()
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:125
mode_type::endian_type endian_type
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:64
constexpr static const std::size_t block_values
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:69
void operator()(InputIterator b, InputIterator e)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:151
boost::uint_t< value_bits >::least value_type
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:67
void operator()(InputIterator first, InputIterator last, Category)
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:138
std::array< value_type, block_values > cache_type
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:70
length_type cache_seen
Definition: block/include/nil/crypto3/block/detail/block_stream_processor.hpp:167