Skip to content

Commit 9b2e293

Browse files
committed
pkey_ctx/set_nonce_type: use ParamBuilder
Simplifies and safens the code
1 parent 16d071a commit 9b2e293

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

openssl/src/pkey_ctx.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ use crate::cipher::CipherRef;
7070
use crate::error::ErrorStack;
7171
use crate::md::MdRef;
7272
use crate::nid::Nid;
73+
#[cfg(ossl320)]
74+
use crate::params::ParamBuilder;
7375
#[cfg(ossl300)]
7476
use crate::params::ParamsRef;
7577
#[cfg(ossl300)]
@@ -917,6 +919,14 @@ impl<T> PkeyCtxRef<T> {
917919
}
918920
}
919921

922+
/// Sets parameters on the given context
923+
#[corresponds(EVP_PKEY_CTX_set_params)]
924+
#[cfg(ossl300)]
925+
#[allow(dead_code)]
926+
fn set_params(&mut self, params: &ParamsRef<'_>) -> Result<(), ErrorStack> {
927+
cvt(unsafe { ffi::EVP_PKEY_CTX_set_params(self.as_ptr(), params.as_ptr()) }).map(|_| ())
928+
}
929+
920930
/// Sets the nonce type for a private key context.
921931
///
922932
/// The nonce for DSA and ECDSA can be either random (the default) or deterministic (as defined by RFC 6979).
@@ -926,17 +936,10 @@ impl<T> PkeyCtxRef<T> {
926936
#[cfg(ossl320)]
927937
#[corresponds(EVP_PKEY_CTX_set_params)]
928938
pub fn set_nonce_type(&mut self, nonce_type: NonceType) -> Result<(), ErrorStack> {
929-
let nonce_field_name = c_str(b"nonce-type\0");
930-
let mut nonce_type = nonce_type.0;
931-
unsafe {
932-
let param_nonce =
933-
ffi::OSSL_PARAM_construct_uint(nonce_field_name.as_ptr(), &mut nonce_type);
934-
let param_end = ffi::OSSL_PARAM_construct_end();
935-
936-
let params = [param_nonce, param_end];
937-
cvt(ffi::EVP_PKEY_CTX_set_params(self.as_ptr(), params.as_ptr()))?;
938-
}
939-
Ok(())
939+
let params = ParamBuilder::new()
940+
.push_uint(c_str(b"nonce-type\0"), nonce_type.0)?
941+
.build()?;
942+
self.set_params(&params)
940943
}
941944

942945
/// Gets the nonce type for a private key context.

0 commit comments

Comments
 (0)