cmac/accumulator.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/static_digest.hpp>
39 
40 namespace nil {
41  namespace crypto3 {
42  namespace accumulators {
43  namespace impl {
44  template<typename Mode>
45  struct mac_impl : boost::accumulators::accumulator_base {
46  protected:
47  typedef Mode mode_type;
48  typedef typename Mode::cipher_type cipher_type;
49  typedef typename Mode::padding_type padding_type;
50 
51  typedef typename mode_type::finalizer_type finalizer_type;
52 
53  constexpr static const std::size_t word_bits = mode_type::word_bits;
54  typedef typename mode_type::word_type word_type;
55 
56  constexpr static const std::size_t state_bits = mode_type::state_bits;
57  constexpr static const std::size_t state_words = mode_type::state_words;
58  typedef typename mode_type::state_type state_type;
59 
60  constexpr static const std::size_t block_bits = mode_type::block_bits;
61  constexpr static const std::size_t block_words = mode_type::block_words;
62  typedef typename mode_type::block_type block_type;
63 
64  typedef boost::container::static_vector<word_type, block_words> cache_type;
65 
66  public:
67  typedef mac::static_digest<block_bits> result_type;
68 
69  template<typename Args>
70  mac_impl(const Args &args) : cipher(args[accumulators::cipher]), seen(0) {
71  }
72 
73  template<typename ArgumentPack>
74  inline void operator()(const ArgumentPack &args) {
75  return process(args[boost::accumulators::sample]);
76  }
77 
78  template<typename ArgumentPack>
79  inline result_type result(const ArgumentPack &args) const {
80  result_type res = digest;
81 
82  if (!cache.empty()) {
83  block_type ib = {0};
84  std::move(cache.begin(), cache.end(), ib.begin());
85  block_type ob = cipher.end_message(ib);
86  std::move(ob.begin(), ob.end(), std::inserter(res, res.end()));
87  }
88 
89  if (seen % block_bits) {
91  } else {
92  finalizer_type(0)(res);
93  }
94 
95  return res;
96  }
97 
98  protected:
99  inline void process(const block_type &block, std::size_t bits) {
100  }
101 
103 
104  std::size_t seen;
107  };
108  } // namespace impl
109 
110  namespace tag {
111  template<typename MessageAuthenticationCode>
112  struct mac : boost::accumulators::depends_on<> {
113  typedef Mode mode_type;
114 
117 
118  typedef boost::mpl::always<accumulators::impl::mac_impl<MessageAuthenticationCode>> impl;
119  };
120  } // namespace tag
121 
122  namespace extract {
123  template<typename MessageAuthenticationCode, typename AccumulatorSet>
124  typename boost::mpl::apply<AccumulatorSet, tag::mac<MessageAuthenticationCode>>::type::result_type
125  mac(const AccumulatorSet &acc) {
126  return boost::accumulators::extract_result<tag::mac<MessageAuthenticationCode>>(acc);
127  }
128  } // namespace extract
129  } // namespace accumulators
130  } // namespace crypto3
131 } // namespace nil
132 
133 #endif // CRYPTO3_ACCUMULATORS_BLOCK_HPP
boost::mpl::apply< AccumulatorSet, tag::mac< ProcessingPolicy > >::type::result_type mac(const AccumulatorSet &acc)
Definition: accumulators/mac.hpp:99
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: pair.hpp:31
mode_type::finalizer_type finalizer_type
Definition: cmac/accumulator.hpp:51
boost::container::static_vector< word_type, block_words > cache_type
Definition: cmac/accumulator.hpp:64
mode_type::block_type block_type
Definition: cmac/accumulator.hpp:62
std::size_t seen
Definition: cmac/accumulator.hpp:104
void process(const block_type &block, std::size_t bits)
Definition: cmac/accumulator.hpp:99
constexpr static const std::size_t block_words
Definition: cmac/accumulator.hpp:61
constexpr static const std::size_t state_words
Definition: cmac/accumulator.hpp:57
mode_type::word_type word_type
Definition: cmac/accumulator.hpp:54
processing_policy::result_type result_type
Definition: accumulators/mac.hpp:51
result_type result(const ArgumentPack &args) const
Definition: cmac/accumulator.hpp:79
mac::static_digest< block_bits > result_type
Definition: cmac/accumulator.hpp:67
cache_type cache
Definition: cmac/accumulator.hpp:105
Mode::padding_type padding_type
Definition: cmac/accumulator.hpp:49
mode_type::state_type state_type
Definition: cmac/accumulator.hpp:58
block::cipher< cipher_type, mode_type, padding_type > cipher
Definition: cmac/accumulator.hpp:102
constexpr static const std::size_t block_bits
Definition: cmac/accumulator.hpp:60
void operator()(const ArgumentPack &args)
Definition: cmac/accumulator.hpp:74
constexpr static const std::size_t state_bits
Definition: cmac/accumulator.hpp:56
result_type digest
Definition: cmac/accumulator.hpp:106
constexpr static const std::size_t word_bits
Definition: cmac/accumulator.hpp:53
mac_impl(const Args &args)
Definition: cmac/accumulator.hpp:70
Mode mode_type
Definition: cmac/accumulator.hpp:47
Mode::cipher_type cipher_type
Definition: cmac/accumulator.hpp:48
boost::mpl::always< accumulators::impl::mac_impl< MessageAuthenticationCode > > impl
Definition: cmac/accumulator.hpp:118
Mode mode_type
Definition: cmac/accumulator.hpp:113