Skip to content
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

RSA OAEP Support #303

Merged
merged 14 commits into from
Apr 10, 2024
17 changes: 11 additions & 6 deletions aws-lc-rs/src/rsa/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(in crate::rsa) mod rfc8017 {
ptr::{DetachableLcPtr, LcPtr},
};
use aws_lc::{
EVP_PKEY_assign_RSA, EVP_PKEY_new, RSA_parse_private_key, RSA_parse_public_key,
EVP_PKEY_assign_RSA, EVP_PKEY_new, RSA_parse_private_key, RSA_public_key_from_bytes,
RSA_public_key_to_bytes, EVP_PKEY,
};
use std::ptr::null_mut;
Expand Down Expand Up @@ -84,9 +84,9 @@ pub(in crate::rsa) mod rfc8017 {
pub(in crate::rsa) fn decode_public_key_der(
public_key: &[u8],
) -> Result<LcPtr<EVP_PKEY>, KeyRejected> {
let mut cbs = unsafe { cbs::build_CBS(public_key) };

let rsa = DetachableLcPtr::new(unsafe { RSA_parse_public_key(&mut cbs) })?;
let rsa = DetachableLcPtr::new(unsafe {
RSA_public_key_from_bytes(public_key.as_ptr(), public_key.len())
})?;

let pkey = LcPtr::new(unsafe { EVP_PKEY_new() })?;

Expand Down Expand Up @@ -130,14 +130,19 @@ pub(in crate::rsa) mod rfc5280 {
encoding::PublicKeyX509Der,
error::{KeyRejected, Unspecified},
ptr::LcPtr,
rsa::key::is_rsa_key,
rsa::key::{is_rsa_key, key_size_bytes},
};
use aws_lc::{EVP_marshal_public_key, EVP_parse_public_key, EVP_PKEY};

pub(in crate::rsa) fn encode_public_key_der(
key: &LcPtr<EVP_PKEY>,
) -> Result<PublicKeyX509Der<'static>, Unspecified> {
let mut der = LcCBB::new(1024);
// Data shows that the SubjectPublicKeyInfo is roughly 356% to 375% increase in size comapred to the RSA key
// size in bytes for keys ranging from 2048-bit to 4096-bit. So size the initial capacity to be roughly
// 400% as a consernative estimate to avoid needing to reallocate for any key in that range.
let key_size_bytes = key_size_bytes(key);

let mut der = LcCBB::new(key_size_bytes + (key_size_bytes * 4));
skmcgrail marked this conversation as resolved.
Show resolved Hide resolved

if 1 != unsafe { EVP_marshal_public_key(der.as_mut_ptr(), **key) } {
return Err(Unspecified);
Expand Down
16 changes: 11 additions & 5 deletions aws-lc-rs/src/rsa/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ impl PrivateDecryptingKey {
}
}

/// Generate a new RSA private key for use with asymmetrical encryption.
/// Generate a new RSA private key pair for use with asymmetrical encryption.
///
/// Supports the following key sizes:
/// * `KeySize::Rsa2048`
/// * `KeySize::Rsa3072`
/// * `KeySize::Rsa4096`
/// * `KeySize::Rsa8192`
///
/// # Errors
/// * `Unspecified` for any error that occurs during the generation of the RSA keypair.
Expand All @@ -130,12 +136,12 @@ impl PrivateDecryptingKey {
Self::new(key)
}

/// Generate a RSA `KeyPair` of the specified key-strength.
/// Generate a new RSA private key pair for use with asymmetrical encryption.
///
/// Supports the following key sizes:
/// * `EncryptionKeySize::Rsa2048`
/// * `EncryptionKeySize::Rsa3072`
/// * `EncryptionKeySize::Rsa4096`
/// * `KeySize::Rsa2048`
/// * `KeySize::Rsa3072`
/// * `KeySize::Rsa4096`
///
/// # Errors
/// * `Unspecified`: Any key generation failure.
Expand Down
1 change: 0 additions & 1 deletion aws-lc-rs/tests/rsa_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn test_signature_rsa_pkcs1_sign() {
let rng = rand::SystemRandom::new();
test::run(
test_file!("data/rsa_pkcs1_sign_tests.txt"),
// test_file!("data/debug.txt"),
|section, test_case| {
assert_eq!(section, "");
let digest_name = test_case.consume_string("Digest");
Expand Down
Loading