@webpqc/jose 0.0.0-stage → 0.0.1

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.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +434 -2
  3. package/esm/_dnt.test_polyfills.d.ts.map +1 -0
  4. package/esm/jwk.d.ts +37 -0
  5. package/esm/jwk.d.ts.map +1 -0
  6. package/esm/jwk.js +34 -0
  7. package/esm/jws.d.ts +33 -0
  8. package/esm/jws.d.ts.map +1 -0
  9. package/esm/jws.js +31 -0
  10. package/esm/mod.d.ts +15 -0
  11. package/esm/mod.d.ts.map +1 -0
  12. package/esm/mod.js +14 -0
  13. package/esm/package.json +3 -0
  14. package/esm/src/alg.d.ts +9 -0
  15. package/esm/src/alg.d.ts.map +1 -0
  16. package/esm/src/alg.js +20 -0
  17. package/esm/src/base64url/decode.d.ts +13 -0
  18. package/esm/src/base64url/decode.d.ts.map +1 -0
  19. package/esm/src/base64url/decode.js +61 -0
  20. package/esm/src/base64url/encode.d.ts +9 -0
  21. package/esm/src/base64url/encode.d.ts.map +1 -0
  22. package/esm/src/base64url/encode.js +51 -0
  23. package/esm/src/base64url/length.d.ts +5 -0
  24. package/esm/src/base64url/length.d.ts.map +1 -0
  25. package/esm/src/base64url/length.js +6 -0
  26. package/esm/src/bytes.d.ts +13 -0
  27. package/esm/src/bytes.d.ts.map +1 -0
  28. package/esm/src/bytes.js +21 -0
  29. package/esm/src/errors.d.ts +8 -0
  30. package/esm/src/errors.d.ts.map +1 -0
  31. package/esm/src/errors.js +11 -0
  32. package/esm/src/jwk/decode.d.ts +72 -0
  33. package/esm/src/jwk/decode.d.ts.map +1 -0
  34. package/esm/src/jwk/decode.js +116 -0
  35. package/esm/src/jwk/encode.d.ts +57 -0
  36. package/esm/src/jwk/encode.d.ts.map +1 -0
  37. package/esm/src/jwk/encode.js +69 -0
  38. package/esm/src/jwk/key.d.ts +11 -0
  39. package/esm/src/jwk/key.d.ts.map +1 -0
  40. package/esm/src/jwk/key.js +18 -0
  41. package/esm/src/jwk/mlDsa.d.ts +20 -0
  42. package/esm/src/jwk/mlDsa.d.ts.map +1 -0
  43. package/esm/src/jwk/mlDsa.js +32 -0
  44. package/esm/src/jwk/mlKem.d.ts +29 -0
  45. package/esm/src/jwk/mlKem.d.ts.map +1 -0
  46. package/esm/src/jwk/mlKem.js +41 -0
  47. package/esm/src/jwk/slhDsa.d.ts +51 -0
  48. package/esm/src/jwk/slhDsa.d.ts.map +1 -0
  49. package/esm/src/jwk/slhDsa.js +79 -0
  50. package/esm/src/jwk/thumbprint.d.ts +43 -0
  51. package/esm/src/jwk/thumbprint.d.ts.map +1 -0
  52. package/esm/src/jwk/thumbprint.js +56 -0
  53. package/esm/src/jwk/types.d.ts +76 -0
  54. package/esm/src/jwk/types.d.ts.map +1 -0
  55. package/esm/src/jwk/types.js +1 -0
  56. package/esm/src/jws/b64.d.ts +14 -0
  57. package/esm/src/jws/b64.d.ts.map +1 -0
  58. package/esm/src/jws/b64.js +18 -0
  59. package/esm/src/jws/decode.d.ts +37 -0
  60. package/esm/src/jws/decode.d.ts.map +1 -0
  61. package/esm/src/jws/decode.js +39 -0
  62. package/esm/src/jws/parse.d.ts +19 -0
  63. package/esm/src/jws/parse.d.ts.map +1 -0
  64. package/esm/src/jws/parse.js +46 -0
  65. package/esm/src/jws/sign.d.ts +43 -0
  66. package/esm/src/jws/sign.d.ts.map +1 -0
  67. package/esm/src/jws/sign.js +106 -0
  68. package/esm/src/jws/types.d.ts +136 -0
  69. package/esm/src/jws/types.d.ts.map +1 -0
  70. package/esm/src/jws/types.js +1 -0
  71. package/esm/src/jws/verify.d.ts +45 -0
  72. package/esm/src/jws/verify.d.ts.map +1 -0
  73. package/esm/src/jws/verify.js +75 -0
  74. package/package.json +57 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Ajitomi Daisuke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,435 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/dajiaji/webpqc/main/assets/webpqc-logo.png" alt="webpqc logo" width="200" />
3
+ </p>
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ <h1 align="center">@webpqc/jose</h1>
6
+
7
+ <div align="center">
8
+ <a href="https://jsr.io/@webpqc/jose"><img src="https://jsr.io/badges/@webpqc/jose" alt="JSR"/></a>
9
+ <a href="https://www.npmjs.com/package/@webpqc/jose"><img src="https://img.shields.io/npm/v/@webpqc/jose?color=%23EE3214" alt="NPM"/></a>
10
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_browsers.yml/badge.svg" alt="Browser CI" />
11
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_node.yml/badge.svg" alt="Node.js CI" />
12
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_deno.yml/badge.svg" alt="Deno CI" />
13
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_cloudflare.yml/badge.svg" alt="Cloudflare Workers CI" />
14
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_bun.yml/badge.svg" alt="bun CI" />
15
+ <img src="https://github.com/dajiaji/webpqc/actions/workflows/ci_llrt.yml/badge.svg" alt="LLRT CI" />
16
+ </div>
17
+
18
+ <div align="center">
19
+ JOSE (JSON Object Signing and Encryption) for post-quantum cryptography, written in TypeScript.<br>
20
+ </div>
21
+ <p></p>
22
+
23
+ <div align="center">
24
+
25
+ [Documentation for main](https://dajiaji.github.io/webpqc/docs/jose/main/)
26
+
27
+ </div>
28
+
29
+ - ✅ JWK: the keys of ML-DSA, ML-KEM and SLH-DSA as JWKs of the key type "AKP"
30
+ ([RFC 9964](https://www.rfc-editor.org/rfc/rfc9964.html)), and their JWK
31
+ Thumbprints ([RFC 7638](https://www.rfc-editor.org/rfc/rfc7638.html)).
32
+ - ✅ JWS: JWSs in the JWS Compact Serialization
33
+ ([RFC 7515](https://www.rfc-editor.org/rfc/rfc7515.html)) which are signed
34
+ with ML-DSA (RFC 9964) or SLH-DSA. The signers and the verifiers of
35
+ [`@webpqc/mldsa`](../mldsa/) and [`@webpqc/slhdsa`](../slhdsa/) sign and
36
+ verify them as they are.
37
+ - ✅ The JWKs which the Web Cryptography API imports and exports
38
+ ([Modern Algorithms in the Web Cryptography API](https://wicg.github.io/webcrypto-modern-algos/)):
39
+ tested against the runtimes which have the algorithms.
40
+ - ✅ Written in TypeScript, with no dependencies. It takes and gives the keys as
41
+ octets, and it is given what signs and what verifies, so it does not depend on
42
+ the packages which implement the algorithms either.
43
+ - ✅ Tree-shaking friendly: an application bundles only what it uses, which is
44
+ about 1 KB (0.6 to 0.7 KB gzipped) to read a JWK or to write one, and 1.4 to
45
+ 1.5 KB (0.8 KB gzipped) to sign a JWS or to verify one. See
46
+ [Bundle size](#bundle-size).
47
+ - ✅ Strict: a JWK is accepted only for the algorithm which is given, with keys
48
+ of its sizes, and a JWS only for the algorithm of the verifier, each in the
49
+ one base64url encoding which its octets have.
50
+ - ✅ Passed the examples of RFC 9964: it gives their JWKs, their JWK Thumbprints
51
+ and their JWSs, octet for octet.
52
+
53
+ > [!WARNING]
54
+ > This package is at an early stage. The API may change, and the implementation
55
+ > has not been audited. Only JWK and JWS are implemented so far: see
56
+ > [Limitations](#limitations).
57
+
58
+ ## Installation
59
+
60
+ `@webpqc/jose` is published on [JSR](https://jsr.io/@webpqc/jose) and
61
+ [npm](https://www.npmjs.com/package/@webpqc/jose). It is an ESM-only package,
62
+ and requires Node.js 20 or later.
63
+
64
+ ```sh
65
+ # Node.js, Cloudflare Workers and Bun, using npm:
66
+ npm install @webpqc/jose
67
+ # Node.js, Cloudflare Workers and Bun, using jsr:
68
+ npx jsr add @webpqc/jose
69
+ # Deno:
70
+ deno add jsr:@webpqc/jose
71
+ ```
72
+
73
+ ## Usage
74
+
75
+ ### JWK
76
+
77
+ The functions take an algorithm, such as `ML_DSA_65`, and the keys as
78
+ `Uint8Array`s, which are the keys of [`@webpqc/mldsa`](../mldsa/),
79
+ [`@webpqc/mlkem`](../mlkem/) and [`@webpqc/slhdsa`](../slhdsa/).
80
+
81
+ ```ts
82
+ import {
83
+ calculateJwkThumbprint,
84
+ exportPrivateJwk,
85
+ exportPublicJwk,
86
+ importPrivateJwk,
87
+ importPublicJwk,
88
+ ML_DSA_65,
89
+ } from "@webpqc/jose";
90
+ import {
91
+ createMlDsa65Signer,
92
+ createMlDsa65Verifier,
93
+ generateMlDsa65KeyPair,
94
+ } from "@webpqc/mldsa";
95
+
96
+ // Generate a key pair with @webpqc/mldsa. The private key is a 32-octet seed.
97
+ const [pk, sk] = await generateMlDsa65KeyPair();
98
+
99
+ // The key pair as a JWK, to store it:
100
+ // { kty: "AKP", alg: "ML-DSA-65", pub: "...", priv: "..." }
101
+ const privateJwk = exportPrivateJwk(ML_DSA_65, pk, sk);
102
+
103
+ // The public key as a JWK, to publish it, with its JWK Thumbprint as "kid":
104
+ // { kty: "AKP", alg: "ML-DSA-65", pub: "...", kid: "..." }
105
+ const publicJwk = {
106
+ ...exportPublicJwk(ML_DSA_65, pk),
107
+ kid: await calculateJwkThumbprint(ML_DSA_65, pk),
108
+ };
109
+
110
+ // A signer takes the private key out of the JWK of the key pair.
111
+ const seed = importPrivateJwk(ML_DSA_65, privateJwk);
112
+ const signer = await createMlDsa65Signer(seed);
113
+ const msg = new TextEncoder().encode("hello world");
114
+ const sig = await signer.sign(msg);
115
+ signer.destroy();
116
+
117
+ // A verifier takes the public key out of the JWK of the public key.
118
+ const verifier = await createMlDsa65Verifier(
119
+ importPublicJwk(ML_DSA_65, publicJwk),
120
+ );
121
+ const ok = await verifier.verify(sig, msg);
122
+ // ok === true
123
+ ```
124
+
125
+ The same is exported by `@webpqc/jose/jwk`, which loads nothing but the modules
126
+ for JWK on a runtime which runs the package without bundling it.
127
+
128
+ ### JWS
129
+
130
+ The functions take what signs or verifies with the key: a signer or a verifier
131
+ of `@webpqc/mldsa` or `@webpqc/slhdsa`, as it is. It has the name of its
132
+ algorithm as `alg`, which is "alg" of the JWS.
133
+
134
+ ```ts
135
+ import {
136
+ calculateJwkThumbprint,
137
+ decodeJwsHeader,
138
+ exportPrivateJwk,
139
+ exportPublicJwk,
140
+ importPrivateJwk,
141
+ importPublicJwk,
142
+ ML_DSA_65,
143
+ signJws,
144
+ verifyJws,
145
+ } from "@webpqc/jose";
146
+ import {
147
+ createMlDsa65Signer,
148
+ createMlDsa65Verifier,
149
+ generateMlDsa65KeyPair,
150
+ } from "@webpqc/mldsa";
151
+
152
+ const [pk, sk] = await generateMlDsa65KeyPair();
153
+ const kid = await calculateJwkThumbprint(ML_DSA_65, pk);
154
+ const privateJwk = exportPrivateJwk(ML_DSA_65, pk, sk);
155
+ const publicJwk = { ...exportPublicJwk(ML_DSA_65, pk), kid };
156
+
157
+ // Sign a payload with the private key of the JWK. The header of the JWS has
158
+ // "alg" of the signer, and what the option `header` has: here, "kid".
159
+ const signer = await createMlDsa65Signer(
160
+ importPrivateJwk(ML_DSA_65, privateJwk),
161
+ );
162
+ // signer.alg === "ML-DSA-65"
163
+ const jws = await signJws(signer, new TextEncoder().encode("hello world"), {
164
+ header: { kid },
165
+ });
166
+ signer.destroy();
167
+ // "eyJhbGciOiJNTC1EU0EtNjUiLCJraWQiOi...In0.aGVsbG8gd29ybGQ.<signature>"
168
+
169
+ // The header names the key, before the JWS is verified.
170
+ const unverified = decodeJwsHeader(jws);
171
+ // unverified.kid === kid
172
+
173
+ // Verify the JWS with the public key of the JWK. It has to be a JWS of the
174
+ // algorithm of the verifier.
175
+ const verifier = await createMlDsa65Verifier(
176
+ importPublicJwk(ML_DSA_65, publicJwk),
177
+ );
178
+ const { header, payload } = await verifyJws(verifier, jws);
179
+ // header: { alg: "ML-DSA-65", kid: "..." }
180
+ // new TextDecoder().decode(payload) === "hello world"
181
+ ```
182
+
183
+ `verifyJws` throws `JoseError` for a JWS which is not one of the algorithm of
184
+ the verifier, or whose signature is not valid. The same is exported by
185
+ `@webpqc/jose/jws`.
186
+
187
+ What signs is anything which has `alg` and `sign(msg)`, and what verifies is
188
+ anything which has `alg` and `verify(sig, msg)`: for example, the keys of the
189
+ Web Cryptography API of a runtime which has the algorithm.
190
+
191
+ ```ts
192
+ import type { JwsSigner, JwsVerifier } from "@webpqc/jose";
193
+
194
+ const signerOf = (privateKey: CryptoKey): JwsSigner => ({
195
+ alg: "ML-DSA-65",
196
+ sign: async (msg) =>
197
+ new Uint8Array(
198
+ await crypto.subtle.sign("ML-DSA-65", privateKey, msg as BufferSource),
199
+ ),
200
+ });
201
+
202
+ const verifierOf = (publicKey: CryptoKey): JwsVerifier => ({
203
+ alg: "ML-DSA-65",
204
+ verify: (sig, msg) =>
205
+ crypto.subtle.verify(
206
+ "ML-DSA-65",
207
+ publicKey,
208
+ sig as BufferSource,
209
+ msg as BufferSource,
210
+ ),
211
+ });
212
+ ```
213
+
214
+ `alg` is required: a JWS without "alg" is neither made nor accepted. The signers
215
+ and the verifiers of `@webpqc/mldsa` and `@webpqc/slhdsa` have it since the
216
+ versions after 0.1.0.
217
+
218
+ ## API
219
+
220
+ | Function | Description |
221
+ | ----------------------------------------------- | ---------------------------------------------------------- |
222
+ | `exportPublicJwk(alg, publicKey)` | Represents a public key as a JWK: `{ kty, alg, pub }`. |
223
+ | `exportPrivateJwk(alg, publicKey, privateKey)` | Represents a key pair as a JWK: `{ kty, alg, pub, priv }`. |
224
+ | `importPublicJwk(alg, jwk)` | Takes the public key out of a JWK which is a public key. |
225
+ | `importPrivateJwk(alg, jwk)` | Takes the private key out of a JWK which has it. |
226
+ | `calculateJwkThumbprint(alg, publicKey, opts?)` | Resolves to the JWK Thumbprint of a public key. |
227
+ | `signJws(signer, payload, opts?)` | Resolves to the JWS of a payload, which the signer signs. |
228
+ | `verifyJws(verifier, jws)` | Resolves to the header and the payload of a JWS, verified. |
229
+ | `decodeJwsHeader(jws)` | Decodes the header of a JWS, without verifying the JWS. |
230
+
231
+ They throw `JoseError` for an invalid argument. `calculateJwkThumbprint` returns
232
+ a promise, as it uses the Web Cryptography API for the hash function: SHA-256,
233
+ or the one of the option `hash` ("SHA-384" or "SHA-512"). In a browser, the Web
234
+ Cryptography API is available only in a secure context (HTTPS or localhost), so
235
+ `calculateJwkThumbprint` does not work on a page which is served over HTTP,
236
+ unlike the other functions. `signJws` and `verifyJws` return a promise as well,
237
+ as a signer and a verifier may.
238
+
239
+ ### Algorithms
240
+
241
+ An algorithm is a constant which has the value of "alg" and the sizes of the
242
+ keys:
243
+
244
+ | Constant | "alg" | "pub" | "priv" |
245
+ | -------------------- | -------------------- | ------------------------------- | ------------------------ |
246
+ | `ML_DSA_44` | `ML-DSA-44` | Public key (1312 octets) | Seed (32 octets) |
247
+ | `ML_DSA_65` | `ML-DSA-65` | Public key (1952 octets) | Seed (32 octets) |
248
+ | `ML_DSA_87` | `ML-DSA-87` | Public key (2592 octets) | Seed (32 octets) |
249
+ | `ML_KEM_512` | `ML-KEM-512` | Encapsulation key (800 octets) | Seed `d‖z` (64 octets) |
250
+ | `ML_KEM_768` | `ML-KEM-768` | Encapsulation key (1184 octets) | Seed `d‖z` (64 octets) |
251
+ | `ML_KEM_1024` | `ML-KEM-1024` | Encapsulation key (1568 octets) | Seed `d‖z` (64 octets) |
252
+ | `SLH_DSA_SHAKE_128S` | `SLH-DSA-SHAKE-128s` | Public key (32 octets) | Private key (64 octets) |
253
+ | `SLH_DSA_SHAKE_128F` | `SLH-DSA-SHAKE-128f` | Public key (32 octets) | Private key (64 octets) |
254
+ | `SLH_DSA_SHAKE_192S` | `SLH-DSA-SHAKE-192s` | Public key (48 octets) | Private key (96 octets) |
255
+ | `SLH_DSA_SHAKE_192F` | `SLH-DSA-SHAKE-192f` | Public key (48 octets) | Private key (96 octets) |
256
+ | `SLH_DSA_SHAKE_256S` | `SLH-DSA-SHAKE-256s` | Public key (64 octets) | Private key (128 octets) |
257
+ | `SLH_DSA_SHAKE_256F` | `SLH-DSA-SHAKE-256f` | Public key (64 octets) | Private key (128 octets) |
258
+
259
+ They are the keys of `@webpqc/mldsa`, `@webpqc/mlkem` and `@webpqc/slhdsa`.
260
+
261
+ - ML-DSA is as [RFC 9964](https://www.rfc-editor.org/rfc/rfc9964.html) defines
262
+ it.
263
+ - ML-KEM and SLH-DSA are as
264
+ [Modern Algorithms in the Web Cryptography API](https://wicg.github.io/webcrypto-modern-algos/)
265
+ defines them, and SLH-DSA-SHAKE-128s also as
266
+ [draft-ietf-cose-sphincs-plus](https://datatracker.ietf.org/doc/draft-ietf-cose-sphincs-plus/)
267
+ does. Their values of "alg" are not registered for JOSE yet, so they may
268
+ change.
269
+
270
+ The package has no table of the algorithms: the functions know an algorithm only
271
+ by the constant which they are given. An algorithm which the package does not
272
+ have, or another value of "alg" for the same keys, is therefore a plain object
273
+ of the type `AkpAlgorithm`:
274
+
275
+ ```ts
276
+ import { type AkpAlgorithm, exportPublicJwk } from "@webpqc/jose";
277
+
278
+ const SLH_DSA_SHA2_128S: AkpAlgorithm<"SLH-DSA-SHA2-128s"> = {
279
+ alg: "SLH-DSA-SHA2-128s",
280
+ pubSize: 32,
281
+ privSize: 64,
282
+ };
283
+
284
+ const jwk = exportPublicJwk(SLH_DSA_SHA2_128S, new Uint8Array(32));
285
+ // { kty: "AKP", alg: "SLH-DSA-SHA2-128s", pub: "AAAA...AAA" }
286
+ ```
287
+
288
+ The functions for JWS take no algorithm: "alg" of a JWS is `alg` of the signer,
289
+ and `verifyJws` takes a JWS only if its "alg" is `alg` of the verifier. For the
290
+ signers and the verifiers of `@webpqc/mldsa` and `@webpqc/slhdsa`, it is the
291
+ "alg" of the table above, so the key of a JWK signs and verifies the JWSs of its
292
+ algorithm.
293
+
294
+ - The JWSs of ML-DSA are the ones of RFC 9964.
295
+ - Of SLH-DSA, draft-ietf-cose-sphincs-plus defines the JWSs of
296
+ SLH-DSA-SHAKE-128s. No specification defines "alg" of a JWS for the other
297
+ parameter sets yet: a JWS of one of them has the name of the parameter set as
298
+ "alg", which another implementation may not take, and which may not be the one
299
+ which is registered in the end.
300
+
301
+ ### What is checked
302
+
303
+ `importPublicJwk` and `importPrivateJwk` take a JWK from anywhere, e.g. the
304
+ result of `JSON.parse`, and check that:
305
+
306
+ - "kty" is "AKP", and "alg", which is required for the key type, is the one of
307
+ the algorithm. A key of another algorithm is rejected even if its keys have
308
+ the same sizes, as the ones of SLH-DSA-SHAKE-128s and SLH-DSA-SHAKE-128f.
309
+ - "pub" and "priv" are strings of the lengths which keys of the sizes of the
310
+ algorithm encode to, in base64url with no padding and no character out of the
311
+ alphabet, and with no bit set which is not a part of the key: each key has one
312
+ encoding alone. A string of another length is rejected without being decoded.
313
+ - A public key has no "priv": `importPublicJwk` rejects a JWK which has it.
314
+
315
+ They do not check:
316
+
317
+ - The other parameters, such as "use" and "key_ops".
318
+ - The keys themselves, which is left to what uses them: `createMlDsa65Verifier`
319
+ rejects a public key which is not valid, for example.
320
+ - That "pub" is the public key of "priv", which only the algorithm itself can
321
+ tell. This is why `importPrivateJwk` checks "pub" as above, as the key type
322
+ requires it, but returns the private key alone: a public key which is not the
323
+ one of the private key is of no use, or worse (RFC 9964, Section 7.4). The
324
+ public key of a private key is the one which the algorithm derives from it,
325
+ e.g. with `deriveMlDsa65KeyPair`. Compare it with "pub" to reject such a JWK,
326
+ as the Web Cryptography API does.
327
+
328
+ `exportPublicJwk`, `exportPrivateJwk` and `calculateJwkThumbprint` check that a
329
+ key is a `Uint8Array` of the size of the algorithm. `exportPrivateJwk` does not
330
+ check that the keys are of one key pair either. `calculateJwkThumbprint` also
331
+ rejects another hash function than the three above, and an algorithm whose "alg"
332
+ has a character which JSON escapes (a quotation mark, a backslash or a control
333
+ character), as RFC 7638 does not define the thumbprint of such a JWK.
334
+
335
+ A key is encoded and decoded without a table and without a branch on its value,
336
+ so that neither a memory access nor a jump depends on a private key.
337
+
338
+ `verifyJws` takes a JWS from anywhere, and checks that:
339
+
340
+ - It is a string of three parts.
341
+ - The header is the base64url encoding of a JSON object in UTF-8, and its "alg"
342
+ is `alg` of the verifier. The JWS never chooses how it is verified: one of
343
+ another algorithm, "none" included, is rejected before the verifier is asked.
344
+ - The header has no "crit": the function understands no extension, so it rejects
345
+ a JWS which requires one (RFC 7515, Section 4.1.11).
346
+ - "b64" of the header, if it is present, is true: a payload which is not encoded
347
+ ([RFC 7797](https://www.rfc-editor.org/rfc/rfc7797.html)) is not supported,
348
+ and is not taken for one which is.
349
+ - The payload and the signature are in base64url, as strictly as the keys of a
350
+ JWK.
351
+ - The verifier gives `true` for the signature and the JWS Signing Input.
352
+
353
+ It does not check the size of the signature, which is the verifier's to check:
354
+ the ones of `@webpqc/mldsa` and `@webpqc/slhdsa` reject a signature of another
355
+ size than the one of their algorithm. Nor does it check the other header
356
+ parameters, such as "kid" and "typ", or the payload: the claims of a JWT, such
357
+ as "exp", are the application's to check. `decodeJwsHeader` checks only that the
358
+ JWS has three parts and that its header is such a JSON object: what it gives is
359
+ not verified, and is to choose the key with.
360
+
361
+ `signJws` checks that `alg` of the signer is a string, that the payload is a
362
+ `Uint8Array`, that the option `header` is an object which JSON can represent,
363
+ which has no "toJSON" and whose "b64" is not other than true, and that the
364
+ signer gives a `Uint8Array`. "alg" of the header is always the one of the
365
+ signer, and is its first parameter, but for the ones whose names are integers,
366
+ which `JSON.stringify` writes before the others. An option with "toJSON" is
367
+ rejected because `JSON.stringify` would write what it gives in place of the
368
+ header which is checked, with any "alg" and "b64". The header is an object
369
+ without a prototype which has the own parameters of the option, so nothing of
370
+ `Object.prototype` is read as one of them.
371
+
372
+ A verifier whose `alg` is not a string verifies no JWS: "alg" of a header is a
373
+ string, which it is not equal to, so `verifyJws` rejects any JWS for it, without
374
+ asking it.
375
+
376
+ A signer is called with the message alone, and a verifier with the signature and
377
+ the message alone. The ones of `@webpqc/mldsa` and `@webpqc/slhdsa` then sign
378
+ and verify with the empty context string, as RFC 9964 requires.
379
+
380
+ ## Bundle size
381
+
382
+ An application bundles only the functions and the algorithms which it names. The
383
+ sizes of an application which uses one function, with `ML_DSA_65` where it takes
384
+ an algorithm, bundled and minified with esbuild
385
+ (`--bundle --format=esm --minify`), and then compressed with `gzip -9`:
386
+
387
+ | Application uses | Minified (bytes) | Gzipped (bytes) |
388
+ | ---------------------------------------------------- | ---------------- | --------------- |
389
+ | `importPublicJwk` | 1,179 | 676 |
390
+ | `importPrivateJwk` | 1,130 | 655 |
391
+ | `exportPublicJwk` | 856 | 563 |
392
+ | `exportPrivateJwk` | 896 | 580 |
393
+ | `calculateJwkThumbprint` | 1,237 | 741 |
394
+ | `signJws` | 1,381 | 769 |
395
+ | `verifyJws` | 1,493 | 819 |
396
+ | `decodeJwsHeader` | 901 | 574 |
397
+ | `importPrivateJwk` and `signJws` | 2,386 | 1,160 |
398
+ | `decodeJwsHeader`, `importPublicJwk` and `verifyJws` | 2,107 | 1,058 |
399
+ | an algorithm alone (`ML_KEM_768`) | 75 | 89 |
400
+ | the whole package | 4,926 | 1,972 |
401
+ | nothing of it | 0 | - |
402
+
403
+ It is so because:
404
+
405
+ - The algorithms are constants which the functions are given, and the package
406
+ has neither a table of them nor a function which chooses one by its name.
407
+ - What signs and what verifies are given as well, with the names of their
408
+ algorithms: the package has no algorithm in it, and the functions for JWS
409
+ bundle not even a constant of one.
410
+ - The modules have nothing but function declarations, literals and a class:
411
+ nothing runs when a module is loaded, so a bundler does not have to prove that
412
+ it can drop it. A package which is imported for nothing leaves nothing.
413
+ - What reads a JWK or a JWS and what writes one are separate modules, down to
414
+ the base64url decoder and encoder. What reads both shares what decodes, as the
415
+ application which verifies a JWS with the key of a JWK above.
416
+
417
+ The [test](./test/treeshake/) bundles the applications above, and fails if a
418
+ bundle has a part of the package or an algorithm which the application does not
419
+ use.
420
+
421
+ ## Limitations
422
+
423
+ - Only JWK and JWS are implemented so far. JWE is not planned.
424
+ - Of JWS, only the JWS Compact Serialization is supported: not the JWS JSON
425
+ Serialization, a detached payload, a payload which is not encoded (RFC 7797),
426
+ nor a header with "crit". The claims of a JWT are not validated.
427
+ - Only the key type "AKP" is supported.
428
+ - Of SLH-DSA, the package has the constants of the parameter sets with SHAKE,
429
+ which are the ones `@webpqc/slhdsa` implements. Only SLH-DSA-SHAKE-128s of
430
+ them has a specification of its JWSs: see [Algorithms](#algorithms).
431
+
432
+ ## Contributing
433
+
434
+ We welcome all kind of contributions, filing issues, suggesting new features or
435
+ sending PRs.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_dnt.test_polyfills.d.ts","sourceRoot":"","sources":["../src/_dnt.test_polyfills.ts"],"names":[],"mappings":"AAeA,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd;;;;WAIG;QACH,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC;KAC5C;CACF;AAED,OAAO,EAAE,CAAC"}
package/esm/jwk.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * JWKs of the key type "AKP" (Algorithm Key Pair, RFC 9964) for the keys of
3
+ * ML-DSA, ML-KEM and SLH-DSA.
4
+ *
5
+ * @example
6
+ *
7
+ * ```ts
8
+ * import {
9
+ * exportPrivateJwk,
10
+ * ML_DSA_65,
11
+ * importPrivateJwk,
12
+ * importPublicJwk,
13
+ * exportPublicJwk,
14
+ * } from "@webpqc/jose/jwk";
15
+ * import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
16
+ *
17
+ * const [pk, sk] = await generateMlDsa65KeyPair();
18
+ *
19
+ * const publicJwk = exportPublicJwk(ML_DSA_65, pk);
20
+ * const privateJwk = exportPrivateJwk(ML_DSA_65, pk, sk);
21
+ *
22
+ * const pk2 = importPublicJwk(ML_DSA_65, publicJwk);
23
+ * const sk2 = importPrivateJwk(ML_DSA_65, privateJwk);
24
+ * ```
25
+ *
26
+ * @module
27
+ */
28
+ export { JoseError } from "./src/errors.js";
29
+ export type { AkpAlgorithm, AkpPrivateJwk, AkpPublicJwk, JwkCommonParameters, } from "./src/jwk/types.js";
30
+ export { exportPrivateJwk, exportPublicJwk } from "./src/jwk/encode.js";
31
+ export { importPrivateJwk, importPublicJwk } from "./src/jwk/decode.js";
32
+ export { calculateJwkThumbprint } from "./src/jwk/thumbprint.js";
33
+ export type { JwkThumbprintOptions } from "./src/jwk/thumbprint.js";
34
+ export { ML_DSA_44, ML_DSA_65, ML_DSA_87 } from "./src/jwk/mlDsa.js";
35
+ export { ML_KEM_1024, ML_KEM_512, ML_KEM_768 } from "./src/jwk/mlKem.js";
36
+ export { SLH_DSA_SHAKE_128F, SLH_DSA_SHAKE_128S, SLH_DSA_SHAKE_192F, SLH_DSA_SHAKE_192S, SLH_DSA_SHAKE_256F, SLH_DSA_SHAKE_256S, } from "./src/jwk/slhDsa.js";
37
+ //# sourceMappingURL=jwk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jwk.d.ts","sourceRoot":"","sources":["../src/jwk.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EACV,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,mBAAmB,GACpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,YAAY,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EACL,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,qBAAqB,CAAC"}
package/esm/jwk.js ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * JWKs of the key type "AKP" (Algorithm Key Pair, RFC 9964) for the keys of
3
+ * ML-DSA, ML-KEM and SLH-DSA.
4
+ *
5
+ * @example
6
+ *
7
+ * ```ts
8
+ * import {
9
+ * exportPrivateJwk,
10
+ * ML_DSA_65,
11
+ * importPrivateJwk,
12
+ * importPublicJwk,
13
+ * exportPublicJwk,
14
+ * } from "@webpqc/jose/jwk";
15
+ * import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
16
+ *
17
+ * const [pk, sk] = await generateMlDsa65KeyPair();
18
+ *
19
+ * const publicJwk = exportPublicJwk(ML_DSA_65, pk);
20
+ * const privateJwk = exportPrivateJwk(ML_DSA_65, pk, sk);
21
+ *
22
+ * const pk2 = importPublicJwk(ML_DSA_65, publicJwk);
23
+ * const sk2 = importPrivateJwk(ML_DSA_65, privateJwk);
24
+ * ```
25
+ *
26
+ * @module
27
+ */
28
+ export { JoseError } from "./src/errors.js";
29
+ export { exportPrivateJwk, exportPublicJwk } from "./src/jwk/encode.js";
30
+ export { importPrivateJwk, importPublicJwk } from "./src/jwk/decode.js";
31
+ export { calculateJwkThumbprint } from "./src/jwk/thumbprint.js";
32
+ export { ML_DSA_44, ML_DSA_65, ML_DSA_87 } from "./src/jwk/mlDsa.js";
33
+ export { ML_KEM_1024, ML_KEM_512, ML_KEM_768 } from "./src/jwk/mlKem.js";
34
+ export { SLH_DSA_SHAKE_128F, SLH_DSA_SHAKE_128S, SLH_DSA_SHAKE_192F, SLH_DSA_SHAKE_192S, SLH_DSA_SHAKE_256F, SLH_DSA_SHAKE_256S, } from "./src/jwk/slhDsa.js";
package/esm/jws.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * JWSs in the JWS Compact Serialization (RFC 7515) which are signed with
3
+ * ML-DSA (RFC 9964) or SLH-DSA, by a signer and a verifier which have the
4
+ * key and the name of the algorithm.
5
+ *
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import { signJws, verifyJws } from "@webpqc/jose/jws";
10
+ * import {
11
+ * createMlDsa65Signer,
12
+ * createMlDsa65Verifier,
13
+ * generateMlDsa65KeyPair,
14
+ * } from "@webpqc/mldsa";
15
+ *
16
+ * const [pk, sk] = await generateMlDsa65KeyPair();
17
+ *
18
+ * const signer = await createMlDsa65Signer(sk);
19
+ * const jws = await signJws(signer, new TextEncoder().encode("hello world"));
20
+ * signer.destroy();
21
+ *
22
+ * const verifier = await createMlDsa65Verifier(pk);
23
+ * const { payload } = await verifyJws(verifier, jws);
24
+ * ```
25
+ *
26
+ * @module
27
+ */
28
+ export { JoseError } from "./src/errors.js";
29
+ export type { JwsHeader, JwsHeaderParameters, JwsSigner, JwsSignOptions, JwsVerifier, JwsVerifyResult, } from "./src/jws/types.js";
30
+ export { signJws } from "./src/jws/sign.js";
31
+ export { verifyJws } from "./src/jws/verify.js";
32
+ export { decodeJwsHeader } from "./src/jws/decode.js";
33
+ //# sourceMappingURL=jws.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jws.d.ts","sourceRoot":"","sources":["../src/jws.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EACV,SAAS,EACT,mBAAmB,EACnB,SAAS,EACT,cAAc,EACd,WAAW,EACX,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC"}
package/esm/jws.js ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * JWSs in the JWS Compact Serialization (RFC 7515) which are signed with
3
+ * ML-DSA (RFC 9964) or SLH-DSA, by a signer and a verifier which have the
4
+ * key and the name of the algorithm.
5
+ *
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import { signJws, verifyJws } from "@webpqc/jose/jws";
10
+ * import {
11
+ * createMlDsa65Signer,
12
+ * createMlDsa65Verifier,
13
+ * generateMlDsa65KeyPair,
14
+ * } from "@webpqc/mldsa";
15
+ *
16
+ * const [pk, sk] = await generateMlDsa65KeyPair();
17
+ *
18
+ * const signer = await createMlDsa65Signer(sk);
19
+ * const jws = await signJws(signer, new TextEncoder().encode("hello world"));
20
+ * signer.destroy();
21
+ *
22
+ * const verifier = await createMlDsa65Verifier(pk);
23
+ * const { payload } = await verifyJws(verifier, jws);
24
+ * ```
25
+ *
26
+ * @module
27
+ */
28
+ export { JoseError } from "./src/errors.js";
29
+ export { signJws } from "./src/jws/sign.js";
30
+ export { verifyJws } from "./src/jws/verify.js";
31
+ export { decodeJwsHeader } from "./src/jws/decode.js";
package/esm/mod.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * JOSE (JSON Object Signing and Encryption) for post-quantum cryptography.
3
+ *
4
+ * It has what the modules `@webpqc/jose/jwk` and `@webpqc/jose/jws` have:
5
+ *
6
+ * - JWKs of the key type "AKP" (Algorithm Key Pair, RFC 9964) for the keys of
7
+ * ML-DSA, ML-KEM and SLH-DSA.
8
+ * - JWSs in the JWS Compact Serialization (RFC 7515) which are signed with
9
+ * ML-DSA (RFC 9964) or SLH-DSA.
10
+ *
11
+ * @module
12
+ */
13
+ export * from "./jwk.js";
14
+ export * from "./jws.js";
15
+ //# sourceMappingURL=mod.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC"}
package/esm/mod.js ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * JOSE (JSON Object Signing and Encryption) for post-quantum cryptography.
3
+ *
4
+ * It has what the modules `@webpqc/jose/jwk` and `@webpqc/jose/jws` have:
5
+ *
6
+ * - JWKs of the key type "AKP" (Algorithm Key Pair, RFC 9964) for the keys of
7
+ * ML-DSA, ML-KEM and SLH-DSA.
8
+ * - JWSs in the JWS Compact Serialization (RFC 7515) which are signed with
9
+ * ML-DSA (RFC 9964) or SLH-DSA.
10
+ *
11
+ * @module
12
+ */
13
+ export * from "./jwk.js";
14
+ export * from "./jws.js";
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Checks that "alg" of a JWK or of a JWS header is the one of the algorithm.
3
+ *
4
+ * @param params The JWK or the header, of which nothing is checked yet.
5
+ * @param alg The value of "alg" of the algorithm.
6
+ * @throws {JoseError} if "alg" is not the one of the algorithm.
7
+ */
8
+ export declare function checkAlg(params: Record<string, unknown>, alg: string): void;
9
+ //# sourceMappingURL=alg.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alg.d.ts","sourceRoot":"","sources":["../../src/src/alg.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,CAW3E"}