class

Beryl::Crypto::V1::RivestShamirAdleman::PrivateKey

Inherits Reference < Object

An RSA private key of two primes, with a modulus of 2,048 to 4,096 bits. It signs and decrypts.

private_key = Beryl::Crypto::V1::RSA::PrivateKey.new(
  modulus: n, public_exponent: e, private_exponent: d,
  first_prime: p, second_prime: q,
  first_prime_exponent: d_p, second_prime_exponent: d_q, coefficient: q_inv,
)
signature = private_key.sign_with_pss("message", hash_function: Beryl::Crypto::V1::SHA256, salt_bytesize: 32)
private_key.public_key.verify_pss_signature("message", signature, hash_function: Beryl::Crypto::V1::SHA256, salt_bytesize: 32) # => true

The private operation uses the Chinese remainder theorem (RFC 8017 §5.1.2, the second representation), runs in time that does not depend on the key or on the input, and checks its result with the public exponent before releasing it (FaultInPrivateKeyOperation). Key generation and keys of more than two primes are not supported; keys come from key files, which a caller parses.

Constants

KEY_TEST_INPUT = 2_u64

The input of the test operation run when the key is built.

LARGEST_MODULUS_BIT_COUNT = 4096
SMALLEST_MODULUS_BIT_COUNT = 2048

Constructors

new(*, modulus : Bytes, public_exponent : Bytes | Int, private_exponent : Bytes, first_prime : Bytes, second_prime : Bytes, first_prime_exponent : Bytes, second_prime_exponent : Bytes, coefficient : Bytes)

Takes the components of RFC 8017 §3.2 as big-endian bytes (leading zero bytes allowed): the modulus n, the public exponent e (or an integer), the private exponent d, the primes p (first_prime) and q (second_prime), d mod (p - 1) (first_prime_exponent), d mod (q - 1) (second_prime_exponent) and q⁻¹ mod p (coefficient).

Raises KeySizeNotSupported or PublicExponentNotSupported for a modulus or exponent outside the ranges Beryl accepts, and MalformedPrivateKey when the components do not belong together. The checks on the secret components run in constant time and give one verdict; then one private operation is run and checked with the public exponent. private_exponent is checked to be below the modulus and is not kept, since the Chinese remainder theorem does not need it.

Source

Instance methods

decrypt_with_oaep(ciphertext : Bytes, *, hash_function : D.class, label : Bytes | String = Bytes.empty) : Bytes | Nil forall D

Returns the message inside ciphertext, encrypted with RSAES-OAEP (RFC 8017 §7.1.2) with hash_function for the label and for MGF1 and the same label; or nil when the ciphertext is the wrong size, is not below the modulus, or does not decode. Every one of those gives the same nil, after the same work, so the response does not tell them apart (the attack of Manger, 2001, needs it to).

Source
finalize
Source
modulus_bit_count

The size of the modulus in bits.

Source
modulus_bytesize

The size of a signature or a ciphertext under this key, in bytes.

Source
public_key

The matching public key.

Source
sign_with_pkcs1(message : Bytes | String, *, hash_function : D.class) : Bytes forall D

Returns the PKCS #1 v1.5 signature (RFC 8017 §8.2.1) of message hashed with hash_function: SHA224, SHA256, SHA384, SHA512 or SHA512_256. The signature is the same every time.

Source
sign_with_pss(message : Bytes | String, *, hash_function : D.class, salt_bytesize : Int32) : Bytes forall D

Returns a PSS signature (RFC 8017 §8.1.1) of message, with hash_function (a SHA-2 hash) for the message and for MGF1 and a random salt of salt_bytesize bytes from Random::Secure. The usual choice is a salt as long as the hash (TLS 1.3 requires it). Raises SaltTooLongForModulus when the salt does not fit.

Source
wipe

Overwrites the secret parts of the key. The key cannot be used after.

Source