Coverage Report

Created: 2021-10-13 08:49

/src/botan/build/include/botan/argon2.h
Line
Count
Source (jump to first uncovered line)
1
/**
2
* (C) 2018,2019 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_ARGON2_H_
8
#define BOTAN_ARGON2_H_
9
10
#include <botan/pwdhash.h>
11
#include <botan/exceptn.h>
12
13
#if defined(BOTAN_HAS_ARGON2_FMT)
14
  #include <botan/argon2fmt.h>
15
#endif
16
17
BOTAN_FUTURE_INTERNAL_HEADER(argon2.h)
18
19
namespace Botan {
20
21
class RandomNumberGenerator;
22
23
/**
24
* Argon2 key derivation function
25
*/
26
class BOTAN_PUBLIC_API(2,11) Argon2 final : public PasswordHash
27
   {
28
   public:
29
      Argon2(uint8_t family, size_t M, size_t t, size_t p);
30
31
      Argon2(const Argon2& other) = default;
32
      Argon2& operator=(const Argon2&) = default;
33
34
      /**
35
      * Derive a new key under the current Argon2 parameter set
36
      */
37
      void derive_key(uint8_t out[], size_t out_len,
38
                      const char* password, size_t password_len,
39
                      const uint8_t salt[], size_t salt_len) const override;
40
41
      void derive_key(uint8_t out[], size_t out_len,
42
                      const char* password, size_t password_len,
43
                      const uint8_t salt[], size_t salt_len,
44
                      const uint8_t ad[], size_t ad_len,
45
                      const uint8_t key[], size_t key_len) const override;
46
47
      std::string to_string() const override;
48
49
0
      size_t M() const { return m_M; }
50
0
      size_t t() const { return m_t; }
51
0
      size_t p() const { return m_p; }
52
53
0
      size_t iterations() const override { return t(); }
54
55
0
      size_t parallelism() const override { return p(); }
56
57
0
      size_t memory_param() const override { return M(); }
58
59
0
      size_t total_memory_usage() const override { return M() * 1024; }
60
61
   private:
62
63
      void argon2(uint8_t output[], size_t output_len,
64
                  const char* password, size_t password_len,
65
                  const uint8_t salt[], size_t salt_len,
66
                  const uint8_t key[], size_t key_len,
67
                  const uint8_t ad[], size_t ad_len) const;
68
69
      uint8_t m_family;
70
      size_t m_M, m_t, m_p;
71
   };
72
73
class BOTAN_PUBLIC_API(2,11) Argon2_Family final : public PasswordHashFamily
74
   {
75
   public:
76
      Argon2_Family(uint8_t family);
77
78
      std::string name() const override;
79
80
      std::unique_ptr<PasswordHash> tune(size_t output_length,
81
                                         std::chrono::milliseconds msec,
82
                                         size_t max_memory) const override;
83
84
      std::unique_ptr<PasswordHash> default_params() const override;
85
86
      std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
87
88
      std::unique_ptr<PasswordHash> from_params(
89
         size_t M, size_t t, size_t p) const override;
90
   private:
91
      const uint8_t m_family;
92
   };
93
94
/**
95
* Argon2 key derivation function
96
*
97
* @param output the output will be placed here
98
* @param output_len length of output
99
* @param password the user password
100
* @param password_len the length of password
101
* @param salt the salt
102
* @param salt_len length of salt
103
* @param key an optional secret key
104
* @param key_len the length of key
105
* @param ad an optional additional input
106
* @param ad_len the length of ad
107
* @param y the Argon2 variant (0 = Argon2d, 1 = Argon2i, 2 = Argon2id)
108
* @param p the parallelization parameter
109
* @param M the amount of memory to use in Kb
110
* @param t the number of iterations to use
111
*/
112
BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
113
inline void argon2(uint8_t output[], size_t output_len,
114
                   const char* password, size_t password_len,
115
                   const uint8_t salt[], size_t salt_len,
116
                   const uint8_t key[], size_t key_len,
117
                   const uint8_t ad[], size_t ad_len,
118
                   uint8_t y, size_t p, size_t M, size_t t)
119
0
   {
120
0
   std::unique_ptr<PasswordHashFamily> pwdhash_fam;
121
0
122
0
   if(y == 0)
123
0
      pwdhash_fam = PasswordHashFamily::create_or_throw("Argon2d");
124
0
   else if(y == 1)
125
0
      pwdhash_fam = PasswordHashFamily::create_or_throw("Argon2i");
126
0
   else if(y == 2)
127
0
      pwdhash_fam = PasswordHashFamily::create_or_throw("Argon2id");
128
0
   else
129
0
      throw Not_Implemented("Unknown Argon2 family type");
130
0
131
0
   auto pwdhash = pwdhash_fam->from_params(M, t, p);
132
0
133
0
   pwdhash->derive_key(output, output_len,
134
0
                       password, password_len,
135
0
                       salt, salt_len,
136
0
                       ad, ad_len,
137
0
                       key, key_len);
138
0
   }
139
140
}
141
142
#endif