gmac.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_GMAC_HPP
26 #define CRYPTO3_MAC_GMAC_HPP
27 
30 
31 namespace nil {
32  namespace crypto3 {
33  namespace hashes {
34  class ghash;
35  }
36  namespace mac {
43  template<typename BlockCipher, typename Hash = hashes::ghash>
44  class gmac {
46 
47  typedef typename policy_type::byte_type byte_type;
48  typedef typename policy_type::word_type word_type;
49 
50  public:
53 
54  constexpr static const std::size_t block_bits = policy_type::block_bits;
55  constexpr static const std::size_t block_words = policy_type::block_words;
57 
58  constexpr static const std::size_t key_bits = policy_type::key_bits;
59  constexpr static const std::size_t key_words = policy_type::key_words;
60  typedef typename policy_type::key_type key_type;
61 
62  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
64 
66  }
67 
68  gmac(const key_type &key) : cipher(key) {
69  }
70 
72  secure_vector<uint8_t> y0(GCM_BS);
73 
74  if (nonce_len == 12) {
75  copy_mem(y0.data(), nonce, nonce_len);
76  y0[GCM_BS - 1] = 1;
77  } else {
78  hash->ghash_update(y0, nonce, nonce_len);
79  hash->add_final_block(y0, 0, nonce_len);
80  }
81 
82  secure_vector<uint8_t> m_enc_y0(GCM_BS);
83  cipher->encrypt(y0.data(), m_enc_y0.data());
84  hash->start(m_enc_y0.data(), m_enc_y0.size());
85  m_initialized = true;
86  }
87 
89  if (m_aad_buf_pos > 0) {
90  const size_t taking = std::min(GCM_BS - m_aad_buf_pos, size);
91  copy_mem(&m_aad_buf[m_aad_buf_pos], input, taking);
92  m_aad_buf_pos += taking;
93  input += taking;
94  size -= taking;
95 
96  if (m_aad_buf_pos == GCM_BS) {
97  hash->update_associated_data(m_aad_buf.data(), GCM_BS);
98  m_aad_buf_pos = 0;
99  }
100  }
101 
102  const size_t left_over = size % GCM_BS;
103  const size_t full_blocks = size - left_over;
104  hash->update_associated_data(input, full_blocks);
105  input += full_blocks;
106 
107  if (left_over > 0) {
108  copy_mem(&m_aad_buf[m_aad_buf_pos], input, left_over);
109  m_aad_buf_pos += left_over;
110  }
111  }
112 
113  void end_message(const block_type &block) {
114  // This ensures the GMAC computation has been initialized with a fresh
115  // nonce. The aim of this check is to prevent developers from re-using
116  // nonces (and potential nonce-reuse attacks).
117  if (!m_initialized) {
118  throw Invalid_State("GMAC was not used with a fresh nonce");
119  }
120 
121  // process the rest of the aad buffer. Even if it is a partial block only
122  // ghash_update will process it properly.
123  if (m_aad_buf_pos > 0) {
124  m_ghash->update_associated_data(m_aad_buf.data(), m_aad_buf_pos);
125  }
126  secure_vector<uint8_t> result = m_ghash->final();
127  copy_mem(mac, result.data(), result.size());
128  clear();
129  }
130 
131  protected:
132  void schedule_key(const key_type &key) {
133  }
134 
137  };
138  } // namespace mac
139  } // namespace crypto3
140 } // namespace nil
141 #endif
GMAC.
Definition: gmac.hpp:44
gmac(const key_type &key)
Definition: gmac.hpp:68
policy_type::hash_type hash_type
Definition: gmac.hpp:52
void schedule_key(const key_type &key)
Definition: gmac.hpp:132
policy_type::key_type key_type
Definition: gmac.hpp:60
void process_block(const block_type &block)
Definition: gmac.hpp:88
policy_type::block_type block_type
Definition: gmac.hpp:56
constexpr static const std::size_t key_bits
Definition: gmac.hpp:58
constexpr static const std::size_t block_bits
Definition: gmac.hpp:54
void end_message(const block_type &block)
Definition: gmac.hpp:113
constexpr static const std::size_t digest_bits
Definition: gmac.hpp:62
policy_type::cipher_type cipher_type
Definition: gmac.hpp:51
hash_type hash
Definition: gmac.hpp:136
constexpr static const std::size_t block_words
Definition: gmac.hpp:55
constexpr static const std::size_t key_words
Definition: gmac.hpp:59
gmac(const cipher_type &cipher, const hash_type &hash)
Definition: gmac.hpp:65
policy_type::digest_type digest_type
Definition: gmac.hpp:63
cipher_type cipher
Definition: gmac.hpp:135
void begin_message(const block_type &block)
Definition: gmac.hpp:71
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
void copy_mem(T *out, const T *in, size_t n)
Definition: memory_operations.hpp:186
Definition: pair.hpp:31
Definition: gmac_policy.hpp:37
cipher_type::key_type key_type
Definition: gmac_policy.hpp:52
boost::uint_t< CHAR_BIT >::exact byte_type
Definition: gmac_policy.hpp:41
BlockCipher cipher_type
Definition: gmac_policy.hpp:38
constexpr static const std::size_t block_words
Definition: gmac_policy.hpp:44
Hash hash_type
Definition: gmac_policy.hpp:39
constexpr static const std::size_t block_bits
Definition: gmac_policy.hpp:43
cipher_type::block_type block_type
Definition: gmac_policy.hpp:45
constexpr static const std::size_t key_bits
Definition: gmac_policy.hpp:51
constexpr static const std::size_t key_words
Definition: gmac_policy.hpp:50
constexpr static const std::size_t digest_bits
Definition: gmac_policy.hpp:47