Skip to content

pkey_ctx: add ability to generate EC params & keys #2434

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
merged 3 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions openssl-sys/src/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ use super::*;

pub const OPENSSL_EC_NAMED_CURVE: c_int = 1;

cfg_if! {
if #[cfg(not(ossl300))] {
pub unsafe fn EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx: *mut EVP_PKEY_CTX, nid: c_int) -> c_int {
EVP_PKEY_CTX_ctrl(
ctx,
EVP_PKEY_EC,
EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID,
nid,
ptr::null_mut(),
)
}
}
}
#[cfg(ossl300)]
pub unsafe fn EVP_EC_gen(curve: *const c_char) -> *mut EVP_PKEY {
EVP_PKEY_Q_keygen(
Expand All @@ -14,3 +28,5 @@ pub unsafe fn EVP_EC_gen(curve: *const c_char) -> *mut EVP_PKEY {
curve,
)
}

pub const EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: c_int = EVP_PKEY_ALG_CTRL + 1;
5 changes: 5 additions & 0 deletions openssl-sys/src/handwritten/ec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use super::super::*;
use libc::*;

#[cfg(ossl300)]
extern "C" {
pub fn EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx: *mut EVP_PKEY_CTX, nid: c_int) -> c_int;
}

#[repr(C)]
#[derive(Copy, Clone)]
pub enum point_conversion_form_t {
Expand Down
28 changes: 28 additions & 0 deletions openssl/src/pkey_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ let cmac_key = ctx.keygen().unwrap();
use crate::cipher::CipherRef;
use crate::error::ErrorStack;
use crate::md::MdRef;
use crate::nid::Nid;
use crate::pkey::{HasPrivate, HasPublic, Id, PKey, PKeyRef, Params, Private};
use crate::rsa::Padding;
use crate::sign::RsaPssSaltlen;
Expand Down Expand Up @@ -463,6 +464,22 @@ impl<T> PkeyCtxRef<T> {
Ok(())
}

/// Sets the EC paramgen curve NID.
///
/// This is only useful for EC keys.
#[corresponds(EVP_PKEY_CTX_set_ec_paramgen_curve_nid)]
#[inline]
pub fn set_ec_paramgen_curve_nid(&mut self, nid: Nid) -> Result<(), ErrorStack> {
unsafe {
cvt(ffi::EVP_PKEY_CTX_set_ec_paramgen_curve_nid(
self.as_ptr(),
nid.as_raw(),
))?;
}

Ok(())
}

/// Returns the RSA padding mode in use.
///
/// This is only useful for RSA keys.
Expand Down Expand Up @@ -983,6 +1000,17 @@ mod test {
assert_eq!(params.size(), size);
}

#[test]
fn ec_keygen() {
let mut ctx = PkeyCtx::new_id(Id::EC).unwrap();
ctx.paramgen_init().unwrap();
ctx.set_ec_paramgen_curve_nid(Nid::X9_62_PRIME256V1)
.unwrap();
let params = ctx.paramgen().unwrap();

assert_eq!(params.size(), 72);
}

#[test]
#[cfg(any(ossl110, boringssl, libressl360, awslc))]
fn hkdf() {
Expand Down
Loading