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
use ffi;
use randombytes::randombytes_into;
use libc::c_ulonglong;
pub const SALTBYTES: usize = ffi::crypto_pwhash_scryptsalsa208sha256_SALTBYTES;
pub const HASHEDPASSWORDBYTES: usize = ffi::crypto_pwhash_scryptsalsa208sha256_STRBYTES;
pub const STRPREFIX: &'static str = ffi::crypto_pwhash_scryptsalsa208sha256_STRPREFIX;
pub const OPSLIMIT_INTERACTIVE: OpsLimit =
OpsLimit(ffi::crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE);
pub const MEMLIMIT_INTERACTIVE: MemLimit =
MemLimit(ffi::crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE);
pub const OPSLIMIT_SENSITIVE: OpsLimit =
OpsLimit(ffi::crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE);
pub const MEMLIMIT_SENSITIVE: MemLimit =
MemLimit(ffi::crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE);
#[derive(Copy, Clone)]
pub struct OpsLimit(pub usize);
#[derive(Copy, Clone)]
pub struct MemLimit(pub usize);
new_type! {
public Salt(SALTBYTES);
}
new_type! {
public HashedPassword(HASHEDPASSWORDBYTES);
}
pub fn gen_salt() -> Salt {
let mut salt = Salt([0; SALTBYTES]);
{
let Salt(ref mut sb) = salt;
randombytes_into(sb);
}
salt
}
pub fn derive_key<'a>(key: &'a mut [u8], passwd: &[u8], &Salt(ref sb): &Salt,
OpsLimit(opslimit): OpsLimit,
MemLimit(memlimit): MemLimit) -> Result<&'a [u8], ()> {
if unsafe {
ffi::crypto_pwhash_scryptsalsa208sha256(key.as_mut_ptr(),
key.len() as c_ulonglong,
passwd.as_ptr(),
passwd.len() as c_ulonglong,
sb,
opslimit as c_ulonglong,
memlimit)
} == 0 {
Ok(key)
} else {
Err(())
}
}
pub fn pwhash(passwd: &[u8], OpsLimit(opslimit): OpsLimit,
MemLimit(memlimit): MemLimit) -> Result<HashedPassword, ()> {
let mut out = HashedPassword([0; HASHEDPASSWORDBYTES]);
if unsafe {
let HashedPassword(ref mut str_) = out;
ffi::crypto_pwhash_scryptsalsa208sha256_str(str_,
passwd.as_ptr(),
passwd.len() as c_ulonglong,
opslimit as c_ulonglong,
memlimit)
} == 0 {
Ok(out)
} else {
Err(())
}
}
pub fn pwhash_verify(&HashedPassword(ref str_): &HashedPassword,
passwd: &[u8]) -> bool {
unsafe {
ffi::crypto_pwhash_scryptsalsa208sha256_str_verify(str_,
passwd.as_ptr(),
passwd.len() as c_ulonglong)
== 0
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_derive_key() {
let mut kb = [0u8; 32];
let salt = Salt([0, 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]);
let pw = b"Correct Horse Battery Staple";
let key_expected = [0xf1, 0xbb, 0xb8, 0x7c, 0x43, 0x36, 0x5b, 0x03,
0x3b, 0x9a, 0xe8, 0x3e, 0x05, 0xef, 0xad, 0x25,
0xdb, 0x8d, 0x83, 0xb8, 0x3d, 0xb1, 0xde, 0xe3,
0x6b, 0xdb, 0xf5, 0x4d, 0xcd, 0x3a, 0x1a, 0x11];
let key = derive_key(&mut kb, pw, &salt,
OPSLIMIT_INTERACTIVE, MEMLIMIT_INTERACTIVE).unwrap();
assert_eq!(key, key_expected);
}
#[test]
fn test_pwhash_verify() {
use randombytes::randombytes;
for i in 0..32usize {
let pw = randombytes(i);
let pwh = pwhash(&pw, OPSLIMIT_INTERACTIVE, MEMLIMIT_INTERACTIVE).unwrap();
assert!(pwhash_verify(&pwh, &pw));
}
}
#[test]
fn test_pwhash_verify_tamper() {
use randombytes::randombytes;
for i in 0..16usize {
let mut pw = randombytes(i);
let pwh = pwhash(&pw, OPSLIMIT_INTERACTIVE, MEMLIMIT_INTERACTIVE).unwrap();
for j in 0..pw.len() {
pw[j] ^= 0x20;
assert!(!pwhash_verify(&pwh, &pw));
pw[j] ^= 0x20;
}
}
}
#[cfg(feature = "default")]
#[test]
fn test_serialisation() {
use randombytes::randombytes;
use test_utils::round_trip;
for i in 0..32usize {
let pw = randombytes(i);
let pwh = pwhash(&pw, OPSLIMIT_INTERACTIVE, MEMLIMIT_INTERACTIVE).unwrap();
let salt = gen_salt();
round_trip(pwh);
round_trip(salt);
}
}
}