aws_lc_rs/rsa/
encryption.rs1pub(super) mod oaep;
5pub(super) mod pkcs1;
6
7use super::key::{generate_rsa_key, validate_rsa_key};
8use super::{encoding, KeySize};
9use crate::aws_lc::{EVP_PKEY, EVP_PKEY_RSA};
10use crate::encoding::{AsDer, Pkcs8V1Der, PublicKeyX509Der};
11use crate::error::{KeyRejected, Unspecified};
12use crate::pkcs8::Version;
13use crate::ptr::LcPtr;
14use core::fmt::Debug;
15
16#[allow(clippy::module_name_repetitions)]
18#[derive(Debug, Clone, Copy, PartialEq)]
19#[non_exhaustive]
20pub enum EncryptionAlgorithmId {
21 OaepSha1Mgf1sha1,
23
24 OaepSha256Mgf1sha256,
26
27 OaepSha384Mgf1sha384,
29
30 OaepSha512Mgf1sha512,
32}
33
34pub struct PrivateDecryptingKey(LcPtr<EVP_PKEY>);
36
37unsafe impl Send for PrivateDecryptingKey {}
44unsafe impl Sync for PrivateDecryptingKey {}
45
46impl PrivateDecryptingKey {
47 fn new(evp_pkey: LcPtr<EVP_PKEY>) -> Result<Self, KeyRejected> {
48 Self::validate_key(&evp_pkey)?;
49 Ok(Self(evp_pkey))
50 }
51
52 fn validate_key(key: &LcPtr<EVP_PKEY>) -> Result<(), KeyRejected> {
53 validate_rsa_key(key)
54 }
55
56 pub fn generate(size: KeySize) -> Result<Self, Unspecified> {
67 let key = generate_rsa_key(size.bits())?;
68 Ok(Self::new(key)?)
69 }
70
71 #[cfg(feature = "fips")]
85 #[deprecated]
86 pub fn generate_fips(size: KeySize) -> Result<Self, Unspecified> {
87 Self::generate(size)
88 }
89
90 pub fn from_pkcs8(pkcs8: &[u8]) -> Result<Self, KeyRejected> {
98 let key = LcPtr::<EVP_PKEY>::parse_rfc5208_private_key(pkcs8, EVP_PKEY_RSA)?;
99 Self::new(key)
100 }
101
102 #[cfg(feature = "fips")]
104 #[must_use]
105 pub fn is_valid_fips_key(&self) -> bool {
106 super::key::is_valid_fips_key(&self.0)
107 }
108
109 #[must_use]
111 pub fn key_size_bytes(&self) -> usize {
112 self.0.as_const().signature_size_bytes()
113 }
114
115 #[must_use]
117 pub fn key_size_bits(&self) -> usize {
118 self.0.as_const().key_size_bits()
119 }
120
121 #[must_use]
123 #[allow(clippy::missing_panics_doc)]
124 pub fn public_key(&self) -> PublicEncryptingKey {
125 PublicEncryptingKey::new(self.0.clone()).expect(
126 "PublicEncryptingKey key size to be supported by PrivateDecryptingKey key sizes",
127 )
128 }
129}
130
131impl Debug for PrivateDecryptingKey {
132 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
133 f.debug_tuple("PrivateDecryptingKey").finish()
134 }
135}
136
137impl AsDer<Pkcs8V1Der<'static>> for PrivateDecryptingKey {
138 fn as_der(&self) -> Result<Pkcs8V1Der<'static>, Unspecified> {
139 Ok(Pkcs8V1Der::new(
140 self.0.as_const().marshal_rfc5208_private_key(Version::V1)?,
141 ))
142 }
143}
144
145impl Clone for PrivateDecryptingKey {
146 fn clone(&self) -> Self {
147 Self(self.0.clone())
148 }
149}
150
151pub struct PublicEncryptingKey(LcPtr<EVP_PKEY>);
153
154unsafe impl Send for PublicEncryptingKey {}
156unsafe impl Sync for PublicEncryptingKey {}
157
158impl PublicEncryptingKey {
159 pub(crate) fn new(evp_pkey: LcPtr<EVP_PKEY>) -> Result<Self, KeyRejected> {
160 Self::validate_key(&evp_pkey)?;
161 Ok(Self(evp_pkey))
162 }
163
164 fn validate_key(key: &LcPtr<EVP_PKEY>) -> Result<(), KeyRejected> {
165 validate_rsa_key(key)
166 }
167
168 pub fn from_der(value: &[u8]) -> Result<Self, KeyRejected> {
174 let key = encoding::rfc5280::decode_public_key_der(value)?;
175 Self::new(key)
176 }
177
178 #[must_use]
180 pub fn key_size_bytes(&self) -> usize {
181 self.0.as_const().signature_size_bytes()
182 }
183
184 #[must_use]
186 pub fn key_size_bits(&self) -> usize {
187 self.0.as_const().key_size_bits()
188 }
189}
190
191impl Debug for PublicEncryptingKey {
192 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
193 f.debug_tuple("PublicEncryptingKey").finish()
194 }
195}
196
197impl Clone for PublicEncryptingKey {
198 fn clone(&self) -> Self {
199 Self(self.0.clone())
200 }
201}
202
203impl AsDer<PublicKeyX509Der<'static>> for PublicEncryptingKey {
204 fn as_der(&self) -> Result<PublicKeyX509Der<'static>, Unspecified> {
209 encoding::rfc5280::encode_public_key_der(&self.0)
210 }
211}