@shieldfive/crypto 1.0.0 → 1.1.0
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/CHANGELOG.md +77 -0
- package/README.md +6 -6
- package/dist/format/signature-tail.d.ts +12 -0
- package/dist/format/signature-tail.d.ts.map +1 -1
- package/dist/format/signature-tail.js +24 -0
- package/dist/format/signature-tail.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/internal/byte-queue.d.ts +34 -0
- package/dist/internal/byte-queue.d.ts.map +1 -0
- package/dist/internal/byte-queue.js +82 -0
- package/dist/internal/byte-queue.js.map +1 -0
- package/dist/internal/encoding.d.ts +6 -0
- package/dist/internal/encoding.d.ts.map +1 -1
- package/dist/internal/encoding.js +11 -0
- package/dist/internal/encoding.js.map +1 -1
- package/dist/migration/v0-bridge.d.ts +5 -0
- package/dist/migration/v0-bridge.d.ts.map +1 -1
- package/dist/migration/v0-bridge.js +5 -0
- package/dist/migration/v0-bridge.js.map +1 -1
- package/dist/streams/aes-gcm-v1.d.ts +9 -0
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -1
- package/dist/streams/aes-gcm-v1.js +18 -26
- package/dist/streams/aes-gcm-v1.js.map +1 -1
- package/dist/streams/pq-hybrid-v1.d.ts +18 -0
- package/dist/streams/pq-hybrid-v1.d.ts.map +1 -1
- package/dist/streams/pq-hybrid-v1.js +14 -25
- package/dist/streams/pq-hybrid-v1.js.map +1 -1
- package/dist/suites/aes-gcm-v0/api.d.ts +10 -3
- package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v0/api.js +10 -3
- package/dist/suites/aes-gcm-v0/api.js.map +1 -1
- package/dist/suites/aes-gcm-v1/api.d.ts +9 -0
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v1/api.js +7 -1
- package/dist/suites/aes-gcm-v1/api.js.map +1 -1
- package/dist/suites/aes-gcm-v2/api.d.ts +9 -0
- package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v2/api.js +7 -1
- package/dist/suites/aes-gcm-v2/api.js.map +1 -1
- package/dist/suites/pq-hybrid-v1/api.d.ts +9 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -1
- package/dist/suites/pq-hybrid-v1/api.js +3 -1
- package/dist/suites/pq-hybrid-v1/api.js.map +1 -1
- package/dist/suites/xchacha-v1/api.d.ts +9 -0
- package/dist/suites/xchacha-v1/api.d.ts.map +1 -1
- package/dist/suites/xchacha-v1/api.js +7 -1
- package/dist/suites/xchacha-v1/api.js.map +1 -1
- package/dist/vault/index.d.ts +7 -3
- package/dist/vault/index.d.ts.map +1 -1
- package/dist/vault/index.js +71 -27
- package/dist/vault/index.js.map +1 -1
- package/package.json +2 -2
- package/spec/format-v0.md +11 -3
- package/spec/format-v1.md +3 -1
- package/spec/intake-link-pin.md +130 -0
- package/spec/vault-formats.md +5 -0
- package/src/format/signature-tail.ts +36 -0
- package/src/index.ts +1 -1
- package/src/internal/byte-queue.ts +86 -0
- package/src/internal/encoding.ts +11 -0
- package/src/migration/v0-bridge.ts +5 -0
- package/src/streams/aes-gcm-v1.ts +27 -25
- package/src/streams/pq-hybrid-v1.ts +32 -25
- package/src/suites/aes-gcm-v0/api.ts +10 -3
- package/src/suites/aes-gcm-v1/api.ts +16 -0
- package/src/suites/aes-gcm-v2/api.ts +16 -0
- package/src/suites/pq-hybrid-v1/api.ts +12 -0
- package/src/suites/xchacha-v1/api.ts +16 -0
- package/src/vault/index.ts +92 -26
|
@@ -40,6 +40,7 @@ import {
|
|
|
40
40
|
} from '../../internal/encoding.js'
|
|
41
41
|
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
42
42
|
import {
|
|
43
|
+
assertSignatureRequirement,
|
|
43
44
|
finalizeSignatureMetadata,
|
|
44
45
|
type VerifiedSignature,
|
|
45
46
|
} from '../../format/signature-tail.js'
|
|
@@ -118,6 +119,10 @@ export async function encryptBlob(
|
|
|
118
119
|
`encryptBlob: suitePayloadOverride must be ${AES_GCM_V1_SUITE_PAYLOAD_LENGTH} bytes`,
|
|
119
120
|
)
|
|
120
121
|
}
|
|
122
|
+
// Run the reader's own payload check on the writer side, so the library
|
|
123
|
+
// never emits a file its readers refuse (a non-zero reserved pad passed
|
|
124
|
+
// through suitePayloadOverride). Throws RangeError like the checks above.
|
|
125
|
+
parseAesGcmV1SuitePayload(suitePayload)
|
|
121
126
|
|
|
122
127
|
const header = await buildAuthenticatedHeader(
|
|
123
128
|
{
|
|
@@ -179,6 +184,15 @@ export interface DecryptOptions {
|
|
|
179
184
|
* block"`. Reaches parity with the streaming decoder.
|
|
180
185
|
*/
|
|
181
186
|
expectedSignerPublicKey?: Uint8Array
|
|
187
|
+
/**
|
|
188
|
+
* When true, decryption FAILS unless the file carries a trailing
|
|
189
|
+
* signature block that verifies against `expectedSignerPublicKey`
|
|
190
|
+
* (which must then be supplied). Use this wherever the caller relies
|
|
191
|
+
* on sender attribution: without it, an unsigned or wrongly-signed file
|
|
192
|
+
* still decrypts and only `signature.verified` tells them apart.
|
|
193
|
+
* Defaults to false (legacy/unsigned files decrypt as before).
|
|
194
|
+
*/
|
|
195
|
+
signatureRequired?: boolean
|
|
182
196
|
/**
|
|
183
197
|
* Called once, after the plaintext is fully decrypted, with the file's
|
|
184
198
|
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
@@ -257,6 +271,7 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
257
271
|
// to verify — otherwise a present block is still surfaced (verified:
|
|
258
272
|
// null) without paying for the extra hash over every ciphertext byte.
|
|
259
273
|
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
274
|
+
const signatureRequired = assertSignatureRequirement(options)
|
|
260
275
|
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
261
276
|
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
262
277
|
|
|
@@ -320,6 +335,7 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
320
335
|
trailingBytes,
|
|
321
336
|
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
322
337
|
expectedSignerPublicKey,
|
|
338
|
+
signatureRequired,
|
|
323
339
|
})
|
|
324
340
|
options.onSignature?.(signature)
|
|
325
341
|
|
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
} from '../../internal/encoding.js'
|
|
37
37
|
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
38
38
|
import {
|
|
39
|
+
assertSignatureRequirement,
|
|
39
40
|
finalizeSignatureMetadata,
|
|
40
41
|
type VerifiedSignature,
|
|
41
42
|
} from '../../format/signature-tail.js'
|
|
@@ -112,6 +113,10 @@ export async function encryptBlob(
|
|
|
112
113
|
`encryptBlob: suitePayloadOverride must be ${AES_GCM_V2_SUITE_PAYLOAD_LENGTH} bytes`,
|
|
113
114
|
)
|
|
114
115
|
}
|
|
116
|
+
// Run the reader's own payload check on the writer side, so the library
|
|
117
|
+
// never emits a file its readers refuse (a non-zero reserved pad passed
|
|
118
|
+
// through suitePayloadOverride). Throws RangeError like the checks above.
|
|
119
|
+
parseAesGcmV2SuitePayload(suitePayload)
|
|
115
120
|
|
|
116
121
|
const header = await buildAuthenticatedHeader(
|
|
117
122
|
{
|
|
@@ -168,6 +173,15 @@ export interface DecryptOptions {
|
|
|
168
173
|
* block"`.
|
|
169
174
|
*/
|
|
170
175
|
expectedSignerPublicKey?: Uint8Array
|
|
176
|
+
/**
|
|
177
|
+
* When true, decryption FAILS unless the file carries a trailing
|
|
178
|
+
* signature block that verifies against `expectedSignerPublicKey`
|
|
179
|
+
* (which must then be supplied). Use this wherever the caller relies
|
|
180
|
+
* on sender attribution: without it, an unsigned or wrongly-signed file
|
|
181
|
+
* still decrypts and only `signature.verified` tells them apart.
|
|
182
|
+
* Defaults to false (legacy/unsigned files decrypt as before).
|
|
183
|
+
*/
|
|
184
|
+
signatureRequired?: boolean
|
|
171
185
|
/**
|
|
172
186
|
* Called once, after the plaintext is fully decrypted, with the file's
|
|
173
187
|
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
@@ -235,6 +249,7 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
235
249
|
// Rebuild the sender-attribution transcript only when verifying, so an
|
|
236
250
|
// unsigned decrypt pays nothing for the extra hash over ciphertext.
|
|
237
251
|
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
252
|
+
const signatureRequired = assertSignatureRequirement(options)
|
|
238
253
|
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
239
254
|
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
240
255
|
|
|
@@ -293,6 +308,7 @@ export async function decryptBlob(options: DecryptOptions): Promise<Blob> {
|
|
|
293
308
|
trailingBytes,
|
|
294
309
|
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
295
310
|
expectedSignerPublicKey,
|
|
311
|
+
signatureRequired,
|
|
296
312
|
})
|
|
297
313
|
options.onSignature?.(signature)
|
|
298
314
|
|
|
@@ -42,6 +42,7 @@ import {
|
|
|
42
42
|
import { randomBytes } from '../../internal/runtime.js'
|
|
43
43
|
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
44
44
|
import {
|
|
45
|
+
assertSignatureRequirement,
|
|
45
46
|
finalizeSignatureMetadata,
|
|
46
47
|
type VerifiedSignature,
|
|
47
48
|
} from '../../format/signature-tail.js'
|
|
@@ -176,6 +177,15 @@ export interface DecryptOptions {
|
|
|
176
177
|
* block"`.
|
|
177
178
|
*/
|
|
178
179
|
expectedSignerPublicKey?: Uint8Array
|
|
180
|
+
/**
|
|
181
|
+
* When true, decryption FAILS unless the file carries a trailing
|
|
182
|
+
* signature block that verifies against `expectedSignerPublicKey`
|
|
183
|
+
* (which must then be supplied). Use this wherever the caller relies
|
|
184
|
+
* on sender attribution: without it, an unsigned or wrongly-signed file
|
|
185
|
+
* still decrypts and only `signature.verified` tells them apart.
|
|
186
|
+
* Defaults to false (legacy/unsigned files decrypt as before).
|
|
187
|
+
*/
|
|
188
|
+
signatureRequired?: boolean
|
|
179
189
|
/**
|
|
180
190
|
* Called once, after the plaintext is fully decrypted, with the file's
|
|
181
191
|
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
@@ -242,6 +252,7 @@ export async function decryptBlob(
|
|
|
242
252
|
// Rebuild the sender-attribution transcript only when verifying, so an
|
|
243
253
|
// unsigned decrypt pays nothing for the extra hash over ciphertext.
|
|
244
254
|
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
255
|
+
const signatureRequired = assertSignatureRequirement(options)
|
|
245
256
|
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
246
257
|
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
247
258
|
|
|
@@ -300,6 +311,7 @@ export async function decryptBlob(
|
|
|
300
311
|
trailingBytes,
|
|
301
312
|
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
302
313
|
expectedSignerPublicKey,
|
|
314
|
+
signatureRequired,
|
|
303
315
|
})
|
|
304
316
|
options.onSignature?.(signature)
|
|
305
317
|
|
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
} from '../../internal/encoding.js'
|
|
35
35
|
import { SenderSigTranscript } from '../../identity/sign.js'
|
|
36
36
|
import {
|
|
37
|
+
assertSignatureRequirement,
|
|
37
38
|
finalizeSignatureMetadata,
|
|
38
39
|
type VerifiedSignature,
|
|
39
40
|
} from '../../format/signature-tail.js'
|
|
@@ -96,6 +97,10 @@ export async function encryptBlob(
|
|
|
96
97
|
`encryptBlob: suitePayloadOverride must be ${XCHACHA_V1_SUITE_PAYLOAD_LENGTH} bytes`,
|
|
97
98
|
)
|
|
98
99
|
}
|
|
100
|
+
// Run the reader's own payload check on the writer side, so the library
|
|
101
|
+
// never emits a file its readers refuse (a non-zero reserved pad passed
|
|
102
|
+
// through suitePayloadOverride). Throws RangeError like the checks above.
|
|
103
|
+
parseXChaChaV1SuitePayload(suitePayload)
|
|
99
104
|
|
|
100
105
|
const header = await buildAuthenticatedHeader(
|
|
101
106
|
{
|
|
@@ -151,6 +156,15 @@ export interface DecryptOptions {
|
|
|
151
156
|
* block"`.
|
|
152
157
|
*/
|
|
153
158
|
expectedSignerPublicKey?: Uint8Array
|
|
159
|
+
/**
|
|
160
|
+
* When true, decryption FAILS unless the file carries a trailing
|
|
161
|
+
* signature block that verifies against `expectedSignerPublicKey`
|
|
162
|
+
* (which must then be supplied). Use this wherever the caller relies
|
|
163
|
+
* on sender attribution: without it, an unsigned or wrongly-signed file
|
|
164
|
+
* still decrypts and only `signature.verified` tells them apart.
|
|
165
|
+
* Defaults to false (legacy/unsigned files decrypt as before).
|
|
166
|
+
*/
|
|
167
|
+
signatureRequired?: boolean
|
|
154
168
|
/**
|
|
155
169
|
* Called once, after the plaintext is fully decrypted, with the file's
|
|
156
170
|
* sender-attribution signature — or `null` for a legacy/unsigned file.
|
|
@@ -219,6 +233,7 @@ export async function decryptBlob(
|
|
|
219
233
|
// Rebuild the sender-attribution transcript only when verifying, so an
|
|
220
234
|
// unsigned decrypt pays nothing for the extra hash over ciphertext.
|
|
221
235
|
const expectedSignerPublicKey = options.expectedSignerPublicKey
|
|
236
|
+
const signatureRequired = assertSignatureRequirement(options)
|
|
222
237
|
const transcript = expectedSignerPublicKey ? new SenderSigTranscript() : null
|
|
223
238
|
transcript?.absorbHeader(parsed.unauthenticatedBytes, parsed.totalChunks)
|
|
224
239
|
|
|
@@ -277,6 +292,7 @@ export async function decryptBlob(
|
|
|
277
292
|
trailingBytes,
|
|
278
293
|
transcriptDigest: transcript ? transcript.digest() : new Uint8Array(32),
|
|
279
294
|
expectedSignerPublicKey,
|
|
295
|
+
signatureRequired,
|
|
280
296
|
})
|
|
281
297
|
options.onSignature?.(signature)
|
|
282
298
|
|
package/src/vault/index.ts
CHANGED
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
bytesToHex,
|
|
36
36
|
concatBytes,
|
|
37
37
|
hexToBytes,
|
|
38
|
+
stripBase64Padding,
|
|
38
39
|
} from "../internal/encoding.js";
|
|
39
40
|
import { parseHeader } from "../format/header.js";
|
|
40
41
|
import { hmacSha256 } from "../internal/hmac.js";
|
|
@@ -217,6 +218,44 @@ export function parseNameEnvelope(
|
|
|
217
218
|
return env;
|
|
218
219
|
}
|
|
219
220
|
|
|
221
|
+
const NAME_IV_BYTES = 12;
|
|
222
|
+
const NAME_TAG_BYTES = 16;
|
|
223
|
+
|
|
224
|
+
/** Decode one base64 envelope field, inside the module's error contract. */
|
|
225
|
+
function envelopeField(
|
|
226
|
+
value: string,
|
|
227
|
+
what: string,
|
|
228
|
+
length?: number,
|
|
229
|
+
): Uint8Array {
|
|
230
|
+
let bytes: Uint8Array;
|
|
231
|
+
try {
|
|
232
|
+
bytes = base64ToBytes(value);
|
|
233
|
+
} catch {
|
|
234
|
+
throw new VaultCryptoError("unsupported_envelope", `${what} is not base64`);
|
|
235
|
+
}
|
|
236
|
+
if (length !== undefined && bytes.length !== length) {
|
|
237
|
+
throw new VaultCryptoError(
|
|
238
|
+
"unsupported_envelope",
|
|
239
|
+
`${what} must be ${length} bytes`,
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return bytes;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* The levels a reader tries, cheapest first. The stored `kdf` is data the
|
|
247
|
+
* server can rewrite, so it may only narrow the search, never raise the cost:
|
|
248
|
+
* "interactive" means interactive alone, anything else means interactive then
|
|
249
|
+
* moderate. An honest envelope relabelled "moderate" still opens at the
|
|
250
|
+
* interactive cost, and no envelope costs more than one legacy (kdf-less)
|
|
251
|
+
* envelope already did.
|
|
252
|
+
*/
|
|
253
|
+
function nameKdfLevels(envelope: NameEnvelope): NameKdfLevel[] {
|
|
254
|
+
return envelope.kdf === "interactive"
|
|
255
|
+
? ["interactive"]
|
|
256
|
+
: ["interactive", "moderate"];
|
|
257
|
+
}
|
|
258
|
+
|
|
220
259
|
/**
|
|
221
260
|
* Derive the per-envelope name key. The Argon2id password is the base64 TEXT
|
|
222
261
|
* of the folder key, byte-for-byte what the web app passes to libsodium; the
|
|
@@ -257,7 +296,8 @@ export async function deriveNameKey(
|
|
|
257
296
|
* Decrypt a v4 or v6 name. `rowId` is the database UUID of the row the name
|
|
258
297
|
* belongs to and is REQUIRED for v6 (it is the AAD). An envelope written
|
|
259
298
|
* without a `kdf` field is tried at interactive, then moderate, which is the
|
|
260
|
-
* web app's own fallback order for a device that cannot know the level.
|
|
299
|
+
* web app's own fallback order for a device that cannot know the level. A
|
|
300
|
+
* stored `kdf` never raises the cost (see nameKdfLevels).
|
|
261
301
|
*/
|
|
262
302
|
export async function decryptName(options: {
|
|
263
303
|
envelope: NameEnvelope;
|
|
@@ -268,18 +308,15 @@ export async function decryptName(options: {
|
|
|
268
308
|
if (envelope.v === 6 && !rowId) {
|
|
269
309
|
throw new VaultCryptoError("invalid_input", "v6 names need the row id");
|
|
270
310
|
}
|
|
271
|
-
const iv =
|
|
272
|
-
const salt =
|
|
311
|
+
const iv = envelopeField(envelope.iv, "iv", NAME_IV_BYTES);
|
|
312
|
+
const salt = envelopeField(envelope.salt, "salt", NAME_SALT_BYTES);
|
|
273
313
|
const sealed = concatBytes([
|
|
274
|
-
|
|
275
|
-
|
|
314
|
+
envelopeField(envelope.ct, "ct"),
|
|
315
|
+
envelopeField(envelope.tag, "tag", NAME_TAG_BYTES),
|
|
276
316
|
]);
|
|
277
317
|
const aad =
|
|
278
318
|
envelope.v === 6 ? TEXT_ENCODER.encode(rowId as string) : undefined;
|
|
279
|
-
const
|
|
280
|
-
? [envelope.kdf]
|
|
281
|
-
: ["interactive", "moderate"];
|
|
282
|
-
for (const level of levels) {
|
|
319
|
+
for (const level of nameKdfLevels(envelope)) {
|
|
283
320
|
const key = await deriveNameKey(folderKey, salt, level);
|
|
284
321
|
const pt = await aesGcmOpen(key, iv, sealed, aad);
|
|
285
322
|
if (pt) {
|
|
@@ -311,12 +348,13 @@ export async function decryptNameWithKey(options: {
|
|
|
311
348
|
throw new VaultCryptoError("invalid_input", "v6 names need the row id");
|
|
312
349
|
}
|
|
313
350
|
const sealed = concatBytes([
|
|
314
|
-
|
|
315
|
-
|
|
351
|
+
envelopeField(envelope.ct, "ct"),
|
|
352
|
+
envelopeField(envelope.tag, "tag", NAME_TAG_BYTES),
|
|
316
353
|
]);
|
|
317
354
|
const aad =
|
|
318
355
|
envelope.v === 6 ? TEXT_ENCODER.encode(rowId as string) : undefined;
|
|
319
|
-
const
|
|
356
|
+
const iv = envelopeField(envelope.iv, "iv", NAME_IV_BYTES);
|
|
357
|
+
const pt = await aesGcmOpen(nameKey, iv, sealed, aad);
|
|
320
358
|
if (pt) {
|
|
321
359
|
try {
|
|
322
360
|
return TEXT_DECODER.decode(pt);
|
|
@@ -341,11 +379,8 @@ export async function deriveNameKeyForEnvelope(options: {
|
|
|
341
379
|
rowId?: string;
|
|
342
380
|
}): Promise<Uint8Array> {
|
|
343
381
|
const { envelope, parentKey, rowId } = options;
|
|
344
|
-
const salt =
|
|
345
|
-
const
|
|
346
|
-
? [envelope.kdf]
|
|
347
|
-
: ["interactive", "moderate"];
|
|
348
|
-
for (const level of levels) {
|
|
382
|
+
const salt = envelopeField(envelope.salt, "salt", NAME_SALT_BYTES);
|
|
383
|
+
for (const level of nameKdfLevels(envelope)) {
|
|
349
384
|
const nameKey = await deriveNameKey(parentKey, salt, level);
|
|
350
385
|
try {
|
|
351
386
|
await decryptNameWithKey(
|
|
@@ -454,10 +489,9 @@ function assertUuid(value: string, what: string): void {
|
|
|
454
489
|
}
|
|
455
490
|
|
|
456
491
|
function toBase64Url(bytes: Uint8Array): string {
|
|
457
|
-
return
|
|
458
|
-
.replace(/\+/g, "-")
|
|
459
|
-
|
|
460
|
-
.replace(/=+$/, "");
|
|
492
|
+
return stripBase64Padding(
|
|
493
|
+
bytesToBase64(bytes).replace(/\+/g, "-").replace(/\//g, "_"),
|
|
494
|
+
);
|
|
461
495
|
}
|
|
462
496
|
|
|
463
497
|
function fromBase64Url(value: string): Uint8Array {
|
|
@@ -471,11 +505,21 @@ function fromBase64Url(value: string): Uint8Array {
|
|
|
471
505
|
);
|
|
472
506
|
}
|
|
473
507
|
const b64 = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
508
|
+
let bytes: Uint8Array;
|
|
474
509
|
try {
|
|
475
|
-
|
|
510
|
+
bytes = base64ToBytes(b64 + "=".repeat((4 - (b64.length % 4)) % 4));
|
|
476
511
|
} catch {
|
|
477
512
|
throw new VaultCryptoError("invalid_connection_string", "bad base64url");
|
|
478
513
|
}
|
|
514
|
+
// One value, one spelling: reject encodings whose final character carries
|
|
515
|
+
// non-zero padding bits, which decoders silently discard.
|
|
516
|
+
if (toBase64Url(bytes) !== value) {
|
|
517
|
+
throw new VaultCryptoError(
|
|
518
|
+
"invalid_connection_string",
|
|
519
|
+
"non-canonical base64url",
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
return bytes;
|
|
479
523
|
}
|
|
480
524
|
|
|
481
525
|
/** SHA-256 of an ML-KEM public key, hex. Pinned in the connection string. */
|
|
@@ -491,8 +535,11 @@ export function publicKeyFingerprint(mlKemPublicKey: Uint8Array): string {
|
|
|
491
535
|
*
|
|
492
536
|
* `chunk_0` is the first frame INCLUDING its 4-byte big-endian length prefix.
|
|
493
537
|
* The server re-reads exactly those ranges from storage and recomputes the MAC,
|
|
494
|
-
* so a matching proof says the
|
|
495
|
-
*
|
|
538
|
+
* so a matching proof says the header and first chunk frame storage holds are
|
|
539
|
+
* the ones the client encrypted. It commits to nothing after chunk_0: a
|
|
540
|
+
* truncated or replaced tail still verifies, and is caught only when the
|
|
541
|
+
* client decrypts (each chunk is AEAD-authenticated). It says nothing about
|
|
542
|
+
* the plaintext.
|
|
496
543
|
*
|
|
497
544
|
* It lives here because it is a keyed construction over ShieldFive's own
|
|
498
545
|
* container format, and the clients that produce it (the web app, and the MCP
|
|
@@ -512,7 +559,15 @@ export async function buildUploadProofV3(options: {
|
|
|
512
559
|
"proofKeyHex must be 32 bytes of hex",
|
|
513
560
|
);
|
|
514
561
|
}
|
|
515
|
-
|
|
562
|
+
let header: ReturnType<typeof parseHeader>;
|
|
563
|
+
try {
|
|
564
|
+
header = parseHeader(ciphertext);
|
|
565
|
+
} catch {
|
|
566
|
+
throw new VaultCryptoError(
|
|
567
|
+
"invalid_input",
|
|
568
|
+
"ciphertext does not start with a valid header",
|
|
569
|
+
);
|
|
570
|
+
}
|
|
516
571
|
const lengthOffset = header.headerLength;
|
|
517
572
|
if (ciphertext.length < lengthOffset + LENGTH_PREFIX_BYTES) {
|
|
518
573
|
throw new VaultCryptoError(
|
|
@@ -527,7 +582,18 @@ export async function buildUploadProofV3(options: {
|
|
|
527
582
|
);
|
|
528
583
|
const chunkZeroLength = view.getUint32(lengthOffset, false);
|
|
529
584
|
const end = lengthOffset + LENGTH_PREFIX_BYTES + chunkZeroLength;
|
|
530
|
-
|
|
585
|
+
// Match the server verifier's bounds: a frame is at least one ciphertext
|
|
586
|
+
// byte plus the 16-byte tag, and at most chunkSize plus the tag.
|
|
587
|
+
if (
|
|
588
|
+
chunkZeroLength < 1 + TAG_BYTES ||
|
|
589
|
+
chunkZeroLength > header.chunkSize + TAG_BYTES
|
|
590
|
+
) {
|
|
591
|
+
throw new VaultCryptoError(
|
|
592
|
+
"invalid_input",
|
|
593
|
+
"first chunk length is outside the valid frame range",
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
if (ciphertext.length < end) {
|
|
531
597
|
throw new VaultCryptoError(
|
|
532
598
|
"invalid_input",
|
|
533
599
|
"ciphertext is missing its first chunk",
|