Skip to content

Commit 5ff8594

Browse files
committed
rename to kdf, remove thread arg
1 parent 826f2a0 commit 5ff8594

File tree

3 files changed

+163
-167
lines changed

3 files changed

+163
-167
lines changed

openssl/src/argon2.rs

Lines changed: 0 additions & 165 deletions
This file was deleted.

openssl/src/kdf.rs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#[cfg(ossl300)]
2+
struct EvpKdf(*mut ffi::EVP_KDF);
3+
4+
#[cfg(ossl300)]
5+
impl Drop for EvpKdf {
6+
fn drop(&mut self) {
7+
unsafe {
8+
ffi::EVP_KDF_free(self.0);
9+
}
10+
}
11+
}
12+
13+
#[cfg(ossl300)]
14+
struct EvpKdfCtx(*mut ffi::EVP_KDF_CTX);
15+
16+
#[cfg(ossl300)]
17+
impl Drop for EvpKdfCtx {
18+
fn drop(&mut self) {
19+
unsafe {
20+
ffi::EVP_KDF_CTX_free(self.0);
21+
}
22+
}
23+
}
24+
25+
cfg_if::cfg_if! {
26+
if #[cfg(all(ossl320, not(osslconf = "OPENSSL_NO_ARGON2")))] {
27+
use std::ffi::{c_char, c_void};
28+
use std::mem::MaybeUninit;
29+
use std::ptr;
30+
use crate::{cvt, cvt_p};
31+
use crate::error::ErrorStack;
32+
33+
/// Derives a key using the argon2id algorithm.
34+
///
35+
/// This function currently does not support multi-threaded operation, so
36+
/// lanes greater than 1 will be processed sequentially.
37+
///
38+
/// Requires OpenSSL 3.2.0 or newer.
39+
#[allow(clippy::too_many_arguments)]
40+
pub fn argon2id(
41+
pass: &[u8],
42+
salt: &[u8],
43+
ad: Option<&[u8]>,
44+
secret: Option<&[u8]>,
45+
mut iter: u32,
46+
mut lanes: u32,
47+
mut memcost: u32,
48+
out: &mut [u8],
49+
) -> Result<(), ErrorStack> {
50+
unsafe {
51+
ffi::init();
52+
let mut threads = 1;
53+
let mut params: [ffi::OSSL_PARAM; 10] =
54+
core::array::from_fn(|_| MaybeUninit::<ffi::OSSL_PARAM>::zeroed().assume_init());
55+
let mut idx = 0;
56+
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
57+
b"pass\0".as_ptr() as *const c_char,
58+
pass.as_ptr() as *mut c_void,
59+
pass.len(),
60+
);
61+
idx += 1;
62+
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
63+
b"salt\0".as_ptr() as *const c_char,
64+
salt.as_ptr() as *mut c_void,
65+
salt.len(),
66+
);
67+
idx += 1;
68+
params[idx] =
69+
ffi::OSSL_PARAM_construct_uint(b"threads\0".as_ptr() as *const c_char, &mut threads);
70+
idx += 1;
71+
params[idx] =
72+
ffi::OSSL_PARAM_construct_uint(b"lanes\0".as_ptr() as *const c_char, &mut lanes);
73+
idx += 1;
74+
params[idx] =
75+
ffi::OSSL_PARAM_construct_uint(b"memcost\0".as_ptr() as *const c_char, &mut memcost);
76+
idx += 1;
77+
params[idx] =
78+
ffi::OSSL_PARAM_construct_uint(b"iter\0".as_ptr() as *const c_char, &mut iter);
79+
idx += 1;
80+
let mut size = out.len() as u32;
81+
params[idx] =
82+
ffi::OSSL_PARAM_construct_uint(b"size\0".as_ptr() as *const c_char, &mut size);
83+
idx += 1;
84+
if let Some(ad) = ad {
85+
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
86+
b"ad\0".as_ptr() as *const c_char,
87+
ad.as_ptr() as *mut c_void,
88+
ad.len(),
89+
);
90+
idx += 1;
91+
}
92+
if let Some(secret) = secret {
93+
params[idx] = ffi::OSSL_PARAM_construct_octet_string(
94+
b"secret\0".as_ptr() as *const c_char,
95+
secret.as_ptr() as *mut c_void,
96+
secret.len(),
97+
);
98+
idx += 1;
99+
}
100+
params[idx] = ffi::OSSL_PARAM_construct_end();
101+
102+
let argon2_p = cvt_p(ffi::EVP_KDF_fetch(
103+
ptr::null_mut(),
104+
b"ARGON2ID\0".as_ptr() as *const c_char,
105+
ptr::null(),
106+
))?;
107+
let argon2 = EvpKdf(argon2_p);
108+
let ctx_p = cvt_p(ffi::EVP_KDF_CTX_new(argon2.0))?;
109+
let ctx = EvpKdfCtx(ctx_p);
110+
cvt(ffi::EVP_KDF_derive(
111+
ctx.0,
112+
out.as_mut_ptr(),
113+
out.len(),
114+
params.as_ptr(),
115+
))
116+
.map(|_| ())
117+
}
118+
}
119+
}
120+
}
121+
122+
#[cfg(test)]
123+
mod tests {
124+
#[test]
125+
#[cfg(all(ossl320, not(osslconf = "OPENSSL_NO_ARGON2")))]
126+
fn argon2id() {
127+
// RFC 9106 test vector for argon2id
128+
let pass = hex::decode("0101010101010101010101010101010101010101010101010101010101010101")
129+
.unwrap();
130+
let salt = hex::decode("02020202020202020202020202020202").unwrap();
131+
let secret = hex::decode("0303030303030303").unwrap();
132+
let ad = hex::decode("040404040404040404040404").unwrap();
133+
let expected = "0d640df58d78766c08c037a34a8b53c9d01ef0452d75b65eb52520e96b01e659";
134+
135+
let mut actual = [0 as u8; 32];
136+
super::argon2id(
137+
&pass,
138+
&salt,
139+
Some(&ad),
140+
Some(&secret),
141+
3,
142+
4,
143+
32,
144+
&mut actual,
145+
)
146+
.unwrap();
147+
assert_eq!(hex::encode(&actual[..]), expected);
148+
}
149+
150+
#[test]
151+
#[cfg(all(ossl320, not(osslconf = "OPENSSL_NO_ARGON2")))]
152+
fn argon2id_no_ad_secret() {
153+
// Test vector from OpenSSL
154+
let pass = "";
155+
let salt = hex::decode("02020202020202020202020202020202").unwrap();
156+
let expected = "0a34f1abde67086c82e785eaf17c68382259a264f4e61b91cd2763cb75ac189a";
157+
158+
let mut actual = [0 as u8; 32];
159+
super::argon2id(&pass.as_bytes(), &salt, None, None, 3, 4, 32, &mut actual).unwrap();
160+
assert_eq!(hex::encode(&actual[..]), expected);
161+
}
162+
}

openssl/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ mod bio;
148148
mod util;
149149
pub mod aes;
150150
#[cfg(ossl320)]
151-
#[cfg(all(ossl320, not(osslconf = "OPENSSL_NO_ARGON2")))]
152-
pub mod argon2;
153151
pub mod asn1;
154152
pub mod base64;
155153
pub mod bn;
@@ -171,6 +169,7 @@ pub mod ex_data;
171169
#[cfg(not(any(libressl, ossl300)))]
172170
pub mod fips;
173171
pub mod hash;
172+
pub mod kdf;
174173
#[cfg(ossl300)]
175174
pub mod lib_ctx;
176175
pub mod md;

0 commit comments

Comments
 (0)