Botan  2.9.0
Crypto and TLS for C++11
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/pbkdf.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 namespace Botan {
24 
25 std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create(const std::string& algo_spec,
26  const std::string& provider)
27  {
28  const SCAN_Name req(algo_spec);
29 
30 #if defined(BOTAN_HAS_PBKDF2)
31  if(req.algo_name() == "PBKDF2")
32  {
33  // TODO OpenSSL
34 
35  if(provider.empty() || provider == "base")
36  {
37  if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
38  return std::unique_ptr<PasswordHashFamily>(new PBKDF2_Family(mac.release()));
39 
40  if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")"))
41  return std::unique_ptr<PasswordHashFamily>(new PBKDF2_Family(mac.release()));
42  }
43 
44  return nullptr;
45  }
46 #endif
47 
48 #if defined(BOTAN_HAS_SCRYPT)
49  if(req.algo_name() == "Scrypt")
50  {
51  return std::unique_ptr<PasswordHashFamily>(new Scrypt_Family);
52  }
53 #endif
54 
55 #if defined(BOTAN_HAS_PGP_S2K)
56  if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1)
57  {
58  if(auto hash = HashFunction::create(req.arg(0)))
59  {
60  return std::unique_ptr<PasswordHashFamily>(new RFC4880_S2K_Family(hash.release()));
61  }
62  }
63 #endif
64 
65  BOTAN_UNUSED(req);
66  BOTAN_UNUSED(provider);
67 
68  return nullptr;
69  }
70 
71 //static
72 std::unique_ptr<PasswordHashFamily>
73 PasswordHashFamily::create_or_throw(const std::string& algo,
74  const std::string& provider)
75  {
76  if(auto pbkdf = PasswordHashFamily::create(algo, provider))
77  {
78  return pbkdf;
79  }
80  throw Lookup_Error("PasswordHashFamily", algo, provider);
81  }
82 
83 std::vector<std::string> PasswordHashFamily::providers(const std::string& algo_spec)
84  {
85  return probe_providers_of<PasswordHashFamily>(algo_spec, { "base", "openssl" });
86  }
87 
88 }
size_t arg_count() const
Definition: scan_name.h:49
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:83
static std::unique_ptr< PasswordHashFamily > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: pwdhash.cpp:73
std::string arg(size_t i) const
Definition: scan_name.cpp:122
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:110
Definition: alg_id.cpp:13
static std::unique_ptr< PasswordHashFamily > create(const std::string &algo_spec, const std::string &provider="")
Definition: pwdhash.cpp:25
#define BOTAN_UNUSED(...)
Definition: assert.h:142
const std::string & algo_name() const
Definition: scan_name.h:44
MechanismType hash