hkdf_functions.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_FUNCTIONS_HPP
26 #define CRYPTO3_KDF_HKDF_FUNCTIONS_HPP
27 
29 #include <vector>
30 
31 namespace nil {
32  namespace crypto3 {
33  namespace kdf {
34  namespace detail {
35  template<typename MessageAuthenticationCode>
36  struct hkdf_functions : public hkdf_policy<MessageAuthenticationCode> {
38  typedef typename policy_type::mac_type mac_type;
39 
40  constexpr static const std::size_t salt_bits = policy_type::salt_bits;
42 
43  constexpr static const std::size_t min_key_bits = policy_type::min_key_bits;
44  constexpr static const std::size_t max_key_bits = policy_type::max_key_bits;
45  typedef typename policy_type::key_type key_type;
46 
47  constexpr static const std::size_t digest_bits = policy_type::digest_bits;
49 
55  static void expand(digest_type &digest, const mac_type &mac) {
56  uint8_t counter = 1;
57  std::vector<uint8_t> h;
58  size_t offset = 0;
59 
60  while (offset != digest_bits && counter != 0) {
61  m_prf->update(h);
62  m_prf->update(label, label_len);
63  m_prf->update(salt, salt_len);
64  m_prf->update(counter++);
65  m_prf->final(h);
66 
67  const size_t written = std::min(h.size(), digest_bits - offset);
68  copy_mem(&key[offset], h.data(), written);
69  offset += written;
70  }
71 
72  return offset;
73  }
74 
80  static void extract(digest_type &digest, const mac_type &mac) {
81  std::vector<uint8_t> prk;
82 
83  m_prf->update(secret, secret_len);
84  m_prf->final(prk);
85 
86  const size_t written = std::min(prk.size(), digest_bits);
87  copy_mem(&key[0], prk.data(), written);
88  return written;
89  }
90  };
91  } // namespace detail
92  } // namespace kdf
93  } // namespace crypto3
94 } // namespace nil
95 
96 #endif // CRYPTO3_HKDF_FUNCTIONS_HPP
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::kdf< Mode > >::type::result_type kdf(const AccumulatorSet &acc)
Definition: kdf.hpp:177
void copy_mem(T *out, const T *in, size_t n)
Definition: memory_operations.hpp:186
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
static void extract(digest_type &digest, const mac_type &mac)
HKDF Extraction Step from RFC 5869.
Definition: hkdf_functions.hpp:80
constexpr static const std::size_t salt_bits
Definition: hkdf_functions.hpp:40
policy_type::mac_type mac_type
Definition: hkdf_functions.hpp:38
hkdf_policy< MessageAuthenticationCode > policy_type
Definition: hkdf_functions.hpp:37
static void expand(digest_type &digest, const mac_type &mac)
HKDF Expansion Step from RFC 5869.
Definition: hkdf_functions.hpp:55
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
Definition: hkdf_policy.hpp:33
constexpr static const std::size_t digest_bits
Definition: hkdf_policy.hpp:46
mac_type::key_type salt_type
Definition: hkdf_policy.hpp:37
mac_type::key_type key_type
Definition: hkdf_policy.hpp:44
constexpr static const std::size_t min_key_bits
Definition: hkdf_policy.hpp:42
mac_type::digest_type digest_type
Definition: hkdf_policy.hpp:47
constexpr static const std::size_t salt_bits
Definition: hkdf_policy.hpp:36
constexpr static const std::size_t max_key_bits
Definition: hkdf_policy.hpp:43
MessageAuthenticationCode mac_type
Definition: hkdf_policy.hpp:34