Coverage Report

Created: 2020-03-26 13:53

/src/botan/build/include/botan/scrypt.h
Line
Count
Source (jump to first uncovered line)
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
0
      size_t N() const { return m_N; }
38
0
      size_t r() const { return m_r; }
39
0
      size_t p() const { return m_p; }
40
41
0
      size_t iterations() const override { return r(); }
42
43
0
      size_t parallelism() const override { return p(); }
44
45
0
      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
0
   {
114
0
   return scrypt(output, output_len,
115
0
                 password.c_str(), password.size(),
116
0
                 salt, salt_len,
117
0
                 N, r, p);
118
0
   }
119
120
inline size_t scrypt_memory_usage(size_t N, size_t r, size_t p)
121
0
   {
122
0
   return 128 * r * (N + p);
123
0
   }
124
125
}
126
127
#endif