Botan 2.19.3
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#include <botan/exceptn.h>
9#include <botan/scan_name.h>
10
11#if defined(BOTAN_HAS_PBKDF2)
12 #include <botan/pbkdf2.h>
13#endif
14
15#if defined(BOTAN_HAS_PGP_S2K)
16 #include <botan/pgp_s2k.h>
17#endif
18
19#if defined(BOTAN_HAS_SCRYPT)
20 #include <botan/scrypt.h>
21#endif
22
23#if defined(BOTAN_HAS_ARGON2)
24 #include <botan/argon2.h>
25#endif
26
27#if defined(BOTAN_HAS_PBKDF_BCRYPT)
28 #include <botan/bcrypt_pbkdf.h>
29#endif
30
31namespace Botan {
32
33std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create(const std::string& algo_spec,
34 const std::string& provider)
35 {
36 const SCAN_Name req(algo_spec);
37
38#if defined(BOTAN_HAS_PBKDF2)
39 if(req.algo_name() == "PBKDF2")
40 {
41 if(provider.empty() || provider == "base")
42 {
43 if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
44 return std::unique_ptr<PasswordHashFamily>(new PBKDF2_Family(mac.release()));
45
46 if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")"))
47 return std::unique_ptr<PasswordHashFamily>(new PBKDF2_Family(mac.release()));
48 }
49
50 return nullptr;
51 }
52#endif
53
54#if defined(BOTAN_HAS_SCRYPT)
55 if(req.algo_name() == "Scrypt")
56 {
57 return std::unique_ptr<PasswordHashFamily>(new Scrypt_Family);
58 }
59#endif
60
61#if defined(BOTAN_HAS_ARGON2)
62 if(req.algo_name() == "Argon2d")
63 {
64 return std::unique_ptr<PasswordHashFamily>(new Argon2_Family(0));
65 }
66 else if(req.algo_name() == "Argon2i")
67 {
68 return std::unique_ptr<PasswordHashFamily>(new Argon2_Family(1));
69 }
70 else if(req.algo_name() == "Argon2id")
71 {
72 return std::unique_ptr<PasswordHashFamily>(new Argon2_Family(2));
73 }
74#endif
75
76#if defined(BOTAN_HAS_PBKDF_BCRYPT)
77 if(req.algo_name() == "Bcrypt-PBKDF")
78 {
79 return std::unique_ptr<PasswordHashFamily>(new Bcrypt_PBKDF_Family);
80 }
81#endif
82
83#if defined(BOTAN_HAS_PGP_S2K)
84 if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1)
85 {
86 if(auto hash = HashFunction::create(req.arg(0)))
87 {
88 return std::unique_ptr<PasswordHashFamily>(new RFC4880_S2K_Family(hash.release()));
89 }
90 }
91#endif
92
93 BOTAN_UNUSED(req);
94 BOTAN_UNUSED(provider);
95
96 return nullptr;
97 }
98
99//static
100std::unique_ptr<PasswordHashFamily>
102 const std::string& provider)
103 {
104 if(auto pbkdf = PasswordHashFamily::create(algo, provider))
105 {
106 return pbkdf;
107 }
108 throw Lookup_Error("PasswordHashFamily", algo, provider);
109 }
110
111std::vector<std::string> PasswordHashFamily::providers(const std::string& algo_spec)
112 {
113 return probe_providers_of<PasswordHashFamily>(algo_spec, { "base", "openssl" });
114 }
115
116}
#define BOTAN_UNUSED(...)
Definition assert.h:142
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition hash.cpp:102
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition mac.cpp:46
static std::vector< std::string > providers(const std::string &algo_spec)
Definition pwdhash.cpp:111
static std::unique_ptr< PasswordHashFamily > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition pwdhash.cpp:101
static std::unique_ptr< PasswordHashFamily > create(const std::string &algo_spec, const std::string &provider="")
Definition pwdhash.cpp:33
std::string arg(size_t i) const
size_t arg_count() const
Definition scan_name.h:56
const std::string & algo_name() const
Definition scan_name.h:51
MechanismType hash