Botan 3.4.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*/
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 size_t iterations() const override { return m_r; }
41
42 size_t parallelism() const override { return m_p; }
43
44 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
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*/
85BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
86
87inline void scrypt(uint8_t output[],
88 size_t output_len,
89 const char* password,
90 size_t password_len,
91 const uint8_t salt[],
92 size_t salt_len,
93 size_t N,
94 size_t r,
95 size_t p) {
96 auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
97 auto pwdhash = pwdhash_fam->from_params(N, r, p);
98 pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len);
99}
100
101/**
102* Scrypt key derivation function (RFC 7914)
103* Before 2.8 this function was the primary interface for scrypt
104*
105* @param output the output will be placed here
106* @param output_len length of output
107* @param password the user password
108* @param salt the salt
109* @param salt_len length of salt
110* @param N the CPU/Memory cost parameter, must be power of 2
111* @param r the block size parameter
112* @param p the parallelization parameter
113*
114* Suitable parameters for most uses would be N = 32768, r = 8, p = 1
115*
116* Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory
117*/
118BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
119
120inline void scrypt(uint8_t output[],
121 size_t output_len,
122 std::string_view password,
123 const uint8_t salt[],
124 size_t salt_len,
125 size_t N,
126 size_t r,
127 size_t p) {
128 auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
129 auto pwdhash = pwdhash_fam->from_params(N, r, p);
130 pwdhash->derive_key(output, output_len, password.data(), password.size(), salt, salt_len);
131}
132
133} // namespace Botan
134
135#endif
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
Scrypt & operator=(const Scrypt &)=default
size_t memory_param() const override
Definition scrypt.h:44
Scrypt(const Scrypt &other)=default
size_t iterations() const override
Definition scrypt.h:40
size_t parallelism() const override
Definition scrypt.h:42
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition compiler.h:150
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
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:87