-
-
Notifications
You must be signed in to change notification settings - Fork 790
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
Open
swenson
wants to merge
9
commits into
sfackler:master
Choose a base branch
from
swenson:pqc-param-builder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 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 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,193 @@ | ||
//! 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::{BigNum, BigNumRef}; | ||
use crate::error::ErrorStack; | ||
use crate::util; | ||
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::ptr; | ||
|
||
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; | ||
} | ||
|
||
impl OsslParamArrayRef { | ||
/// Locates the `OsslParamArray` in the `OsslParamArray` array | ||
#[corresponds(OSSL_PARAM_locate)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub fn locate(&self, key: &CStr) -> Result<&OsslParamArrayRef, ErrorStack> { | ||
swenson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafe { | ||
let param = cvt_p(ffi::OSSL_PARAM_locate(self.as_ptr(), key.as_ptr()))?; | ||
Ok(OsslParamArrayRef::from_ptr(param)) | ||
} | ||
} | ||
|
||
/// Get `BigNum` from the current `OsslParamArray` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean? I think this operation only makes sense in the context of a single There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I'll clarify these comments. |
||
#[corresponds(OSSL_PARAM_get_BN)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub fn get_bn(&self) -> Result<BigNum, ErrorStack> { | ||
unsafe { | ||
let mut bn: *mut ffi::BIGNUM = ptr::null_mut(); | ||
cvt(ffi::OSSL_PARAM_get_BN(self.as_ptr(), &mut bn))?; | ||
Ok(BigNum::from_ptr(bn)) | ||
} | ||
} | ||
|
||
/// Get `&str` from the current `OsslParamArray` | ||
#[corresponds(OSSL_PARAM_get_utf8_string)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub fn get_utf8_string(&self) -> Result<&str, ErrorStack> { | ||
unsafe { | ||
let mut val: *const c_char = ptr::null_mut(); | ||
cvt(ffi::OSSL_PARAM_get_utf8_string_ptr(self.as_ptr(), &mut val))?; | ||
Ok(CStr::from_ptr(val).to_str().unwrap()) | ||
} | ||
} | ||
|
||
/// Get octet string (as `&[u8]) from the current `OsslParamArray` | ||
#[corresponds(OSSL_PARAM_get_octet_string)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub fn get_octet_string(&self) -> Result<&[u8], ErrorStack> { | ||
unsafe { | ||
let mut val: *const c_void = ptr::null_mut(); | ||
let mut val_len: usize = 0; | ||
cvt(ffi::OSSL_PARAM_get_octet_string_ptr( | ||
self.as_ptr(), | ||
&mut val, | ||
&mut val_len, | ||
))?; | ||
Ok(util::from_raw_parts(val as *const u8, val_len)) | ||
} | ||
} | ||
} | ||
|
||
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 OsslParamBuilder; | ||
/// Reference to `OsslParamBuilder`. | ||
pub struct OsslParamBuilderRef; | ||
} | ||
|
||
impl OsslParamBuilder { | ||
/// 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 fn new() -> Result<OsslParamBuilder, ErrorStack> { | ||
unsafe { | ||
ffi::init(); | ||
|
||
cvt_p(ffi::OSSL_PARAM_BLD_new()).map(OsslParamBuilder) | ||
} | ||
} | ||
|
||
/// 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 fn to_param(&mut self) -> Result<OsslParamArray, ErrorStack> { | ||
unsafe { | ||
let params = cvt_p(ffi::OSSL_PARAM_BLD_to_param(self.0))?; | ||
Ok(OsslParamArray::from_ptr(params)) | ||
} | ||
} | ||
} | ||
|
||
impl OsslParamBuilderRef { | ||
/// Adds a `BigNum` to `OsslParamBuilder`. | ||
/// | ||
/// Note, that both key and bn need to exist until the `to_param` is called! | ||
#[corresponds(OSSL_PARAM_BLD_push_BN)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub fn add_bn(&mut self, key: &CStr, bn: &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`. | ||
/// | ||
/// Note, that both `key` and `buf` need to exist until the `to_param` is called! | ||
#[corresponds(OSSL_PARAM_BLD_push_utf8_string)] | ||
#[allow(dead_code)] // TODO: remove when when used by ML-DSA / ML-KEM | ||
pub fn add_utf8_string(&mut self, key: &CStr, buf: &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`. | ||
/// | ||
/// Note, that both `key` and `buf` need to exist until the `to_param` is called! | ||
#[corresponds(OSSL_PARAM_BLD_push_octet_string)] | ||
#[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))] | ||
pub fn add_octet_string(&mut self, key: &CStr, buf: &[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`. | ||
/// | ||
/// Note, that both `key` and `buf` need to exist until the `to_param` is called! | ||
#[corresponds(OSSL_PARAM_BLD_push_uint)] | ||
#[cfg_attr(any(not(ossl320), osslconf = "OPENSSL_NO_ARGON2"), allow(dead_code))] | ||
pub fn add_uint(&mut self, key: &CStr, val: u32) -> Result<(), ErrorStack> { | ||
unsafe { | ||
cvt(ffi::OSSL_PARAM_BLD_push_uint( | ||
self.as_ptr(), | ||
key.as_ptr(), | ||
val as c_uint, | ||
)) | ||
.map(|_| ()) | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.