Botan 3.6.1
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")
86inline 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 size_t p) {
95 auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
96 auto pwdhash = pwdhash_fam->from_params(N, r, p);
97 pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len);
98}
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*/
117BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
118inline 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 size_t p) {
126 auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
127 auto pwdhash = pwdhash_fam->from_params(N, r, p);
128 pwdhash->derive_key(output, output_len, password.data(), password.size(), salt, salt_len);
129}
130
131} // namespace Botan
132
133#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:86