bitwarden_crypto_primitives/
pbkdf2.rs1use std::num::NonZeroU32;
4
5use crate::backend;
6
7pub fn pbkdf2_hmac_sha256(password: &[u8], salt: &[u8], rounds: NonZeroU32, out: &mut [u8]) {
9 backend::pbkdf2::pbkdf2_hmac_sha256(password, salt, rounds, out);
10}
11
12#[cfg(test)]
13mod tests {
14 use super::*;
15
16 fn derive(password: &[u8], salt: &[u8], rounds: u32, len: usize) -> String {
17 let mut out = vec![0u8; len];
18 let rounds = NonZeroU32::new(rounds).expect("non-zero rounds");
19 pbkdf2_hmac_sha256(password, salt, rounds, &mut out);
20 hex::encode(out)
21 }
22
23 #[test]
25 fn known_answer() {
26 assert_eq!(
27 derive(b"passwd", b"salt", 1, 64),
28 "55ac046e56e3089fec1691c22544b605f94185216dde0465e68b9d57c20dacbc\
29 49ca9cccf179b645991664b39d77ef317c71b845b1e30bd509112041d3a19783"
30 );
31 }
32
33 #[test]
34 fn known_answer_empty_password_and_salt() {
35 assert_eq!(
36 derive(b"", b"salt", 2, 32),
37 "62384466264daadc4144018c6bd864648272b34da8980d31521ffcce92ae003b"
38 );
39 assert_eq!(
40 derive(b"password", b"", 2, 32),
41 "8bc2f9167a81cdcfad1235cd9047f1136271c1f978fcfcb35e22dbeafa4634f6"
42 );
43 }
44
45 #[test]
47 fn known_answer_partial_block_and_many_rounds() {
48 assert_eq!(
49 derive(b"password", b"salt", 2, 33),
50 "ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43\
51 83"
52 );
53 assert_eq!(
54 derive(b"password", b"salt", 100_000, 32),
55 "0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5"
56 );
57 }
58}