jwt-pq 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0c45ba386fb98df768124e32529f0e0d80f574ddf0a0132e57e87a3ce94ea7a
4
- data.tar.gz: c6071071b4814e40c707179d575447b7d730a1a2a789ba1cee5bdf71929abb47
3
+ metadata.gz: 2d93fee922c0b9bc8284d640c036026637b9a7b8a4fe9ba86bc87111e471064a
4
+ data.tar.gz: ea9de1cf2d087851d6ac1130ea9b5edb648b5ff00f0a0e17b83e072610070b93
5
5
  SHA512:
6
- metadata.gz: 96c10bfd04d93b946f634257b95427a64e39cf23d8a10f330b1b4bdc3d29e212d0a575ddb7486df7ab47d88bd8af73d21ac4ec2620507db9c5289e2bc5a2588b
7
- data.tar.gz: 88bc0b05ade5ab27f3dfa8b56d7f8bf3d8bb566145468e452c7a570db5db0e57ddabcc9ad452d86a48206633368675b83cf7a28d832f54a157cc80e75559464a
6
+ metadata.gz: bf5c2afe8fc73d311cbfd76d0ee1863d750908241ee82b84abf86ca22d631ea9ddcd48cc7ead5b1c8b0fcb10b18d4e206998206b0588c41cec3301bc8957ee15
7
+ data.tar.gz: b4b7881a4900ce4ce33b8f7d1ba078fc124a49f534fee53a0135c1cd4d87df5ad00d7f5d87d6abe4398c003e31a0c9ba2d4d4bfb4806b4dfe58009ad722d8e3e
data/.yardopts CHANGED
@@ -1,6 +1,6 @@
1
1
  --markup markdown
2
2
  --readme README.md
3
- --files CHANGELOG.md,SECURITY.md
3
+ --files CHANGELOG.md,SECURITY.md,MIGRATING.md
4
4
  --output-dir doc
5
5
  --no-private
6
6
  --protected
data/CHANGELOG.md CHANGED
@@ -7,6 +7,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.6.2] - 2026-12-03
11
+
12
+ ### Added
13
+
14
+ - `MIGRATING.md`: a step-by-step guide for moving from RS256/ES256/EdDSA to ML-DSA without a flag day — parameter-set choice, token-size pitfalls (cookies, header limits), a mixed classical + ML-DSA JWKS, side-by-side verification with the algorithm pinned per key, and retiring the classical key (#81)
15
+
16
+ ### Changed
17
+
18
+ - Documentation now tracks [RFC 9964](https://datatracker.ietf.org/doc/rfc9964/) (published May 2026, formerly `draft-ietf-cose-dilithium`) and [RFC 9881](https://datatracker.ietf.org/doc/rfc9881/). `SPEC.md` records two known divergences, both limited to private-key serialization: JWK `priv` carries the expanded private key where RFC 9964 requires the 32-byte seed, and PKCS#8 private PEMs carry the expanded key without the inner DER encoding RFC 9881 defines. Signatures, public keys, public JWKs, and thumbprints conform. This documentation update itself changes no behaviour (#80)
19
+ - README and `SPEC.md` now state that the hybrid `EdDSA+ML-DSA-*` mode is specific to jwt-pq and experimental, and the README no longer claims interop coverage beyond what the workflow tests (ML-DSA-65); the IETF hybrid format (`draft-ietf-jose-pq-composite-sigs`) uses different `alg` names and a different construction (#80)
20
+
21
+ ### Security
22
+
23
+ - **Documentation fix — action required if you copied the JWKS consumer example.** The README and YARD examples for resolving a verification key by `kid` passed `algorithms: [header["alg"]]` to `JWT.decode`, taking the allowed algorithm from the unverified token header. With that pattern a forged token carrying `"alg": "none"`, a known `kid`, and no signature decodes successfully. The examples now pin the algorithm to the resolved key (`algorithms: [key.algorithm]`; for a `HybridKey`, use `key.hybrid_algorithm`). The library code itself was not affected: decoding with a pinned algorithm list always rejected such tokens. If your code uses the old form, change it to `key.algorithm` or an explicit allow-list (#79)
24
+ - The `ML-DSA-*` and `EdDSA+ML-DSA-*` algorithms now require the token's `alg` to match the key's parameter set. Previously an ML-DSA-65 key would sign a token labelled `ML-DSA-44` (and verify one), so a token could claim a parameter set it did not use. Signing with a mismatched key now raises `JWT::EncodeError`; on verification a mismatched key simply does not verify, so `JWT.decode` raises `JWT::VerificationError` unless another supplied key matches. Verifying against several keys or several allowed algorithms keeps working: each key is only tried under its own parameter set (#79)
25
+
26
+ ### Dependencies
27
+
28
+ - Dev dependency `jwt-eddsa` requirement updated from `~> 0.9` to `~> 1.0` (#64)
29
+ - CI runners pinned to `ubuntu-24.04` (#76)
30
+ - Bump `ruby/setup-ruby` from 1.316.0 to 1.346.0 (#62, #78)
31
+ - Bump `rubygems/release-gem` from 1.4.0 to 1.5.0 (#63, #75, #77)
32
+ - Bump `actions/setup-python` from 6.3.0 to 7.1.0 (#61, #70)
33
+ - Bump `codecov/codecov-action` from 7.0.0 to 7.1.1 (#66)
34
+ - Bump `actions/checkout` from 7.0.0 to 7.0.1 (#60)
35
+ - Bump `actions/deploy-pages` from 5.0.0 to 5.0.1 (#65)
36
+
37
+ ## [0.6.1] - 2026-07-13
38
+
39
+ ### Security
40
+
41
+ - Bundled liboqs updated from 0.15.0 to [0.16.0](https://github.com/open-quantum-safe/liboqs/releases/tag/0.16.0), which patches five upstream security issues, including [CVE-2026-44518](https://github.com/open-quantum-safe/liboqs/security/advisories/GHSA-wf7v-fhxj-73m2) and [GHSA-2wxh-55qf-c7wg](https://github.com/open-quantum-safe/liboqs/security/advisories/GHSA-2wxh-55qf-c7wg) (moderate, out-of-bounds reads in XMSS/XMSS^MT signature verification). None of the patched code paths (XMSS/XMSS^MT, CROSS, FrodoKEM) are compiled into jwt-pq's ML-DSA-only build, so jwt-pq itself was not exposed; the bump keeps the bundled library on a supported upstream release (#57)
42
+
43
+ ### Dependencies
44
+
45
+ - Bump `codecov/codecov-action` from 6.0.1 to 7.0.0 (#49)
46
+ - Bump `actions/checkout` from 6.0.3 to 7.0.0 (#52)
47
+
10
48
  ## [0.6.0] - 2026-04-22
11
49
 
12
50
  ### Added
@@ -151,7 +189,9 @@ Throughput on Ruby 3.4.6, macOS x86_64, liboqs 0.15.0 (benchmark-ips, 2s warmup
151
189
  - Optional dependency on jwt-eddsa / ed25519
152
190
  - Error classes: `LiboqsError`, `KeyError`, `SignatureError`, `MissingDependencyError`
153
191
 
154
- [Unreleased]: https://github.com/marcelopazzo/jwt-pq/compare/v0.6.0...HEAD
192
+ [Unreleased]: https://github.com/marcelopazzo/jwt-pq/compare/v0.6.2...HEAD
193
+ [0.6.2]: https://github.com/marcelopazzo/jwt-pq/compare/v0.6.1...v0.6.2
194
+ [0.6.1]: https://github.com/marcelopazzo/jwt-pq/compare/v0.6.0...v0.6.1
155
195
  [0.6.0]: https://github.com/marcelopazzo/jwt-pq/compare/v0.5.1...v0.6.0
156
196
  [0.5.1]: https://github.com/marcelopazzo/jwt-pq/compare/v0.5.0...v0.5.1
157
197
  [0.5.0]: https://github.com/marcelopazzo/jwt-pq/compare/v0.4.0...v0.5.0
data/Gemfile CHANGED
@@ -14,7 +14,7 @@ group :development, :test do
14
14
  end
15
15
 
16
16
  group :test do
17
- gem "jwt-eddsa", "~> 0.9"
17
+ gem "jwt-eddsa", "~> 1.0"
18
18
  gem "simplecov", require: false
19
19
  gem "simplecov-cobertura", require: false
20
20
  end
data/MIGRATING.md ADDED
@@ -0,0 +1,196 @@
1
+ # Migrating to post-quantum JWT signatures
2
+
3
+ This guide is for applications that sign JWTs with RS256, ES256, EdDSA or
4
+ another classical algorithm today and want to move to ML-DSA with jwt-pq
5
+ without a flag day.
6
+
7
+ ## What this does and does not protect
8
+
9
+ jwt-pq replaces the **signature** on a JWT. A token signed with ML-DSA
10
+ cannot be forged by an attacker who can break RSA or elliptic-curve
11
+ signatures.
12
+
13
+ It does not change anything else:
14
+
15
+ - **Transport.** Tokens still travel over TLS. Moving TLS to post-quantum
16
+ key exchange is a separate change, made in your web server or load
17
+ balancer.
18
+ - **Encryption.** jwt-pq signs; it does not encrypt. Encrypted tokens
19
+ (JWE) are out of scope.
20
+ - **Stored secrets.** Keys, passwords and data at rest are unaffected.
21
+
22
+ Signatures are also less exposed than encryption to "record now, break
23
+ later": a forged token is only useful while its signing key is still
24
+ trusted. That makes the order of work simple — move long-lived keys and
25
+ long-lived tokens first.
26
+
27
+ ## Before you start
28
+
29
+ **Pick a parameter set.**
30
+
31
+ | If you… | Use |
32
+ |---|---|
33
+ | have no specific requirement | `ML-DSA-65` |
34
+ | must follow NSA CNSA 2.0 | `ML-DSA-87` |
35
+ | are tightly constrained on token size | `ML-DSA-44` |
36
+
37
+ **Use plain `ML-DSA-*`, not the hybrid mode, unless you control every
38
+ verifier.** `ML-DSA-*` is standardized in
39
+ [RFC 9964](https://datatracker.ietf.org/doc/rfc9964/) and verifies with
40
+ any conforming library. The hybrid `EdDSA+ML-DSA-*` mode is specific to
41
+ jwt-pq and will change to follow the IETF's hybrid format once that is
42
+ published (see [SPEC.md](https://github.com/marcelopazzo/jwt-pq/blob/main/SPEC.md)).
43
+
44
+ **Check that your tokens still fit.** ML-DSA signatures are large. The
45
+ same four-claim payload produces:
46
+
47
+ | Algorithm | Token size |
48
+ |---|---|
49
+ | RS256 | ~0.5 KB |
50
+ | ML-DSA-44 | ~3.4 KB |
51
+ | ML-DSA-65 | ~4.6 KB |
52
+ | ML-DSA-87 | ~6.4 KB |
53
+
54
+ Things that commonly break:
55
+
56
+ - **Cookies.** Browsers cap a cookie at about 4 KB, counting its name
57
+ and attributes. ML-DSA-65 and ML-DSA-87 tokens do not fit, and an
58
+ ML-DSA-44 token fits only with a very small payload. For all three,
59
+ keep the token server-side and put a session identifier in the cookie
60
+ instead.
61
+ - **Header limits.** Proxies and application servers cap request header
62
+ size, often at 8 KB per header line. A token in `Authorization` plus
63
+ your other headers has to fit; check every hop.
64
+ - **Logs, URLs and caches** that assumed tokens of a few hundred bytes.
65
+
66
+ Signing and verification are also slower than Ed25519 (see the
67
+ [performance table](https://github.com/marcelopazzo/jwt-pq/blob/main/README.md#performance)), though still thousands of
68
+ operations per second per core.
69
+
70
+ ## The rollout
71
+
72
+ The rule that keeps this safe: **verifiers learn the new key before any
73
+ issuer uses it, and forget the old key only after its last token has
74
+ expired.**
75
+
76
+ ### 1. Generate and store the ML-DSA key
77
+
78
+ ```ruby
79
+ require "jwt/pq"
80
+
81
+ key = JWT::PQ::Key.generate(:ml_dsa_65)
82
+ private_pem = key.private_to_pem # store this in your secret manager
83
+ ```
84
+
85
+ Load it at boot with `JWT::PQ::Key.from_pem(private_pem)`. The private PEM
86
+ is in a jwt-pq-specific encoding for now, so treat it as readable by
87
+ jwt-pq only (see [SPEC.md](https://github.com/marcelopazzo/jwt-pq/blob/main/SPEC.md), "Known divergences").
88
+
89
+ ### 2. Publish the public key next to your current one
90
+
91
+ One JWKS document can carry both. jwt-pq's `kid` is the key's RFC 7638
92
+ thumbprint.
93
+
94
+ ```ruby
95
+ classical = JWT::JWK.new(rsa_public_key, kid: "rsa-2024")
96
+ pq_keys = JWT::PQ::JWKSet.new([key])
97
+
98
+ jwks_document = { keys: [classical.export] + pq_keys.export[:keys] }
99
+ # serve JSON.generate(jwks_document) at /.well-known/jwks.json
100
+ ```
101
+
102
+ `JWKSet#export` emits public keys only by default. Older verifiers ignore
103
+ the `AKP` entry they do not understand; check that yours do before going
104
+ further.
105
+
106
+ ### 3. Teach every verifier to accept both
107
+
108
+ Deploy this everywhere tokens are verified **before** changing the
109
+ issuer. Each key is verified only under its own algorithm, and the
110
+ locally configured classical keys are checked first, so existing tokens
111
+ never depend on fetching the JWKS.
112
+
113
+ Before deploying, look at the tokens you issue today. Keep their `kid`
114
+ values exactly as they are in the `CLASSICAL` table. If they carry no
115
+ `kid` at all, they must still verify after this change: map them
116
+ explicitly to one key and one algorithm, as `CLASSICAL_WITHOUT_KID` does
117
+ below, and remove that mapping once those tokens have expired.
118
+
119
+ ```ruby
120
+ # Your current keys, under the exact `kid` values your issuer already emits.
121
+ CLASSICAL = { "rsa-2024" => { key: rsa_public_key, algorithm: "RS256" } }.freeze
122
+
123
+ # Only if your issuer signs tokens without a `kid` today; otherwise nil.
124
+ CLASSICAL_WITHOUT_KID = { key: rsa_public_key, algorithm: "RS256" }.freeze
125
+
126
+ def pq_keys
127
+ JWT::PQ::JWKSet.fetch("https://issuer.example/.well-known/jwks.json")
128
+ end
129
+
130
+ def verify(token)
131
+ _payload, header = JWT.decode(token, nil, false) # unverified: only `kid` is read
132
+ kid = header["kid"]
133
+
134
+ classical = kid ? CLASSICAL[kid] : CLASSICAL_WITHOUT_KID
135
+
136
+ if classical
137
+ JWT.decode(token, classical[:key], true, algorithms: [classical[:algorithm]])
138
+ elsif kid && (pq_key = pq_keys[kid])
139
+ JWT.decode(token, pq_key, true, algorithms: [pq_key.algorithm])
140
+ else
141
+ raise JWT::DecodeError, "unknown kid"
142
+ end
143
+ rescue JWT::PQ::Error => e
144
+ # JWKS unreachable or malformed: fail as a verification error, not a 500.
145
+ raise JWT::DecodeError, "could not load verification keys: #{e.message}"
146
+ end
147
+ ```
148
+
149
+ > [!WARNING]
150
+ > Never pass the token's own `alg` to `algorithms:`. The header is
151
+ > attacker-controlled until the signature is verified; a token that claims
152
+ > `"alg": "none"` would be accepted. Take the algorithm from the key you
153
+ > resolved, as above.
154
+
155
+ `JWKSet.fetch` caches for five minutes by default and skips the classical
156
+ entries in a mixed JWKS. Call it per request, as above, rather than once
157
+ at boot: it returns the cached set, and a rotated key is picked up when
158
+ the cache expires. It does not serve a stale set when a refresh fails, so
159
+ an unreachable JWKS endpoint means ML-DSA tokens are rejected until it
160
+ recovers; raise `cache_ttl:` if that trade-off is wrong for you.
161
+
162
+ ### 4. Switch the issuer
163
+
164
+ ```ruby
165
+ token = JWT.encode(payload, key, key.algorithm, { kid: key.jwk_thumbprint })
166
+ ```
167
+
168
+ Pass `key.algorithm` so the `alg` always matches the key's parameter set,
169
+ and always set `kid`; it is how verifiers find the key. If you can, switch a
170
+ small share of traffic first and watch for verification failures and
171
+ oversized-header errors (HTTP 400, 413 or 431).
172
+
173
+ ### 5. Retire the classical key
174
+
175
+ Wait at least as long as your longest-lived classical token, including
176
+ refresh tokens. Then remove the classical branch from your verifiers and
177
+ the classical key from the JWKS. Until you do this, the classical
178
+ algorithm is still a way in: a forged RS256 token is accepted for as long
179
+ as verifiers trust the RSA key.
180
+
181
+ ## Rolling back
182
+
183
+ Until step 5 everything is reversible: point the issuer back at the
184
+ classical key and verifiers keep working, because they still accept both.
185
+ After step 5, rolling back means re-publishing the classical key and
186
+ redeploying verifiers first.
187
+
188
+ ## Checklist
189
+
190
+ - [ ] Parameter set chosen; tokens fit in every cookie, header limit and
191
+ store they pass through
192
+ - [ ] ML-DSA private key generated and stored as a secret
193
+ - [ ] Public key published in the JWKS alongside the classical key
194
+ - [ ] Every verifier accepts both, with `algorithms:` pinned per key
195
+ - [ ] Issuer switched, with `kid` set on every token
196
+ - [ ] Classical key removed after the longest token lifetime has passed
data/README.md CHANGED
@@ -15,6 +15,13 @@ Post-quantum JWT signatures for Ruby. Adds **ML-DSA** (FIPS 204) support to the
15
15
  - PEM serialization (SPKI / PKCS#8) via [pqc_asn1](https://github.com/msuliq/pqc_asn1)
16
16
  - JWK export/import with RFC 7638 thumbprints
17
17
 
18
+ ## Migrating from RS256, ES256 or EdDSA
19
+
20
+ See [MIGRATING.md](MIGRATING.md) for a step-by-step rollout that keeps
21
+ existing tokens working: choosing a parameter set, checking token size
22
+ against cookie and header limits, publishing a mixed JWKS, and verifying
23
+ classical and ML-DSA tokens side by side until the old key is retired.
24
+
18
25
  ## Requirements
19
26
 
20
27
  - Ruby >= 3.2
@@ -92,13 +99,18 @@ The hybrid signature is a concatenation of `Ed25519 (64 bytes) || ML-DSA`, store
92
99
  ```ruby
93
100
  # Export
94
101
  pub_pem = key.to_pem # SPKI format
95
- priv_pem = key.private_to_pem # PKCS#8 format
102
+ priv_pem = key.private_to_pem # PKCS#8 container, jwt-pq-specific contents (see below)
96
103
 
97
104
  # Import
98
105
  pub_key = JWT::PQ::Key.from_pem(pub_pem)
99
106
  full_key = JWT::PQ::Key.from_pem_pair(public_pem: pub_pem, private_pem: priv_pem)
100
107
  ```
101
108
 
109
+ Public PEMs are standard SPKI. Private PEMs, and the `priv` member of a
110
+ private JWK, are not yet in the RFC 9881 / RFC 9964 encodings: use them
111
+ only to move keys between jwt-pq processes, not with OpenSSL or other
112
+ libraries. See [SPEC.md](SPEC.md#known-divergences-from-the-specs).
113
+
102
114
  ### JWK
103
115
 
104
116
  ```ruby
@@ -110,6 +122,7 @@ jwk.export
110
122
 
111
123
  jwk.export(include_private: true)
112
124
  # => { kty: "AKP", alg: "ML-DSA-65", pub: "...", priv: "...", kid: "..." }
125
+ # `priv` is jwt-pq-specific for now — see the note under "PEM serialization"
113
126
 
114
127
  # Import
115
128
  restored = JWT::PQ::JWK.import(jwk_hash)
@@ -129,9 +142,20 @@ File.write("jwks.json", jwks.to_json)
129
142
  jwks = JWT::PQ::JWKSet.import(JSON.parse(fetch_jwks))
130
143
  _payload, header = JWT.decode(token, nil, false) # unverified peek
131
144
  key = jwks[header["kid"]] or raise "unknown kid"
132
- payload, = JWT.decode(token, key, true, algorithms: [header["alg"]])
145
+ payload, = JWT.decode(token, key, true, algorithms: [key.algorithm])
133
146
  ```
134
147
 
148
+ > [!WARNING]
149
+ > Always take `algorithms:` from the key you resolved (`key.algorithm`) or
150
+ > from your own configuration — never from the token's header. The header
151
+ > is attacker-controlled until the signature is verified: passing
152
+ > `algorithms: [header["alg"]]` lets a forged token with `"alg": "none"`
153
+ > and no signature decode successfully. Versions of this README up to
154
+ > 0.6.1 showed that unsafe form; if you copied it, change it.
155
+ >
156
+ > For a `JWT::PQ::HybridKey`, the JWT algorithm is `key.hybrid_algorithm`
157
+ > (`"EdDSA+ML-DSA-65"`); `key.algorithm` returns only the ML-DSA half.
158
+
135
159
  Members are indexed by their RFC 7638 thumbprint (the same value
136
160
  `JWK#export` emits as `kid`). Remember to set the `kid` header when
137
161
  signing: `JWT.encode(payload, key, alg, { kid: key.jwk_thumbprint })`.
@@ -172,7 +196,7 @@ jwks = JWT::PQ::JWKSet.fetch("https://issuer.example/.well-known/jwks.json")
172
196
 
173
197
  _payload, header = JWT.decode(token, nil, false)
174
198
  key = jwks[header["kid"]] or raise "unknown kid"
175
- payload, = JWT.decode(token, key, true, algorithms: [header["alg"]])
199
+ payload, = JWT.decode(token, key, true, algorithms: [key.algorithm])
176
200
  ```
177
201
 
178
202
  The cache is process-global and keyed by URL, so repeated calls inside
@@ -209,12 +233,12 @@ at 1 MB, 5 s timeouts. Network/HTTP failures raise
209
233
 
210
234
  The hybrid algorithms (`EdDSA+ML-DSA-{44,65,87}`) provide defense-in-depth: if either algorithm is broken, the other still protects the token.
211
235
 
212
- The `alg` header values follow a `ClassicAlg+PQAlg` convention. The IETF draft [`draft-ietf-cose-dilithium`](https://datatracker.ietf.org/doc/draft-ietf-cose-dilithium/) is still evolving — these values may change in future versions to align with the final standard.
236
+ The `alg` header values follow a `ClassicAlg+PQAlg` convention that is specific to jwt-pq: hybrid tokens verify only with this gem. The IETF's hybrid format is still a draft ([`draft-ietf-jose-pq-composite-sigs`](https://datatracker.ietf.org/doc/draft-ietf-jose-pq-composite-sigs/)) and uses different `alg` names and a different construction, so treat hybrid mode as experimental — it will change to follow that draft. If you need interoperability today, use plain `ML-DSA-*`.
213
237
 
214
238
  ## Correctness
215
239
 
216
240
  - **NIST ACVP known-answer tests.** `spec/jwt/pq/kat_spec.rb` runs the full sigVer KAT subset shipped with liboqs against `JWT::PQ::Key#verify` for ML-DSA-44/65/87, covering both positive and negative cases. These vectors are executed in CI on every push. (sigGen KATs are not used because FIPS 204 specifies hedged signing with internal randomness, which makes signature output non-deterministic.)
217
- - **Cross-language interop.** The [`Cross-interop`](https://github.com/marcelopazzo/jwt-pq/actions/workflows/interop.yml) workflow signs with `jwt-pq` and verifies with [`dilithium-py`](https://pypi.org/project/dilithium-py/), and vice versa, for all three parameter sets. It runs on every push and weekly on `cron`.
241
+ - **Cross-language interop.** The [`Cross-interop`](https://github.com/marcelopazzo/jwt-pq/actions/workflows/interop.yml) workflow signs with `jwt-pq` and verifies with [`dilithium-py`](https://pypi.org/project/dilithium-py/), and vice versa, for ML-DSA-65 raw message signatures, and checks that an exported JWK's `pub` decodes to a working public key. It runs on every push and weekly on `cron`.
218
242
 
219
243
  ## Performance
220
244
 
@@ -235,13 +259,15 @@ ML-DSA operations are delegated to [liboqs](https://github.com/open-quantum-safe
235
259
 
236
260
  ## Specification tracking
237
261
 
238
- jwt-pq targets the current IETF specs for JOSE/COSE post-quantum signatures:
262
+ jwt-pq targets the published IETF and NIST specs for JOSE/COSE post-quantum signatures:
239
263
 
240
- - [`draft-ietf-cose-dilithium`](https://datatracker.ietf.org/doc/draft-ietf-cose-dilithium/) — ML-DSA in JOSE/COSE, including the `AKP` key type *(draft)*
264
+ - [RFC 9964](https://datatracker.ietf.org/doc/rfc9964/) — ML-DSA in JOSE/COSE, including the `AKP` key type *(published May 2026)*
241
265
  - [RFC 9864](https://datatracker.ietf.org/doc/rfc9864/) — Fully-Specified Algorithms for JOSE and COSE *(published October 2025)*
242
266
  - [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final) — ML-DSA itself *(final)*
243
267
 
244
- See [SPEC.md](SPEC.md) for the full tracked-specs table, the current-draft revisions the shipped `alg`/`kty` values target, the hybrid-mode convention, known divergences, and the compatibility policy for draft transitions and the eventual RFC finalization.
268
+ Signatures, public keys, public JWKs, and thumbprints conform to RFC 9964. Private-key serialization (JWK `priv` and PKCS#8 PEM) does not yet. A private JWK's `priv` holds the expanded private key, where RFC 9964 requires the 32-byte seed. A private PEM holds the expanded key too, which RFC 9881 permits, but without the inner DER encoding the RFC defines. Private keys exported by jwt-pq should therefore only be re-imported by jwt-pq.
269
+
270
+ See [SPEC.md](SPEC.md) for the full tracked-specs table, the known divergences, the hybrid-mode convention, and the compatibility policy.
245
271
 
246
272
  ## Thread safety
247
273
 
data/SECURITY.md CHANGED
@@ -38,7 +38,7 @@ Out of scope (report upstream):
38
38
 
39
39
  ## Upstream liboqs advisories
40
40
 
41
- jwt-pq bundles a pinned version of liboqs (see `LIBOQS_VERSION` in `ext/jwt/pq/extconf.rb`, currently **0.15.0**, integrity-checked against a SHA-256 of the source tarball).
41
+ jwt-pq bundles a pinned version of liboqs (see `LIBOQS_VERSION` in `ext/jwt/pq/extconf.rb`, currently **0.16.0**, integrity-checked against a SHA-256 of the source tarball).
42
42
 
43
43
  When [liboqs](https://github.com/open-quantum-safe/liboqs/security/advisories) publishes a security advisory affecting an algorithm we ship:
44
44
 
data/SPEC.md CHANGED
@@ -2,39 +2,108 @@
2
2
 
3
3
  This document records which external specifications each jwt-pq release
4
4
  targets, what its current divergences are (if any), and the compatibility
5
- policy for drafts in flight.
5
+ policy for specs that are still in flight.
6
6
 
7
7
  ## Tracked specifications — jwt-pq 0.6.x
8
8
 
9
- Last reviewed: **2026-04-22**.
9
+ Last reviewed: **2026-12-03**.
10
10
 
11
11
  | Spec | Role in jwt-pq | Status |
12
12
  |-----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|-----------------------------|
13
13
  | [FIPS 204](https://csrc.nist.gov/pubs/fips/204/final) | ML-DSA itself — key generation, signing, verification, key sizes | Final (August 2024) |
14
14
  | [RFC 9864](https://datatracker.ietf.org/doc/rfc9864/) | Fully-Specified Algorithms for JOSE and COSE — underlies the `AKP` key type concept | Final (October 2025) |
15
15
  | [RFC 7515](https://www.rfc-editor.org/rfc/rfc7515) / [RFC 7517](https://www.rfc-editor.org/rfc/rfc7517) / [RFC 7518](https://www.rfc-editor.org/rfc/rfc7518) / [RFC 7638](https://www.rfc-editor.org/rfc/rfc7638) | JWS wire format, JWK structure, `alg` registration, JWK thumbprints | Final |
16
- | [`draft-ietf-cose-dilithium`](https://datatracker.ietf.org/doc/draft-ietf-cose-dilithium/) | `AKP` (`"Algorithm Key Pair"`) JWK key type and ML-DSA JWS `alg` names (`ML-DSA-44/65/87`) | Internet-Draft, pre-RFC |
16
+ | [RFC 9964](https://datatracker.ietf.org/doc/rfc9964/) | ML-DSA for JOSE and COSE — the `AKP` (`"Algorithm Key Pair"`) JWK key type, the JWS `alg` names (`ML-DSA-44/65/87`), and the JWK Thumbprint members | Final (May 2026); was `draft-ietf-cose-dilithium` |
17
+ | [RFC 9881](https://datatracker.ietf.org/doc/rfc9881/) | ML-DSA algorithm identifiers for X.509 — the OIDs and the SPKI / PKCS#8 encodings behind `to_pem` / `from_pem` | Final (October 2025) |
17
18
 
18
19
  The hybrid mode (`EdDSA+ML-DSA-*`) is jwt-pq's own convention —
19
20
  concatenated `Ed25519 || ML-DSA` signatures with a `pq_alg` header for
20
- cross-impl disambiguation. There is no IETF draft that specifies this
21
- exact shape for EdDSA+ML-DSA; it is implemented here as an interop-safe
22
- stepping stone until the JOSE WG publishes one.
21
+ cross-impl disambiguation. It is **not** an IETF format. The JOSE WG's
22
+ work on hybrids is
23
+ [`draft-ietf-jose-pq-composite-sigs`](https://datatracker.ietf.org/doc/draft-ietf-jose-pq-composite-sigs/)
24
+ (an active Internet-Draft, revision 07 at the time of this review), which
25
+ defines different `alg` names (for example `ML-DSA-44-Ed25519`) and a
26
+ different signature construction. Tokens signed with `EdDSA+ML-DSA-*`
27
+ verify only with jwt-pq; treat hybrid mode as experimental and expect it
28
+ to change when that draft is published.
29
+
30
+ ## What matches the specs
31
+
32
+ Everything a verifier touches is implemented to RFC 9964:
33
+
34
+ - `alg` values `ML-DSA-44`, `ML-DSA-65`, `ML-DSA-87`
35
+ - Pure ML-DSA with an empty context string (no HashML-DSA)
36
+ - Public JWKs: `kty: "AKP"`, `alg`, and `pub` as the raw FIPS 204 public key
37
+ - JWK Thumbprints over `alg`, `kty`, `pub`
38
+ - SPKI public-key PEMs (raw FIPS 204 public key in the BIT STRING)
39
+
40
+ How this is checked: verification runs against the NIST ACVP known-answer
41
+ vectors for all three parameter sets. The cross-language interop workflow
42
+ checks ML-DSA-65 only, against `dilithium-py`: raw message signatures in
43
+ both directions, and decoding the `pub` member of an exported JWK. JWS
44
+ tokens, ML-DSA-44/87, thumbprints, and SPKI PEMs are not yet checked
45
+ against a second implementation.
23
46
 
24
47
  ## Known divergences from the specs
25
48
 
26
- None at the time of writing. jwt-pq implements the ML-DSA JOSE convention
27
- as currently written in the draft. Any divergence introduced in the
28
- future (e.g. to work around an ambiguous draft point) will be listed
29
- here with a link to the commit that introduced it.
49
+ Both divergences concern **private-key serialization only**. Signatures,
50
+ public keys, and thumbprints are unaffected, so tokens signed by jwt-pq
51
+ verify with any conforming implementation and vice versa.
52
+
53
+ 1. **JWK `priv` is the expanded private key, not the seed.** RFC 9964
54
+ requires `priv` to be the 32-byte ML-DSA seed. `JWK#export(include_private: true)`
55
+ emits the expanded FIPS 204 private key instead (2,560 / 4,032 / 4,896
56
+ bytes for ML-DSA-44 / 65 / 87), and `JWK.import` accepts only that
57
+ form. A private JWK exported by jwt-pq is therefore not importable by a
58
+ conforming implementation, and a conforming seed-form private JWK is
59
+ rejected by jwt-pq with a key-size `KeyError`. Public JWKs — the ones
60
+ published on `/.well-known/jwks.json` — are not affected.
61
+
62
+ 2. **PKCS#8 private-key PEMs carry the raw expanded key.** RFC 9881
63
+ defines the PKCS#8 `privateKey` content as a DER-encoded `CHOICE` of
64
+ seed, expanded key (a nested `OCTET STRING`), or both, and recommends
65
+ the seed. `Key#private_to_pem` writes the expanded key bytes directly
66
+ into the `privateKey` OCTET STRING, without the inner `CHOICE`
67
+ encoding, and `Key.from_pem` / `Key.from_pem_pair` expect the same.
68
+ The embedded public key (`[1]`) is likewise written as the raw key
69
+ bytes rather than as BIT STRING content. These PEMs round-trip within
70
+ jwt-pq but should not be expected to load in other tooling. In the
71
+ other direction, an RFC 9881-conforming private PEM — seed form or
72
+ `CHOICE`-wrapped expanded key, as OpenSSL 3.5 writes — is rejected
73
+ with a `KeyError`: an invalid private key size from `from_pem_pair`,
74
+ or a missing public key from `from_pem`.
75
+
76
+ Until these are resolved, use the JWK `priv` and private PEM forms only
77
+ to move keys between jwt-pq processes. The two gaps have different
78
+ fixes:
79
+
80
+ - **PKCS#8, expanded-key form.** RFC 9881 permits the expanded key, so
81
+ writing and reading it with the correct inner DER encoding is a change
82
+ to the serialization layer only. It does not depend on the ML-DSA
83
+ backend.
84
+ - **Seed forms.** Emitting a seed as JWK `priv`, and importing a seed
85
+ from a JWK or a seed-form PEM, requires deriving the key pair from the
86
+ seed. That needs seed-based key generation in the ML-DSA backend (the
87
+ alternative backend in
88
+ [#14](https://github.com/marcelopazzo/jwt-pq/issues/14) is one route
89
+ to it).
90
+
91
+ Both are planned for future minor releases and will follow the
92
+ compatibility policy below: the import side will keep accepting the
93
+ current form for at least one minor version.
94
+
95
+ Any further divergence will be listed here with a link to the commit
96
+ that introduced it.
30
97
 
31
98
  ## Compatibility policy
32
99
 
33
- Because `draft-ietf-cose-dilithium` is an evolving Internet-Draft and
34
- not yet an RFC, the `alg`, `kty`, and `pub`/`priv` field semantics can
35
- change between draft revisions. Our policy for adapting to them:
100
+ The ML-DSA wire format is now fixed by RFC 9964, so the `alg`, `kty`,
101
+ and `pub` values jwt-pq emits are stable. Changes are still expected in
102
+ two places: closing the private-key divergences above, and the hybrid
103
+ mode, which will follow `draft-ietf-jose-pq-composite-sigs` once it
104
+ settles. Our policy for those changes:
36
105
 
37
- 1. **Pre-1.0 (current).** A breaking change in the draft that requires
106
+ 1. **Pre-1.0 (current).** A breaking change in a tracked draft that requires
38
107
  a wire-format change — renamed `kty`, renamed `alg`, field moved to
39
108
  a different section — ships in a new **minor** version (`0.4 → 0.5`).
40
109
  The CHANGELOG entry will call it out as breaking and name the draft
@@ -52,12 +121,15 @@ change between draft revisions. Our policy for adapting to them:
52
121
  This gives operators a migration window without requiring a
53
122
  flag-day rollout.
54
123
 
55
- 4. **Final RFC.** When `draft-ietf-cose-dilithium` is published as an
56
- RFC, jwt-pq will bump to the RFC's final field names on the export
57
- side in a minor (pre-1.0) or major (post-1.0) release, and document
58
- the RFC reference here. The import side will continue accepting the
59
- last draft revision for at least one further minor to ease
60
- migration.
124
+ 4. **Final RFC.** When a tracked draft is published as an RFC, jwt-pq
125
+ will move to the RFC's final names and encodings on the export side
126
+ in a minor (pre-1.0) or major (post-1.0) release, and document the
127
+ RFC reference here. The import side will continue accepting the
128
+ previous form for at least one further minor to ease migration.
129
+ (`draft-ietf-cose-dilithium` became RFC 9964 with no change to the
130
+ `alg`, `kty`, or `pub` values jwt-pq already emitted. The exception
131
+ is private-key serialization, where jwt-pq has not yet moved to the
132
+ RFC encodings — see "Known divergences" above.)
61
133
 
62
134
  ## Reporting divergences
63
135
 
@@ -10,8 +10,8 @@ require "zlib"
10
10
  require "etc"
11
11
  require "tmpdir"
12
12
 
13
- LIBOQS_VERSION = "0.15.0"
14
- LIBOQS_SHA256 = "3983f7cd1247f37fb76a040e6fd684894d44a84cecdcfbdb90559b3216684b5c"
13
+ LIBOQS_VERSION = "0.16.0"
14
+ LIBOQS_SHA256 = "162d5b510518ee5f285f82fa1f16402a885176e818bf1b1a4c3c91c9a2f01eae"
15
15
  LIBOQS_URL = "https://github.com/open-quantum-safe/liboqs/archive/refs/tags/#{LIBOQS_VERSION}.tar.gz"
16
16
 
17
17
  PACKAGE_ROOT_DIR = File.expand_path("../../..", __dir__)
@@ -38,6 +38,7 @@ module JWT
38
38
  )
39
39
  end
40
40
  raise_sign_error!("Both Ed25519 and ML-DSA private keys required") unless signing_key.private?
41
+ raise_sign_error!(mismatch_message(signing_key)) unless signing_key.hybrid_algorithm == alg
41
42
 
42
43
  # Delegate to HybridKey#sign so the Ed25519 and ML-DSA halves
43
44
  # are taken atomically under the hybrid key's mutex — a
@@ -52,6 +53,9 @@ module JWT
52
53
  "Expected a JWT::PQ::HybridKey, got #{verification_key.class}."
53
54
  )
54
55
  end
56
+ # False, not a raise: ruby-jwt calls verify once per candidate
57
+ # key and per allowed algorithm, and must be able to move on.
58
+ return false unless verification_key.hybrid_algorithm == alg
55
59
 
56
60
  return false if signature.bytesize <= ED25519_SIG_SIZE
57
61
 
@@ -73,6 +77,14 @@ module JWT
73
77
  # :nocov:
74
78
  end
75
79
 
80
+ # The hybrid `alg` a token carries must name the ML-DSA parameter
81
+ # set of the key it is signed with.
82
+ #
83
+ # @api private
84
+ def mismatch_message(key)
85
+ "Algorithm mismatch: key is #{key.hybrid_algorithm} but the token alg is #{alg}"
86
+ end
87
+
76
88
  # Boolean-returning wrapper over `Ed25519::VerifyKey#verify`, which
77
89
  # raises on failure. Gives the verify path a uniform shape for both
78
90
  # halves (both produce a bool by assignment, rather than one via
@@ -34,6 +34,12 @@ module JWT
34
34
  "Use JWT::PQ::Key.generate(:#{alg_symbol}) to create a key."
35
35
  )
36
36
  end
37
+ # A key of another parameter set cannot have produced this
38
+ # signature. Return false rather than raise: ruby-jwt calls
39
+ # verify once per candidate key (and per allowed algorithm), and
40
+ # a raise here would stop it before it reached the right one.
41
+ return false unless verification_key.algorithm == alg
42
+
37
43
  verification_key.verify(data, signature)
38
44
  # :nocov: — defensive rescue; Key#verify returns bool, does not raise PQ::Error in practice
39
45
  rescue JWT::PQ::Error
@@ -47,6 +53,8 @@ module JWT
47
53
  case key
48
54
  when JWT::PQ::Key
49
55
  raise_sign_error!("Private key required for signing") unless key.private?
56
+ raise_sign_error!(mismatch_message(key)) unless key.algorithm == alg
57
+
50
58
  key
51
59
  else
52
60
  raise_sign_error!(
@@ -56,6 +64,13 @@ module JWT
56
64
  end
57
65
  end
58
66
 
67
+ # The `alg` a token carries must name the parameter set of the key
68
+ # it is signed or verified with. Without this check an ML-DSA-65
69
+ # key would happily sign or verify a token labelled ML-DSA-44.
70
+ def mismatch_message(key)
71
+ "Algorithm mismatch: key is #{key.algorithm} but the token alg is #{alg}"
72
+ end
73
+
59
74
  def alg_symbol
60
75
  alg.downcase.tr("-", "_")
61
76
  end
data/lib/jwt/pq/jwk.rb CHANGED
@@ -8,20 +8,22 @@ module JWT
8
8
  module PQ
9
9
  # JWK (JSON Web Key) import/export for ML-DSA keys.
10
10
  #
11
- # Follows the `draft-ietf-cose-dilithium` conventions for the `AKP`
11
+ # Follows RFC 9964 for the public members of the `AKP`
12
12
  # ("Algorithm Key Pair") key type:
13
13
  #
14
14
  # - `kty`: `"AKP"`
15
15
  # - `alg`: `"ML-DSA-44"`, `"ML-DSA-65"`, or `"ML-DSA-87"`
16
16
  # - `pub`: base64url-encoded public key (no padding)
17
- # - `priv`: base64url-encoded private key (optional, no padding)
17
+ # - `priv`: base64url-encoded **expanded** private key (optional, no
18
+ # padding). RFC 9964 specifies the 32-byte seed here; see SPEC.md,
19
+ # "Known divergences". Only re-import these with jwt-pq.
18
20
  # - `kid`: RFC 7638 thumbprint over the required members
19
21
  #
20
22
  # @example Export and re-import
21
23
  # jwk = JWT::PQ::JWK.new(key).export
22
24
  # restored = JWT::PQ::JWK.import(jwk)
23
25
  #
24
- # @see https://datatracker.ietf.org/doc/draft-ietf-cose-dilithium/ draft-ietf-cose-dilithium
26
+ # @see https://datatracker.ietf.org/doc/rfc9964/ RFC 9964 (ML-DSA for JOSE and COSE)
25
27
  # @see https://www.rfc-editor.org/rfc/rfc7638 RFC 7638 (JWK Thumbprint)
26
28
  class JWK
27
29
  # Algorithm names accepted in the `alg` field.
@@ -49,6 +51,10 @@ module JWT
49
51
  # `include_private: true` to emit the `priv` field as well (and only
50
52
  # when the wrapped key actually has a private component).
51
53
  #
54
+ # The `priv` value is the expanded private key, not the 32-byte seed
55
+ # RFC 9964 specifies, so a private JWK exported here can only be
56
+ # re-imported by jwt-pq. See SPEC.md, "Known divergences".
57
+ #
52
58
  # @param include_private [Boolean] include the `priv` field. Default: false.
53
59
  # @return [Hash{Symbol=>String}] a JWK with `:kty`, `:alg`, `:pub`,
54
60
  # `:kid`, and optionally `:priv`.
@@ -20,7 +20,7 @@ module JWT
20
20
  # jwks = JWT::PQ::JWKSet.import(JSON.parse(fetch_jwks))
21
21
  # _payload, header = JWT.decode(token, nil, false) # unverified peek
22
22
  # key = jwks[header["kid"]] or raise "unknown kid"
23
- # payload, = JWT.decode(token, key, true, algorithms: [header["alg"]])
23
+ # payload, = JWT.decode(token, key, true, algorithms: [key.algorithm])
24
24
  #
25
25
  # The set indexes members by their RFC 7638 JWK Thumbprint, which is the
26
26
  # `kid` {JWK#export} emits. If you import a JWKS that uses custom (non-
@@ -206,7 +206,7 @@ module JWT
206
206
  # jwks = JWT::PQ::JWKSet.fetch("https://issuer.example/.well-known/jwks.json")
207
207
  # _payload, header = JWT.decode(token, nil, false)
208
208
  # key = jwks[header["kid"]] or raise "unknown kid"
209
- # payload, = JWT.decode(token, key, true, algorithms: [header["alg"]])
209
+ # payload, = JWT.decode(token, key, true, algorithms: [key.algorithm])
210
210
  #
211
211
  # By default, members with unknown `kty`/`alg` in the fetched
212
212
  # body are skipped (see {.import}); pass `strict: true` to make
@@ -35,7 +35,7 @@ module JWT
35
35
  # jwks = JWT::PQ::JWKSet.fetch("https://issuer.example/.well-known/jwks.json")
36
36
  # _payload, header = JWT.decode(token, nil, false)
37
37
  # key = jwks[header["kid"]] or raise "unknown kid"
38
- # payload, = JWT.decode(token, key, true, algorithms: [header["alg"]])
38
+ # payload, = JWT.decode(token, key, true, algorithms: [key.algorithm])
39
39
  class Loader
40
40
  DEFAULT_CACHE_TTL = 300
41
41
  DEFAULT_TIMEOUT = 5
data/lib/jwt/pq/key.rb CHANGED
@@ -185,6 +185,10 @@ module JWT
185
185
  # PEM documents. For a PKCS#8 PEM that does not carry the public key,
186
186
  # use {.from_pem_pair} with a separate public PEM instead.
187
187
  #
188
+ # Private PEMs must have been written by jwt-pq ({#private_to_pem});
189
+ # RFC 9881-encoded private keys from other tools are not yet
190
+ # accepted. Public (SPKI) PEMs from any conforming tool work.
191
+ #
188
192
  # @param pem_string [String] a PEM-encoded key document.
189
193
  # @return [Key] a public-only or full keypair, depending on the PEM format.
190
194
  # @raise [KeyError] for unknown OIDs or PKCS#8 PEMs missing the public key.
@@ -244,6 +248,11 @@ module JWT
244
248
  # The PEM carries both the private key and the public key (so the pair
245
249
  # can later be re-imported with {.from_pem} alone).
246
250
  #
251
+ # The contents are not yet in the RFC 9881 encoding (the expanded key
252
+ # is written without the `CHOICE` wrapper), so this PEM can only be
253
+ # re-imported by jwt-pq — not by OpenSSL or other libraries. See
254
+ # SPEC.md, "Known divergences".
255
+ #
247
256
  # Thread-safe: the read of `@private_key` and the DER build are
248
257
  # serialized against {#destroy!} on the per-instance mutex, so a
249
258
  # concurrent `destroy!` cannot zero the bytes mid-encode.
@@ -3,6 +3,6 @@
3
3
  module JWT
4
4
  module PQ
5
5
  # Current gem version.
6
- VERSION = "0.6.0"
6
+ VERSION = "0.6.2"
7
7
  end
8
8
  end
data/lib/jwt/pq.rb CHANGED
@@ -27,7 +27,7 @@ module JWT
27
27
  # decoded, _header = JWT.decode(token, key, true, algorithms: ["ML-DSA-65"])
28
28
  #
29
29
  # @see https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.204.pdf FIPS 204 (ML-DSA)
30
- # @see https://datatracker.ietf.org/doc/draft-ietf-cose-dilithium/ draft-ietf-cose-dilithium
30
+ # @see https://datatracker.ietf.org/doc/rfc9964/ RFC 9964 (ML-DSA for JOSE and COSE)
31
31
  module PQ
32
32
  # Whether the `ed25519` (or `jwt-eddsa`) gem is available for hybrid mode.
33
33
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jwt-pq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Almeida
@@ -65,6 +65,7 @@ files:
65
65
  - CHANGELOG.md
66
66
  - Gemfile
67
67
  - LICENSE
68
+ - MIGRATING.md
68
69
  - README.md
69
70
  - Rakefile
70
71
  - SECURITY.md