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

randombit / botan / 12993346889

27 Jan 2025 04:15PM UTC coverage: 91.248% (+0.002%) from 91.246%
12993346889

push

github

butteronarchbtw
add (s)afi encoding & decoding

94261 of 103302 relevant lines covered (91.25%)

11708235.22 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/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

53
std::unique_ptr<PasswordHashFamily> PasswordHashFamily::create(std::string_view algo_spec, std::string_view provider) {
2,296✔
54
   const SCAN_Name req(algo_spec);
2,296✔
55

56
#if defined(BOTAN_HAS_PBKDF2)
57
   if(req.algo_name() == "PBKDF2") {
2,296✔
58
      if(provider.empty() || provider == "base") {
658✔
59
         if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")")) {
2,632✔
60
            return std::make_unique<PBKDF2_Family>(std::move(mac));
30✔
61
         }
30✔
62

63
         if(auto mac = MessageAuthenticationCode::create(req.arg(0))) {
1,256✔
64
            return std::make_unique<PBKDF2_Family>(std::move(mac));
628✔
65
         }
628✔
66
      }
67

68
      return nullptr;
×
69
   }
70
#endif
71

72
#if defined(BOTAN_HAS_SCRYPT)
73
   if(req.algo_name() == "Scrypt") {
1,638✔
74
      return std::make_unique<Scrypt_Family>();
495✔
75
   }
76
#endif
77

78
#if defined(BOTAN_HAS_ARGON2)
79
   if(req.algo_name() == "Argon2d") {
1,143✔
80
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(0));
40✔
81
   } else if(req.algo_name() == "Argon2i") {
1,103✔
82
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(1));
49✔
83
   } else if(req.algo_name() == "Argon2id") {
1,054✔
84
      return std::make_unique<Argon2_Family>(static_cast<uint8_t>(2));
1,003✔
85
   }
86
#endif
87

88
#if defined(BOTAN_HAS_PBKDF_BCRYPT)
89
   if(req.algo_name() == "Bcrypt-PBKDF") {
51✔
90
      return std::make_unique<Bcrypt_PBKDF_Family>();
37✔
91
   }
92
#endif
93

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

102
   BOTAN_UNUSED(req);
×
103
   BOTAN_UNUSED(provider);
×
104

105
   return nullptr;
×
106
}
2,296✔
107

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

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

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