Skip to content

std.crypto: every i31 temp holds a 4096-bit modulus, and RSA-4096 has a known-answer test (#1052) - #2672

Merged
ehartford merged 1 commit into
fix-1056from
fix-1052
Oct 25, 2026
Merged

ehartford merged 1 commit into
fix-1056from
fix-1052

Conversation

@ehartford

@ehartford ehartford commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Root cause: i31_from_monty (lib/std/crypto/bigint.w) declared
var one: [u32; 80] and var d: [u32; 80] and i31_montmul wrote d[0..mlen]
with mlen = (bitlen + 30) / 31: 100 words for RSA-3072, 133 for RSA-4096,
which rsa_pkcs1_sha256_verify admits (n_len up to 512 bytes). Any modulus
above 2449 bits wrote past both arrays. The new known-answer test, before
the fix: "panic: index out of bounds" in rsa_pkcs1_sha256_verify, with a
caller frame already overwritten.

I31_MAX_WORDS (140: the header and 133 limbs, with room) sizes every fixed
i31 temp, in bigint.w and rsa.w alike.

Test: test/behavior/behav_1052_rsa4096_kat.w calls
std.crypto.rsa.rsa4096_pkcs1_sha256_kat(): an openssl-generated RSA-4096
PKCS#1 v1.5 SHA-256 signature of "abc" must verify, and the same signature
with one bit flipped must not (the self-check shape of
std.crypto.gcm.aes128_gcm_kat). RSA verification had no test before.

Closes #1052

… a known-answer test (#1052)

Root cause: i31_from_monty (lib/std/crypto/bigint.w) declared
`var one: [u32; 80]` and `var d: [u32; 80]` and i31_montmul wrote d[0..mlen]
with mlen = (bitlen + 30) / 31: 100 words for RSA-3072, 133 for RSA-4096,
which rsa_pkcs1_sha256_verify admits (n_len up to 512 bytes). Any modulus
above 2449 bits wrote past both arrays. The new known-answer test, before
the fix: "panic: index out of bounds" in rsa_pkcs1_sha256_verify, with a
caller frame already overwritten.

I31_MAX_WORDS (140: the header and 133 limbs, with room) sizes every fixed
i31 temp, in bigint.w and rsa.w alike.

Test: test/behavior/behav_1052_rsa4096_kat.w calls
std.crypto.rsa.rsa4096_pkcs1_sha256_kat(): an openssl-generated RSA-4096
PKCS#1 v1.5 SHA-256 signature of "abc" must verify, and the same signature
with one bit flipped must not (the self-check shape of
std.crypto.gcm.aes128_gcm_kat). RSA verification had no test before.
@ehartford
ehartford added this pull request to stack #2678 October 25, 2026 22:52
@ehartford ehartford changed the title fix 1052 std.crypto: every i31 temp holds a 4096-bit modulus, and RSA-4096 has a known-answer test (#1052) Oct 25, 2026
@ehartford
ehartford marked this pull request as ready for review October 25, 2026 23:05
@ehartford
ehartford merged commit 392bf61 into main Oct 25, 2026
@ehartford
ehartford deleted the fix-1052 branch October 25, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[crypto] bigint i31_from_monty fixed [u32;80] temps overflow for moduli above 2449 bits (RSA-3072/4096 admitted by callers)

1 participant