cbc_mac.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2019 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_MAC_CBC_MAC_HPP
26 #define CRYPTO3_MAC_CBC_MAC_HPP
27 
28 #include <boost/range/begin.hpp>
29 #include <boost/range/end.hpp>
30 
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace mac {
42  template<typename BlockCipher>
43  class cbc_mac {
45 
46  public:
48 
49  constexpr static const std::size_t block_bits = policy_type::block_bits;
50  constexpr static const std::size_t block_words = policy_type::block_words;
52 
53  constexpr static const std::size_t key_bits = policy_type::key_bits;
54  constexpr static const std::size_t key_words = policy_type::key_words;
55  typedef typename policy_type::key_type key_type;
56 
57  constexpr static const std::size_t state_bits = policy_type::state_bits;
58  constexpr static const std::size_t state_words = policy_type::state_words;
60 
61  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
63 
65  }
66 
67  cbc_mac(const key_type &key) : cipher(key) {
68  }
69 
70  inline void begin_message(state_type &state, const block_type &block) {
71  size_t xored = std::min(output_length() - m_position, length);
72  xor_buf(&m_state[m_position], input, xored);
73  m_position += xored;
74 
75  if (m_position < output_length()) {
76  return;
77  }
78 
79  m_cipher->encrypt(m_state);
80  input += xored;
81  length -= xored;
82  while (length >= output_length()) {
83  xor_buf(m_state, input, output_length());
84  m_cipher->encrypt(m_state);
85  input += output_length();
86  length -= output_length();
87  }
88 
89  xor_buf(m_state, input, length);
90  m_position = length;
91  }
92 
93  void process_block(state_type &state, const block_type &block) {
94  size_t xored = std::min(output_length() - m_position, length);
95  xor_buf(&m_state[m_position], input, xored);
96  m_position += xored;
97 
98  if (m_position < output_length()) {
99  return;
100  }
101 
102  m_cipher->encrypt(m_state);
103  input += xored;
104  length -= xored;
105  while (length >= output_length()) {
106  xor_buf(m_state, input, output_length());
107  m_cipher->encrypt(m_state);
108  input += output_length();
109  length -= output_length();
110  }
111 
112  xor_buf(m_state, input, length);
113  m_position = length;
114  }
115 
116  void end_message(digest_type &digest, const state_type &state, const block_type &block) {
117  if (m_position) {
118  m_cipher->encrypt(m_state);
119  }
120 
121  copy_mem(mac, m_state.data(), m_state.size());
122  zeroise(m_state);
123  m_position = 0;
124  }
125 
126  protected:
127  void schedule_key(const key_type &key) {
128  }
129 
131  };
132  } // namespace mac
133  } // namespace crypto3
134 } // namespace nil
135 
136 #endif
CBC-MAC.
Definition: cbc_mac.hpp:43
block_cipher_type cipher
Definition: cbc_mac.hpp:130
constexpr static const std::size_t block_bits
Definition: cbc_mac.hpp:49
void process_block(state_type &state, const block_type &block)
Definition: cbc_mac.hpp:93
void end_message(digest_type &digest, const state_type &state, const block_type &block)
Definition: cbc_mac.hpp:116
policy_type::digest_type digest_type
Definition: cbc_mac.hpp:62
constexpr static const std::size_t key_bits
Definition: cbc_mac.hpp:53
void schedule_key(const key_type &key)
Definition: cbc_mac.hpp:127
cbc_mac(const key_type &key)
Definition: cbc_mac.hpp:67
policy_type::block_type block_type
Definition: cbc_mac.hpp:51
constexpr static const std::size_t block_words
Definition: cbc_mac.hpp:50
constexpr static const std::size_t key_words
Definition: cbc_mac.hpp:54
constexpr static const std::size_t state_bits
Definition: cbc_mac.hpp:57
policy_type::key_type key_type
Definition: cbc_mac.hpp:55
cbc_mac(const block_cipher_type &cipher)
Definition: cbc_mac.hpp:64
void begin_message(state_type &state, const block_type &block)
Definition: cbc_mac.hpp:70
constexpr static const std::size_t state_words
Definition: cbc_mac.hpp:58
policy_type::state_type state_type
Definition: cbc_mac.hpp:59
constexpr static const std::size_t digest_bits
Definition: cbc_mac.hpp:61
BlockCipher block_cipher_type
Definition: cbc_mac.hpp:47
Definition: block/include/nil/crypto3/detail/static_digest.hpp:72
constexpr T min(const vector< T, N > &v)
computes the minimum valued element
Definition: algebra/include/nil/crypto3/algebra/vector/math.hpp:135
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::accumulators::accumulator_set< mac::digest< MessageAuthenticationCode::input_block_bits >, boost::accumulators::features< accumulators::tag::mac< MessageAuthenticationCode > >> BlockCipher
Definition: cbc_mac_state.hpp:40
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: memory_operations.hpp:245
void copy_mem(T *out, const T *in, size_t n)
Definition: memory_operations.hpp:186
Definition: pair.hpp:31
Definition: block/include/nil/crypto3/detail/digest.hpp:72
Definition: cbc_mac_policy.hpp:37
cipher_type::key_type key_type
Definition: cbc_mac_policy.hpp:53
block_type state_type
Definition: cbc_mac_policy.hpp:46
constexpr static const std::size_t block_words
Definition: cbc_mac_policy.hpp:41
constexpr static const std::size_t state_bits
Definition: cbc_mac_policy.hpp:44
constexpr static const std::size_t block_bits
Definition: cbc_mac_policy.hpp:40
constexpr static const std::size_t key_bits
Definition: cbc_mac_policy.hpp:52
constexpr static const std::size_t digest_bits
Definition: cbc_mac_policy.hpp:48
constexpr static const std::size_t state_words
Definition: cbc_mac_policy.hpp:45
cipher_type::block_type block_type
Definition: cbc_mac_policy.hpp:42
constexpr static const std::size_t key_words
Definition: cbc_mac_policy.hpp:51