Coverage Report

Created: 2022-05-14 06:06

/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 iterations() const override { return m_r; }
38
39
0
      size_t parallelism() const override { return m_p; }
40
41
0
      size_t memory_param() const override { return m_N; }
42
43
      size_t total_memory_usage() const override;
44
45
   private:
46
      size_t m_N, m_r, m_p;
47
   };
48
49
class BOTAN_PUBLIC_API(2,8) Scrypt_Family final : public PasswordHashFamily
50
   {
51
   public:
52
      std::string name() const override;
53
54
      std::unique_ptr<PasswordHash> tune(size_t output_length,
55
                                         std::chrono::milliseconds msec,
56
                                         size_t max_memory) const override;
57
58
      std::unique_ptr<PasswordHash> default_params() const override;
59
60
      std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
61
62
      std::unique_ptr<PasswordHash> from_params(
63
         size_t N, size_t r, size_t p) const override;
64
   };
65
66
/**
67
* Scrypt key derivation function (RFC 7914)
68
*
69
* @param output the output will be placed here
70
* @param output_len length of output
71
* @param password the user password
72
* @param password_len length of password
73
* @param salt the salt
74
* @param salt_len length of salt
75
* @param N the CPU/Memory cost parameter, must be power of 2
76
* @param r the block size parameter
77
* @param p the parallelization parameter
78
*
79
* Suitable parameters for most uses would be N = 32768, r = 8, p = 1
80
*
81
* Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory
82
*/
83
BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
84
inline void scrypt(uint8_t output[], size_t output_len,
85
                   const char* password, size_t password_len,
86
                   const uint8_t salt[], size_t salt_len,
87
                   size_t N, size_t r, size_t p)
88
0
   {
89
0
   auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
90
0
   auto pwdhash = pwdhash_fam->from_params(N, r, p);
91
0
   pwdhash->derive_key(output, output_len,
92
0
                       password, password_len,
93
0
                       salt, salt_len);
94
0
   }
95
96
/**
97
* Scrypt key derivation function (RFC 7914)
98
* Before 2.8 this function was the primary interface for scrypt
99
*
100
* @param output the output will be placed here
101
* @param output_len length of output
102
* @param password the user password
103
* @param salt the salt
104
* @param salt_len length of salt
105
* @param N the CPU/Memory cost parameter, must be power of 2
106
* @param r the block size parameter
107
* @param p the parallelization parameter
108
*
109
* Suitable parameters for most uses would be N = 32768, r = 8, p = 1
110
*
111
* Scrypt uses approximately (p + N + 1) * 128 * r bytes of memory
112
*/
113
BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
114
inline void scrypt(uint8_t output[], size_t output_len,
115
                   const std::string& password,
116
                   const uint8_t salt[], size_t salt_len,
117
                   size_t N, size_t r, size_t p)
118
0
   {
119
0
   auto pwdhash_fam = PasswordHashFamily::create_or_throw("Scrypt");
120
0
   auto pwdhash = pwdhash_fam->from_params(N, r, p);
121
0
   pwdhash->derive_key(output, output_len,
122
0
                       password.c_str(), password.size(),
123
0
                       salt, salt_len);
124
0
   }
125
126
}
127
128
#endif