Skip to content

Commit 2caa7c9

Browse files
VadmemeVadmemeskmcgrail
authored
Implement TryInto<PublicEncryptingKey> for PublicKeyComponents (#582)
* Implement PublicKeyComponents::build_encrypting_key * Add test for PublicKeyComponents::build_encrypting_key * Implement feature as TryInto --------- Co-authored-by: Vadmeme <[email protected]> Co-authored-by: Sean McGrail <[email protected]>
1 parent 91689c4 commit 2caa7c9

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

aws-lc-rs/src/rsa/encryption.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl Clone for PrivateDecryptingKey {
146146
pub struct PublicEncryptingKey(LcPtr<EVP_PKEY>);
147147

148148
impl PublicEncryptingKey {
149-
fn new(evp_pkey: LcPtr<EVP_PKEY>) -> Result<Self, Unspecified> {
149+
pub(crate) fn new(evp_pkey: LcPtr<EVP_PKEY>) -> Result<Self, Unspecified> {
150150
Self::validate_key(&evp_pkey)?;
151151
Ok(Self(evp_pkey))
152152
}

aws-lc-rs/src/rsa/key.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
hex,
2121
ptr::{DetachableLcPtr, LcPtr},
2222
rand,
23+
rsa::PublicEncryptingKey,
2324
sealed::Sealed,
2425
};
2526
#[cfg(feature = "fips")]
@@ -366,7 +367,7 @@ impl PublicKey {
366367
}
367368
}
368369

369-
/// Low-level API for the verification of RSA signatures.
370+
/// Low-level API for RSA public keys.
370371
///
371372
/// When the public key is in DER-encoded PKCS#1 ASN.1 format, it is
372373
/// recommended to use `aws_lc_rs::signature::verify()` with
@@ -456,6 +457,22 @@ where
456457
}
457458
}
458459

460+
impl<B> TryInto<PublicEncryptingKey> for PublicKeyComponents<B>
461+
where
462+
B: AsRef<[u8]> + Debug,
463+
{
464+
type Error = Unspecified;
465+
466+
/// Try to build a `PublicEncryptingKey` from the public key components.
467+
///
468+
/// # Errors
469+
/// `error::Unspecified` if the key failed to verify.
470+
fn try_into(self) -> Result<PublicEncryptingKey, Self::Error> {
471+
let rsa = self.build_rsa()?;
472+
PublicEncryptingKey::new(rsa)
473+
}
474+
}
475+
459476
pub(super) fn generate_rsa_key(size: c_int, fips: bool) -> Result<LcPtr<EVP_PKEY>, Unspecified> {
460477
// We explicitly don't use `EVP_PKEY_keygen`, as it will force usage of either the FIPS or non-FIPS
461478
// keygen function based on the whether the build of AWS-LC had FIPS enbaled. Rather we delegate to the desired

aws-lc-rs/tests/basic_rsa_test.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0 OR ISC
33

44
use aws_lc_rs::rand::SystemRandom;
5+
use aws_lc_rs::rsa::{Pkcs1PublicEncryptingKey, PublicEncryptingKey};
56
use aws_lc_rs::signature;
67
use aws_lc_rs::signature::RsaKeyPair;
78
use aws_lc_rs::test::from_dirty_hex;
@@ -193,3 +194,27 @@ fn test_signature_rsa_primitive_verification() {
193194
let result = public_key.verify(&signature::RSA_PKCS1_2048_8192_SHA256, &msg, &sig);
194195
assert!(result.is_ok());
195196
}
197+
198+
#[test]
199+
fn test_encryption_rsa_primitive() {
200+
let n = from_dirty_hex(
201+
r"CEA80475324C1DC8347827818DA58BAC069D3419C614A6EA1AC6A3B510DCD72CC516954905E9FEF908D45
202+
E13006ADF27D467A7D83C111D1A5DF15EF293771AEFB920032A5BB989F8E4F5E1B05093D3F130F984C07A772
203+
A3683F4DC6FB28A96815B32123CCDD13954F19D5B8B24A103E771A34C328755C65ED64E1924FFD04D30B2142
204+
CC262F6E0048FEF6DBC652F21479EA1C4B1D66D28F4D46EF7185E390CBFA2E02380582F3188BB94EBBF05D31
205+
487A09AFF01FCBB4CD4BFD1F0A833B38C11813C84360BB53C7D4481031C40BAD8713BB6B835CB08098ED15BA
206+
31EE4BA728A8C8E10F7294E1B4163B7AEE57277BFD881A6F9D43E02C6925AA3A043FB7FB78D",
207+
);
208+
209+
let e = from_dirty_hex(r"260445");
210+
let msg = from_dirty_hex(r"68656c6c6f2c20776f726c64");
211+
212+
let public_key = signature::RsaPublicKeyComponents { n: &n, e: &e };
213+
let public_encrypting_key: PublicEncryptingKey = public_key.try_into().unwrap();
214+
let pkcs_encrypting_key = Pkcs1PublicEncryptingKey::new(public_encrypting_key).unwrap();
215+
216+
let mut encrypted = vec![0u8; pkcs_encrypting_key.ciphertext_size()];
217+
pkcs_encrypting_key.encrypt(&msg, &mut encrypted).unwrap();
218+
219+
assert_ne!(encrypted, msg);
220+
}

0 commit comments

Comments
 (0)