x919_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_ANSI_X919_MAC_HPP
26 #define CRYPTO3_ANSI_X919_MAC_HPP
27 
28 #include <nil/crypto3/block/des.hpp>
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace mac {
40  template<typename BlockCipher = block::des>
41  class ansi_x919_mac {
43 
44  public:
46 
47  constexpr static const std::size_t word_bits = policy_type::word_bits;
49 
50  constexpr static const std::size_t block_bits = policy_type::block_bits;
51  constexpr static const std::size_t block_words = policy_type::block_words;
52  typedef typename policy_type::block_type block_type;
53 
54  constexpr static const std::size_t key_words = policy_type::key_words;
55  constexpr static const std::size_t key_bits = policy_type::key_bits;
56  typedef typename policy_type::key_type key_type;
57 
58  constexpr static const std::size_t key_schedule_bits = policy_type::key_schedule_bits;
59  constexpr static const std::size_t key_schedule_words = policy_type::key_schedule_words;
61 
62  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
64 
65  ansi_x919_mac(const key_type &key) :
66  c1(key.size() == key_words ? key : make_array(key.begin(), key.begin() + key_words)),
67  c2(key.size() == key_words ? key : make_array(key.begin() + key_words, key.end())) {
68  }
69 
70  ansi_x919_mac(const cipher_type &c_1, const cipher_type &c_2) : c1(c_1), c2(c_2) {
71  }
72 
73  void process(digest_type &state, const block_type &block) {
74  size_t xored = std::min(8 - m_position, length);
75  xor_buf(&state[m_position], input, xored);
76  m_position += xored;
77 
78  if (m_position < 8) {
79  return;
80  }
81 
82  c1.encrypt_block(state);
83  input += xored;
84  length -= xored;
85  while (length >= 8) {
86  xor_buf(state, input, 8);
87  c1.encrypt_block(state);
88  input += 8;
89  length -= 8;
90  }
91 
92  xor_buf(state, input, length);
93  m_position = length;
94  }
95 
96  void end_message(digest_type &state, const block_type &block) {
97  if (m_position) {
98  c1.encrypt_block(state);
99  }
100  c2.decrypt_block(state.data(), mac);
101  c1.encrypt_block(mac);
102  zeroise(state);
103  m_position = 0;
104  }
105 
106  protected:
108  };
109  } // namespace mac
110  } // namespace crypto3
111 } // namespace nil
112 
113 #endif
DES/3DES-based MAC from ANSI X9.19.
Definition: x919_mac.hpp:41
policy_type::key_type key_type
Definition: x919_mac.hpp:56
policy_type::block_type block_type
Definition: x919_mac.hpp:52
ansi_x919_mac(const key_type &key)
Definition: x919_mac.hpp:65
constexpr static const std::size_t key_schedule_words
Definition: x919_mac.hpp:59
void end_message(digest_type &state, const block_type &block)
Definition: x919_mac.hpp:96
policy_type::digest_type digest_type
Definition: x919_mac.hpp:63
BlockCipher cipher_type
Definition: x919_mac.hpp:45
constexpr static const std::size_t word_bits
Definition: x919_mac.hpp:47
constexpr static const std::size_t key_words
Definition: x919_mac.hpp:54
ansi_x919_mac(const cipher_type &c_1, const cipher_type &c_2)
Definition: x919_mac.hpp:70
void process(digest_type &state, const block_type &block)
Definition: x919_mac.hpp:73
constexpr static const std::size_t key_bits
Definition: x919_mac.hpp:55
policy_type::key_schedule_type key_schedule_type
Definition: x919_mac.hpp:60
cipher_type c1
Definition: x919_mac.hpp:107
cipher_type c2
Definition: x919_mac.hpp:107
constexpr static const std::size_t key_schedule_bits
Definition: x919_mac.hpp:58
constexpr static const std::size_t digest_bits
Definition: x919_mac.hpp:62
constexpr static const std::size_t block_words
Definition: x919_mac.hpp:51
policy_type::word_type word_type
Definition: x919_mac.hpp:48
constexpr static const std::size_t block_bits
Definition: x919_mac.hpp:50
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
std::array< detail::ValueType< RandomAccessIterator >, N > make_array(RandomAccessIterator first, RandomAccessIterator last)
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:65
void xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: memory_operations.hpp:245
Definition: pair.hpp:31
Definition: x919_mac_policy.hpp:36
constexpr static const std::size_t key_words
Definition: x919_mac_policy.hpp:45
constexpr static const std::size_t key_schedule_bits
Definition: x919_mac_policy.hpp:48
constexpr static const std::size_t key_schedule_words
Definition: x919_mac_policy.hpp:49
cipher_type::key_schedule_type key_schedule_type
Definition: x919_mac_policy.hpp:50
constexpr static const std::size_t word_bits
Definition: x919_mac_policy.hpp:41
std::array< byte_type, digest_size > digest_type
Definition: x919_mac_policy.hpp:54
cipher_type::word_type word_type
Definition: x919_mac_policy.hpp:42
constexpr static const std::size_t digest_bits
Definition: x919_mac_policy.hpp:53
constexpr static const std::size_t key_bits
Definition: x919_mac_policy.hpp:44
boost::container::static_vector< word_type, 2 *key_words > key_type
Definition: x919_mac_policy.hpp:46