Botan 3.13.0
Crypto and TLS for C&
pbkdf.h
Go to the documentation of this file.
1/*
2* PBKDF
3* (C) 1999-2007,2012,2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_PBKDF_H_
9#define BOTAN_PBKDF_H_
10
11#include <botan/symkey.h>
12#include <chrono>
13#include <memory>
14#include <string>
15#include <string_view>
16
17/*
18* This entire interface is deprecated. Use the interface in pwdhash.h
19*/
21
22namespace Botan {
23
24/**
25* Base class for PBKDF (password based key derivation function)
26* implementations. Converts a password into a key using a salt
27* and iterated hashing to make brute force attacks harder.
28*
29* Starting in 2.8 this functionality is also offered by PasswordHash.
30*
31* @warning
32* This class will be removed in a future major release. Use PasswordHash
33*/
34class BOTAN_PUBLIC_API(2, 0) PBKDF /* NOLINT(*-special-member-functions) */ {
35 public:
36 /**
37 * Create an instance based on a name
38 * If provider is empty then best available is chosen.
39 * @param algo_spec algorithm name
40 * @param provider provider implementation to choose
41 * @return a null pointer if the algo/provider combination cannot be found
42 */
43 BOTAN_DEPRECATED("Use PasswordHashFamily + PasswordHash")
44 static std::unique_ptr<PBKDF> create(std::string_view algo_spec, std::string_view provider = "");
45
46 /**
47 * Create an instance based on a name, or throw if the
48 * algo/provider combination cannot be found. If provider is
49 * empty then best available is chosen.
50 */
51 BOTAN_DEPRECATED("Use PasswordHashFamily + PasswordHash")
52 static std::unique_ptr<PBKDF> create_or_throw(std::string_view algo_spec, std::string_view provider = "");
53
54 /**
55 * List the providers available for a given PBKDF
56 * @return list of available providers for this algorithm, empty if not available
57 */
58 static std::vector<std::string> providers(std::string_view algo_spec);
59
60 /**
61 * Create a new uninitialized object of the same type
62 * @return new instance of this same algorithm
63 */
64 virtual std::unique_ptr<PBKDF> new_object() const = 0;
65
66 /**
67 * Create a new uninitialized object of the same type
68 * @return new instance of this same algorithm
69 */
70 PBKDF* clone() const { return this->new_object().release(); }
71
72 /**
73 * Return free-form string identifying this algorithm
74 */
75 virtual std::string name() const = 0;
76
77 virtual ~PBKDF() = default;
78
79 /**
80 * Derive a key from a passphrase for a number of iterations
81 * specified by either iterations or if iterations == 0 then
82 * running until msec time has elapsed.
83 *
84 * @param out buffer to store the derived key, must be of out_len bytes
85 * @param out_len the desired length of the key to produce
86 * @param passphrase the password to derive the key from
87 * @param salt a randomly chosen salt
88 * @param salt_len length of salt in bytes
89 * @param iterations the number of iterations to use (use 10K or more)
90 * @param msec if iterations is zero, then instead the PBKDF is
91 * run until msec milliseconds has passed.
92 * @return the number of iterations performed
93 */
94 virtual size_t pbkdf(uint8_t out[],
95 size_t out_len,
96 std::string_view passphrase,
97 const uint8_t salt[],
98 size_t salt_len,
99 size_t iterations,
100 std::chrono::milliseconds msec) const = 0;
101
102 /**
103 * Derive a key from a passphrase for a number of iterations.
104 *
105 * @param out buffer to store the derived key, must be of out_len bytes
106 * @param out_len the desired length of the key to produce
107 * @param passphrase the password to derive the key from
108 * @param salt a randomly chosen salt
109 * @param salt_len length of salt in bytes
110 * @param iterations the number of iterations to use (use 10K or more)
111 */
112 void pbkdf_iterations(uint8_t out[],
113 size_t out_len,
114 std::string_view passphrase,
115 const uint8_t salt[],
116 size_t salt_len,
117 size_t iterations) const;
118
119 /**
120 * Derive a key from a passphrase, running until msec time has elapsed.
121 *
122 * @param out buffer to store the derived key, must be of out_len bytes
123 * @param out_len the desired length of the key to produce
124 * @param passphrase the password to derive the key from
125 * @param salt a randomly chosen salt
126 * @param salt_len length of salt in bytes
127 * @param msec if iterations is zero, then instead the PBKDF is
128 * run until msec milliseconds has passed.
129 * @param iterations set to the number iterations executed
130 */
131 void pbkdf_timed(uint8_t out[],
132 size_t out_len,
133 std::string_view passphrase,
134 const uint8_t salt[],
135 size_t salt_len,
136 std::chrono::milliseconds msec,
137 size_t& iterations) const;
138
139 /**
140 * Derive a key from a passphrase for a number of iterations.
141 *
142 * @param out_len the desired length of the key to produce
143 * @param passphrase the password to derive the key from
144 * @param salt a randomly chosen salt
145 * @param salt_len length of salt in bytes
146 * @param iterations the number of iterations to use (use 10K or more)
147 * @return the derived key
148 */
150 size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const;
151
152 /**
153 * Derive a key from a passphrase, running until msec time has elapsed.
154 *
155 * @param out_len the desired length of the key to produce
156 * @param passphrase the password to derive the key from
157 * @param salt a randomly chosen salt
158 * @param salt_len length of salt in bytes
159 * @param msec if iterations is zero, then instead the PBKDF is
160 * run until msec milliseconds has passed.
161 * @param iterations set to the number iterations executed
162 * @return the derived key
163 */
164 secure_vector<uint8_t> pbkdf_timed(size_t out_len,
165 std::string_view passphrase,
166 const uint8_t salt[],
167 size_t salt_len,
168 std::chrono::milliseconds msec,
169 size_t& iterations) const;
170
171 // Following kept for compat with 1.10:
172
173 /**
174 * Derive a key from a passphrase
175 * @param out_len the desired length of the key to produce
176 * @param passphrase the password to derive the key from
177 * @param salt a randomly chosen salt
178 * @param salt_len length of salt in bytes
179 * @param iterations the number of iterations to use (use 10K or more)
180 */
182 size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const {
183 return OctetString(pbkdf_iterations(out_len, passphrase, salt, salt_len, iterations));
184 }
185
186 /**
187 * Derive a key from a passphrase
188 * @param out_len the desired length of the key to produce
189 * @param passphrase the password to derive the key from
190 * @param salt a randomly chosen salt
191 * @param iterations the number of iterations to use (use 10K or more)
192 */
193 template <typename Alloc>
194 OctetString derive_key(size_t out_len,
195 std::string_view passphrase,
196 const std::vector<uint8_t, Alloc>& salt,
197 size_t iterations) const {
198 return OctetString(pbkdf_iterations(out_len, passphrase, salt.data(), salt.size(), iterations));
199 }
200
201 /**
202 * Derive a key from a passphrase
203 * @param out_len the desired length of the key to produce
204 * @param passphrase the password to derive the key from
205 * @param salt a randomly chosen salt
206 * @param salt_len length of salt in bytes
207 * @param msec is how long to run the PBKDF
208 * @param iterations is set to the number of iterations used
209 */
210 OctetString derive_key(size_t out_len,
211 std::string_view passphrase,
212 const uint8_t salt[],
213 size_t salt_len,
214 std::chrono::milliseconds msec,
215 size_t& iterations) const {
216 return OctetString(pbkdf_timed(out_len, passphrase, salt, salt_len, msec, iterations));
217 }
218
219 /**
220 * Derive a key from a passphrase using a certain amount of time
221 * @param out_len the desired length of the key to produce
222 * @param passphrase the password to derive the key from
223 * @param salt a randomly chosen salt
224 * @param msec is how long to run the PBKDF
225 * @param iterations is set to the number of iterations used
226 */
227 template <typename Alloc>
228 OctetString derive_key(size_t out_len,
229 std::string_view passphrase,
230 const std::vector<uint8_t, Alloc>& salt,
231 std::chrono::milliseconds msec,
232 size_t& iterations) const {
233 return OctetString(pbkdf_timed(out_len, passphrase, salt.data(), salt.size(), msec, iterations));
234 }
235};
236
237/**
238* Compatibility typedef for PBKDF
239*/
240typedef PBKDF S2K;
241
242/**
243* Password based key derivation function factory method
244* @param algo_spec the name of the desired PBKDF algorithm
245* @param provider the provider to use
246* @return pointer to newly allocated object of that type
247*/
248BOTAN_DEPRECATED("Use PasswordHashFamily + PasswordHash")
249inline PBKDF* get_pbkdf(std::string_view algo_spec, std::string_view provider = "") {
250 return PBKDF::create_or_throw(algo_spec, provider).release();
251}
252
253/**
254* Password based key derivation function factory method
255* @param algo_spec the name of the desired PBKDF algorithm
256* @return pointer to newly allocated object of that type
257*/
258BOTAN_DEPRECATED("Use PasswordHashFamily + PasswordHash") inline PBKDF* get_s2k(std::string_view algo_spec) {
259 return PBKDF::create_or_throw(algo_spec).release();
260}
261
262} // namespace Botan
263
264#endif
#define BOTAN_DEPRECATED_HEADER(hdr)
Definition api.h:100
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
static std::vector< std::string > providers(std::string_view algo_spec)
Definition pbkdf.cpp:64
OctetString derive_key(size_t out_len, std::string_view passphrase, const std::vector< uint8_t, Alloc > &salt, size_t iterations) const
Definition pbkdf.h:194
OctetString derive_key(size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
Definition pbkdf.h:181
virtual std::unique_ptr< PBKDF > new_object() const =0
virtual ~PBKDF()=default
static std::unique_ptr< PBKDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition pbkdf.cpp:57
virtual size_t pbkdf(uint8_t out[], size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations, std::chrono::milliseconds msec) const =0
void pbkdf_iterations(uint8_t out[], size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const
Definition pbkdf.cpp:78
PBKDF * clone() const
Definition pbkdf.h:70
void pbkdf_timed(uint8_t out[], size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
Definition pbkdf.cpp:68
virtual std::string name() const =0
static std::unique_ptr< PBKDF > create(std::string_view algo_spec, std::string_view provider="")
Definition pbkdf.cpp:24
OctetString derive_key(size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, std::chrono::milliseconds msec, size_t &iterations) const
Definition pbkdf.h:210
OctetString derive_key(size_t out_len, std::string_view passphrase, const std::vector< uint8_t, Alloc > &salt, std::chrono::milliseconds msec, size_t &iterations) const
Definition pbkdf.h:228
PBKDF S2K
Definition pbkdf.h:240
PBKDF * get_pbkdf(std::string_view algo_spec, std::string_view provider="")
Definition pbkdf.h:249
PBKDF * get_s2k(std::string_view algo_spec)
Definition pbkdf.h:258
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128