Skip to content

pkey_ctx: add ability to generate RSA keys #2431

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 7, 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
3 changes: 3 additions & 0 deletions openssl-sys/src/handwritten/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use libc::*;
cfg_if! {
if #[cfg(ossl300)] {
extern "C" {
pub fn EVP_PKEY_CTX_set_rsa_keygen_bits(ctx: *mut EVP_PKEY_CTX, bits: c_int) -> c_int;
pub fn EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx: *mut EVP_PKEY_CTX, pubexp: *mut BIGNUM) -> c_int;

pub fn EVP_PKEY_CTX_set_rsa_padding(ctx: *mut EVP_PKEY_CTX, pad_mode: c_int) -> c_int;
pub fn EVP_PKEY_CTX_get_rsa_padding(ctx: *mut EVP_PKEY_CTX, pad_mode: *mut c_int) -> c_int;

Expand Down
22 changes: 22 additions & 0 deletions openssl-sys/src/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ pub const RSA_F4: c_long = 0x10001;

cfg_if! {
if #[cfg(not(ossl300))] {
pub unsafe fn EVP_PKEY_CTX_set_rsa_keygen_bits(ctx: *mut EVP_PKEY_CTX, bits: c_int) -> c_int {
EVP_PKEY_CTX_ctrl(
ctx,
EVP_PKEY_RSA,
EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_RSA_KEYGEN_BITS,
bits,
ptr::null_mut(),
)
}
pub unsafe fn EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx: *mut EVP_PKEY_CTX, pubexp: *mut BIGNUM) -> c_int {
EVP_PKEY_CTX_ctrl(
ctx,
EVP_PKEY_RSA,
EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP,
0,
pubexp as *mut _,
)
}
pub unsafe fn EVP_PKEY_CTX_set_rsa_padding(ctx: *mut EVP_PKEY_CTX, pad: c_int) -> c_int {
EVP_PKEY_CTX_ctrl(
ctx,
Expand Down Expand Up @@ -82,6 +102,8 @@ pub unsafe fn EVP_PKEY_CTX_set0_rsa_oaep_label(

pub const EVP_PKEY_CTRL_RSA_PADDING: c_int = EVP_PKEY_ALG_CTRL + 1;
pub const EVP_PKEY_CTRL_RSA_PSS_SALTLEN: c_int = EVP_PKEY_ALG_CTRL + 2;
pub const EVP_PKEY_CTRL_RSA_KEYGEN_BITS: c_int = EVP_PKEY_ALG_CTRL + 3;
pub const EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: c_int = EVP_PKEY_ALG_CTRL + 4;

pub const EVP_PKEY_CTRL_RSA_MGF1_MD: c_int = EVP_PKEY_ALG_CTRL + 5;

Expand Down
57 changes: 57 additions & 0 deletions openssl/src/pkey_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ let cmac_key = ctx.keygen().unwrap();
//! let valid = ctx.verify(text, &signature).unwrap();
//! assert!(valid);
//! ```
use crate::bn::BigNumRef;
#[cfg(not(any(boringssl, awslc)))]
use crate::cipher::CipherRef;
use crate::error::ErrorStack;
Expand All @@ -73,6 +74,7 @@ use crate::pkey::{HasPrivate, HasPublic, Id, PKey, PKeyRef, Params, Private};
use crate::rsa::Padding;
use crate::sign::RsaPssSaltlen;
use crate::{cvt, cvt_p};
use cfg_if::cfg_if;
use foreign_types::{ForeignType, ForeignTypeRef};
#[cfg(not(any(boringssl, awslc)))]
use libc::c_int;
Expand Down Expand Up @@ -544,6 +546,48 @@ impl<T> PkeyCtxRef<T> {
Ok(())
}

/// Sets the RSA keygen bits.
///
/// This is only useful for RSA keys.
#[corresponds(EVP_PKEY_CTX_set_rsa_keygen_bits)]
#[inline]
pub fn set_rsa_keygen_bits(&mut self, bits: u32) -> Result<(), ErrorStack> {
unsafe {
cvt(ffi::EVP_PKEY_CTX_set_rsa_keygen_bits(
self.as_ptr(),
bits as i32,
))?;
}

Ok(())
}

/// Sets the RSA keygen public exponent.
///
/// This is only useful for RSA keys.
#[corresponds(EVP_PKEY_CTX_set1_rsa_keygen_pubexp)]
#[inline]
pub fn set_rsa_keygen_pubexp(&mut self, pubexp: &BigNumRef) -> Result<(), ErrorStack> {
unsafe {
cfg_if! {
if #[cfg(ossl300)] {
cvt(ffi::EVP_PKEY_CTX_set1_rsa_keygen_pubexp(
self.as_ptr(),
pubexp.as_ptr(),
))?;
} else {
cvt(ffi::EVP_PKEY_CTX_set_rsa_keygen_pubexp(
self.as_ptr(),
// Dupe the BN because the EVP_PKEY_CTX takes ownership of it and will free it.
cvt_p(ffi::BN_dup(pubexp.as_ptr()))?,
))?;
}
}
}

Ok(())
}

/// Sets the RSA PSS salt length.
///
/// This is only useful for RSA keys.
Expand Down Expand Up @@ -874,6 +918,7 @@ impl<T> PkeyCtxRef<T> {
#[cfg(test)]
mod test {
use super::*;
use crate::bn::BigNum;
#[cfg(not(any(boringssl, awslc)))]
use crate::cipher::Cipher;
use crate::ec::{EcGroup, EcKey};
Expand Down Expand Up @@ -1057,6 +1102,18 @@ mod test {
assert_eq!(params.size(), 72);
}

#[test]
fn rsa_keygen() {
let pubexp = BigNum::from_u32(65537).unwrap();
let mut ctx = PkeyCtx::new_id(Id::RSA).unwrap();
ctx.keygen_init().unwrap();
ctx.set_rsa_keygen_pubexp(&pubexp).unwrap();
ctx.set_rsa_keygen_bits(2048).unwrap();
let key = ctx.keygen().unwrap();

assert_eq!(key.bits(), 2048);
}

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