1 // crypto_pwhash_scryptsalsa208sha256.h
2 
3 use libc::{c_ulonglong, size_t};
4 use libsodium_sys::*;
5 use std::ffi::CStr;
6 
7 #[test]
test_crypto_pwhash_scryptsalsa208sha256_saltbytes()8 fn test_crypto_pwhash_scryptsalsa208sha256_saltbytes() {
9     assert!(
10         unsafe { crypto_pwhash_scryptsalsa208sha256_saltbytes() }
11             == crypto_pwhash_scryptsalsa208sha256_SALTBYTES as usize
12     )
13 }
14 
15 #[test]
test_crypto_pwhash_scryptsalsa208sha256_strbytes()16 fn test_crypto_pwhash_scryptsalsa208sha256_strbytes() {
17     assert!(
18         unsafe { crypto_pwhash_scryptsalsa208sha256_strbytes() }
19             == crypto_pwhash_scryptsalsa208sha256_STRBYTES as usize
20     )
21 }
22 
23 #[test]
test_crypto_pwhash_scryptsalsa208sha256_opslimit_interactive()24 fn test_crypto_pwhash_scryptsalsa208sha256_opslimit_interactive() {
25     assert!(
26         unsafe { crypto_pwhash_scryptsalsa208sha256_opslimit_interactive() }
27             == crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE as usize
28     )
29 }
30 
31 #[test]
test_crypto_pwhash_scryptsalsa208sha256_memlimit_interactive()32 fn test_crypto_pwhash_scryptsalsa208sha256_memlimit_interactive() {
33     assert!(
34         unsafe { crypto_pwhash_scryptsalsa208sha256_memlimit_interactive() }
35             == crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE as usize
36     )
37 }
38 
39 #[test]
test_crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive()40 fn test_crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive() {
41     assert!(
42         unsafe { crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive() }
43             == crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE as usize
44     )
45 }
46 
47 #[test]
test_crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive()48 fn test_crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive() {
49     assert!(
50         unsafe { crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive() }
51             == crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE as usize
52     )
53 }
54 
55 #[test]
test_crypto_pwhash_scryptsalsa208sha256_strprefix()56 fn test_crypto_pwhash_scryptsalsa208sha256_strprefix() {
57     unsafe {
58         let s = crypto_pwhash_scryptsalsa208sha256_strprefix();
59         let s = CStr::from_ptr(s);
60         let p = CStr::from_bytes_with_nul(crypto_pwhash_scryptsalsa208sha256_STRPREFIX).unwrap();
61         assert_eq!(s, p);
62     }
63 }
64 
65 #[test]
test_crypto_pwhash_scryptsalsa208sha256_str()66 fn test_crypto_pwhash_scryptsalsa208sha256_str() {
67     let password = "Correct Horse Battery Staple";
68     let mut hashed_password = [0; crypto_pwhash_scryptsalsa208sha256_STRBYTES as usize];
69     let ret_hash = unsafe {
70         crypto_pwhash_scryptsalsa208sha256_str(
71             hashed_password.as_mut_ptr(),
72             password.as_ptr() as *const _,
73             password.len() as c_ulonglong,
74             u64::from(crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE),
75             crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE as size_t,
76         )
77     };
78     assert!(ret_hash == 0);
79     let ret_verify = unsafe {
80         crypto_pwhash_scryptsalsa208sha256_str_verify(
81             hashed_password.as_ptr(),
82             password.as_ptr() as *const _,
83             password.len() as c_ulonglong,
84         )
85     };
86     assert!(ret_verify == 0);
87 }
88 
89 #[test]
test_crypto_pwhash_scryptsalsa208sha256_ll_1()90 fn test_crypto_pwhash_scryptsalsa208sha256_ll_1() {
91     // See https://www.tarsnap.com/scrypt/scrypt.pdf Page 16
92     let password = "";
93     let salt = "";
94     let n = 16;
95     let r = 1;
96     let p = 1;
97     let mut buf = [0u8; 64];
98     let expected = [
99         0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20, 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04,
100         0x97, 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8, 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2,
101         0x14, 0x42, 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8, 0x32, 0x6a, 0x75, 0x3a, 0x0f,
102         0xc8, 0x1f, 0x17, 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28, 0xcf, 0x35, 0xe2, 0x0c,
103         0x38, 0xd1, 0x89, 0x06,
104     ];
105     let ret = unsafe {
106         crypto_pwhash_scryptsalsa208sha256_ll(
107             password.as_ptr(),
108             password.len() as size_t,
109             salt.as_ptr(),
110             salt.len() as size_t,
111             n,
112             r,
113             p,
114             buf.as_mut_ptr(),
115             buf.len() as size_t,
116         )
117     };
118     assert!(ret == 0);
119     assert!(buf[0..] == expected[0..]);
120 }
121 
122 #[test]
test_crypto_pwhash_scryptsalsa208sha256_ll_2()123 fn test_crypto_pwhash_scryptsalsa208sha256_ll_2() {
124     // See https://www.tarsnap.com/scrypt/scrypt.pdf Page 16
125     let password = "password";
126     let salt = "NaCl";
127     let n = 1024;
128     let r = 8;
129     let p = 16;
130     let mut buf = [0u8; 64];
131     let expected = [
132         0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00, 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9,
133         0xfe, 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30, 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37,
134         0x31, 0x62, 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88, 0x6f, 0xf1, 0x09, 0x27, 0x9d,
135         0x98, 0x30, 0xda, 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d, 0x83, 0x60, 0xcb, 0xdf,
136         0xa2, 0xcc, 0x06, 0x40,
137     ];
138     let ret = unsafe {
139         crypto_pwhash_scryptsalsa208sha256_ll(
140             password.as_ptr(),
141             password.len() as size_t,
142             salt.as_ptr(),
143             salt.len() as size_t,
144             n,
145             r,
146             p,
147             buf.as_mut_ptr(),
148             buf.len() as size_t,
149         )
150     };
151     assert!(ret == 0);
152     assert!(buf[0..] == expected[0..]);
153 }
154 
155 #[test]
test_crypto_pwhash_scryptsalsa208sha256_ll_3()156 fn test_crypto_pwhash_scryptsalsa208sha256_ll_3() {
157     // See https://www.tarsnap.com/scrypt/scrypt.pdf Page 16
158     let password = "pleaseletmein";
159     let salt = "SodiumChloride";
160     let n = 16384;
161     let r = 8;
162     let p = 1;
163     let mut buf = [0u8; 64];
164     let expected = [
165         0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48, 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38,
166         0xeb, 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e, 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d,
167         0xa1, 0xf2, 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf, 0x62, 0xd4, 0x97, 0x05, 0x24,
168         0x2a, 0x9a, 0xf9, 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40, 0xdf, 0xcf, 0x01, 0x7b,
169         0x45, 0x57, 0x58, 0x87,
170     ];
171     let ret = unsafe {
172         crypto_pwhash_scryptsalsa208sha256_ll(
173             password.as_ptr(),
174             password.len() as size_t,
175             salt.as_ptr(),
176             salt.len() as size_t,
177             n,
178             r,
179             p,
180             buf.as_mut_ptr(),
181             buf.len() as size_t,
182         )
183     };
184     assert!(ret == 0);
185     assert!(buf[0..] == expected[0..]);
186 }
187 
188 #[test]
test_crypto_pwhash_scryptsalsa208sha256_ll_4()189 fn test_crypto_pwhash_scryptsalsa208sha256_ll_4() {
190     // See https://www.tarsnap.com/scrypt/scrypt.pdf Page 16
191     let password = "pleaseletmein";
192     let salt = "SodiumChloride";
193     let n = 1_048_576;
194     let r = 8;
195     let p = 1;
196     let mut buf = [0u8; 64];
197     let expected = [
198         0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae, 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8,
199         0x81, 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d, 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad,
200         0xaa, 0x47, 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f, 0xfa, 0x1c, 0x6d, 0x92, 0x7c,
201         0x40, 0xf4, 0xc3, 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb, 0xcb, 0xf4, 0x5c, 0x6f,
202         0xa7, 0x7a, 0x41, 0xa4,
203     ];
204     let ret = unsafe {
205         crypto_pwhash_scryptsalsa208sha256_ll(
206             password.as_ptr(),
207             password.len() as size_t,
208             salt.as_ptr(),
209             salt.len() as size_t,
210             n,
211             r,
212             p,
213             buf.as_mut_ptr(),
214             buf.len() as size_t,
215         )
216     };
217     assert!(ret == 0);
218     assert!(buf[0..] == expected[0..]);
219 }
220