Skip to content

Commit 63a93bc

Browse files
authored
Added PBKDF1, for use in #12296 (#12369)
1 parent a2ce5b3 commit 63a93bc

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/rust/cryptography-crypto/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33
// for complete details.
44

5+
pub mod pbkdf1;
56
pub mod pkcs12;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// This file is dual licensed under the terms of the Apache License, Version
2+
// 2.0, and the BSD License. See the LICENSE file in the root of this repository
3+
// for complete details.
4+
5+
/// This is the OpenSSL KDF that's used in decrypting PEM blocks. It is a
6+
/// generalization of PBKDF1.
7+
pub fn openssl_kdf(
8+
hash_alg: openssl::hash::MessageDigest,
9+
password: &[u8],
10+
salt: [u8; 8],
11+
length: usize,
12+
) -> Result<Vec<u8>, openssl::error::ErrorStack> {
13+
let mut key = Vec::with_capacity(length);
14+
15+
while key.len() < length {
16+
let mut h = openssl::hash::Hasher::new(hash_alg)?;
17+
18+
if !key.is_empty() {
19+
h.update(&key[key.len() - hash_alg.size()..])?;
20+
}
21+
22+
h.update(password)?;
23+
h.update(&salt)?;
24+
25+
let digest = h.finish()?;
26+
let size = digest.len().min(length - key.len());
27+
key.extend_from_slice(&digest[..size]);
28+
}
29+
30+
Ok(key)
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::openssl_kdf;
36+
37+
#[test]
38+
fn test_openssl_kdf() {
39+
for (md, password, salt, expected) in [
40+
(
41+
openssl::hash::MessageDigest::md5(),
42+
b"password123" as &[u8],
43+
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
44+
&[
45+
0x31, 0xcc, 0x91, 0x39, 0x80, 0x69, 0x98, 0xb2, 0xaa, 0xc3, 0x66, 0xcf, 0x40,
46+
0x1b, 0x49, 0xdc, 0x0d, 0x37, 0xbd, 0x5c, 0x22, 0x52, 0xc7, 0xcb,
47+
][..],
48+
),
49+
(
50+
openssl::hash::MessageDigest::md5(),
51+
b"diffpassword",
52+
[0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22],
53+
&[
54+
0x89, 0x9b, 0x70, 0x37, 0xdb, 0x29, 0x42, 0x69, 0xdb, 0x4d, 0x67, 0xb9, 0x81,
55+
0x67, 0xa7, 0x59, 0xfd, 0xec, 0x5d, 0x0f, 0x57, 0x52, 0xef, 0x04,
56+
],
57+
),
58+
(
59+
openssl::hash::MessageDigest::md5(),
60+
b"secret_key",
61+
[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
62+
&[
63+
0xa1, 0x98, 0x00, 0xf9, 0xab, 0x97, 0x36, 0xa1, 0x83, 0x4a, 0x19, 0x76, 0x47,
64+
0x25, 0xda, 0x9b,
65+
],
66+
),
67+
(
68+
openssl::hash::MessageDigest::md5(),
69+
b"another_password",
70+
[0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00],
71+
&[
72+
0xc2, 0x09, 0x35, 0x72, 0xe5, 0x62, 0xd4, 0xba, 0x90, 0x4a, 0x5f, 0x46, 0x7f,
73+
0x27, 0xd2, 0x6c,
74+
],
75+
),
76+
(
77+
openssl::hash::MessageDigest::md5(),
78+
b"very_long_and_complex_password",
79+
[0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef],
80+
&[
81+
0x1c, 0x5d, 0xdf, 0xa2, 0xca, 0xca, 0x41, 0xa4, 0xc9, 0xb4, 0x31, 0xd2, 0x9f,
82+
0x04, 0x46, 0x81, 0xd2, 0x2b, 0x4e, 0x40, 0x06, 0x41, 0x5d, 0x37, 0x20, 0xef,
83+
0x01, 0x1d, 0xc7, 0x8a, 0x16, 0xb5,
84+
],
85+
),
86+
(
87+
openssl::hash::MessageDigest::md5(),
88+
b"different_secure_password",
89+
[0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10],
90+
&[
91+
0xca, 0x08, 0xce, 0xb2, 0x46, 0x8f, 0xdc, 0x72, 0x83, 0xc7, 0x0b, 0x8a, 0xd0,
92+
0x94, 0x54, 0x2b, 0x22, 0xd5, 0x1f, 0xf2, 0x5d, 0x0c, 0x1d, 0x99, 0xf3, 0x2f,
93+
0x54, 0xa8, 0x68, 0x95, 0x13, 0xbd,
94+
],
95+
),
96+
] {
97+
let key = openssl_kdf(md, password, salt, expected.len()).unwrap();
98+
assert_eq!(key, expected);
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)