Skip to main content

boring/
pkcs5.rs

1use crate::ffi;
2use std::ffi::c_int;
3use std::ptr;
4
5use crate::error::ErrorStack;
6use crate::hash::MessageDigest;
7use crate::symm::Cipher;
8use crate::{cvt, cvt_nz, try_int};
9
10#[derive(Clone, Eq, PartialEq, Hash, Debug)]
11pub struct KeyIvPair {
12    pub key: Vec<u8>,
13    pub iv: Option<Vec<u8>>,
14}
15
16/// Derives a key and an IV from various parameters.
17///
18/// If specified, `salt` must be 8 bytes in length.
19///
20/// If the total key and IV length is less than 16 bytes and MD5 is used then
21/// the algorithm is compatible with the key derivation algorithm from PKCS#5
22/// v1.5 or PBKDF1 from PKCS#5 v2.0.
23///
24/// New applications should not use this and instead use
25/// `pbkdf2_hmac` or another more modern key derivation algorithm.
26#[allow(clippy::useless_conversion)]
27pub fn bytes_to_key(
28    cipher: Cipher,
29    digest: MessageDigest,
30    data: &[u8],
31    salt: Option<&[u8]>,
32    count: u32,
33) -> Result<KeyIvPair, ErrorStack> {
34    unsafe {
35        assert!(data.len() <= c_int::MAX as usize);
36        let salt_ptr = match salt {
37            Some(salt) => {
38                pub const PKCS5_SALT_LEN: c_int = 8;
39                assert_eq!(salt.len(), PKCS5_SALT_LEN as usize);
40                salt.as_ptr()
41            }
42            None => ptr::null(),
43        };
44
45        ffi::init();
46
47        let mut iv = cipher.iv_len().map(|l| vec![0; l]);
48
49        let cipher = cipher.as_ptr();
50        let digest = digest.as_ptr();
51
52        let len = cvt_nz(ffi::EVP_BytesToKey(
53            cipher,
54            digest,
55            salt_ptr,
56            ptr::null(),
57            data.len(),
58            count,
59            ptr::null_mut(),
60            ptr::null_mut(),
61        ))?;
62
63        let mut key = vec![0; len.get()];
64        let iv_ptr = iv
65            .as_mut()
66            .map(|v| v.as_mut_ptr())
67            .unwrap_or(ptr::null_mut());
68
69        cvt(ffi::EVP_BytesToKey(
70            cipher,
71            digest,
72            salt_ptr,
73            data.as_ptr(),
74            data.len(),
75            count,
76            key.as_mut_ptr(),
77            iv_ptr,
78        ))?;
79
80        Ok(KeyIvPair { key, iv })
81    }
82}
83
84/// Derives a key from a password and salt using the PBKDF2-HMAC algorithm with a digest function.
85pub fn pbkdf2_hmac(
86    pass: &[u8],
87    salt: &[u8],
88    iter: usize,
89    hash: MessageDigest,
90    key: &mut [u8],
91) -> Result<(), ErrorStack> {
92    unsafe {
93        ffi::init();
94
95        cvt(ffi::PKCS5_PBKDF2_HMAC(
96            pass.as_ptr().cast(),
97            pass.len(),
98            salt.as_ptr(),
99            salt.len(),
100            try_int(iter)?,
101            hash.as_ptr(),
102            key.len(),
103            key.as_mut_ptr(),
104        ))
105    }
106}
107
108/// Derives a key from a password and salt using the scrypt algorithm.
109pub fn scrypt(
110    pass: &[u8],
111    salt: &[u8],
112    n: u64,
113    r: u64,
114    p: u64,
115    maxmem: usize,
116    key: &mut [u8],
117) -> Result<(), ErrorStack> {
118    unsafe {
119        ffi::init();
120        cvt(ffi::EVP_PBE_scrypt(
121            pass.as_ptr().cast(),
122            pass.len(),
123            salt.as_ptr().cast(),
124            salt.len(),
125            n,
126            r,
127            p,
128            maxmem,
129            key.as_mut_ptr(),
130            key.len(),
131        ))
132    }
133}
134
135#[cfg(test)]
136mod tests {
137    use crate::hash::MessageDigest;
138    use crate::symm::Cipher;
139
140    // Test vectors from
141    // https://git.lysator.liu.se/nettle/nettle/blob/nettle_3.1.1_release_20150424/testsuite/pbkdf2-test.c
142    #[test]
143    fn pbkdf2_hmac_sha256() {
144        let mut buf = [0; 16];
145
146        super::pbkdf2_hmac(b"passwd", b"salt", 1, MessageDigest::sha256(), &mut buf).unwrap();
147        assert_eq!(
148            buf,
149            &[
150                0x55_u8, 0xac_u8, 0x04_u8, 0x6e_u8, 0x56_u8, 0xe3_u8, 0x08_u8, 0x9f_u8, 0xec_u8,
151                0x16_u8, 0x91_u8, 0xc2_u8, 0x25_u8, 0x44_u8, 0xb6_u8, 0x05_u8,
152            ][..]
153        );
154
155        super::pbkdf2_hmac(
156            b"Password",
157            b"NaCl",
158            80000,
159            MessageDigest::sha256(),
160            &mut buf,
161        )
162        .unwrap();
163        assert_eq!(
164            buf,
165            &[
166                0x4d_u8, 0xdc_u8, 0xd8_u8, 0xf6_u8, 0x0b_u8, 0x98_u8, 0xbe_u8, 0x21_u8, 0x83_u8,
167                0x0c_u8, 0xee_u8, 0x5e_u8, 0xf2_u8, 0x27_u8, 0x01_u8, 0xf9_u8,
168            ][..]
169        );
170    }
171
172    // Test vectors from
173    // https://git.lysator.liu.se/nettle/nettle/blob/nettle_3.1.1_release_20150424/testsuite/pbkdf2-test.c
174    #[test]
175    fn pbkdf2_hmac_sha512() {
176        let mut buf = [0; 64];
177
178        super::pbkdf2_hmac(b"password", b"NaCL", 1, MessageDigest::sha512(), &mut buf).unwrap();
179        assert_eq!(
180            &buf[..],
181            &[
182                0x73_u8, 0xde_u8, 0xcf_u8, 0xa5_u8, 0x8a_u8, 0xa2_u8, 0xe8_u8, 0x4f_u8, 0x94_u8,
183                0x77_u8, 0x1a_u8, 0x75_u8, 0x73_u8, 0x6b_u8, 0xb8_u8, 0x8b_u8, 0xd3_u8, 0xc7_u8,
184                0xb3_u8, 0x82_u8, 0x70_u8, 0xcf_u8, 0xb5_u8, 0x0c_u8, 0xb3_u8, 0x90_u8, 0xed_u8,
185                0x78_u8, 0xb3_u8, 0x05_u8, 0x65_u8, 0x6a_u8, 0xf8_u8, 0x14_u8, 0x8e_u8, 0x52_u8,
186                0x45_u8, 0x2b_u8, 0x22_u8, 0x16_u8, 0xb2_u8, 0xb8_u8, 0x09_u8, 0x8b_u8, 0x76_u8,
187                0x1f_u8, 0xc6_u8, 0x33_u8, 0x60_u8, 0x60_u8, 0xa0_u8, 0x9f_u8, 0x76_u8, 0x41_u8,
188                0x5e_u8, 0x9f_u8, 0x71_u8, 0xea_u8, 0x47_u8, 0xf9_u8, 0xe9_u8, 0x06_u8, 0x43_u8,
189                0x06_u8,
190            ][..]
191        );
192
193        super::pbkdf2_hmac(
194            b"pass\0word",
195            b"sa\0lt",
196            1,
197            MessageDigest::sha512(),
198            &mut buf,
199        )
200        .unwrap();
201        assert_eq!(
202            &buf[..],
203            &[
204                0x71_u8, 0xa0_u8, 0xec_u8, 0x84_u8, 0x2a_u8, 0xbd_u8, 0x5c_u8, 0x67_u8, 0x8b_u8,
205                0xcf_u8, 0xd1_u8, 0x45_u8, 0xf0_u8, 0x9d_u8, 0x83_u8, 0x52_u8, 0x2f_u8, 0x93_u8,
206                0x36_u8, 0x15_u8, 0x60_u8, 0x56_u8, 0x3c_u8, 0x4d_u8, 0x0d_u8, 0x63_u8, 0xb8_u8,
207                0x83_u8, 0x29_u8, 0x87_u8, 0x10_u8, 0x90_u8, 0xe7_u8, 0x66_u8, 0x04_u8, 0xa4_u8,
208                0x9a_u8, 0xf0_u8, 0x8f_u8, 0xe7_u8, 0xc9_u8, 0xf5_u8, 0x71_u8, 0x56_u8, 0xc8_u8,
209                0x79_u8, 0x09_u8, 0x96_u8, 0xb2_u8, 0x0f_u8, 0x06_u8, 0xbc_u8, 0x53_u8, 0x5e_u8,
210                0x5a_u8, 0xb5_u8, 0x44_u8, 0x0d_u8, 0xf7_u8, 0xe8_u8, 0x78_u8, 0x29_u8, 0x6f_u8,
211                0xa7_u8,
212            ][..]
213        );
214
215        super::pbkdf2_hmac(
216            b"passwordPASSWORDpassword",
217            b"salt\0\0\0",
218            50,
219            MessageDigest::sha512(),
220            &mut buf,
221        )
222        .unwrap();
223        assert_eq!(
224            &buf[..],
225            &[
226                0x01_u8, 0x68_u8, 0x71_u8, 0xa4_u8, 0xc4_u8, 0xb7_u8, 0x5f_u8, 0x96_u8, 0x85_u8,
227                0x7f_u8, 0xd2_u8, 0xb9_u8, 0xf8_u8, 0xca_u8, 0x28_u8, 0x02_u8, 0x3b_u8, 0x30_u8,
228                0xee_u8, 0x2a_u8, 0x39_u8, 0xf5_u8, 0xad_u8, 0xca_u8, 0xc8_u8, 0xc9_u8, 0x37_u8,
229                0x5f_u8, 0x9b_u8, 0xda_u8, 0x1c_u8, 0xcd_u8, 0x1b_u8, 0x6f_u8, 0x0b_u8, 0x2f_u8,
230                0xc3_u8, 0xad_u8, 0xda_u8, 0x50_u8, 0x54_u8, 0x12_u8, 0xe7_u8, 0x9d_u8, 0x89_u8,
231                0x00_u8, 0x56_u8, 0xc6_u8, 0x2e_u8, 0x52_u8, 0x4c_u8, 0x7d_u8, 0x51_u8, 0x15_u8,
232                0x4b_u8, 0x1a_u8, 0x85_u8, 0x34_u8, 0x57_u8, 0x5b_u8, 0xd0_u8, 0x2d_u8, 0xee_u8,
233                0x39_u8,
234            ][..]
235        );
236    }
237
238    #[test]
239    fn bytes_to_key() {
240        let salt = [16_u8, 34_u8, 19_u8, 23_u8, 141_u8, 4_u8, 207_u8, 221_u8];
241
242        let data = [
243            143_u8, 210_u8, 75_u8, 63_u8, 214_u8, 179_u8, 155_u8, 241_u8, 242_u8, 31_u8, 154_u8,
244            56_u8, 198_u8, 145_u8, 192_u8, 64_u8, 2_u8, 245_u8, 167_u8, 220_u8, 55_u8, 119_u8,
245            233_u8, 136_u8, 139_u8, 27_u8, 71_u8, 242_u8, 119_u8, 175_u8, 65_u8, 207_u8,
246        ];
247
248        let expected_key = vec![
249            249_u8, 115_u8, 114_u8, 97_u8, 32_u8, 213_u8, 165_u8, 146_u8, 58_u8, 87_u8, 234_u8,
250            3_u8, 43_u8, 250_u8, 97_u8, 114_u8, 26_u8, 98_u8, 245_u8, 246_u8, 238_u8, 177_u8,
251            229_u8, 161_u8, 183_u8, 224_u8, 174_u8, 3_u8, 6_u8, 244_u8, 236_u8, 255_u8,
252        ];
253        let expected_iv = vec![
254            4_u8, 223_u8, 153_u8, 219_u8, 28_u8, 142_u8, 234_u8, 68_u8, 227_u8, 69_u8, 98_u8,
255            107_u8, 208_u8, 14_u8, 236_u8, 60_u8,
256        ];
257
258        assert_eq!(
259            super::bytes_to_key(
260                Cipher::aes_256_cbc(),
261                MessageDigest::sha1(),
262                &data,
263                Some(&salt),
264                1,
265            )
266            .unwrap(),
267            super::KeyIvPair {
268                key: expected_key,
269                iv: Some(expected_iv),
270            }
271        );
272    }
273
274    #[test]
275    fn scrypt() {
276        use hex;
277
278        let pass = "pleaseletmein";
279        let salt = "SodiumChloride";
280        let expected =
281            "7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613\
282             f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887";
283
284        let mut actual = [0; 64];
285        super::scrypt(
286            pass.as_bytes(),
287            salt.as_bytes(),
288            16384,
289            8,
290            1,
291            0,
292            &mut actual,
293        )
294        .unwrap();
295        assert_eq!(hex::encode(&actual[..]), expected);
296    }
297}