diff --git a/src/key.rs b/src/key.rs index 7b54f320..cbbaf3ab 100644 --- a/src/key.rs +++ b/src/key.rs @@ -4,7 +4,9 @@ use core::fmt; use core::hash::{Hash, Hasher}; use crypto_bigint::modular::{BoxedMontyForm, BoxedMontyParams}; -use crypto_bigint::{BoxedUint, Integer, NonZero, Odd, Resize}; +use crypto_bigint::{ + BoxedUint, Integer, Monty, NonZero, Odd, Resize, Unsigned, U2048, U3072, U4096, +}; use rand_core::CryptoRng; use zeroize::{Zeroize, ZeroizeOnDrop}; #[cfg(feature = "serde")] @@ -27,18 +29,28 @@ use crate::traits::{PaddingScheme, SignatureScheme}; /// Represents the public part of an RSA key. #[derive(Debug, Clone)] -pub struct RsaPublicKey { +pub struct GenericRsaPublicKey { /// Modulus: product of prime numbers `p` and `q` - n: NonZero, + n: NonZero, /// Public exponent: power to which a plaintext message is raised in /// order to encrypt it. /// /// Typically `0x10001` (`65537`) - e: BoxedUint, + e: U, - n_params: BoxedMontyParams, + n_params: ::Params, } +/// RSA private key using dynamically sized heap-allocated integers for backing storage. +pub type RsaPublicKey = GenericRsaPublicKey; + +/// RSA-2048 public key (stack-allocated). +pub type Rsa2048PublicKey = GenericRsaPublicKey; +/// RSA-3072 public key (stack-allocated). +pub type Rsa3072PublicKey = GenericRsaPublicKey; +/// RSA-4096 public key (stack-allocated). +pub type Rsa4096PublicKey = GenericRsaPublicKey; + impl Eq for RsaPublicKey {} impl PartialEq for RsaPublicKey { @@ -60,17 +72,27 @@ impl Hash for RsaPublicKey { /// Represents a whole RSA key, public and private parts. #[derive(Clone)] -pub struct RsaPrivateKey { +pub struct GenericRsaPrivateKey { /// Public components of the private key. - pubkey_components: RsaPublicKey, + pubkey_components: GenericRsaPublicKey, /// Private exponent - pub(crate) d: BoxedUint, + pub(crate) d: U, /// Prime factors of N, contains >= 2 elements. - pub(crate) primes: Vec, + pub(crate) primes: Vec, /// Precomputed values to speed up private operations - pub(crate) precomputed: Option, + pub(crate) precomputed: Option>, } +/// RSA private key using dynamically sized heap-allocated integers for backing storage. +pub type RsaPrivateKey = GenericRsaPrivateKey; + +/// RSA-2048 private key (stack-allocated). +pub type Rsa2048PrivateKey = GenericRsaPrivateKey; +/// RSA-3072 private key (stack-allocated). +pub type Rsa3072PrivateKey = GenericRsaPrivateKey; +/// RSA-4096 private key (stack-allocated). +pub type Rsa4096PrivateKey = GenericRsaPrivateKey; + impl fmt::Debug for RsaPrivateKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let precomputed = if self.precomputed.is_some() { @@ -111,35 +133,36 @@ impl Hash for RsaPrivateKey { } } -impl Drop for RsaPrivateKey { +impl Drop for GenericRsaPrivateKey { fn drop(&mut self) { self.d.zeroize(); self.primes.zeroize(); - self.precomputed.zeroize(); } } impl ZeroizeOnDrop for RsaPrivateKey {} #[derive(Clone)] -pub(crate) struct PrecomputedValues { +pub(crate) struct GenericPrecomputedValues { /// D mod (P-1) - pub(crate) dp: BoxedUint, + pub(crate) dp: U, /// D mod (Q-1) - pub(crate) dq: BoxedUint, + pub(crate) dq: U, /// Q^-1 mod P - pub(crate) qinv: BoxedMontyForm, + pub(crate) qinv: U::Monty, /// Montgomery params for `p` - pub(crate) p_params: BoxedMontyParams, + pub(crate) p_params: ::Params, /// Montgomery params for `q` - pub(crate) q_params: BoxedMontyParams, + pub(crate) q_params: ::Params, } +pub(crate) type PrecomputedValues = GenericPrecomputedValues; + impl ZeroizeOnDrop for PrecomputedValues {} -impl Zeroize for PrecomputedValues { - fn zeroize(&mut self) { +impl Drop for GenericPrecomputedValues { + fn drop(&mut self) { self.dp.zeroize(); self.dq.zeroize(); // TODO: once these have landed in crypto-bigint @@ -148,12 +171,6 @@ impl Zeroize for PrecomputedValues { } } -impl Drop for PrecomputedValues { - fn drop(&mut self) { - self.zeroize(); - } -} - impl From for RsaPublicKey { fn from(private_key: RsaPrivateKey) -> Self { (&private_key).into() diff --git a/src/lib.rs b/src/lib.rs index ef3ea7de..6246cd21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -247,7 +247,10 @@ pub use sha2; pub use crate::{ errors::{Error, Result}, - key::{RsaPrivateKey, RsaPublicKey}, + key::{ + Rsa2048PrivateKey, Rsa2048PublicKey, Rsa3072PrivateKey, Rsa3072PublicKey, + Rsa4096PrivateKey, Rsa4096PublicKey, RsaPrivateKey, RsaPublicKey, + }, oaep::Oaep, pkcs1v15::{Pkcs1v15Encrypt, Pkcs1v15Sign}, pss::Pss,