cmac.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_CMAC_HPP
26 #define CRYPTO3_MAC_CMAC_HPP
27 
28 #include <nil/crypto3/detail/poly_dbl.hpp>
29 
32 
33 namespace nil {
34  namespace crypto3 {
35  namespace mac {
41  template<typename BlockCipher>
42  class cmac {
44 
45  public:
47 
48  constexpr static const std::size_t block_bits = policy_type::block_bits;
49  constexpr static const std::size_t block_words = policy_type::block_words;
51 
52  constexpr static const std::size_t key_bits = policy_type::key_bits;
53  constexpr static const std::size_t key_words = policy_type::key_words;
54  typedef typename policy_type::key_type key_type;
55 
56  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
58 
60  BOOST_STATIC_ASSERT(poly_double_supported_size(cipher_type::block_bits / CHAR_BIT));
61  }
62 
63  cmac(const key_type &key) : cipher(key) {
64  }
65 
66  inline void begin_message(const block_type &block) const {
67  return process_block(block);
68  }
69 
70  inline void process_block(const block_type &block) const {
71  const size_t bs = output_length();
72 
73  buffer_insert(m_buffer, m_position, input, length);
74  if (m_position + length > bs) {
75  xor_buf(m_state, m_buffer, bs);
76  m_cipher->encrypt(m_state);
77  input += (bs - m_position);
78  length -= (bs - m_position);
79  while (length > bs) {
80  xor_buf(m_state, input, bs);
81  m_cipher->encrypt(m_state);
82  input += bs;
83  length -= bs;
84  }
85  copy_mem(m_buffer.data(), input, length);
86  m_position = 0;
87  }
88  m_position += length;
89  }
90 
91  inline void end_message(const block_type &block) const {
92  xor_buf(m_state, m_buffer, m_position);
93 
94  if (m_position == output_length()) {
95  xor_buf(m_state, m_B, output_length());
96  } else {
97  m_state[m_position] ^= 0x80;
98  xor_buf(m_state, m_P, output_length());
99  }
100 
101  m_cipher->encrypt(m_state);
102 
103  copy_mem(mac, m_state.data(), output_length());
104 
105  zeroise(m_state);
106  zeroise(m_buffer);
107  m_position = 0;
108  }
109 
110  protected:
111  void schedule_key(const key_type &key) {
112  using namespace nil::crypto3::detail;
113 
114  cipher.encrypt_block(m_B);
115  poly_double_n(m_B.data(), m_B.size());
116  poly_double_n(m_P.data(), m_B.data(), m_P.size());
117  }
118 
120  };
121 
122  template<typename BlockCipher>
124  } // namespace mac
125  } // namespace crypto3
126 } // namespace nil
127 
128 #endif
CMAC, also known as OMAC1.
Definition: cmac.hpp:42
constexpr static const std::size_t block_bits
Definition: cmac.hpp:48
void begin_message(const block_type &block) const
Definition: cmac.hpp:66
void process_block(const block_type &block) const
Definition: cmac.hpp:70
BlockCipher cipher_type
Definition: cmac.hpp:46
policy_type::digest_type digest_type
Definition: cmac.hpp:57
cmac(const key_type &key)
Definition: cmac.hpp:63
constexpr static const std::size_t digest_bits
Definition: cmac.hpp:56
constexpr static const std::size_t key_bits
Definition: cmac.hpp:52
policy_type::key_type key_type
Definition: cmac.hpp:54
policy_type::block_type block_type
Definition: cmac.hpp:50
cipher_type cipher
Definition: cmac.hpp:119
void schedule_key(const key_type &key)
Definition: cmac.hpp:111
cmac(const cipher_type &cipher)
Definition: cmac.hpp:59
constexpr static const std::size_t key_words
Definition: cmac.hpp:53
void end_message(const block_type &block) const
Definition: cmac.hpp:91
constexpr static const std::size_t block_words
Definition: cmac.hpp:49
Definition: block/include/nil/crypto3/detail/static_digest.hpp:72
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
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:33
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 poly_double_n(uint8_t out[], InputIterator first, InputIterator last)
Definition: mac/include/nil/crypto3/detail/poly_dbl.hpp:83
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: memory_operations.hpp:245
constexpr bool poly_double_supported_size(size_t n)
Definition: mac/include/nil/crypto3/detail/poly_dbl.hpp:105
void copy_mem(T *out, const T *in, size_t n)
Definition: memory_operations.hpp:186
Definition: pair.hpp:31
Definition: cmac_policy.hpp:37
constexpr static const std::size_t key_words
Definition: cmac_policy.hpp:49
constexpr static const std::size_t block_bits
Definition: cmac_policy.hpp:42
constexpr static const std::size_t block_words
Definition: cmac_policy.hpp:43
cipher_type::block_type block_type
Definition: cmac_policy.hpp:44
cipher_type::key_type key_type
Definition: cmac_policy.hpp:51
constexpr static const std::size_t key_bits
Definition: cmac_policy.hpp:50
constexpr static const std::size_t digest_bits
Definition: cmac_policy.hpp:46