Botan  2.15.0
Crypto and TLS for C++11
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 */
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 
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
size_t p() const
Definition: scrypt.h:39
int(* final)(unsigned char *, CTX *)
size_t r() const
Definition: scrypt.h:38
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: bigint.h:1142
size_t parallelism() const override
Definition: scrypt.h:43
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
size_t scrypt_memory_usage(size_t N, size_t r, size_t p)
Definition: scrypt.h:120
std::string name
size_t N() const
Definition: scrypt.h:37
size_t salt_len
Definition: x509_obj.cpp:25
Definition: alg_id.cpp:13
size_t memory_param() const override
Definition: scrypt.h:45
size_t iterations() const override
Definition: scrypt.h:41
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.cpp:211