• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/src/lib/pbkdf/pwdhash.cpp
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/exceptn.h>
10
#include <botan/internal/scan_name.h>
11

12
#if defined(BOTAN_HAS_PBKDF2)
13
   #include <botan/pbkdf2.h>
14
#endif
15

16
#if defined(BOTAN_HAS_PGP_S2K)
17
   #include <botan/pgp_s2k.h>
18
#endif
19

20
#if defined(BOTAN_HAS_SCRYPT)
21
   #include <botan/scrypt.h>
22
#endif
23

24
#if defined(BOTAN_HAS_ARGON2)
25
   #include <botan/argon2.h>
26
#endif
27

28
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
29
   #include <botan/bcrypt_pbkdf.h>
30
#endif
31

32
namespace Botan {
33

34
void PasswordHash::derive_key(uint8_t out[],
×
35
                              size_t out_len,
36
                              const char* password,
37
                              size_t password_len,
38
                              const uint8_t salt[],
39
                              size_t salt_len,
40
                              const uint8_t ad[],
41
                              size_t ad_len,
42
                              const uint8_t key[],
43
                              size_t key_len) const {
44
   BOTAN_UNUSED(ad, key);
×
45

46
   if(ad_len == 0 && key_len == 0)
×
47
      return this->derive_key(out, out_len, password, password_len, salt, salt_len);
×
48
   else
49
      throw Not_Implemented("PasswordHash " + this->to_string() + " does not support AD or key");
×
50
}
51

52
std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create(std::string_view algo_spec, std::string_view provider) {
1,741✔
53
   const SCAN_Name req(algo_spec);
1,741✔
54

55
#if defined(BOTAN_HAS_PBKDF2)
56
   if(req.algo_name() == "PBKDF2") {
3,482✔
57
      if(provider.empty() || provider == "base") {
381✔
58
         if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")"))
762✔
59
            return std::make_unique<PBKDF2_Family>(std::move(mac));
30✔
60

61
         if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
702✔
62
            return std::make_unique<PBKDF2_Family>(std::move(mac));
351✔
63
      }
64

65
      return nullptr;
×
66
   }
67
#endif
68

69
#if defined(BOTAN_HAS_SCRYPT)
70
   if(req.algo_name() == "Scrypt") {
2,720✔
71
      return std::make_unique<Scrypt_Family>();
219✔
72
   }
73
#endif
74

75
#if defined(BOTAN_HAS_ARGON2)
76
   if(req.algo_name() == "Argon2d") {
2,282✔
77
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(0));
40✔
78
   } else if(req.algo_name() == "Argon2i") {
2,202✔
79
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(1));
49✔
80
   } else if(req.algo_name() == "Argon2id") {
2,104✔
81
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(2));
1,001✔
82
   }
83
#endif
84

85
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
86
   if(req.algo_name() == "Bcrypt-PBKDF") {
102✔
87
      return std::make_unique<Bcrypt_PBKDF_Family>();
37✔
88
   }
89
#endif
90

91
#if defined(BOTAN_HAS_PGP_S2K)
92
   if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1) {
42✔
93
      if(auto hash = HashFunction::create(req.arg(0))) {
28✔
94
         return std::make_unique<RFC4880_S2K_Family>(std::move(hash));
14✔
95
      }
14✔
96
   }
97
#endif
98

99
   BOTAN_UNUSED(req);
×
100
   BOTAN_UNUSED(provider);
×
101

102
   return nullptr;
×
103
}
1,741✔
104

105
//static
106
std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create_or_throw(std::string_view algo,
411✔
107
                                                                        std::string_view provider) {
108
   if(auto pbkdf = PasswordHashFamily::create(algo, provider)) {
411✔
109
      return pbkdf;
411✔
110
   }
411✔
111
   throw Lookup_Error("PasswordHashFamily", algo, provider);
×
112
}
113

114
std::vector<std::string> PasswordHashFamily::providers(std::string_view algo_spec) {
×
115
   return probe_providers_of<PasswordHashFamily>(algo_spec);
×
116
}
117

118
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc