Module
std.x.crypto.rsa — RSA signature VERIFICATION (RFC 8017, PKCS #1 v2.2): RSASSA-PKCS1-v1_5 (§8.2.2) and RSASSA-PSS with MGF1 (§8.1.2, §9.1.2, B.2.1), over SHA-256, SHA-384 and SHA-512. Stability tier: x — the D53 crypto ladder's RSA rung (sprint sc79, wolf-std#101), the algorithm most of the public PKI still signs with: X.509 certificate signatures (PKCS#1 v1.5 and PSS) and TLS 1.3's rsa_pss_rsae_* CertificateVerify.
The nursery banner (D31). std.x.crypto.rsa is not std.crypto: its path, names and behaviour may change or vanish in one sprint; no other std module may depend on it. Its reason and graduation trigger live in std/x/README.md. The digests are std.x.crypto.sha2's, called through the module boundary — a resident-on-resident edge the graduation move must resolve (named beside curve25519's and p256's).
Verify only. No signing, no key generation, no encryption or decryption: nothing in this module ever holds a private key.
## Public-key work: no constant-time obligation
Every input here is public — a public key, a message, a signature — so no branch or memory access protects a secret, and this module carries no constant-time obligation (the sc18 verify-is-public rule). The modular exponentiation branches on the exponent's bits and the comparisons exit early; both are public. It is stated here, and on each pub fn, so that a later signing rung cannot inherit this bignum by assumption: signing needs its own constant-time design.
## The key policy (refusals by name)
public_key and parse_public_key refuse, each by its own tag: small_modulus (n under 2048 bits), large_modulus (n over 4096 bits: the bignum is sized for 2048–4096, and the bound keeps a hostile key from buying unbounded work), bad_modulus (an even n), bad_exponent (e even, e < 3, e ≥ n, or e ≥ 2^256 — RFC 8017 §3.1's range with FIPS 186-5's upper bound), and, for the DER form, der (an RSAPublicKey that is not strict X.690 DER: indefinite or non-minimal lengths, a negative or non-minimal INTEGER, trailing bytes). The verifiers re-check the policy on every call, because this pin has no private fields and a PublicKey can be forged: a key outside the policy verifies nothing.
## The two schemes, exactly as RFC 8017 writes them
verify_pkcs1 is RSASSA-PKCS1-V1_5-VERIFY: RSAVP1, then the ENCODE-AND-COMPARE of §8.2.2 steps 3–4 — the expected encoding 0x00 0x01 PS 0x00 T is built from the message and compared octet for octet, with T the DER DigestInfo whose algorithm parameters are NULL (§9.2 Note 1). No ASN.1 is parsed out of a signature, so the BER-padding and garbage-in-DigestInfo forgery classes have nothing to hide in; a DigestInfo without the NULL is rejected. verify_pss is RSASSA-PSS-VERIFY with emBits = modBits − 1, MGF1 over the message hash, and the salt length the caller names (TLS 1.3's rsa_pss_rsae_* require the digest length, RFC 8446 §4.2.3).
## The bignum
Little-endian limbs of 24 bits in checked ints, so every product is under 2^48 and every accumulator under 2^50 (X3's checked arithmetic enforces the bound at each operation). Multiplication is Montgomery's (CIOS, R = 2^(24k) for k limbs); R² mod n is built by doubling 2^(L+t) mod n, where L = 24k = t·2^s with t odd, and then squaring it s times in the Montgomery domain. The exponentiation is left-to-right square-and-multiply over e's bits: 19 Montgomery multiplications for e = 65537.
Byte sequences are List[byte], big-endian (RFC 8017's I2OSP and OS2IP). to_hex is this module's own rendering (the ladder precedent).
Functions and methods
fn · Source line 97
pub fn hash_sha256() -> int
The SHA-256 selector for verify_pkcs1 and verify_pss: its digest size in bits. ct: public.
rsa.hash_sha256() == 256
fn · Source line 106
pub fn hash_sha384() -> int
The SHA-384 selector. ct: public.
rsa.hash_sha384() == 384
fn · Source line 115
pub fn hash_sha512() -> int
The SHA-512 selector. ct: public.
rsa.hash_sha512() == 512
fn · Source line 194
pub fn public_key(modulus: List[byte], exponent: List[byte]) -> PublicKey ! { small_modulus, large_modulus, bad_modulus, bad_exponent, }
An RSA public key from its modulus and exponent as big-endian bytes (leading zero octets allowed and dropped), under the module's policy.
Rows: small_modulus — n is under 2048 bits; large_modulus — n is over 4096 bits; bad_modulus — n is even; bad_exponent — e is even, below 3, at least n, or at least 2^256. Never traps. ct: public data.
Example (prose — a fenced example cannot carry a 2048-bit modulus on one line): rsa.public_key(n, e) answers the key for a 2048-bit odd n and e = 65537, and small_modulus for a 1024-bit n; tests/x/crypto/rsa/key_policy_rows.lu holds every refusal.
fn · Source line 310
pub fn parse_public_key(der_bytes: List[byte]) -> PublicKey ! { der, small_modulus, large_modulus, bad_modulus, bad_exponent, }
An RSA public key from its DER RSAPublicKey (RFC 8017 A.1.1: SEQUENCE { modulus INTEGER, publicExponent INTEGER }) — the content of an X.509 rsaEncryption SubjectPublicKeyInfo's BIT STRING.
Rows: der — not exactly one strict-DER SEQUENCE of two non-negative minimal INTEGERs; then public_key's four refusals. Never traps. ct: public data.
fn · Source line 791
pub fn verify_pkcs1(key: PublicKey, hash: int, message: List[byte], sig: List[byte]) -> bool
RSASSA-PKCS1-V1_5-VERIFY (RFC 8017 §8.2.2): true iff sig is the signature of message under key with the selected hash. The expected encoding 0x00 0x01 PS 0x00 T (T the DER DigestInfo with NULL parameters, PS at least eight 0xFF octets) is built and compared whole with RSAVP1's output (steps 3–4), so nothing in the signature is parsed. False — never a trap — for a key outside the module's policy, a signature that is not exactly k octets, a signature representative ≥ n, or any octet that differs.
Traps assert only when hash is not one of the hash_* selectors (the caller's constant, not data). ct: VERIFY IS PUBLIC DATA — no branch here protects a secret.
Example (prose — a 2048-bit key does not fit a fenced example line): every case of the nine vendored Wycheproof RSASSA-PKCS1-v1_5 files is a row under tests/x/crypto/rsa/, decided again by rsa_check.py.
fn · Source line 852
pub fn verify_pss( key: PublicKey, hash: int, message: List[byte], sig: List[byte], salt_len: int, ) -> bool
RSASSA-PSS-VERIFY (RFC 8017 §8.1.2, EMSA-PSS §9.1.2): true iff sig is a PSS signature of message under key, with MGF1 over the selected hash and a salt of exactly salt_len octets. emBits is modBits − 1, so for a modulus whose length is 1 mod 8 the encoded message is k − 1 octets and RSAVP1's leading octet must be zero. False — never a trap — for a key outside the policy, a signature that is not k octets, a representative ≥ n, or an inconsistent encoding.
TLS 1.3's rsa_pss_rsae_sha256/384/512 pass the digest length as salt_len (RFC 8446 §4.2.3). Traps assert when hash is not a hash_* selector or salt_len is negative (the caller's constants). ct: VERIFY IS PUBLIC DATA.
Example (prose, as for verify_pkcs1): every case of the eight vendored Wycheproof RSASSA-PSS files is a row under tests/x/crypto/rsa/.
fn · Source line 894
pub fn to_hex(b: List[byte]) -> str
Lowercase hex of a byte list. ct: public data (a rendering convenience only).
rsa.to_hex("abc".bytes()) == "616263"
rsa.to_hex(List[byte]()) == ""