Skip to main content

argon2/
config.rs

1// Copyright (c) 2017 Martijn Rijkeboer <mrr@sru-systems.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::variant::Variant;
10use crate::version::Version;
11
12/// Structure containing configuration settings.
13///
14/// # Examples
15///
16/// ```
17/// use argon2::{Config, Variant, Version};
18///
19/// let config = Config::default();
20/// assert_eq!(config.ad, &[]);
21/// assert_eq!(config.hash_length, 32);
22/// assert_eq!(config.lanes, 1);
23/// assert_eq!(config.mem_cost, 19 * 1024);
24/// assert_eq!(config.secret, &[]);
25/// assert_eq!(config.time_cost, 2);
26/// assert_eq!(config.variant, Variant::Argon2id);
27/// assert_eq!(config.version, Version::Version13);
28/// ```
29#[derive(Clone, Debug, PartialEq)]
30pub struct Config<'a> {
31    /// The associated data.
32    pub ad: &'a [u8],
33
34    /// The length of the resulting hash.
35    pub hash_length: u32,
36
37    /// The number of lanes.
38    pub lanes: u32,
39
40    /// The amount of memory requested (KB).
41    pub mem_cost: u32,
42
43    /// The key.
44    pub secret: &'a [u8],
45
46    /// The number of passes.
47    pub time_cost: u32,
48
49    /// The variant.
50    pub variant: Variant,
51
52    /// The version number.
53    pub version: Version,
54}
55
56impl<'a> Config<'a> {
57    /// Default configuration used by the original C implementation.
58    pub fn original() -> Config<'a> {
59        Config {
60            ad: &[],
61            hash_length: 32,
62            lanes: 1,
63            mem_cost: 4096,
64            secret: &[],
65            time_cost: 3,
66            variant: Variant::Argon2i,
67            version: Version::Version13,
68        }
69    }
70
71    /// OWASP recommended configuration with t=1 and 46 MiB memory.
72    pub fn owasp1() -> Config<'a> {
73        Config {
74            ad: &[],
75            hash_length: 32,
76            lanes: 1,
77            mem_cost: 47104,
78            secret: &[],
79            time_cost: 1,
80            variant: Variant::Argon2id,
81            version: Version::Version13,
82        }
83    }
84
85    /// OWASP recommended configuration with t=2 and 19 MiB memory.
86    pub fn owasp2() -> Config<'a> {
87        Config {
88            ad: &[],
89            hash_length: 32,
90            lanes: 1,
91            mem_cost: 19456,
92            secret: &[],
93            time_cost: 2,
94            variant: Variant::Argon2id,
95            version: Version::Version13,
96        }
97    }
98
99    /// OWASP recommended configuration with t=3 and 12 MiB memory.
100    pub fn owasp3() -> Config<'a> {
101        Config {
102            ad: &[],
103            hash_length: 32,
104            lanes: 1,
105            mem_cost: 12288,
106            secret: &[],
107            time_cost: 3,
108            variant: Variant::Argon2id,
109            version: Version::Version13,
110        }
111    }
112
113    /// OWASP recommended configuration with t=4 and 9 MiB memory.
114    pub fn owasp4() -> Config<'a> {
115        Config {
116            ad: &[],
117            hash_length: 32,
118            lanes: 1,
119            mem_cost: 9216,
120            secret: &[],
121            time_cost: 4,
122            variant: Variant::Argon2id,
123            version: Version::Version13,
124        }
125    }
126
127    /// OWASP recommended configuration with t=5 and 7 MiB memory.
128    pub fn owasp5() -> Config<'a> {
129        Config {
130            ad: &[],
131            hash_length: 32,
132            lanes: 1,
133            mem_cost: 7168,
134            secret: &[],
135            time_cost: 5,
136            variant: Variant::Argon2id,
137            version: Version::Version13,
138        }
139    }
140
141    /// RFC9106 recommended configuration with t=1 and 2 GiB memory.
142    pub fn rfc9106() -> Config<'a> {
143        Config {
144            ad: &[],
145            hash_length: 32,
146            lanes: 1,
147            mem_cost: 2097152,
148            secret: &[],
149            time_cost: 1,
150            variant: Variant::Argon2id,
151            version: Version::Version13,
152        }
153    }
154
155    /// RFC9106 recommended configuration for memory-constrained environments.
156    pub fn rfc9106_low_mem() -> Config<'a> {
157        Config {
158            ad: &[],
159            hash_length: 32,
160            lanes: 1,
161            mem_cost: 65536,
162            secret: &[],
163            time_cost: 3,
164            variant: Variant::Argon2id,
165            version: Version::Version13,
166        }
167    }
168}
169
170impl<'a> Default for Config<'a> {
171    /// OWASP recommended configuration with t=2 and 19 MiB memory.
172    fn default() -> Config<'a> {
173        Config::owasp2()
174    }
175}
176
177#[cfg(test)]
178mod tests {
179
180    use crate::config::Config;
181    use crate::variant::Variant;
182    use crate::version::Version;
183
184    #[test]
185    fn default_returns_correct_instance() {
186        let config = Config::default();
187        assert_eq!(config.ad, &[]);
188        assert_eq!(config.hash_length, 32);
189        assert_eq!(config.lanes, 1);
190        assert_eq!(config.mem_cost, 19 * 1024);
191        assert_eq!(config.secret, &[]);
192        assert_eq!(config.time_cost, 2);
193        assert_eq!(config.variant, Variant::Argon2id);
194        assert_eq!(config.version, Version::Version13);
195    }
196
197    #[test]
198    fn original_returns_correct_instance() {
199        let config = Config::original();
200        assert_eq!(config.ad, &[]);
201        assert_eq!(config.hash_length, 32);
202        assert_eq!(config.lanes, 1);
203        assert_eq!(config.mem_cost, 4096);
204        assert_eq!(config.secret, &[]);
205        assert_eq!(config.time_cost, 3);
206        assert_eq!(config.variant, Variant::Argon2i);
207        assert_eq!(config.version, Version::Version13);
208    }
209
210    #[test]
211    fn owasp1_returns_correct_instance() {
212        let config = Config::owasp1();
213        assert_eq!(config.ad, &[]);
214        assert_eq!(config.hash_length, 32);
215        assert_eq!(config.lanes, 1);
216        assert_eq!(config.mem_cost, 46 * 1024);
217        assert_eq!(config.secret, &[]);
218        assert_eq!(config.time_cost, 1);
219        assert_eq!(config.variant, Variant::Argon2id);
220        assert_eq!(config.version, Version::Version13);
221    }
222
223    #[test]
224    fn owasp2_returns_correct_instance() {
225        let config = Config::owasp2();
226        assert_eq!(config.ad, &[]);
227        assert_eq!(config.hash_length, 32);
228        assert_eq!(config.lanes, 1);
229        assert_eq!(config.mem_cost, 19 * 1024);
230        assert_eq!(config.secret, &[]);
231        assert_eq!(config.time_cost, 2);
232        assert_eq!(config.variant, Variant::Argon2id);
233        assert_eq!(config.version, Version::Version13);
234    }
235
236    #[test]
237    fn owasp3_returns_correct_instance() {
238        let config = Config::owasp3();
239        assert_eq!(config.ad, &[]);
240        assert_eq!(config.hash_length, 32);
241        assert_eq!(config.lanes, 1);
242        assert_eq!(config.mem_cost, 12 * 1024);
243        assert_eq!(config.secret, &[]);
244        assert_eq!(config.time_cost, 3);
245        assert_eq!(config.variant, Variant::Argon2id);
246        assert_eq!(config.version, Version::Version13);
247    }
248
249    #[test]
250    fn owasp4_returns_correct_instance() {
251        let config = Config::owasp4();
252        assert_eq!(config.ad, &[]);
253        assert_eq!(config.hash_length, 32);
254        assert_eq!(config.lanes, 1);
255        assert_eq!(config.mem_cost, 9 * 1024);
256        assert_eq!(config.secret, &[]);
257        assert_eq!(config.time_cost, 4);
258        assert_eq!(config.variant, Variant::Argon2id);
259        assert_eq!(config.version, Version::Version13);
260    }
261
262    #[test]
263    fn owasp5_returns_correct_instance() {
264        let config = Config::owasp5();
265        assert_eq!(config.ad, &[]);
266        assert_eq!(config.hash_length, 32);
267        assert_eq!(config.lanes, 1);
268        assert_eq!(config.mem_cost, 7 * 1024);
269        assert_eq!(config.secret, &[]);
270        assert_eq!(config.time_cost, 5);
271        assert_eq!(config.variant, Variant::Argon2id);
272        assert_eq!(config.version, Version::Version13);
273    }
274
275    #[test]
276    fn rfc9106_returns_correct_instance() {
277        let config = Config::rfc9106();
278        assert_eq!(config.ad, &[]);
279        assert_eq!(config.hash_length, 32);
280        assert_eq!(config.lanes, 1);
281        assert_eq!(config.mem_cost, 2 * 1024 * 1024);
282        assert_eq!(config.secret, &[]);
283        assert_eq!(config.time_cost, 1);
284        assert_eq!(config.variant, Variant::Argon2id);
285        assert_eq!(config.version, Version::Version13);
286    }
287
288    #[test]
289    fn rfc9106_low_mem_returns_correct_instance() {
290        let config = Config::rfc9106_low_mem();
291        assert_eq!(config.ad, &[]);
292        assert_eq!(config.hash_length, 32);
293        assert_eq!(config.lanes, 1);
294        assert_eq!(config.mem_cost, 64 * 1024);
295        assert_eq!(config.secret, &[]);
296        assert_eq!(config.time_cost, 3);
297        assert_eq!(config.variant, Variant::Argon2id);
298        assert_eq!(config.version, Version::Version13);
299    }
300}