/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 | | /** |
26 | | * Derive a new key under the current Scrypt parameter set |
27 | | */ |
28 | | void derive_key(uint8_t out[], |
29 | | size_t out_len, |
30 | | const char* password, |
31 | | size_t password_len, |
32 | | const uint8_t salt[], |
33 | | size_t salt_len) const override; |
34 | | |
35 | | std::string to_string() const override; |
36 | | |
37 | 0 | size_t iterations() const override { return m_r; } |
38 | | |
39 | 0 | size_t parallelism() const override { return m_p; } |
40 | | |
41 | 0 | size_t memory_param() const override { return m_N; } |
42 | | |
43 | | size_t total_memory_usage() const override; |
44 | | |
45 | | private: |
46 | | size_t m_N, m_r, m_p; |
47 | | }; |
48 | | |
49 | | class BOTAN_PUBLIC_API(2, 8) Scrypt_Family final : public PasswordHashFamily { |
50 | | public: |
51 | | std::string name() const override; |
52 | | |
53 | | std::unique_ptr<PasswordHash> tune(size_t output_length, |
54 | | std::chrono::milliseconds msec, |
55 | | size_t max_memory, |
56 | | std::chrono::milliseconds tune_msec) const override; |
57 | | |
58 | | std::unique_ptr<PasswordHash> default_params() const override; |
59 | | |
60 | | std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override; |
61 | | |
62 | | std::unique_ptr<PasswordHash> from_params(size_t N, size_t r, size_t p) const override; |
63 | | }; |
64 | | |
65 | | /** |
66 | | * Scrypt key derivation function (RFC 7914) |
67 | | * |
68 | | * @param output the output will be placed here |
69 | | * @param output_len length of output |
70 | | * @param password the user password |
71 | | * @param password_len length of password |
72 | | * @param salt the salt |
73 | | * @param salt_len length of salt |
74 | | * @param N the CPU/Memory cost parameter, must be power of 2 |
75 | | * @param r the block size parameter |
76 | | * @param p the parallelization parameter |
77 | | * |
78 | | * Suitable parameters for most uses would be N = 32768, r = 8, p = 1 |
79 | | * |
80 | | * Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory |
81 | | */ |
82 | | BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash") |
83 | | inline void scrypt(uint8_t output[], |
84 | | size_t output_len, |
85 | | const char* password, |
86 | | size_t password_len, |
87 | | const uint8_t salt[], |
88 | | size_t salt_len, |
89 | | size_t N, |
90 | | size_t r, |
91 | 0 | size_t p) { |
92 | 0 | auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt"); |
93 | 0 | auto pwdhash = pwdhash_fam->from_params(N, r, p); |
94 | 0 | pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len); |
95 | 0 | } |
96 | | |
97 | | /** |
98 | | * Scrypt key derivation function (RFC 7914) |
99 | | * Before 2.8 this function was the primary interface for scrypt |
100 | | * |
101 | | * @param output the output will be placed here |
102 | | * @param output_len length of output |
103 | | * @param password the user password |
104 | | * @param salt the salt |
105 | | * @param salt_len length of salt |
106 | | * @param N the CPU/Memory cost parameter, must be power of 2 |
107 | | * @param r the block size parameter |
108 | | * @param p the parallelization parameter |
109 | | * |
110 | | * Suitable parameters for most uses would be N = 32768, r = 8, p = 1 |
111 | | * |
112 | | * Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory |
113 | | */ |
114 | | BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash") |
115 | | inline void scrypt(uint8_t output[], |
116 | | size_t output_len, |
117 | | std::string_view password, |
118 | | const uint8_t salt[], |
119 | | size_t salt_len, |
120 | | size_t N, |
121 | | size_t r, |
122 | 0 | size_t p) { |
123 | 0 | auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt"); |
124 | 0 | auto pwdhash = pwdhash_fam->from_params(N, r, p); |
125 | 0 | pwdhash->derive_key(output, output_len, password.data(), password.size(), salt, salt_len); |
126 | 0 | } |
127 | | |
128 | | } // namespace Botan |
129 | | |
130 | | #endif |