@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
@@ -0,0 +1,32 @@
1
+ // The algorithms are plain literals, with no expression to evaluate, so that
2
+ // a bundler drops the ones which an application does not use.
3
+ /**
4
+ * ML-DSA-44 (FIPS 204), as RFC 9964 represents its keys: "pub" is the public
5
+ * key (1312 octets), and "priv" is the 32-octet seed, which is the private
6
+ * key of `@webpqc/mldsa`.
7
+ */
8
+ export const ML_DSA_44 = {
9
+ alg: "ML-DSA-44",
10
+ pubSize: 1312,
11
+ privSize: 32,
12
+ };
13
+ /**
14
+ * ML-DSA-65 (FIPS 204), as RFC 9964 represents its keys: "pub" is the public
15
+ * key (1952 octets), and "priv" is the 32-octet seed, which is the private
16
+ * key of `@webpqc/mldsa`.
17
+ */
18
+ export const ML_DSA_65 = {
19
+ alg: "ML-DSA-65",
20
+ pubSize: 1952,
21
+ privSize: 32,
22
+ };
23
+ /**
24
+ * ML-DSA-87 (FIPS 204), as RFC 9964 represents its keys: "pub" is the public
25
+ * key (2592 octets), and "priv" is the 32-octet seed, which is the private
26
+ * key of `@webpqc/mldsa`.
27
+ */
28
+ export const ML_DSA_87 = {
29
+ alg: "ML-DSA-87",
30
+ pubSize: 2592,
31
+ privSize: 32,
32
+ };
@@ -0,0 +1,29 @@
1
+ import type { AkpAlgorithm } from "./types.js";
2
+ /**
3
+ * ML-KEM-512 (FIPS 203), as the Web Cryptography API represents its keys
4
+ * (Modern Algorithms in the Web Cryptography API): "pub" is the encapsulation
5
+ * key (800 octets), and "priv" is the 64-octet seed `d || z`, which is the
6
+ * private key of `@webpqc/mlkem`.
7
+ *
8
+ * The value of "alg" is not registered for JOSE yet.
9
+ */
10
+ export declare const ML_KEM_512: AkpAlgorithm<"ML-KEM-512">;
11
+ /**
12
+ * ML-KEM-768 (FIPS 203), as the Web Cryptography API represents its keys
13
+ * (Modern Algorithms in the Web Cryptography API): "pub" is the encapsulation
14
+ * key (1184 octets), and "priv" is the 64-octet seed `d || z`, which is the
15
+ * private key of `@webpqc/mlkem`.
16
+ *
17
+ * The value of "alg" is not registered for JOSE yet.
18
+ */
19
+ export declare const ML_KEM_768: AkpAlgorithm<"ML-KEM-768">;
20
+ /**
21
+ * ML-KEM-1024 (FIPS 203), as the Web Cryptography API represents its keys
22
+ * (Modern Algorithms in the Web Cryptography API): "pub" is the encapsulation
23
+ * key (1568 octets), and "priv" is the 64-octet seed `d || z`, which is the
24
+ * private key of `@webpqc/mlkem`.
25
+ *
26
+ * The value of "alg" is not registered for JOSE yet.
27
+ */
28
+ export declare const ML_KEM_1024: AkpAlgorithm<"ML-KEM-1024">;
29
+ //# sourceMappingURL=mlKem.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mlKem.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/mlKem.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,EAAE,YAAY,CAAC,YAAY,CAIjD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,EAAE,YAAY,CAAC,YAAY,CAIjD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,EAAE,YAAY,CAAC,aAAa,CAInD,CAAC"}
@@ -0,0 +1,41 @@
1
+ // The algorithms are plain literals, with no expression to evaluate, so that
2
+ // a bundler drops the ones which an application does not use.
3
+ /**
4
+ * ML-KEM-512 (FIPS 203), as the Web Cryptography API represents its keys
5
+ * (Modern Algorithms in the Web Cryptography API): "pub" is the encapsulation
6
+ * key (800 octets), and "priv" is the 64-octet seed `d || z`, which is the
7
+ * private key of `@webpqc/mlkem`.
8
+ *
9
+ * The value of "alg" is not registered for JOSE yet.
10
+ */
11
+ export const ML_KEM_512 = {
12
+ alg: "ML-KEM-512",
13
+ pubSize: 800,
14
+ privSize: 64,
15
+ };
16
+ /**
17
+ * ML-KEM-768 (FIPS 203), as the Web Cryptography API represents its keys
18
+ * (Modern Algorithms in the Web Cryptography API): "pub" is the encapsulation
19
+ * key (1184 octets), and "priv" is the 64-octet seed `d || z`, which is the
20
+ * private key of `@webpqc/mlkem`.
21
+ *
22
+ * The value of "alg" is not registered for JOSE yet.
23
+ */
24
+ export const ML_KEM_768 = {
25
+ alg: "ML-KEM-768",
26
+ pubSize: 1184,
27
+ privSize: 64,
28
+ };
29
+ /**
30
+ * ML-KEM-1024 (FIPS 203), as the Web Cryptography API represents its keys
31
+ * (Modern Algorithms in the Web Cryptography API): "pub" is the encapsulation
32
+ * key (1568 octets), and "priv" is the 64-octet seed `d || z`, which is the
33
+ * private key of `@webpqc/mlkem`.
34
+ *
35
+ * The value of "alg" is not registered for JOSE yet.
36
+ */
37
+ export const ML_KEM_1024 = {
38
+ alg: "ML-KEM-1024",
39
+ pubSize: 1568,
40
+ privSize: 64,
41
+ };
@@ -0,0 +1,51 @@
1
+ import type { AkpAlgorithm } from "./types.js";
2
+ /**
3
+ * SLH-DSA-SHAKE-128s (FIPS 205), as the Web Cryptography API (Modern
4
+ * Algorithms in the Web Cryptography API) and draft-ietf-cose-sphincs-plus
5
+ * represent its keys: "pub" is the public key (32 octets), and "priv" is the
6
+ * private key (64 octets).
7
+ *
8
+ * The value of "alg" is not registered for JOSE yet.
9
+ */
10
+ export declare const SLH_DSA_SHAKE_128S: AkpAlgorithm<"SLH-DSA-SHAKE-128s">;
11
+ /**
12
+ * SLH-DSA-SHAKE-128f (FIPS 205), as the Web Cryptography API represents its
13
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
14
+ * key (32 octets), and "priv" is the private key (64 octets).
15
+ *
16
+ * The value of "alg" is not registered for JOSE yet.
17
+ */
18
+ export declare const SLH_DSA_SHAKE_128F: AkpAlgorithm<"SLH-DSA-SHAKE-128f">;
19
+ /**
20
+ * SLH-DSA-SHAKE-192s (FIPS 205), as the Web Cryptography API represents its
21
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
22
+ * key (48 octets), and "priv" is the private key (96 octets).
23
+ *
24
+ * The value of "alg" is not registered for JOSE yet.
25
+ */
26
+ export declare const SLH_DSA_SHAKE_192S: AkpAlgorithm<"SLH-DSA-SHAKE-192s">;
27
+ /**
28
+ * SLH-DSA-SHAKE-192f (FIPS 205), as the Web Cryptography API represents its
29
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
30
+ * key (48 octets), and "priv" is the private key (96 octets).
31
+ *
32
+ * The value of "alg" is not registered for JOSE yet.
33
+ */
34
+ export declare const SLH_DSA_SHAKE_192F: AkpAlgorithm<"SLH-DSA-SHAKE-192f">;
35
+ /**
36
+ * SLH-DSA-SHAKE-256s (FIPS 205), as the Web Cryptography API represents its
37
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
38
+ * key (64 octets), and "priv" is the private key (128 octets).
39
+ *
40
+ * The value of "alg" is not registered for JOSE yet.
41
+ */
42
+ export declare const SLH_DSA_SHAKE_256S: AkpAlgorithm<"SLH-DSA-SHAKE-256s">;
43
+ /**
44
+ * SLH-DSA-SHAKE-256f (FIPS 205), as the Web Cryptography API represents its
45
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
46
+ * key (64 octets), and "priv" is the private key (128 octets).
47
+ *
48
+ * The value of "alg" is not registered for JOSE yet.
49
+ */
50
+ export declare const SLH_DSA_SHAKE_256F: AkpAlgorithm<"SLH-DSA-SHAKE-256f">;
51
+ //# sourceMappingURL=slhDsa.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slhDsa.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/slhDsa.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAO/C;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,oBAAoB,CAIjE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,oBAAoB,CAIjE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,oBAAoB,CAIjE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,oBAAoB,CAIjE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,oBAAoB,CAIjE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,oBAAoB,CAIjE,CAAC"}
@@ -0,0 +1,79 @@
1
+ // The algorithms are plain literals, with no expression to evaluate, so that
2
+ // a bundler drops the ones which an application does not use.
3
+ // "pub" is the public key `PK.seed || PK.root` (2n octets), and "priv" is the
4
+ // private key `SK.seed || SK.prf || PK.seed || PK.root` (4n octets), where n
5
+ // is 16, 24 or 32 for the NIST security category 1, 3 or 5 (FIPS 205,
6
+ // Table 2). They are the keys of `@webpqc/slhdsa`.
7
+ /**
8
+ * SLH-DSA-SHAKE-128s (FIPS 205), as the Web Cryptography API (Modern
9
+ * Algorithms in the Web Cryptography API) and draft-ietf-cose-sphincs-plus
10
+ * represent its keys: "pub" is the public key (32 octets), and "priv" is the
11
+ * private key (64 octets).
12
+ *
13
+ * The value of "alg" is not registered for JOSE yet.
14
+ */
15
+ export const SLH_DSA_SHAKE_128S = {
16
+ alg: "SLH-DSA-SHAKE-128s",
17
+ pubSize: 32,
18
+ privSize: 64,
19
+ };
20
+ /**
21
+ * SLH-DSA-SHAKE-128f (FIPS 205), as the Web Cryptography API represents its
22
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
23
+ * key (32 octets), and "priv" is the private key (64 octets).
24
+ *
25
+ * The value of "alg" is not registered for JOSE yet.
26
+ */
27
+ export const SLH_DSA_SHAKE_128F = {
28
+ alg: "SLH-DSA-SHAKE-128f",
29
+ pubSize: 32,
30
+ privSize: 64,
31
+ };
32
+ /**
33
+ * SLH-DSA-SHAKE-192s (FIPS 205), as the Web Cryptography API represents its
34
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
35
+ * key (48 octets), and "priv" is the private key (96 octets).
36
+ *
37
+ * The value of "alg" is not registered for JOSE yet.
38
+ */
39
+ export const SLH_DSA_SHAKE_192S = {
40
+ alg: "SLH-DSA-SHAKE-192s",
41
+ pubSize: 48,
42
+ privSize: 96,
43
+ };
44
+ /**
45
+ * SLH-DSA-SHAKE-192f (FIPS 205), as the Web Cryptography API represents its
46
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
47
+ * key (48 octets), and "priv" is the private key (96 octets).
48
+ *
49
+ * The value of "alg" is not registered for JOSE yet.
50
+ */
51
+ export const SLH_DSA_SHAKE_192F = {
52
+ alg: "SLH-DSA-SHAKE-192f",
53
+ pubSize: 48,
54
+ privSize: 96,
55
+ };
56
+ /**
57
+ * SLH-DSA-SHAKE-256s (FIPS 205), as the Web Cryptography API represents its
58
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
59
+ * key (64 octets), and "priv" is the private key (128 octets).
60
+ *
61
+ * The value of "alg" is not registered for JOSE yet.
62
+ */
63
+ export const SLH_DSA_SHAKE_256S = {
64
+ alg: "SLH-DSA-SHAKE-256s",
65
+ pubSize: 64,
66
+ privSize: 128,
67
+ };
68
+ /**
69
+ * SLH-DSA-SHAKE-256f (FIPS 205), as the Web Cryptography API represents its
70
+ * keys (Modern Algorithms in the Web Cryptography API): "pub" is the public
71
+ * key (64 octets), and "priv" is the private key (128 octets).
72
+ *
73
+ * The value of "alg" is not registered for JOSE yet.
74
+ */
75
+ export const SLH_DSA_SHAKE_256F = {
76
+ alg: "SLH-DSA-SHAKE-256f",
77
+ pubSize: 64,
78
+ privSize: 128,
79
+ };
@@ -0,0 +1,43 @@
1
+ import type { AkpAlgorithm } from "./types.js";
2
+ /**
3
+ * Options for {@link calculateJwkThumbprint}.
4
+ */
5
+ export interface JwkThumbprintOptions {
6
+ /**
7
+ * The hash function, by its name in the Web Cryptography API. Defaults to
8
+ * "SHA-256". No other hash function than these is accepted.
9
+ */
10
+ hash?: "SHA-256" | "SHA-384" | "SHA-512";
11
+ }
12
+ /**
13
+ * Computes the JWK Thumbprint (RFC 7638) of a public key, as the JWK of the
14
+ * key type "AKP" which {@link exportPublicJwk} gives for it.
15
+ *
16
+ * The hash is taken over the parameters "alg", "kty" and "pub" (RFC 9964,
17
+ * Section 6), with the Web Cryptography API, so a key pair has the
18
+ * thumbprint of its public key. It is commonly used as "kid".
19
+ *
20
+ * @param alg The algorithm of the key, such as {@link ML_DSA_65}.
21
+ * @param publicKey The public key.
22
+ * @param opts Options: see {@link JwkThumbprintOptions}.
23
+ * @returns A promise that resolves to the thumbprint, base64url encoded.
24
+ * @throws {JoseError} if the public key is not a Uint8Array of the size which
25
+ * the algorithm gives it, if the hash function is not one of
26
+ * {@link JwkThumbprintOptions}, or if "alg" has a character which JSON
27
+ * escapes, for which the thumbprint is not defined.
28
+ *
29
+ * @example
30
+ *
31
+ * ```ts
32
+ * import { calculateJwkThumbprint, ML_DSA_65, exportPublicJwk } from "@webpqc/jose";
33
+ * import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
34
+ *
35
+ * const [pk, _sk] = await generateMlDsa65KeyPair();
36
+ * const jwk = {
37
+ * ...exportPublicJwk(ML_DSA_65, pk),
38
+ * kid: await calculateJwkThumbprint(ML_DSA_65, pk),
39
+ * };
40
+ * ```
41
+ */
42
+ export declare function calculateJwkThumbprint(alg: AkpAlgorithm, publicKey: Uint8Array, opts?: JwkThumbprintOptions): Promise<string>;
43
+ //# sourceMappingURL=thumbprint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"thumbprint.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/thumbprint.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,IAAI,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;CAC1C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,YAAY,EACjB,SAAS,EAAE,UAAU,EACrB,IAAI,CAAC,EAAE,oBAAoB,GAC1B,OAAO,CAAC,MAAM,CAAC,CA0BjB"}
@@ -0,0 +1,56 @@
1
+ import { encodeBase64Url } from "../base64url/encode.js";
2
+ import { JoseError } from "../errors.js";
3
+ import { encodeKey } from "./key.js";
4
+ /**
5
+ * Computes the JWK Thumbprint (RFC 7638) of a public key, as the JWK of the
6
+ * key type "AKP" which {@link exportPublicJwk} gives for it.
7
+ *
8
+ * The hash is taken over the parameters "alg", "kty" and "pub" (RFC 9964,
9
+ * Section 6), with the Web Cryptography API, so a key pair has the
10
+ * thumbprint of its public key. It is commonly used as "kid".
11
+ *
12
+ * @param alg The algorithm of the key, such as {@link ML_DSA_65}.
13
+ * @param publicKey The public key.
14
+ * @param opts Options: see {@link JwkThumbprintOptions}.
15
+ * @returns A promise that resolves to the thumbprint, base64url encoded.
16
+ * @throws {JoseError} if the public key is not a Uint8Array of the size which
17
+ * the algorithm gives it, if the hash function is not one of
18
+ * {@link JwkThumbprintOptions}, or if "alg" has a character which JSON
19
+ * escapes, for which the thumbprint is not defined.
20
+ *
21
+ * @example
22
+ *
23
+ * ```ts
24
+ * import { calculateJwkThumbprint, ML_DSA_65, exportPublicJwk } from "@webpqc/jose";
25
+ * import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
26
+ *
27
+ * const [pk, _sk] = await generateMlDsa65KeyPair();
28
+ * const jwk = {
29
+ * ...exportPublicJwk(ML_DSA_65, pk),
30
+ * kid: await calculateJwkThumbprint(ML_DSA_65, pk),
31
+ * };
32
+ * ```
33
+ */
34
+ export async function calculateJwkThumbprint(alg, publicKey, opts) {
35
+ // The Web Cryptography API has other hash functions, such as SHA-1, and
36
+ // takes their names in any case.
37
+ const hash = opts?.hash ?? "SHA-256";
38
+ if (hash !== "SHA-256" && hash !== "SHA-384" && hash !== "SHA-512") {
39
+ throw new JoseError('hash must be "SHA-256", "SHA-384" or "SHA-512"');
40
+ }
41
+ // The values are hashed as they are, without being escaped, so there is no
42
+ // thumbprint for a value which cannot be in JSON without it: one with a
43
+ // quotation mark, a backslash or a control character (RFC 7638, Section
44
+ // 3.3), or with a surrogate which is not one of a pair. The type is checked
45
+ // first: a String object would pass the comparison, and a symbol or a
46
+ // bigint would throw another error in it.
47
+ const name = alg.alg;
48
+ if (typeof name !== "string" || JSON.stringify(name) !== `"${name}"`) {
49
+ throw new JoseError('"alg" must be a string which JSON does not escape');
50
+ }
51
+ // The required parameters in the lexicographic order of their names, with
52
+ // no white space (RFC 7638, Section 3.3).
53
+ const json = `{"alg":"${name}","kty":"AKP","pub":"${encodeKey(publicKey, alg.pubSize, "publicKey")}"}`;
54
+ const digest = new Uint8Array(await crypto.subtle.digest(hash, new TextEncoder().encode(json)));
55
+ return encodeBase64Url(digest, digest.length);
56
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * An algorithm whose keys are represented as JWKs of the key type "AKP"
3
+ * (Algorithm Key Pair), which RFC 9964 defines.
4
+ *
5
+ * It is all that the functions of this package know about an algorithm:
6
+ * they take it as their first argument, and the package has no table of the
7
+ * algorithms, so an application bundles only the ones which it names.
8
+ *
9
+ * The algorithms of this package, such as {@link ML_DSA_65}, are constants of
10
+ * this type. One for another algorithm, or for another value of `alg` of the
11
+ * same keys, is a plain object as well:
12
+ *
13
+ * ```ts
14
+ * import { type AkpAlgorithm, exportPublicJwk } from "@webpqc/jose";
15
+ *
16
+ * const SLH_DSA_SHA2_128S: AkpAlgorithm<"SLH-DSA-SHA2-128s"> = {
17
+ * alg: "SLH-DSA-SHA2-128s",
18
+ * pubSize: 32,
19
+ * privSize: 64,
20
+ * };
21
+ *
22
+ * const jwk = exportPublicJwk(SLH_DSA_SHA2_128S, new Uint8Array(32));
23
+ * // jwk.alg === "SLH-DSA-SHA2-128s"
24
+ * ```
25
+ */
26
+ export interface AkpAlgorithm<A extends string = string> {
27
+ /** The value of the JWK parameter "alg". */
28
+ readonly alg: A;
29
+ /** The size of the public key (the JWK parameter "pub") in octets. */
30
+ readonly pubSize: number;
31
+ /** The size of the private key (the JWK parameter "priv") in octets. */
32
+ readonly privSize: number;
33
+ }
34
+ /**
35
+ * The parameters of a JWK which do not depend on the key type, as far as a
36
+ * JWK of this package may have them. The functions of this package neither
37
+ * set nor check them.
38
+ */
39
+ export interface JwkCommonParameters {
40
+ /** The key ID. */
41
+ kid?: string;
42
+ /** The intended use of the public key: "sig" or "enc". */
43
+ use?: string;
44
+ /** The operations which the key is intended to be used for. */
45
+ key_ops?: string[];
46
+ /** Whether the key is extractable, as the Web Cryptography API sets it. */
47
+ ext?: boolean;
48
+ }
49
+ /**
50
+ * A public key as a JWK of the key type "AKP" (RFC 9964, Section 3).
51
+ */
52
+ export interface AkpPublicJwk<A extends string = string> extends JwkCommonParameters {
53
+ /** The key type. */
54
+ kty: "AKP";
55
+ /** The algorithm which the key is for. */
56
+ alg: A;
57
+ /** The public key, base64url encoded. */
58
+ pub: string;
59
+ /** A public key has no private key. */
60
+ priv?: never;
61
+ }
62
+ /**
63
+ * A key pair as a JWK of the key type "AKP" (RFC 9964, Section 3), which has
64
+ * the private key together with the public key.
65
+ */
66
+ export interface AkpPrivateJwk<A extends string = string> extends JwkCommonParameters {
67
+ /** The key type. */
68
+ kty: "AKP";
69
+ /** The algorithm which the key is for. */
70
+ alg: A;
71
+ /** The public key, base64url encoded. */
72
+ pub: string;
73
+ /** The private key, base64url encoded. */
74
+ priv: string;
75
+ }
76
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;IACrD,4CAA4C;IAC5C,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC;IAChB,sEAAsE;IACtE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,wEAAwE;IACxE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AAED;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,kBAAkB;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,0DAA0D;IAC1D,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,2EAA2E;IAC3E,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CACrD,SAAQ,mBAAmB;IAC3B,oBAAoB;IACpB,GAAG,EAAE,KAAK,CAAC;IACX,0CAA0C;IAC1C,GAAG,EAAE,CAAC,CAAC;IACP,yCAAyC;IACzC,GAAG,EAAE,MAAM,CAAC;IACZ,uCAAuC;IACvC,IAAI,CAAC,EAAE,KAAK,CAAC;CACd;AAED;;;GAGG;AACH,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CACtD,SAAQ,mBAAmB;IAC3B,oBAAoB;IACpB,GAAG,EAAE,KAAK,CAAC;IACX,0CAA0C;IAC1C,GAAG,EAAE,CAAC,CAAC;IACP,yCAAyC;IACzC,GAAG,EAAE,MAAM,CAAC;IACZ,0CAA0C;IAC1C,IAAI,EAAE,MAAM,CAAC;CACd"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Rejects a header which tells that the payload is not base64url encoded
3
+ * (the header parameter "b64" of RFC 7797), which this package does not
4
+ * support.
5
+ *
6
+ * The signature of such a JWS is over the payload as it is, so a JWS which
7
+ * has it must not be taken for one of an encoded payload, nor be made with
8
+ * one: the payload would be another (RFC 7797, Section 8).
9
+ *
10
+ * @param header The JWS Protected Header.
11
+ * @throws {JoseError} if "b64" is present and is not true.
12
+ */
13
+ export declare function checkB64(header: Record<string, unknown>): void;
14
+ //# sourceMappingURL=b64.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"b64.d.ts","sourceRoot":"","sources":["../../../src/src/jws/b64.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAI9D"}
@@ -0,0 +1,18 @@
1
+ import { JoseError } from "../errors.js";
2
+ /**
3
+ * Rejects a header which tells that the payload is not base64url encoded
4
+ * (the header parameter "b64" of RFC 7797), which this package does not
5
+ * support.
6
+ *
7
+ * The signature of such a JWS is over the payload as it is, so a JWS which
8
+ * has it must not be taken for one of an encoded payload, nor be made with
9
+ * one: the payload would be another (RFC 7797, Section 8).
10
+ *
11
+ * @param header The JWS Protected Header.
12
+ * @throws {JoseError} if "b64" is present and is not true.
13
+ */
14
+ export function checkB64(header) {
15
+ if (header.b64 !== undefined && header.b64 !== true) {
16
+ throw new JoseError('"b64" must be true if present');
17
+ }
18
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Decodes the JWS Protected Header of a JWS in the JWS Compact Serialization
3
+ * (RFC 7515), without verifying the signature.
4
+ *
5
+ * It is for what has to be known before the JWS is verified: which key it
6
+ * is signed with ("kid"), and with which algorithm ("alg"). The header is
7
+ * not to be trusted otherwise: it is whatever the one who made the JWS put
8
+ * in it, and {@link verifyJws} gives it again once the signature is
9
+ * verified.
10
+ *
11
+ * It checks only that the JWS has three parts, and that the first one is the
12
+ * base64url encoding of a JSON object in UTF-8. No parameter is checked, so
13
+ * check the type of one before using it.
14
+ *
15
+ * @param jws The JWS.
16
+ * @returns The JWS Protected Header.
17
+ * @throws {JoseError} if the JWS has no such header.
18
+ *
19
+ * @example
20
+ *
21
+ * ```ts
22
+ * import { decodeJwsHeader, verifyJws } from "@webpqc/jose";
23
+ * import { createMlDsa65Verifier } from "@webpqc/mldsa";
24
+ *
25
+ * async function verify(keys: Map<string, Uint8Array>, jws: string) {
26
+ * const { kid } = decodeJwsHeader(jws);
27
+ * const pk = typeof kid === "string" ? keys.get(kid) : undefined;
28
+ * if (pk === undefined) {
29
+ * throw new Error("unknown key");
30
+ * }
31
+ * const verifier = await createMlDsa65Verifier(pk);
32
+ * return await verifyJws(verifier, jws);
33
+ * }
34
+ * ```
35
+ */
36
+ export declare function decodeJwsHeader(jws: string): Record<string, unknown>;
37
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../src/src/jws/decode.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEpE"}
@@ -0,0 +1,39 @@
1
+ import { headerOf, partsOf } from "./parse.js";
2
+ /**
3
+ * Decodes the JWS Protected Header of a JWS in the JWS Compact Serialization
4
+ * (RFC 7515), without verifying the signature.
5
+ *
6
+ * It is for what has to be known before the JWS is verified: which key it
7
+ * is signed with ("kid"), and with which algorithm ("alg"). The header is
8
+ * not to be trusted otherwise: it is whatever the one who made the JWS put
9
+ * in it, and {@link verifyJws} gives it again once the signature is
10
+ * verified.
11
+ *
12
+ * It checks only that the JWS has three parts, and that the first one is the
13
+ * base64url encoding of a JSON object in UTF-8. No parameter is checked, so
14
+ * check the type of one before using it.
15
+ *
16
+ * @param jws The JWS.
17
+ * @returns The JWS Protected Header.
18
+ * @throws {JoseError} if the JWS has no such header.
19
+ *
20
+ * @example
21
+ *
22
+ * ```ts
23
+ * import { decodeJwsHeader, verifyJws } from "@webpqc/jose";
24
+ * import { createMlDsa65Verifier } from "@webpqc/mldsa";
25
+ *
26
+ * async function verify(keys: Map<string, Uint8Array>, jws: string) {
27
+ * const { kid } = decodeJwsHeader(jws);
28
+ * const pk = typeof kid === "string" ? keys.get(kid) : undefined;
29
+ * if (pk === undefined) {
30
+ * throw new Error("unknown key");
31
+ * }
32
+ * const verifier = await createMlDsa65Verifier(pk);
33
+ * return await verifyJws(verifier, jws);
34
+ * }
35
+ * ```
36
+ */
37
+ export function decodeJwsHeader(jws) {
38
+ return headerOf(partsOf(jws)[0]);
39
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Splits a JWS in the JWS Compact Serialization (RFC 7515, Section 7.1) into
3
+ * its three parts, which are the header, the payload and the signature, each
4
+ * still encoded.
5
+ *
6
+ * @param jws The JWS, which may be anything.
7
+ * @throws {JoseError} if it is not a string of three parts.
8
+ */
9
+ export declare function partsOf(jws: unknown): string[];
10
+ /**
11
+ * Decodes the JWS Protected Header: the first part of a JWS.
12
+ *
13
+ * @param encoded The header as it is in the JWS.
14
+ * @returns The header, of which no parameter is checked.
15
+ * @throws {JoseError} if it is not the base64url encoding of a JSON object
16
+ * in UTF-8.
17
+ */
18
+ export declare function headerOf(encoded: string): Record<string, unknown>;
19
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../src/src/jws/parse.ts"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,EAAE,CAO9C;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAoBjE"}
@@ -0,0 +1,46 @@
1
+ // What reads a JWS and what writes one are separate modules, as the ones for
2
+ // JWK are: an application which only verifies does not bundle what signs.
3
+ import { decodeBase64Url } from "../base64url/decode.js";
4
+ import { JoseError } from "../errors.js";
5
+ /**
6
+ * Splits a JWS in the JWS Compact Serialization (RFC 7515, Section 7.1) into
7
+ * its three parts, which are the header, the payload and the signature, each
8
+ * still encoded.
9
+ *
10
+ * @param jws The JWS, which may be anything.
11
+ * @throws {JoseError} if it is not a string of three parts.
12
+ */
13
+ export function partsOf(jws) {
14
+ // No more than four parts are made out of it, however many it has.
15
+ const parts = typeof jws === "string" ? jws.split(".", 4) : [];
16
+ if (parts.length !== 3) {
17
+ throw new JoseError("jws must be a string of three parts");
18
+ }
19
+ return parts;
20
+ }
21
+ /**
22
+ * Decodes the JWS Protected Header: the first part of a JWS.
23
+ *
24
+ * @param encoded The header as it is in the JWS.
25
+ * @returns The header, of which no parameter is checked.
26
+ * @throws {JoseError} if it is not the base64url encoding of a JSON object
27
+ * in UTF-8.
28
+ */
29
+ export function headerOf(encoded) {
30
+ let header;
31
+ try {
32
+ // What is not base64url is decoded to nothing, which is not JSON either.
33
+ // With `fatal`, octets which are not UTF-8 are thrown for, and not
34
+ // replaced: two values which differ in them, e.g. of "kid", would be
35
+ // taken for the same. With `ignoreBOM`, a byte order mark is left where
36
+ // it is, which is not JSON.
37
+ header = JSON.parse(new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(decodeBase64Url(encoded)));
38
+ }
39
+ catch {
40
+ // Not base64url, not UTF-8 or not JSON: rejected below.
41
+ }
42
+ if (typeof header !== "object" || header === null || Array.isArray(header)) {
43
+ throw new JoseError("the header must be a JSON object in base64url");
44
+ }
45
+ return header;
46
+ }