Skip to Content
oxcrypto

RSA signatures

Sign with RSASSA-PSS or RSASSA-PKCS1-v1_5 and SHA-1, SHA-2 or SHA-3.

from oxcrypto.signature.rsa import RSA

private_key = RSA.generate(bits=3072)
public_key = private_key.public_key()

signature = RSA.PSS_SHA256.sign(private_key, b"message")
assert RSA.PSS_SHA256.verify(public_key, signature, b"message")

pem = private_key.to_pem(password=b"passphrase")
assert RSA.load_private_pem(pem, password=b"passphrase") == private_key

Variants are PKCS1V15_<HASH> and PSS_<HASH>, where <HASH> is SHA1, SHA224, SHA256, SHA384, SHA512, SHA512_224, SHA512_256, SHA3_224, SHA3_256, SHA3_384 or SHA3_512. SHA-1 is for verifying existing signatures only.

PSS uses MGF1 with the same hash as the message. The keyword-only salt_length defaults to the hash output size and must match between sign and verify. PKCS#1 v1.5 signatures are deterministic. PSS signatures are randomized from the operating system CSPRNG.

generate takes keyword-only bits (2048 to 8192, default 3072) and public_exponent (odd, 65537 to 2³³ − 1, default 65537), and draws from the operating system CSPRNG. Loading accepts keys from 1024 to 8192 bits and exponents from 2 to 2³³ − 1, so older 1024-bit keys can still verify existing signatures. Do not create new keys below 2048 bits.

MethodFormats
RSAPrivateKey.to_der, to_pem, RSA.load_private_der, load_private_pem"pkcs8" (default, optional password), "pkcs1"
RSAPublicKey.to_der, to_pem, RSA.load_public_der, load_public_pem"spki" (default), "pkcs1"

The same key objects work with RSA encryption.

Side channels

RUSTSEC-2023-0071 (the Marvin attack, CVE-2023-49092) reported timing leaks in earlier rsa releases. The advisory lists 0.10.0 as patched: the 0.10 series moves to crypto-bigint and blinds private operations, following draft-irtf-cfrg-rsa-guidance. oxcrypto has not measured timing independently. The rsa README tracks remaining work in RustCrypto/RSA#711.

RSA-PSS uses the selected digest for both message hashing and MGF1.

On this page