Commit 61a27426 authored by Arthur Gautier's avatar Arthur Gautier
Browse files

expose rsa OAEP label methods

parent 5c78192b
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -61,6 +61,22 @@ pub unsafe fn EVP_PKEY_CTX_set_rsa_oaep_md(ctx: *mut EVP_PKEY_CTX, md: *mut EVP_
    )
}

#[cfg(any(ossl102, libressl310))]
pub unsafe fn EVP_PKEY_CTX_set0_rsa_oaep_label(
    ctx: *mut EVP_PKEY_CTX,
    label: *mut c_void,
    len: c_int,
) -> c_int {
    EVP_PKEY_CTX_ctrl(
        ctx,
        EVP_PKEY_RSA,
        EVP_PKEY_OP_TYPE_CRYPT,
        EVP_PKEY_CTRL_RSA_OAEP_LABEL,
        len,
        label as *mut c_void,
    )
}

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;

@@ -70,6 +86,8 @@ pub const EVP_PKEY_CTRL_GET_RSA_PADDING: c_int = EVP_PKEY_ALG_CTRL + 6;

#[cfg(any(ossl102, libressl310))]
pub const EVP_PKEY_CTRL_RSA_OAEP_MD: c_int = EVP_PKEY_ALG_CTRL + 9;
#[cfg(any(ossl102, libressl310))]
pub const EVP_PKEY_CTRL_RSA_OAEP_LABEL: c_int = EVP_PKEY_ALG_CTRL + 10;

pub const RSA_PKCS1_PADDING: c_int = 1;
pub const RSA_SSLV23_PADDING: c_int = 2;
+38 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@
//! decrypted.truncate(decrypted_len);
//! assert_eq!(&*decrypted, data);
//! ```
use libc::{c_int, c_void};
use std::{marker::PhantomData, ptr};

use crate::error::ErrorStack;
@@ -157,6 +158,43 @@ impl<'a> Encrypter<'a> {
        }
    }

    /// Sets the RSA OAEP label.
    ///
    /// This is only useful for RSA keys.
    ///
    /// This corresponds to [`EVP_PKEY_CTX_set0_rsa_oaep_label`].
    ///
    /// [`EVP_PKEY_CTX_set0_rsa_oaep_label`]: https://www.openssl.org/docs/manmaster/man3/EVP_PKEY_CTX_set0_rsa_oaep_label.html
    #[cfg(any(ossl102, libressl310))]
    pub fn set_rsa_oaep_label(&mut self, label: &[u8]) -> Result<(), ErrorStack> {
        unsafe {
            let p = cvt_p(ffi::CRYPTO_malloc(
                label.len() as _,
                concat!(file!(), "\0").as_ptr() as *const _,
                line!() as c_int,
            ))?;
            ptr::copy_nonoverlapping(label.as_ptr(), p as *mut u8, label.len());

            cvt(ffi::EVP_PKEY_CTX_set0_rsa_oaep_label(
                self.pctx,
                p as *mut c_void,
                label.len() as c_int,
            ))
            .map(|_| ())
            .map_err(|e| {
                #[cfg(not(ossl110))]
                ::ffi::CRYPTO_free(p as *mut c_void);
                #[cfg(ossl110)]
                ::ffi::CRYPTO_free(
                    p as *mut c_void,
                    concat!(file!(), "\0").as_ptr() as *const _,
                    line!() as c_int,
                );
                e
            })
        }
    }

    /// Performs public key encryption.
    ///
    /// In order to know the size needed for the output buffer, use [`encrypt_len`](Encrypter::encrypt_len).