Skip to content

Commit 00e12f7

Browse files
committed
Use &CStr instead of &[u8] for keys; builder should be mutable
1 parent 69182d1 commit 00e12f7

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

openssl/src/kdf.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ cfg_if::cfg_if! {
3434
use crate::error::ErrorStack;
3535
use crate::ossl_param::OsslParamBuilder;
3636

37-
const OSSL_KDF_PARAM_PASSWORD: &[u8; 5] = b"pass\0";
38-
const OSSL_KDF_PARAM_SALT: &[u8; 5] = b"salt\0";
39-
const OSSL_KDF_PARAM_SECRET: &[u8; 7] = b"secret\0";
40-
const OSSL_KDF_PARAM_ITER: &[u8; 5] = b"iter\0";
41-
const OSSL_KDF_PARAM_SIZE: &[u8; 5] = b"size\0";
42-
const OSSL_KDF_PARAM_THREADS: &[u8; 8] = b"threads\0";
43-
const OSSL_KDF_PARAM_ARGON2_AD: &[u8; 3] = b"ad\0";
44-
const OSSL_KDF_PARAM_ARGON2_LANES: &[u8; 6] = b"lanes\0";
45-
const OSSL_KDF_PARAM_ARGON2_MEMCOST: &[u8; 8] = b"memcost\0";
37+
const OSSL_KDF_PARAM_PASSWORD: &CStr = c"pass";
38+
const OSSL_KDF_PARAM_SALT: &CStr = c"salt";
39+
const OSSL_KDF_PARAM_SECRET: &CStr = c"secret";
40+
const OSSL_KDF_PARAM_ITER: &CStr = c"iter";
41+
const OSSL_KDF_PARAM_SIZE: &CStr = c"size";
42+
const OSSL_KDF_PARAM_THREADS: &CStr = c"threads";
43+
const OSSL_KDF_PARAM_ARGON2_AD: &CStr = c"ad";
44+
const OSSL_KDF_PARAM_ARGON2_LANES: &CStr = c"lanes";
45+
const OSSL_KDF_PARAM_ARGON2_MEMCOST: &CStr = c"memcost";
4646

4747
#[allow(clippy::too_many_arguments)]
4848
pub fn argon2d(
@@ -120,7 +120,7 @@ cfg_if::cfg_if! {
120120
if max_threads > 0 {
121121
threads = cmp::min(lanes, cmp::min(max_threads, u32::MAX as u64) as u32);
122122
}
123-
let bld = OsslParamBuilder::new()?;
123+
let mut bld = OsslParamBuilder::new()?;
124124
bld.add_octet_string(OSSL_KDF_PARAM_PASSWORD, pass)?;
125125
bld.add_octet_string(OSSL_KDF_PARAM_SALT, salt)?;
126126
bld.add_uint(OSSL_KDF_PARAM_THREADS, threads)?;

openssl/src/ossl_param.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,12 @@ foreign_type_and_impl_send_sync! {
3333
pub struct OsslParamRef;
3434
}
3535

36-
impl OsslParam {}
37-
3836
impl OsslParamRef {
3937
/// Locates the `OsslParam` in the `OsslParam` array
4038
#[corresponds(OSSL_PARAM_locate)]
41-
pub fn locate(&self, key: &[u8]) -> Result<&OsslParamRef, ErrorStack> {
39+
pub fn locate(&self, key: &CStr) -> Result<&OsslParamRef, ErrorStack> {
4240
unsafe {
43-
let param = cvt_p(ffi::OSSL_PARAM_locate(
44-
self.as_ptr(),
45-
key.as_ptr() as *const c_char,
46-
))?;
41+
let param = cvt_p(ffi::OSSL_PARAM_locate(self.as_ptr(), key.as_ptr()))?;
4742
Ok(OsslParamRef::from_ptr(param))
4843
}
4944
}
@@ -109,7 +104,7 @@ impl OsslParamBuilder {
109104

110105
/// Constructs the `OsslParam`.
111106
#[corresponds(OSSL_PARAM_BLD_to_param)]
112-
pub fn to_param(&self) -> Result<OsslParam, ErrorStack> {
107+
pub fn to_param(&mut self) -> Result<OsslParam, ErrorStack> {
113108
unsafe {
114109
let params = cvt_p(ffi::OSSL_PARAM_BLD_to_param(self.0))?;
115110
Ok(OsslParam::from_ptr(params))
@@ -122,11 +117,11 @@ impl OsslParamBuilderRef {
122117
///
123118
/// Note, that both key and bn need to exist until the `to_param` is called!
124119
#[corresponds(OSSL_PARAM_BLD_push_BN)]
125-
pub fn add_bn(&self, key: &[u8], bn: &BigNumRef) -> Result<(), ErrorStack> {
120+
pub fn add_bn(&mut self, key: &CStr, bn: &BigNumRef) -> Result<(), ErrorStack> {
126121
unsafe {
127122
cvt(ffi::OSSL_PARAM_BLD_push_BN(
128123
self.as_ptr(),
129-
key.as_ptr() as *const c_char,
124+
key.as_ptr(),
130125
bn.as_ptr(),
131126
))
132127
.map(|_| ())
@@ -137,11 +132,11 @@ impl OsslParamBuilderRef {
137132
///
138133
/// Note, that both `key` and `buf` need to exist until the `to_param` is called!
139134
#[corresponds(OSSL_PARAM_BLD_push_utf8_string)]
140-
pub fn add_utf8_string(&self, key: &[u8], buf: &str) -> Result<(), ErrorStack> {
135+
pub fn add_utf8_string(&mut self, key: &CStr, buf: &str) -> Result<(), ErrorStack> {
141136
unsafe {
142137
cvt(ffi::OSSL_PARAM_BLD_push_utf8_string(
143138
self.as_ptr(),
144-
key.as_ptr() as *const c_char,
139+
key.as_ptr(),
145140
buf.as_ptr() as *const c_char,
146141
buf.len(),
147142
))
@@ -153,11 +148,11 @@ impl OsslParamBuilderRef {
153148
///
154149
/// Note, that both `key` and `buf` need to exist until the `to_param` is called!
155150
#[corresponds(OSSL_PARAM_BLD_push_octet_string)]
156-
pub fn add_octet_string(&self, key: &[u8], buf: &[u8]) -> Result<(), ErrorStack> {
151+
pub fn add_octet_string(&mut self, key: &CStr, buf: &[u8]) -> Result<(), ErrorStack> {
157152
unsafe {
158153
cvt(ffi::OSSL_PARAM_BLD_push_octet_string(
159154
self.as_ptr(),
160-
key.as_ptr() as *const c_char,
155+
key.as_ptr(),
161156
buf.as_ptr() as *const c_void,
162157
buf.len(),
163158
))
@@ -169,11 +164,11 @@ impl OsslParamBuilderRef {
169164
///
170165
/// Note, that both `key` and `buf` need to exist until the `to_param` is called!
171166
#[corresponds(OSSL_PARAM_BLD_push_uint)]
172-
pub fn add_uint(&self, key: &[u8], val: u32) -> Result<(), ErrorStack> {
167+
pub fn add_uint(&mut self, key: &CStr, val: u32) -> Result<(), ErrorStack> {
173168
unsafe {
174169
cvt(ffi::OSSL_PARAM_BLD_push_uint(
175170
self.as_ptr(),
176-
key.as_ptr() as *const c_char,
171+
key.as_ptr(),
177172
val as c_uint,
178173
))
179174
.map(|_| ())

0 commit comments

Comments
 (0)