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

randombit / botan / 18922739902

29 Oct 2025 09:33PM UTC coverage: 90.667% (-0.002%) from 90.669%
18922739902

Pull #5124

github

web-flow
Merge f58f26d0c into 19e477fda
Pull Request #5124: Cooperative Cancellation in PasswordHash

100478 of 110821 relevant lines covered (90.67%)

12194658.53 hits per line

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

70.73
/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/assert.h>
10
#include <botan/exceptn.h>
11
#include <botan/internal/scan_name.h>
12

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

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

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

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

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

33
namespace Botan {
34

35
void PasswordHash::derive_key(uint8_t out[],
×
36
                              size_t out_len,
37
                              const char* password,
38
                              size_t password_len,
39
                              const uint8_t salt[],
40
                              size_t salt_len,
41
                              const uint8_t ad[],
42
                              size_t ad_len,
43
                              const uint8_t key[],
44
                              size_t key_len,
45
                              [[maybe_unused]] const std::optional<std::stop_token>& stop_token) const {
46
   BOTAN_UNUSED(ad, key);
×
47

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

55
std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create(std::string_view algo_spec, std::string_view provider) {
2,310✔
56
   const SCAN_Name req(algo_spec);
2,310✔
57

58
#if defined(BOTAN_HAS_PBKDF2)
59
   if(req.algo_name() == "PBKDF2") {
2,310✔
60
      if(provider.empty() || provider == "base") {
664✔
61
         if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")")) {
2,656✔
62
            return std::make_unique<PBKDF2_Family>(std::move(mac));
31✔
63
         }
31✔
64

65
         if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
1,266✔
66
            return std::make_unique<PBKDF2_Family>(std::move(mac));
633✔
67
         }
633✔
68
      }
69

70
      return nullptr;
×
71
   }
72
#endif
73

74
#if defined(BOTAN_HAS_SCRYPT)
75
   if(req.algo_name() == "Scrypt") {
1,646✔
76
      return std::make_unique<Scrypt_Family>();
498✔
77
   }
78
#endif
79

80
#if defined(BOTAN_HAS_ARGON2)
81
   if(req.algo_name() == "Argon2d") {
1,148✔
82
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(0));
41✔
83
   } else if(req.algo_name() == "Argon2i") {
1,107✔
84
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(1));
50✔
85
   } else if(req.algo_name() == "Argon2id") {
1,057✔
86
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(2));
1,004✔
87
   }
88
#endif
89

90
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
91
   if(req.algo_name() == "Bcrypt-PBKDF") {
53✔
92
      return std::make_unique<Bcrypt_PBKDF_Family>();
38✔
93
   }
94
#endif
95

96
#if defined(BOTAN_HAS_PGP_S2K)
97
   if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1) {
15✔
98
      if(auto hash = HashFunction::create(req.arg(0))) {
30✔
99
         return std::make_unique<RFC4880_S2K_Family>(std::move(hash));
15✔
100
      }
15✔
101
   }
102
#endif
103

104
   BOTAN_UNUSED(req);
×
105
   BOTAN_UNUSED(provider);
×
106

107
   return nullptr;
×
108
}
2,310✔
109

110
//static
111
std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create_or_throw(std::string_view algo,
829✔
112
                                                                        std::string_view provider) {
113
   if(auto pbkdf = PasswordHashFamily::create(algo, provider)) {
829✔
114
      return pbkdf;
829✔
115
   }
829✔
116
   throw Lookup_Error("PasswordHashFamily", algo, provider);
×
117
}
118

119
std::vector<std::string> PasswordHashFamily::providers(std::string_view algo_spec) {
×
120
   return probe_providers_of<PasswordHashFamily>(algo_spec);
×
121
}
122

123
}  // 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

© 2026 Coveralls, Inc