Skip to content

Commit 3e30c18

Browse files
committed
Record the MessageSigner bare-signature contract in the bcpqc-add-signature-algorithm skill and the architecture guide, so that a new PQC signature port does not carry the reference harness's crypto_sign signed-message envelope forward as MAYO, SNOVA, QR-UOV, SQIsign and AIMer each did, with the encoding assertions a sign-then-verify round-trip test cannot catch, relates to github #2403.
1 parent 85e34e2 commit 3e30c18

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎.claude/skills/bcpqc-add-signature-algorithm/SKILL.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ Before this skill kicks in, the lightweight side of the algorithm must already b
2727
- `<Alg>KeyGenerationParameters` extending `KeyGenerationParameters`, wrapping a `SecureRandom` + `<Alg>Parameters`
2828
- `<Alg>Signer` implementing `MessageSigner` (or `Signer` for streaming) with `init(boolean, CipherParameters)`, `generateSignature(byte[])`, `verifySignature(byte[], byte[])`
2929

30+
**The `MessageSigner` contract is the bare signature — never the NIST `crypto_sign` "sm" envelope.** A reference C implementation's `crypto_sign` writes the *signed message* (`signature || message`, or `message || signature` for some harnesses) and its `crypto_sign_open` recovers the message from it. That envelope is an artefact of the KAT harness making each vector file self-contained; it is not part of any scheme's specification, and a `MessageSigner` must not emit it:
31+
32+
- `generateSignature(byte[])` returns exactly the parameter set's signature bytes. Have the engine write the bare signature — don't allocate `sigBytes + message.length` and strip the message afterwards.
33+
- `verifySignature(byte[], byte[])` requires **exactly** that length, `return false` otherwise. A `>=` / "long enough" check makes the encoding non-unique (trailing bytes ride along on a valid signature) and a missing check indexes past the end of a short buffer, throwing out of `Signature.verify()`.
34+
- The KAT files still record the envelope, so the **test** rebuilds it — see the `TestUtils.SignerOperation.toVectorSignature` hook, or `FalconTest`, which does the same reconstruction inline.
35+
36+
This is not hypothetical: MAYO, SNOVA, QR-UOV, SQIsign and AIMer all shipped the envelope and had to be corrected in 1.86 (github #2403). Because `ContentSigner` hands the signature straight into the structure being signed, the envelope propagated a verbatim copy of the signed data into every certificate, CRL, CMS `SignedData` and TLS `CertificateVerify` built on those algorithms. A sign-then-verify round-trip test passes happily with an envelope on both ends, which is why it survived five ports — hence the encoding assertion in Step 15.
37+
3038
If any of these are missing, that's a separate task (porting the reference implementation). This skill picks up *after* that's done.
3139

3240
## The two-provider split — read this first
@@ -236,6 +244,11 @@ Add `prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/<Alg>Test.java
236244
5. **`test<Alg>RandomSig`** — sign+verify with the non-parameter-bound `Signature.getInstance("<Alg>", "BCPQC")` form.
237245
6. **`testBcProviderKeyInfoConverter`** — **the bridge regression test.** For every parameter set, generate a keypair via BCPQC, then call `BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo.getInstance(pubEnc))` and `BouncyCastleProvider.getPrivateKey(...)` and assert each returned key is a `<Alg>Key` with the right `getParameterSpec().getName()` and `.equals()` the original. If this test passes but tests 1-5 also pass, the algorithm is end-to-end wired. If 1-5 pass but `testBcProviderKeyInfoConverter` fails, step 13 was forgotten.
238246

247+
Then the **encoding regression test**, which is `core`-side rather than part of `<Alg>Test` — the contract it pins belongs to the lightweight `MessageSigner`, not to the JCA shell:
248+
249+
- Add a `test<Alg>()` method to `core/src/test/java/org/bouncycastle/pqc/crypto/test/PqcSignatureEncodingTest.java`, following the existing per-scheme methods: one parameter set (the encoding does not vary by parameter set, and key generation can be expensive) driven through `checkBareSignature`, which asserts the produced signature is exactly the parameter set's signature length, that it verifies, and that none of `signature || message`, `message || signature`, the signature with a byte appended, the signature with 64 bytes appended, the signature truncated by one, or an empty array verifies.
250+
- Add the empty / one-byte cases for the new scheme to `PqcMalformedInputTest.testMalformedSignatureReturnsFalse` too. Note an *oversized* all-zero buffer asserts nothing there — verification fails on the crypto whatever the length gate does — so trailing-byte rejection stays in `PqcSignatureEncodingTest`, where there is a genuine signature to append to.
251+
239252
Add `suite.addTestSuite(<Alg>Test.class);` to `prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/AllTests.java`.
240253

241254
### Step 16 — Release notes
@@ -296,6 +309,7 @@ When in doubt, mimic these files for the corresponding step:
296309
| 13 | `prov/.../jce/provider/BouncyCastleProvider.java` `loadPQCKeys()` snova section |
297310
| 14 | `prov/src/main/jdk1.9/module-info.java` snova entries |
298311
| 15 | `prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/SnovaTest.java` (mostly — but add a `testBcProviderKeyInfoConverter`; SNOVA's existing test doesn't cover that path, but FAEST's does) |
312+
| 15 (encoding) | `core/src/test/java/org/bouncycastle/pqc/crypto/test/PqcSignatureEncodingTest.java` `testSnova` + `checkBareSignature`, and the SNOVA block in `PqcMalformedInputTest.testMalformedSignatureReturnsFalse` |
299313
| 16 | `docs/releasenotes.html` — look at the FAEST entry under 1.85 for the prose template |
300314

301315
The FAEST entries (added in 1.85 alongside the introduction of this skill) are the most recent worked example of the full pipeline and are a useful cross-check.

‎docs/claude/architecture.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ The bridge is `BouncyCastleProvider.loadPQCKeys()` in `prov/src/main/java/org/bo
9090
Practical checklist when porting a new PQC algorithm — easy to leave any of these out and end up with a half-wired addition:
9191

9292
- `core/src/main/java/org/bouncycastle/asn1/bc/BCObjectIdentifiers.java` (or `NISTObjectIdentifiers.java` for NIST-standardised schemes) — one OID per parameter set. **Grep the existing `bc_sig.branch(...)` / `bc_kem.branch(...)` arcs before picking a number.** A collision with another algorithm's parent arc is silent at compile time and only fires when `BouncyCastlePQCProvider.<init>` runs the second `Mappings.configure`, where it throws `IllegalStateException: duplicate provider key (Alg.Alias.KeyFactory.<oid>)` — diagnosable but not until provider load.
93-
- `core/src/main/java/org/bouncycastle/pqc/crypto/<alg>/` — lightweight classes: `*Parameters`, `*PublicKeyParameters`, `*PrivateKeyParameters`, `*KeyGenerationParameters`, `*KeyPairGenerator`, `*Signer` (or KEM equivalents).
93+
- `core/src/main/java/org/bouncycastle/pqc/crypto/<alg>/` — lightweight classes: `*Parameters`, `*PublicKeyParameters`, `*PrivateKeyParameters`, `*KeyGenerationParameters`, `*KeyPairGenerator`, `*Signer` (or KEM equivalents). The `MessageSigner` contract is the **bare signature**: `generateSignature` returns exactly the parameter set's signature bytes, never the reference harness's `crypto_sign` "sm" envelope (`signature || message`, or `message || signature` for some harnesses), and `verifySignature` requires exactly that length rather than merely enough of it. The KAT files record the envelope; the test rebuilds it (see `TestUtils.SignerOperation.toVectorSignature`, or `FalconTest` inline). MAYO, SNOVA, QR-UOV, SQIsign and AIMer all shipped the envelope and had to be corrected in 1.86 (github #2403) — a sign-then-verify round-trip test does not catch it, so assert the length and that neither envelope ordering verifies (`core/src/test/java/org/bouncycastle/pqc/crypto/test/PqcSignatureEncodingTest.java`).
9494
- `core/src/main/java/org/bouncycastle/pqc/crypto/util/Utils.java` — `<alg>Oids` / `<alg>Params` maps plus `<alg>OidLookup` / `<alg>ParamsLookup` helpers.
9595
- `core/src/main/java/org/bouncycastle/pqc/crypto/util/PublicKeyFactory.java` — `<Alg>Converter` inner class + one `converters.put(oid, new <Alg>Converter())` per OID.
9696
- `core/src/main/java/org/bouncycastle/pqc/crypto/util/PrivateKeyFactory.java` — `else if (algOID.on(BCObjectIdentifiers.<alg>))` branch.
@@ -102,7 +102,7 @@ Practical checklist when porting a new PQC algorithm — easy to leave any of th
102102
- `prov/.../jcajce/provider/BouncyCastlePQCProvider.java` — add `"<Alg>"` to `ALGORITHMS`.
103103
- `prov/.../jce/provider/BouncyCastleProvider.java` — in `loadPQCKeys()`, `addKeyInfoConverter(BCObjectIdentifiers.<alg>_<param>, new <Alg>KeyFactorySpi())` for every OID. **This is the BCPQC→BC bridge; skip it and certs / PKCS#8 work fine through BCPQC but break through BC.** Test it.
104104
- `prov/src/main/jdk1.9/module-info.java` — `opens org.bouncycastle.pqc.jcajce.provider.<alg> to java.base;` plus `exports org.bouncycastle.pqc.crypto.<alg>;` plus `exports org.bouncycastle.pqc.jcajce.provider.<alg>;`. Mirror `pqc.crypto.<alg>` into `prov/src/main/ext-jdk1.9/module-info.java` (the legacy distribution does not export the JCE-side `provider.<alg>` packages).
105-
- Tests in `prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/<Alg>Test.java` plus an entry in `AllTests.java`. Include a `testBcProviderKeyInfoConverter`-style case that exercises `BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo)` and `getPrivateKey(PrivateKeyInfo)` against every parameter set, proving the `loadPQCKeys()` registration works.
105+
- Tests in `prov/src/test/java/org/bouncycastle/pqc/jcajce/provider/test/<Alg>Test.java` plus an entry in `AllTests.java`, and the signature-encoding assertions above in `core`'s `PqcSignatureEncodingTest` / `PqcMalformedInputTest`. Include a `testBcProviderKeyInfoConverter`-style case that exercises `BouncyCastleProvider.getPublicKey(SubjectPublicKeyInfo)` and `getPrivateKey(PrivateKeyInfo)` against every parameter set, proving the `loadPQCKeys()` registration works.
106106
- `docs/releasenotes.html` — one `<li>` under the current unreleased version's "Additional Features and Functionality" block.
107107

108108
## PQC engines should stay package-private — drive KATs through the public API

0 commit comments

Comments
 (0)