Botan 3.14.1
Crypto and TLS for C&
pwdhash.cpp
Go to the documentation of this file.
1/*
2* (C) 2018 Ribose Inc
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/pwdhash.h>
8
9#include <botan/assert.h>
10#include <botan/exceptn.h>
11#include <botan/internal/algorithm_spec.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/probe_providers.h>
14
15#if defined(BOTAN_HAS_PBKDF2)
16 #include <botan/pbkdf2.h>
17#endif
18
19#if defined(BOTAN_HAS_PGP_S2K)
20 #include <botan/pgp_s2k.h>
21#endif
22
23#if defined(BOTAN_HAS_SCRYPT)
24 #include <botan/scrypt.h>
25#endif
26
27#if defined(BOTAN_HAS_ARGON2)
28 #include <botan/argon2.h>
29#endif
30
31#if defined(BOTAN_HAS_PBKDF_BCRYPT)
32 #include <botan/bcrypt_pbkdf.h>
33#endif
34
35#if defined(BOTAN_HAS_PKCS12_KDF)
36 #include <botan/internal/pkcs12_kdf.h>
37#endif
38
39namespace Botan {
40
41void PasswordHash::derive_key(uint8_t out[],
42 size_t out_len,
43 const char* password,
44 size_t password_len,
45 const uint8_t salt[],
46 size_t salt_len,
47 const uint8_t ad[],
48 size_t ad_len,
49 const uint8_t key[],
50 size_t key_len) const {
51 BOTAN_UNUSED(ad, key);
52
53 if(ad_len == 0 && key_len == 0) {
54 return this->derive_key(out, out_len, password, password_len, salt, salt_len);
55 } else {
56 throw Not_Implemented("PasswordHash " + this->to_string() + " does not support AD or key");
57 }
58}
59
60std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create(std::string_view algo_spec, std::string_view provider) {
61 const AlgorithmSpec req(algo_spec);
62
63#if defined(BOTAN_HAS_PBKDF2)
64 if(auto m = req.match("PBKDF2({prf})")) {
65 if(provider.empty() || provider == "base") {
66 const auto prf = m->str("prf");
67 if(auto mac = MessageAuthenticationCode::create(fmt("HMAC({})", prf))) {
68 return std::make_unique<PBKDF2_Family>(std::move(mac));
69 }
70
71 if(auto mac = MessageAuthenticationCode::create(prf)) {
72 return std::make_unique<PBKDF2_Family>(std::move(mac));
73 }
74 }
75
76 return nullptr;
77 }
78#endif
79
80#if defined(BOTAN_HAS_SCRYPT)
81 if(req.matches("Scrypt")) {
82 return std::make_unique<Scrypt_Family>();
83 }
84#endif
85
86#if defined(BOTAN_HAS_ARGON2)
87 if(req.matches("Argon2d")) {
88 return std::make_unique<Argon2_Family>(static_cast<uint8_t>(0));
89 } else if(req.matches("Argon2i")) {
90 return std::make_unique<Argon2_Family>(static_cast<uint8_t>(1));
91 } else if(req.matches("Argon2id")) {
92 return std::make_unique<Argon2_Family>(static_cast<uint8_t>(2));
93 }
94#endif
95
96#if defined(BOTAN_HAS_PBKDF_BCRYPT)
97 if(req.matches("Bcrypt-PBKDF")) {
98 return std::make_unique<Bcrypt_PBKDF_Family>();
99 }
100#endif
101
102#if defined(BOTAN_HAS_PGP_S2K)
103 if(auto m = req.match("OpenPGP-S2K({hash})")) {
104 if(auto hash = HashFunction::create(m->str("hash"))) {
105 return std::make_unique<RFC4880_S2K_Family>(std::move(hash));
106 }
107 }
108#endif
109
110#if defined(BOTAN_HAS_PKCS12_KDF)
111 if(auto m = req.match("PKCS12-KDF({hash},{id:int})")) {
112 if(auto hash = HashFunction::create(m->str("hash"))) {
113 return std::make_unique<PKCS12_KDF_Family>(std::move(hash), m->integer("id"));
114 }
115 }
116#endif
117
118 BOTAN_UNUSED(req);
119 BOTAN_UNUSED(provider);
120
121 return nullptr;
122}
123
124//static
125std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create_or_throw(std::string_view algo,
126 std::string_view provider) {
127 if(auto pbkdf = PasswordHashFamily::create(algo, provider)) {
128 return pbkdf;
129 }
130 throw Lookup_Error("PasswordHashFamily", algo, provider);
131}
132
133std::vector<std::string> PasswordHashFamily::providers(std::string_view algo_spec) {
135}
136
137} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
bool matches(const AlgorithmPattern &pattern) const
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::unique_ptr< PasswordHashFamily > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:125
static std::vector< std::string > providers(std::string_view algo_spec)
Definition pwdhash.cpp:133
static std::unique_ptr< PasswordHashFamily > create(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:60
virtual void derive_key(uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len) const =0
virtual std::string to_string() const =0
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"})