/src/botan/build/include/botan/pgp_s2k.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * OpenPGP PBKDF |
3 | | * (C) 1999-2007,2017 Jack Lloyd |
4 | | * (C) 2018 Ribose Inc |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #ifndef BOTAN_OPENPGP_S2K_H_ |
10 | | #define BOTAN_OPENPGP_S2K_H_ |
11 | | |
12 | | #include <botan/pbkdf.h> |
13 | | #include <botan/pwdhash.h> |
14 | | #include <botan/hash.h> |
15 | | #include <botan/rfc4880.h> |
16 | | |
17 | | BOTAN_FUTURE_INTERNAL_HEADER(pgp_s2k.h) |
18 | | |
19 | | namespace Botan { |
20 | | |
21 | | /** |
22 | | * OpenPGP's S2K |
23 | | * |
24 | | * See RFC 4880 sections 3.7.1.1, 3.7.1.2, and 3.7.1.3 |
25 | | * If the salt is empty and iterations == 1, "simple" S2K is used |
26 | | * If the salt is non-empty and iterations == 1, "salted" S2K is used |
27 | | * If the salt is non-empty and iterations > 1, "iterated" S2K is used |
28 | | * |
29 | | * Due to complexities of the PGP S2K algorithm, time-based derivation |
30 | | * is not supported. So if iterations == 0 and msec.count() > 0, an |
31 | | * exception is thrown. In the future this may be supported, in which |
32 | | * case "iterated" S2K will be used and the number of iterations |
33 | | * performed is returned. |
34 | | * |
35 | | * Note that unlike PBKDF2, OpenPGP S2K's "iterations" are defined as |
36 | | * the number of bytes hashed. |
37 | | */ |
38 | | class BOTAN_PUBLIC_API(2,2) OpenPGP_S2K final : public PBKDF |
39 | | { |
40 | | public: |
41 | | /** |
42 | | * @param hash the hash function to use |
43 | | */ |
44 | 0 | explicit OpenPGP_S2K(HashFunction* hash) : m_hash(hash) {} |
45 | | |
46 | | std::string name() const override |
47 | 0 | { |
48 | 0 | return "OpenPGP-S2K(" + m_hash->name() + ")"; |
49 | 0 | } |
50 | | |
51 | | std::unique_ptr<PBKDF> new_object() const override |
52 | 0 | { |
53 | 0 | return std::make_unique<OpenPGP_S2K>(m_hash->clone()); |
54 | 0 | } |
55 | | |
56 | | size_t pbkdf(uint8_t output_buf[], size_t output_len, |
57 | | const std::string& passphrase, |
58 | | const uint8_t salt[], size_t salt_len, |
59 | | size_t iterations, |
60 | | std::chrono::milliseconds msec) const override; |
61 | | |
62 | | /** |
63 | | * RFC 4880 encodes the iteration count to a single-byte value |
64 | | */ |
65 | | static uint8_t encode_count(size_t iterations) |
66 | 0 | { |
67 | 0 | return RFC4880_encode_count(iterations); |
68 | 0 | } |
69 | | |
70 | | static size_t decode_count(uint8_t encoded_iter) |
71 | 0 | { |
72 | 0 | return RFC4880_decode_count(encoded_iter); |
73 | 0 | } |
74 | | |
75 | | private: |
76 | | std::unique_ptr<HashFunction> m_hash; |
77 | | }; |
78 | | |
79 | | /** |
80 | | * OpenPGP's S2K |
81 | | * |
82 | | * See RFC 4880 sections 3.7.1.1, 3.7.1.2, and 3.7.1.3 |
83 | | * If the salt is empty and iterations == 1, "simple" S2K is used |
84 | | * If the salt is non-empty and iterations == 1, "salted" S2K is used |
85 | | * If the salt is non-empty and iterations > 1, "iterated" S2K is used |
86 | | * |
87 | | * Note that unlike PBKDF2, OpenPGP S2K's "iterations" are defined as |
88 | | * the number of bytes hashed. |
89 | | */ |
90 | | class BOTAN_PUBLIC_API(2,8) RFC4880_S2K final : public PasswordHash |
91 | | { |
92 | | public: |
93 | | /** |
94 | | * @param hash the hash function to use |
95 | | * @param iterations is rounded due to PGP formatting |
96 | | */ |
97 | | RFC4880_S2K(HashFunction* hash, size_t iterations); |
98 | | |
99 | | std::string to_string() const override; |
100 | | |
101 | 0 | size_t iterations() const override { return m_iterations; } |
102 | | |
103 | | void derive_key(uint8_t out[], size_t out_len, |
104 | | const char* password, size_t password_len, |
105 | | const uint8_t salt[], size_t salt_len) const override; |
106 | | |
107 | | private: |
108 | | std::unique_ptr<HashFunction> m_hash; |
109 | | size_t m_iterations; |
110 | | }; |
111 | | |
112 | | class BOTAN_PUBLIC_API(2,8) RFC4880_S2K_Family final : public PasswordHashFamily |
113 | | { |
114 | | public: |
115 | 0 | RFC4880_S2K_Family(HashFunction* hash) : m_hash(hash) {} |
116 | | |
117 | | std::string name() const override; |
118 | | |
119 | | std::unique_ptr<PasswordHash> tune(size_t output_len, |
120 | | std::chrono::milliseconds msec, |
121 | | size_t max_mem) const override; |
122 | | |
123 | | /** |
124 | | * Return some default parameter set for this PBKDF that should be good |
125 | | * enough for most users. The value returned may change over time as |
126 | | * processing power and attacks improve. |
127 | | */ |
128 | | std::unique_ptr<PasswordHash> default_params() const override; |
129 | | |
130 | | std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override; |
131 | | |
132 | | std::unique_ptr<PasswordHash> from_params( |
133 | | size_t iter, size_t, size_t) const override; |
134 | | private: |
135 | | std::unique_ptr<HashFunction> m_hash; |
136 | | }; |
137 | | |
138 | | } |
139 | | |
140 | | #endif |