@@ -27,7 +27,9 @@ cfg_if::cfg_if! {
27
27
use std:: ffi:: { c_char, c_void} ;
28
28
use std:: mem:: MaybeUninit ;
29
29
use std:: ptr;
30
+ use foreign_types:: ForeignTypeRef ;
30
31
use crate :: { cvt, cvt_p} ;
32
+ use crate :: lib_ctx:: LibCtxRef ;
31
33
use crate :: error:: ErrorStack ;
32
34
33
35
/// Derives a key using the argon2id algorithm.
@@ -38,6 +40,7 @@ cfg_if::cfg_if! {
38
40
/// Requires OpenSSL 3.2.0 or newer.
39
41
#[ allow( clippy:: too_many_arguments) ]
40
42
pub fn argon2id(
43
+ ctx: Option <& LibCtxRef >,
41
44
pass: & [ u8 ] ,
42
45
salt: & [ u8 ] ,
43
46
ad: Option <& [ u8 ] >,
@@ -49,6 +52,8 @@ cfg_if::cfg_if! {
49
52
) -> Result <( ) , ErrorStack > {
50
53
unsafe {
51
54
ffi:: init( ) ;
55
+ let libctx = ctx. map_or( ptr:: null_mut( ) , ForeignTypeRef :: as_ptr) ;
56
+
52
57
let mut threads = 1 ;
53
58
let mut params: [ ffi:: OSSL_PARAM ; 10 ] =
54
59
core:: array:: from_fn( |_| MaybeUninit :: <ffi:: OSSL_PARAM >:: zeroed( ) . assume_init( ) ) ;
@@ -100,7 +105,7 @@ cfg_if::cfg_if! {
100
105
params[ idx] = ffi:: OSSL_PARAM_construct_end ( ) ;
101
106
102
107
let argon2 = EvpKdf ( cvt_p( ffi:: EVP_KDF_fetch (
103
- ptr :: null_mut ( ) ,
108
+ libctx ,
104
109
b"ARGON2ID\0 " . as_ptr( ) as * const c_char,
105
110
ptr:: null( ) ,
106
111
) ) ?) ;
@@ -132,6 +137,7 @@ mod tests {
132
137
133
138
let mut actual = [ 0u8 ; 32 ] ;
134
139
super :: argon2id (
140
+ None ,
135
141
& pass,
136
142
& salt,
137
143
Some ( & ad) ,
@@ -154,7 +160,7 @@ mod tests {
154
160
let expected = "0a34f1abde67086c82e785eaf17c68382259a264f4e61b91cd2763cb75ac189a" ;
155
161
156
162
let mut actual = [ 0u8 ; 32 ] ;
157
- super :: argon2id ( pass, & salt, None , None , 3 , 4 , 32 , & mut actual) . unwrap ( ) ;
163
+ super :: argon2id ( None , pass, & salt, None , None , 3 , 4 , 32 , & mut actual) . unwrap ( ) ;
158
164
assert_eq ! ( hex:: encode( & actual[ ..] ) , expected) ;
159
165
}
160
166
}
0 commit comments