silk.crypto.rsa.
RSA keys, signatures, and encryption: PKCS #1 v1.5 and PSS signatures, OAEP and PKCS #1 v1.5 encryption, and key generation (RFC 8017).
When to use#
Use RsaPrivateKey to sign, to decrypt, or to generate a key, and RsaPublicKey to
verify and to encrypt, for example for crypto.sign and crypto.publicEncrypt in Node.js or
for a JSON Web Token with RS256 or PS256. Use RsaPublicKey.verifyCertificate for the
signature of an X.509 certificate.
Details#
A key has a modulus of 512 to 8192 bits and an odd public exponent of 3 or more, such as
65537. Make a key from its big-endian parts with RsaPublicKey.fromComponents or
RsaPrivateKey.fromComponents, or generate one with RsaPrivateKey.generate.
Keys of the RSA-PSS type, which OpenSSL writes with the id-RSASSA-PSS algorithm and
optional limits on the hash and the salt, are RsaPssPublicKey and RsaPssPrivateKey.
The signature functions take the digest of the message, not the message, and the hash as a
type argument that implements silk.crypto.digest.Digest, such as Sha256. PSS and OAEP use
MGF1 with the same hash. A signature or a ciphertext has the length in bytes of the modulus.
The private operations use the Chinese remainder theorem and the constant-time arithmetic of
silk.crypto.modular, and check each result with the public key, so a fault does not leak
the key. OAEP decryption checks the padding without a branch on secret data.
Errors#
RsaErrorfor a key, a parameter, a length, a signature, or a ciphertext that is not valid.
Gotchas#
A valid signature proves only that the holder of the private key signed the message. It does
not prove that you can trust the key. To check a certificate chain, use the silk.tls.trust_*
modules. PKCS #1 v1.5 encryption is here only for old formats. Its decryption uses implicit
rejection, so a bad ciphertext gives a wrong message instead of an error. Use OAEP for new
formats.
Examples#
Sign and verify with a generated key#
import silk.core.effect { Effect }
import silk.core.option { Option }
import silk.core.result { Result }
import silk.crypto.rsa { RsaError, RsaPrivateKey }
import silk.crypto.sha2 { Sha256 }
import silk.num.u64
import silk.system.random { Random }
// A test provider. Never use it for keys.
struct Counter {
state: u64,
}
impl Random for Counter {
effect fn fillBytes(self: &mut Self, output: &mut [u8]) -> () {
let mut index: usize = 0
while index < output.length {
self.state = u64.wrappingAdd(u64.wrappingMultiply(self.state, 6364136223846793005), 1)
output[index] = u64.toU8(u64.shiftRight(self.state, 56))
index = index + 1
}
}
}
effect fn signAndVerify() -> i32 ! RsaError ? &mut Random {
let key = run RsaPrivateKey.generate(1024, 65537)
let digest = Sha256.hash(b"hello")
let mut signature: [u8; 128] = [0; 128]
run key.signPss<Sha256>(&digest, 32, &mut signature)
let publicKey = key.publicKey()
return match publicKey.verifyPss<Sha256>(&digest, &signature, Option<usize>.some(32)) {
Result<(), RsaError>.Success { value: _ } => 0
Result<(), RsaError>.Failure { error: _ } => 1
}
}
pub effect fn main() -> i32 ! RsaError {
let mut random = Counter { state: 1 }
return run signAndVerify() |> Effect.provideMut<Random>(&mut random)
}Import as RsaPublicKey with import silk.crypto.rsa { RsaPublicKey }.
Public declarations: 7.
RsaError#
pub enum RsaErrorThe reason that an RSA key, parameter, signature, or ciphertext was rejected.
InvalidKey#
InvalidKeyA key part is not valid: the modulus is even or not 512 to 8192 bits, the exponent is even, below 3, or not below the modulus, or the private parts do not match the public key.
InvalidEncoding#
InvalidEncodingA DER value is not valid, not minimal, or has bytes after it.
InvalidParameters#
InvalidParametersAn algorithm identifier, a key size, or an exponent is not supported.
InvalidLength#
InvalidLengthAn input or output does not have the required length, such as a signature that is not as long as the modulus, or a digest that is not as long as the hash output.
MessageTooLong#
MessageTooLongThe message is too long to encrypt with this key.
KeyTooSmall#
KeyTooSmallThe key is too short for the hash, the salt, or the padding.
InvalidSignature#
InvalidSignatureThe signature does not match the digest and the key.
InvalidCiphertext#
InvalidCiphertextThe ciphertext is not valid for this key and padding.
Implementation Debug for RsaError#
impl Debug for RsaErrorOperation debug#
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
! WriterError
? &mut WriterWrites the name of the variant, such as InvalidKey.
RsaPublicKey#
pub struct RsaPublicKeyAn RSA public key: a modulus of 512 to 8192 bits and an odd public exponent.
When to use#
Use it to verify PKCS #1 v1.5 and PSS signatures and to encrypt with OAEP or PKCS #1 v1.5.
Details#
Make one from its parts with RsaPublicKey.fromComponents, from a certificate with
RsaPublicKey.fromCertificateKey, or from a private key with RsaPrivateKey.publicKey.
Gotchas#
A valid signature proves only that the holder of the private key signed the message. It does not prove that you can trust the key. It is a fixed-size value of about 3 KiB.
RsaPrivateKey#
pub struct RsaPrivateKeyAn RSA private key with its two primes and the values that speed up its operations.
When to use#
Use it to sign with PKCS #1 v1.5 or PSS and to decrypt OAEP or PKCS #1 v1.5. Get its public
half with RsaPrivateKey.publicKey.
Details#
Make one from its parts with RsaPrivateKey.fromComponents, or generate one with
RsaPrivateKey.generate. It holds two primes, so multi-prime keys are not supported.
Gotchas#
It does not erase its memory when dropped. It is a fixed-size value of about 11 KiB.
Implementation RsaPublicKey#
impl RsaPublicKeyAssociated function fromComponents#
pub fn fromComponents(modulus: &[u8], exponent: &[u8]) -> Result<RsaPublicKey, RsaError>Returns the key for the big-endian modulus and exponent bytes. Leading zero bytes are
allowed.
The modulus must be odd and have 512 to 8192 significant bits. The exponent must be odd, 3 or more, and less than the modulus.
Errors#
InvalidKeyif a value is not in this range.
Method bitLength#
pub fn bitLength(self: &RsaPublicKey) -> usizeReturns the number of significant bits of the modulus, such as 2048.
Method byteLength#
pub fn byteLength(self: &RsaPublicKey) -> usizeReturns the length in bytes of the modulus, which is the length of every signature and ciphertext of the key.
Method modulus#
pub fn modulus(self: &RsaPublicKey) -> NaturalReturns the modulus as a number.
Method exponent#
pub fn exponent(self: &RsaPublicKey) -> NaturalReturns the public exponent as a number.
Method verifyPkcs1#
pub fn verifyPkcs1<H: Digest>(self: &RsaPublicKey, digest: &[u8], signature: &[u8]) -> Result<
(),
RsaError,
>Checks that signature is a valid RSASSA-PKCS1-v1_5 signature of the message with the
digest digest, made with the hash H.
Errors#
InvalidLengthifdigestis not as long as the output ofH, orsignatureis not as long as the modulus.KeyTooSmallif the key cannot hold the digest ofH.InvalidSignatureif the signature does not match.
Method verifyPss#
pub fn verifyPss<H: Digest>(
self: &RsaPublicKey,
digest: &[u8],
signature: &[u8],
saltLength: Option<usize>,
) -> Result<(), RsaError>Checks that signature is a valid RSASSA-PSS signature of the message with the digest
digest, made with the hash H and MGF1 with H.
With Some, saltLength is the salt length that the signature must have. With None, the
salt can have any length, as with RSA_PSS_SALTLEN_AUTO in Node.js.
Errors#
InvalidLengthifdigestis not as long as the output ofH, orsignatureis not as long as the modulus.InvalidSignatureif the signature does not match.
Method maximumPssSaltLength#
pub fn maximumPssSaltLength<H: Digest>(self: &RsaPublicKey) -> usizeReturns the longest PSS salt that fits with the hash H: the modulus length less the
digest length less 2, or 0 if the key is too short for H.
Method encryptOaep#
pub effect fn encryptOaep<H: Digest>(
self: &RsaPublicKey,
label: &[u8],
message: &[u8],
output: &mut [u8],
) -> ()
! RsaError
? &mut RandomEncrypts message with RSAES-OAEP, the hash H, MGF1 with H, and label, and writes the
ciphertext to output. It reads the seed from the ambient Random.
The message can have up to the modulus length less twice the digest length less 2 bytes.
Errors#
InvalidLengthifoutputis not as long as the modulus.KeyTooSmallif the key is too short forH.MessageTooLongifmessageis too long.
Method encryptPkcs1#
pub effect fn encryptPkcs1(self: &RsaPublicKey, message: &[u8], output: &mut [u8]) -> ()
! RsaError
? &mut RandomEncrypts message with RSAES-PKCS1-v1_5 and writes the ciphertext to output. It reads the
padding from the ambient Random.
The message can have up to the modulus length less 11 bytes.
Errors#
InvalidLengthifoutputis not as long as the modulus.MessageTooLongifmessageis too long.
Gotchas#
Use it only for formats that require it. Prefer RsaPublicKey.encryptOaep: PKCS #1 v1.5
padding has a long history of padding-oracle attacks.
Method recoverPkcs1Raw#
pub fn recoverPkcs1Raw(self: &RsaPublicKey, signature: &[u8], output: &mut [u8]) -> Result<
usize,
RsaError,
>Recovers the message of a signature made with PKCS #1 v1.5 type 1 padding over raw bytes,
as RSA_public_decrypt with RSA_PKCS1_PADDING and crypto.publicDecrypt in Node.js do,
and returns the length of the message that it writes to the start of output.
The recovered block must be 0x00, 0x01, at least 8 bytes of 0xFF, 0x00, and the message.
It is the inverse of RsaPrivateKey.signPkcs1Raw. The message can have up to the modulus
length less 11 bytes.
Errors#
InvalidLengthifsignatureis not as long as the modulus, oroutputis shorter than the message.InvalidSignatureifsignatureis not less than the modulus or the padding is not valid.
Gotchas#
It does not check a digest. To verify a signature of a message, use
RsaPublicKey.verifyPkcs1.
Method encryptRaw#
pub fn encryptRaw(self: &RsaPublicKey, input: &[u8], output: &mut [u8]) -> Result<(), RsaError>Computes input^e modulo n without padding (RSAEP, RSA_NO_PADDING in Node.js) and writes
it to output. It is also the public operation of crypto.publicDecrypt with
RSA_NO_PADDING.
Errors#
InvalidLengthifinputoroutputis not as long as the modulus.InvalidCiphertextifinputis not less than the modulus.
Gotchas#
Raw RSA is not secure on its own. Use it only to build a padding scheme.
Implementation RsaPrivateKey#
impl RsaPrivateKeyAssociated function fromComponents#
pub fn fromComponents(
modulus: &[u8],
publicExponent: &[u8],
privateExponent: &[u8],
firstPrime: &[u8],
secondPrime: &[u8],
firstExponent: &[u8],
secondExponent: &[u8],
coefficient: &[u8],
) -> Result<RsaPrivateKey, RsaError>Returns the private key for its big-endian parts, as in a PKCS #1 RSAPrivateKey: the
modulus n, the public exponent e, the private exponent d, the primes p and q, d mod (p - 1),
d mod (q - 1), and the inverse of q modulo p.
It checks that n is p * q and that the coefficient is the inverse of q. A wrong exponent is
found by the check of each result, which then fails with InvalidKey.
Errors#
InvalidKeyif the public key is not valid, a prime is even or below 3, n is not p * q, or the coefficient is not the inverse of q modulo p.
Associated function generate#
pub effect fn generate(bits: usize, exponent: u32) -> RsaPrivateKey
! RsaError
? &mut RandomReturns a new key with a modulus of bits bits and the public exponent exponent, from
the ambient Random, as generateKeyPair('rsa') does in Node.js.
The primes have half the bits each, with the top two bits set, and pass trial division and Miller-Rabin tests with random bases (64 rounds, or 128 for primes above 2048 bits). The first prime is the larger. The private exponent is the inverse of e modulo (p - 1)(q - 1).
Errors#
InvalidParametersifbitsis not 512 to 8192, orexponentis even or below 3.
Gotchas#
It takes a random time, about a second for 2048 bits and much longer for 4096 bits. The
Random provider must be cryptographically secure.
Method publicKey#
pub fn publicKey(self: &RsaPrivateKey) -> RsaPublicKeyReturns the public half of the key.
Method privateExponent#
pub fn privateExponent(self: &RsaPrivateKey) -> NaturalReturns the private exponent d.
Method firstPrime#
pub fn firstPrime(self: &RsaPrivateKey) -> NaturalReturns the first prime p.
Method secondPrime#
pub fn secondPrime(self: &RsaPrivateKey) -> NaturalReturns the second prime q.
Method firstExponent#
pub fn firstExponent(self: &RsaPrivateKey) -> NaturalReturns d mod (p - 1).
Method secondExponent#
pub fn secondExponent(self: &RsaPrivateKey) -> NaturalReturns d mod (q - 1).
Method coefficient#
pub fn coefficient(self: &RsaPrivateKey) -> NaturalReturns the inverse of q modulo p.
Method signPkcs1#
pub fn signPkcs1<H: Digest>(self: &RsaPrivateKey, digest: &[u8], signature: &mut [u8]) -> Result<
(),
RsaError,
>Signs the message with the digest digest with RSASSA-PKCS1-v1_5 and the hash H, and
writes the signature to signature.
Errors#
InvalidLengthifdigestis not as long as the output ofH, orsignatureis not as long as the modulus.KeyTooSmallif the key cannot hold the digest ofH.InvalidKeyif the private parts do not match the public key.
Method signPss#
pub effect fn signPss<H: Digest>(
self: &RsaPrivateKey,
digest: &[u8],
saltLength: usize,
signature: &mut [u8],
) -> ()
! RsaError
? &mut RandomSigns the message with the digest digest with RSASSA-PSS, the hash H, MGF1 with H,
and a random salt of saltLength bytes from the ambient Random, and writes the signature
to signature.
A salt as long as the digest is the common choice. RsaPublicKey.maximumPssSaltLength
gives the longest, which Node.js uses by default.
Errors#
InvalidLengthifdigestis not as long as the output ofH, orsignatureis not as long as the modulus.KeyTooSmallif the key cannot hold the digest ofHand the salt.InvalidKeyif the private parts do not match the public key.
Method decryptOaep#
pub fn decryptOaep<H: Digest>(
self: &RsaPrivateKey,
label: &[u8],
ciphertext: &[u8],
output: &mut [u8],
) -> Result<usize, RsaError>Decrypts an RSAES-OAEP ciphertext made with the hash H, MGF1 with H, and label, and
returns the length of the message that it writes to the start of output.
It checks the padding without a branch on secret data and has one error for every bad ciphertext, so the errors do not reveal the plaintext.
Errors#
InvalidLengthifciphertextis not as long as the modulus, oroutputis shorter than the longest message for the key: the modulus length less twice the digest length less 2.KeyTooSmallif the key is too short forH.InvalidCiphertextif the ciphertext is not valid.InvalidKeyif the private parts do not match the public key.
Method signPkcs1Raw#
pub fn signPkcs1Raw(self: &RsaPrivateKey, message: &[u8], signature: &mut [u8]) -> Result<
(),
RsaError,
>Signs message as raw bytes with PKCS #1 v1.5 type 1 padding, as RSA_private_encrypt
with RSA_PKCS1_PADDING and crypto.privateEncrypt in Node.js do, and writes the signature
to signature.
The padded block is 0x00, 0x01, 0xFF bytes, 0x00, and the message, with no DigestInfo. The
message can have up to the modulus length less 11 bytes. Recover it with
RsaPublicKey.recoverPkcs1Raw.
Errors#
InvalidLengthifsignatureis not as long as the modulus.MessageTooLongifmessageis too long.InvalidKeyif the private parts do not match the public key.
Gotchas#
It does not hash or encode a digest. To sign a message for RsaPublicKey.verifyPkcs1,
use RsaPrivateKey.signPkcs1.
Method decryptRaw#
pub fn decryptRaw(self: &RsaPrivateKey, input: &[u8], output: &mut [u8]) -> Result<
(),
RsaError,
>Computes input^d modulo n without padding (RSADP, RSA_NO_PADDING in Node.js) and writes
it to output. It is also the private operation of crypto.privateEncrypt with
RSA_NO_PADDING.
Errors#
InvalidLengthifinputoroutputis not as long as the modulus.InvalidCiphertextifinputis not less than the modulus.InvalidKeyif the private parts do not match the public key.
Gotchas#
Raw RSA is not secure on its own. Use it only to build a padding scheme.
Implementation RsaPublicKey#
impl RsaPublicKeyAssociated function fromCertificateKey#
pub fn fromCertificateKey(algorithmDer: &[u8], keyDer: &[u8], unusedBits: u8) -> Result<
RsaPublicKey,
RsaError,
>Returns the key in the subjectPublicKeyInfo of a certificate.
Pass the algorithm DER, the BIT STRING bytes, and the unused-bit count from
silk.tls.certificate as they are. The algorithm must be rsaEncryption with no parameters
or NULL. The key must have a modulus of 2048 to 4096 bits with no leading zero byte, and the
exponent 65537.
Errors#
InvalidEncodingif the DER is not valid.InvalidParametersif the algorithm is wrong.InvalidLengthifunusedBitsis not 0.InvalidKeyif the key is not in the profile.
Method verifyCertificate#
pub fn verifyCertificate(
self: &RsaPublicKey,
message: &[u8],
algorithmDer: &[u8],
signature: &[u8],
unusedBits: u8,
) -> Result<(), RsaError>Verifies a certificate signature with this key.
message is the signed part of the certificate (the TBSCertificate DER), algorithmDer is
its signature AlgorithmIdentifier, and signature and unusedBits come from its signature
BIT STRING. It accepts sha256WithRSAEncryption (PKCS #1 v1.5) and RSASSA-PSS with
SHA-256, MGF1 with SHA-256, and a 32-byte salt.
Errors#
InvalidLengthifunusedBitsis not 0.InvalidParametersif the algorithm or its parameters are not supported.InvalidLengthifsignaturedoes not have the length of the modulus.InvalidSignatureif the signature does not match.
Gotchas#
It checks only this one signature. It does not check the chain, the dates, or the names.
Implementation RsaPublicKey#
impl RsaPublicKeyAssociated function fromPkcs1Der#
pub fn fromPkcs1Der(der: &[u8]) -> Result<RsaPublicKey, RsaError>Returns the key in a DER PKCS #1 RSAPublicKey: a SEQUENCE of the modulus and the
exponent, as in a PEM RSA PUBLIC KEY.
Errors#
InvalidEncodingif the DER is not valid, not minimal, or has bytes after it.InvalidKeyif the key is not valid, as withRsaPublicKey.fromComponents.
Associated function fromSpkiDer#
pub fn fromSpkiDer(der: &[u8]) -> Result<RsaPublicKey, RsaError>Returns the key in a DER SubjectPublicKeyInfo with the rsaEncryption algorithm, as in a
PEM PUBLIC KEY.
Errors#
InvalidEncodingif the DER is not valid, or the BIT STRING has unused bits.InvalidParametersif the algorithm is notrsaEncryptionwith NULL or no parameters.InvalidKeyif the key is not valid, as withRsaPublicKey.fromComponents.
Method pkcs1DerLength#
pub fn pkcs1DerLength(self: &RsaPublicKey) -> usizeReturns the length in bytes of RsaPublicKey.writePkcs1Der.
Method writePkcs1Der#
pub fn writePkcs1Der(self: &RsaPublicKey, output: &mut [u8]) -> Result<usize, RsaError>Writes the key as a DER PKCS #1 RSAPublicKey to the start of output and returns its
length.
Errors#
InvalidLengthifoutputis shorter thanRsaPublicKey.pkcs1DerLength.
Method spkiDerLength#
pub fn spkiDerLength(self: &RsaPublicKey) -> usizeReturns the length in bytes of RsaPublicKey.writeSpkiDer.
Method writeSpkiDer#
pub fn writeSpkiDer(self: &RsaPublicKey, output: &mut [u8]) -> Result<usize, RsaError>Writes the key as a DER SubjectPublicKeyInfo with rsaEncryption to the start of
output and returns its length.
Errors#
InvalidLengthifoutputis shorter thanRsaPublicKey.spkiDerLength.
Implementation RsaPrivateKey#
impl RsaPrivateKeyAssociated function fromPkcs1Der#
pub fn fromPkcs1Der(der: &[u8]) -> Result<RsaPrivateKey, RsaError>Returns the key in a DER PKCS #1 RSAPrivateKey, as in a PEM RSA PRIVATE KEY.
Errors#
InvalidEncodingif the DER is not valid, not minimal, or has bytes after it.InvalidParametersif it is a multi-prime key (version 1).InvalidKeyif the parts are not valid, as withRsaPrivateKey.fromComponents.
Associated function fromPkcs8Der#
pub fn fromPkcs8Der(der: &[u8]) -> Result<RsaPrivateKey, RsaError>Returns the key in a DER PKCS #8 PrivateKeyInfo with the rsaEncryption algorithm, as
in a PEM PRIVATE KEY. Attributes and a public key after the private key are skipped.
Errors#
InvalidEncodingif the DER is not valid, or the version is not 0 or 1.InvalidParametersif the algorithm is notrsaEncryptionwith NULL or no parameters, or the key is multi-prime.InvalidKeyif the parts are not valid, as withRsaPrivateKey.fromComponents.
Gotchas#
It does not decrypt an ENCRYPTED PRIVATE KEY.
Method pkcs1DerLength#
pub fn pkcs1DerLength(self: &RsaPrivateKey) -> usizeReturns the length in bytes of RsaPrivateKey.writePkcs1Der.
Method writePkcs1Der#
pub fn writePkcs1Der(self: &RsaPrivateKey, output: &mut [u8]) -> Result<usize, RsaError>Writes the key as a DER PKCS #1 RSAPrivateKey to the start of output and returns its
length.
Errors#
InvalidLengthifoutputis shorter thanRsaPrivateKey.pkcs1DerLength.
Method pkcs8DerLength#
pub fn pkcs8DerLength(self: &RsaPrivateKey) -> usizeReturns the length in bytes of RsaPrivateKey.writePkcs8Der.
Method writePkcs8Der#
pub fn writePkcs8Der(self: &RsaPrivateKey, output: &mut [u8]) -> Result<usize, RsaError>Writes the key as a DER PKCS #8 PrivateKeyInfo (version 0, rsaEncryption) to the start
of output and returns its length.
Errors#
InvalidLengthifoutputis shorter thanRsaPrivateKey.pkcs8DerLength.
RsaPssHash#
pub enum RsaPssHashA hash that the parameters of an RSA-PSS key name (RFC 4055).
Sha1#
Sha1SHA-1, the default when the parameters name no hash.
Sha224#
Sha224SHA-224.
Sha256#
Sha256SHA-256.
Sha384#
Sha384SHA-384.
Sha512#
Sha512SHA-512.
Sha512_224#
Sha512_224SHA-512/224.
Sha512_256#
Sha512_256SHA-512/256.
Implementation RsaPssHash#
impl RsaPssHashMethod digestLength#
pub fn digestLength(self: &Self) -> usizeReturns the length in bytes of the digest of the hash, such as 32 for SHA-256.
RsaPssRestrictions#
pub struct RsaPssRestrictionsThe parameters of an RSA-PSS key, which limit the signatures that the key may make.
Details#
A key with these parameters signs only with hash, MGF1 with maskHash, and a salt of at
least saltLength bytes. The defaults of RFC 4055 are SHA-1, MGF1 with SHA-1, and 20 bytes.
Gotchas#
The sign and verify functions do not read these. The caller checks its options against them, as Node.js and OpenSSL do.
Field hash#
pub hash: RsaPssHashThe hash of the message.
Field maskHash#
pub maskHash: RsaPssHashThe hash of MGF1.
Field saltLength#
pub saltLength: u32The smallest salt length in bytes.
RsaPssPublicKey#
pub struct RsaPssPublicKeyAn RSA public key with the RSA-PSS algorithm (id-RSASSA-PSS), as OpenSSL writes for a key
of type rsa-pss.
Details#
restrictions is None when the algorithm has no parameters, so the key may sign with any
PSS parameters. Read one with RsaPssPublicKey.fromSpkiDer.
Field key#
pub key: RsaPublicKeyThe key.
Field restrictions#
pub restrictions: Option<RsaPssRestrictions>The parameters of the key, or None.
RsaPssPrivateKey#
pub struct RsaPssPrivateKeyAn RSA private key with the RSA-PSS algorithm (id-RSASSA-PSS), as OpenSSL writes for a key
of type rsa-pss.
Details#
restrictions is None when the algorithm has no parameters. Read one with
RsaPssPrivateKey.fromPkcs8Der.
Field key#
pub key: RsaPrivateKeyThe key.
Field restrictions#
pub restrictions: Option<RsaPssRestrictions>The parameters of the key, or None.
Implementation RsaPssPublicKey#
impl RsaPssPublicKeyAssociated function fromSpkiDer#
pub fn fromSpkiDer(der: &[u8]) -> Result<RsaPssPublicKey, RsaError>Returns the key in a DER SubjectPublicKeyInfo with the id-RSASSA-PSS algorithm, as in
a PEM PUBLIC KEY that OpenSSL writes for an rsa-pss key.
Errors#
InvalidEncodingif the DER is not valid, or the BIT STRING has unused bits.InvalidParametersif the algorithm is notid-RSASSA-PSS, or its parameters name a hash other than SHA-1 or SHA-2, a mask other than MGF1, a salt length aboveu32.MAX, or a trailer field other than 1.InvalidKeyif the key is not valid, as withRsaPublicKey.fromComponents.
Method spkiDerLength#
pub fn spkiDerLength(self: &RsaPssPublicKey) -> usizeReturns the length in bytes of RsaPssPublicKey.writeSpkiDer.
Method writeSpkiDer#
pub fn writeSpkiDer(self: &RsaPssPublicKey, output: &mut [u8]) -> Result<usize, RsaError>Writes the key as a DER SubjectPublicKeyInfo with id-RSASSA-PSS to the start of
output and returns its length. The parameters are left out when restrictions is
None, and each field that holds its default is left out, as OpenSSL writes them.
Errors#
InvalidLengthifoutputis shorter thanRsaPssPublicKey.spkiDerLength.
Implementation RsaPssPrivateKey#
impl RsaPssPrivateKeyAssociated function fromPkcs8Der#
pub fn fromPkcs8Der(der: &[u8]) -> Result<RsaPssPrivateKey, RsaError>Returns the key in a DER PKCS #8 PrivateKeyInfo with the id-RSASSA-PSS algorithm, as
in a PEM PRIVATE KEY that OpenSSL writes for an rsa-pss key. Attributes and a public
key after the private key are skipped.
Errors#
InvalidEncodingif the DER is not valid, or the version is not 0 or 1.InvalidParametersif the algorithm or its parameters are not supported, as withRsaPssPublicKey.fromSpkiDer, or the key is multi-prime.InvalidKeyif the parts are not valid, as withRsaPrivateKey.fromComponents.
Method publicKey#
pub fn publicKey(self: &RsaPssPrivateKey) -> RsaPssPublicKeyReturns the public half of the key with the same parameters.
Method pkcs8DerLength#
pub fn pkcs8DerLength(self: &RsaPssPrivateKey) -> usizeReturns the length in bytes of RsaPssPrivateKey.writePkcs8Der.
Method writePkcs8Der#
pub fn writePkcs8Der(self: &RsaPssPrivateKey, output: &mut [u8]) -> Result<usize, RsaError>Writes the key as a DER PKCS #8 PrivateKeyInfo (version 0, id-RSASSA-PSS) to the start
of output and returns its length. The parameters are written as with
RsaPssPublicKey.writeSpkiDer.
Errors#
InvalidLengthifoutputis shorter thanRsaPssPrivateKey.pkcs8DerLength.
Implementation RsaPrivateKey#
impl RsaPrivateKeyMethod decryptPkcs1#
pub fn decryptPkcs1(self: &RsaPrivateKey, ciphertext: &[u8], output: &mut [u8]) -> Result<
usize,
RsaError,
>Decrypts an RSAES-PKCS1-v1_5 ciphertext with implicit rejection and returns the length of
the message that it writes to the start of output.
When the padding is not valid, it returns a message derived from the private key and the
ciphertext instead of an error, as OpenSSL 3.2 and later and Node.js do, so a padding oracle
does not reveal the plaintext. The check and the choice run without a branch on secret
data. The bytes of output after the message are zero.
Errors#
InvalidLengthifciphertextis not as long as the modulus, oroutputis shorter than the modulus length less 11.InvalidCiphertextifciphertextis not less than the modulus.InvalidKeyif the private parts do not match the public key.
Gotchas#
A wrong key or a changed ciphertext gives a wrong message, not an error. Use it only for formats that require PKCS #1 v1.5, and check the message with something else, such as its expected length or a MAC.
Category: Cryptography.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.