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#

  • RsaError for 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#

silk
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#

silk
pub enum RsaError

The reason that an RSA key, parameter, signature, or ciphertext was rejected.

InvalidKey#

silk
InvalidKey

A 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#

silk
InvalidEncoding

A DER value is not valid, not minimal, or has bytes after it.

InvalidParameters#

silk
InvalidParameters

An algorithm identifier, a key size, or an exponent is not supported.

InvalidLength#

silk
InvalidLength

An 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#

silk
MessageTooLong

The message is too long to encrypt with this key.

KeyTooSmall#

silk
KeyTooSmall

The key is too short for the hash, the salt, or the padding.

InvalidSignature#

silk
InvalidSignature

The signature does not match the digest and the key.

InvalidCiphertext#

silk
InvalidCiphertext

The ciphertext is not valid for this key and padding.

Implementation Debug for RsaError#

silk
impl Debug for RsaError

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

Writes the name of the variant, such as InvalidKey.

RsaPublicKey#

silk
pub struct RsaPublicKey

An 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#

silk
pub struct RsaPrivateKey

An 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#

silk
impl RsaPublicKey

Associated function fromComponents#

silk
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#

  • InvalidKey if a value is not in this range.

Method bitLength#

silk
pub fn bitLength(self: &RsaPublicKey) -> usize

Returns the number of significant bits of the modulus, such as 2048.

Method byteLength#

silk
pub fn byteLength(self: &RsaPublicKey) -> usize

Returns the length in bytes of the modulus, which is the length of every signature and ciphertext of the key.

Method modulus#

silk
pub fn modulus(self: &RsaPublicKey) -> Natural

Returns the modulus as a number.

Method exponent#

silk
pub fn exponent(self: &RsaPublicKey) -> Natural

Returns the public exponent as a number.

Method verifyPkcs1#

silk
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#

  • InvalidLength if digest is not as long as the output of H, or signature is not as long as the modulus.

  • KeyTooSmall if the key cannot hold the digest of H.

  • InvalidSignature if the signature does not match.

Method verifyPss#

silk
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#

  • InvalidLength if digest is not as long as the output of H, or signature is not as long as the modulus.

  • InvalidSignature if the signature does not match.

Method maximumPssSaltLength#

silk
pub fn maximumPssSaltLength<H: Digest>(self: &RsaPublicKey) -> usize

Returns 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#

silk
pub effect fn encryptOaep<H: Digest>(
    self: &RsaPublicKey,
    label: &[u8],
    message: &[u8],
    output: &mut [u8],
  ) -> ()
    ! RsaError
    ? &mut Random

Encrypts 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#

  • InvalidLength if output is not as long as the modulus.

  • KeyTooSmall if the key is too short for H.

  • MessageTooLong if message is too long.

Method encryptPkcs1#

silk
pub effect fn encryptPkcs1(self: &RsaPublicKey, message: &[u8], output: &mut [u8]) -> ()
    ! RsaError
    ? &mut Random

Encrypts 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#

  • InvalidLength if output is not as long as the modulus.

  • MessageTooLong if message is 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#

silk
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#

  • InvalidLength if signature is not as long as the modulus, or output is shorter than the message.

  • InvalidSignature if signature is 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#

silk
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#

  • InvalidLength if input or output is not as long as the modulus.

  • InvalidCiphertext if input is not less than the modulus.

Gotchas#

Raw RSA is not secure on its own. Use it only to build a padding scheme.

Implementation RsaPrivateKey#

silk
impl RsaPrivateKey

Associated function fromComponents#

silk
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#

  • InvalidKey if 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#

silk
pub effect fn generate(bits: usize, exponent: u32) -> RsaPrivateKey
    ! RsaError
    ? &mut Random

Returns 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#

  • InvalidParameters if bits is not 512 to 8192, or exponent is 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#

silk
pub fn publicKey(self: &RsaPrivateKey) -> RsaPublicKey

Returns the public half of the key.

Method privateExponent#

silk
pub fn privateExponent(self: &RsaPrivateKey) -> Natural

Returns the private exponent d.

Method firstPrime#

silk
pub fn firstPrime(self: &RsaPrivateKey) -> Natural

Returns the first prime p.

Method secondPrime#

silk
pub fn secondPrime(self: &RsaPrivateKey) -> Natural

Returns the second prime q.

Method firstExponent#

silk
pub fn firstExponent(self: &RsaPrivateKey) -> Natural

Returns d mod (p - 1).

Method secondExponent#

silk
pub fn secondExponent(self: &RsaPrivateKey) -> Natural

Returns d mod (q - 1).

Method coefficient#

silk
pub fn coefficient(self: &RsaPrivateKey) -> Natural

Returns the inverse of q modulo p.

Method signPkcs1#

silk
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#

  • InvalidLength if digest is not as long as the output of H, or signature is not as long as the modulus.

  • KeyTooSmall if the key cannot hold the digest of H.

  • InvalidKey if the private parts do not match the public key.

Method signPss#

silk
pub effect fn signPss<H: Digest>(
    self: &RsaPrivateKey,
    digest: &[u8],
    saltLength: usize,
    signature: &mut [u8],
  ) -> ()
    ! RsaError
    ? &mut Random

Signs 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#

  • InvalidLength if digest is not as long as the output of H, or signature is not as long as the modulus.

  • KeyTooSmall if the key cannot hold the digest of H and the salt.

  • InvalidKey if the private parts do not match the public key.

Method decryptOaep#

silk
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#

  • InvalidLength if ciphertext is not as long as the modulus, or output is shorter than the longest message for the key: the modulus length less twice the digest length less 2.

  • KeyTooSmall if the key is too short for H.

  • InvalidCiphertext if the ciphertext is not valid.

  • InvalidKey if the private parts do not match the public key.

Method signPkcs1Raw#

silk
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#

  • InvalidLength if signature is not as long as the modulus.

  • MessageTooLong if message is too long.

  • InvalidKey if 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#

silk
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#

  • InvalidLength if input or output is not as long as the modulus.

  • InvalidCiphertext if input is not less than the modulus.

  • InvalidKey if 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#

silk
impl RsaPublicKey

Associated function fromCertificateKey#

silk
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#

  • InvalidEncoding if the DER is not valid.

  • InvalidParameters if the algorithm is wrong.

  • InvalidLength if unusedBits is not 0.

  • InvalidKey if the key is not in the profile.

Method verifyCertificate#

silk
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#

  • InvalidLength if unusedBits is not 0.

  • InvalidParameters if the algorithm or its parameters are not supported.

  • InvalidLength if signature does not have the length of the modulus.

  • InvalidSignature if 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#

silk
impl RsaPublicKey

Associated function fromPkcs1Der#

silk
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#

  • InvalidEncoding if the DER is not valid, not minimal, or has bytes after it.

  • InvalidKey if the key is not valid, as with RsaPublicKey.fromComponents.

Associated function fromSpkiDer#

silk
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#

  • InvalidEncoding if the DER is not valid, or the BIT STRING has unused bits.

  • InvalidParameters if the algorithm is not rsaEncryption with NULL or no parameters.

  • InvalidKey if the key is not valid, as with RsaPublicKey.fromComponents.

Method pkcs1DerLength#

silk
pub fn pkcs1DerLength(self: &RsaPublicKey) -> usize

Returns the length in bytes of RsaPublicKey.writePkcs1Der.

Method writePkcs1Der#

silk
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#

Method spkiDerLength#

silk
pub fn spkiDerLength(self: &RsaPublicKey) -> usize

Returns the length in bytes of RsaPublicKey.writeSpkiDer.

Method writeSpkiDer#

silk
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#

Implementation RsaPrivateKey#

silk
impl RsaPrivateKey

Associated function fromPkcs1Der#

silk
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#

  • InvalidEncoding if the DER is not valid, not minimal, or has bytes after it.

  • InvalidParameters if it is a multi-prime key (version 1).

  • InvalidKey if the parts are not valid, as with RsaPrivateKey.fromComponents.

Associated function fromPkcs8Der#

silk
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#

  • InvalidEncoding if the DER is not valid, or the version is not 0 or 1.

  • InvalidParameters if the algorithm is not rsaEncryption with NULL or no parameters, or the key is multi-prime.

  • InvalidKey if the parts are not valid, as with RsaPrivateKey.fromComponents.

Gotchas#

It does not decrypt an ENCRYPTED PRIVATE KEY.

Method pkcs1DerLength#

silk
pub fn pkcs1DerLength(self: &RsaPrivateKey) -> usize

Returns the length in bytes of RsaPrivateKey.writePkcs1Der.

Method writePkcs1Der#

silk
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#

Method pkcs8DerLength#

silk
pub fn pkcs8DerLength(self: &RsaPrivateKey) -> usize

Returns the length in bytes of RsaPrivateKey.writePkcs8Der.

Method writePkcs8Der#

silk
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#

RsaPssHash#

silk
pub enum RsaPssHash

A hash that the parameters of an RSA-PSS key name (RFC 4055).

Sha1#

silk
Sha1

SHA-1, the default when the parameters name no hash.

Sha224#

silk
Sha224

SHA-224.

Sha256#

silk
Sha256

SHA-256.

Sha384#

silk
Sha384

SHA-384.

Sha512#

silk
Sha512

SHA-512.

Sha512_224#

silk
Sha512_224

SHA-512/224.

Sha512_256#

silk
Sha512_256

SHA-512/256.

Implementation RsaPssHash#

silk
impl RsaPssHash

Method digestLength#

silk
pub fn digestLength(self: &Self) -> usize

Returns the length in bytes of the digest of the hash, such as 32 for SHA-256.

RsaPssRestrictions#

silk
pub struct RsaPssRestrictions

The 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#

silk
pub hash: RsaPssHash

The hash of the message.

Field maskHash#

silk
pub maskHash: RsaPssHash

The hash of MGF1.

Field saltLength#

silk
pub saltLength: u32

The smallest salt length in bytes.

RsaPssPublicKey#

silk
pub struct RsaPssPublicKey

An 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#

silk
pub key: RsaPublicKey

The key.

Field restrictions#

silk
pub restrictions: Option<RsaPssRestrictions>

The parameters of the key, or None.

RsaPssPrivateKey#

silk
pub struct RsaPssPrivateKey

An 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#

silk
pub key: RsaPrivateKey

The key.

Field restrictions#

silk
pub restrictions: Option<RsaPssRestrictions>

The parameters of the key, or None.

Implementation RsaPssPublicKey#

silk
impl RsaPssPublicKey

Associated function fromSpkiDer#

silk
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#

  • InvalidEncoding if the DER is not valid, or the BIT STRING has unused bits.

  • InvalidParameters if the algorithm is not id-RSASSA-PSS, or its parameters name a hash other than SHA-1 or SHA-2, a mask other than MGF1, a salt length above u32.MAX, or a trailer field other than 1.

  • InvalidKey if the key is not valid, as with RsaPublicKey.fromComponents.

Method spkiDerLength#

silk
pub fn spkiDerLength(self: &RsaPssPublicKey) -> usize

Returns the length in bytes of RsaPssPublicKey.writeSpkiDer.

Method writeSpkiDer#

silk
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#

Implementation RsaPssPrivateKey#

silk
impl RsaPssPrivateKey

Associated function fromPkcs8Der#

silk
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#

  • InvalidEncoding if the DER is not valid, or the version is not 0 or 1.

  • InvalidParameters if the algorithm or its parameters are not supported, as with RsaPssPublicKey.fromSpkiDer, or the key is multi-prime.

  • InvalidKey if the parts are not valid, as with RsaPrivateKey.fromComponents.

Method publicKey#

silk
pub fn publicKey(self: &RsaPssPrivateKey) -> RsaPssPublicKey

Returns the public half of the key with the same parameters.

Method pkcs8DerLength#

silk
pub fn pkcs8DerLength(self: &RsaPssPrivateKey) -> usize

Returns the length in bytes of RsaPssPrivateKey.writePkcs8Der.

Method writePkcs8Der#

silk
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#

Implementation RsaPrivateKey#

silk
impl RsaPrivateKey

Method decryptPkcs1#

silk
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#

  • InvalidLength if ciphertext is not as long as the modulus, or output is shorter than the modulus length less 11.

  • InvalidCiphertext if ciphertext is not less than the modulus.

  • InvalidKey if 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.

View the source of this page