Beryl::Crypto::V1::RivestShamirAdleman::PrivateKey
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
The input of the test operation run when the key is built.
Constructors
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.
Instance methods
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).
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.
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.