pub struct RsaPrivateKey {
pubkey_components: RsaPublicKey,
pub(crate) d: BoxedUint,
pub(crate) primes: Vec<BoxedUint>,
pub(crate) precomputed: Option<PrecomputedValues>,
}Expand description
Represents a whole RSA key, public and private parts.
Fields§
§pubkey_components: RsaPublicKeyPublic components of the private key.
d: BoxedUintPrivate exponent
primes: Vec<BoxedUint>Prime factors of N, contains >= 2 elements.
precomputed: Option<PrecomputedValues>Precomputed values to speed up private operations
Implementations§
Source§impl RsaPrivateKey
impl RsaPrivateKey
Sourceconst MIN_SIZE: u32 = 1024
const MIN_SIZE: u32 = 1024
Minimum size of the modulus n in bits. Currently only applies to keygen.
Sourcepub fn new<R: CryptoRng + ?Sized>(rng: &mut R, bit_size: usize) -> Result<Self>
pub fn new<R: CryptoRng + ?Sized>(rng: &mut R, bit_size: usize) -> Result<Self>
Generate a new RSA key pair with a modulus of the given bit size using the passed in rng.
§Errors
- If
bit_sizeis lower than the minimum 1024-bits.
Sourcepub fn new_with_exp<R: CryptoRng + ?Sized>(
rng: &mut R,
bit_size: usize,
exp: BoxedUint,
) -> Result<RsaPrivateKey>
pub fn new_with_exp<R: CryptoRng + ?Sized>( rng: &mut R, bit_size: usize, exp: BoxedUint, ) -> Result<RsaPrivateKey>
Generate a new RSA key pair of the given bit size and the public exponent
using the passed in rng.
Unless you have specific needs, you should use RsaPrivateKey::new instead.
Sourcefn from_components_inner(
n: BoxedUint,
e: BoxedUint,
d: BoxedUint,
primes: Vec<BoxedUint>,
) -> Result<RsaPrivateKey>
fn from_components_inner( n: BoxedUint, e: BoxedUint, d: BoxedUint, primes: Vec<BoxedUint>, ) -> Result<RsaPrivateKey>
Private helper function that constructs an RSA key pair from components WITHOUT performing any validation or precomputation.
This is the shared implementation used by from_components and
from_components_with_large_exponent.
Callers are responsible for:
- Validating the key (to ensure precomputation won’t fail)
- Calling precompute() after validation
Sourcepub fn from_components(
n: BoxedUint,
e: BoxedUint,
d: BoxedUint,
primes: Vec<BoxedUint>,
) -> Result<RsaPrivateKey>
pub fn from_components( n: BoxedUint, e: BoxedUint, d: BoxedUint, primes: Vec<BoxedUint>, ) -> Result<RsaPrivateKey>
Constructs an RSA key pair from individual components:
n: RSA moduluse: public exponent (i.e. encrypting exponent)d: private exponent (i.e. decrypting exponent)primes: prime factors ofn: typically two primespandq. More than two primes can be provided for multiprime RSA, however this is generally not recommended. If noprimesare provided, a prime factor recovery algorithm will be employed to attempt to recover the factors (as described in NIST SP 800-56B Revision 2 Appendix C.2). This algorithm only works if there are just two prime factorspandq(as opposed to multiprime), andeis between 2^16 and 2^256.
Sourcepub fn from_p_q(
p: BoxedUint,
q: BoxedUint,
public_exponent: BoxedUint,
) -> Result<RsaPrivateKey>
pub fn from_p_q( p: BoxedUint, q: BoxedUint, public_exponent: BoxedUint, ) -> Result<RsaPrivateKey>
Constructs an RSA key pair from its two primes p and q.
This will rebuild the private exponent and the modulus.
Private exponent will be rebuilt using the method defined in NIST 800-56B Section 6.2.1.
Sourcepub fn from_primes(
primes: Vec<BoxedUint>,
public_exponent: BoxedUint,
) -> Result<RsaPrivateKey>
pub fn from_primes( primes: Vec<BoxedUint>, public_exponent: BoxedUint, ) -> Result<RsaPrivateKey>
Constructs an RSA key pair from its primes.
This will rebuild the private exponent and the modulus.
Sourcepub fn as_public_key(&self) -> &RsaPublicKey
pub fn as_public_key(&self) -> &RsaPublicKey
Get the public key from the private key.
Specific alternative to AsRef::as_ref.
Sourcepub fn to_public_key(&self) -> RsaPublicKey
pub fn to_public_key(&self) -> RsaPublicKey
Get the public key from the private key, cloning n and e.
Generally this is not needed since RsaPrivateKey implements the PublicKey trait,
but it can occasionally be useful to discard the private information entirely.
Sourcepub fn precompute(&mut self) -> Result<()>
pub fn precompute(&mut self) -> Result<()>
Performs some calculations to speed up private key operations.
Sourcepub fn clear_precomputed(&mut self)
pub fn clear_precomputed(&mut self)
Clears precomputed values by setting to None
Sourcepub fn crt_coefficient(&self) -> Option<BoxedUint>
pub fn crt_coefficient(&self) -> Option<BoxedUint>
Compute CRT coefficient: (1/q) mod p.
Sourcepub fn validate(&self) -> Result<()>
pub fn validate(&self) -> Result<()>
Performs basic sanity checks on the key.
Returns Ok(()) if everything is good, otherwise an appropriate error.
Sourcepub fn decrypt<P: PaddingScheme>(
&self,
padding: P,
ciphertext: &[u8],
) -> Result<Vec<u8>>
pub fn decrypt<P: PaddingScheme>( &self, padding: P, ciphertext: &[u8], ) -> Result<Vec<u8>>
Decrypt the given message.
Sourcepub fn decrypt_blinded<R: CryptoRng + ?Sized, P: PaddingScheme>(
&self,
rng: &mut R,
padding: P,
ciphertext: &[u8],
) -> Result<Vec<u8>>
pub fn decrypt_blinded<R: CryptoRng + ?Sized, P: PaddingScheme>( &self, rng: &mut R, padding: P, ciphertext: &[u8], ) -> Result<Vec<u8>>
Decrypt the given message.
Uses rng to blind the decryption process.
Sourcepub fn sign<S: SignatureScheme>(
&self,
padding: S,
digest_in: &[u8],
) -> Result<Vec<u8>>
pub fn sign<S: SignatureScheme>( &self, padding: S, digest_in: &[u8], ) -> Result<Vec<u8>>
Sign the given digest.
Sourcepub fn sign_with_rng<R: CryptoRng + ?Sized, S: SignatureScheme>(
&self,
rng: &mut R,
padding: S,
digest_in: &[u8],
) -> Result<Vec<u8>>
pub fn sign_with_rng<R: CryptoRng + ?Sized, S: SignatureScheme>( &self, rng: &mut R, padding: S, digest_in: &[u8], ) -> Result<Vec<u8>>
Sign the given digest using the provided rng, which is used in the
following ways depending on the SignatureScheme:
Pkcs1v15Signpadding: uses the RNG to mask the private key operation with random blinding, which helps mitigate sidechannel attacks.Pssalways requires randomness. UsePss::newfor a standard RSASSA-PSS signature, orPss::new_blindedfor RSA-BSSA blind signatures.
Trait Implementations§
Source§impl<D> AsRef<RsaPrivateKey> for BlindedSigningKey<D>where
D: Digest,
impl<D> AsRef<RsaPrivateKey> for BlindedSigningKey<D>where
D: Digest,
Source§fn as_ref(&self) -> &RsaPrivateKey
fn as_ref(&self) -> &RsaPrivateKey
Source§impl<D> AsRef<RsaPrivateKey> for SigningKey<D>where
D: Digest,
impl<D> AsRef<RsaPrivateKey> for SigningKey<D>where
D: Digest,
Source§fn as_ref(&self) -> &RsaPrivateKey
fn as_ref(&self) -> &RsaPrivateKey
Source§impl<D> AsRef<RsaPrivateKey> for SigningKey<D>where
D: Digest,
impl<D> AsRef<RsaPrivateKey> for SigningKey<D>where
D: Digest,
Source§fn as_ref(&self) -> &RsaPrivateKey
fn as_ref(&self) -> &RsaPrivateKey
Source§impl AsRef<RsaPublicKey> for RsaPrivateKey
impl AsRef<RsaPublicKey> for RsaPrivateKey
Source§fn as_ref(&self) -> &RsaPublicKey
fn as_ref(&self) -> &RsaPublicKey
Source§impl Clone for RsaPrivateKey
impl Clone for RsaPrivateKey
Source§fn clone(&self) -> RsaPrivateKey
fn clone(&self) -> RsaPrivateKey
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for RsaPrivateKey
impl Debug for RsaPrivateKey
Source§impl DecodeRsaPrivateKey for RsaPrivateKey
impl DecodeRsaPrivateKey for RsaPrivateKey
Source§fn from_pkcs1_der(bytes: &[u8]) -> Result<Self>
fn from_pkcs1_der(bytes: &[u8]) -> Result<Self>
Source§fn from_pkcs1_pem(s: &str) -> Result<Self, Error>
fn from_pkcs1_pem(s: &str) -> Result<Self, Error>
Source§impl Drop for RsaPrivateKey
impl Drop for RsaPrivateKey
Source§impl EncodePrivateKey for RsaPrivateKey
impl EncodePrivateKey for RsaPrivateKey
Source§fn to_pkcs8_der(&self) -> Result<SecretDocument>
fn to_pkcs8_der(&self) -> Result<SecretDocument>
SecretDocument containing a PKCS#8-encoded private key. Read moreSource§fn to_pkcs8_pem(
&self,
line_ending: LineEnding,
) -> Result<Zeroizing<String>, Error>
fn to_pkcs8_pem( &self, line_ending: LineEnding, ) -> Result<Zeroizing<String>, Error>
LineEnding. Read moreSource§fn write_pkcs8_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
fn write_pkcs8_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
Source§fn write_pkcs8_pem_file(
&self,
path: impl AsRef<Path>,
line_ending: LineEnding,
) -> Result<(), Error>
fn write_pkcs8_pem_file( &self, path: impl AsRef<Path>, line_ending: LineEnding, ) -> Result<(), Error>
Source§impl EncodeRsaPrivateKey for RsaPrivateKey
impl EncodeRsaPrivateKey for RsaPrivateKey
Source§fn to_pkcs1_der(&self) -> Result<SecretDocument>
fn to_pkcs1_der(&self) -> Result<SecretDocument>
SecretDocument containing a PKCS#1-encoded private key.Source§fn to_pkcs1_pem(
&self,
line_ending: LineEnding,
) -> Result<Zeroizing<String>, Error>
fn to_pkcs1_pem( &self, line_ending: LineEnding, ) -> Result<Zeroizing<String>, Error>
LineEnding.Source§fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>
Source§fn write_pkcs1_pem_file(
&self,
path: impl AsRef<Path>,
line_ending: LineEnding,
) -> Result<(), Error>
fn write_pkcs1_pem_file( &self, path: impl AsRef<Path>, line_ending: LineEnding, ) -> Result<(), Error>
Source§impl From<&RsaPrivateKey> for RsaPublicKey
impl From<&RsaPrivateKey> for RsaPublicKey
Source§fn from(private_key: &RsaPrivateKey) -> Self
fn from(private_key: &RsaPrivateKey) -> Self
Source§impl<D> From<BlindedSigningKey<D>> for RsaPrivateKeywhere
D: Digest,
impl<D> From<BlindedSigningKey<D>> for RsaPrivateKeywhere
D: Digest,
Source§fn from(key: BlindedSigningKey<D>) -> Self
fn from(key: BlindedSigningKey<D>) -> Self
Source§impl<D> From<RsaPrivateKey> for BlindedSigningKey<D>where
D: Digest,
impl<D> From<RsaPrivateKey> for BlindedSigningKey<D>where
D: Digest,
Source§fn from(key: RsaPrivateKey) -> Self
fn from(key: RsaPrivateKey) -> Self
Source§impl From<RsaPrivateKey> for RsaPublicKey
impl From<RsaPrivateKey> for RsaPublicKey
Source§fn from(private_key: RsaPrivateKey) -> Self
fn from(private_key: RsaPrivateKey) -> Self
Source§impl<D> From<RsaPrivateKey> for SigningKey<D>where
D: Digest + AssociatedOid,
impl<D> From<RsaPrivateKey> for SigningKey<D>where
D: Digest + AssociatedOid,
Source§fn from(key: RsaPrivateKey) -> Self
fn from(key: RsaPrivateKey) -> Self
Source§impl<D> From<RsaPrivateKey> for SigningKey<D>where
D: Digest,
impl<D> From<RsaPrivateKey> for SigningKey<D>where
D: Digest,
Source§fn from(key: RsaPrivateKey) -> Self
fn from(key: RsaPrivateKey) -> Self
Source§impl<D> From<SigningKey<D>> for RsaPrivateKeywhere
D: Digest,
impl<D> From<SigningKey<D>> for RsaPrivateKeywhere
D: Digest,
Source§fn from(key: SigningKey<D>) -> Self
fn from(key: SigningKey<D>) -> Self
Source§impl<D> From<SigningKey<D>> for RsaPrivateKeywhere
D: Digest,
impl<D> From<SigningKey<D>> for RsaPrivateKeywhere
D: Digest,
Source§fn from(key: SigningKey<D>) -> Self
fn from(key: SigningKey<D>) -> Self
Source§impl Hash for RsaPrivateKey
impl Hash for RsaPrivateKey
Source§impl PartialEq for RsaPrivateKey
impl PartialEq for RsaPrivateKey
Source§impl PrivateKeyParts for RsaPrivateKey
impl PrivateKeyParts for RsaPrivateKey
Source§fn qinv(&self) -> Option<&BoxedMontyForm>
fn qinv(&self) -> Option<&BoxedMontyForm>
Source§fn crt_values(&self) -> Option<&[CrtValue]>
fn crt_values(&self) -> Option<&[CrtValue]>
Source§fn p_params(&self) -> Option<&BoxedMontyParams>
fn p_params(&self) -> Option<&BoxedMontyParams>
p if precomputed.Source§fn q_params(&self) -> Option<&BoxedMontyParams>
fn q_params(&self) -> Option<&BoxedMontyParams>
q if precomputed.Source§impl PublicKeyParts for RsaPrivateKey
impl PublicKeyParts for RsaPrivateKey
Source§fn n_params(&self) -> &BoxedMontyParams
fn n_params(&self) -> &BoxedMontyParams
Source§fn size(&self) -> usize
fn size(&self) -> usize
Source§fn n_bits_precision(&self) -> u32
fn n_bits_precision(&self) -> u32
n.