Botan 3.7.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/scan_name.h>
13
14#if defined(BOTAN_HAS_PBKDF2)
15 #include <botan/pbkdf2.h>
16#endif
17
18#if defined(BOTAN_HAS_PGP_S2K)
19 #include <botan/pgp_s2k.h>
20#endif
21
22namespace Botan {
23
24std::unique_ptr<PBKDF> PBKDF::create(std::string_view algo_spec, std::string_view provider) {
25 const SCAN_Name req(algo_spec);
26
27#if defined(BOTAN_HAS_PBKDF2)
28 if(req.algo_name() == "PBKDF2") {
29 if(provider.empty() || provider == "base") {
30 if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")")) {
31 return std::make_unique<PKCS5_PBKDF2>(std::move(mac));
32 }
33
34 if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
35 return std::make_unique<PKCS5_PBKDF2>(std::move(mac));
36 }
37 }
38
39 return nullptr;
40 }
41#endif
42
43#if defined(BOTAN_HAS_PGP_S2K)
44 if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1) {
45 if(auto hash = HashFunction::create(req.arg(0))) {
46 return std::make_unique<OpenPGP_S2K>(std::move(hash));
47 }
48 }
49#endif
50
51 BOTAN_UNUSED(req, provider);
52
53 return nullptr;
54}
55
56//static
57std::unique_ptr<PBKDF> PBKDF::create_or_throw(std::string_view algo, std::string_view provider) {
58 if(auto pbkdf = PBKDF::create(algo, provider)) {
59 return pbkdf;
60 }
61 throw Lookup_Error("PBKDF", algo, provider);
62}
63
64std::vector<std::string> PBKDF::providers(std::string_view algo_spec) {
65 return probe_providers_of<PBKDF>(algo_spec);
66}
67
68void PBKDF::pbkdf_timed(uint8_t out[],
69 size_t out_len,
70 std::string_view passphrase,
71 const uint8_t salt[],
72 size_t salt_len,
73 std::chrono::milliseconds msec,
74 size_t& iterations) const {
75 iterations = pbkdf(out, out_len, passphrase, salt, salt_len, 0, msec);
76}
77
78void PBKDF::pbkdf_iterations(uint8_t out[],
79 size_t out_len,
80 std::string_view passphrase,
81 const uint8_t salt[],
82 size_t salt_len,
83 size_t iterations) const {
84 if(iterations == 0) {
85 throw Invalid_Argument(name() + ": Invalid iteration count");
86 }
87
88 const size_t iterations_run =
89 pbkdf(out, out_len, passphrase, salt, salt_len, iterations, std::chrono::milliseconds(0));
90 BOTAN_ASSERT_EQUAL(iterations, iterations_run, "Expected PBKDF iterations");
91}
92
94 size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const {
95 secure_vector<uint8_t> out(out_len);
96 pbkdf_iterations(out.data(), out_len, passphrase, salt, salt_len, iterations);
97 return out;
98}
99
101 std::string_view passphrase,
102 const uint8_t salt[],
103 size_t salt_len,
104 std::chrono::milliseconds msec,
105 size_t& iterations) const {
106 secure_vector<uint8_t> out(out_len);
107 pbkdf_timed(out.data(), out_len, passphrase, salt, salt_len, msec, iterations);
108 return out;
109}
110
111} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:68
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:107
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:64
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
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
std::string arg(size_t i) const
size_t arg_count() const
Definition scan_name.h:49
const std::string & algo_name() const
Definition scan_name.h:44
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
Definition scan_name.h:105
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61