@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.
- package/LICENSE +21 -0
- package/README.md +434 -2
- package/esm/_dnt.test_polyfills.d.ts.map +1 -0
- package/esm/jwk.d.ts +37 -0
- package/esm/jwk.d.ts.map +1 -0
- package/esm/jwk.js +34 -0
- package/esm/jws.d.ts +33 -0
- package/esm/jws.d.ts.map +1 -0
- package/esm/jws.js +31 -0
- package/esm/mod.d.ts +15 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +14 -0
- package/esm/package.json +3 -0
- package/esm/src/alg.d.ts +9 -0
- package/esm/src/alg.d.ts.map +1 -0
- package/esm/src/alg.js +20 -0
- package/esm/src/base64url/decode.d.ts +13 -0
- package/esm/src/base64url/decode.d.ts.map +1 -0
- package/esm/src/base64url/decode.js +61 -0
- package/esm/src/base64url/encode.d.ts +9 -0
- package/esm/src/base64url/encode.d.ts.map +1 -0
- package/esm/src/base64url/encode.js +51 -0
- package/esm/src/base64url/length.d.ts +5 -0
- package/esm/src/base64url/length.d.ts.map +1 -0
- package/esm/src/base64url/length.js +6 -0
- package/esm/src/bytes.d.ts +13 -0
- package/esm/src/bytes.d.ts.map +1 -0
- package/esm/src/bytes.js +21 -0
- package/esm/src/errors.d.ts +8 -0
- package/esm/src/errors.d.ts.map +1 -0
- package/esm/src/errors.js +11 -0
- package/esm/src/jwk/decode.d.ts +72 -0
- package/esm/src/jwk/decode.d.ts.map +1 -0
- package/esm/src/jwk/decode.js +116 -0
- package/esm/src/jwk/encode.d.ts +57 -0
- package/esm/src/jwk/encode.d.ts.map +1 -0
- package/esm/src/jwk/encode.js +69 -0
- package/esm/src/jwk/key.d.ts +11 -0
- package/esm/src/jwk/key.d.ts.map +1 -0
- package/esm/src/jwk/key.js +18 -0
- package/esm/src/jwk/mlDsa.d.ts +20 -0
- package/esm/src/jwk/mlDsa.d.ts.map +1 -0
- package/esm/src/jwk/mlDsa.js +32 -0
- package/esm/src/jwk/mlKem.d.ts +29 -0
- package/esm/src/jwk/mlKem.d.ts.map +1 -0
- package/esm/src/jwk/mlKem.js +41 -0
- package/esm/src/jwk/slhDsa.d.ts +51 -0
- package/esm/src/jwk/slhDsa.d.ts.map +1 -0
- package/esm/src/jwk/slhDsa.js +79 -0
- package/esm/src/jwk/thumbprint.d.ts +43 -0
- package/esm/src/jwk/thumbprint.d.ts.map +1 -0
- package/esm/src/jwk/thumbprint.js +56 -0
- package/esm/src/jwk/types.d.ts +76 -0
- package/esm/src/jwk/types.d.ts.map +1 -0
- package/esm/src/jwk/types.js +1 -0
- package/esm/src/jws/b64.d.ts +14 -0
- package/esm/src/jws/b64.d.ts.map +1 -0
- package/esm/src/jws/b64.js +18 -0
- package/esm/src/jws/decode.d.ts +37 -0
- package/esm/src/jws/decode.d.ts.map +1 -0
- package/esm/src/jws/decode.js +39 -0
- package/esm/src/jws/parse.d.ts +19 -0
- package/esm/src/jws/parse.d.ts.map +1 -0
- package/esm/src/jws/parse.js +46 -0
- package/esm/src/jws/sign.d.ts +43 -0
- package/esm/src/jws/sign.d.ts.map +1 -0
- package/esm/src/jws/sign.js +106 -0
- package/esm/src/jws/types.d.ts +136 -0
- package/esm/src/jws/types.d.ts.map +1 -0
- package/esm/src/jws/types.js +1 -0
- package/esm/src/jws/verify.d.ts +45 -0
- package/esm/src/jws/verify.d.ts.map +1 -0
- package/esm/src/jws/verify.js +75 -0
- package/package.json +57 -4
package/esm/src/alg.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { JoseError } from "./errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Checks that "alg" of a JWK or of a JWS header is the one of the algorithm.
|
|
4
|
+
*
|
|
5
|
+
* @param params The JWK or the header, of which nothing is checked yet.
|
|
6
|
+
* @param alg The value of "alg" of the algorithm.
|
|
7
|
+
* @throws {JoseError} if "alg" is not the one of the algorithm.
|
|
8
|
+
*/
|
|
9
|
+
export function checkAlg(params, alg) {
|
|
10
|
+
// One without "alg" is rejected whatever the algorithm is given as: an
|
|
11
|
+
// algorithm which has no name, from a caller without the types, would be
|
|
12
|
+
// equal to it.
|
|
13
|
+
const name = params.alg;
|
|
14
|
+
if (typeof name !== "string") {
|
|
15
|
+
throw new JoseError('"alg" must be a string');
|
|
16
|
+
}
|
|
17
|
+
if (name !== alg) {
|
|
18
|
+
throw new JoseError(`"alg" must be "${alg}"`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decodes a base64url string without padding (RFC 7515, Section 2).
|
|
3
|
+
*
|
|
4
|
+
* It accepts nothing but the one encoding which the encoder gives for the
|
|
5
|
+
* octets: no padding, no character out of the alphabet (so no white space),
|
|
6
|
+
* and no bit set in the last character which is not a part of the octets.
|
|
7
|
+
* Two strings therefore never decode to the same octets.
|
|
8
|
+
*
|
|
9
|
+
* @param encoded The string to decode.
|
|
10
|
+
* @returns The octets, or `undefined` if the string is not valid.
|
|
11
|
+
*/
|
|
12
|
+
export declare function decodeBase64Url(encoded: string): Uint8Array | undefined;
|
|
13
|
+
//# sourceMappingURL=decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../src/src/base64url/decode.ts"],"names":[],"mappings":"AAoBA;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CA+BvE"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// See the comment at the top of ./encode.ts.
|
|
2
|
+
/**
|
|
3
|
+
* Maps the code of a character in the URL and filename safe alphabet
|
|
4
|
+
* (RFC 4648, Section 5) to its 6-bit value, or to -1 for any other character.
|
|
5
|
+
*
|
|
6
|
+
* As the encoder, it is computed without a table and without a branch.
|
|
7
|
+
*/
|
|
8
|
+
function valueOf(c) {
|
|
9
|
+
// (lo - c) & (c - hi) is negative if and only if lo < c < hi, and >> 31
|
|
10
|
+
// makes -1 out of it, and 0 out of anything else. c is a UTF-16 code unit,
|
|
11
|
+
// or NaN beyond the end of the string, which the operators take for 0.
|
|
12
|
+
return -1 +
|
|
13
|
+
((((64 - c) & (c - 91)) >> 31) & (c - 64)) + // "A"-"Z": c - 65
|
|
14
|
+
((((96 - c) & (c - 123)) >> 31) & (c - 70)) + // "a"-"z": c - 71
|
|
15
|
+
((((47 - c) & (c - 58)) >> 31) & (c + 5)) + // "0"-"9": c + 4
|
|
16
|
+
((((44 - c) & (c - 46)) >> 31) & 63) + // "-": 62
|
|
17
|
+
((((94 - c) & (c - 96)) >> 31) & 64); // "_": 63
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Decodes a base64url string without padding (RFC 7515, Section 2).
|
|
21
|
+
*
|
|
22
|
+
* It accepts nothing but the one encoding which the encoder gives for the
|
|
23
|
+
* octets: no padding, no character out of the alphabet (so no white space),
|
|
24
|
+
* and no bit set in the last character which is not a part of the octets.
|
|
25
|
+
* Two strings therefore never decode to the same octets.
|
|
26
|
+
*
|
|
27
|
+
* @param encoded The string to decode.
|
|
28
|
+
* @returns The octets, or `undefined` if the string is not valid.
|
|
29
|
+
*/
|
|
30
|
+
export function decodeBase64Url(encoded) {
|
|
31
|
+
const m = encoded.length;
|
|
32
|
+
// 3 octets are 4 characters, 2 are 3, and 1 is 2: nothing is 1 character.
|
|
33
|
+
if (m % 4 === 1) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const out = new Uint8Array(Math.floor(m * 3 / 4));
|
|
37
|
+
let acc = 0;
|
|
38
|
+
let bits = 0;
|
|
39
|
+
let o = 0;
|
|
40
|
+
let invalid = 0;
|
|
41
|
+
for (let i = 0; i < m; i++) {
|
|
42
|
+
const v = valueOf(encoded.charCodeAt(i));
|
|
43
|
+
// Negative from the first invalid character on.
|
|
44
|
+
invalid |= v;
|
|
45
|
+
acc = (acc << 6) | (v & 63);
|
|
46
|
+
bits += 6;
|
|
47
|
+
// Whether it runs depends on the position alone.
|
|
48
|
+
if (bits >= 8) {
|
|
49
|
+
bits -= 8;
|
|
50
|
+
out[o++] = acc >> bits;
|
|
51
|
+
acc &= (1 << bits) - 1;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// What is left in acc are the bits of the last character which are not a
|
|
55
|
+
// part of the octets.
|
|
56
|
+
if (invalid < 0 || acc !== 0) {
|
|
57
|
+
out.fill(0);
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encodes the first `n` octets of `bytes` to base64url without padding
|
|
3
|
+
* (RFC 7515, Section 2).
|
|
4
|
+
*
|
|
5
|
+
* @param bytes The octets to encode.
|
|
6
|
+
* @param n The number of the octets, which is the caller's to validate.
|
|
7
|
+
*/
|
|
8
|
+
export declare function encodeBase64Url(bytes: Uint8Array, n: number): string;
|
|
9
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../../src/src/base64url/encode.ts"],"names":[],"mappings":"AAwBA;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAsBpE"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// The encoder and the decoder are separate modules, and have nothing but
|
|
2
|
+
// function declarations: an application which only reads JWKs does not bundle
|
|
3
|
+
// the encoder, and the one which only writes them the decoder.
|
|
4
|
+
import { encodedLength } from "./length.js";
|
|
5
|
+
/**
|
|
6
|
+
* Maps a 6-bit value to the code of its character in the URL and filename
|
|
7
|
+
* safe alphabet (RFC 4648, Section 5).
|
|
8
|
+
*
|
|
9
|
+
* It is computed without a table and without a branch, so that neither a
|
|
10
|
+
* memory access nor a jump depends on the value, which may be a part of a
|
|
11
|
+
* private key.
|
|
12
|
+
*/
|
|
13
|
+
function charCodeOf(v) {
|
|
14
|
+
// "A"-"Z" are 65 + v. The others are shifted when v is beyond the range
|
|
15
|
+
// before them: (x - v) >> 8 is -1 if v > x, and 0 otherwise.
|
|
16
|
+
return v + 65 +
|
|
17
|
+
(((25 - v) >> 8) & 6) - // "a"-"z": 71 + v
|
|
18
|
+
(((51 - v) >> 8) & 75) - // "0"-"9": v - 4
|
|
19
|
+
(((61 - v) >> 8) & 13) + // "-": v - 17
|
|
20
|
+
(((62 - v) >> 8) & 49); // "_": v + 32
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Encodes the first `n` octets of `bytes` to base64url without padding
|
|
24
|
+
* (RFC 7515, Section 2).
|
|
25
|
+
*
|
|
26
|
+
* @param bytes The octets to encode.
|
|
27
|
+
* @param n The number of the octets, which is the caller's to validate.
|
|
28
|
+
*/
|
|
29
|
+
export function encodeBase64Url(bytes, n) {
|
|
30
|
+
const out = new Uint8Array(encodedLength(n));
|
|
31
|
+
let acc = 0;
|
|
32
|
+
let bits = 0;
|
|
33
|
+
let o = 0;
|
|
34
|
+
for (let i = 0; i < n; i++) {
|
|
35
|
+
acc = (acc << 8) | bytes[i];
|
|
36
|
+
bits += 8;
|
|
37
|
+
// Whether it runs once or twice depends on the position alone.
|
|
38
|
+
while (bits >= 6) {
|
|
39
|
+
bits -= 6;
|
|
40
|
+
out[o++] = charCodeOf((acc >> bits) & 63);
|
|
41
|
+
}
|
|
42
|
+
acc &= (1 << bits) - 1;
|
|
43
|
+
}
|
|
44
|
+
if (bits > 0) {
|
|
45
|
+
out[o] = charCodeOf((acc << (6 - bits)) & 63);
|
|
46
|
+
}
|
|
47
|
+
const encoded = new TextDecoder().decode(out);
|
|
48
|
+
// The string cannot be erased, but this copy of it can.
|
|
49
|
+
out.fill(0);
|
|
50
|
+
return encoded;
|
|
51
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"length.d.ts","sourceRoot":"","sources":["../../../src/src/base64url/length.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE/C"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the number of the elements of a Uint8Array, or -1 for anything
|
|
3
|
+
* else.
|
|
4
|
+
*
|
|
5
|
+
* Both are read with the getters of %TypedArray%.prototype rather than from
|
|
6
|
+
* the value. Unlike `instanceof`, they identify a Uint8Array which was
|
|
7
|
+
* created in another realm (an iframe or node:vm), and a subclass cannot
|
|
8
|
+
* override them, so the number is the one of the elements which are really
|
|
9
|
+
* there. Another type such as ArrayBuffer has `byteLength` but no elements to
|
|
10
|
+
* read, so it would be encoded as zeros.
|
|
11
|
+
*/
|
|
12
|
+
export declare function sizeOfBytes(value: unknown): number;
|
|
13
|
+
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/src/bytes.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAOlD"}
|
package/esm/src/bytes.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// This module has nothing but function declarations, so that a bundler drops
|
|
2
|
+
// what an application does not use without having to prove that an
|
|
3
|
+
// expression at the top level has no side effect.
|
|
4
|
+
/**
|
|
5
|
+
* Returns the number of the elements of a Uint8Array, or -1 for anything
|
|
6
|
+
* else.
|
|
7
|
+
*
|
|
8
|
+
* Both are read with the getters of %TypedArray%.prototype rather than from
|
|
9
|
+
* the value. Unlike `instanceof`, they identify a Uint8Array which was
|
|
10
|
+
* created in another realm (an iframe or node:vm), and a subclass cannot
|
|
11
|
+
* override them, so the number is the one of the elements which are really
|
|
12
|
+
* there. Another type such as ArrayBuffer has `byteLength` but no elements to
|
|
13
|
+
* read, so it would be encoded as zeros.
|
|
14
|
+
*/
|
|
15
|
+
export function sizeOfBytes(value) {
|
|
16
|
+
const proto = Object.getPrototypeOf(Uint8Array.prototype);
|
|
17
|
+
const get = (name) => Object.getOwnPropertyDescriptor(proto, name).get.call(value);
|
|
18
|
+
return get(Symbol.toStringTag) === "Uint8Array"
|
|
19
|
+
? get("length")
|
|
20
|
+
: -1;
|
|
21
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error which the functions of this package throw for an invalid
|
|
3
|
+
* argument, such as a JWK which is not the one of the algorithm.
|
|
4
|
+
*/
|
|
5
|
+
export declare class JoseError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/src/errors.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,SAAU,SAAQ,KAAK;gBACf,OAAO,EAAE,MAAM;CAKnC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error which the functions of this package throw for an invalid
|
|
3
|
+
* argument, such as a JWK which is not the one of the algorithm.
|
|
4
|
+
*/
|
|
5
|
+
export class JoseError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
// A literal, not `this.constructor.name`, which a minifier renames.
|
|
9
|
+
this.name = "JoseError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { AkpAlgorithm } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Takes the public key out of a JWK of the key type "AKP" (RFC 9964) which
|
|
4
|
+
* is a public key.
|
|
5
|
+
*
|
|
6
|
+
* It checks "kty", "alg", and that "pub" is the base64url encoding, with no
|
|
7
|
+
* padding and no other character, of as many octets as the public key of the
|
|
8
|
+
* algorithm has. A JWK with "priv" is rejected, as it is not a public key:
|
|
9
|
+
* use {@link importPrivateJwk} for it. The other parameters, such as "use" and
|
|
10
|
+
* "key_ops", are not checked, and neither is the public key itself, which is
|
|
11
|
+
* left to the one which uses it, e.g. `createMlDsa65Verifier`.
|
|
12
|
+
*
|
|
13
|
+
* @param alg The algorithm which the key has to be for, such as
|
|
14
|
+
* {@link ML_DSA_65}.
|
|
15
|
+
* @param jwk The JWK, e.g. the result of `JSON.parse`.
|
|
16
|
+
* @returns The public key.
|
|
17
|
+
* @throws {JoseError} if the JWK is not a public key of the algorithm.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { ML_DSA_65, importPublicJwk } from "@webpqc/jose";
|
|
23
|
+
* import { createMlDsa65Verifier } from "@webpqc/mldsa";
|
|
24
|
+
*
|
|
25
|
+
* async function verify(jwk: unknown, sig: Uint8Array, msg: Uint8Array) {
|
|
26
|
+
* const verifier = await createMlDsa65Verifier(
|
|
27
|
+
* importPublicJwk(ML_DSA_65, jwk),
|
|
28
|
+
* );
|
|
29
|
+
* return await verifier.verify(sig, msg);
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function importPublicJwk(alg: AkpAlgorithm, jwk: unknown): Uint8Array;
|
|
34
|
+
/**
|
|
35
|
+
* Takes the private key out of a JWK of the key type "AKP" (RFC 9964) which
|
|
36
|
+
* has it.
|
|
37
|
+
*
|
|
38
|
+
* It checks "kty", "alg", and that "pub" and "priv" are the base64url
|
|
39
|
+
* encodings, with no padding and no other character, of as many octets as
|
|
40
|
+
* the keys of the algorithm have. The other parameters, such as "use" and
|
|
41
|
+
* "key_ops", are not checked.
|
|
42
|
+
*
|
|
43
|
+
* "pub" is checked because the key type requires it, but it is not returned:
|
|
44
|
+
* only the algorithm itself can tell that it is the public key of the private
|
|
45
|
+
* key, and a public key which is not would be of no use, or worse (RFC 9964,
|
|
46
|
+
* Section 7.4). The public key of the private key is the one which the
|
|
47
|
+
* algorithm derives from it, e.g. with `deriveMlDsa65KeyPair`.
|
|
48
|
+
*
|
|
49
|
+
* @param alg The algorithm which the key has to be for, such as
|
|
50
|
+
* {@link ML_DSA_65}.
|
|
51
|
+
* @param jwk The JWK, e.g. the result of `JSON.parse`.
|
|
52
|
+
* @returns The private key.
|
|
53
|
+
* @throws {JoseError} if the JWK is not a private key of the algorithm.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { ML_DSA_65, importPrivateJwk } from "@webpqc/jose";
|
|
59
|
+
* import { createMlDsa65Signer } from "@webpqc/mldsa";
|
|
60
|
+
*
|
|
61
|
+
* async function sign(jwk: unknown, msg: Uint8Array) {
|
|
62
|
+
* const sk = importPrivateJwk(ML_DSA_65, jwk);
|
|
63
|
+
* const signer = await createMlDsa65Signer(sk);
|
|
64
|
+
* sk.fill(0);
|
|
65
|
+
* const sig = await signer.sign(msg);
|
|
66
|
+
* signer.destroy();
|
|
67
|
+
* return sig;
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function importPrivateJwk(alg: AkpAlgorithm, jwk: unknown): Uint8Array;
|
|
72
|
+
//# sourceMappingURL=decode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/decode.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAoC/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,OAAO,GACX,UAAU,CAMZ;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,YAAY,EACjB,GAAG,EAAE,OAAO,GACX,UAAU,CAIZ"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { checkAlg } from "../alg.js";
|
|
2
|
+
import { decodeBase64Url } from "../base64url/decode.js";
|
|
3
|
+
import { encodedLength } from "../base64url/length.js";
|
|
4
|
+
import { JoseError } from "../errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* Checks that a value is a JWK of the key type "AKP" for the algorithm, and
|
|
7
|
+
* returns it as an object whose parameters are still to be checked.
|
|
8
|
+
*/
|
|
9
|
+
function akpOf(alg, jwk) {
|
|
10
|
+
if (typeof jwk !== "object" || jwk === null) {
|
|
11
|
+
throw new JoseError("jwk must be an object");
|
|
12
|
+
}
|
|
13
|
+
const params = jwk;
|
|
14
|
+
if (params.kty !== "AKP") {
|
|
15
|
+
throw new JoseError('"kty" must be "AKP"');
|
|
16
|
+
}
|
|
17
|
+
// "alg" is required for the key type (RFC 9964, Section 3). It tells what
|
|
18
|
+
// "pub" and "priv" are, so a key for another algorithm is not taken for
|
|
19
|
+
// this one.
|
|
20
|
+
checkAlg(params, alg.alg);
|
|
21
|
+
return params;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Decodes a key of `size` octets from the value of a JWK parameter.
|
|
25
|
+
*/
|
|
26
|
+
function decodeKey(value, size, name) {
|
|
27
|
+
// The length of the string is checked first, so that one which is too
|
|
28
|
+
// long is rejected without being decoded.
|
|
29
|
+
const key = typeof value === "string" && value.length === encodedLength(size)
|
|
30
|
+
? decodeBase64Url(value)
|
|
31
|
+
: undefined;
|
|
32
|
+
if (key === undefined) {
|
|
33
|
+
throw new JoseError(`"${name}" must be ${size} octets in base64url`);
|
|
34
|
+
}
|
|
35
|
+
return key;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Takes the public key out of a JWK of the key type "AKP" (RFC 9964) which
|
|
39
|
+
* is a public key.
|
|
40
|
+
*
|
|
41
|
+
* It checks "kty", "alg", and that "pub" is the base64url encoding, with no
|
|
42
|
+
* padding and no other character, of as many octets as the public key of the
|
|
43
|
+
* algorithm has. A JWK with "priv" is rejected, as it is not a public key:
|
|
44
|
+
* use {@link importPrivateJwk} for it. The other parameters, such as "use" and
|
|
45
|
+
* "key_ops", are not checked, and neither is the public key itself, which is
|
|
46
|
+
* left to the one which uses it, e.g. `createMlDsa65Verifier`.
|
|
47
|
+
*
|
|
48
|
+
* @param alg The algorithm which the key has to be for, such as
|
|
49
|
+
* {@link ML_DSA_65}.
|
|
50
|
+
* @param jwk The JWK, e.g. the result of `JSON.parse`.
|
|
51
|
+
* @returns The public key.
|
|
52
|
+
* @throws {JoseError} if the JWK is not a public key of the algorithm.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { ML_DSA_65, importPublicJwk } from "@webpqc/jose";
|
|
58
|
+
* import { createMlDsa65Verifier } from "@webpqc/mldsa";
|
|
59
|
+
*
|
|
60
|
+
* async function verify(jwk: unknown, sig: Uint8Array, msg: Uint8Array) {
|
|
61
|
+
* const verifier = await createMlDsa65Verifier(
|
|
62
|
+
* importPublicJwk(ML_DSA_65, jwk),
|
|
63
|
+
* );
|
|
64
|
+
* return await verifier.verify(sig, msg);
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export function importPublicJwk(alg, jwk) {
|
|
69
|
+
const params = akpOf(alg, jwk);
|
|
70
|
+
if (params.priv !== undefined) {
|
|
71
|
+
throw new JoseError('"priv" must not be present in a public key');
|
|
72
|
+
}
|
|
73
|
+
return decodeKey(params.pub, alg.pubSize, "pub");
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Takes the private key out of a JWK of the key type "AKP" (RFC 9964) which
|
|
77
|
+
* has it.
|
|
78
|
+
*
|
|
79
|
+
* It checks "kty", "alg", and that "pub" and "priv" are the base64url
|
|
80
|
+
* encodings, with no padding and no other character, of as many octets as
|
|
81
|
+
* the keys of the algorithm have. The other parameters, such as "use" and
|
|
82
|
+
* "key_ops", are not checked.
|
|
83
|
+
*
|
|
84
|
+
* "pub" is checked because the key type requires it, but it is not returned:
|
|
85
|
+
* only the algorithm itself can tell that it is the public key of the private
|
|
86
|
+
* key, and a public key which is not would be of no use, or worse (RFC 9964,
|
|
87
|
+
* Section 7.4). The public key of the private key is the one which the
|
|
88
|
+
* algorithm derives from it, e.g. with `deriveMlDsa65KeyPair`.
|
|
89
|
+
*
|
|
90
|
+
* @param alg The algorithm which the key has to be for, such as
|
|
91
|
+
* {@link ML_DSA_65}.
|
|
92
|
+
* @param jwk The JWK, e.g. the result of `JSON.parse`.
|
|
93
|
+
* @returns The private key.
|
|
94
|
+
* @throws {JoseError} if the JWK is not a private key of the algorithm.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* import { ML_DSA_65, importPrivateJwk } from "@webpqc/jose";
|
|
100
|
+
* import { createMlDsa65Signer } from "@webpqc/mldsa";
|
|
101
|
+
*
|
|
102
|
+
* async function sign(jwk: unknown, msg: Uint8Array) {
|
|
103
|
+
* const sk = importPrivateJwk(ML_DSA_65, jwk);
|
|
104
|
+
* const signer = await createMlDsa65Signer(sk);
|
|
105
|
+
* sk.fill(0);
|
|
106
|
+
* const sig = await signer.sign(msg);
|
|
107
|
+
* signer.destroy();
|
|
108
|
+
* return sig;
|
|
109
|
+
* }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export function importPrivateJwk(alg, jwk) {
|
|
113
|
+
const params = akpOf(alg, jwk);
|
|
114
|
+
decodeKey(params.pub, alg.pubSize, "pub");
|
|
115
|
+
return decodeKey(params.priv, alg.privSize, "priv");
|
|
116
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { AkpAlgorithm, AkpPrivateJwk, AkpPublicJwk } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a public key as a JWK of the key type "AKP" (RFC 9964).
|
|
4
|
+
*
|
|
5
|
+
* The JWK has the parameters "kty", "alg" and "pub". Add the others, such as
|
|
6
|
+
* "kid", to the object which is returned.
|
|
7
|
+
*
|
|
8
|
+
* @param alg The algorithm of the key, such as {@link ML_DSA_65}.
|
|
9
|
+
* @param publicKey The public key.
|
|
10
|
+
* @returns The JWK.
|
|
11
|
+
* @throws {JoseError} if the public key is not a Uint8Array of the size which
|
|
12
|
+
* the algorithm gives it.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { ML_DSA_65, exportPublicJwk } from "@webpqc/jose";
|
|
18
|
+
* import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
|
|
19
|
+
*
|
|
20
|
+
* const [pk, _sk] = await generateMlDsa65KeyPair();
|
|
21
|
+
* const jwk = exportPublicJwk(ML_DSA_65, pk);
|
|
22
|
+
* // { kty: "AKP", alg: "ML-DSA-65", pub: "..." }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function exportPublicJwk<A extends string>(alg: AkpAlgorithm<A>, publicKey: Uint8Array): AkpPublicJwk<A>;
|
|
26
|
+
/**
|
|
27
|
+
* Represents a key pair as a JWK of the key type "AKP" (RFC 9964), which has
|
|
28
|
+
* the private key.
|
|
29
|
+
*
|
|
30
|
+
* The JWK has the parameters "kty", "alg", "pub" and "priv". The arguments
|
|
31
|
+
* are in the order of the key pairs of `@webpqc/mldsa`, `@webpqc/mlkem` and
|
|
32
|
+
* `@webpqc/slhdsa`, whose private keys are what "priv" is: the seed of ML-DSA
|
|
33
|
+
* and ML-KEM, and the private key of SLH-DSA.
|
|
34
|
+
*
|
|
35
|
+
* It does not check that the public key is the one of the private key, which
|
|
36
|
+
* only the algorithm itself can tell: give it the keys of one key pair.
|
|
37
|
+
*
|
|
38
|
+
* @param alg The algorithm of the keys, such as {@link ML_DSA_65}.
|
|
39
|
+
* @param publicKey The public key.
|
|
40
|
+
* @param privateKey The private key.
|
|
41
|
+
* @returns The JWK.
|
|
42
|
+
* @throws {JoseError} if a key is not a Uint8Array of the size which the
|
|
43
|
+
* algorithm gives it.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { exportPrivateJwk, ML_DSA_65 } from "@webpqc/jose";
|
|
49
|
+
* import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
|
|
50
|
+
*
|
|
51
|
+
* const [pk, sk] = await generateMlDsa65KeyPair();
|
|
52
|
+
* const jwk = exportPrivateJwk(ML_DSA_65, pk, sk);
|
|
53
|
+
* // { kty: "AKP", alg: "ML-DSA-65", pub: "...", priv: "..." }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function exportPrivateJwk<A extends string>(alg: AkpAlgorithm<A>, publicKey: Uint8Array, privateKey: Uint8Array): AkpPrivateJwk<A>;
|
|
57
|
+
//# sourceMappingURL=encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/encode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE5E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,MAAM,EAC9C,GAAG,EAAE,YAAY,CAAC,CAAC,CAAC,EACpB,SAAS,EAAE,UAAU,GACpB,YAAY,CAAC,CAAC,CAAC,CAMjB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAC/C,GAAG,EAAE,YAAY,CAAC,CAAC,CAAC,EACpB,SAAS,EAAE,UAAU,EACrB,UAAU,EAAE,UAAU,GACrB,aAAa,CAAC,CAAC,CAAC,CAOlB"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { encodeKey } from "./key.js";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a public key as a JWK of the key type "AKP" (RFC 9964).
|
|
4
|
+
*
|
|
5
|
+
* The JWK has the parameters "kty", "alg" and "pub". Add the others, such as
|
|
6
|
+
* "kid", to the object which is returned.
|
|
7
|
+
*
|
|
8
|
+
* @param alg The algorithm of the key, such as {@link ML_DSA_65}.
|
|
9
|
+
* @param publicKey The public key.
|
|
10
|
+
* @returns The JWK.
|
|
11
|
+
* @throws {JoseError} if the public key is not a Uint8Array of the size which
|
|
12
|
+
* the algorithm gives it.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { ML_DSA_65, exportPublicJwk } from "@webpqc/jose";
|
|
18
|
+
* import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
|
|
19
|
+
*
|
|
20
|
+
* const [pk, _sk] = await generateMlDsa65KeyPair();
|
|
21
|
+
* const jwk = exportPublicJwk(ML_DSA_65, pk);
|
|
22
|
+
* // { kty: "AKP", alg: "ML-DSA-65", pub: "..." }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function exportPublicJwk(alg, publicKey) {
|
|
26
|
+
return {
|
|
27
|
+
kty: "AKP",
|
|
28
|
+
alg: alg.alg,
|
|
29
|
+
pub: encodeKey(publicKey, alg.pubSize, "publicKey"),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Represents a key pair as a JWK of the key type "AKP" (RFC 9964), which has
|
|
34
|
+
* the private key.
|
|
35
|
+
*
|
|
36
|
+
* The JWK has the parameters "kty", "alg", "pub" and "priv". The arguments
|
|
37
|
+
* are in the order of the key pairs of `@webpqc/mldsa`, `@webpqc/mlkem` and
|
|
38
|
+
* `@webpqc/slhdsa`, whose private keys are what "priv" is: the seed of ML-DSA
|
|
39
|
+
* and ML-KEM, and the private key of SLH-DSA.
|
|
40
|
+
*
|
|
41
|
+
* It does not check that the public key is the one of the private key, which
|
|
42
|
+
* only the algorithm itself can tell: give it the keys of one key pair.
|
|
43
|
+
*
|
|
44
|
+
* @param alg The algorithm of the keys, such as {@link ML_DSA_65}.
|
|
45
|
+
* @param publicKey The public key.
|
|
46
|
+
* @param privateKey The private key.
|
|
47
|
+
* @returns The JWK.
|
|
48
|
+
* @throws {JoseError} if a key is not a Uint8Array of the size which the
|
|
49
|
+
* algorithm gives it.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { exportPrivateJwk, ML_DSA_65 } from "@webpqc/jose";
|
|
55
|
+
* import { generateMlDsa65KeyPair } from "@webpqc/mldsa";
|
|
56
|
+
*
|
|
57
|
+
* const [pk, sk] = await generateMlDsa65KeyPair();
|
|
58
|
+
* const jwk = exportPrivateJwk(ML_DSA_65, pk, sk);
|
|
59
|
+
* // { kty: "AKP", alg: "ML-DSA-65", pub: "...", priv: "..." }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export function exportPrivateJwk(alg, publicKey, privateKey) {
|
|
63
|
+
return {
|
|
64
|
+
kty: "AKP",
|
|
65
|
+
alg: alg.alg,
|
|
66
|
+
pub: encodeKey(publicKey, alg.pubSize, "publicKey"),
|
|
67
|
+
priv: encodeKey(privateKey, alg.privSize, "privateKey"),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encodes a key of `size` octets to base64url, as the value of a JWK
|
|
3
|
+
* parameter.
|
|
4
|
+
*
|
|
5
|
+
* @param key The key.
|
|
6
|
+
* @param size The size which the algorithm gives the key, in octets.
|
|
7
|
+
* @param name The name of the argument, for the message of the error.
|
|
8
|
+
* @throws {JoseError} if the key is not a Uint8Array of the size.
|
|
9
|
+
*/
|
|
10
|
+
export declare function encodeKey(key: Uint8Array, size: number, name: string): string;
|
|
11
|
+
//# sourceMappingURL=key.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/key.ts"],"names":[],"mappings":"AAIA;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAK7E"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { encodeBase64Url } from "../base64url/encode.js";
|
|
2
|
+
import { sizeOfBytes } from "../bytes.js";
|
|
3
|
+
import { JoseError } from "../errors.js";
|
|
4
|
+
/**
|
|
5
|
+
* Encodes a key of `size` octets to base64url, as the value of a JWK
|
|
6
|
+
* parameter.
|
|
7
|
+
*
|
|
8
|
+
* @param key The key.
|
|
9
|
+
* @param size The size which the algorithm gives the key, in octets.
|
|
10
|
+
* @param name The name of the argument, for the message of the error.
|
|
11
|
+
* @throws {JoseError} if the key is not a Uint8Array of the size.
|
|
12
|
+
*/
|
|
13
|
+
export function encodeKey(key, size, name) {
|
|
14
|
+
if (sizeOfBytes(key) !== size) {
|
|
15
|
+
throw new JoseError(`${name} must be a Uint8Array of ${size} octets`);
|
|
16
|
+
}
|
|
17
|
+
return encodeBase64Url(key, size);
|
|
18
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AkpAlgorithm } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* ML-DSA-44 (FIPS 204), as RFC 9964 represents its keys: "pub" is the public
|
|
4
|
+
* key (1312 octets), and "priv" is the 32-octet seed, which is the private
|
|
5
|
+
* key of `@webpqc/mldsa`.
|
|
6
|
+
*/
|
|
7
|
+
export declare const ML_DSA_44: AkpAlgorithm<"ML-DSA-44">;
|
|
8
|
+
/**
|
|
9
|
+
* ML-DSA-65 (FIPS 204), as RFC 9964 represents its keys: "pub" is the public
|
|
10
|
+
* key (1952 octets), and "priv" is the 32-octet seed, which is the private
|
|
11
|
+
* key of `@webpqc/mldsa`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const ML_DSA_65: AkpAlgorithm<"ML-DSA-65">;
|
|
14
|
+
/**
|
|
15
|
+
* ML-DSA-87 (FIPS 204), as RFC 9964 represents its keys: "pub" is the public
|
|
16
|
+
* key (2592 octets), and "priv" is the 32-octet seed, which is the private
|
|
17
|
+
* key of `@webpqc/mldsa`.
|
|
18
|
+
*/
|
|
19
|
+
export declare const ML_DSA_87: AkpAlgorithm<"ML-DSA-87">;
|
|
20
|
+
//# sourceMappingURL=mlDsa.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mlDsa.d.ts","sourceRoot":"","sources":["../../../src/src/jwk/mlDsa.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;GAIG;AACH,eAAO,MAAM,SAAS,EAAE,YAAY,CAAC,WAAW,CAI/C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,SAAS,EAAE,YAAY,CAAC,WAAW,CAI/C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,SAAS,EAAE,YAAY,CAAC,WAAW,CAI/C,CAAC"}
|