hmac.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2019 Mikhail Komarov <nemo@nil.foundation>
3 // Copyright (c) 2020 Nikita Kaskov <nbering@nil.foundation>
4 // Copyright (c) 2021 Ilias Khairullin <ilias@nil.foundation>
5 //
6 // MIT License
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 //---------------------------------------------------------------------------//
26 
27 #ifndef CRYPTO3_MAC_HMAC_HPP
28 #define CRYPTO3_MAC_HMAC_HPP
29 
30 #include <array>
31 
32 #include <nil/crypto3/detail/strxor.hpp>
33 
35 
37 
38 namespace nil {
39  namespace crypto3 {
40  namespace mac {
46  template<typename Hash>
47  struct hmac {
48  typedef Hash hash_type;
49 
50  constexpr static const std::size_t block_bits = hash_type::block_bits;
51  constexpr static const std::size_t block_words = hash_type::block_words;
52  constexpr static const std::size_t block_octets = block_bits / 8;
53  typedef typename hash_type::block_type block_type;
54 
55  constexpr static const std::size_t digest_bits = hash_type::digest_bits;
56  typedef typename hash_type::digest_type digest_type;
57 
58  template<typename InputRange,
59  typename ValueType = typename std::iterator_traits<typename InputRange::iterator>::value_type>
60  using is_key_type = typename std::enable_if<std::is_same<std::uint8_t, ValueType>::value, bool>::type;
61 
65  typedef std::pair<std::array<std::uint8_t, block_octets>, std::array<std::uint8_t, block_octets>>
67  };
68 
69  template<typename Hash>
70  struct mac_key<hmac<Hash>> {
72 
76 
78 
79  constexpr static const std::size_t block_octets = policy_type::block_octets;
80 
81  template<typename KeyRange>
82  mac_key(const KeyRange &key) : schedule_key(process_schedule_key(key)) {
83  }
84 
85  inline void init_accumulator(internal_accumulator_type &i_acc) const {
86  hash<hash_type>(schedule_key.first, i_acc);
87  }
88 
89  template<typename InputRange>
90  inline void update(internal_accumulator_type &i_acc, InputRange range) const {
91  hash<hash_type>(range, i_acc);
92  }
93 
94  template<typename InputIterator>
95  inline void update(internal_accumulator_type &i_acc, InputIterator first, InputIterator last) const {
96  hash<hash_type>(first, last, i_acc);
97  }
98 
100  digest_type i_digest = ::nil::crypto3::accumulators::extract::hash<hash_type>(i_acc);
101 
103  hash<hash_type>(schedule_key.second, o_acc);
104  hash<hash_type>(i_digest, o_acc);
105  return ::nil::crypto3::accumulators::extract::hash<hash_type>(o_acc);
106  }
107 
108  protected:
109  template<typename KeyRange, typename policy_type::template is_key_type<KeyRange> = true>
110  inline static schedule_key_type process_schedule_key(const KeyRange &key) {
111  constexpr std::uint8_t ipad = 0x36;
112  constexpr std::uint8_t opad = 0x5C;
113 
114  schedule_key_type schedule_key;
115  std::fill(schedule_key.first.begin(), schedule_key.first.end(), ipad);
116  std::fill(schedule_key.second.begin(), schedule_key.second.end(), opad);
117 
118  if (key.size() > block_octets) {
119  digest_type hashed_key = hash<hash_type>(key);
121  hashed_key.cbegin(), hashed_key.cend(), schedule_key.first.cbegin(),
122  schedule_key.first.cbegin() + hashed_key.size(), schedule_key.first.begin());
124  hashed_key.cbegin(), hashed_key.cend(), schedule_key.second.cbegin(),
125  schedule_key.second.cbegin() + hashed_key.size(), schedule_key.second.begin());
126  } else {
127  ::nil::crypto3::detail::strxor(key.cbegin(), key.cend(), schedule_key.first.cbegin(),
128  schedule_key.first.cbegin() + key.size(),
129  schedule_key.first.begin());
130  ::nil::crypto3::detail::strxor(key.cbegin(), key.cend(), schedule_key.second.cbegin(),
131  schedule_key.second.cbegin() + key.size(),
132  schedule_key.second.begin());
133  }
134 
135  return schedule_key;
136  }
137 
139  };
140  } // namespace mac
141  } // namespace crypto3
142 } // namespace nil
143 
144 #endif
constexpr matrix< T, N, M > fill(T value)
generates a matrix containing a single value
Definition: matrix/utility.hpp:102
boost::mpl::apply< AccumulatorSet, tag::mac< ProcessingPolicy > >::type::result_type mac(const AccumulatorSet &acc)
Definition: accumulators/mac.hpp:99
constexpr std::enable_if< std::is_same< typename std::iterator_traits< InputIterator1 >::value_type, typename std::iterator_traits< InputIterator2 >::value_type >::value, OutputIterator >::type strxor(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator out)
Definition: algebra/include/nil/crypto3/algebra/algorithms/strxor.hpp:42
typename std::iterator_traits< Iterator >::value_type ValueType
Definition: algebra/include/nil/crypto3/detail/make_array.hpp:50
Definition: pair.hpp:31
Definition: hash_state.hpp:43
Definition: hmac.hpp:47
typename std::enable_if< std::is_same< std::uint8_t, ValueType >::value, bool >::type is_key_type
Definition: hmac.hpp:60
hash_type::digest_type digest_type
Definition: hmac.hpp:56
constexpr static const std::size_t block_bits
Definition: hmac.hpp:50
std::pair< std::array< std::uint8_t, block_octets >, std::array< std::uint8_t, block_octets > > schedule_key_type
Definition: hmac.hpp:66
constexpr static const std::size_t block_octets
Definition: hmac.hpp:52
Hash hash_type
Definition: hmac.hpp:48
constexpr static const std::size_t block_words
Definition: hmac.hpp:51
hash_type::block_type block_type
Definition: hmac.hpp:53
constexpr static const std::size_t digest_bits
Definition: hmac.hpp:55
digest_type compute(internal_accumulator_type &i_acc) const
Definition: hmac.hpp:99
void init_accumulator(internal_accumulator_type &i_acc) const
Definition: hmac.hpp:85
static schedule_key_type process_schedule_key(const KeyRange &key)
Definition: hmac.hpp:110
accumulator_set< hash_type > internal_accumulator_type
Definition: hmac.hpp:77
schedule_key_type schedule_key
Definition: hmac.hpp:138
hmac< Hash > policy_type
Definition: hmac.hpp:71
mac_key(const KeyRange &key)
Definition: hmac.hpp:82
void update(internal_accumulator_type &i_acc, InputRange range) const
Definition: hmac.hpp:90
void update(internal_accumulator_type &i_acc, InputIterator first, InputIterator last) const
Definition: hmac.hpp:95
policy_type::digest_type digest_type
Definition: hmac.hpp:74
policy_type::hash_type hash_type
Definition: hmac.hpp:73
policy_type::schedule_key_type schedule_key_type
Definition: hmac.hpp:75
Mac key - a key that can be used to create and verify MAC.
Definition: mac_key.hpp:41