Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 BOTAN_FUTURE_INTERNAL_HEADER(scrypt.h)
14
15 namespace Botan {
16
17 /**
18 * Scrypt key derivation function (RFC 7914)
19 */
20 class BOTAN_PUBLIC_API(2,8) Scrypt final : public PasswordHash
21 {
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[], size_t out_len,
32 const char* password, size_t password_len,
33 const uint8_t salt[], size_t salt_len) const override;
34
35 std::string to_string() const override;
36
37 size_t N() const { return m_N; }
38 size_t r() const { return m_r; }
39 size_t p() const { return m_p; }
40
41 size_t iterations() const override { return r(); }
42
43 size_t parallelism() const override { return p(); }
44
45 size_t memory_param() const override { return N(); }
46
47 size_t total_memory_usage() const override;
48
49 private:
50 size_t m_N, m_r, m_p;
51 };
52
53 class BOTAN_PUBLIC_API(2,8) Scrypt_Family final : public PasswordHashFamily
54 {
55 public:
56 std::string name() const override;
57
58 std::unique_ptr<PasswordHash> tune(size_t output_length,
59 std::chrono::milliseconds msec,
60 size_t max_memory) const override;
61
62 std::unique_ptr<PasswordHash> default_params() const override;
63
64 std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
65
66 std::unique_ptr<PasswordHash> from_params(
67 size_t N, size_t r, size_t p) const override;
68 };
69
70 /**
71 * Scrypt key derivation function (RFC 7914)
72 *
73 * @param output the output will be placed here
74 * @param output_len length of output
75 * @param password the user password
76 * @param password_len length of password
77 * @param salt the salt
78 * @param salt_len length of salt
79 * @param N the CPU/Memory cost parameter, must be power of 2
80 * @param r the block size parameter
81 * @param p the parallelization parameter
82 *
83 * Suitable parameters for most uses would be N = 32768, r = 8, p = 1
84 *
85 * Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory
86 */
87 void BOTAN_PUBLIC_API(2,8) scrypt(uint8_t output[], size_t output_len,
88 const char* password, size_t password_len,
89 const uint8_t salt[], size_t salt_len,
90 size_t N, size_t r, size_t p);
91
92 /**
93 * Scrypt key derivation function (RFC 7914)
94 * Before 2.8 this function was the primary interface for scrypt
95 *
96 * @param output the output will be placed here
97 * @param output_len length of output
98 * @param password the user password
99 * @param salt the salt
100 * @param salt_len length of salt
101 * @param N the CPU/Memory cost parameter, must be power of 2
102 * @param r the block size parameter
103 * @param p the parallelization parameter
104 *
105 * Suitable parameters for most uses would be N = 32768, r = 8, p = 1
106 *
107 * Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory
108 */
109 inline void scrypt(uint8_t output[], size_t output_len,
110 const std::string& password,
111 const uint8_t salt[], size_t salt_len,
112 size_t N, size_t r, size_t p)
113 {
114 return scrypt(output, output_len,
115 password.c_str(), password.size(),
116 salt, salt_len,
117 N, r, p);
118 }
119
120 inline size_t scrypt_memory_usage(size_t N, size_t r, size_t p)
121 {
122 return 128 * r * (N + p);
123 }
124
125 }
126
127 #endif
Botan::scrypt
void scrypt(uint8_t output[], size_t output_len, const std::string &password, const uint8_t salt[], size_t salt_len, size_t N, size_t r, size_t p)
Definition: scrypt.h:109
Botan::PasswordHashFamily::tune
virtual std::unique_ptr< PasswordHash > tune(size_t output_length, std::chrono::milliseconds msec, size_t max_memory_usage_mb=0) const =0
Botan::PasswordHash::derive_key
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
m_r
BigInt m_r
Definition: curve_gfp.cpp:87
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:28
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
std
Definition: bigint.h:1135
Botan::Scrypt::parallelism
size_t parallelism() const override
Definition: scrypt.h:43
Botan::Scrypt
Definition: scrypt.h:20
Botan::PasswordHash::total_memory_usage
virtual size_t total_memory_usage() const
Definition: pwdhash.h:59
Botan::scrypt_memory_usage
size_t scrypt_memory_usage(size_t N, size_t r, size_t p)
Definition: scrypt.h:120
Botan::PasswordHashFamily::from_iterations
virtual std::unique_ptr< PasswordHash > from_iterations(size_t iterations) const =0
Botan::PasswordHash
Definition: pwdhash.h:24
Botan::Scrypt::r
size_t r() const
Definition: scrypt.h:38
Botan::PasswordHash::to_string
virtual std::string to_string() const =0
Botan::PasswordHashFamily
Definition: pwdhash.h:79
salt_len
size_t salt_len
Definition: x509_obj.cpp:25
Botan
Definition: alg_id.cpp:13
Botan::Scrypt::N
size_t N() const
Definition: scrypt.h:37
Botan::Scrypt_Family
Definition: scrypt.h:53
Botan::Scrypt::memory_param
size_t memory_param() const override
Definition: scrypt.h:45
Botan::Scrypt::iterations
size_t iterations() const override
Definition: scrypt.h:41
m_p
BigInt m_p
Definition: pow_mod.cpp:108
Botan::PasswordHashFamily::default_params
virtual std::unique_ptr< PasswordHash > default_params() const =0
Botan::PasswordHashFamily::name
virtual std::string name() const =0
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:139
Botan::Scrypt::p
size_t p() const
Definition: scrypt.h:39
Botan::PasswordHashFamily::from_params
virtual std::unique_ptr< PasswordHash > from_params(size_t i1, size_t i2=0, size_t i3=0) const =0