/src/botan/src/lib/pbkdf/pwdhash.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
31 | | namespace Botan { |
32 | | |
33 | | std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create(const std::string& algo_spec, |
34 | | const std::string& provider) |
35 | 0 | { |
36 | 0 | const SCAN_Name req(algo_spec); |
37 | 0 |
|
38 | 0 | #if defined(BOTAN_HAS_PBKDF2) |
39 | 0 | if(req.algo_name() == "PBKDF2") |
40 | 0 | { |
41 | 0 | // TODO OpenSSL |
42 | 0 |
|
43 | 0 | if(provider.empty() || provider == "base") |
44 | 0 | { |
45 | 0 | if(auto mac = MessageAuthenticationCode::create(req.arg(0))) |
46 | 0 | return std::unique_ptr<PasswordHashFamily>(new PBKDF2_Family(mac.release())); |
47 | 0 | |
48 | 0 | if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")")) |
49 | 0 | return std::unique_ptr<PasswordHashFamily>(new PBKDF2_Family(mac.release())); |
50 | 0 | } |
51 | 0 | |
52 | 0 | return nullptr; |
53 | 0 | } |
54 | 0 | #endif |
55 | 0 | |
56 | 0 | #if defined(BOTAN_HAS_SCRYPT) |
57 | 0 | if(req.algo_name() == "Scrypt") |
58 | 0 | { |
59 | 0 | return std::unique_ptr<PasswordHashFamily>(new Scrypt_Family); |
60 | 0 | } |
61 | 0 | #endif |
62 | 0 | |
63 | 0 | #if defined(BOTAN_HAS_ARGON2) |
64 | 0 | if(req.algo_name() == "Argon2d") |
65 | 0 | { |
66 | 0 | return std::unique_ptr<PasswordHashFamily>(new Argon2_Family(0)); |
67 | 0 | } |
68 | 0 | else if(req.algo_name() == "Argon2i") |
69 | 0 | { |
70 | 0 | return std::unique_ptr<PasswordHashFamily>(new Argon2_Family(1)); |
71 | 0 | } |
72 | 0 | else if(req.algo_name() == "Argon2id") |
73 | 0 | { |
74 | 0 | return std::unique_ptr<PasswordHashFamily>(new Argon2_Family(2)); |
75 | 0 | } |
76 | 0 | #endif |
77 | 0 | |
78 | 0 | #if defined(BOTAN_HAS_PBKDF_BCRYPT) |
79 | 0 | if(req.algo_name() == "Bcrypt-PBKDF") |
80 | 0 | { |
81 | 0 | return std::unique_ptr<PasswordHashFamily>(new Bcrypt_PBKDF_Family); |
82 | 0 | } |
83 | 0 | #endif |
84 | 0 | |
85 | 0 | #if defined(BOTAN_HAS_PGP_S2K) |
86 | 0 | if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1) |
87 | 0 | { |
88 | 0 | if(auto hash = HashFunction::create(req.arg(0))) |
89 | 0 | { |
90 | 0 | return std::unique_ptr<PasswordHashFamily>(new RFC4880_S2K_Family(hash.release())); |
91 | 0 | } |
92 | 0 | } |
93 | 0 | #endif |
94 | 0 | |
95 | 0 | BOTAN_UNUSED(req); |
96 | 0 | BOTAN_UNUSED(provider); |
97 | 0 |
|
98 | 0 | return nullptr; |
99 | 0 | } |
100 | | |
101 | | //static |
102 | | std::unique_ptr<PasswordHashFamily> |
103 | | PasswordHashFamily::create_or_throw(const std::string& algo, |
104 | | const std::string& provider) |
105 | 0 | { |
106 | 0 | if(auto pbkdf = PasswordHashFamily::create(algo, provider)) |
107 | 0 | { |
108 | 0 | return pbkdf; |
109 | 0 | } |
110 | 0 | throw Lookup_Error("PasswordHashFamily", algo, provider); |
111 | 0 | } |
112 | | |
113 | | std::vector<std::string> PasswordHashFamily::providers(const std::string& algo_spec) |
114 | 0 | { |
115 | 0 | return probe_providers_of<PasswordHashFamily>(algo_spec, { "base", "openssl" }); |
116 | 0 | } |
117 | | |
118 | | } |