• 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

52.38
/src/lib/pbkdf/pbkdf.cpp
1
/*
2
* PBKDF
3
* (C) 2012 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/pbkdf.h>
9

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
namespace Botan {
22

23
std::unique_ptr<PBKDF> PBKDF::create(std::string_view algo_spec, std::string_view provider) {
30✔
24
   const SCAN_Name req(algo_spec);
30✔
25

26
#if defined(BOTAN_HAS_PBKDF2)
27
   if(req.algo_name() == "PBKDF2") {
60✔
28
      // TODO OpenSSL
29

30
      if(provider.empty() || provider == "base") {
17✔
31
         if(auto mac = MessageAuthenticationCode::create("HMAC(" + req.arg(0) + ")"))
34✔
32
            return std::make_unique<PKCS5_PBKDF2>(std::move(mac));
2✔
33

34
         if(auto mac = MessageAuthenticationCode::create(req.arg(0)))
30✔
35
            return std::make_unique<PKCS5_PBKDF2>(std::move(mac));
15✔
36
      }
37

38
      return nullptr;
×
39
   }
40
#endif
41

42
#if defined(BOTAN_HAS_PGP_S2K)
43
   if(req.algo_name() == "OpenPGP-S2K" && req.arg_count() == 1) {
39✔
44
      if(auto hash = HashFunction::create(req.arg(0)))
26✔
45
         return std::make_unique<OpenPGP_S2K>(std::move(hash));
13✔
46
   }
47
#endif
48

49
   BOTAN_UNUSED(req);
×
50
   BOTAN_UNUSED(provider);
×
51

52
   return nullptr;
×
53
}
30✔
54

55
//static
56
std::unique_ptr<PBKDF> PBKDF::create_or_throw(std::string_view algo, std::string_view provider) {
×
57
   if(auto pbkdf = PBKDF::create(algo, provider)) {
×
58
      return pbkdf;
×
59
   }
×
60
   throw Lookup_Error("PBKDF", algo, provider);
×
61
}
62

63
std::vector<std::string> PBKDF::providers(std::string_view algo_spec) { return probe_providers_of<PBKDF>(algo_spec); }
×
64

65
void PBKDF::pbkdf_timed(uint8_t out[],
×
66
                        size_t out_len,
67
                        std::string_view passphrase,
68
                        const uint8_t salt[],
69
                        size_t salt_len,
70
                        std::chrono::milliseconds msec,
71
                        size_t& iterations) const {
72
   iterations = pbkdf(out, out_len, passphrase, salt, salt_len, 0, msec);
×
73
}
×
74

75
void PBKDF::pbkdf_iterations(uint8_t out[],
79✔
76
                             size_t out_len,
77
                             std::string_view passphrase,
78
                             const uint8_t salt[],
79
                             size_t salt_len,
80
                             size_t iterations) const {
81
   if(iterations == 0)
79✔
82
      throw Invalid_Argument(name() + ": Invalid iteration count");
×
83

84
   const size_t iterations_run =
79✔
85
      pbkdf(out, out_len, passphrase, salt, salt_len, iterations, std::chrono::milliseconds(0));
79✔
86
   BOTAN_ASSERT_EQUAL(iterations, iterations_run, "Expected PBKDF iterations");
79✔
87
}
79✔
88

89
secure_vector<uint8_t> PBKDF::pbkdf_iterations(
79✔
90
   size_t out_len, std::string_view passphrase, const uint8_t salt[], size_t salt_len, size_t iterations) const {
91
   secure_vector<uint8_t> out(out_len);
79✔
92
   pbkdf_iterations(out.data(), out_len, passphrase, salt, salt_len, iterations);
79✔
93
   return out;
79✔
94
}
×
95

96
secure_vector<uint8_t> PBKDF::pbkdf_timed(size_t out_len,
×
97
                                          std::string_view passphrase,
98
                                          const uint8_t salt[],
99
                                          size_t salt_len,
100
                                          std::chrono::milliseconds msec,
101
                                          size_t& iterations) const {
102
   secure_vector<uint8_t> out(out_len);
×
103
   pbkdf_timed(out.data(), out_len, passphrase, salt, salt_len, msec, iterations);
×
104
   return out;
×
105
}
×
106

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