Botan 3.11.0
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_AVX2)
76 static void blamka_avx2(uint64_t N[128], uint64_t T[128]);
77#endif
78
79#if defined(BOTAN_HAS_ARGON2_SIMD64)
80 static void blamka_simd64(uint64_t N[128], uint64_t T[128]);
81#endif
82
83 void argon2(uint8_t output[],
84 size_t output_len,
85 const char* password,
86 size_t password_len,
87 const uint8_t salt[],
88 size_t salt_len,
89 const uint8_t key[],
90 size_t key_len,
91 const uint8_t ad[],
92 size_t ad_len) const;
93
94 uint8_t m_family;
95 size_t m_M, m_t, m_p;
96};
97
99 public:
100 BOTAN_FUTURE_EXPLICIT Argon2_Family(uint8_t family);
101
102 std::string name() const override;
103
104 std::unique_ptr<PasswordHash> tune_params(size_t output_len,
105 uint64_t desired_runtime_msec,
106 std::optional<size_t> max_memory,
107 uint64_t tune_msec) const override;
108
109 std::unique_ptr<PasswordHash> default_params() const override;
110
111 std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
112
113 std::unique_ptr<PasswordHash> from_params(size_t M, size_t t, size_t p) const override;
114
115 private:
116 const uint8_t m_family;
117};
118
119/**
120* Argon2 key derivation function
121*
122* @param output the output will be placed here
123* @param output_len length of output
124* @param password the user password
125* @param password_len the length of password
126* @param salt the salt
127* @param salt_len length of salt
128* @param key an optional secret key
129* @param key_len the length of key
130* @param ad an optional additional input
131* @param ad_len the length of ad
132* @param y the Argon2 variant (0 = Argon2d, 1 = Argon2i, 2 = Argon2id)
133* @param p the parallelization parameter
134* @param M the amount of memory to use in Kb
135* @param t the number of iterations to use
136*/
137BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash")
138
139inline void argon2(uint8_t output[],
140 size_t output_len,
141 const char* password,
142 size_t password_len,
143 const uint8_t salt[],
144 size_t salt_len,
145 const uint8_t key[],
146 size_t key_len,
147 const uint8_t ad[],
148 size_t ad_len,
149 uint8_t y,
150 size_t p,
151 size_t M,
152 size_t t) {
153 auto pwdhash_fam = PasswordHashFamily::create_or_throw([y] {
154 switch(y) {
155 case 0:
156 return "Argon2d";
157 case 1:
158 return "Argon2i";
159 case 2:
160 return "Argon2id";
161 default:
162 throw Not_Implemented("Unknown Argon2 family type");
163 }
164 }());
165 auto pwdhash = pwdhash_fam->from_params(M, t, p);
166 pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len, ad, ad_len, key, key_len);
167}
168
169} // namespace Botan
170
171#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:139