Struct RsaPrivateKey

Source
pub struct RsaPrivateKey<T>{ /* private fields */ }
Expand description

Represents a whole RSA key, public and private parts.

Implementations§

Source§

impl<T: UnsignedModularInt> RsaPrivateKey<T>

Source

pub fn new<R: CryptoRngCore>( rng: &mut R, bit_size: usize, ) -> Result<RsaPrivateKey<T>>

Generate a new Rsa key pair of the given bit size using the passed in rng.

Source

pub fn new_with_exp<R: CryptoRngCore>( rng: &mut R, bit_size: usize, exp: T, ) -> Result<RsaPrivateKey<T>>

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.

Source

pub fn from_components(n: T, e: T, d: T, primes: [T; 4]) -> Result<Self>

Constructs an RSA key pair from individual components:

  • n: RSA modulus
  • e: public exponent (i.e. encrypting exponent)
  • d: private exponent (i.e. decrypting exponent)
  • primes: prime factors of n: typically two primes p and q. More than two primes can be provided for multiprime RSA, however this is generally not recommended. If no primes are 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 factors p and q (as opposed to multiprime), and e is between 2^16 and 2^256.
Source

pub fn from_p_q(p: T, q: T, public_exponent: T) -> Result<Self>

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.

Source

pub fn from_primes(primes: [T; 4], public_exponent: T) -> Result<Self>

Constructs an RSA key pair from its primes.

This will rebuild the private exponent and the modulus.

Source

pub fn to_public_key(&self) -> RsaPublicKey<T>

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.

Source

pub fn precompute(&mut self) -> Result<()>

Performs some calculations to speed up private key operations.

Source

pub fn clear_precomputed(&mut self)

Clears precomputed values by setting to None

Source

pub fn crt_coefficient(&self) -> Option<T>

Compute CRT coefficient: (1/q) mod p.

Source

pub fn validate(&self) -> Result<()>

Performs basic sanity checks on the key. Returns Ok(()) if everything is good, otherwise an appropriate error.

Source

pub fn decrypt<P: PaddingScheme<T>>( &self, padding: P, ciphertext: &[u8], storage: &mut [u8], ) -> Result<&[u8]>

Decrypt the given message.

Source

pub fn decrypt_blinded<R: CryptoRngCore, P: PaddingScheme<T>>( &self, rng: &mut R, padding: P, ciphertext: &[u8], storage: &mut [u8], ) -> Result<&[u8]>

Decrypt the given message.

Uses rng to blind the decryption process.

Source

pub fn sign<S: SignatureScheme<T>>( &self, padding: S, digest_in: &[u8], storage: &mut [u8], ) -> Result<&[u8]>

Sign the given digest.

Source

pub fn sign_with_rng<R: CryptoRngCore, S: SignatureScheme<T>>( &self, rng: &mut R, padding: S, digest_in: &[u8], storage: &mut [u8], ) -> Result<&[u8]>

Sign the given digest using the provided rng, which is used in the following ways depending on the SignatureScheme:

  • Pkcs1v15Sign padding: uses the RNG to mask the private key operation with random blinding, which helps mitigate sidechannel attacks.
  • Pss always requires randomness. Use [Pss::new][crate::Pss::new] for a standard RSASSA-PSS signature, or [Pss::new_blinded][crate::Pss::new_blinded] for RSA-BSSA blind signatures.

Trait Implementations§

Source§

impl<D, T> AsRef<RsaPrivateKey<T>> for BlindedSigningKey<D, T>

Source§

fn as_ref(&self) -> &RsaPrivateKey<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<D, T> AsRef<RsaPrivateKey<T>> for SigningKey<D, T>

Source§

fn as_ref(&self) -> &RsaPrivateKey<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: UnsignedModularInt> AsRef<RsaPublicKey<T>> for RsaPrivateKey<T>

Source§

fn as_ref(&self) -> &RsaPublicKey<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Clone for RsaPrivateKey<T>

Source§

fn clone(&self) -> RsaPrivateKey<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for RsaPrivateKey<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for RsaPrivateKey<T>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: UnsignedModularInt> Drop for RsaPrivateKey<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: UnsignedModularInt> From<&RsaPrivateKey<T>> for RsaPublicKey<T>

Source§

fn from(private_key: &RsaPrivateKey<T>) -> Self

Converts to this type from the input type.
Source§

impl<D, T> From<BlindedSigningKey<D, T>> for RsaPrivateKey<T>

Source§

fn from(key: BlindedSigningKey<D, T>) -> Self

Converts to this type from the input type.
Source§

impl<D, T> From<RsaPrivateKey<T>> for BlindedSigningKey<D, T>

Source§

fn from(key: RsaPrivateKey<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: UnsignedModularInt> From<RsaPrivateKey<T>> for RsaPublicKey<T>

Source§

fn from(private_key: RsaPrivateKey<T>) -> Self

Converts to this type from the input type.
Source§

impl<D, T> From<RsaPrivateKey<T>> for SigningKey<D, T>

Source§

fn from(key: RsaPrivateKey<T>) -> Self

Converts to this type from the input type.
Source§

impl<D, T> From<SigningKey<D, T>> for RsaPrivateKey<T>

Source§

fn from(key: SigningKey<D, T>) -> Self

Converts to this type from the input type.
Source§

impl<T: UnsignedModularInt> Hash for RsaPrivateKey<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: UnsignedModularInt> PartialEq for RsaPrivateKey<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: UnsignedModularInt> PrivateKeyParts<T> for RsaPrivateKey<T>

Source§

fn d(&self) -> &T

Returns the private exponent of the key.
Source§

fn primes(&self) -> &[T]

Returns the prime factors.
Source§

fn dp(&self) -> Option<&T>

Returns the precomputed dp value, D mod (P-1)
Source§

fn dq(&self) -> Option<&T>

Returns the precomputed dq value, D mod (Q-1)
Source§

fn qinv(&self) -> Option<&MontyForm<T>>

Returns the precomputed qinv value, Q^-1 mod P
Source§

fn crt_values(&self) -> Option<&[CrtValue<T>]>

Returns an iterator over the CRT Values
Source§

fn p_params(&self) -> Option<&MontyParams<T>>

Returns the params for p if precomupted.
Source§

fn q_params(&self) -> Option<&MontyParams<T>>

Returns the params for q if precomupted.
Source§

impl<T: UnsignedModularInt> PublicKeyParts<T> for RsaPrivateKey<T>

Source§

fn n(&self) -> &T

Returns the modulus of the key.
Source§

fn e(&self) -> &T

Returns the public exponent of the key.
Source§

fn n_params(&self) -> &MontyParams<T>

Returns the parameters for montgomery operations.
Source§

fn size(&self) -> usize

Returns the modulus size in bytes. Raw signatures and ciphertexts for or by this public key will have the same size.
Source§

fn n_bits_precision(&self) -> u32

Returns precision (in bits) of n.
Source§

impl<T> Serialize for RsaPrivateKey<T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> TryFrom<PrivateKeyInfo<AnyRef<'_>, OctetStringRef<'_>, BitStringRef<'_>>> for RsaPrivateKey<T>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(private_key_info: PrivateKeyInfoRef<'_>) -> Result<Self>

Performs the conversion.
Source§

impl<T: UnsignedModularInt> Eq for RsaPrivateKey<T>

Source§

impl<T: UnsignedModularInt> ZeroizeOnDrop for RsaPrivateKey<T>

Auto Trait Implementations§

§

impl<T> Freeze for RsaPrivateKey<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for RsaPrivateKey<T>
where T: RefUnwindSafe,

§

impl<T> Send for RsaPrivateKey<T>
where T: Send,

§

impl<T> Sync for RsaPrivateKey<T>
where T: Sync,

§

impl<T> Unpin for RsaPrivateKey<T>
where T: Unpin,

§

impl<T> UnwindSafe for RsaPrivateKey<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DecodePrivateKey for T
where T: for<'a> TryFrom<PrivateKeyInfo<AnyRef<'a>, OctetStringRef<'a>, BitStringRef<'a>>, Error = Error>,

Source§

fn from_pkcs8_der(bytes: &[u8]) -> Result<T, Error>

Deserialize PKCS#8 private key from ASN.1 DER-encoded data (binary format).
Source§

fn read_pkcs8_der_file(path: impl AsRef<Path>) -> Result<Self, Error>

Load PKCS#8 private key from an ASN.1 DER-encoded file on the local filesystem (binary format).
Source§

impl<T> DecodeRsaPrivateKey for T
where T: for<'a> TryFrom<PrivateKeyInfo<AnyRef<'a>, OctetStringRef<'a>, BitStringRef<'a>>, Error = Error>,

Source§

fn from_pkcs1_der(private_key: &[u8]) -> Result<T, Error>

Deserialize PKCS#1 private key from ASN.1 DER-encoded data (binary format).
Source§

fn read_pkcs1_der_file(path: impl AsRef<Path>) -> Result<Self, Error>

Load PKCS#1 private key from an ASN.1 DER-encoded file on the local filesystem (binary format).
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,