1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Secret keys used to encrypt or sign confidential data have to be chosen from
//! a very large keyspace. However, passwords are usually short, human-generated
//! strings, making dictionary attacks practical.
//!
//! The pwhash operation derives a secret key of any size from a password and a
//!salt.
//!
//! - The generated key has the size defined by the application, no matter what
//! the password length is.
//! - The same password hashed with same parameters will always produce the same
//! key.
//! - The same password hashed with different salts will produce different keys.
//! - The function deriving a key from a password and a salt is CPU intensive
//! and intentionally requires a fair amount of memory. Therefore, it mitigates
//! brute-force attacks by requiring a significant effort to verify each
//! password.
//!
//! Common use cases:
//!
//! - Protecting an on-disk secret key with a password,
//! - Password storage, or rather: storing what it takes to verify a password
//! without having to store the actual password.
//!
//! # Notes
//! Do not forget to initialize the library with *init()*. The *pwhash*
//! functions will still work without doing so, but possibly way slower.
//!
//! Do not use constants (including OPSLIMIT_* and MEMLIMIT_*) in order to
//! verify a password. Save the parameters along with the hash instead, and use
//! these saved parameters for the verification.
//!
//! Alternatively, use *pwhash()* and *pwhash_verify()*, that automatically take
//! care of including and extracting the parameters.
//!
//! By doing so, passwords can be rehashed using different parameters if
//! required later on.
//!
//! Cleartext passwords should not stay in memory longer than needed.
//!
//! It is highly recommended to use *mlock()* to lock memory regions storing
//! cleartext passwords, and to call *munlock()* right after *pwhash()* and
//! *pwhash_verify()* return.
//!
//! By design, a password whose length is 65 bytes or more is reduced to
//! SHA-256(password). This can have security implications if the password is
//! present in another password database using raw, unsalted SHA-256. Or when
//! upgrading passwords previously hashed with unsalted SHA-256 to scrypt.
//!
//! If this is a concern, passwords should be pre-hashed before being hashed
//! using scrypt:
//!
//! # Guidelines for choosing scrypt parameters
//!
//! Start by determining how much memory can be used the scrypt function. What
//! will be the highest number of threads/processes evaluating the function
//! simultaneously (ideally, no more than 1 per CPU core)? How much physical
//! memory is guaranteed to be available?
//!
//! memlimit should be a power of 2. Do not use anything less than 16 Mb, even
//! for interactive use.
//!
//! Then, a reasonable starting point for opslimit is memlimit / 32.
//!
//! Measure how long the scrypt function needs in order to hash a password. If
//! this it is way too long for your application, reduce memlimit and adjust
//! opslimit using the above formula.
//!
//! If the function is so fast that you can afford it to be more computationally
//! intensive without any usability issues, increase opslimit.
//!
//! For online use (e.g. login in on a website), a 1 second computation is
//! likely to be the acceptable maximum.
//!
//! For interactive use (e.g. a desktop application), a 5 second pause after
//! having entered a password is acceptable if the password doesn't need to be
//! entered more than once per session.
//!
//! For non-interactive use and infrequent use (e.g. restoring an encrypted
//! backup), an even slower computation can be an option.
//!
//! But the best defense against brute-force password cracking remains using
//! strong passwords. Libraries such as passwdqc can help enforce this.
use ;
use ;
use secmem;
pub const SALTBYTES: usize = 32;
pub const STRBYTES: usize = 102;
pub const STRPREFIX: &'static str = "$7$";
pub const OPSLIMIT_INTERACTIVE: usize = 524288;
pub const MEMLIMIT_INTERACTIVE: usize = 16777216;
pub const OPSLIMIT_SENSITIVE: usize = 33554432;
pub const MEMLIMIT_SENSITIVE: usize = 1073741824;
extern "C"
/// The *keygen()* function derives a key of the given length from a password
/// and a salt.
///
/// The computed key is returned.
///
/// opslimit represents a maximum amount of computations to perform. Raising
/// this number will make the function require more CPU cycles to compute a key.
///
/// memlimit is the maximum amount of RAM that the function will use, in bytes.
/// It is highly recommended to allow the function to use at least 16 megabytes.
///
/// For interactive, online operations, *OPSLIMIT_INTERACTIVE* and
/// *MEMLIMIT_INTERACTIVE* provide a safe base line for these two parameters.
/// However, using higher values may improve security.
///
/// For highly sensitive data, *OPSLIMIT_SENSITIVE* and *MEMLIMIT_SENSITIVE* can
/// be used as an alternative. But with these parameters, deriving a key takes
/// about 2 seconds on a 2.8 Ghz Core i7 CPU and requires up to 1 gigabyte of
/// dedicated RAM.
///
/// The salt should be unpredictable. *randombytes::random_byte_array()* is the
/// easiest way to fill the salt.
///
/// Keep in mind that in order to produce the same key from the same password,
/// the same salt, and the same values for opslimit and memlimit have to be
/// used. Therefore, these parameters have to be stored for each user.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::{init,randombytes};
/// use sodium_sys::crypto::hash::passhash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Generate the hash.
/// let mut salt = [0; passhash::SALTBYTES];
/// randombytes::random_byte_array(&mut salt);
/// let key = passhash::keygen(b"test", 64, &salt, None, None).unwrap();
/// assert!(key.len() == 64);
/// ```
/// The *pwhash()* function creates an ASCII encoded string, which includes:
///
/// - The result of a memory-hard, CPU-intensive hash function applied to the
/// password.
/// - The automatically generated salt used for the previous computation.
/// - The other parameters required to verify the password: opslimit and
/// memlimit.
///
/// *OPSLIMIT_INTERACTIVE* and *MEMLIMIT_INTERACTIVE* are safe baseline values
/// to use for opslimit and memlimit.
///
/// The output string is zero-terminated, includes only ASCII characters and can
/// be safely stored into SQL databases and other data stores. No extra
/// information has to be stored in order to verify the password.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::init;
/// use sodium_sys::crypto::hash::passhash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Generate the hash.
/// let hash = passhash::pwhash(b"test", None, None).unwrap();
/// assert!(hash.len() == passhash::STRBYTES);
///
/// // Generate a more secure hash.
/// let hash = passhash::pwhash(b"test",
/// Some(passhash::OPSLIMIT_SENSITIVE),
/// Some(passhash::MEMLIMIT_SENSITIVE)).unwrap();
/// assert!(hash.len() == passhash::STRBYTES);
/// ```
/// The *pwhash_verify()* function verifies that the given hash is a valid
/// password verification string (as generated by *pwhash()*) for the given
/// password.
///
/// # Examples
///
/// ```
/// use sodium_sys::crypto::utils::init;
/// use sodium_sys::crypto::hash::passhash;
///
/// // Initialize sodium_sys
/// init::init();
///
/// // Generate the hash.
/// let hash = passhash::pwhash(b"test", None, None).unwrap();
/// assert!(hash.len() == passhash::STRBYTES);
///
/// // Verify the hash
/// let isvalid = passhash::pwhash_verify(b"test", hash);
/// assert!(isvalid);
///
/// // Generate a more secure hash.
/// let hash1 = passhash::pwhash(b"test",
/// Some(passhash::OPSLIMIT_SENSITIVE),
/// Some(passhash::MEMLIMIT_SENSITIVE)).unwrap();
/// assert!(hash1.len() == passhash::STRBYTES);
///
/// // Verify the hash
/// let isvalid = passhash::pwhash_verify(b"test", hash1);
/// assert!(isvalid);
/// ```
/// The traditional, low-level scrypt API is also available