Coverage Report

Created: 2025-04-11 06:34

/src/botan/build/include/public/botan/scrypt.h
Line
Count
Source (jump to first uncovered line)
1
/**
2
* (C) 2018 Jack Lloyd
3
* (C) 2018 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_SCRYPT_H_
9
#define BOTAN_SCRYPT_H_
10
11
#include <botan/pwdhash.h>
12
13
// Use pwdhash.h
14
BOTAN_FUTURE_INTERNAL_HEADER(scrypt.h)
15
16
namespace Botan {
17
18
/**
19
* Scrypt key derivation function (RFC 7914)
20
*/
21
class BOTAN_PUBLIC_API(2, 8) Scrypt final : public PasswordHash {
22
   public:
23
      Scrypt(size_t N, size_t r, size_t p);
24
25
      Scrypt(const Scrypt& other) = default;
26
      Scrypt& operator=(const Scrypt&) = default;
27
28
      /**
29
      * Derive a new key under the current Scrypt parameter set
30
      */
31
      void derive_key(uint8_t out[],
32
                      size_t out_len,
33
                      const char* password,
34
                      size_t password_len,
35
                      const uint8_t salt[],
36
                      size_t salt_len) const override;
37
38
      std::string to_string() const override;
39
40
0
      size_t iterations() const override { return m_r; }
41
42
0
      size_t parallelism() const override { return m_p; }
43
44
0
      size_t memory_param() const override { return m_N; }
45
46
      size_t total_memory_usage() const override;
47
48
   private:
49
      size_t m_N, m_r, m_p;
50
};
51
52
class BOTAN_PUBLIC_API(2, 8) Scrypt_Family final : public PasswordHashFamily {
53
   public:
54
      std::string name() const override;
55
56
      std::unique_ptr<PasswordHash> tune(size_t output_length,
57
                                         std::chrono::milliseconds msec,
58
                                         size_t max_memory,
59
                                         std::chrono::milliseconds tune_msec) const override;
60
61
      std::unique_ptr<PasswordHash> default_params() const override;
62
63
      std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
64
65
      std::unique_ptr<PasswordHash> from_params(size_t N, size_t r, size_t p) const override;
66
};
67
68
/**
69
* Scrypt key derivation function (RFC 7914)
70
*
71
* @param output the output will be placed here
72
* @param output_len length of output
73
* @param password the user password
74
* @param password_len length of password
75
* @param salt the salt
76
* @param salt_len length of salt
77
* @param N the CPU/Memory cost parameter, must be power of 2
78
* @param r the block size parameter
79
* @param p the parallelization parameter
80
*
81
* Suitable parameters for most uses would be N = 32768, r = 8, p = 1
82
*
83
* Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory
84
*/
85
BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
86
inline void scrypt(uint8_t output[],
87
                   size_t output_len,
88
                   const char* password,
89
                   size_t password_len,
90
                   const uint8_t salt[],
91
                   size_t salt_len,
92
                   size_t N,
93
                   size_t r,
94
0
                   size_t p) {
95
0
   auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
96
0
   auto pwdhash = pwdhash_fam->from_params(N, r, p);
97
0
   pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len);
98
0
}
99
100
/**
101
* Scrypt key derivation function (RFC 7914)
102
* Before 2.8 this function was the primary interface for scrypt
103
*
104
* @param output the output will be placed here
105
* @param output_len length of output
106
* @param password the user password
107
* @param salt the salt
108
* @param salt_len length of salt
109
* @param N the CPU/Memory cost parameter, must be power of 2
110
* @param r the block size parameter
111
* @param p the parallelization parameter
112
*
113
* Suitable parameters for most uses would be N = 32768, r = 8, p = 1
114
*
115
* Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory
116
*/
117
BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
118
inline void scrypt(uint8_t output[],
119
                   size_t output_len,
120
                   std::string_view password,
121
                   const uint8_t salt[],
122
                   size_t salt_len,
123
                   size_t N,
124
                   size_t r,
125
0
                   size_t p) {
126
0
   auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
127
0
   auto pwdhash = pwdhash_fam->from_params(N, r, p);
128
0
   pwdhash->derive_key(output, output_len, password.data(), password.size(), salt, salt_len);
129
0
}
130
131
}  // namespace Botan
132
133
#endif