@shieldfive/crypto 1.0.0-beta.2 → 1.0.0-beta.4
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 +70 -0
- package/SECURITY.md +33 -9
- package/dist/identity/index.d.ts +1 -1
- package/dist/identity/index.d.ts.map +1 -1
- package/dist/identity/index.js +1 -1
- package/dist/identity/index.js.map +1 -1
- package/dist/identity/sign.d.ts +73 -21
- package/dist/identity/sign.d.ts.map +1 -1
- package/dist/identity/sign.js +105 -35
- package/dist/identity/sign.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/streams/aes-gcm-v1.d.ts +9 -5
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -1
- package/dist/streams/aes-gcm-v1.js +26 -24
- package/dist/streams/aes-gcm-v1.js.map +1 -1
- package/dist/streams/pq-hybrid-v1.d.ts +4 -3
- package/dist/streams/pq-hybrid-v1.d.ts.map +1 -1
- package/dist/streams/pq-hybrid-v1.js +20 -18
- package/dist/streams/pq-hybrid-v1.js.map +1 -1
- package/dist/suites/aes-gcm-v1/api.d.ts +15 -2
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -1
- package/dist/suites/aes-gcm-v1/api.js.map +1 -1
- package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -1
- package/dist/suites/pq-hybrid-v1/index.js +11 -4
- package/dist/suites/pq-hybrid-v1/index.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.map +1 -1
- package/package.json +2 -1
- package/spec/format-v1.md +59 -17
- package/spec/key-derivation.md +9 -2
- package/spec/threat-model.md +8 -2
- package/src/identity/index.ts +3 -2
- package/src/identity/sign.ts +118 -40
- package/src/index.ts +1 -1
- package/src/streams/aes-gcm-v1.ts +37 -30
- package/src/streams/pq-hybrid-v1.ts +29 -21
- package/src/suites/aes-gcm-v1/api.ts +15 -2
- package/src/suites/pq-hybrid-v1/index.ts +14 -8
- package/src/suites/xchacha-v1/api.ts +9 -0
|
@@ -55,8 +55,9 @@ import {
|
|
|
55
55
|
buildSignatureBlock,
|
|
56
56
|
deriveEd25519PublicKey,
|
|
57
57
|
ED25519_SECRET_KEY_LENGTH,
|
|
58
|
+
SenderSigTranscript,
|
|
58
59
|
SIGNATURE_ALGO_ED25519,
|
|
59
|
-
|
|
60
|
+
signSenderTranscript,
|
|
60
61
|
} from '../identity/sign.js'
|
|
61
62
|
import {
|
|
62
63
|
type DecryptionMetadata,
|
|
@@ -99,9 +100,10 @@ export interface PqHybridV1EncryptStreamOptions {
|
|
|
99
100
|
suitePayload: Uint8Array
|
|
100
101
|
/**
|
|
101
102
|
* Optional 32-byte Ed25519 secret seed. When provided, the stream
|
|
102
|
-
* appends a signature block
|
|
103
|
-
*
|
|
104
|
-
*
|
|
103
|
+
* appends a signature block after the final chunk, signing a transcript
|
|
104
|
+
* that commits to the header and every chunk's full ciphertext (see
|
|
105
|
+
* {@link SenderSigTranscript}). When omitted, no block is emitted and
|
|
106
|
+
* the file decrypts as unsigned (legacy behavior).
|
|
105
107
|
*/
|
|
106
108
|
ed25519SecretKey?: Uint8Array
|
|
107
109
|
}
|
|
@@ -192,15 +194,14 @@ export function createPqHybridV1EncryptStream(
|
|
|
192
194
|
// - chunk index counter
|
|
193
195
|
// - bytes consumed counter (for sanity check vs plaintextSize)
|
|
194
196
|
// - context (lazily initialized)
|
|
195
|
-
// -
|
|
196
|
-
//
|
|
197
|
+
// - signature transcript (present only when signing): commits to the
|
|
198
|
+
// header and every chunk's full ciphertext, hashed incrementally
|
|
197
199
|
let pending = new Uint8Array(0)
|
|
198
200
|
let chunkIndex = 0
|
|
199
201
|
let bytesConsumed = 0
|
|
200
202
|
let contextPromise: ReturnType<typeof createChunkContext> | null = null
|
|
201
203
|
let headerEmitted = false
|
|
202
|
-
|
|
203
|
-
const chunkMacs: Uint8Array[] = []
|
|
204
|
+
const transcript = ed25519SecretKey ? new SenderSigTranscript() : null
|
|
204
205
|
|
|
205
206
|
async function ensureHeader(
|
|
206
207
|
controller: TransformStreamDefaultController<Uint8Array>,
|
|
@@ -216,7 +217,7 @@ export function createPqHybridV1EncryptStream(
|
|
|
216
217
|
})
|
|
217
218
|
const macKey = await deriveHeaderMacKey(combinedKey, fileId)
|
|
218
219
|
const mac = await hmacSha256(macKey, unauth)
|
|
219
|
-
|
|
220
|
+
transcript?.absorbHeader(unauth, totalChunks)
|
|
220
221
|
controller.enqueue(concatBytes([unauth, mac]))
|
|
221
222
|
headerEmitted = true
|
|
222
223
|
}
|
|
@@ -232,9 +233,9 @@ export function createPqHybridV1EncryptStream(
|
|
|
232
233
|
const ct = await encryptChunk(ctx, chunkIndex, plaintext)
|
|
233
234
|
controller.enqueue(uint32BE(ct.length))
|
|
234
235
|
controller.enqueue(ct)
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
236
|
+
// Commit the FULL chunk ciphertext (body + tag) to the signature
|
|
237
|
+
// transcript, not just the malleable Poly1305 tag.
|
|
238
|
+
transcript?.absorbChunk(chunkIndex, ct)
|
|
238
239
|
chunkIndex += 1
|
|
239
240
|
}
|
|
240
241
|
|
|
@@ -290,16 +291,15 @@ export function createPqHybridV1EncryptStream(
|
|
|
290
291
|
return
|
|
291
292
|
}
|
|
292
293
|
if (ed25519SecretKey) {
|
|
293
|
-
if (!
|
|
294
|
+
if (!transcript) {
|
|
294
295
|
controller.error(
|
|
295
|
-
new Error('encrypt stream:
|
|
296
|
+
new Error('encrypt stream: signature transcript not initialized'),
|
|
296
297
|
)
|
|
297
298
|
return
|
|
298
299
|
}
|
|
299
|
-
const signature =
|
|
300
|
+
const signature = signSenderTranscript({
|
|
300
301
|
ed25519SecretKey,
|
|
301
|
-
|
|
302
|
-
chunkMacs,
|
|
302
|
+
transcriptDigest: transcript.digest(),
|
|
303
303
|
})
|
|
304
304
|
const publicKey = deriveEd25519PublicKey(ed25519SecretKey)
|
|
305
305
|
controller.enqueue(
|
|
@@ -436,7 +436,9 @@ export function createPqHybridV1DecryptStream(
|
|
|
436
436
|
let bytesEmitted = 0
|
|
437
437
|
let nextCipherLen = -1
|
|
438
438
|
let contextPromise: ReturnType<typeof createChunkContext> | null = null
|
|
439
|
-
|
|
439
|
+
// Rebuilds the signer's transcript as chunks arrive so a trailing
|
|
440
|
+
// signature block can be verified against the actual ciphertext.
|
|
441
|
+
const transcript = new SenderSigTranscript()
|
|
440
442
|
|
|
441
443
|
function append(input: Uint8Array): void {
|
|
442
444
|
const merged = new Uint8Array(buffer.length + input.length)
|
|
@@ -522,6 +524,11 @@ export function createPqHybridV1DecryptStream(
|
|
|
522
524
|
parsePqHybridV1SuitePayload(parsedHeader.suitePayload)
|
|
523
525
|
}
|
|
524
526
|
|
|
527
|
+
transcript.absorbHeader(
|
|
528
|
+
parsedHeader.unauthenticatedBytes,
|
|
529
|
+
parsedHeader.totalChunks,
|
|
530
|
+
)
|
|
531
|
+
|
|
525
532
|
contextPromise = createChunkContext(
|
|
526
533
|
combinedKey,
|
|
527
534
|
parsedHeader.fileId,
|
|
@@ -568,7 +575,9 @@ export function createPqHybridV1DecryptStream(
|
|
|
568
575
|
const pt = await decryptChunk(ctx, chunkIndex, ctBytes)
|
|
569
576
|
bytesEmitted += pt.length
|
|
570
577
|
controller.enqueue(pt)
|
|
571
|
-
|
|
578
|
+
// Commit the FULL chunk ciphertext (body + tag) to the transcript for
|
|
579
|
+
// optional signature verification at flush time.
|
|
580
|
+
transcript.absorbChunk(chunkIndex, ctBytes)
|
|
572
581
|
chunkIndex += 1
|
|
573
582
|
return true
|
|
574
583
|
}
|
|
@@ -612,8 +621,7 @@ export function createPqHybridV1DecryptStream(
|
|
|
612
621
|
}
|
|
613
622
|
const signatureResult = finalizeSignatureMetadata({
|
|
614
623
|
trailingBytes: buffer,
|
|
615
|
-
|
|
616
|
-
chunkMacs,
|
|
624
|
+
transcriptDigest: transcript.digest(),
|
|
617
625
|
expectedSignerPublicKey,
|
|
618
626
|
})
|
|
619
627
|
buffer = new Uint8Array(0)
|
|
@@ -43,9 +43,22 @@ const LENGTH_PREFIX_BYTES = 4
|
|
|
43
43
|
|
|
44
44
|
export interface EncryptOptions {
|
|
45
45
|
blob: Blob
|
|
46
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* Optional pre-existing content key (32 bytes). If omitted, generated fresh.
|
|
48
|
+
*
|
|
49
|
+
* SECURITY: `contentKey` and `fileId` together form a one-use AEAD nonce
|
|
50
|
+
* namespace. Supplying BOTH overrides transfers the uniqueness obligation to
|
|
51
|
+
* the caller — reusing the same (contentKey, fileId) pair across two
|
|
52
|
+
* different plaintexts reuses the AEAD key and nonce and is catastrophic
|
|
53
|
+
* (enables plaintext recovery and authenticated chunk substitution). Never
|
|
54
|
+
* reuse a pair. Omitting both (the default) generates fresh material per file.
|
|
55
|
+
*/
|
|
47
56
|
contentKey?: Uint8Array
|
|
48
|
-
/**
|
|
57
|
+
/**
|
|
58
|
+
* Optional pre-existing file_id (16 bytes). If omitted, generated fresh.
|
|
59
|
+
* See the SECURITY note on `contentKey` — do not reuse a (contentKey, fileId)
|
|
60
|
+
* pair across different plaintexts.
|
|
61
|
+
*/
|
|
49
62
|
fileId?: Uint8Array
|
|
50
63
|
/** Optional embedded wrapped key + IV (60 + 12 bytes). Defaults to zeros. */
|
|
51
64
|
suitePayloadOverride?: Uint8Array
|
|
@@ -332,15 +332,21 @@ export async function decapsulateFromHeader(options: {
|
|
|
332
332
|
recipientSecretKey,
|
|
333
333
|
)
|
|
334
334
|
|
|
335
|
-
// 2) Classical unwrap.
|
|
335
|
+
// 2) Classical unwrap. libsodium-wrappers THROWS on Poly1305 MAC failure
|
|
336
|
+
// (it does not return false), so the previous `if (!classicalShare)` guard
|
|
337
|
+
// was unreachable dead code. Catch the throw and re-raise a single generic
|
|
338
|
+
// error, folding the classical-unwrap failure into one message instead of
|
|
339
|
+
// surfacing libsodium's internal "wrong secret key" string to the caller.
|
|
336
340
|
const wrapped48 = parsed.classicalWrapped.slice(0, 48)
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
341
|
+
let classicalShare: Uint8Array
|
|
342
|
+
try {
|
|
343
|
+
classicalShare = sodium.crypto_secretbox_open_easy(
|
|
344
|
+
wrapped48,
|
|
345
|
+
parsed.classicalNonce,
|
|
346
|
+
envelopeKey,
|
|
347
|
+
)
|
|
348
|
+
} catch {
|
|
349
|
+
throw new Error('decapsulate: unwrap failed')
|
|
344
350
|
}
|
|
345
351
|
|
|
346
352
|
// 3) Combine.
|
|
@@ -37,7 +37,16 @@ const LENGTH_PREFIX_BYTES = 4
|
|
|
37
37
|
|
|
38
38
|
export interface XChaChaEncryptOptions {
|
|
39
39
|
blob: Blob
|
|
40
|
+
/**
|
|
41
|
+
* SECURITY: `contentKey` and `fileId` together form a one-use AEAD nonce
|
|
42
|
+
* namespace. Supplying BOTH overrides transfers the uniqueness obligation to
|
|
43
|
+
* the caller — reusing the same (contentKey, fileId) pair across two
|
|
44
|
+
* different plaintexts reuses the AEAD key and nonce and is catastrophic
|
|
45
|
+
* (enables plaintext recovery and authenticated chunk substitution). Never
|
|
46
|
+
* reuse a pair. Omitting both (the default) generates fresh material per file.
|
|
47
|
+
*/
|
|
40
48
|
contentKey?: Uint8Array
|
|
49
|
+
/** See the SECURITY note on `contentKey`. */
|
|
41
50
|
fileId?: Uint8Array
|
|
42
51
|
suitePayloadOverride?: Uint8Array
|
|
43
52
|
chunkSize?: number
|