-
Notifications
You must be signed in to change notification settings - Fork 793
ML-KEM/ML-DSA part 2: param builder #2451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+203
−65
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
69182d1
ML-KEM/ML-DSA part 2: param builder
swenson 00e12f7
Use &CStr instead of &[u8] for keys; builder should be mutable
swenson 131cddd
Remove allow dead code from param builder
swenson 6e4440f
Use CStr::from_bytes_with_nul_unchecked instead of c-string literal, …
swenson 7642cc6
Rename OsslParam to OsslParamArray and add clarifying comments
swenson 99520b0
Make OsslParam and friends pub(crate); clarify comments about OsslPar…
swenson 20091b3
Create separate type for the OSSL_PARAM array and a reference to an i…
swenson deae632
Remove out OsslParam for now
swenson 21a83a6
Annotate lifetimes for builder methods
swenson 6edce67
Add explicit lifetime to the param builder since it does not own its …
swenson b32f820
Remove 'a from mut self param builder references so that they don't l…
swenson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
//! OSSL_PARAM management for OpenSSL 3.* | ||
//! | ||
//! The OSSL_PARAM structure represents an array of generic | ||
//! attributes that can represent various | ||
//! properties in OpenSSL, including keys and operations. | ||
//! | ||
//! This is always represented as an array of OSSL_PARAM | ||
//! structures, terminated by an entry with a NULL key. | ||
//! | ||
//! For convinience, the OSSL_PARAM_BLD builder can be used to | ||
//! dynamically construct these structures. | ||
//! | ||
//! Note, that this module is available only in OpenSSL 3.* and | ||
//! only internally for this crate. | ||
|
||
use crate::bn::BigNumRef; | ||
use crate::error::ErrorStack; | ||
use crate::{cvt, cvt_p}; | ||
use foreign_types::{ForeignType, ForeignTypeRef}; | ||
use libc::{c_char, c_uint, c_void}; | ||
use openssl_macros::corresponds; | ||
use std::ffi::CStr; | ||
use std::marker::PhantomData; | ||
|
||
foreign_type_and_impl_send_sync! { | ||
// This is the singular type, but it is always allocated | ||
// and used as an array of such types. | ||
type CType = ffi::OSSL_PARAM; | ||
// OSSL_PARMA_free correctly frees the entire array. | ||
fn drop = ffi::OSSL_PARAM_free; | ||
|
||
/// `OsslParamArray` constructed using `OsslParamBuilder`. | ||
/// Internally this is a pointer to an array of the OSSL_PARAM | ||
/// structures. | ||
pub struct OsslParamArray; | ||
/// Reference to `OsslParamArray`. | ||
pub struct OsslParamArrayRef; | ||
} | ||
|
||
foreign_type_and_impl_send_sync! { | ||
type CType = ffi::OSSL_PARAM_BLD; | ||
fn drop = ffi::OSSL_PARAM_BLD_free; | ||
|
||
/// Builder used to construct `OsslParamArray`. | ||
pub struct OsslParamBuilderInternal; | ||
/// Reference to `OsslParamBuilderInternal`. | ||
pub struct OsslParamBuilderRefInternal; | ||
} | ||
|
||
/// Wrapper around the internal OsslParamBuilderInternal that adds lifetime management | ||
/// since the builder does not own the key and value data that is added to it. | ||
pub struct OsslParamBuilder<'a> { | ||
builder: OsslParamBuilderInternal, | ||
_marker: PhantomData<&'a ()>, | ||
} | ||
|
||
impl<'a> OsslParamBuilder<'a> { | ||
/// Returns a builder for an OsslParamArray. | ||
/// | ||
/// The array is initially empty. | ||
#[corresponds(OSSL_PARAM_BLD_new)] | ||
#[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))] | ||
pub(crate) fn new() -> Result<OsslParamBuilder<'a>, ErrorStack> { | ||
unsafe { | ||
ffi::init(); | ||
|
||
cvt_p(ffi::OSSL_PARAM_BLD_new()).map(|builder| OsslParamBuilder { | ||
builder: OsslParamBuilderInternal(builder), | ||
_marker: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
/// Constructs the `OsslParamArray` and clears this builder. | ||
#[corresponds(OSSL_PARAM_BLD_to_param)] | ||
#[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))] | ||
#[allow(clippy::wrong_self_convention)] | ||
pub(crate) fn to_param(&'a mut self) -> Result<OsslParamArray, ErrorStack> { | ||
unsafe { | ||
let params = cvt_p(ffi::OSSL_PARAM_BLD_to_param(self.as_ptr()))?; | ||
Ok(OsslParamArray::from_ptr(params)) | ||
} | ||
} | ||
|
||
/// Adds a `BigNum` to `OsslParamBuilder`. | ||
#[corresponds(OSSL_PARAM_BLD_push_BN)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub(crate) fn add_bn(&mut self, key: &'a CStr, bn: &'a BigNumRef) -> Result<(), ErrorStack> { | ||
unsafe { | ||
cvt(ffi::OSSL_PARAM_BLD_push_BN( | ||
self.as_ptr(), | ||
key.as_ptr(), | ||
bn.as_ptr(), | ||
)) | ||
.map(|_| ()) | ||
} | ||
} | ||
|
||
/// Adds a utf8 string to `OsslParamBuilder`. | ||
#[corresponds(OSSL_PARAM_BLD_push_utf8_string)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub(crate) fn add_utf8_string( | ||
&mut self, | ||
key: &'a CStr, | ||
buf: &'a str, | ||
) -> Result<(), ErrorStack> { | ||
unsafe { | ||
cvt(ffi::OSSL_PARAM_BLD_push_utf8_string( | ||
self.as_ptr(), | ||
key.as_ptr(), | ||
buf.as_ptr() as *const c_char, | ||
buf.len(), | ||
)) | ||
.map(|_| ()) | ||
} | ||
} | ||
|
||
/// Adds a octet string to `OsslParamBuilder`. | ||
#[corresponds(OSSL_PARAM_BLD_push_octet_string)] | ||
#[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))] | ||
pub(crate) fn add_octet_string( | ||
&mut self, | ||
key: &'a CStr, | ||
buf: &'a [u8], | ||
) -> Result<(), ErrorStack> { | ||
unsafe { | ||
cvt(ffi::OSSL_PARAM_BLD_push_octet_string( | ||
self.as_ptr(), | ||
key.as_ptr(), | ||
buf.as_ptr() as *const c_void, | ||
buf.len(), | ||
)) | ||
.map(|_| ()) | ||
} | ||
} | ||
|
||
/// Adds a unsigned int to `OsslParamBuilder`. | ||
#[corresponds(OSSL_PARAM_BLD_push_uint)] | ||
#[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))] | ||
pub(crate) fn add_uint(&mut self, key: &'a CStr, val: u32) -> Result<(), ErrorStack> { | ||
unsafe { | ||
cvt(ffi::OSSL_PARAM_BLD_push_uint( | ||
self.as_ptr(), | ||
key.as_ptr(), | ||
val as c_uint, | ||
)) | ||
.map(|_| ()) | ||
} | ||
} | ||
|
||
/// Returns a raw pointer to the underlying `OSSL_PARAM_BLD` structure. | ||
pub(crate) unsafe fn as_ptr(&mut self) -> *mut ffi::OSSL_PARAM_BLD { | ||
self.builder.as_ptr() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.