pgp_s2k_functions.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 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_PBKDF_PGP_S2K_FUNCTIONS_HPP
26 #define CRYPTO3_PBKDF_PGP_S2K_FUNCTIONS_HPP
27 
29 
30 namespace nil {
31  namespace crypto3 {
32  namespace pbkdf {
33  namespace detail {
34  template<typename Hash>
35  struct pgp_s2k_functions : public pgp_s2k_policy<Hash> {
37 
39 
40  constexpr static const std::size_t round_constants_size = policy_type::round_constants_size;
42 
46  static std::uint8_t encode_count(std::size_t iterations) {
47  /*
48  Only 256 different iterations are actually representable in OpenPGP format ...
49  */
50  for (std::size_t c = 0; c < policy_type::round_constants_size; ++c) {
51  const uint32_t decoded_iter = policy_type::round_constants[c];
52  if (decoded_iter >= iterations) {
53  return static_cast<uint8_t>(c);
54  }
55  }
56 
57  return 255;
58  }
59 
60  static std::size_t decode_count(std::uint8_t encoded_iter) {
61  return policy_type::round_constants[encoded_iter];
62  }
63  };
64  } // namespace detail
65  } // namespace pbkdf
66  } // namespace crypto3
67 } // namespace nil
68 
69 #endif // CRYPTO3_PGP_S2K_FUNCTIONS_HPP
Definition: pair.hpp:31
Definition: pgp_s2k_functions.hpp:35
constexpr static const std::size_t round_constants_size
Definition: pgp_s2k_functions.hpp:40
static std::size_t decode_count(std::uint8_t encoded_iter)
Definition: pgp_s2k_functions.hpp:60
pgp_s2k_policy< Hash > policy_type
Definition: pgp_s2k_functions.hpp:38
pgp_s2k_policy< Hash >::hash_type hash_type
Definition: pgp_s2k_functions.hpp:36
policy_type::round_constants_type round_constants_type
Definition: pgp_s2k_functions.hpp:41
static std::uint8_t encode_count(std::size_t iterations)
Definition: pgp_s2k_functions.hpp:46
Definition: pgp_s2k_policy.hpp:35
std::array< round_constant_type, rounds > round_constants_type
Definition: pgp_s2k_policy.hpp:40
Hash hash_type
Definition: pgp_s2k_policy.hpp:36
constexpr static const round_constants_type round_constants
Definition: pgp_s2k_policy.hpp:46