prf_tls_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_PRF_TLS_FUNCTIONS_HPP
26 #define CRYPTO3_KDF_PRF_TLS_FUNCTIONS_HPP
27 
29 
30 #include <vector>
31 
32 namespace nil {
33  namespace crypto3 {
34  namespace kdf {
35  namespace detail {
36  template<std::size_t Version, typename MessageAuthenticationCode1, typename MessageAuthenticationCode2>
38  : public prf_tls_policy<Version, MessageAuthenticationCode1, MessageAuthenticationCode2> {
40 
41  constexpr static const std::size_t version = policy_type::version;
44 
45  template<typename MessageAuthenticationCode>
46  static void p_hash(uint8_t out[], size_t out_len, MessageAuthenticationCode &mac,
47  const uint8_t salt[], size_t salt_len) {
48  std::vector<uint8_t> A(salt, salt + salt_len);
49  std::vector<uint8_t> h;
50 
51  size_t offset = 0;
52 
53  while (offset != out_len) {
54  A = mac.process(A);
55 
56  mac.update(A);
57  mac.update(salt, salt_len);
58  mac.final(h);
59 
60  const size_t writing = std::min(h.size(), out_len - offset);
61  xor_buf(&out[offset], h.data(), writing);
62  offset += writing;
63  }
64  }
65  };
66  } // namespace detail
67  } // namespace kdf
68  } // namespace crypto3
69 } // namespace nil
70 
71 #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 xor_buf(uint8_t out[], const uint8_t in[], size_t length)
Definition: memory_operations.hpp:245
Definition: pair.hpp:31
Definition: prf_tls_functions.hpp:38
constexpr static const std::size_t version
Definition: prf_tls_functions.hpp:41
prf_tls_policy< Version, MessageAuthenticationCode1, MessageAuthenticationCode2 > policy_type
Definition: prf_tls_functions.hpp:39
policy_type::mac_type1 mac_type1
Definition: prf_tls_functions.hpp:42
policy_type::mac_type2 mac_type2
Definition: prf_tls_functions.hpp:43
static void p_hash(uint8_t out[], size_t out_len, MessageAuthenticationCode &mac, const uint8_t salt[], size_t salt_len)
Definition: prf_tls_functions.hpp:46
Definition: prf_tls_policy.hpp:33
MessageAuthenticationCode2 mac_type2
Definition: prf_tls_policy.hpp:36
MessageAuthenticationCode1 mac_type1
Definition: prf_tls_policy.hpp:35
constexpr static const std::size_t version
Definition: prf_tls_policy.hpp:34