Harden malformed key inputs → parameter-named ArgumentException (preview.3)#8
Merged
Merged
Conversation
…ity review)
A security review found that several public paths passed caller bytes straight
into NSec, Nethermind BLS, or the platform EC import before length validation,
leaking System.FormatException, Nethermind.Crypto.Bls+BlsException, and macOS
AppleCommonCryptoCryptographicException instead of a parameter-named
ArgumentException (violating NFR-3). It also found DeriveX25519PublicKeyFromEd25519
would mint an invalid X25519 reference from a low-order Ed25519 key (all-zero
input → X25519 public key 01 00 … 00).
Fixes:
- Add RawKeyGuard.RequireLength and validate raw key/scalar lengths up front at
every backend hand-off:
* Ed25519 Sign/Verify/Restore (32)
* X25519 KeyAgreement/DeriveSharedSecret/Restore (priv + pub 32)
* NIST EC private key via ImportEcPrivateKey chokepoint
(EcScalarByteLength: P-256=32, P-384=48, P-521=66)
* BLS Sign/Restore (32) + try/catch mapping BlsException → ArgumentException
- Reject the five canonical Curve25519 low-order u-coordinates in
DeriveX25519PublicKeyFromEd25519 (throws ArgumentException(ed25519PublicKey)).
- Delete the KnownBackendDeviations allow-list in InputValidationFuzzTests — no
more pinned NFR-3 breaches; any non-contract exception now fails the suite.
- Add KeyInputValidationTests (parameter-named assertions, low-order rejection,
valid-key round-trip sanity). Tighten PRD NFR-3 AC; record lesson L5.
- Document RUSTSEC-2026-0097 remediation is gated on a zkryptium release that
bumps rand past 0.8.x (no in-tree fix); keep the CI --ignore + justification.
- Bump version to 1.0.0-preview.3.
Verified: native-present leg 780/780; adversarial sweep (8 key types × 18
lengths × 3 content patterns × all byte-oriented entry points) leaked no
non-contract exception type; 1000 real Ed25519 keys still convert; 200 P-521
keys confirm the 66-byte scalar length.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Owner
Author
Review — looks good to merge ✅Reviewed the diff and spot-checked the security-sensitive parts against the source. This is a focused, well-scoped hardening change with strong test coverage. No blocking concerns. What I verified:
Things I noticed (non-blocking):
CI: ubuntu, macOS, and the no-native (BBS-absent) legs all passed. Only Nice work — the lesson capture (L5) and the tightened PRD AC make the stricter contract durable rather than a one-off fix. Generated by Claude Code |
Extends the malformed-key hardening: a correctly-sized NIST private key whose scalar is out of range (D = 0 or D >= the curve order n) previously fell through the length guard and failed at ImportParameters with the opaque platform CryptographicException. ImportEcPrivateKey now checks 0 < D < n against the curve order up front and throws a parameter-named ArgumentException, matching the BLS and secp256k1 invalid-scalar paths. - Add P256/P384/P521 order constants (EcCurveOrder), each verified against the published FIPS 186-4 / SEC 2 decimal order. - Range-check D in ImportEcPrivateKey (the shared chokepoint for Sign/DeriveSharedSecret/FromPrivateKey). - Add KeyInputValidationTests for D=0 and D>=n across P-256/384/521. Verified: native-present leg 786/786; boundary probe confirmed D=n-1 accepted, D=n rejected, D=1 accepted (order boundary exact). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses a security review of the key-handling surface. Three findings:
System.FormatException,Nethermind.Crypto.Bls+BlsException, and macOSAppleCommonCryptoCryptographicExceptioninstead of a parameter-namedArgumentException(NFR-3 violation). The fuzz suite pinned part of this in aKnownBackendDeviationsallow-list.DeriveX25519PublicKeyFromEd25519only checked length andy = 1; an all-zero (low-order) Ed25519 key minted X25519 public key01 00 … 00.rand 0.8.5viazkryptium 0.6.1).Changes
RawKeyGuard.RequireLength— up-front length validation at every backend hand-off:Sign/Verify/Restore(32)KeyAgreement/DeriveSharedSecret/Restore(private + public 32)ImportEcPrivateKeychokepoint (EcScalarByteLength: P-256=32, P-384=48, P-521=66)Sign/Restore(32) +try/catchmappingBlsException→ArgumentException(privateKey)(covers the valid-length/invalid-scalar case too)DeriveX25519PublicKeyFromEd25519— rejects the five canonical Curve25519 low-order u-coordinates (the all-zero key maps to u=1), throwingArgumentException(ed25519PublicKey).KnownBackendDeviationsallow-list inInputValidationFuzzTests— no more pinned NFR-3 breaches; any non-contract exception now fails the suite.KeyInputValidationTests—.WithParameterName(...)assertions across every hardened path, low-order rejection, and a valid-key round-trip sanity check.zkryptiumrelease bumpingrandpast 0.8.x (no in-tree fix); recorded lesson L5.1.0.0-preview.3.Consistency follow-up
A correctly-sized but out-of-range NIST scalar (
D = 0orD ≥ n) is now also normalized:ImportEcPrivateKeyrange-checks0 < D < nagainst the verified P-256/384/521 curve orders and throwsArgumentException("privateKey"), instead of falling through to the opaque platformCryptographicException. This matches the BLS/secp256k1 invalid-scalar paths (curve orders verified against published FIPS 186-4 / SEC 2 values; boundary confirmed: D=n−1 accepted, D=n rejected).Verification
dotnet build NetCrypto.sln -c Releaseclean; native-present leg (--filter "Category!=BbsAbsent") → 786/786 passed.FormatException/BlsException/IndexOutOfRange/NullReference/Overflowleaks. Algebraically-constructed low-order-target inputs were all rejected (0 bypasses); 1000 real Ed25519 keys still convert; 200 P-521 keys confirm the 66-byte scalar length (no false rejection).Note
Based on
mainafter PR #3 merged (preview.2 → preview.3, linear). Thetasks/todo20260613-105639.mdaudit-notes file is the security review's own record (with a resolution section appended); it was already untracked in the repo.🤖 Generated with Claude Code