@kokuin/vault 0.1.0 → 0.7.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/README.md +108 -1
- package/lib/branch.js +2 -3
- package/lib/digest.d.ts +27 -8
- package/lib/digest.js +63 -41
- package/lib/errors.d.ts +38 -13
- package/lib/errors.js +43 -25
- package/lib/factors.d.ts +18 -6
- package/lib/factors.js +12 -6
- package/lib/id.d.ts +12 -3
- package/lib/id.js +14 -6
- package/lib/index.d.ts +7 -1
- package/lib/index.js +1 -1
- package/lib/key-cache.d.ts +22 -4
- package/lib/key-cache.js +4 -4
- package/lib/record-types.d.ts +17 -6
- package/lib/record-types.js +269 -79
- package/lib/records.d.ts +29 -10
- package/lib/records.js +57 -14
- package/lib/suite.d.ts +3 -3
- package/lib/suite.js +127 -31
- package/package.json +8 -7
package/lib/records.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { RecordClaim, SignedRecord } from '@kokuin/records';
|
|
2
|
+
import type { CryptoOptions } from '@kokuin/token';
|
|
2
3
|
import type { EncryptEntryResult, WrapResult } from './suite.js';
|
|
4
|
+
export type VaultRecordPayloadErrorParams = {
|
|
5
|
+
type: string;
|
|
6
|
+
id: string;
|
|
7
|
+
reason: string;
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
};
|
|
3
10
|
export declare const VAULT_OP_KIND = "kokuin/vault-op/v1";
|
|
4
11
|
export declare const VAULT_RECORD_TYPES: readonly ["vault.key", "vault.key-branch", "vault.wrapping", "vault.entry", "vault.revocation"];
|
|
5
12
|
export type VaultRecordType = (typeof VAULT_RECORD_TYPES)[number];
|
|
@@ -72,20 +79,32 @@ export type DecodedVaultRecord = {
|
|
|
72
79
|
}[VaultRecordType];
|
|
73
80
|
export declare class VaultRecordPayloadError extends Error {
|
|
74
81
|
#private;
|
|
75
|
-
constructor(params:
|
|
76
|
-
type: string;
|
|
77
|
-
id: string;
|
|
78
|
-
reason: string;
|
|
79
|
-
cause?: unknown;
|
|
80
|
-
});
|
|
82
|
+
constructor(params: VaultRecordPayloadErrorParams);
|
|
81
83
|
get type(): string;
|
|
82
84
|
get id(): string;
|
|
83
85
|
}
|
|
84
|
-
export
|
|
86
|
+
export type VaultRevocationRecordIDParams = {
|
|
87
|
+
keyID: string;
|
|
88
|
+
recipientDID: string;
|
|
89
|
+
};
|
|
90
|
+
export declare function vaultRevocationRecordID({ recipientDID, keyID, }: VaultRevocationRecordIDParams): string;
|
|
85
91
|
/** Structural metadata only. Consumer evidence is checked by the injected validator. */
|
|
86
|
-
export declare function
|
|
92
|
+
export declare function parseVaultRecordMeta<Type extends VaultRecordType>(type: Type, claim: RecordClaim): VaultRecordMetas[Type] | null;
|
|
93
|
+
export type VaultRecordMetaParams<Type extends VaultRecordType> = {
|
|
94
|
+
type: Type;
|
|
95
|
+
claim: RecordClaim;
|
|
96
|
+
} & CryptoOptions;
|
|
97
|
+
export declare function vaultRecordMeta<Type extends VaultRecordType>({ claim, type, ...options }: VaultRecordMetaParams<Type>): Promise<VaultRecordMetas[Type] | null>;
|
|
87
98
|
/** Canonical JSON with binary fields encoded as unpadded base64url. */
|
|
88
|
-
export
|
|
99
|
+
export type EncodeVaultRecordPayloadParams<Type extends VaultRecordType> = {
|
|
100
|
+
type: Type;
|
|
101
|
+
payload: VaultRecordPayloads[Type];
|
|
102
|
+
};
|
|
103
|
+
export declare function encodeVaultRecordPayload<Type extends VaultRecordType>({ payload, type, }: EncodeVaultRecordPayloadParams<Type>): Uint8Array;
|
|
89
104
|
/** Pure codec. Signature and payload-digest verification belong to the caller. */
|
|
90
|
-
export
|
|
105
|
+
export type DecodeVaultRecordParams = {
|
|
106
|
+
record: Pick<SignedRecord, 'type' | 'id' | 'owner' | 'hlc' | 'payload'>;
|
|
107
|
+
recordMeta: Record<string, unknown>;
|
|
108
|
+
} & CryptoOptions;
|
|
109
|
+
export declare function decodeVaultRecord({ recordMeta, record, ...options }: DecodeVaultRecordParams): Promise<DecodedVaultRecord>;
|
|
91
110
|
export {};
|
package/lib/records.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { nobleCrypto } from '@kokuin/crypto';
|
|
1
2
|
import { parseCanonicalHLC, recordPayloadDigest } from '@kokuin/records';
|
|
2
3
|
import { normalizeDID } from '@kokuin/token';
|
|
3
4
|
import { canonicalStringify, fromB64U, fromUTF, toB64U, toUTF } from '@sozai/codec';
|
|
@@ -29,7 +30,7 @@ export class VaultRecordPayloadError extends Error {
|
|
|
29
30
|
return this.#id;
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
|
-
export function vaultRevocationRecordID(
|
|
33
|
+
export function vaultRevocationRecordID({ recipientDID, keyID }) {
|
|
33
34
|
return `${keyID.length}:${keyID}/${normalizeDID(recipientDID)}`;
|
|
34
35
|
}
|
|
35
36
|
function plain(value) {
|
|
@@ -79,7 +80,7 @@ const entryMetadata = {
|
|
|
79
80
|
return plain(v) && Object.values(v).every((value)=>typeof value === 'string');
|
|
80
81
|
}
|
|
81
82
|
};
|
|
82
|
-
/** Structural metadata only. Consumer evidence is checked by the injected validator. */ export function
|
|
83
|
+
/** Structural metadata only. Consumer evidence is checked by the injected validator. */ export function parseVaultRecordMeta(type, claim) {
|
|
83
84
|
if (claim.type !== type || !plain(claim.meta)) return null;
|
|
84
85
|
const m = claim.meta;
|
|
85
86
|
let valid;
|
|
@@ -91,7 +92,7 @@ const entryMetadata = {
|
|
|
91
92
|
version: count,
|
|
92
93
|
state: (v)=>claim.deleted ? v === 'deleted' : v === 'active' || v === 'retired',
|
|
93
94
|
...consumer
|
|
94
|
-
}) && m.keyID === claim.id
|
|
95
|
+
}) && m.keyID === claim.id;
|
|
95
96
|
break;
|
|
96
97
|
case 'vault.key-branch':
|
|
97
98
|
valid = !claim.deleted && shape(m, {
|
|
@@ -130,13 +131,31 @@ const entryMetadata = {
|
|
|
130
131
|
keyID,
|
|
131
132
|
revokedDID: did,
|
|
132
133
|
...consumer
|
|
133
|
-
}) && claim.id === vaultRevocationRecordID(
|
|
134
|
+
}) && claim.id === vaultRevocationRecordID({
|
|
135
|
+
keyID: m.keyID,
|
|
136
|
+
recipientDID: m.revokedDID
|
|
137
|
+
});
|
|
134
138
|
break;
|
|
135
139
|
default:
|
|
136
140
|
return null;
|
|
137
141
|
}
|
|
138
142
|
return valid ? m : null;
|
|
139
143
|
}
|
|
144
|
+
export async function vaultRecordMeta({ claim, type, ...options }) {
|
|
145
|
+
const { id, owner } = claim;
|
|
146
|
+
const parsed = parseVaultRecordMeta(type, claim);
|
|
147
|
+
if (parsed === null) return null;
|
|
148
|
+
const meta = JSON.parse(JSON.stringify(parsed));
|
|
149
|
+
if (type === 'vault.key') {
|
|
150
|
+
const key = meta;
|
|
151
|
+
if (await createVaultKeyID({
|
|
152
|
+
...options,
|
|
153
|
+
owner,
|
|
154
|
+
unique: fromB64U(key.keyUnique)
|
|
155
|
+
}) !== id) return null;
|
|
156
|
+
}
|
|
157
|
+
return meta;
|
|
158
|
+
}
|
|
140
159
|
function wire(type, payload) {
|
|
141
160
|
if (type === 'vault.wrapping') {
|
|
142
161
|
const { descriptors, descriptorDigest, iv, wrappedKey } = payload;
|
|
@@ -156,7 +175,7 @@ function wire(type, payload) {
|
|
|
156
175
|
}
|
|
157
176
|
return payload;
|
|
158
177
|
}
|
|
159
|
-
|
|
178
|
+
export function encodeVaultRecordPayload({ payload, type }) {
|
|
160
179
|
return fromUTF(canonicalStringify(wire(type, payload)));
|
|
161
180
|
}
|
|
162
181
|
function bytes(value) {
|
|
@@ -221,23 +240,47 @@ function payloadOf(type, data) {
|
|
|
221
240
|
return {};
|
|
222
241
|
}
|
|
223
242
|
}
|
|
224
|
-
|
|
225
|
-
const { type, id, owner, hlc
|
|
243
|
+
export async function decodeVaultRecord({ recordMeta, record, ...options }) {
|
|
244
|
+
const { type, id, owner, hlc } = record;
|
|
245
|
+
const captured = {
|
|
246
|
+
crypto: options.crypto ?? nobleCrypto
|
|
247
|
+
};
|
|
248
|
+
const payload = record.payload === null ? null : new Uint8Array(record.payload);
|
|
249
|
+
let meta;
|
|
226
250
|
try {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
251
|
+
meta = JSON.parse(JSON.stringify(recordMeta));
|
|
252
|
+
} catch (cause) {
|
|
253
|
+
// biome-ignore lint/style/useErrorCause: the constructor carries cause in its parameter object
|
|
254
|
+
throw new VaultRecordPayloadError({
|
|
255
|
+
type,
|
|
256
|
+
id,
|
|
257
|
+
reason: cause instanceof Error ? cause.message : 'invalid record',
|
|
258
|
+
cause
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
const payloadDigest = payload === null ? null : await recordPayloadDigest({
|
|
262
|
+
...captured,
|
|
263
|
+
payload
|
|
264
|
+
});
|
|
265
|
+
const recordType = type;
|
|
266
|
+
const decodedMeta = VAULT_RECORD_TYPES.includes(type) ? await vaultRecordMeta({
|
|
267
|
+
...captured,
|
|
268
|
+
type: recordType,
|
|
269
|
+
claim: {
|
|
231
270
|
type,
|
|
232
271
|
id,
|
|
233
272
|
owner,
|
|
234
273
|
hlc,
|
|
235
274
|
meta,
|
|
236
275
|
deleted: false,
|
|
237
|
-
payloadDigest
|
|
238
|
-
}
|
|
276
|
+
payloadDigest
|
|
277
|
+
}
|
|
278
|
+
}) : null;
|
|
279
|
+
try {
|
|
280
|
+
if (!VAULT_RECORD_TYPES.includes(type)) throw new Error('unknown record type');
|
|
281
|
+
if (payload === null) throw new Error('a tombstone has no payload');
|
|
239
282
|
if (decodedMeta === null) throw new Error('record metadata');
|
|
240
|
-
if (type === 'vault.key-branch' && id !==
|
|
283
|
+
if (type === 'vault.key-branch' && id !== payloadDigest) throw new Error('record id is not the payload digest');
|
|
241
284
|
return {
|
|
242
285
|
type,
|
|
243
286
|
id,
|
package/lib/suite.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { KeyAgreementIdentity, MethodRegistry } from '@kokuin/token';
|
|
1
|
+
import type { CryptoOptions, KeyAgreementIdentity, MethodRegistry } from '@kokuin/token';
|
|
2
2
|
import type { Runtime } from '@sozai/runtime';
|
|
3
3
|
import type { FactorDescriptor } from './factors.js';
|
|
4
4
|
/** X25519 / HKDF-SHA256 / Argon2id / A256GCM. */
|
|
@@ -29,7 +29,7 @@ export type VaultSuite = {
|
|
|
29
29
|
encryptEntry(params: EncryptEntryParams): EncryptEntryResult;
|
|
30
30
|
decryptEntry(params: DecryptEntryParams): Uint8Array;
|
|
31
31
|
};
|
|
32
|
-
export type WrapParams = {
|
|
32
|
+
export type WrapParams = CryptoOptions & {
|
|
33
33
|
runtime: Runtime;
|
|
34
34
|
contentKey: Uint8Array;
|
|
35
35
|
factors: Array<FactorInput>;
|
|
@@ -48,7 +48,7 @@ export type WrapResult = {
|
|
|
48
48
|
iv: Uint8Array;
|
|
49
49
|
wrappedKey: Uint8Array;
|
|
50
50
|
};
|
|
51
|
-
export type UnwrapParams = {
|
|
51
|
+
export type UnwrapParams = CryptoOptions & {
|
|
52
52
|
/** Trusted local state or authenticated metadata, independent of presented descriptors. */
|
|
53
53
|
expectedDescriptorDigest?: string;
|
|
54
54
|
/** Informational digest from the presented wrapping, never used as a trust anchor. */
|
package/lib/suite.js
CHANGED
|
@@ -47,7 +47,7 @@ function invalidArgon2Cost(cost) {
|
|
|
47
47
|
return null;
|
|
48
48
|
}
|
|
49
49
|
// Factor order, wrapping salt, and suite/key/version binding are part of the wire scheme.
|
|
50
|
-
function deriveKEK(secrets, wrappingID, suiteID, keyID, keyVersion) {
|
|
50
|
+
function deriveKEK({ secrets, wrappingID, suiteID, keyID, keyVersion }) {
|
|
51
51
|
const info = fromUTF(`${WRAP_INFO_PREFIX}:${suiteID}:${keyID}:${keyVersion}`);
|
|
52
52
|
return hkdf(sha256, concatBytes(...secrets), fromUTF(wrappingID), info, CONTENT_KEY_LENGTH);
|
|
53
53
|
}
|
|
@@ -55,11 +55,12 @@ function deriveKEK(secrets, wrappingID, suiteID, keyID, keyVersion) {
|
|
|
55
55
|
function wrappingAAD(descriptors) {
|
|
56
56
|
return fromUTF(canonicalStringify(descriptors));
|
|
57
57
|
}
|
|
58
|
-
function entryAAD(suiteID, keyID, keyVersion, entryID) {
|
|
58
|
+
function entryAAD({ suiteID, keyID, keyVersion, entryID }) {
|
|
59
59
|
return fromUTF(`kokuin/vault/entry/v1:${suiteID}:${keyID}:${keyVersion}:${entryID}`);
|
|
60
60
|
}
|
|
61
61
|
async function didFactorSecretForSender(recipientDID, methods) {
|
|
62
|
-
const { sharedSecret, ephemeralPublicKey } = await deriveSharedSecretAsync(
|
|
62
|
+
const { sharedSecret, ephemeralPublicKey } = await deriveSharedSecretAsync({
|
|
63
|
+
recipient: recipientDID,
|
|
63
64
|
methods: methods ?? []
|
|
64
65
|
});
|
|
65
66
|
return {
|
|
@@ -73,7 +74,9 @@ async function didFactorSecretForSender(recipientDID, methods) {
|
|
|
73
74
|
async function secretFactorForSender(runtime, input, cost) {
|
|
74
75
|
const invalid = invalidArgon2Cost(cost);
|
|
75
76
|
if (invalid != null) {
|
|
76
|
-
throw new InvalidVaultFactors(
|
|
77
|
+
throw new InvalidVaultFactors({
|
|
78
|
+
reason: `invalid Argon2 cost: ${invalid}`
|
|
79
|
+
});
|
|
77
80
|
}
|
|
78
81
|
const salt = runtime.getRandomValues(new Uint8Array(SALT_LENGTH));
|
|
79
82
|
const secret = await argon2idAsync(input, salt, {
|
|
@@ -90,11 +93,15 @@ async function secretFactorForSender(runtime, input, cost) {
|
|
|
90
93
|
}
|
|
91
94
|
async function secretFactorForRecipient(kind, input, params) {
|
|
92
95
|
if (input == null) {
|
|
93
|
-
throw new NoAvailableFactor(
|
|
96
|
+
throw new NoAvailableFactor({
|
|
97
|
+
kind
|
|
98
|
+
});
|
|
94
99
|
}
|
|
95
100
|
const { salt, m, t, p } = params;
|
|
96
101
|
if (typeof salt !== 'string' || typeof m !== 'number' || typeof t !== 'number' || typeof p !== 'number') {
|
|
97
|
-
throw new VaultTampered(
|
|
102
|
+
throw new VaultTampered({
|
|
103
|
+
message: `${kind} factor descriptor carries no usable Argon2 parameters`
|
|
104
|
+
});
|
|
98
105
|
}
|
|
99
106
|
const invalid = invalidArgon2Cost({
|
|
100
107
|
m,
|
|
@@ -102,18 +109,26 @@ async function secretFactorForRecipient(kind, input, params) {
|
|
|
102
109
|
p
|
|
103
110
|
});
|
|
104
111
|
if (invalid != null) {
|
|
105
|
-
throw new VaultTampered(
|
|
112
|
+
throw new VaultTampered({
|
|
113
|
+
message: `${kind} factor descriptor has invalid Argon2 cost: ${invalid}`
|
|
114
|
+
});
|
|
106
115
|
}
|
|
107
116
|
let decodedSalt;
|
|
108
117
|
try {
|
|
109
118
|
decodedSalt = fromB64(salt);
|
|
110
119
|
} catch (cause) {
|
|
111
|
-
|
|
112
|
-
|
|
120
|
+
// biome-ignore lint/style/useErrorCause: the params object forwards options.cause.
|
|
121
|
+
throw new VaultTampered({
|
|
122
|
+
message: `${kind} factor descriptor carries an invalid Argon2 salt`,
|
|
123
|
+
options: {
|
|
124
|
+
cause
|
|
125
|
+
}
|
|
113
126
|
});
|
|
114
127
|
}
|
|
115
128
|
if (decodedSalt.length !== SALT_LENGTH) {
|
|
116
|
-
throw new VaultTampered(
|
|
129
|
+
throw new VaultTampered({
|
|
130
|
+
message: `${kind} factor descriptor Argon2 salt must be ${SALT_LENGTH} bytes`
|
|
131
|
+
});
|
|
117
132
|
}
|
|
118
133
|
return await argon2idAsync(input, decodedSalt, {
|
|
119
134
|
m,
|
|
@@ -125,7 +140,9 @@ async function secretFactorForRecipient(kind, input, params) {
|
|
|
125
140
|
// A PIN requires device possession; a passphrase alone does not substitute for it.
|
|
126
141
|
export function validateFactorList(kinds) {
|
|
127
142
|
if (kinds.length === 0) {
|
|
128
|
-
throw new InvalidVaultFactors(
|
|
143
|
+
throw new InvalidVaultFactors({
|
|
144
|
+
reason: 'a wrapping needs at least one factor'
|
|
145
|
+
});
|
|
129
146
|
}
|
|
130
147
|
const seen = new Set();
|
|
131
148
|
for (const kind of kinds){
|
|
@@ -133,23 +150,33 @@ export function validateFactorList(kinds) {
|
|
|
133
150
|
continue;
|
|
134
151
|
}
|
|
135
152
|
if (seen.has(kind)) {
|
|
136
|
-
throw new InvalidVaultFactors(
|
|
153
|
+
throw new InvalidVaultFactors({
|
|
154
|
+
reason: `duplicate "${kind}" factor`
|
|
155
|
+
});
|
|
137
156
|
}
|
|
138
157
|
seen.add(kind);
|
|
139
158
|
}
|
|
140
159
|
if (seen.has('pin') && !kinds.includes('did')) {
|
|
141
|
-
throw new InvalidVaultFactors(
|
|
160
|
+
throw new InvalidVaultFactors({
|
|
161
|
+
reason: 'a "pin" factor requires a "did" factor in the same wrapping'
|
|
162
|
+
});
|
|
142
163
|
}
|
|
143
164
|
}
|
|
144
165
|
async function didFactorSecretForRecipient(identity, params) {
|
|
145
166
|
if (identity == null) {
|
|
146
|
-
throw new NoAvailableFactor(
|
|
167
|
+
throw new NoAvailableFactor({
|
|
168
|
+
kind: 'did'
|
|
169
|
+
});
|
|
147
170
|
}
|
|
148
171
|
const epk = params.epk;
|
|
149
172
|
if (typeof epk !== 'string') {
|
|
150
|
-
throw new VaultTampered(
|
|
173
|
+
throw new VaultTampered({
|
|
174
|
+
message: 'did factor descriptor carries no epk'
|
|
175
|
+
});
|
|
151
176
|
}
|
|
152
|
-
return await identity.agreeKey(
|
|
177
|
+
return await identity.agreeKey({
|
|
178
|
+
ephemeralPublicKey: fromB64(epk)
|
|
179
|
+
});
|
|
153
180
|
}
|
|
154
181
|
const suite1 = {
|
|
155
182
|
id: SUITE_VAULT_1,
|
|
@@ -157,7 +184,24 @@ const suite1 = {
|
|
|
157
184
|
generateContentKey (runtime) {
|
|
158
185
|
return runtime.getRandomValues(new Uint8Array(CONTENT_KEY_LENGTH));
|
|
159
186
|
},
|
|
160
|
-
async wrap (
|
|
187
|
+
async wrap (input) {
|
|
188
|
+
const params = {
|
|
189
|
+
...input,
|
|
190
|
+
contentKey: new Uint8Array(input.contentKey),
|
|
191
|
+
factors: input.factors.map((factor)=>{
|
|
192
|
+
return factor.kind === 'did' ? {
|
|
193
|
+
...factor
|
|
194
|
+
} : {
|
|
195
|
+
...factor,
|
|
196
|
+
input: new Uint8Array(factor.input),
|
|
197
|
+
...factor.cost === undefined ? {} : {
|
|
198
|
+
cost: {
|
|
199
|
+
...factor.cost
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
})
|
|
204
|
+
};
|
|
161
205
|
validateFactorList(params.factors.map((factor)=>factor.kind));
|
|
162
206
|
const secrets = [];
|
|
163
207
|
const descriptors = [];
|
|
@@ -186,18 +230,42 @@ const suite1 = {
|
|
|
186
230
|
}
|
|
187
231
|
}
|
|
188
232
|
}
|
|
189
|
-
const kek = deriveKEK(
|
|
233
|
+
const kek = deriveKEK({
|
|
234
|
+
secrets,
|
|
235
|
+
wrappingID: params.wrappingID,
|
|
236
|
+
suiteID: SUITE_VAULT_1,
|
|
237
|
+
keyID: params.keyID,
|
|
238
|
+
keyVersion: params.keyVersion
|
|
239
|
+
});
|
|
190
240
|
const iv = params.runtime.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
191
241
|
return {
|
|
192
242
|
descriptors,
|
|
193
|
-
descriptorDigest: descriptorDigest(
|
|
243
|
+
descriptorDigest: await descriptorDigest({
|
|
244
|
+
...params,
|
|
245
|
+
descriptors
|
|
246
|
+
}),
|
|
194
247
|
iv,
|
|
195
248
|
wrappedKey: gcm(kek, iv, wrappingAAD(descriptors)).encrypt(params.contentKey)
|
|
196
249
|
};
|
|
197
250
|
},
|
|
198
|
-
async unwrap (
|
|
251
|
+
async unwrap (input) {
|
|
252
|
+
const params = {
|
|
253
|
+
...input,
|
|
254
|
+
descriptors: JSON.parse(JSON.stringify(input.descriptors)),
|
|
255
|
+
iv: new Uint8Array(input.iv),
|
|
256
|
+
wrappedKey: new Uint8Array(input.wrappedKey),
|
|
257
|
+
inputs: input.inputs === undefined ? undefined : Object.fromEntries(Object.entries(input.inputs).flatMap(([kind, bytes])=>{
|
|
258
|
+
return bytes === undefined ? [] : [
|
|
259
|
+
[
|
|
260
|
+
kind,
|
|
261
|
+
new Uint8Array(bytes)
|
|
262
|
+
]
|
|
263
|
+
];
|
|
264
|
+
}))
|
|
265
|
+
};
|
|
199
266
|
if (params.expectedDescriptorDigest !== undefined) {
|
|
200
|
-
checkDescriptorDigest({
|
|
267
|
+
await checkDescriptorDigest({
|
|
268
|
+
crypto: params.crypto,
|
|
201
269
|
descriptors: params.descriptors,
|
|
202
270
|
expectedDescriptorDigest: params.expectedDescriptorDigest
|
|
203
271
|
});
|
|
@@ -216,35 +284,61 @@ const suite1 = {
|
|
|
216
284
|
userSuppliedKinds.push(descriptor.kind);
|
|
217
285
|
break;
|
|
218
286
|
default:
|
|
219
|
-
throw new NoAvailableFactor(
|
|
287
|
+
throw new NoAvailableFactor({
|
|
288
|
+
kind: descriptor.kind
|
|
289
|
+
});
|
|
220
290
|
}
|
|
221
291
|
}
|
|
222
|
-
const kek = deriveKEK(
|
|
292
|
+
const kek = deriveKEK({
|
|
293
|
+
secrets,
|
|
294
|
+
wrappingID: params.wrappingID,
|
|
295
|
+
suiteID: SUITE_VAULT_1,
|
|
296
|
+
keyID: params.keyID,
|
|
297
|
+
keyVersion: params.keyVersion
|
|
298
|
+
});
|
|
223
299
|
try {
|
|
224
300
|
return gcm(kek, params.iv, wrappingAAD(params.descriptors)).decrypt(params.wrappedKey);
|
|
225
301
|
} catch (cause) {
|
|
226
302
|
// A tag failure cannot attribute corruption versus a wrong secret, including DID-only wraps.
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
303
|
+
// biome-ignore lint/style/useErrorCause: the params object forwards options.cause.
|
|
304
|
+
throw new VaultSecretRejected({
|
|
305
|
+
kinds: userSuppliedKinds,
|
|
306
|
+
options: {
|
|
307
|
+
cause,
|
|
308
|
+
keyID: params.keyID
|
|
309
|
+
}
|
|
230
310
|
});
|
|
231
311
|
}
|
|
232
312
|
},
|
|
233
313
|
encryptEntry (params) {
|
|
234
314
|
const iv = params.runtime.getRandomValues(new Uint8Array(IV_LENGTH));
|
|
235
|
-
const aad = entryAAD(
|
|
315
|
+
const aad = entryAAD({
|
|
316
|
+
suiteID: SUITE_VAULT_1,
|
|
317
|
+
keyID: params.keyID,
|
|
318
|
+
keyVersion: params.keyVersion,
|
|
319
|
+
entryID: params.entryID
|
|
320
|
+
});
|
|
236
321
|
return {
|
|
237
322
|
iv,
|
|
238
323
|
ciphertext: gcm(params.contentKey, iv, aad).encrypt(params.plaintext)
|
|
239
324
|
};
|
|
240
325
|
},
|
|
241
326
|
decryptEntry (params) {
|
|
242
|
-
const aad = entryAAD(
|
|
327
|
+
const aad = entryAAD({
|
|
328
|
+
suiteID: SUITE_VAULT_1,
|
|
329
|
+
keyID: params.keyID,
|
|
330
|
+
keyVersion: params.keyVersion,
|
|
331
|
+
entryID: params.entryID
|
|
332
|
+
});
|
|
243
333
|
try {
|
|
244
334
|
return gcm(params.contentKey, params.iv, aad).decrypt(params.ciphertext);
|
|
245
335
|
} catch (cause) {
|
|
246
|
-
|
|
247
|
-
|
|
336
|
+
// biome-ignore lint/style/useErrorCause: the params object forwards options.cause.
|
|
337
|
+
throw new VaultTampered({
|
|
338
|
+
message: 'vault entry failed authentication: it was altered, or is being read under the wrong key version',
|
|
339
|
+
options: {
|
|
340
|
+
cause
|
|
341
|
+
}
|
|
248
342
|
});
|
|
249
343
|
}
|
|
250
344
|
}
|
|
@@ -258,7 +352,9 @@ const SUITES = new Map([
|
|
|
258
352
|
export function getSuite(id) {
|
|
259
353
|
const suite = SUITES.get(id);
|
|
260
354
|
if (suite == null) {
|
|
261
|
-
throw new UnsupportedVaultSuite(
|
|
355
|
+
throw new UnsupportedVaultSuite({
|
|
356
|
+
suite: id
|
|
357
|
+
});
|
|
262
358
|
}
|
|
263
359
|
return suite;
|
|
264
360
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kokuin/vault",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Database-free content keys, ordered factor wrapping and authenticated byte encryption",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vault",
|
|
@@ -24,18 +24,19 @@
|
|
|
24
24
|
"lib/*"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@kokuin/
|
|
28
|
-
"@kokuin/
|
|
29
|
-
"@kokuin/
|
|
27
|
+
"@kokuin/crypto": "^0.7.1",
|
|
28
|
+
"@kokuin/jwe": "^0.7.1",
|
|
29
|
+
"@kokuin/records": "^0.7.2",
|
|
30
|
+
"@kokuin/token": "^0.7.1",
|
|
30
31
|
"@noble/ciphers": "^2.4.0",
|
|
31
32
|
"@noble/hashes": "^2.4.0",
|
|
32
|
-
"@sozai/codec": "^0.4.
|
|
33
|
-
"@sozai/hlc": "^0.1.
|
|
33
|
+
"@sozai/codec": "^0.4.4",
|
|
34
|
+
"@sozai/hlc": "^0.1.1",
|
|
34
35
|
"@sozai/json": "^0.3.0",
|
|
35
36
|
"@sozai/runtime": "^0.1.0"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"@types/node": "^26.6.
|
|
39
|
+
"@types/node": "^26.6.6"
|
|
39
40
|
},
|
|
40
41
|
"publishConfig": {
|
|
41
42
|
"access": "public"
|