From f42840d6411360a898b6fda72ffe3208a69ec2ec Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 29 Sep 2026 13:15:29 -0500 Subject: [PATCH] rsa: make SecretKey.fromComponents public An OpenPGP RSA secret key carries n, e, d, p, q and u = p^-1 mod q as bare integers rather than DER, so a signer reading one had no way to build a SecretKey. fromComponents takes n, e and d and was already the first half of fromPkcs1Der; it is now public and documented. It gives a key without the CRT: OpenPGP does not store d mod (p-1) or d mod (q-1), and ff's Montgomery arithmetic cannot reduce by the even p - 1 to compute them. The test rebuilds a DER key from its numbers and checks the two sign identically. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01LuezSGJ4rfoAKyUrZAbmQW --- README.md | 2 +- src/rsa.zig | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 09f62f2..bb1e61b 100644 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ the numbers and the reasoning. | `Aes192` | AES-192, the key size `std.crypto` omits — it ships `Aes128` and `Aes256` and nothing between. Built from `std`'s own hardware rounds, so it has `std`'s timing; only the key schedule is written here. **Encryption only**, because CFB and CTR never run a cipher backwards; `initDec` is deliberately absent, so asking for it is a compile error rather than a surprise. | | `weak_keys`, `isWeak` | The four keys for which DES is an involution. A password-derived key can be one by accident, and `usmDESPrivProtocol` derives its key from a password. | | `hasOddParity`, `setOddParity` | The parity convention DES keys are distributed under. The cipher ignores the parity bits entirely — that is what "56-bit key" means. | -| `rsa.SecretKey` | An RSA private key, read from PKCS#1 or PKCS#8 DER or from the PEM around either — `fromPem` tells the two apart by looking. Held by value, so the DER it came from can be wiped. | +| `rsa.SecretKey` | An RSA private key, read from PKCS#1 or PKCS#8 DER or from the PEM around either — `fromPem` tells the two apart by looking — or built by `fromComponents` from bare `n`, `e` and `d`, as an OpenPGP key carries them. Held by value, so the DER it came from can be wiped. | | `rsa.PublicKey` | An RSA public key, from a `SubjectPublicKeyInfo` or a bare PKCS#1 `RSAPublicKey`, DER or PEM. Both shapes are accepted because formats that carry one are inconsistent about which they mean. | | `rsa.pkcs1v1_5.Signer(Hash)` | RFC 8017 RSASSA-PKCS1-v1_5, over SHA-1, SHA-224, SHA-256, SHA-384 or SHA-512. `sign`/`verify` over a message, `signConcat`/`verifyConcat` over its pieces, and `signDigest`/`verifyDigest` for a protocol that hashes something which never exists as contiguous bytes. | | `rsa.SecretKey.Crt` | RFC 8017 §3.2's second representation — the two primes and the three values derived from them — kept when the key carried them, which every PKCS#1 and PKCS#8 key does. Signing then costs a quarter of what it otherwise would, and every signature is verified before release. | diff --git a/src/rsa.zig b/src/rsa.zig index 403b0ca..da3941e 100644 --- a/src/rsa.zig +++ b/src/rsa.zig @@ -523,7 +523,21 @@ pub const SecretKey = struct { return fromDer(der_bytes); } - fn fromComponents( + /// A secret key from its three numbers: the modulus `n`, the public + /// exponent `e` and the private exponent `d`, each big-endian and + /// unsigned. `e` is held to the four bytes `PublicKey.fromBytes` allows, + /// leading zeros included; `n` and `d` may carry any. + /// + /// For formats that carry a key as bare integers rather than as DER. An + /// OpenPGP RSA secret key is one: RFC 9580 section 5.5.5.1 stores `n` and + /// `e`, then `d`, `p`, `q` and `u = p^-1 mod q` -- but not `d mod (p-1)` + /// or `d mod (q-1)`, and those cannot be computed here, because + /// `std.crypto.ff` does Montgomery arithmetic, which needs an odd modulus, + /// and `p - 1` is even. So the key this returns has no second + /// representation and signs the whole-`d` way: about four times slower + /// than the CRT, which is still about a tenth of a second for a 4096-bit + /// key. Every signature is verified before release either way. + pub fn fromComponents( modulus: []const u8, public_exponent: []const u8, private_exponent: []const u8, @@ -1301,6 +1315,42 @@ test "the two representations sign identically" { try testing.expectEqualSlices(u8, without, with); } +test "a key built from n, e and d signs as the DER it came from does" { + // What `fromComponents` is for: a format that hands over bare integers. + // The numbers are read back out of a key that was parsed from DER, so + // the two must sign identically, and neither has the CRT to lean on. + var der_buf: [max_secret_key_der]u8 = undefined; + const sk = try SecretKey.fromPem(&der_buf, key_2048_pkcs8); + const k = sk.modulusLength(); + + var n_bytes: [max_modulus_len]u8 = undefined; + var e_bytes: [max_modulus_len]u8 = undefined; + var d_bytes: [max_modulus_len]u8 = undefined; + try sk.n.toBytes(n_bytes[0..k], .big); + try sk.e.toBytes(e_bytes[0..k], .big); + try sk.d.toBytes(d_bytes[0..k], .big); + + // `d` keeps its leading zeros, if it has any, at the full width of the + // modulus; `e` has to lose them, since a public exponent is refused at + // more than four bytes. + const e_trimmed = std.mem.trimStart(u8, e_bytes[0..k], &.{0}); + const rebuilt = try SecretKey.fromComponents(n_bytes[0..k], e_trimmed, d_bytes[0..k]); + try testing.expect(rebuilt.crt == null); + + var a: [max_modulus_len]u8 = undefined; + var b: [max_modulus_len]u8 = undefined; + const want = try pkcs1v1_5.Signer(Sha256).sign(&a, test_message, sk); + const got = try pkcs1v1_5.Signer(Sha256).sign(&b, test_message, rebuilt); + try testing.expectEqualSlices(u8, want, got); + + // And a `d` of zero is not a key. + @memset(d_bytes[0..k], 0); + try testing.expectError( + error.InvalidKey, + SecretKey.fromComponents(n_bytes[0..k], e_trimmed, d_bytes[0..k]), + ); +} + test "a key whose components contradict each other signs nothing" { // The Bellcore attack in the form this library can be made to suffer it. // A CRT signer that gets one half wrong emits a signature from which -- knotmirror 0.0.1