Botan 3.14.1
Crypto and TLS for C&
pbkdf.cpp
Go to the documentation of this file.
1/*
2* PBKDF
3* (C) 2012 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/pbkdf.h>
9
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/internal/algorithm_spec.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/probe_providers.h>
15
16#if defined(BOTAN_HAS_PBKDF2)
17 #include <botan/pbkdf2.h>
18#endif
19
20#if defined(BOTAN_HAS_PGP_S2K)
21 #include <botan/pgp_s2k.h>
22#endif
23
24namespace Botan {
25
26std::unique_ptr<PBKDF> PBKDF::create(std::string_view algo_spec, std::string_view provider) {
27 const AlgorithmSpec req(algo_spec);
28
29#if defined(BOTAN_HAS_PBKDF2)
30 if(auto m = req.match("PBKDF2({prf})")) {
31 if(provider.empty() || provider == "base") {
32 const auto prf = m->str("prf");
33 if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", prf))) {
34 return std::make_unique<PKCS5_PBKDF2>(std::move(mac));
35 }
36
37 if(auto mac = MessageAuthenticationCode::create(prf)) {
38 return std::make_unique<PKCS5_PBKDF2>(std::move(mac));
39 }
40 }
41
42 return nullptr;
43 }
44#endif
45
46#if defined(BOTAN_HAS_PGP_S2K)
47 if(auto m = req.match("OpenPGP-S2K({hash})")) {
48 if(auto hash = HashFunction::create(m->str("hash"))) {
49 return std::make_unique<OpenPGP_S2K>(std::move(hash));
50 }
51 }
52#endif
53
54 BOTAN_UNUSED(req, provider);
55
56 return nullptr;
57}
58
59//static
60std::unique_ptr<PBKDF> PBKDF::create_or_throw(std::string_view algo, std::string_view provider) {
61 if(auto pbkdf = PBKDF::create(algo, provider)) {
62 return pbkdf;
63 }
64 throw Lookup_Error("PBKDF", algo, provider);
65}
66
67std::vector<std::string> PBKDF::providers(std::string_view algo_spec) {
68 return probe_providers_of<PBKDF>(algo_spec);
69}
70
71void PBKDF::pbkdf_timed(uint8_t out[],
72 size_t out_len,
73 std::string_view passphrase,
74 const uint8_t salt[],
75 size_t salt_len,
76 std::chrono::milliseconds msec,
77 size_t& iterations) const {
78 iterations = pbkdf(out, out_len, passphrase, salt, salt_len, 0, msec);
79}
80
81void PBKDF::pbkdf_iterations(uint8_t out[],
82 size_t out_len,
83 std::string_view passphrase,
84 const uint8_t salt[],
85 size_t salt_len,
86 size_t iterations) const {
87 if(iterations == 0) {
88 throw Invalid_Argument(name() + ": Invalid iteration count");
89 }
90
91 const size_t iterations_run =
92 pbkdf(out, out_len, passphrase, salt, salt_len, iterations, std::chrono::milliseconds(0));
93 BOTAN_ASSERT_EQUAL(iterations, iterations_run, "Expected PBKDF iterations");
94}
95
97 size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const {
98 secure_vector<uint8_t> out(out_len);
99 pbkdf_iterations(out.data(), out_len, passphrase, salt, salt_len, iterations);
100 return out;
101}
102
104 std::string_view passphrase,
105 const uint8_t salt[],
106 size_t salt_len,
107 std::chrono::milliseconds msec,
108 size_t& iterations) const {
109 secure_vector<uint8_t> out(out_len);
110 pbkdf_timed(out.data(), out_len, passphrase, salt, salt_len, msec, iterations);
111 return out;
112}
113
114} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:95
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:112
static std::unique_ptr< MessageAuthenticationCode > create(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:51
static std::vector< std::string > providers(std::string_view algo_spec)
Definition pbkdf.cpp:67
static std::unique_ptr< PBKDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition pbkdf.cpp:60
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:81
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:71
virtual std::string name() const =0
static std::unique_ptr< PBKDF > create(std::string_view algo_spec, std::string_view provider="")
Definition pbkdf.cpp:26
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128