Skip to content

Add checked VerifyingKey constructors - #14

Open
zshenker wants to merge 1 commit into
ps256-featurefrom
checked-verifying-keys
Open

zshenker wants to merge 1 commit into
ps256-featurefrom
checked-verifying-keys

Conversation

@zshenker

Copy link
Copy Markdown
Collaborator

Stacked on #9 (base: ps256-feature, itself on #6). It needs VerifyingKey from #6 and the ps256 feature from #9.

Problem

VerifyingKey's variants accept any bytes. A malformed or undersized key only surfaces as a failure on the first token it's used for. Callers who want to validate configured keys have to verify a dummy signature.

Change

New constructors validate key material and return Error::InvalidKey:

  • VerifyingKey::es256_spki(der) parses the SPKI DER P-256 public key.
  • VerifyingKey::ps256_spki(der) parses the SPKI DER RSA public key and enforces the existing 2048-bit minimum. It requires the ps256 feature.
  • VerifyingKey::hmac_sha256(secret) requires at least 32 bytes. RFC 7518 §3.2 requires an HMAC key at least as long as the hash output.

The enum variants remain available for unchecked construction.

Tests

  • The tests cover valid keys, a truncated SPKI, a PKCS#8 private key passed as SPKI, garbage input, a 1024-bit RSA key, an EC key passed as RSA, and HMAC keys of 31 and 32 bytes.
  • cargo test passes with default features (63 unit tests, 74 doctests) and with --no-default-features (56 unit tests, 74 doctests). Clippy is clean in both configurations.

🤖 Generated with Claude Code

Callers configuring verification keys had no way to validate them
except by verifying a dummy signature. Add constructors that check key
material when the key is configured:

- VerifyingKey::es256_spki parses the SPKI DER P-256 key.
- VerifyingKey::ps256_spki (ps256 feature) parses the SPKI DER RSA key
  and enforces the existing 2048-bit minimum.
- VerifyingKey::hmac_sha256 requires at least 32 bytes, per RFC 7518
  §3.2 (key at least as long as the hash output).

All return Error::InvalidKey. The enum variants remain available for
unchecked construction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant