Unverified Commit 8d7699d3 authored by Huw Jones's avatar Huw Jones
Browse files

pkey_ctx: add set rsa keygen set bits & pubexp

parent c26187aa
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -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,
@@ -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;

+44 −0
Original line number Diff line number Diff line
@@ -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;
@@ -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;
@@ -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.