RSA encryption
Encrypt with RSAES-OAEP, or decrypt legacy RSAES-PKCS1-v1_5 data.
from oxcrypto.encryption.rsa import RSA
private_key = RSA.generate(bits=3072)
public_key = private_key.public_key()
ciphertext = RSA.OAEP_SHA256.encrypt(public_key, b"secret", label=b"context")
assert RSA.OAEP_SHA256.decrypt(private_key, ciphertext, label=b"context") == b"secret"
legacy = RSA.OAEP_SHA256.encrypt(public_key, b"secret", mgf1_hash=RSA.OAEP_SHA1)
assert RSA.OAEP_SHA256.decrypt(private_key, legacy, mgf1_hash=RSA.OAEP_SHA1) == b"secret"OAEP_<HASH> variants use SHA1, SHA224, SHA256, SHA384, SHA512,
SHA512_224, SHA512_256, SHA3_224, SHA3_256, SHA3_384 or SHA3_512
to hash the label. The keyword-only mgf1_hash takes another OAEP_<HASH>
variant whose hash MGF1 uses, and defaults to the variant's own hash. label
defaults to empty. Both must match between encrypt and decrypt. The
plaintext can be at most the modulus size minus twice the hash size minus 2
bytes.
PKCS1V15 is legacy RSAES-PKCS1-v1_5, for reading existing data. It takes no
label or mgf1_hash. Its decryption uses implicit rejection as written in
rsa 0.10 (draft-irtf-cfrg-rsa-guidance): a ciphertext with invalid padding
returns a pseudorandom message derived from the key and ciphertext instead of
raising, so the caller cannot tell from the result whether the padding was
valid. The surrounding protocol must authenticate the decrypted message.
Side channels
RUSTSEC-2023-0071 (the Marvin attack) reported timing leaks in RSA decryption
in earlier rsa releases, which the advisory lists as patched in 0.10.0. The
rsa README still warns that PKCS#1 v1.5 encryption has a long history of
Bleichenbacher and Marvin attacks. oxcrypto has not measured timing
independently.
Limitations
rsa0.10 does not check that the first octet of the decoded OAEP block is zero (RFC 8017 section 7.1.2, step 3.g), so Wycheproof's "First byte is 1" ciphertexts decrypt instead of failing. Upstream tests this behavior deliberately.- Private-key operations do not use CRT (see signature zeroization).
| Standard | RFC 8017 (PKCS #1 v2.2) sections 7.1 and 7.2; NIST SP 800-56B Rev. 2 for OAEP |
| Crate | rsa 0.10.0 (RustCrypto, MIT OR Apache-2.0), stable |
| Audit | See RSA signatures |
| MSRV | 1.87, builds with the project MSRV 1.88 |
| Experimental | No; PKCS#1 v1.5 is legacy |
| Test vectors | Wycheproof rsa_oaep_* and rsa_pkcs1_2048 |