Skip to content

Commit 01b045f

Browse files
authored
Merge pull request #2434 from huwcbjones/huw/pkey-ctx-ec-keygen
pkey_ctx: add ability to generate EC params & keys
2 parents fc8f3da + 7484506 commit 01b045f

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

openssl-sys/src/ec.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ use super::*;
55

66
pub const OPENSSL_EC_NAMED_CURVE: c_int = 1;
77

8+
cfg_if! {
9+
if #[cfg(not(ossl300))] {
10+
pub unsafe fn EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx: *mut EVP_PKEY_CTX, nid: c_int) -> c_int {
11+
EVP_PKEY_CTX_ctrl(
12+
ctx,
13+
EVP_PKEY_EC,
14+
EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN,
15+
EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID,
16+
nid,
17+
ptr::null_mut(),
18+
)
19+
}
20+
}
21+
}
822
#[cfg(ossl300)]
923
pub unsafe fn EVP_EC_gen(curve: *const c_char) -> *mut EVP_PKEY {
1024
EVP_PKEY_Q_keygen(
@@ -14,3 +28,5 @@ pub unsafe fn EVP_EC_gen(curve: *const c_char) -> *mut EVP_PKEY {
1428
curve,
1529
)
1630
}
31+
32+
pub const EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: c_int = EVP_PKEY_ALG_CTRL + 1;

openssl-sys/src/handwritten/ec.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use super::super::*;
22
use libc::*;
33

4+
#[cfg(ossl300)]
5+
extern "C" {
6+
pub fn EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx: *mut EVP_PKEY_CTX, nid: c_int) -> c_int;
7+
}
8+
49
#[repr(C)]
510
#[derive(Copy, Clone)]
611
pub enum point_conversion_form_t {

openssl/src/pkey_ctx.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ let cmac_key = ctx.keygen().unwrap();
6868
use crate::cipher::CipherRef;
6969
use crate::error::ErrorStack;
7070
use crate::md::MdRef;
71+
use crate::nid::Nid;
7172
use crate::pkey::{HasPrivate, HasPublic, Id, PKey, PKeyRef, Params, Private};
7273
use crate::rsa::Padding;
7374
use crate::sign::RsaPssSaltlen;
@@ -463,6 +464,22 @@ impl<T> PkeyCtxRef<T> {
463464
Ok(())
464465
}
465466

467+
/// Sets the EC paramgen curve NID.
468+
///
469+
/// This is only useful for EC keys.
470+
#[corresponds(EVP_PKEY_CTX_set_ec_paramgen_curve_nid)]
471+
#[inline]
472+
pub fn set_ec_paramgen_curve_nid(&mut self, nid: Nid) -> Result<(), ErrorStack> {
473+
unsafe {
474+
cvt(ffi::EVP_PKEY_CTX_set_ec_paramgen_curve_nid(
475+
self.as_ptr(),
476+
nid.as_raw(),
477+
))?;
478+
}
479+
480+
Ok(())
481+
}
482+
466483
/// Returns the RSA padding mode in use.
467484
///
468485
/// This is only useful for RSA keys.
@@ -983,6 +1000,17 @@ mod test {
9831000
assert_eq!(params.size(), size);
9841001
}
9851002

1003+
#[test]
1004+
fn ec_keygen() {
1005+
let mut ctx = PkeyCtx::new_id(Id::EC).unwrap();
1006+
ctx.paramgen_init().unwrap();
1007+
ctx.set_ec_paramgen_curve_nid(Nid::X9_62_PRIME256V1)
1008+
.unwrap();
1009+
let params = ctx.paramgen().unwrap();
1010+
1011+
assert_eq!(params.size(), 72);
1012+
}
1013+
9861014
#[test]
9871015
#[cfg(any(ossl110, boringssl, libressl360, awslc))]
9881016
fn hkdf() {

0 commit comments

Comments
 (0)