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