Struct rsa::RsaPrivateKey

source ·
pub struct RsaPrivateKey { /* private fields */ }
Expand description

Represents a whole RSA key, public and private parts.

Implementations§

source§

impl RsaPrivateKey

source

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

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

source

pub fn new_with_exp<R: CryptoRngCore + ?Sized>( rng: &mut R, bit_size: usize, exp: &BigUint ) -> 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.

source

pub fn from_components( n: BigUint, e: BigUint, d: BigUint, primes: Vec<BigUint> ) -> Result<RsaPrivateKey>

Constructs an RSA key pair from the individual components.

source

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.

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 dp(&self) -> Option<&BigUint>

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

source

pub fn dq(&self) -> Option<&BigUint>

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

source

pub fn qinv(&self) -> Option<&BigInt>

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

source

pub fn d(&self) -> &BigUint

Returns the private exponent of the key.

source

pub fn primes(&self) -> &[BigUint]

Returns the prime factors.

source

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

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>( &self, padding: P, ciphertext: &[u8] ) -> Result<Vec<u8>>

Decrypt the given message.

source

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

Decrypt the given message.

Uses rng to blind the decryption process.

source

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

Sign the given digest.

source

pub fn sign_with_rng<R: CryptoRngCore, 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:

  • 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 for a standard RSASSA-PSS signature, or Pss::new_blinded for RSA-BSSA blind signatures.

Methods from Deref<Target = RsaPublicKey>§

source

pub const MIN_PUB_EXPONENT: u64 = 2u64

source

pub const MAX_PUB_EXPONENT: u64 = 8_589_934_591u64

source

pub const MAX_SIZE: usize = 4_096usize

Trait Implementations§

source§

impl<D> AsRef<RsaPrivateKey> for BlindedSigningKey<D>where D: Digest,

source§

fn as_ref(&self) -> &RsaPrivateKey

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

impl<D> AsRef<RsaPrivateKey> for SigningKey<D>where D: Digest,

source§

fn as_ref(&self) -> &RsaPrivateKey

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

impl<D> AsRef<RsaPrivateKey> for SigningKey<D>where D: Digest,

source§

fn as_ref(&self) -> &RsaPrivateKey

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

impl Clone for RsaPrivateKey

source§

fn clone(&self) -> RsaPrivateKey

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 Debug for RsaPrivateKey

source§

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

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

impl DecodePrivateKey for RsaPrivateKey

source§

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

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

fn from_pkcs8_pem(s: &str) -> Result<Self, Error>

Deserialize PKCS#8-encoded private key from PEM. Read more
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§

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

Load PKCS#8 private key from a PEM-encoded file on the local filesystem.
source§

impl Deref for RsaPrivateKey

§

type Target = RsaPublicKey

The resulting type after dereferencing.
source§

fn deref(&self) -> &RsaPublicKey

Dereferences the value.
source§

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

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 Drop for RsaPrivateKey

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl EncodePrivateKey for RsaPrivateKey

source§

fn to_pkcs8_der(&self) -> Result<SecretDocument>

Serialize a [SecretDocument] containing a PKCS#8-encoded private key.
source§

fn to_pkcs8_pem( &self, line_ending: LineEnding ) -> Result<Zeroizing<String>, Error>

Serialize this private key as PEM-encoded PKCS#8 with the given [LineEnding].
source§

fn write_pkcs8_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>

Write ASN.1 DER-encoded PKCS#8 private key to the given path
source§

fn write_pkcs8_pem_file( &self, path: impl AsRef<Path>, line_ending: LineEnding ) -> Result<(), Error>

Write ASN.1 DER-encoded PKCS#8 private key to the given path
source§

impl From<&RsaPrivateKey> for RsaPublicKey

source§

fn from(private_key: &RsaPrivateKey) -> Self

Converts to this type from the input type.
source§

impl<D> From<BlindedSigningKey<D>> for RsaPrivateKeywhere D: Digest,

source§

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

Converts to this type from the input type.
source§

impl<D> From<RsaPrivateKey> for BlindedSigningKey<D>where D: Digest,

source§

fn from(key: RsaPrivateKey) -> Self

Converts to this type from the input type.
source§

impl From<RsaPrivateKey> for RsaPublicKey

source§

fn from(private_key: RsaPrivateKey) -> Self

Converts to this type from the input type.
source§

impl<D> From<RsaPrivateKey> for SigningKey<D>where D: Digest,

source§

fn from(key: RsaPrivateKey) -> Self

Converts to this type from the input type.
source§

impl<D> From<RsaPrivateKey> for SigningKey<D>where D: Digest,

source§

fn from(key: RsaPrivateKey) -> Self

Converts to this type from the input type.
source§

impl<D> From<SigningKey<D>> for RsaPrivateKeywhere D: Digest,

source§

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

Converts to this type from the input type.
source§

impl<D> From<SigningKey<D>> for RsaPrivateKeywhere D: Digest,

source§

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

Converts to this type from the input type.
source§

impl PartialEq<RsaPrivateKey> for RsaPrivateKey

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PublicKeyParts for RsaPrivateKey

source§

fn n(&self) -> &BigUint

Returns the modulus of the key.
source§

fn e(&self) -> &BigUint

Returns the public exponent of the key.
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§

impl Serialize for RsaPrivateKey

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 TryFrom<PrivateKeyInfo<'_>> for RsaPrivateKey

§

type Error = Error

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

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

Performs the conversion.
source§

impl Zeroize for RsaPrivateKey

source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
source§

impl Eq for RsaPrivateKey

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> DecodeRsaPrivateKey for Twhere T: DecodePrivateKey,

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 from_pkcs1_pem(s: &str) -> Result<Self, Error>

Deserialize PKCS#1-encoded private key from PEM. Read more
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§

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

Load PKCS#1 private key from a PEM-encoded file on the local filesystem.
source§

impl<T> EncodeRsaPrivateKey for Twhere T: EncodePrivateKey,

source§

fn to_pkcs1_der(&self) -> Result<SecretDocument, Error>

Serialize a [SecretDocument] containing a PKCS#1-encoded private key.
source§

fn to_pkcs1_pem( &self, line_ending: LineEnding ) -> Result<Zeroizing<String>, Error>

Serialize this private key as PEM-encoded PKCS#1 with the given [LineEnding].
source§

fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<(), Error>

Write ASN.1 DER-encoded PKCS#1 private key to the given path.
source§

fn write_pkcs1_pem_file( &self, path: impl AsRef<Path>, line_ending: LineEnding ) -> Result<(), Error>

Write ASN.1 DER-encoded PKCS#1 private key to the given path.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

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