prf_x942.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_ANSI_X942_PRF_HPP
26 #define CRYPTO3_KDF_ANSI_X942_PRF_HPP
27 
29 
31 
32 #include <vector>
33 
34 namespace nil {
35  namespace crypto3 {
36  namespace kdf {
42  template<typename Hash = hash::sha1>
43  class x942_prf {
45 
46  public:
48 
49  static void process() {
50  const OID kek_algo(m_key_wrap_oid);
51 
52  std::vector<uint8_t> h;
53  std::vector<uint8_t> in;
54  size_t offset = 0;
55  uint32_t counter = 1;
56 
57  in.reserve(salt_len + label_len);
58  in += std::make_pair(label, label_len);
59  in += std::make_pair(salt, salt_len);
60 
61  while (offset != key_len && counter) {
62  hash->update(secret, secret_len);
63 
64  hash->update(
65  der_encoder()
66  .start_cons(SEQUENCE)
67 
68  .start_cons(SEQUENCE)
69  .encode(kek_algo)
70  .raw_bytes(encode_x942_int(counter))
71  .end_cons()
72 
73  .encode_if(salt_len != 0,
74  der_encoder().start_explicit(0).encode(in, OCTET_STRING).end_explicit())
75 
76  .start_explicit(2)
77  .raw_bytes(encode_x942_int(static_cast<uint32_t>(8 * key_len)))
78  .end_explicit()
79 
80  .end_cons()
81  .get_contents());
82 
83  hash->final(h);
84  const size_t copied = std::min(h.size(), key_len - offset);
85  copy_mem(&key[offset], h.data(), copied);
86  offset += copied;
87 
88  ++counter;
89  }
90 
91  return offset;
92  }
93  };
94  } // namespace kdf
95  } // namespace crypto3
96 } // namespace nil
97 
98 #endif
PRF from ANSI X9.42.
Definition: prf_x942.hpp:43
static void process()
Definition: prf_x942.hpp:49
policy_type::hash_type hash_type
Definition: prf_x942.hpp:47
std::enable_if< detail::is_iterator< OutputIterator >::value, OutputIterator >::type encode(InputIterator first, InputIterator last, OutputIterator out)
Encodes the elements with particular codec defined with Encoder in the range, defined by [first,...
Definition: codec/include/nil/crypto3/codec/algorithm/encode.hpp:57
std::enable_if<!boost::accumulators::detail::is_accumulator_set< OutputIterator >::value, OutputIterator >::type hash(InputIterator first, InputIterator last, OutputIterator out)
Definition: algorithm/hash.hpp:78
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::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: prf_x942_functions.hpp:35
policy_type::hash_type hash_type
Definition: prf_x942_functions.hpp:38