pub struct PrivateKey {
pubkey_components: PublicKey,
pub(crate) d: BoxedUint,
pub(crate) primes: Vec<Odd<BoxedUint>>,
pub(crate) precomputed: Option<PrecomputedValues>,
}Expand description
Represents a whole RSA key, public and private parts.
Fields§
§pubkey_components: PublicKeyPublic components of the private key.
d: BoxedUintPrivate exponent.
primes: Vec<Odd<BoxedUint>>Prime factors of N, contains >= 2 elements.
precomputed: Option<PrecomputedValues>Precomputed values to speed up private operations.
Implementations§
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn oaep_decrypt<D>(&self, ciphertext: &[u8]) -> Result<Vec<u8>>where
D: Digest + FixedOutputReset,
pub fn oaep_decrypt<D>(&self, ciphertext: &[u8]) -> Result<Vec<u8>>where
D: Digest + FixedOutputReset,
Decrypt ciphertext using OAEP.
Uses D for both OAEP and MGF1, with an empty label. For a different MGF1
digest or a nonempty label, use DecryptingKey.
Uses the operating system RNG for blinding when getrandom is enabled.
Otherwise, this operation is unblinded; use Self::oaep_decrypt_with_rng
to provide blinding randomness.
§Errors
Returns an error if the key or ciphertext is invalid or the private operation fails, including RNG failures.
Sourcepub fn oaep_decrypt_with_rng<D, R>(
&self,
rng: &mut R,
ciphertext: &[u8],
) -> Result<Vec<u8>>
pub fn oaep_decrypt_with_rng<D, R>( &self, rng: &mut R, ciphertext: &[u8], ) -> Result<Vec<u8>>
Decrypt ciphertext using OAEP, using the provided RNG for blinding.
Uses D for both OAEP and MGF1, with an empty label. For a different MGF1
digest or a nonempty label, use DecryptingKey.
§Errors
Returns an error if the key or ciphertext is invalid or the private operation fails, including RNG failures.
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn pss_sign<D>(&self, msg: &[u8]) -> Result<Vec<u8>>where
D: Digest + FixedOutputReset,
pub fn pss_sign<D>(&self, msg: &[u8]) -> Result<Vec<u8>>where
D: Digest + FixedOutputReset,
Sign msg using PSS after hashing it with D.
Uses the operating system RNG for the salt and blinding. The salt length is
D’s output size. For other salt lengths, use SigningKey.
§Errors
Returns an error if the key is too small for D or the private operation fails,
including RNG failures.
Sourcepub fn pss_sign_prehash<D>(&self, hash: &[u8]) -> Result<Vec<u8>>where
D: Digest + FixedOutputReset,
pub fn pss_sign_prehash<D>(&self, hash: &[u8]) -> Result<Vec<u8>>where
D: Digest + FixedOutputReset,
Sign a message hash computed using D with PSS, without hashing it again.
Uses the operating system RNG for the salt and blinding. The salt length is
D’s output size. For other salt lengths, use SigningKey.
§Errors
Returns Error::InputNotHashed if hash has the wrong length, or an error
if the key is too small for D or the private operation fails, including RNG failures.
Sourcepub fn pss_sign_with_rng<D, R>(
&self,
rng: &mut R,
msg: &[u8],
) -> Result<Vec<u8>>
pub fn pss_sign_with_rng<D, R>( &self, rng: &mut R, msg: &[u8], ) -> Result<Vec<u8>>
Sign msg using PSS after hashing it with D.
Uses the provided RNG for the salt and blinding. The salt length is D’s
output size. For other salt lengths, use SigningKey.
§Errors
Returns an error if the key is too small for D or the private operation fails,
including RNG failures.
Sourcepub fn pss_sign_prehash_with_rng<D, R>(
&self,
rng: &mut R,
hash: &[u8],
) -> Result<Vec<u8>>
pub fn pss_sign_prehash_with_rng<D, R>( &self, rng: &mut R, hash: &[u8], ) -> Result<Vec<u8>>
Sign a message hash computed using D with PSS, without hashing it again.
Uses the provided RNG for the salt and blinding. The salt length is D’s
output size. For other salt lengths, use SigningKey.
§Errors
Returns Error::InputNotHashed if hash has the wrong length, or an error
if the key is too small for D or the private operation fails, including RNG failures.
Source§impl PrivateKey
impl PrivateKey
Sourcepub fn pkcs1v15_decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
pub fn pkcs1v15_decrypt(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
Decrypt ciphertext using PKCS#1 v1.5 with implicit rejection.
Invalid padding returns a deterministic synthetic message. Ok does not establish
authenticity; the enclosing protocol must authenticate the returned message.
See Pkcs1v15Encrypt and the crate::pkcs1v15 module’s security notes.
Uses the operating system RNG for blinding when getrandom is enabled.
Otherwise, this operation is unblinded; use Self::pkcs1v15_decrypt_with_rng
to provide blinding randomness.
§Errors
Returns an error for invalid keys, ciphertext lengths or representatives, unsupported modulus lengths, or operational failures, including RNG failures.
Sourcepub fn pkcs1v15_decrypt_with_rng<R>(
&self,
rng: &mut R,
ciphertext: &[u8],
) -> Result<Vec<u8>>where
R: TryCryptoRng + ?Sized,
pub fn pkcs1v15_decrypt_with_rng<R>(
&self,
rng: &mut R,
ciphertext: &[u8],
) -> Result<Vec<u8>>where
R: TryCryptoRng + ?Sized,
Decrypt ciphertext using PKCS#1 v1.5, using the provided RNG for blinding.
Uses implicit rejection as described in Self::pkcs1v15_decrypt. The enclosing
protocol must authenticate the returned message, even when this returns Ok.
§Errors
Returns the same errors as Self::pkcs1v15_decrypt.
Sourcepub fn pkcs1v15_sign<D>(&self, msg: &[u8]) -> Result<Vec<u8>>where
D: Digest + AssociatedOid,
pub fn pkcs1v15_sign<D>(&self, msg: &[u8]) -> Result<Vec<u8>>where
D: Digest + AssociatedOid,
Sign msg using PKCS#1 v1.5 after hashing it with D.
Uses the operating system RNG for blinding when getrandom is enabled.
Otherwise, this operation is unblinded; use Self::pkcs1v15_sign_with_rng
to provide blinding randomness. Blinding does not change the signature.
§Errors
Returns an error if the key is too small for D or the private operation fails.
Sourcepub fn pkcs1v15_sign_prehash<D>(&self, hash: &[u8]) -> Result<Vec<u8>>where
D: Digest + AssociatedOid,
pub fn pkcs1v15_sign_prehash<D>(&self, hash: &[u8]) -> Result<Vec<u8>>where
D: Digest + AssociatedOid,
Sign a message hash computed using D with PKCS#1 v1.5.
The hash is signed directly, without hashing it again. Blinding follows
Self::pkcs1v15_sign.
§Errors
Returns Error::InputNotHashed if hash has the wrong length, or an error
if the key is too small for D or the private operation fails.
Sourcepub fn pkcs1v15_sign_with_rng<D, R>(
&self,
rng: &mut R,
msg: &[u8],
) -> Result<Vec<u8>>
pub fn pkcs1v15_sign_with_rng<D, R>( &self, rng: &mut R, msg: &[u8], ) -> Result<Vec<u8>>
Sign msg using PKCS#1 v1.5 after hashing it with D, using the provided RNG
for blinding. Blinding does not change the signature.
§Errors
Returns an error if the key is too small for D or the private operation fails,
including RNG failures.
Sourcepub fn pkcs1v15_sign_prehash_with_rng<D, R>(
&self,
rng: &mut R,
hash: &[u8],
) -> Result<Vec<u8>>
pub fn pkcs1v15_sign_prehash_with_rng<D, R>( &self, rng: &mut R, hash: &[u8], ) -> Result<Vec<u8>>
Sign a message hash computed using D with PKCS#1 v1.5, using the provided RNG
for blinding. The hash is signed directly, without hashing it again.
§Errors
Returns Error::InputNotHashed if hash has the wrong length, or an error
if the key is too small for D or the private operation fails, including RNG failures.
Source§impl PrivateKey
impl PrivateKey
Sourcepub const DEFAULT_BITS: usize = 3072
pub const DEFAULT_BITS: usize = 3072
Default modulus size in bits used by Generate.
A 3072-bit RSA modulus provides an estimated 128-bit security strength according to NIST SP 800-57 Part 1 Rev. 5, Table 2.
Sourcepub fn new<R: TryCryptoRng + ?Sized>(
rng: &mut R,
bit_size: usize,
) -> Result<Self>
pub fn new<R: TryCryptoRng + ?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. - If the RNG fails, returns
Error::Rng.
Sourcepub fn new_with_exp<R: TryCryptoRng + ?Sized>(
rng: &mut R,
bit_size: usize,
exp: BoxedUint,
) -> Result<PrivateKey>
pub fn new_with_exp<R: TryCryptoRng + ?Sized>( rng: &mut R, bit_size: usize, exp: BoxedUint, ) -> Result<PrivateKey>
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 PrivateKey::new instead.
Sourcefn from_components_inner(
n: BoxedUint,
e: BoxedUint,
d: BoxedUint,
primes: Vec<BoxedUint>,
wide_primes: bool,
) -> Result<PrivateKey>
fn from_components_inner( n: BoxedUint, e: BoxedUint, d: BoxedUint, primes: Vec<BoxedUint>, wide_primes: bool, ) -> Result<PrivateKey>
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
- Calling
precompute()after validation, propagating any error
Sourcepub fn from_components(
n: BoxedUint,
e: BoxedUint,
d: BoxedUint,
primes: Vec<BoxedUint>,
) -> Result<PrivateKey>
pub fn from_components( n: BoxedUint, e: BoxedUint, d: BoxedUint, primes: Vec<BoxedUint>, ) -> Result<PrivateKey>
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: distinct 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.
For two-prime keys, each prime must fit in ceil(bit_length(n) / 2) bits. Both primes
and their CRT exponents use this public-derived storage width, rounded up to whole limbs.
Use Self::from_components_with_wide_primes to explicitly import unbalanced factors.
Multiprime keys use modulus-width factors and the non-CRT private operation.
Sourcepub fn from_components_with_wide_primes(
n: BoxedUint,
e: BoxedUint,
d: BoxedUint,
primes: Vec<BoxedUint>,
) -> Result<PrivateKey>
pub fn from_components_with_wide_primes( n: BoxedUint, e: BoxedUint, d: BoxedUint, primes: Vec<BoxedUint>, ) -> Result<PrivateKey>
Constructs a key using public-modulus-width storage for every prime and CRT exponent.
This explicitly supports unbalanced prime factors which do not fit the half-modulus bit
bound of Self::from_components. It is slower than ordinary two-prime CRT arithmetic.
The width policy must be chosen as public configuration, not by inspecting secret factor
sizes or retrying a failed half-width import with this constructor.
Encoded-key decoding uses the ordinary half-width policy. For unbalanced encoded keys, decode the components and pass them to this constructor explicitly.
Sourcepub fn from_p_q(
p: BoxedUint,
q: BoxedUint,
public_exponent: BoxedUint,
) -> Result<PrivateKey>
pub fn from_p_q( p: BoxedUint, q: BoxedUint, public_exponent: BoxedUint, ) -> Result<PrivateKey>
Constructs an RSA key pair from its two primes p and q.
The half-modulus factor bound of Self::from_components applies.
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<PrivateKey>
pub fn from_primes( primes: Vec<BoxedUint>, public_exponent: BoxedUint, ) -> Result<PrivateKey>
Constructs an RSA key pair from its primes.
The factor-width policy of Self::from_components applies.
This will rebuild the private exponent and the modulus.
Sourcepub fn as_public_key(&self) -> &PublicKey
pub fn as_public_key(&self) -> &PublicKey
Get the public key from the private key.
Specific alternative to AsRef::as_ref.
Sourcepub fn to_public_key(&self) -> PublicKey
pub fn to_public_key(&self) -> PublicKey
Get the public key from the private key, cloning n and e.
Generally this is not needed since PrivateKey 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.
§Errors
Returns Error::InvalidPrime if either of the first two factors is one, or if they
are not coprime and the CRT coefficient cannot be computed.
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.
With the getrandom feature (enabled by default), uses the operating system RNG for
RSA blinding. Without this feature, decryption is unblinded; use Self::decrypt_blinded
to supply an RNG explicitly.
Pkcs1v15Encrypt uses implicit rejection: invalid padding returns
deterministic synthetic plaintext, not an error. The enclosing protocol must authenticate
the returned message.
Sourcepub fn decrypt_blinded<R: TryCryptoRng + ?Sized, P: PaddingScheme>(
&self,
rng: &mut R,
padding: P,
ciphertext: &[u8],
) -> Result<Vec<u8>>
pub fn decrypt_blinded<R: TryCryptoRng + ?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.
Pkcs1v15Encrypt has the same implicit-rejection
behavior as Self::decrypt, independently of the blinding randomness.
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.
With the getrandom feature (enabled by default), supplies the operating system RNG for
RSA blinding and any randomness required by the signature scheme. Without this feature,
use Self::sign_with_rng to supply an RNG explicitly.
Sourcepub fn sign_with_rng<R: TryCryptoRng + ?Sized, S: SignatureScheme>(
&self,
rng: &mut R,
padding: S,
digest_in: &[u8],
) -> Result<Vec<u8>>
pub fn sign_with_rng<R: TryCryptoRng + ?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 with RSA base blinding enabled by default.
Trait Implementations§
Source§impl<D> AsRef<PrivateKey> for SigningKey<D>where
D: Digest,
impl<D> AsRef<PrivateKey> for SigningKey<D>where
D: Digest,
Source§fn as_ref(&self) -> &PrivateKey
fn as_ref(&self) -> &PrivateKey
Source§impl<D> AsRef<PrivateKey> for SigningKey<D>where
D: Digest,
impl<D> AsRef<PrivateKey> for SigningKey<D>where
D: Digest,
Source§fn as_ref(&self) -> &PrivateKey
fn as_ref(&self) -> &PrivateKey
Source§impl AsRef<PublicKey> for PrivateKey
impl AsRef<PublicKey> for PrivateKey
Source§impl Clone for PrivateKey
impl Clone for PrivateKey
Source§fn clone(&self) -> PrivateKey
fn clone(&self) -> PrivateKey
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PrivateKey
impl Debug for PrivateKey
Source§impl DecodeRsaPrivateKey for PrivateKey
impl DecodeRsaPrivateKey for PrivateKey
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 PrivateKey
impl Drop for PrivateKey
Source§impl EncodePrivateKey for PrivateKey
impl EncodePrivateKey for PrivateKey
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 PrivateKey
impl EncodeRsaPrivateKey for PrivateKey
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>
impl Eq for PrivateKey
Source§impl From<&PrivateKey> for PublicKey
impl From<&PrivateKey> for PublicKey
Source§fn from(private_key: &PrivateKey) -> Self
fn from(private_key: &PrivateKey) -> Self
Source§impl<D> From<PrivateKey> for SigningKey<D>where
D: Digest,
impl<D> From<PrivateKey> for SigningKey<D>where
D: Digest,
Source§fn from(key: PrivateKey) -> Self
fn from(key: PrivateKey) -> Self
Source§impl<D> From<PrivateKey> for SigningKey<D>where
D: Digest + AssociatedOid,
impl<D> From<PrivateKey> for SigningKey<D>where
D: Digest + AssociatedOid,
Source§fn from(key: PrivateKey) -> Self
fn from(key: PrivateKey) -> Self
Source§impl From<PrivateKey> for PublicKey
impl From<PrivateKey> for PublicKey
Source§fn from(private_key: PrivateKey) -> Self
fn from(private_key: PrivateKey) -> Self
Source§impl<D> From<SigningKey<D>> for PrivateKeywhere
D: Digest,
impl<D> From<SigningKey<D>> for PrivateKeywhere
D: Digest,
Source§fn from(key: SigningKey<D>) -> Self
fn from(key: SigningKey<D>) -> Self
Source§impl<D> From<SigningKey<D>> for PrivateKeywhere
D: Digest,
impl<D> From<SigningKey<D>> for PrivateKeywhere
D: Digest,
Source§fn from(key: SigningKey<D>) -> Self
fn from(key: SigningKey<D>) -> Self
Source§impl Generate for PrivateKey
Generate a key with a Self::DEFAULT_BITS-bit modulus and public exponent 65537.
impl Generate for PrivateKey
Generate a key with a Self::DEFAULT_BITS-bit modulus and public exponent 65537.
Source§fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(
rng: &mut R,
) -> Result<Self, R::Error>
fn try_generate_from_rng<R: TryCryptoRng + ?Sized>( rng: &mut R, ) -> Result<Self, R::Error>
TryCryptoRng. Read moreSource§fn generate_from_rng<R>(rng: &mut R) -> Self
fn generate_from_rng<R>(rng: &mut R) -> Self
CryptoRng.