Struct jwt_compact::alg::RsaPrivateKey

source ·
pub struct RsaPrivateKey { /* private fields */ }
Available on crate feature rsa only.
Expand description

Represents a whole RSA key, public and private parts.

Implementations§

source§

impl RsaPrivateKey

source

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

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

source

pub fn new_with_exp<R>( rng: &mut R, bit_size: usize, exp: &BigUint, ) -> Result<RsaPrivateKey, Error>
where R: CryptoRngCore + ?Sized,

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, Error>

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: BigUint, q: BigUint, public_exponent: BigUint, ) -> Result<RsaPrivateKey, Error>

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: Vec<BigUint>, public_exponent: BigUint, ) -> Result<RsaPrivateKey, Error>

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

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<(), Error>

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<BigUint>

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

source

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

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

source

pub fn decrypt<P>( &self, padding: P, ciphertext: &[u8], ) -> Result<Vec<u8>, Error>
where P: PaddingScheme,

Decrypt the given message.

source

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

Decrypt the given message.

Uses rng to blind the decryption process.

source

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

Sign the given digest.

source

pub fn sign_with_rng<R, S>( &self, rng: &mut R, padding: S, digest_in: &[u8], ) -> Result<Vec<u8>, Error>

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.

Trait Implementations§

source§

impl AsRef<RsaPublicKey> for RsaPrivateKey

source§

fn as_ref(&self) -> &RsaPublicKey

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<(), Error>

Formats the value using the given formatter. 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, Error>

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

impl<'a> From<&'a RsaPrivateKey> for JsonWebKey<'a>

Warning. Contrary to RFC 7518, this implementation does not set dp, dq, and qi fields in the JWK root object, as well as d and t fields for additional factors (i.e., in the oth array).

source§

fn from(key: &'a RsaPrivateKey) -> JsonWebKey<'a>

Converts to this type from the input type.
source§

impl From<&RsaPrivateKey> for RsaPublicKey

source§

fn from(private_key: &RsaPrivateKey) -> RsaPublicKey

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl From<RsaPrivateKey> for RsaPublicKey

source§

fn from(private_key: RsaPrivateKey) -> RsaPublicKey

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl Hash for RsaPrivateKey

source§

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

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 PartialEq 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 PrivateKeyParts for RsaPrivateKey

source§

fn d(&self) -> &BigUint

Returns the private exponent of the key.
source§

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

Returns the prime factors.
source§

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

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

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

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

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

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

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

Returns an iterator over the CRT Values
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 TryFrom<&JsonWebKey<'_>> for RsaPrivateKey

Warning. Contrary to RFC 7518 (at least, in spirit), this conversion ignores dp, dq, and qi fields from JWK, as well as d and t fields for additional factors.

§

type Error = JwkError

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

fn try_from(jwk: &JsonWebKey<'_>) -> Result<Self, Self::Error>

Performs the conversion.
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<RsaPrivateKey, Error>

Performs the conversion.
source§

impl TryFrom<RsaPrivateKey> for StrongKey<RsaPrivateKey>

§

type Error = WeakKeyError<RsaPrivateKey>

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

fn try_from(key: RsaPrivateKey) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for RsaPrivateKey

source§

impl ZeroizeOnDrop for RsaPrivateKey

Auto Trait Implementations§

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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

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

§

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

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

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

§

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

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

impl<T> EncodeRsaPrivateKey for T
where T: EncodePrivateKey,

§

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

Serialize a [SecretDocument] containing a PKCS#1-encoded private key.
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

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where 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 T
where U: Into<T>,

§

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>,

§

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.
§

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

§

fn vzip(self) -> V