Coverage Report

Created: 2025-04-11 06:45

/src/botan/build/include/public/botan/bcrypt_pbkdf.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2018,2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_PBKDF_BCRYPT_H_
8
#define BOTAN_PBKDF_BCRYPT_H_
9
10
#include <botan/pwdhash.h>
11
12
// Use pwdhash.h
13
BOTAN_FUTURE_INTERNAL_HEADER(bcrypt_pbkdf.h)
14
15
namespace Botan {
16
17
/**
18
* Bcrypt-PBKDF key derivation function
19
*/
20
class BOTAN_PUBLIC_API(2, 11) Bcrypt_PBKDF final : public PasswordHash {
21
   public:
22
      Bcrypt_PBKDF(size_t iterations);
23
24
      Bcrypt_PBKDF(const Bcrypt_PBKDF& other) = default;
25
      Bcrypt_PBKDF& operator=(const Bcrypt_PBKDF&) = default;
26
27
      /**
28
      * Derive a new key under the current Bcrypt-PBKDF parameter set
29
      */
30
      void derive_key(uint8_t out[],
31
                      size_t out_len,
32
                      const char* password,
33
                      size_t password_len,
34
                      const uint8_t salt[],
35
                      size_t salt_len) const override;
36
37
      std::string to_string() const override;
38
39
0
      size_t iterations() const override { return m_iterations; }
40
41
0
      size_t parallelism() const override { return 0; }
42
43
0
      size_t memory_param() const override { return 0; }
44
45
0
      size_t total_memory_usage() const override { return 4096; }
46
47
   private:
48
      size_t m_iterations;
49
};
50
51
class BOTAN_PUBLIC_API(2, 11) Bcrypt_PBKDF_Family final : public PasswordHashFamily {
52
   public:
53
0
      Bcrypt_PBKDF_Family() = default;
54
55
      std::string name() const override;
56
57
      std::unique_ptr<PasswordHash> tune(size_t output_length,
58
                                         std::chrono::milliseconds msec,
59
                                         size_t max_memory,
60
                                         std::chrono::milliseconds tune_msec) const override;
61
62
      std::unique_ptr<PasswordHash> default_params() const override;
63
64
      std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
65
66
      std::unique_ptr<PasswordHash> from_params(size_t i, size_t, size_t) const override;
67
};
68
69
/**
70
* Bcrypt PBKDF compatible with OpenBSD bcrypt_pbkdf
71
*/
72
BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
73
74
inline void bcrypt_pbkdf(uint8_t output[],
75
                         size_t output_len,
76
                         const char* password,
77
                         size_t password_len,
78
                         const uint8_t salt[],
79
                         size_t salt_len,
80
0
                         size_t rounds) {
81
0
   auto pwdhash_fam = PasswordHashFamily::create_or_throw("Bcrypt-PBKDF");
82
0
   auto pwdhash = pwdhash_fam->from_params(rounds);
83
0
   pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len);
84
0
}
85
86
}  // namespace Botan
87
88
#endif