prf_tls.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_TLS_PRF_HPP
26 #define CRYPTO3_KDF_TLS_PRF_HPP
27 
29 #include <nil/crypto3/hash/md5.hpp>
30 
31 #include <nil/crypto3/mac/hmac.hpp>
32 
34 
35 #include <vector>
36 
37 namespace nil {
38  namespace crypto3 {
39  namespace kdf {
47  template<std::size_t Version, typename MessageAuthenticationCode1, typename MessageAuthenticationCode2>
48  class prf_tls { };
49 
59  template<typename MessageAuthenticationCode1, typename MessageAuthenticationCode2>
60  class prf_tls<1, MessageAuthenticationCode1, MessageAuthenticationCode2> {
63 
64  public:
65  constexpr static const std::size_t version = policy_type::version;
68 
69  constexpr static const std::size_t secret_bits = policy_type::secret_bits;
70  typedef typename policy_type::secret_type secret_type;
71 
72  constexpr static const std::size_t label_bits = policy_type::label_bits;
73  typedef typename policy_type::label_type label_type;
74 
75  constexpr static const std::size_t salt_bits = policy_type::salt_bits;
76  typedef typename policy_type::salt_type salt_type;
77 
78  prf_tls(const secret_type &secret) : mac1(secret), mac2(secret) {
79  }
80 
81  static void process() {
82  const size_t S1_len = (secret_len + 1) / 2, S2_len = (secret_len + 1) / 2;
83  const uint8_t *S1 = secret;
84  const uint8_t *S2 = secret + (secret_len - S2_len);
85  std::vector<uint8_t> msg;
86 
87  msg.reserve(label_len + salt_len);
88  msg += std::make_pair(label, label_len);
89  msg += std::make_pair(salt, salt_len);
90 
91  policy_type::template p_hash<mac_type2>(key, key_len, mac2, msg.data(), msg.size());
92  policy_type::template p_hash<mac_type1>(key, key_len, mac1, msg.data(), msg.size());
93 
94  return key_len;
95  }
96 
97  protected:
100  };
101 
106  template<>
107  class prf_tls<1, mac::hmac<hashes::sha1>, mac::hmac<hashes::md5>> {
109 
110  public:
111  constexpr static const std::size_t version = policy_type::version;
114 
115  constexpr static const std::size_t secret_bits = policy_type::secret_bits;
116  typedef typename policy_type::secret_type secret_type;
117 
118  constexpr static const std::size_t label_bits = policy_type::label_bits;
119  typedef typename policy_type::label_type label_type;
120 
121  constexpr static const std::size_t salt_bits = policy_type::salt_bits;
122  typedef typename policy_type::salt_type salt_type;
123 
124  prf_tls(const secret_type &secret) : mac1(secret), mac2(secret) {
125  }
126 
127  static void process() {
128  const size_t S1_len = (secret_len + 1) / 2, S2_len = (secret_len + 1) / 2;
129  const uint8_t *S1 = secret;
130  const uint8_t *S2 = secret + (secret_len - S2_len);
131  std::vector<uint8_t> msg;
132 
133  msg.reserve(label_len + salt_len);
134  msg += std::make_pair(label, label_len);
135  msg += std::make_pair(salt, salt_len);
136 
137  policy_type::template p_hash<mac_type2>(key, key_len, mac2, msg.data(), msg.size());
138  policy_type::template p_hash<mac_type1>(key, key_len, mac1, msg.data(), msg.size());
139  return key_len;
140  }
141 
142  protected:
145  };
146 
152  template<typename MessageAuthenticationCode>
153  class prf_tls<2, MessageAuthenticationCode, MessageAuthenticationCode> {
155 
156  public:
157  constexpr static const std::size_t version = policy_type::version;
160 
161  constexpr static const std::size_t secret_bits = policy_type::secret_bits;
162  typedef typename policy_type::secret_type secret_type;
163 
164  constexpr static const std::size_t label_bits = policy_type::label_bits;
165  typedef typename policy_type::label_type label_type;
166 
167  constexpr static const std::size_t salt_bits = policy_type::salt_bits;
168  typedef typename policy_type::salt_type salt_type;
169 
170  prf_tls(const secret_type &secret) : mac(secret) {
171  }
172 
173  static void process() {
174  std::vector<uint8_t> msg;
175 
176  msg.reserve(label_len + salt_len);
177  msg += std::make_pair(label, label_len);
178  msg += std::make_pair(salt, salt_len);
179 
180  policy_type::template P_hash<mac_type1>(key, key_len, mac, msg.data(), msg.size());
181  return key_len;
182  }
183 
184  protected:
186  };
187  } // namespace kdf
188  } // namespace crypto3
189 } // namespace nil
190 
191 #endif
Definition: prf_tls.hpp:48
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
Definition: pair.hpp:31
Definition: prf_tls_functions.hpp:38
policy_type::mac_type1 mac_type1
Definition: prf_tls_functions.hpp:42
policy_type::mac_type2 mac_type2
Definition: prf_tls_functions.hpp:43
Definition: hmac.hpp:47