Botan 3.9.0
Crypto and TLS for C&
scrypt.h
Go to the documentation of this file.
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
15
16namespace Botan {
17
18/**
19* Scrypt key derivation function (RFC 7914)
20*/
21class 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 size_t iterations() const override { return m_r; }
38
39 size_t parallelism() const override { return m_p; }
40
41 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
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*/
82BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
83inline 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 size_t p) {
92 auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
93 auto pwdhash = pwdhash_fam->from_params(N, r, p);
94 pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len);
95}
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*/
114BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
115inline 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 size_t p) {
123 auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
124 auto pwdhash = pwdhash_fam->from_params(N, r, p);
125 pwdhash->derive_key(output, output_len, password.data(), password.size(), salt, salt_len);
126}
127
128} // namespace Botan
129
130#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition api.h:98
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
static std::unique_ptr< PasswordHashFamily > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:110
virtual size_t total_memory_usage() const
Definition pwdhash.h:59
virtual void derive_key(uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len) const =0
virtual std::string to_string() const =0
std::unique_ptr< PasswordHash > from_params(size_t N, size_t r, size_t p) const override
Definition scrypt.cpp:104
std::unique_ptr< PasswordHash > tune(size_t output_length, std::chrono::milliseconds msec, size_t max_memory, std::chrono::milliseconds tune_msec) const override
Definition scrypt.cpp:37
std::unique_ptr< PasswordHash > from_iterations(size_t iter) const override
Definition scrypt.cpp:108
std::string name() const override
Definition scrypt.cpp:29
std::unique_ptr< PasswordHash > default_params() const override
Definition scrypt.cpp:33
size_t memory_param() const override
Definition scrypt.h:41
size_t iterations() const override
Definition scrypt.h:37
size_t parallelism() const override
Definition scrypt.h:39
Scrypt(size_t N, size_t r, size_t p)
Definition scrypt.cpp:127
void scrypt(uint8_t output[], size_t output_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len, size_t N, size_t r, size_t p)
Definition scrypt.h:83