@intellectual-frontiers/crypto 0.0.0-stage → 0.1.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 +8 -2
- package/dist/cms.d.ts +106 -0
- package/dist/cms.js +350 -0
- package/dist/cose.d.ts +34 -0
- package/dist/cose.js +204 -0
- package/dist/crypt.d.ts +30 -0
- package/dist/crypt.js +117 -0
- package/dist/dsse.d.ts +137 -0
- package/dist/dsse.js +243 -0
- package/dist/md5.d.ts +19 -0
- package/dist/md5.js +146 -0
- package/dist/mod.d.ts +25 -0
- package/dist/mod.js +25 -0
- package/dist/sha256.d.ts +25 -0
- package/dist/sha256.js +34 -0
- package/dist/sigv4.d.ts +67 -0
- package/dist/sigv4.js +169 -0
- package/dist/uuid.d.ts +53 -0
- package/dist/uuid.js +92 -0
- package/dist/webauthn.d.ts +212 -0
- package/dist/webauthn.js +381 -0
- package/package.json +84 -4
- package/src/cms.ts +387 -0
- package/src/cose.ts +199 -0
- package/src/crypt.ts +117 -0
- package/src/dsse.ts +339 -0
- package/src/md5.ts +150 -0
- package/src/mod.ts +26 -0
- package/src/sha256.ts +55 -0
- package/src/sigv4.ts +216 -0
- package/src/uuid.ts +111 -0
- package/src/webauthn.ts +548 -0
package/dist/cose.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Spec: 0001-crypto FR-008
|
|
2
|
+
/**
|
|
3
|
+
* COSE keys and signatures (RFC 9052, RFC 9053) as WebAuthn uses them (commons 0014-webauthn FR-002): a credential's public key read from
|
|
4
|
+
* its CBOR `COSE_Key`, checked for one of the two algorithms commons accepts (ES256, `-7`: ECDSA on P-256 with SHA-256; EdDSA, `-8`: Ed25519),
|
|
5
|
+
* imported into Web Crypto, and a signature verified with it, an ECDSA signature turned from the DER `Ecdsa-Sig-Value` an authenticator
|
|
6
|
+
* gives into the raw `r || s` Web Crypto takes. A `COSE_Key` can also be made from a public JSON Web Key, which is how a test (or a
|
|
7
|
+
* migration from stored JWKs) writes one.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { coseKeyFromJwk, parseCoseKey, verifyCose } from "./cose.ts";
|
|
12
|
+
* const pair = await crypto.subtle.generateKey({ name: "Ed25519" }, true, ["sign", "verify"]);
|
|
13
|
+
* const cose = coseKeyFromJwk(await crypto.subtle.exportKey("jwk", pair.publicKey));
|
|
14
|
+
* const data = new TextEncoder().encode("signed");
|
|
15
|
+
* const sig = new Uint8Array(await crypto.subtle.sign({ name: "Ed25519" }, pair.privateKey, data));
|
|
16
|
+
* if (cose.kind === "ok") {
|
|
17
|
+
* const key = parseCoseKey(cose.value);
|
|
18
|
+
* if (key.kind === "ok") await verifyCose(key.value, sig, data); // { kind: "ok", value: true }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @limits
|
|
23
|
+
* Only ES256 on P-256 and EdDSA on Ed25519: RS256 and the other algorithms an authenticator may offer are refused, as commons never offers
|
|
24
|
+
* them.
|
|
25
|
+
*
|
|
26
|
+
* @module
|
|
27
|
+
*/
|
|
28
|
+
import { fromBase64Url, toBase64Url } from "@intellectual-frontiers/formats/base64";
|
|
29
|
+
import { decodeCbor, encodeCbor, formatCborError, isMap } from "@intellectual-frontiers/formats/cbor";
|
|
30
|
+
import { children, readTlv, TAG } from "@intellectual-frontiers/formats/der";
|
|
31
|
+
import { err, ok } from "@intellectual-frontiers/core";
|
|
32
|
+
/** COSE's identifier of ES256: ECDSA on P-256 with SHA-256. */
|
|
33
|
+
export const ES256 = -7;
|
|
34
|
+
/** COSE's identifier of EdDSA (here always Ed25519). */
|
|
35
|
+
export const EDDSA = -8;
|
|
36
|
+
/** The algorithms commons accepts, in order of preference. */
|
|
37
|
+
export const ALGORITHMS = [EDDSA, ES256];
|
|
38
|
+
function bytesAt(m, label, name, size) {
|
|
39
|
+
const v = m.get(label);
|
|
40
|
+
if (!(v instanceof Uint8Array))
|
|
41
|
+
return err(`the COSE key has no ${name} as bytes`);
|
|
42
|
+
if (v.length !== size)
|
|
43
|
+
return err(`the COSE key's ${name} is ${v.length} bytes, not ${size}`);
|
|
44
|
+
return ok(v);
|
|
45
|
+
}
|
|
46
|
+
/** A `COSE_Key` read from its CBOR: a map with `kty` (1), `alg` (3), `crv` (-1), `x` (-2) and, for EC2, `y` (-3); anything else refused. */
|
|
47
|
+
export function parseCoseKey(bytes) {
|
|
48
|
+
const d = decodeCbor(bytes);
|
|
49
|
+
if (d.kind === "err")
|
|
50
|
+
return err(`the COSE key is not CBOR (${formatCborError(d.error)})`);
|
|
51
|
+
const m = d.value;
|
|
52
|
+
if (!isMap(m))
|
|
53
|
+
return err("the COSE key is not a CBOR map");
|
|
54
|
+
const kty = m.get(1);
|
|
55
|
+
const alg = m.get(3);
|
|
56
|
+
const crv = m.get(-1);
|
|
57
|
+
if (alg === ES256) {
|
|
58
|
+
if (kty !== 2)
|
|
59
|
+
return err("an ES256 COSE key whose kty is not EC2 (2)");
|
|
60
|
+
if (crv !== 1)
|
|
61
|
+
return err("an ES256 COSE key whose curve is not P-256 (1)");
|
|
62
|
+
const x = bytesAt(m, -2, "x", 32);
|
|
63
|
+
if (x.kind === "err")
|
|
64
|
+
return x;
|
|
65
|
+
const y = bytesAt(m, -3, "y", 32);
|
|
66
|
+
if (y.kind === "err")
|
|
67
|
+
return y;
|
|
68
|
+
return ok({ kty: "EC2", alg: ES256, crv: "P-256", x: x.value, y: y.value });
|
|
69
|
+
}
|
|
70
|
+
if (alg === EDDSA) {
|
|
71
|
+
if (kty !== 1)
|
|
72
|
+
return err("an EdDSA COSE key whose kty is not OKP (1)");
|
|
73
|
+
if (crv !== 6)
|
|
74
|
+
return err("an EdDSA COSE key whose curve is not Ed25519 (6)");
|
|
75
|
+
const x = bytesAt(m, -2, "x", 32);
|
|
76
|
+
if (x.kind === "err")
|
|
77
|
+
return x;
|
|
78
|
+
return ok({ kty: "OKP", alg: EDDSA, crv: "Ed25519", x: x.value });
|
|
79
|
+
}
|
|
80
|
+
return err(`the COSE key's algorithm ${typeof alg === "number" || typeof alg === "bigint" ? String(alg) : "(none)"} is not accepted (only ES256, -7, and EdDSA, -8)`);
|
|
81
|
+
}
|
|
82
|
+
/** The CBOR `COSE_Key` of a key (deterministic: its labels sorted). */
|
|
83
|
+
export function encodeCoseKey(key) {
|
|
84
|
+
const m = new Map(key.kty === "EC2"
|
|
85
|
+
? [
|
|
86
|
+
[1, 2],
|
|
87
|
+
[3, ES256],
|
|
88
|
+
[-1, 1],
|
|
89
|
+
[-2, key.x],
|
|
90
|
+
[-3, key.y],
|
|
91
|
+
]
|
|
92
|
+
: [
|
|
93
|
+
[1, 1],
|
|
94
|
+
[3, EDDSA],
|
|
95
|
+
[-1, 6],
|
|
96
|
+
[-2, key.x],
|
|
97
|
+
]);
|
|
98
|
+
return encodeCbor(m);
|
|
99
|
+
}
|
|
100
|
+
/** The `COSE_Key` of a public JSON Web Key: EC on P-256 (as ES256) or OKP on Ed25519 (as EdDSA); any other key refused. */
|
|
101
|
+
export function coseKeyFromJwk(jwk) {
|
|
102
|
+
const b = (v, name) => {
|
|
103
|
+
if (v === undefined)
|
|
104
|
+
return err(`the JWK has no ${name}`);
|
|
105
|
+
const r = fromBase64Url(v);
|
|
106
|
+
return r.kind === "ok" && r.value.length === 32 ? r : err(`the JWK's ${name} is not 32 bytes of base64url`);
|
|
107
|
+
};
|
|
108
|
+
if (jwk.kty === "EC" && jwk.crv === "P-256") {
|
|
109
|
+
const x = b(jwk.x, "x");
|
|
110
|
+
if (x.kind === "err")
|
|
111
|
+
return x;
|
|
112
|
+
const y = b(jwk.y, "y");
|
|
113
|
+
if (y.kind === "err")
|
|
114
|
+
return y;
|
|
115
|
+
return ok(encodeCoseKey({ kty: "EC2", alg: ES256, crv: "P-256", x: x.value, y: y.value }));
|
|
116
|
+
}
|
|
117
|
+
if (jwk.kty === "OKP" && jwk.crv === "Ed25519") {
|
|
118
|
+
const x = b(jwk.x, "x");
|
|
119
|
+
if (x.kind === "err")
|
|
120
|
+
return x;
|
|
121
|
+
return ok(encodeCoseKey({ kty: "OKP", alg: EDDSA, crv: "Ed25519", x: x.value }));
|
|
122
|
+
}
|
|
123
|
+
return err(`a JWK of type ${String(jwk.kty)} on ${String(jwk.crv)} (only EC on P-256 and OKP on Ed25519)`);
|
|
124
|
+
}
|
|
125
|
+
/** The public JSON Web Key of a COSE key. */
|
|
126
|
+
export function jwkOfCoseKey(key) {
|
|
127
|
+
return key.kty === "EC2"
|
|
128
|
+
? { kty: "EC", crv: "P-256", x: toBase64Url(key.x), y: toBase64Url(key.y) }
|
|
129
|
+
: { kty: "OKP", crv: "Ed25519", x: toBase64Url(key.x) };
|
|
130
|
+
}
|
|
131
|
+
/** Bytes Web Crypto takes as a `BufferSource` (a copy on its own `ArrayBuffer`). */
|
|
132
|
+
function own(b) {
|
|
133
|
+
return new Uint8Array(b);
|
|
134
|
+
}
|
|
135
|
+
/** The key imported into Web Crypto for verifying; a point not on its curve is refused. */
|
|
136
|
+
export async function importCoseKey(key) {
|
|
137
|
+
try {
|
|
138
|
+
if (key.kty === "EC2") {
|
|
139
|
+
const raw = new Uint8Array(65);
|
|
140
|
+
raw[0] = 4;
|
|
141
|
+
raw.set(key.x, 1);
|
|
142
|
+
raw.set(key.y, 33);
|
|
143
|
+
return ok(await crypto.subtle.importKey("raw", raw, { name: "ECDSA", namedCurve: "P-256" }, false, ["verify"]));
|
|
144
|
+
}
|
|
145
|
+
return ok(await crypto.subtle.importKey("raw", own(key.x), { name: "Ed25519" }, false, ["verify"]));
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
return err(`the public key cannot be used (${e instanceof Error ? e.message : String(e)})`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** An integer of a DER signature as exactly `size` unsigned big-endian bytes, or undefined where it does not fit or is negative. */
|
|
152
|
+
function fixed(content, size) {
|
|
153
|
+
if (content.length === 0 || ((content[0] ?? 0) & 0x80) !== 0)
|
|
154
|
+
return undefined;
|
|
155
|
+
let start = 0;
|
|
156
|
+
while (start < content.length - 1 && content[start] === 0)
|
|
157
|
+
start++;
|
|
158
|
+
const magnitude = content.subarray(start);
|
|
159
|
+
if (magnitude.length > size)
|
|
160
|
+
return undefined;
|
|
161
|
+
const out = new Uint8Array(size);
|
|
162
|
+
out.set(magnitude, size - magnitude.length);
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
/** An ECDSA signature as DER (`Ecdsa-Sig-Value ::= SEQUENCE { r INTEGER, s INTEGER }`) turned into raw `r || s` of `size` bytes each. */
|
|
166
|
+
export function ecdsaDerToRaw(der, size = 32) {
|
|
167
|
+
const seq = readTlv(der);
|
|
168
|
+
if (seq.kind === "err" || seq.value.tag !== TAG.sequence || seq.value.raw.length !== der.length)
|
|
169
|
+
return err("the ECDSA signature is not a DER sequence");
|
|
170
|
+
const parts = children(seq.value.content);
|
|
171
|
+
if (parts.kind === "err" || parts.value.length !== 2)
|
|
172
|
+
return err("the ECDSA signature is not two integers");
|
|
173
|
+
const [r, s] = parts.value;
|
|
174
|
+
if (r === undefined || s === undefined || r.tag !== TAG.integer || s.tag !== TAG.integer)
|
|
175
|
+
return err("the ECDSA signature is not two integers");
|
|
176
|
+
const rr = fixed(r.content, size);
|
|
177
|
+
const ss = fixed(s.content, size);
|
|
178
|
+
if (rr === undefined || ss === undefined)
|
|
179
|
+
return err(`the ECDSA signature's integers do not fit ${size} bytes`);
|
|
180
|
+
const out = new Uint8Array(size * 2);
|
|
181
|
+
out.set(rr, 0);
|
|
182
|
+
out.set(ss, size);
|
|
183
|
+
return ok(out);
|
|
184
|
+
}
|
|
185
|
+
/** Whether `signature` is the key's signature of `data` (ES256: DER as WebAuthn gives it; EdDSA: 64 raw bytes); an unusable key or a malformed signature is an error. */
|
|
186
|
+
export async function verifyCose(key, signature, data) {
|
|
187
|
+
const k = await importCoseKey(key);
|
|
188
|
+
if (k.kind === "err")
|
|
189
|
+
return k;
|
|
190
|
+
try {
|
|
191
|
+
if (key.kty === "EC2") {
|
|
192
|
+
const raw = ecdsaDerToRaw(signature);
|
|
193
|
+
if (raw.kind === "err")
|
|
194
|
+
return raw;
|
|
195
|
+
return ok(await crypto.subtle.verify({ name: "ECDSA", hash: "SHA-256" }, k.value, own(raw.value), own(data)));
|
|
196
|
+
}
|
|
197
|
+
if (signature.length !== 64)
|
|
198
|
+
return err(`the Ed25519 signature is ${signature.length} bytes, not 64`);
|
|
199
|
+
return ok(await crypto.subtle.verify({ name: "Ed25519" }, k.value, own(signature), own(data)));
|
|
200
|
+
}
|
|
201
|
+
catch (e) {
|
|
202
|
+
return err(`the signature cannot be checked (${e instanceof Error ? e.message : String(e)})`);
|
|
203
|
+
}
|
|
204
|
+
}
|
package/dist/crypt.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-512 crypt (`$6$`), the password hash of glibc's `crypt(3)` that Linux accounts and Ubuntu's installer accept, on Web Crypto alone
|
|
3
|
+
* (commons 0001-crypto FR-003). The algorithm is Ulrich Drepper's "Unix crypt using SHA-256 and SHA-512". The salt is given,
|
|
4
|
+
* so the kernel stays free of effects: a caller makes one from random bytes with `cryptSalt(crypto.getRandomValues(new Uint8Array(16)))`.
|
|
5
|
+
* Taken from Stolos's `src/archon/core/crypt.ts`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { sha512Crypt } from "./crypt.js";
|
|
10
|
+
* await sha512Crypt("Hello world!", "saltstring");
|
|
11
|
+
* // "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
/** The rounds glibc uses when none are named, and the least and most it accepts. */
|
|
17
|
+
export declare const CRYPT_ROUNDS: {
|
|
18
|
+
readonly default: 5000;
|
|
19
|
+
readonly min: 1000;
|
|
20
|
+
readonly max: 999999999;
|
|
21
|
+
};
|
|
22
|
+
/** A salt of crypt's alphabet (`./0-9A-Za-z`) made from bytes, one character each, at most 16. */
|
|
23
|
+
export declare function cryptSalt(bytes: Uint8Array): string;
|
|
24
|
+
/**
|
|
25
|
+
* A password hashed as SHA-512 crypt: `$6$<salt>$<hash>`, with `rounds=<n>$` after `$6$` where rounds are given (5000 where they are
|
|
26
|
+
* not). Only the first 16 characters of the salt count, and given rounds are clamped to what glibc accepts, as glibc does.
|
|
27
|
+
*/
|
|
28
|
+
export declare function sha512Crypt(password: string, salt: string, rounds?: number): Promise<string>;
|
|
29
|
+
/** Whether a password matches a `$6$` hash, by hashing it again with the hash's own salt and rounds. */
|
|
30
|
+
export declare function verifySha512Crypt(password: string, hash: string): Promise<boolean>;
|
package/dist/crypt.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Spec: 0001-crypto FR-003
|
|
2
|
+
/**
|
|
3
|
+
* SHA-512 crypt (`$6$`), the password hash of glibc's `crypt(3)` that Linux accounts and Ubuntu's installer accept, on Web Crypto alone
|
|
4
|
+
* (commons 0001-crypto FR-003). The algorithm is Ulrich Drepper's "Unix crypt using SHA-256 and SHA-512". The salt is given,
|
|
5
|
+
* so the kernel stays free of effects: a caller makes one from random bytes with `cryptSalt(crypto.getRandomValues(new Uint8Array(16)))`.
|
|
6
|
+
* Taken from Stolos's `src/archon/core/crypt.ts`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { sha512Crypt } from "./crypt.ts";
|
|
11
|
+
* await sha512Crypt("Hello world!", "saltstring");
|
|
12
|
+
* // "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
const B64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
18
|
+
/** The rounds glibc uses when none are named, and the least and most it accepts. */
|
|
19
|
+
export const CRYPT_ROUNDS = { default: 5000, min: 1000, max: 999_999_999 };
|
|
20
|
+
async function sha512(parts) {
|
|
21
|
+
const buf = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
22
|
+
let at = 0;
|
|
23
|
+
for (const p of parts) {
|
|
24
|
+
buf.set(p, at);
|
|
25
|
+
at += p.length;
|
|
26
|
+
}
|
|
27
|
+
return new Uint8Array(await crypto.subtle.digest("SHA-512", buf));
|
|
28
|
+
}
|
|
29
|
+
function repeatTo(src, n) {
|
|
30
|
+
const out = new Uint8Array(n);
|
|
31
|
+
for (let i = 0; i < n; i++)
|
|
32
|
+
out[i] = src[i % src.length] ?? 0;
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
/** A salt of crypt's alphabet (`./0-9A-Za-z`) made from bytes, one character each, at most 16. */
|
|
36
|
+
export function cryptSalt(bytes) {
|
|
37
|
+
return Array.from(bytes.subarray(0, 16), (b) => B64[b % 64] ?? ".").join("");
|
|
38
|
+
}
|
|
39
|
+
const ORDER = [
|
|
40
|
+
[0, 21, 42],
|
|
41
|
+
[22, 43, 1],
|
|
42
|
+
[44, 2, 23],
|
|
43
|
+
[3, 24, 45],
|
|
44
|
+
[25, 46, 4],
|
|
45
|
+
[47, 5, 26],
|
|
46
|
+
[6, 27, 48],
|
|
47
|
+
[28, 49, 7],
|
|
48
|
+
[50, 8, 29],
|
|
49
|
+
[9, 30, 51],
|
|
50
|
+
[31, 52, 10],
|
|
51
|
+
[53, 11, 32],
|
|
52
|
+
[12, 33, 54],
|
|
53
|
+
[34, 55, 13],
|
|
54
|
+
[56, 14, 35],
|
|
55
|
+
[15, 36, 57],
|
|
56
|
+
[37, 58, 16],
|
|
57
|
+
[59, 17, 38],
|
|
58
|
+
[18, 39, 60],
|
|
59
|
+
[40, 61, 19],
|
|
60
|
+
[62, 20, 41],
|
|
61
|
+
];
|
|
62
|
+
/**
|
|
63
|
+
* A password hashed as SHA-512 crypt: `$6$<salt>$<hash>`, with `rounds=<n>$` after `$6$` where rounds are given (5000 where they are
|
|
64
|
+
* not). Only the first 16 characters of the salt count, and given rounds are clamped to what glibc accepts, as glibc does.
|
|
65
|
+
*/
|
|
66
|
+
export async function sha512Crypt(password, salt, rounds) {
|
|
67
|
+
const enc = new TextEncoder();
|
|
68
|
+
const named = rounds !== undefined;
|
|
69
|
+
const r = rounds === undefined ? CRYPT_ROUNDS.default : Math.min(CRYPT_ROUNDS.max, Math.max(CRYPT_ROUNDS.min, Math.floor(rounds)));
|
|
70
|
+
const s = salt.slice(0, 16);
|
|
71
|
+
const P = enc.encode(password);
|
|
72
|
+
const S = enc.encode(s);
|
|
73
|
+
const B = await sha512([P, S, P]);
|
|
74
|
+
const aParts = [P, S, repeatTo(B, P.length)];
|
|
75
|
+
for (let n = P.length; n > 0; n >>= 1)
|
|
76
|
+
aParts.push(n & 1 ? B : P);
|
|
77
|
+
const A = await sha512(aParts);
|
|
78
|
+
const DP = await sha512(Array.from({ length: P.length }, () => P));
|
|
79
|
+
const Pbytes = repeatTo(DP, P.length);
|
|
80
|
+
const DS = await sha512(Array.from({ length: 16 + (A[0] ?? 0) }, () => S));
|
|
81
|
+
const Sbytes = repeatTo(DS, S.length);
|
|
82
|
+
let C = A;
|
|
83
|
+
for (let i = 0; i < r; i++) {
|
|
84
|
+
const parts = [i & 1 ? Pbytes : C];
|
|
85
|
+
if (i % 3)
|
|
86
|
+
parts.push(Sbytes);
|
|
87
|
+
if (i % 7)
|
|
88
|
+
parts.push(Pbytes);
|
|
89
|
+
parts.push(i & 1 ? C : Pbytes);
|
|
90
|
+
C = await sha512(parts);
|
|
91
|
+
}
|
|
92
|
+
let out = "";
|
|
93
|
+
const b64 = (value, n) => {
|
|
94
|
+
let v = value;
|
|
95
|
+
for (let i = 0; i < n; i++) {
|
|
96
|
+
out += B64[v & 63] ?? ".";
|
|
97
|
+
v >>= 6;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
for (const [a, b, c] of ORDER)
|
|
101
|
+
b64(((C[a] ?? 0) << 16) | ((C[b] ?? 0) << 8) | (C[c] ?? 0), 4);
|
|
102
|
+
b64(C[63] ?? 0, 2);
|
|
103
|
+
return `$6$${named ? `rounds=${r}$` : ""}${s}$${out}`;
|
|
104
|
+
}
|
|
105
|
+
/** Whether a password matches a `$6$` hash, by hashing it again with the hash's own salt and rounds. */
|
|
106
|
+
export async function verifySha512Crypt(password, hash) {
|
|
107
|
+
const m = /^\$6\$(?:rounds=(\d+)\$)?([^$]{0,16})\$[./0-9A-Za-z]{86}$/.exec(hash);
|
|
108
|
+
if (m === null)
|
|
109
|
+
return false;
|
|
110
|
+
const again = await sha512Crypt(password, m[2] ?? "", m[1] === undefined ? undefined : Number(m[1]));
|
|
111
|
+
if (again.length !== hash.length)
|
|
112
|
+
return false;
|
|
113
|
+
let d = 0;
|
|
114
|
+
for (let i = 0; i < hash.length; i++)
|
|
115
|
+
d |= again.charCodeAt(i) ^ hash.charCodeAt(i);
|
|
116
|
+
return d === 0;
|
|
117
|
+
}
|
package/dist/dsse.d.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* in-toto Statements v1 and DSSE envelopes (commons 0001-crypto FR-006): a statement says that a predicate of some type holds
|
|
3
|
+
* of its subjects, each named and identified by its digests; DSSE signs a payload of a stated type, by the pre-authentication encoding
|
|
4
|
+
* (PAE) of the two, and carries it in a JSON envelope with its signatures. Signing and verifying use Web Crypto (`crypto.subtle`) on keys
|
|
5
|
+
* the caller has imported as `CryptoKey`s, so the module has no effect beyond the computation: Ed25519, ECDSA on P-256, P-384 and P-521
|
|
6
|
+
* (with SHA-256, SHA-384 and SHA-512, the signature in ASN.1 DER as sigstore and in-toto's own tools write it), and RSA-PSS and
|
|
7
|
+
* RSASSA-PKCS1-v1_5 with the key's hash. Verification returns the payload, or an error that says why it was refused: an envelope that is
|
|
8
|
+
* malformed, a payload type not the one expected, a key not usable, or too few valid signatures.
|
|
9
|
+
*
|
|
10
|
+
* Taken from Stolos's `src/archon/export/intoto.ts`, which built a statement and an envelope for a signature callback but never verified
|
|
11
|
+
* one. What changed: the statement is generic in its predicate; the subject's digest is any map of algorithms; signing takes keys rather
|
|
12
|
+
* than a callback; verification, the envelope's decoder and the ECDSA signature form are new. Stolos's evidence bundle predicate (its
|
|
13
|
+
* predicate type, the manifest's summary and the subject list of a bundle's items) is Stolos's own domain and stays there.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { pae, signStatement, statement, subject, verifyStatement } from "./dsse.js";
|
|
18
|
+
* import { unknownValue } from "@intellectual-frontiers/core";
|
|
19
|
+
* new TextDecoder().decode(pae("http://example.com/HelloWorld", new TextEncoder().encode("hello world")));
|
|
20
|
+
* // "DSSEv1 29 http://example.com/HelloWorld 11 hello world"
|
|
21
|
+
* const keys = await crypto.subtle.generateKey({ name: "Ed25519" }, false, ["sign", "verify"]);
|
|
22
|
+
* const s = statement([await subject("a.txt", new TextEncoder().encode("a"))], "https://example.com/Note/v1", { note: "hi" });
|
|
23
|
+
* const signed = await signStatement(s, [{ key: keys.privateKey, keyid: "me" }]);
|
|
24
|
+
* if (signed.kind === "ok") await verifyStatement(signed.value, [{ key: keys.publicKey, keyid: "me" }], unknownValue); // { kind: "ok", ... }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @module
|
|
28
|
+
*/
|
|
29
|
+
import { type DecodeError, type Decoder, type Result } from "@intellectual-frontiers/core";
|
|
30
|
+
/** The `_type` of an in-toto Statement v1. */
|
|
31
|
+
export declare const STATEMENT_TYPE = "https://in-toto.io/Statement/v1";
|
|
32
|
+
/** The DSSE payload type of an in-toto statement. */
|
|
33
|
+
export declare const INTOTO_PAYLOAD_TYPE = "application/vnd.in-toto+json";
|
|
34
|
+
/** An in-toto ResourceDescriptor as a statement's subject: its name, its digests by algorithm (`sha256`, ...) in lower-case hexadecimal, and what else is known of it. */
|
|
35
|
+
export type ResourceDescriptor = {
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly digest: Readonly<Record<string, string>>;
|
|
38
|
+
readonly uri?: string;
|
|
39
|
+
readonly mediaType?: string;
|
|
40
|
+
readonly annotations?: Readonly<Record<string, unknown>>;
|
|
41
|
+
};
|
|
42
|
+
/** An in-toto Statement v1: a predicate of type `predicateType` that holds of the subjects. */
|
|
43
|
+
export type Statement<P> = {
|
|
44
|
+
readonly _type: typeof STATEMENT_TYPE;
|
|
45
|
+
readonly subject: readonly ResourceDescriptor[];
|
|
46
|
+
readonly predicateType: string;
|
|
47
|
+
readonly predicate: P;
|
|
48
|
+
};
|
|
49
|
+
/** A DSSE envelope: the payload in base64, its type, and the signatures (base64) over PAE(payloadType, payload). */
|
|
50
|
+
export type DsseEnvelope = {
|
|
51
|
+
readonly payload: string;
|
|
52
|
+
readonly payloadType: string;
|
|
53
|
+
readonly signatures: readonly {
|
|
54
|
+
readonly keyid?: string;
|
|
55
|
+
readonly sig: string;
|
|
56
|
+
}[];
|
|
57
|
+
};
|
|
58
|
+
/** A key to sign with (a private key) or verify with (a public key), and the id that names it in an envelope, if any. */
|
|
59
|
+
export type DsseKey = {
|
|
60
|
+
readonly key: CryptoKey;
|
|
61
|
+
readonly keyid?: string;
|
|
62
|
+
};
|
|
63
|
+
/** A signature algorithm this module signs and verifies with, as read from a key. */
|
|
64
|
+
export type DsseAlgorithm = {
|
|
65
|
+
readonly kind: "ed25519";
|
|
66
|
+
} | {
|
|
67
|
+
readonly kind: "ecdsa";
|
|
68
|
+
readonly curve: "P-256" | "P-384" | "P-521";
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: "rsa-pss";
|
|
71
|
+
readonly hash: "SHA-256" | "SHA-384" | "SHA-512";
|
|
72
|
+
} | {
|
|
73
|
+
readonly kind: "rsassa-pkcs1-v1_5";
|
|
74
|
+
readonly hash: "SHA-256" | "SHA-384" | "SHA-512";
|
|
75
|
+
};
|
|
76
|
+
/** Why a key cannot sign or verify: its place in the list given, and the reason. */
|
|
77
|
+
export type DsseKeyError = {
|
|
78
|
+
readonly kind: "key";
|
|
79
|
+
readonly index: number;
|
|
80
|
+
readonly reason: string;
|
|
81
|
+
};
|
|
82
|
+
/** Why an envelope was refused. */
|
|
83
|
+
export type DsseError = {
|
|
84
|
+
readonly kind: "malformed";
|
|
85
|
+
readonly reason: string;
|
|
86
|
+
} | {
|
|
87
|
+
readonly kind: "payload-type";
|
|
88
|
+
readonly expected: string;
|
|
89
|
+
readonly found: string;
|
|
90
|
+
} | DsseKeyError | {
|
|
91
|
+
readonly kind: "no-valid-signature";
|
|
92
|
+
readonly signatures: number;
|
|
93
|
+
readonly verified: number;
|
|
94
|
+
readonly threshold: number;
|
|
95
|
+
};
|
|
96
|
+
/** Why a signed statement was refused: the envelope's error, or a payload that is not an in-toto statement with the predicate expected. */
|
|
97
|
+
export type StatementError = DsseError | {
|
|
98
|
+
readonly kind: "statement";
|
|
99
|
+
readonly error: DecodeError;
|
|
100
|
+
};
|
|
101
|
+
/** What a verified envelope carries: its payload type, its payload, and the keys that verified a signature. */
|
|
102
|
+
export type Verified = {
|
|
103
|
+
readonly payloadType: string;
|
|
104
|
+
readonly payload: Uint8Array;
|
|
105
|
+
readonly keys: readonly DsseKey[];
|
|
106
|
+
};
|
|
107
|
+
/** A subject: the name, and the SHA-256 of the bytes; `more` adds a URI, a media type or annotations. */
|
|
108
|
+
export declare function subject(name: string, bytes: Uint8Array, more?: {
|
|
109
|
+
readonly uri?: string;
|
|
110
|
+
readonly mediaType?: string;
|
|
111
|
+
readonly annotations?: Readonly<Record<string, unknown>>;
|
|
112
|
+
}): Promise<ResourceDescriptor>;
|
|
113
|
+
/** A statement of the predicate about the subjects. */
|
|
114
|
+
export declare function statement<P>(subjects: readonly ResourceDescriptor[], predicateType: string, predicate: P): Statement<P>;
|
|
115
|
+
/** The decoder of a ResourceDescriptor with a name and digests. */
|
|
116
|
+
export declare const resourceDescriptorDecoder: Decoder<ResourceDescriptor>;
|
|
117
|
+
/** The decoder of an in-toto Statement v1 whose predicate `predicate` decodes. */
|
|
118
|
+
export declare function statementDecoder<P>(predicate: Decoder<P>): Decoder<Statement<P>>;
|
|
119
|
+
/** The decoder of a DSSE envelope: its fields present, and the payload and signatures in base64. */
|
|
120
|
+
export declare const envelopeDecoder: Decoder<DsseEnvelope>;
|
|
121
|
+
/** A DSSE envelope read from JSON text, or why it is not one. */
|
|
122
|
+
export declare function parseEnvelope(text: string): Result<DsseEnvelope, DsseError>;
|
|
123
|
+
/** DSSE's pre-authentication encoding: `DSSEv1 <len(type)> <type> <len(payload)> <payload>`, the lengths in bytes as ASCII decimal. */
|
|
124
|
+
export declare function pae(payloadType: string, payload: Uint8Array): Uint8Array<ArrayBuffer>;
|
|
125
|
+
/** The signature algorithm of a key, or the reason it is not one this module uses. */
|
|
126
|
+
export declare function signatureAlgorithm(key: CryptoKey): Result<DsseAlgorithm, string>;
|
|
127
|
+
/** The payload of the type signed by every key given (private keys), in an envelope; or the first key that cannot sign. */
|
|
128
|
+
export declare function signEnvelope(payloadType: string, payload: Uint8Array, keys: readonly DsseKey[]): Promise<Result<DsseEnvelope, DsseKeyError>>;
|
|
129
|
+
/**
|
|
130
|
+
* The payload of an envelope whose type is `payloadType` and which at least `threshold` of the keys given (public keys, 1 by default)
|
|
131
|
+
* each verify a signature of. A signature that names a key id is tried only with the keys of that id or of none.
|
|
132
|
+
*/
|
|
133
|
+
export declare function verifyEnvelope(envelope: DsseEnvelope, payloadType: string, keys: readonly DsseKey[], threshold?: number): Promise<Result<Verified, DsseError>>;
|
|
134
|
+
/** A statement as JSON, signed by every key given, in an envelope of type `application/vnd.in-toto+json`. */
|
|
135
|
+
export declare function signStatement<P>(s: Statement<P>, keys: readonly DsseKey[]): Promise<Result<DsseEnvelope, DsseKeyError>>;
|
|
136
|
+
/** The statement an envelope of type `application/vnd.in-toto+json` carries, verified as `verifyEnvelope` does, its predicate decoded by `predicate`. */
|
|
137
|
+
export declare function verifyStatement<P>(envelope: DsseEnvelope, keys: readonly DsseKey[], predicate: Decoder<P>, threshold?: number): Promise<Result<Statement<P>, StatementError>>;
|