/src/botan/build/include/public/botan/argon2.h
Line | Count | Source |
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 | | |
17 | | BOTAN_FUTURE_INTERNAL_HEADER(argon2.h) |
18 | | |
19 | | namespace Botan { |
20 | | |
21 | | /** |
22 | | * Argon2 key derivation function |
23 | | */ |
24 | | class 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 | 0 | size_t M() const { return m_M; } |
52 | | |
53 | 0 | size_t t() const { return m_t; } |
54 | | |
55 | 0 | size_t p() const { return m_p; } |
56 | | |
57 | 0 | bool supports_keyed_operation() const override { return true; } |
58 | | |
59 | 0 | bool supports_associated_data() const override { return true; } |
60 | | |
61 | 0 | size_t iterations() const override { return t(); } |
62 | | |
63 | 0 | size_t parallelism() const override { return p(); } |
64 | | |
65 | 0 | size_t memory_param() const override { return M(); } |
66 | | |
67 | 0 | 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 | | |
102 | | class BOTAN_PUBLIC_API(2, 11) Argon2_Family final : public PasswordHashFamily { |
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 | | */ |
141 | | BOTAN_DEPRECATED("Use PasswordHashFamily+PasswordHash") |
142 | | |
143 | | inline 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 | 0 | size_t t) { |
157 | 0 | auto pwdhash_fam = PasswordHashFamily::create_or_throw([y] { |
158 | 0 | switch(y) { |
159 | 0 | case 0: |
160 | 0 | return "Argon2d"; |
161 | 0 | case 1: |
162 | 0 | return "Argon2i"; |
163 | 0 | case 2: |
164 | 0 | return "Argon2id"; |
165 | 0 | default: |
166 | 0 | throw Not_Implemented("Unknown Argon2 family type"); |
167 | 0 | } |
168 | 0 | }()); |
169 | 0 | auto pwdhash = pwdhash_fam->from_params(M, t, p); |
170 | 0 | pwdhash->derive_key(output, output_len, password, password_len, salt, salt_len, ad, ad_len, key, key_len); |
171 | 0 | } |
172 | | |
173 | | } // namespace Botan |
174 | | |
175 | | #endif |