hkdf.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_KDF_HKDF_HPP
26 #define CRYPTO3_KDF_HKDF_HPP
27 
28 #include <nil/crypto3/detail/pack.hpp>
29 
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace mac {
35  template<typename Hash>
36  struct hmac;
37  }
38  namespace kdf {
45  template<typename Hash, typename MessageAuthenticationCode = mac::hmac<Hash>>
46  class hkdf {
48 
49  public:
50  typedef MessageAuthenticationCode mac_type;
51  typedef Hash hash_type;
52 
53  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
54  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
55  typedef typename policy_type::key_type key_type;
56 
57  constexpr static const std::size_t secret_bits = policy_type::secret_bits;
59 
60  constexpr static const std::size_t label_bits = policy_type::label_bits;
61  typedef typename policy_type::label_type label_type;
62 
63  constexpr static const std::size_t salt_bits = policy_type::salt_bits;
65 
66  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
68 
69  hkdf(const secret_type &secret, const salt_type &salt = salt_type()) :
70  extract_mac(salt.size() ? salt : [&]() -> salt_type {
71  salt_type ret;
72  ret.fill(0);
73  pack(hash_type::digest_bits, ret);
74  return ret;
75  }()),
76  expand_mac(secret) {
77  }
78 
79  inline digest_type process(const key_type &key) {
81  compute(digest, key);
82  return digest;
83  }
84 
85  inline void process(digest_type &digest, const key_type &key) {
86  policy_type::extract(digest, expand_mac, salt, salt_len, nullptr, 0);
87  policy_type::expand(digest, extract_mac);
88  }
89 
90  protected:
91  mac_type extract_mac, expand_mac;
92  };
93  } // namespace kdf
94  } // namespace crypto3
95 } // namespace nil
96 #endif
Definition: hkdf.hpp:46
MessageAuthenticationCode mac_type
Definition: hkdf.hpp:50
constexpr static const std::size_t label_bits
Definition: hkdf.hpp:60
constexpr static const std::size_t digest_bits
Definition: hkdf.hpp:66
policy_type::label_type label_type
Definition: hkdf.hpp:61
constexpr static const std::size_t max_key_bits
Definition: hkdf.hpp:54
mac_type extract_mac
Definition: hkdf.hpp:91
policy_type::salt_type salt_type
Definition: hkdf.hpp:64
constexpr static const std::size_t min_key_bits
Definition: hkdf.hpp:53
digest_type process(const key_type &key)
Definition: hkdf.hpp:79
constexpr static const std::size_t salt_bits
Definition: hkdf.hpp:63
mac_type expand_mac
Definition: hkdf.hpp:91
Hash hash_type
Definition: hkdf.hpp:51
hkdf(const secret_type &secret, const salt_type &salt=salt_type())
Definition: hkdf.hpp:69
constexpr static const std::size_t secret_bits
Definition: hkdf.hpp:57
policy_type::secret_type secret_type
Definition: hkdf.hpp:58
policy_type::digest_type digest_type
Definition: hkdf.hpp:67
policy_type::key_type key_type
Definition: hkdf.hpp:55
void process(digest_type &digest, const key_type &key)
Definition: hkdf.hpp:85
void pack(InputIterator first, InputIterator last, std::random_access_iterator_tag, OutputIterator out)
Packs elements from the range [first, last) into elements starting from out. Works for input containe...
Definition: block/include/nil/crypto3/detail/pack.hpp:835
boost::mpl::apply< AccumulatorSet, tag::mac< ProcessingPolicy > >::type::result_type mac(const AccumulatorSet &acc)
Definition: accumulators/mac.hpp:99
boost::mpl::apply< AccumulatorSet, tag::kdf< Mode > >::type::result_type kdf(const AccumulatorSet &acc)
Definition: kdf.hpp:177
boost::container::small_vector< octet_type, DigestBits/octet_bits > digest
Definition: codec/include/nil/crypto3/detail/digest.hpp:71
OutputIterator compute(InputIterator first, InputIterator last, const mac::mac_key< Mac > &key, OutputIterator out)
Definition: mac/include/nil/crypto3/mac/algorithm/compute.hpp:56
Definition: pair.hpp:31
Definition: block/include/nil/crypto3/detail/digest.hpp:72
Definition: hkdf_functions.hpp:36
constexpr static const std::size_t max_key_bits
Definition: hkdf_functions.hpp:44
policy_type::key_type key_type
Definition: hkdf_functions.hpp:45
constexpr static const std::size_t digest_bits
Definition: hkdf_functions.hpp:47
constexpr static const std::size_t salt_bits
Definition: hkdf_functions.hpp:40
policy_type::salt_type salt_type
Definition: hkdf_functions.hpp:41
policy_type::digest_type digest_type
Definition: hkdf_functions.hpp:48
constexpr static const std::size_t min_key_bits
Definition: hkdf_functions.hpp:43
mac_type::key_type secret_type
Definition: hkdf_policy.hpp:40
constexpr static const std::size_t secret_bits
Definition: hkdf_policy.hpp:39
Definition: hmac.hpp:47