Botan 3.11.1
Crypto and TLS for C&
argon2.h
Go to the documentation of this file.
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/exceptn.h>
11#include <botan/pwdhash.h>
12
13#if defined(BOTAN_HAS_ARGON2_FMT)
14 #include <botan/argon2fmt.h>
15#endif
16
18
19namespace Botan {
20
21/**
22* Argon2 key derivation function
23*/
24class BOTAN_PUBLIC_API(2, 11) Argon2 final : public PasswordHash {
25 public:
26 Argon2(uint8_t family, size_t M, size_t t, size_t p);
27
28 /**
29 * Derive a new key under the current Argon2 parameter set
30 */
31 void derive_key(uint8_t out[],
32 size_t out_len,
33 const char* password,
34 size_t password_len,
35 const uint8_t salt[],
36 size_t salt_len) const override;
37
38 void derive_key(uint8_t out[],
39 size_t out_len,
40 const char* password,
41 size_t password_len,
42 const uint8_t salt[],
43 size_t salt_len,
44 const uint8_t ad[],
45 size_t ad_len,
46 const uint8_t key[],
47 size_t key_len) const override;
48
49 std::string to_string() const override;
50
51 size_t M() const { return m_M; }
52
53 size_t t() const { return m_t; }
54
55 size_t p() const { return m_p; }
56
57 bool supports_keyed_operation() const override { return true; }
58
59 bool supports_associated_data() const override { return true; }
60
61 size_t iterations() const override { return t(); }
62
63 size_t parallelism() const override { return p(); }
64
65 size_t memory_param() const override { return M(); }
66
67 size_t total_memory_usage() const override { return M() * 1024; }
68
69 /**
70 * Argon2's BLAMKA function
71 */
72 static void blamka(uint64_t N[128], uint64_t T[128]);
73
74 private:
75#if defined(BOTAN_HAS_ARGON2_AVX512)
76 static void blamka_avx512(uint64_t N[128], uint64_t T[128]);
77#endif
78
79#if defined(BOTAN_HAS_ARGON2_AVX2)
80 static void blamka_avx2(uint64_t N[128], uint64_t T[128]);
81#endif
82
83#if defined(BOTAN_HAS_ARGON2_SIMD64)
84 static void blamka_simd64(uint64_t N[128], uint64_t T[128]);
85#endif
86
87 void argon2(uint8_t output[],
88 size_t output_len,
89 const char* password,
90 size_t password_len,
91 const uint8_t salt[],
92 size_t salt_len,
93 const uint8_t key[],
94 size_t key_len,
95 const uint8_t ad[],
96 size_t ad_len) const;
97
98 uint8_t m_family;
99 size_t m_M, m_t, m_p;
100};
101
103 public:
104 BOTAN_FUTURE_EXPLICIT Argon2_Family(uint8_t family);
105
106 std::string name() const override;
107
108 std::unique_ptr<PasswordHash> tune_params(size_t output_len,
109 uint64_t desired_runtime_msec,
110 std::optional<size_t> max_memory,
111 uint64_t tune_msec) const override;
112
113 std::unique_ptr<PasswordHash> default_params() const override;
114
115 std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
116
117 std::unique_ptr<PasswordHash> from_params(size_t M, size_t t, size_t p) const override;
118
119 private:
120 const uint8_t m_family;
121};
122
123/**
124* Argon2 key derivation function
125*
126* @param output the output will be placed here
127* @param output_len length of output
128* @param password the user password
129* @param password_len the length of password
130* @param salt the salt
131* @param salt_len length of salt
132* @param key an optional secret key
133* @param key_len the length of key
134* @param ad an optional additional input
135* @param ad_len the length of ad
136* @param y the Argon2 variant (0 = Argon2d, 1 = Argon2i, 2 = Argon2id)
137* @param p the parallelization parameter
138* @param M the amount of memory to use in Kb
139* @param t the number of iterations to use
140*/
141BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
142
143inline void argon2(uint8_t output[],
144 size_t output_len,
145 const char* password,
146 size_t password_len,
147 const uint8_t salt[],
148 size_t salt_len,
149 const uint8_t key[],
150 size_t key_len,
151 const uint8_t ad[],
152 size_t ad_len,
153 uint8_t y,
154 size_t p,
155 size_t M,
156 size_t t) {
157 auto pwdhash_fam = PasswordHashFamily::create_or_throw([y] {
158 switch(y) {
159 case 0:
160 return "Argon2d";
161 case 1:
162 return "Argon2i";
163 case 2:
164 return "Argon2id";
165 default:
166 throw Not_Implemented("Unknown Argon2 family type");
167 }
168 }());
169 auto pwdhash = pwdhash_fam->from_params(M, t, p);
170 pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len, ad, ad_len, key, key_len);
171}
172
173} // namespace Botan
174
175#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition api.h:98
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
std::unique_ptr< PasswordHash > from_params(size_t M, size_t t, size_t p) const override
std::string name() const override
BOTAN_FUTURE_EXPLICIT Argon2_Family(uint8_t family)
std::unique_ptr< PasswordHash > tune_params(size_t output_len, uint64_t desired_runtime_msec, std::optional< size_t > max_memory, uint64_t tune_msec) const override
std::unique_ptr< PasswordHash > default_params() const override
std::unique_ptr< PasswordHash > from_iterations(size_t iter) const override
size_t p() const
Definition argon2.h:55
bool supports_keyed_operation() const override
Definition argon2.h:57
size_t iterations() const override
Definition argon2.h:61
size_t total_memory_usage() const override
Definition argon2.h:67
size_t t() const
Definition argon2.h:53
size_t parallelism() const override
Definition argon2.h:63
bool supports_associated_data() const override
Definition argon2.h:59
size_t M() const
Definition argon2.h:51
size_t memory_param() const override
Definition argon2.h:65
Argon2(uint8_t family, size_t M, size_t t, size_t p)
static std::unique_ptr< PasswordHashFamily > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:110
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
virtual std::string to_string() const =0
void argon2(uint8_t output[], size_t output_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len, const uint8_t key[], size_t key_len, const uint8_t ad[], size_t ad_len, uint8_t y, size_t p, size_t M, size_t t)
Definition argon2.h:143