@shieldfive/crypto 1.0.0-alpha.2
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 +81 -0
- package/LICENSE +201 -0
- package/README.md +241 -0
- package/SECURITY.md +123 -0
- package/dist/format/header.d.ts +60 -0
- package/dist/format/header.d.ts.map +1 -0
- package/dist/format/header.js +203 -0
- package/dist/format/header.js.map +1 -0
- package/dist/identity/index.d.ts +117 -0
- package/dist/identity/index.d.ts.map +1 -0
- package/dist/identity/index.js +217 -0
- package/dist/identity/index.js.map +1 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +124 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/encoding.d.ts +33 -0
- package/dist/internal/encoding.d.ts.map +1 -0
- package/dist/internal/encoding.js +136 -0
- package/dist/internal/encoding.js.map +1 -0
- package/dist/internal/hkdf.d.ts +14 -0
- package/dist/internal/hkdf.d.ts.map +1 -0
- package/dist/internal/hkdf.js +27 -0
- package/dist/internal/hkdf.js.map +1 -0
- package/dist/internal/hmac.d.ts +9 -0
- package/dist/internal/hmac.d.ts.map +1 -0
- package/dist/internal/hmac.js +18 -0
- package/dist/internal/hmac.js.map +1 -0
- package/dist/internal/runtime.d.ts +29 -0
- package/dist/internal/runtime.d.ts.map +1 -0
- package/dist/internal/runtime.js +67 -0
- package/dist/internal/runtime.js.map +1 -0
- package/dist/internal/sodium.d.ts +12 -0
- package/dist/internal/sodium.d.ts.map +1 -0
- package/dist/internal/sodium.js +34 -0
- package/dist/internal/sodium.js.map +1 -0
- package/dist/internal/types.d.ts +94 -0
- package/dist/internal/types.d.ts.map +1 -0
- package/dist/internal/types.js +65 -0
- package/dist/internal/types.js.map +1 -0
- package/dist/kdf/argon2id.d.ts +72 -0
- package/dist/kdf/argon2id.d.ts.map +1 -0
- package/dist/kdf/argon2id.js +97 -0
- package/dist/kdf/argon2id.js.map +1 -0
- package/dist/migration/v0-bridge.d.ts +94 -0
- package/dist/migration/v0-bridge.d.ts.map +1 -0
- package/dist/migration/v0-bridge.js +203 -0
- package/dist/migration/v0-bridge.js.map +1 -0
- package/dist/streams/aes-gcm-v1.d.ts +76 -0
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -0
- package/dist/streams/aes-gcm-v1.js +322 -0
- package/dist/streams/aes-gcm-v1.js.map +1 -0
- package/dist/suites/aes-gcm-v0/api.d.ts +48 -0
- package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v0/api.js +110 -0
- package/dist/suites/aes-gcm-v0/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/api.d.ts +54 -0
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/api.js +179 -0
- package/dist/suites/aes-gcm-v1/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/index.d.ts +56 -0
- package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/index.js +174 -0
- package/dist/suites/aes-gcm-v1/index.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts +40 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.js +157 -0
- package/dist/suites/pq-hybrid-v1/api.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts +101 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.js +225 -0
- package/dist/suites/pq-hybrid-v1/index.js.map +1 -0
- package/dist/suites/xchacha-v1/api.d.ts +29 -0
- package/dist/suites/xchacha-v1/api.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/api.js +151 -0
- package/dist/suites/xchacha-v1/api.js.map +1 -0
- package/dist/suites/xchacha-v1/index.d.ts +50 -0
- package/dist/suites/xchacha-v1/index.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/index.js +133 -0
- package/dist/suites/xchacha-v1/index.js.map +1 -0
- package/package.json +112 -0
- package/spec/format-v0.md +81 -0
- package/spec/format-v1.md +245 -0
- package/spec/key-derivation.md +122 -0
- package/spec/threat-model.md +152 -0
- package/src/format/header.ts +284 -0
- package/src/identity/index.ts +358 -0
- package/src/index.ts +197 -0
- package/src/internal/encoding.ts +153 -0
- package/src/internal/hkdf.ts +47 -0
- package/src/internal/hmac.ts +33 -0
- package/src/internal/runtime.ts +80 -0
- package/src/internal/sodium.ts +38 -0
- package/src/internal/types.ts +128 -0
- package/src/kdf/argon2id.ts +164 -0
- package/src/migration/v0-bridge.ts +347 -0
- package/src/streams/aes-gcm-v1.ts +443 -0
- package/src/suites/aes-gcm-v0/api.ts +155 -0
- package/src/suites/aes-gcm-v1/api.ts +291 -0
- package/src/suites/aes-gcm-v1/index.ts +266 -0
- package/src/suites/pq-hybrid-v1/api.ts +259 -0
- package/src/suites/pq-hybrid-v1/index.ts +343 -0
- package/src/suites/xchacha-v1/api.ts +251 -0
- package/src/suites/xchacha-v1/index.ts +213 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"v0-bridge.js","sourceRoot":"","sources":["../../src/migration/v0-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AAC/D,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,WAAW,EACX,YAAY,EACZ,qBAAqB,EACrB,YAAY,GAEb,MAAM,6BAA6B,CAAA;AAEpC,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,WAAW,EACX,YAAY,EACZ,qBAAqB,EACrB,YAAY,GAEb,CAAA;AAqCD,SAAS,SAAS,CAAC,MAAkB,EAAE,OAAe;IACpD,MAAM,EAAE,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,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AG,IAAI,CAAA;gBACZ,KAAK,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAA;gBACtC,OAAM;YACR,CAAC;YAED,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAA;QAChD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,KAAK,CAAC,WAAW,CAAC;gBAChB,IAAI,EAAE,OAAO;gBACb,KAAK,EAAG,GAAa,CAAC,OAAO,IAAI,eAAe;aACjD,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming encrypt/decrypt for the v1 wire format.
|
|
3
|
+
*
|
|
4
|
+
* The whole-blob APIs in `suites/<name>/api.ts` materialize entire files in
|
|
5
|
+
* memory. That's fine for small files; for multi-gigabyte files on a
|
|
6
|
+
* mobile device it isn't.
|
|
7
|
+
*
|
|
8
|
+
* This module exposes a WHATWG TransformStream factory that ingests
|
|
9
|
+
* plaintext (or ciphertext) chunks from any ReadableStream and emits the
|
|
10
|
+
* other side, without ever holding more than one chunk-plus-header in
|
|
11
|
+
* memory at once.
|
|
12
|
+
*
|
|
13
|
+
* Currently implemented:
|
|
14
|
+
* - Suite 0x01 (aes-256-gcm-v1) encrypt
|
|
15
|
+
* - Suite 0x01 (aes-256-gcm-v1) decrypt
|
|
16
|
+
*
|
|
17
|
+
* The XChaCha and PQ-hybrid suites use the same chunk format and AAD;
|
|
18
|
+
* adding them is a copy of the AES module with the suite primitive
|
|
19
|
+
* swapped, but we ship one suite first to lock the streaming contract.
|
|
20
|
+
*/
|
|
21
|
+
export interface AesGcmV1EncryptStreamOptions {
|
|
22
|
+
/** Total plaintext byte count. Required because the header carries it. */
|
|
23
|
+
plaintextSize: number;
|
|
24
|
+
/** Plaintext bytes per chunk. Default 4 MiB. */
|
|
25
|
+
chunkSize?: number;
|
|
26
|
+
/** Pre-existing 32-byte content key, or generated if omitted. */
|
|
27
|
+
contentKey?: Uint8Array;
|
|
28
|
+
/** Pre-existing 16-byte file_id, or generated if omitted. */
|
|
29
|
+
fileId?: Uint8Array;
|
|
30
|
+
/** Optional embedded suite payload (60-byte wrapped key + 12-byte IV). */
|
|
31
|
+
suitePayloadOverride?: Uint8Array;
|
|
32
|
+
}
|
|
33
|
+
export interface AesGcmV1EncryptStreamResult {
|
|
34
|
+
/** TransformStream<Uint8Array, Uint8Array> emitting the v1 wire format. */
|
|
35
|
+
stream: TransformStream<Uint8Array, Uint8Array>;
|
|
36
|
+
/** The content key used (for envelope wrapping by the caller). */
|
|
37
|
+
contentKey: Uint8Array;
|
|
38
|
+
/** The file_id used (for application-level metadata storage). */
|
|
39
|
+
fileId: Uint8Array;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Build a TransformStream that encrypts plaintext into v1 wire-format
|
|
43
|
+
* ciphertext on the fly. The stream emits the header first, then each
|
|
44
|
+
* chunk as it arrives.
|
|
45
|
+
*
|
|
46
|
+
* The caller MUST pass `plaintextSize` because the header records it.
|
|
47
|
+
* If the caller doesn't know the size up front, they must accumulate
|
|
48
|
+
* the plaintext into a buffer first — there is no way to write a
|
|
49
|
+
* self-describing header otherwise.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createAesGcmV1EncryptStream(options: AesGcmV1EncryptStreamOptions): AesGcmV1EncryptStreamResult;
|
|
52
|
+
export interface AesGcmV1DecryptStreamOptions {
|
|
53
|
+
contentKey: Uint8Array;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Build a TransformStream that decrypts a v1 ciphertext stream into
|
|
57
|
+
* plaintext. Unlike the whole-blob decryptor, this version processes
|
|
58
|
+
* input incrementally — useful for very large files or when ciphertext
|
|
59
|
+
* arrives chunked over the network.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createAesGcmV1DecryptStream(options: AesGcmV1DecryptStreamOptions): TransformStream<Uint8Array, Uint8Array>;
|
|
62
|
+
/**
|
|
63
|
+
* Pipe a ReadableStream<Uint8Array> through the AES-GCM-v1 encrypt
|
|
64
|
+
* transform and return the resulting ReadableStream + key material.
|
|
65
|
+
*/
|
|
66
|
+
export declare function encryptStreamAesGcmV1(source: ReadableStream<Uint8Array>, options: AesGcmV1EncryptStreamOptions): {
|
|
67
|
+
ciphertext: ReadableStream<Uint8Array>;
|
|
68
|
+
contentKey: Uint8Array;
|
|
69
|
+
fileId: Uint8Array;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Pipe a ReadableStream<Uint8Array> through the AES-GCM-v1 decrypt
|
|
73
|
+
* transform and return the resulting plaintext ReadableStream.
|
|
74
|
+
*/
|
|
75
|
+
export declare function decryptStreamAesGcmV1(source: ReadableStream<Uint8Array>, options: AesGcmV1DecryptStreamOptions): ReadableStream<Uint8Array>;
|
|
76
|
+
//# sourceMappingURL=aes-gcm-v1.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm-v1.d.ts","sourceRoot":"","sources":["../../src/streams/aes-gcm-v1.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AA6BH,MAAM,WAAW,4BAA4B;IAC3C,0EAA0E;IAC1E,aAAa,EAAE,MAAM,CAAA;IACrB,gDAAgD;IAChD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,iEAAiE;IACjE,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,UAAU,CAAA;IACnB,0EAA0E;IAC1E,oBAAoB,CAAC,EAAE,UAAU,CAAA;CAClC;AAED,MAAM,WAAW,2BAA2B;IAC1C,2EAA2E;IAC3E,MAAM,EAAE,eAAe,CAAC,UAAU,EAAE,UAAU,CAAC,CAAA;IAC/C,kEAAkE;IAClE,UAAU,EAAE,UAAU,CAAA;IACtB,iEAAiE;IACjE,MAAM,EAAE,UAAU,CAAA;CACnB;AAED;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,OAAO,EAAE,4BAA4B,GACpC,2BAA2B,CAwI7B;AAMD,MAAM,WAAW,4BAA4B;IAC3C,UAAU,EAAE,UAAU,CAAA;CACvB;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,OAAO,EAAE,4BAA4B,GACpC,eAAe,CAAC,UAAU,EAAE,UAAU,CAAC,CA0KzC;AAMD;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,OAAO,EAAE,4BAA4B,GACpC;IACD,UAAU,EAAE,cAAc,CAAC,UAAU,CAAC,CAAA;IACtC,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,UAAU,CAAA;CACnB,CAQA;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,OAAO,EAAE,4BAA4B,GACpC,cAAc,CAAC,UAAU,CAAC,CAE5B"}
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming encrypt/decrypt for the v1 wire format.
|
|
3
|
+
*
|
|
4
|
+
* The whole-blob APIs in `suites/<name>/api.ts` materialize entire files in
|
|
5
|
+
* memory. That's fine for small files; for multi-gigabyte files on a
|
|
6
|
+
* mobile device it isn't.
|
|
7
|
+
*
|
|
8
|
+
* This module exposes a WHATWG TransformStream factory that ingests
|
|
9
|
+
* plaintext (or ciphertext) chunks from any ReadableStream and emits the
|
|
10
|
+
* other side, without ever holding more than one chunk-plus-header in
|
|
11
|
+
* memory at once.
|
|
12
|
+
*
|
|
13
|
+
* Currently implemented:
|
|
14
|
+
* - Suite 0x01 (aes-256-gcm-v1) encrypt
|
|
15
|
+
* - Suite 0x01 (aes-256-gcm-v1) decrypt
|
|
16
|
+
*
|
|
17
|
+
* The XChaCha and PQ-hybrid suites use the same chunk format and AAD;
|
|
18
|
+
* adding them is a copy of the AES module with the suite primitive
|
|
19
|
+
* swapped, but we ship one suite first to lock the streaming contract.
|
|
20
|
+
*/
|
|
21
|
+
import { buildAuthenticatedHeader, HeaderError, parseHeader, } from '../format/header.js';
|
|
22
|
+
import { AES_GCM_V1_SUITE_PAYLOAD_LENGTH, AES_GCM_V1_TAG_BYTES, createChunkContext, decryptChunk, encryptChunk, generateContentMaterial, } from '../suites/aes-gcm-v1/index.js';
|
|
23
|
+
import { DEFAULT_CHUNK_SIZE, HEADER_SIZES, MAX_TOTAL_CHUNKS, SUITE, } from '../internal/types.js';
|
|
24
|
+
import { readUint32BE, uint32BE } from '../internal/encoding.js';
|
|
25
|
+
const LENGTH_PREFIX_BYTES = 4;
|
|
26
|
+
/**
|
|
27
|
+
* Build a TransformStream that encrypts plaintext into v1 wire-format
|
|
28
|
+
* ciphertext on the fly. The stream emits the header first, then each
|
|
29
|
+
* chunk as it arrives.
|
|
30
|
+
*
|
|
31
|
+
* The caller MUST pass `plaintextSize` because the header records it.
|
|
32
|
+
* If the caller doesn't know the size up front, they must accumulate
|
|
33
|
+
* the plaintext into a buffer first — there is no way to write a
|
|
34
|
+
* self-describing header otherwise.
|
|
35
|
+
*/
|
|
36
|
+
export function createAesGcmV1EncryptStream(options) {
|
|
37
|
+
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
38
|
+
if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
|
|
39
|
+
throw new RangeError('encrypt stream: chunkSize must be positive integer');
|
|
40
|
+
}
|
|
41
|
+
if (!Number.isSafeInteger(options.plaintextSize) ||
|
|
42
|
+
options.plaintextSize < 0) {
|
|
43
|
+
throw new RangeError('encrypt stream: plaintextSize must be a non-negative integer');
|
|
44
|
+
}
|
|
45
|
+
const totalChunks = options.plaintextSize === 0
|
|
46
|
+
? 0
|
|
47
|
+
: Math.ceil(options.plaintextSize / chunkSize);
|
|
48
|
+
if (totalChunks > MAX_TOTAL_CHUNKS) {
|
|
49
|
+
throw new RangeError('encrypt stream: file would exceed total_chunks limit');
|
|
50
|
+
}
|
|
51
|
+
const generated = generateContentMaterial();
|
|
52
|
+
const contentKey = options.contentKey ?? generated.contentKey;
|
|
53
|
+
const fileId = options.fileId ?? generated.fileId;
|
|
54
|
+
if (contentKey.length !== 32) {
|
|
55
|
+
throw new RangeError('encrypt stream: contentKey must be 32 bytes');
|
|
56
|
+
}
|
|
57
|
+
if (fileId.length !== HEADER_SIZES.FILE_ID) {
|
|
58
|
+
throw new RangeError('encrypt stream: fileId must be 16 bytes');
|
|
59
|
+
}
|
|
60
|
+
const suitePayload = options.suitePayloadOverride ??
|
|
61
|
+
new Uint8Array(AES_GCM_V1_SUITE_PAYLOAD_LENGTH);
|
|
62
|
+
// State held across the lifetime of the stream:
|
|
63
|
+
// - pending plaintext buffer (collects until we have a full chunk)
|
|
64
|
+
// - chunk index counter
|
|
65
|
+
// - bytes consumed counter (for sanity check vs plaintextSize)
|
|
66
|
+
// - context (lazily initialized)
|
|
67
|
+
let pending = new Uint8Array(0);
|
|
68
|
+
let chunkIndex = 0;
|
|
69
|
+
let bytesConsumed = 0;
|
|
70
|
+
let contextPromise = null;
|
|
71
|
+
let headerEmitted = false;
|
|
72
|
+
async function ensureHeader(controller) {
|
|
73
|
+
if (headerEmitted)
|
|
74
|
+
return;
|
|
75
|
+
const header = await buildAuthenticatedHeader({
|
|
76
|
+
suite: SUITE.AES_256_GCM_V1,
|
|
77
|
+
fileId,
|
|
78
|
+
chunkSize,
|
|
79
|
+
totalChunks,
|
|
80
|
+
plaintextSize: options.plaintextSize,
|
|
81
|
+
suitePayload,
|
|
82
|
+
}, contentKey);
|
|
83
|
+
controller.enqueue(header);
|
|
84
|
+
headerEmitted = true;
|
|
85
|
+
}
|
|
86
|
+
async function emitChunk(plaintext, controller) {
|
|
87
|
+
if (!contextPromise) {
|
|
88
|
+
contextPromise = createChunkContext(contentKey, fileId, totalChunks);
|
|
89
|
+
}
|
|
90
|
+
const ctx = await contextPromise;
|
|
91
|
+
const ct = await encryptChunk(ctx, chunkIndex, plaintext);
|
|
92
|
+
controller.enqueue(uint32BE(ct.length));
|
|
93
|
+
controller.enqueue(ct);
|
|
94
|
+
chunkIndex += 1;
|
|
95
|
+
}
|
|
96
|
+
const transformer = {
|
|
97
|
+
async transform(chunk, controller) {
|
|
98
|
+
await ensureHeader(controller);
|
|
99
|
+
bytesConsumed += chunk.length;
|
|
100
|
+
if (bytesConsumed > options.plaintextSize) {
|
|
101
|
+
controller.error(new RangeError('encrypt stream: input exceeded declared plaintextSize'));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
// Append to pending buffer
|
|
105
|
+
const merged = new Uint8Array(pending.length + chunk.length);
|
|
106
|
+
merged.set(pending, 0);
|
|
107
|
+
merged.set(chunk, pending.length);
|
|
108
|
+
pending = merged;
|
|
109
|
+
// Drain whole chunks
|
|
110
|
+
while (pending.length >= chunkSize && chunkIndex < totalChunks - 1) {
|
|
111
|
+
const slice = pending.subarray(0, chunkSize);
|
|
112
|
+
await emitChunk(slice, controller);
|
|
113
|
+
pending = pending.subarray(chunkSize);
|
|
114
|
+
// Subarray shares the buffer; copy out so we can drop the rest.
|
|
115
|
+
pending = pending.slice(0);
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
async flush(controller) {
|
|
119
|
+
await ensureHeader(controller);
|
|
120
|
+
if (bytesConsumed !== options.plaintextSize) {
|
|
121
|
+
controller.error(new RangeError(`encrypt stream: input was ${bytesConsumed} bytes, declared ${options.plaintextSize}`));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
// Emit any remaining whole chunks plus the final partial chunk.
|
|
125
|
+
while (chunkIndex < totalChunks) {
|
|
126
|
+
const isLastIteration = chunkIndex === totalChunks - 1;
|
|
127
|
+
const sliceSize = isLastIteration
|
|
128
|
+
? pending.length // final chunk: whatever's left
|
|
129
|
+
: chunkSize;
|
|
130
|
+
const slice = pending.subarray(0, sliceSize);
|
|
131
|
+
await emitChunk(slice, controller);
|
|
132
|
+
pending = pending.subarray(sliceSize).slice(0);
|
|
133
|
+
}
|
|
134
|
+
if (pending.length !== 0) {
|
|
135
|
+
controller.error(new RangeError('encrypt stream: residual bytes after final chunk'));
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
return {
|
|
140
|
+
stream: new TransformStream(transformer),
|
|
141
|
+
contentKey,
|
|
142
|
+
fileId,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Build a TransformStream that decrypts a v1 ciphertext stream into
|
|
147
|
+
* plaintext. Unlike the whole-blob decryptor, this version processes
|
|
148
|
+
* input incrementally — useful for very large files or when ciphertext
|
|
149
|
+
* arrives chunked over the network.
|
|
150
|
+
*/
|
|
151
|
+
export function createAesGcmV1DecryptStream(options) {
|
|
152
|
+
const { contentKey } = options;
|
|
153
|
+
if (contentKey.length !== 32) {
|
|
154
|
+
throw new RangeError('decrypt stream: contentKey must be 32 bytes');
|
|
155
|
+
}
|
|
156
|
+
// Phases:
|
|
157
|
+
// 0 = waiting for header bytes
|
|
158
|
+
// 1 = streaming chunks: alternating length-prefix + ciphertext
|
|
159
|
+
let phase = 0;
|
|
160
|
+
let buffer = new Uint8Array(0);
|
|
161
|
+
let parsedHeader = null;
|
|
162
|
+
let chunkIndex = 0;
|
|
163
|
+
let bytesEmitted = 0;
|
|
164
|
+
let nextCipherLen = -1;
|
|
165
|
+
let contextPromise = null;
|
|
166
|
+
function append(input) {
|
|
167
|
+
const merged = new Uint8Array(buffer.length + input.length);
|
|
168
|
+
merged.set(buffer, 0);
|
|
169
|
+
merged.set(input, buffer.length);
|
|
170
|
+
buffer = merged;
|
|
171
|
+
}
|
|
172
|
+
function consume(n) {
|
|
173
|
+
const out = buffer.subarray(0, n).slice(0);
|
|
174
|
+
buffer = buffer.subarray(n).slice(0);
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
async function tryParseHeader() {
|
|
178
|
+
// Need at least the fixed prefix + MAC (no suite_payload).
|
|
179
|
+
if (buffer.length <
|
|
180
|
+
HEADER_SIZES.MAGIC +
|
|
181
|
+
HEADER_SIZES.SUITE +
|
|
182
|
+
HEADER_SIZES.FLAGS +
|
|
183
|
+
HEADER_SIZES.FILE_ID +
|
|
184
|
+
HEADER_SIZES.CHUNK_SIZE +
|
|
185
|
+
HEADER_SIZES.TOTAL_CHUNKS +
|
|
186
|
+
HEADER_SIZES.PLAINTEXT_SIZE +
|
|
187
|
+
HEADER_SIZES.SUITE_PAYLOAD_LEN) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
// Read suite_payload_len so we know how much more to wait for.
|
|
191
|
+
const suitePayloadLenOffset = HEADER_SIZES.MAGIC +
|
|
192
|
+
HEADER_SIZES.SUITE +
|
|
193
|
+
HEADER_SIZES.FLAGS +
|
|
194
|
+
HEADER_SIZES.FILE_ID +
|
|
195
|
+
HEADER_SIZES.CHUNK_SIZE +
|
|
196
|
+
HEADER_SIZES.TOTAL_CHUNKS +
|
|
197
|
+
HEADER_SIZES.PLAINTEXT_SIZE;
|
|
198
|
+
const suitePayloadLen = (buffer[suitePayloadLenOffset] << 8) |
|
|
199
|
+
buffer[suitePayloadLenOffset + 1];
|
|
200
|
+
const fullHeaderLen = suitePayloadLenOffset +
|
|
201
|
+
HEADER_SIZES.SUITE_PAYLOAD_LEN +
|
|
202
|
+
suitePayloadLen +
|
|
203
|
+
HEADER_SIZES.HEADER_MAC;
|
|
204
|
+
if (buffer.length < fullHeaderLen)
|
|
205
|
+
return false;
|
|
206
|
+
const headerBytes = consume(fullHeaderLen);
|
|
207
|
+
parsedHeader = parseHeader(headerBytes);
|
|
208
|
+
if (parsedHeader.suite !== SUITE.AES_256_GCM_V1) {
|
|
209
|
+
throw new HeaderError(`decrypt stream: this stream only supports aes-256-gcm-v1; got suite 0x${parsedHeader.suite.toString(16)}`);
|
|
210
|
+
}
|
|
211
|
+
// Verify header MAC using parseHeader's exposed unauthenticated bytes
|
|
212
|
+
// and mac fields.
|
|
213
|
+
const { verifyHeaderMac } = await import('../format/header.js');
|
|
214
|
+
await verifyHeaderMac(parsedHeader, contentKey);
|
|
215
|
+
contextPromise = createChunkContext(contentKey, parsedHeader.fileId, parsedHeader.totalChunks);
|
|
216
|
+
phase = 1;
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
async function tryEmitChunk(controller) {
|
|
220
|
+
if (!parsedHeader)
|
|
221
|
+
return false;
|
|
222
|
+
if (chunkIndex >= parsedHeader.totalChunks) {
|
|
223
|
+
// Already consumed all expected chunks; anything remaining is trailing
|
|
224
|
+
// garbage and must be flagged at flush time.
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
if (nextCipherLen < 0) {
|
|
228
|
+
if (buffer.length < LENGTH_PREFIX_BYTES)
|
|
229
|
+
return false;
|
|
230
|
+
const lenBytes = consume(LENGTH_PREFIX_BYTES);
|
|
231
|
+
nextCipherLen = readUint32BE(lenBytes);
|
|
232
|
+
const isFinal = chunkIndex === parsedHeader.totalChunks - 1;
|
|
233
|
+
const minCipher = 1 + AES_GCM_V1_TAG_BYTES;
|
|
234
|
+
const maxCipher = parsedHeader.chunkSize + AES_GCM_V1_TAG_BYTES;
|
|
235
|
+
if (nextCipherLen < minCipher || nextCipherLen > maxCipher) {
|
|
236
|
+
throw new HeaderError('chunk_length_out_of_range');
|
|
237
|
+
}
|
|
238
|
+
if (!isFinal && nextCipherLen !== maxCipher) {
|
|
239
|
+
throw new HeaderError('non_final_chunk_wrong_size');
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (buffer.length < nextCipherLen)
|
|
243
|
+
return false;
|
|
244
|
+
const ctBytes = consume(nextCipherLen);
|
|
245
|
+
nextCipherLen = -1;
|
|
246
|
+
const ctx = await contextPromise;
|
|
247
|
+
const pt = await decryptChunk(ctx, chunkIndex, ctBytes);
|
|
248
|
+
bytesEmitted += pt.length;
|
|
249
|
+
controller.enqueue(pt);
|
|
250
|
+
chunkIndex += 1;
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
return new TransformStream({
|
|
254
|
+
async transform(chunk, controller) {
|
|
255
|
+
append(chunk);
|
|
256
|
+
try {
|
|
257
|
+
if (phase === 0) {
|
|
258
|
+
const ok = await tryParseHeader();
|
|
259
|
+
if (!ok)
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
// Drain as many chunks as we have data for.
|
|
263
|
+
while (await tryEmitChunk(controller)) {
|
|
264
|
+
/* loop */
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
catch (err) {
|
|
268
|
+
controller.error(err);
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
async flush(controller) {
|
|
272
|
+
try {
|
|
273
|
+
if (phase === 0) {
|
|
274
|
+
throw new HeaderError('header_too_short');
|
|
275
|
+
}
|
|
276
|
+
// No header yet means truncated input.
|
|
277
|
+
if (!parsedHeader) {
|
|
278
|
+
throw new HeaderError('header_too_short');
|
|
279
|
+
}
|
|
280
|
+
// Try once more in case flush is called with everything queued.
|
|
281
|
+
while (await tryEmitChunk(controller)) {
|
|
282
|
+
/* loop */
|
|
283
|
+
}
|
|
284
|
+
if (chunkIndex !== parsedHeader.totalChunks) {
|
|
285
|
+
throw new HeaderError('chunk_truncated');
|
|
286
|
+
}
|
|
287
|
+
if (bytesEmitted !== parsedHeader.plaintextSize) {
|
|
288
|
+
throw new HeaderError('plaintext_size_mismatch');
|
|
289
|
+
}
|
|
290
|
+
if (buffer.length !== 0) {
|
|
291
|
+
throw new HeaderError('trailing_bytes_after_last_chunk');
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
catch (err) {
|
|
295
|
+
controller.error(err);
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
301
|
+
// Helpers for ergonomics
|
|
302
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
303
|
+
/**
|
|
304
|
+
* Pipe a ReadableStream<Uint8Array> through the AES-GCM-v1 encrypt
|
|
305
|
+
* transform and return the resulting ReadableStream + key material.
|
|
306
|
+
*/
|
|
307
|
+
export function encryptStreamAesGcmV1(source, options) {
|
|
308
|
+
const { stream, contentKey, fileId } = createAesGcmV1EncryptStream(options);
|
|
309
|
+
return {
|
|
310
|
+
ciphertext: source.pipeThrough(stream),
|
|
311
|
+
contentKey,
|
|
312
|
+
fileId,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Pipe a ReadableStream<Uint8Array> through the AES-GCM-v1 decrypt
|
|
317
|
+
* transform and return the resulting plaintext ReadableStream.
|
|
318
|
+
*/
|
|
319
|
+
export function decryptStreamAesGcmV1(source, options) {
|
|
320
|
+
return source.pipeThrough(createAesGcmV1DecryptStream(options));
|
|
321
|
+
}
|
|
322
|
+
//# sourceMappingURL=aes-gcm-v1.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm-v1.js","sourceRoot":"","sources":["../../src/streams/aes-gcm-v1.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EACL,wBAAwB,EACxB,WAAW,EACX,WAAW,GACZ,MAAM,qBAAqB,CAAA;AAC5B,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EACpB,kBAAkB,EAClB,YAAY,EACZ,YAAY,EACZ,uBAAuB,GACxB,MAAM,+BAA+B,CAAA;AACtC,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,KAAK,GACN,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AAEhE,MAAM,mBAAmB,GAAG,CAAC,CAAA;AA4B7B;;;;;;;;;GASG;AACH,MAAM,UAAU,2BAA2B,CACzC,OAAqC;IAErC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,kBAAkB,CAAA;IACzD,IAAI,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AC,OAAO,CAAC,CAAC,CAAA;AACjE,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cipher suite "0x00" / v0: legacy AES-256-GCM, READ-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* This module decrypts files written by the pre-v1 ShieldFive client.
|
|
5
|
+
* It is intentionally read-only: there is no `encryptV0` exported symbol,
|
|
6
|
+
* and applications MUST NOT write new files in this format.
|
|
7
|
+
*
|
|
8
|
+
* v0 wire format (described in `spec/format-v0.md`):
|
|
9
|
+
*
|
|
10
|
+
* v0_file := chunk_0 || chunk_1 || ... || chunk_n
|
|
11
|
+
* chunk_i := AES-GCM(key=content_key, iv=prefix(4)||counter_be(8), aad=∅, pt=plain_i)
|
|
12
|
+
*
|
|
13
|
+
* No on-disk header. The reader must be told the chunk size and nonce
|
|
14
|
+
* prefix out of band (from the application database).
|
|
15
|
+
*/
|
|
16
|
+
export declare const V0_TAG_BYTES = 16;
|
|
17
|
+
export declare const V0_IV_BYTES = 12;
|
|
18
|
+
export declare const V0_KEY_BYTES = 32;
|
|
19
|
+
export declare const V0_NONCE_PREFIX_BYTES = 4;
|
|
20
|
+
export interface V0DecryptOptions {
|
|
21
|
+
/** Encrypted file bytes (concatenated chunks; no header) */
|
|
22
|
+
blob: Blob;
|
|
23
|
+
/** 32-byte AES-256 content key, already unwrapped from the envelope */
|
|
24
|
+
contentKey: Uint8Array;
|
|
25
|
+
/** 4-byte per-file random prefix, stored in the database */
|
|
26
|
+
noncePrefix: Uint8Array;
|
|
27
|
+
/** Plaintext bytes per chunk (5 MiB or 8 MiB in production) */
|
|
28
|
+
chunkSize: number;
|
|
29
|
+
/** Optional progress callback (0..1) */
|
|
30
|
+
onProgress?: (progress: number) => void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Decrypt a v0 file. Throws if any chunk fails to authenticate, if the
|
|
34
|
+
* blob size is inconsistent with the chunk size (no clean chunk boundary),
|
|
35
|
+
* or if the inputs have wrong sizes.
|
|
36
|
+
*/
|
|
37
|
+
export declare function decryptV0(options: V0DecryptOptions): Promise<Blob>;
|
|
38
|
+
/**
|
|
39
|
+
* Convenience wrapper that returns plaintext bytes directly.
|
|
40
|
+
*/
|
|
41
|
+
export declare function decryptV0ToBytes(options: V0DecryptOptions): Promise<Uint8Array>;
|
|
42
|
+
/**
|
|
43
|
+
* Detect whether a blob is v0 (legacy) by checking the absence of the v1
|
|
44
|
+
* magic prefix. Use only when out-of-band metadata is unavailable. For
|
|
45
|
+
* normal application use, the database flag `cipher_version` is canonical.
|
|
46
|
+
*/
|
|
47
|
+
export declare function looksLikeV0(blob: Blob): Promise<boolean>;
|
|
48
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v0/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,eAAO,MAAM,YAAY,KAAK,CAAA;AAC9B,eAAO,MAAM,WAAW,KAAK,CAAA;AAC7B,eAAO,MAAM,YAAY,KAAK,CAAA;AAC9B,eAAO,MAAM,qBAAqB,IAAI,CAAA;AAEtC,MAAM,WAAW,gBAAgB;IAC/B,4DAA4D;IAC5D,IAAI,EAAE,IAAI,CAAA;IACV,uEAAuE;IACvE,UAAU,EAAE,UAAU,CAAA;IACtB,4DAA4D;IAC5D,WAAW,EAAE,UAAU,CAAA;IACvB,+DAA+D;IAC/D,SAAS,EAAE,MAAM,CAAA;IACjB,wCAAwC;IACxC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;CACxC;AASD;;;;GAIG;AACH,wBAAsB,SAAS,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CA6ExE;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,UAAU,CAAC,CAGrB;AAED;;;;GAIG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,CAW9D"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cipher suite "0x00" / v0: legacy AES-256-GCM, READ-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* This module decrypts files written by the pre-v1 ShieldFive client.
|
|
5
|
+
* It is intentionally read-only: there is no `encryptV0` exported symbol,
|
|
6
|
+
* and applications MUST NOT write new files in this format.
|
|
7
|
+
*
|
|
8
|
+
* v0 wire format (described in `spec/format-v0.md`):
|
|
9
|
+
*
|
|
10
|
+
* v0_file := chunk_0 || chunk_1 || ... || chunk_n
|
|
11
|
+
* chunk_i := AES-GCM(key=content_key, iv=prefix(4)||counter_be(8), aad=∅, pt=plain_i)
|
|
12
|
+
*
|
|
13
|
+
* No on-disk header. The reader must be told the chunk size and nonce
|
|
14
|
+
* prefix out of band (from the application database).
|
|
15
|
+
*/
|
|
16
|
+
import { uint64BE } from '../../internal/encoding.js';
|
|
17
|
+
import { getSubtle } from '../../internal/runtime.js';
|
|
18
|
+
export const V0_TAG_BYTES = 16;
|
|
19
|
+
export const V0_IV_BYTES = 12;
|
|
20
|
+
export const V0_KEY_BYTES = 32;
|
|
21
|
+
export const V0_NONCE_PREFIX_BYTES = 4;
|
|
22
|
+
function buildIv(prefix, counter) {
|
|
23
|
+
const iv = new Uint8Array(V0_IV_BYTES);
|
|
24
|
+
iv.set(prefix, 0);
|
|
25
|
+
iv.set(uint64BE(counter), 4);
|
|
26
|
+
return iv;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Decrypt a v0 file. Throws if any chunk fails to authenticate, if the
|
|
30
|
+
* blob size is inconsistent with the chunk size (no clean chunk boundary),
|
|
31
|
+
* or if the inputs have wrong sizes.
|
|
32
|
+
*/
|
|
33
|
+
export async function decryptV0(options) {
|
|
34
|
+
const { blob, contentKey, noncePrefix, chunkSize, onProgress } = options;
|
|
35
|
+
if (!(blob instanceof Blob)) {
|
|
36
|
+
throw new TypeError('decryptV0: blob must be a Blob or File');
|
|
37
|
+
}
|
|
38
|
+
if (contentKey.length !== V0_KEY_BYTES) {
|
|
39
|
+
throw new RangeError('decryptV0: contentKey must be 32 bytes');
|
|
40
|
+
}
|
|
41
|
+
if (noncePrefix.length !== V0_NONCE_PREFIX_BYTES) {
|
|
42
|
+
throw new RangeError('decryptV0: noncePrefix must be 4 bytes');
|
|
43
|
+
}
|
|
44
|
+
if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
|
|
45
|
+
throw new RangeError('decryptV0: chunkSize must be a positive integer');
|
|
46
|
+
}
|
|
47
|
+
// Compute chunk layout from blob size.
|
|
48
|
+
const fullCipherLen = chunkSize + V0_TAG_BYTES;
|
|
49
|
+
const totalSize = blob.size;
|
|
50
|
+
if (totalSize === 0) {
|
|
51
|
+
return new Blob([], { type: 'application/octet-stream' });
|
|
52
|
+
}
|
|
53
|
+
const fullChunkCount = Math.floor(totalSize / fullCipherLen);
|
|
54
|
+
const trailingBytes = totalSize - fullChunkCount * fullCipherLen;
|
|
55
|
+
let totalChunks;
|
|
56
|
+
if (trailingBytes === 0) {
|
|
57
|
+
totalChunks = fullChunkCount;
|
|
58
|
+
}
|
|
59
|
+
else if (trailingBytes <= V0_TAG_BYTES) {
|
|
60
|
+
throw new Error(`decryptV0: trailing bytes (${trailingBytes}) too small for an AEAD tag — file truncated`);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
totalChunks = fullChunkCount + 1;
|
|
64
|
+
}
|
|
65
|
+
// Import the AES-GCM key once.
|
|
66
|
+
const key = await getSubtle().importKey('raw', contentKey, { name: 'AES-GCM' }, false, ['decrypt']);
|
|
67
|
+
const plaintextParts = [];
|
|
68
|
+
let cursor = 0;
|
|
69
|
+
for (let i = 0; i < totalChunks; i += 1) {
|
|
70
|
+
const isFinal = i === totalChunks - 1;
|
|
71
|
+
const cipherLen = isFinal && trailingBytes > 0 ? trailingBytes : fullCipherLen;
|
|
72
|
+
const cipherBytes = new Uint8Array(await blob.slice(cursor, cursor + cipherLen).arrayBuffer());
|
|
73
|
+
cursor += cipherLen;
|
|
74
|
+
const iv = buildIv(noncePrefix, i);
|
|
75
|
+
let pt;
|
|
76
|
+
try {
|
|
77
|
+
pt = await getSubtle().decrypt({ name: 'AES-GCM', iv: iv, tagLength: 128 }, key, cipherBytes);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
throw new Error(`decryptV0: chunk ${i} failed authentication`, { cause: err });
|
|
81
|
+
}
|
|
82
|
+
plaintextParts.push(pt);
|
|
83
|
+
onProgress?.((i + 1) / totalChunks);
|
|
84
|
+
}
|
|
85
|
+
return new Blob(plaintextParts, { type: 'application/octet-stream' });
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Convenience wrapper that returns plaintext bytes directly.
|
|
89
|
+
*/
|
|
90
|
+
export async function decryptV0ToBytes(options) {
|
|
91
|
+
const blob = await decryptV0(options);
|
|
92
|
+
return new Uint8Array(await blob.arrayBuffer());
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Detect whether a blob is v0 (legacy) by checking the absence of the v1
|
|
96
|
+
* magic prefix. Use only when out-of-band metadata is unavailable. For
|
|
97
|
+
* normal application use, the database flag `cipher_version` is canonical.
|
|
98
|
+
*/
|
|
99
|
+
export async function looksLikeV0(blob) {
|
|
100
|
+
if (blob.size < 5)
|
|
101
|
+
return true; // too small to be v1
|
|
102
|
+
const head = new Uint8Array(await blob.slice(0, 5).arrayBuffer());
|
|
103
|
+
// v1 magic is "SF5\x01\x00" = 0x53 0x46 0x35 0x01 0x00
|
|
104
|
+
return !(head[0] === 0x53 &&
|
|
105
|
+
head[1] === 0x46 &&
|
|
106
|
+
head[2] === 0x35 &&
|
|
107
|
+
head[3] === 0x01 &&
|
|
108
|
+
head[4] === 0x00);
|
|
109
|
+
}
|
|
110
|
+
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W,CAAC,IAAU;IAC1C,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC;QAAE,OAAO,IAAI,CAAA,CAAC,qBAAqB;IACpD,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAA;IACjE,uDAAuD;IACvD,OAAO,CAAC,CACN,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI;QAChB,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI;QAChB,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI;QAChB,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI;QAChB,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CACjB,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level encrypt/decrypt API for the AES-256-GCM v1 suite.
|
|
3
|
+
*
|
|
4
|
+
* This is what most callers will import. It glues together:
|
|
5
|
+
* - header construction
|
|
6
|
+
* - chunk encryption/decryption with full AAD binding
|
|
7
|
+
* - the v1 wire format
|
|
8
|
+
*
|
|
9
|
+
* Suites 0x02 (XChaCha20-Poly1305) and 0x03 (PQ hybrid) provide identical
|
|
10
|
+
* APIs in their own modules.
|
|
11
|
+
*/
|
|
12
|
+
import { type EncryptedFile, type ProgressCallback } from '../../internal/types.js';
|
|
13
|
+
export interface EncryptOptions {
|
|
14
|
+
blob: Blob;
|
|
15
|
+
/** Optional pre-existing content key (32 bytes). If omitted, generated. */
|
|
16
|
+
contentKey?: Uint8Array;
|
|
17
|
+
/** Optional pre-existing file_id (16 bytes). If omitted, generated. */
|
|
18
|
+
fileId?: Uint8Array;
|
|
19
|
+
/** Optional embedded wrapped key + IV (60 + 12 bytes). Defaults to zeros. */
|
|
20
|
+
suitePayloadOverride?: Uint8Array;
|
|
21
|
+
/** Plaintext bytes per chunk. Defaults to 4 MiB. */
|
|
22
|
+
chunkSize?: number;
|
|
23
|
+
/** Progress callback (0..1). Called once per chunk and once at completion. */
|
|
24
|
+
onProgress?: ProgressCallback;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Encrypt a Blob/File with the AES-256-GCM v1 suite.
|
|
28
|
+
* Returns the encrypted blob (header + chunks), plus the content key and
|
|
29
|
+
* file id so the caller can wrap/store them externally.
|
|
30
|
+
*/
|
|
31
|
+
export declare function encryptBlob(options: EncryptOptions): Promise<EncryptedFile & {
|
|
32
|
+
contentKey: Uint8Array;
|
|
33
|
+
}>;
|
|
34
|
+
export interface DecryptOptions {
|
|
35
|
+
blob: Blob;
|
|
36
|
+
contentKey: Uint8Array;
|
|
37
|
+
onProgress?: ProgressCallback;
|
|
38
|
+
/**
|
|
39
|
+
* If provided, the parser checks the suite matches before any AEAD
|
|
40
|
+
* operation. Callers who want to refuse, e.g., legacy suites can pass
|
|
41
|
+
* an explicit allowlist.
|
|
42
|
+
*/
|
|
43
|
+
allowedSuites?: ReadonlyArray<number>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Decrypt a v1 blob produced by `encryptBlob`. Reads the entire blob into
|
|
47
|
+
* memory in chunked passes; for streaming decryption see `streams/`.
|
|
48
|
+
*/
|
|
49
|
+
export declare function decryptBlob(options: DecryptOptions): Promise<Blob>;
|
|
50
|
+
export declare function encryptBytes(bytes: Uint8Array | ArrayBuffer, options: Omit<EncryptOptions, 'blob'>): Promise<EncryptedFile & {
|
|
51
|
+
contentKey: Uint8Array;
|
|
52
|
+
}>;
|
|
53
|
+
export declare function decryptToBytes(options: DecryptOptions): Promise<Uint8Array>;
|
|
54
|
+
//# sourceMappingURL=api.d.ts.map
|