accumulators/block.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_ACCUMULATORS_BLOCK_HPP
26 #define CRYPTO3_ACCUMULATORS_BLOCK_HPP
27 
28 #include <boost/container/static_vector.hpp>
29 
30 #include <boost/parameter/value_type.hpp>
31 
32 #include <boost/accumulators/framework/accumulator_base.hpp>
33 #include <boost/accumulators/framework/extractor.hpp>
34 #include <boost/accumulators/framework/depends_on.hpp>
35 #include <boost/accumulators/framework/parameters/sample.hpp>
36 
37 #include <nil/crypto3/detail/make_array.hpp>
38 #include <nil/crypto3/detail/digest.hpp>
39 #include <nil/crypto3/detail/inject.hpp>
40 
42 
45 #include <boost/accumulators/framework/parameters/sample.hpp>
47 
49 
50 namespace nil {
51  namespace crypto3 {
52  namespace accumulators {
53  namespace impl {
54  template<typename Mode>
55  struct block_impl : boost::accumulators::accumulator_base {
56  protected:
57  typedef Mode mode_type;
58  typedef typename Mode::cipher_type cipher_type;
59  typedef typename Mode::padding_type padding_type;
60 
61  typedef typename mode_type::endian_type endian_type;
62 
63  constexpr static const std::size_t word_bits = mode_type::word_bits;
64  typedef typename mode_type::word_type word_type;
65 
66  constexpr static const std::size_t block_bits = mode_type::block_bits;
67  constexpr static const std::size_t block_words = mode_type::block_words;
68  typedef typename mode_type::block_type block_type;
69 
70  constexpr static const std::size_t value_bits = sizeof(typename block_type::value_type) * CHAR_BIT;
71  constexpr static const std::size_t block_values = block_bits / value_bits;
72 
73  typedef ::nil::crypto3::detail::injector<endian_type, value_bits, block_values, block_bits>
75 
76  public:
78 
79  template<typename Args>
80  block_impl(const Args &args) :
81  total_seen(0), filled(false), mode(args[boost::accumulators::sample]) {
82  }
83 
84  template<typename ArgumentPack>
85  inline void operator()(const ArgumentPack &args) {
86  resolve_type(args[boost::accumulators::sample],
87  args[::nil::crypto3::accumulators::bits | std::size_t()]);
88  }
89 
90  inline result_type result(boost::accumulators::dont_care) const {
91  using namespace ::nil::crypto3::detail;
92 
93  result_type res = dgst;
94 
95  block_type processed_block = mode.end_message(cache, total_seen);
96 
97  res = ::nil::crypto3::resize<block_bits>(res, res.size() + block_values);
98 
99  pack<endian_type, endian_type, value_bits, octet_bits>(
100  processed_block.begin(), processed_block.end(), res.end() - block_values);
101 
102  return res;
103  }
104 
105  protected:
106  inline void resolve_type(const block_type &value, std::size_t bits) {
107  process(value, bits == 0 ? block_bits : bits);
108  }
109 
110  inline void resolve_type(const word_type &value, std::size_t bits) {
111  process(value, bits == 0 ? word_bits : bits);
112  }
113 
114  inline void process_block() {
115  using namespace ::nil::crypto3::detail;
116 
117  block_type processed_block;
118  if (dgst.empty()) {
119  processed_block = mode.begin_message(cache, total_seen);
120  } else {
121  processed_block = mode.process_block(cache, total_seen);
122  }
123 
124  dgst = ::nil::crypto3::resize<block_bits>(dgst, dgst.size() + block_values);
125 
126  pack<endian_type, endian_type, value_bits, octet_bits>(
127  processed_block.begin(), processed_block.end(), dgst.end() - block_values);
128 
129  filled = false;
130  }
131 
132  inline void process(const block_type &value, std::size_t value_seen) {
133  using namespace ::nil::crypto3::detail;
134 
135  if (filled) {
136  process_block();
137  }
138 
139  std::size_t cached_bits = total_seen % block_bits;
140 
141  if (cached_bits != 0) {
142  // If there are already any bits in the cache
143 
144  std::size_t needed_to_fill_bits = block_bits - cached_bits;
145  std::size_t new_bits_to_append =
146  (needed_to_fill_bits > value_seen) ? value_seen : needed_to_fill_bits;
147 
148  injector_type::inject(value, new_bits_to_append, cache, cached_bits);
149  total_seen += new_bits_to_append;
150 
151  if (cached_bits == block_bits) {
152  // If there are enough bits in the incoming value to fill the block
153  filled = true;
154 
155  if (value_seen > new_bits_to_append) {
156 
157  process_block();
158 
159  // If there are some remaining bits in the incoming value - put them into the cache,
160  // which is now empty
161 
162  cached_bits = 0;
163 
164  injector_type::inject(value, value_seen - new_bits_to_append, cache, cached_bits,
165  new_bits_to_append);
166 
167  total_seen += value_seen - new_bits_to_append;
168  }
169  }
170 
171  } else {
172 
173  total_seen += value_seen;
174 
175  // If there are no bits in the cache
176  if (value_seen == block_bits) {
177  // The incoming value is a full block
178  filled = true;
179 
180  std::move(value.begin(), value.end(), cache.begin());
181 
182  } else {
183  // The incoming value is not a full block
184  std::move(value.begin(),
185  value.begin() + value_seen / word_bits + (value_seen % word_bits ? 1 : 0),
186  cache.begin());
187  }
188  }
189  }
190 
191  inline void process(const word_type &value, std::size_t value_seen) {
192  using namespace ::nil::crypto3::detail;
193 
194  if (filled) {
195  process_block();
196  }
197 
198  std::size_t cached_bits = total_seen % block_bits;
199 
200  if (cached_bits % word_bits != 0) {
201  std::size_t needed_to_fill_bits = block_bits - cached_bits;
202  std::size_t new_bits_to_append =
203  (needed_to_fill_bits > value_seen) ? value_seen : needed_to_fill_bits;
204 
205  injector_type::inject(value, new_bits_to_append, cache, cached_bits);
206  total_seen += new_bits_to_append;
207 
208  if (cached_bits == block_bits) {
209  // If there are enough bits in the incoming value to fill the block
210 
211  filled = true;
212 
213  if (value_seen > new_bits_to_append) {
214 
215  process_block();
216 
217  // If there are some remaining bits in the incoming value - put them into the cache,
218  // which is now empty
219  cached_bits = 0;
220 
221  injector_type::inject(value, value_seen - new_bits_to_append, cache, cached_bits,
222  new_bits_to_append);
223 
224  total_seen += value_seen - new_bits_to_append;
225  }
226  }
227 
228  } else {
229  cache[cached_bits / word_bits] = value;
230 
231  total_seen += value_seen;
232  }
233  }
234 
236 
237  bool filled;
238  std::size_t total_seen;
241  };
242  } // namespace impl
243 
244  namespace tag {
245  template<typename Mode>
246  struct block : boost::accumulators::depends_on<bits_count> {
247  typedef Mode mode_type;
248 
251 
252  typedef boost::mpl::always<accumulators::impl::block_impl<mode_type>> impl;
253  };
254  } // namespace tag
255 
256  namespace extract {
257  template<typename Mode, typename AccumulatorSet>
258  typename boost::mpl::apply<AccumulatorSet, tag::block<Mode>>::type::result_type
259  block(const AccumulatorSet &acc) {
260  return boost::accumulators::extract_result<tag::block<Mode>>(acc);
261  }
262  } // namespace extract
263  } // namespace accumulators
264  } // namespace crypto3
265 } // namespace nil
266 
267 #endif // CRYPTO3_ACCUMULATORS_BLOCK_HPP
boost::mpl::apply< AccumulatorSet, tag::block< Mode > >::type::result_type block(const AccumulatorSet &acc)
Definition: accumulators/block.hpp:259
OutputIterator move(const SinglePassRange &rng, OutputIterator result)
Definition: move.hpp:45
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:33
Definition: pair.hpp:31
Definition: accumulators/block.hpp:55
mode_type::word_type word_type
Definition: accumulators/block.hpp:64
::nil::crypto3::detail::injector< endian_type, value_bits, block_values, block_bits > injector_type
Definition: accumulators/block.hpp:74
result_type dgst
Definition: accumulators/block.hpp:240
void process(const word_type &value, std::size_t value_seen)
Definition: accumulators/block.hpp:191
void resolve_type(const block_type &value, std::size_t bits)
Definition: accumulators/block.hpp:106
Mode::padding_type padding_type
Definition: accumulators/block.hpp:59
constexpr static const std::size_t block_bits
Definition: accumulators/block.hpp:66
constexpr static const std::size_t block_values
Definition: accumulators/block.hpp:71
mode_type mode
Definition: accumulators/block.hpp:235
constexpr static const std::size_t block_words
Definition: accumulators/block.hpp:67
mode_type::endian_type endian_type
Definition: accumulators/block.hpp:61
void process(const block_type &value, std::size_t value_seen)
Definition: accumulators/block.hpp:132
void resolve_type(const word_type &value, std::size_t bits)
Definition: accumulators/block.hpp:110
block_type cache
Definition: accumulators/block.hpp:239
result_type result(boost::accumulators::dont_care) const
Definition: accumulators/block.hpp:90
Mode::cipher_type cipher_type
Definition: accumulators/block.hpp:58
bool filled
Definition: accumulators/block.hpp:237
block_impl(const Args &args)
Definition: accumulators/block.hpp:80
digest< block_bits > result_type
Definition: accumulators/block.hpp:77
Mode mode_type
Definition: accumulators/block.hpp:57
constexpr static const std::size_t word_bits
Definition: accumulators/block.hpp:63
std::size_t total_seen
Definition: accumulators/block.hpp:238
void process_block()
Definition: accumulators/block.hpp:114
constexpr static const std::size_t value_bits
Definition: accumulators/block.hpp:70
mode_type::block_type block_type
Definition: accumulators/block.hpp:68
void operator()(const ArgumentPack &args)
Definition: accumulators/block.hpp:85
Definition: accumulators/block.hpp:246
Mode mode_type
Definition: accumulators/block.hpp:247
boost::mpl::always< accumulators::impl::block_impl< mode_type > > impl
Definition: accumulators/block.hpp:252
static void inject(const block_type &b_src, std::size_t b_src_seen, block_type &b_dst, std::size_t &b_dst_seen, std::size_t block_shift=0)
Definition: block/include/nil/crypto3/detail/inject.hpp:267