@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":"api.d.ts","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v1/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAiBH,OAAO,EAEL,KAAK,aAAa,EAGlB,KAAK,gBAAgB,EAEtB,MAAM,yBAAyB,CAAA;AAUhC,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,IAAI,CAAA;IACV,2EAA2E;IAC3E,UAAU,CAAC,EAAE,UAAU,CAAA;IACvB,uEAAuE;IACvE,MAAM,CAAC,EAAE,UAAU,CAAA;IACnB,6EAA6E;IAC7E,oBAAoB,CAAC,EAAE,UAAU,CAAA;IACjC,oDAAoD;IACpD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,gBAAgB,CAAA;CAC9B;AAED;;;;GAIG;AACH,wBAAsB,WAAW,CAC/B,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,aAAa,GAAG;IAAE,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC,CA8ErD;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,IAAI,CAAA;IACV,UAAU,EAAE,UAAU,CAAA;IACtB,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B;;;;OAIG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CACtC;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAgHxE;AAED,wBAAsB,YAAY,CAChC,KAAK,EAAE,UAAU,GAAG,WAAW,EAC/B,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,MAAM,CAAC,GACpC,OAAO,CAAC,aAAa,GAAG;IAAE,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC,CAKrD;AAED,wBAAsB,cAAc,CAClC,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,UAAU,CAAC,CAGrB"}
|
|
@@ -0,0 +1,179 @@
|
|
|
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 { buildAuthenticatedHeader, parseHeader, verifyHeaderMac, HeaderError, } from '../../format/header.js';
|
|
13
|
+
import { AES_GCM_V1_SUITE_PAYLOAD_LENGTH, AES_GCM_V1_TAG_BYTES, createChunkContext, decryptChunk, encryptChunk, generateContentMaterial, parseAesGcmV1SuitePayload, } from './index.js';
|
|
14
|
+
import { DEFAULT_CHUNK_SIZE, HEADER_SIZES, MAX_TOTAL_CHUNKS, SUITE, } from '../../internal/types.js';
|
|
15
|
+
import { asBlobPart, concatBytes, readUint32BE, uint32BE, } from '../../internal/encoding.js';
|
|
16
|
+
const LENGTH_PREFIX_BYTES = 4;
|
|
17
|
+
/**
|
|
18
|
+
* Encrypt a Blob/File with the AES-256-GCM v1 suite.
|
|
19
|
+
* Returns the encrypted blob (header + chunks), plus the content key and
|
|
20
|
+
* file id so the caller can wrap/store them externally.
|
|
21
|
+
*/
|
|
22
|
+
export async function encryptBlob(options) {
|
|
23
|
+
const { blob, onProgress } = options;
|
|
24
|
+
if (!(blob instanceof Blob)) {
|
|
25
|
+
throw new TypeError('encryptBlob: blob must be a Blob or File');
|
|
26
|
+
}
|
|
27
|
+
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
28
|
+
if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
|
|
29
|
+
throw new RangeError('encryptBlob: chunkSize must be a positive integer');
|
|
30
|
+
}
|
|
31
|
+
const plaintextSize = blob.size;
|
|
32
|
+
const totalChunks = plaintextSize === 0 ? 0 : Math.ceil(plaintextSize / chunkSize);
|
|
33
|
+
if (totalChunks > MAX_TOTAL_CHUNKS) {
|
|
34
|
+
throw new RangeError('encryptBlob: file would exceed total_chunks limit');
|
|
35
|
+
}
|
|
36
|
+
// Resolve key material
|
|
37
|
+
const generated = generateContentMaterial();
|
|
38
|
+
const contentKey = options.contentKey ?? generated.contentKey;
|
|
39
|
+
const fileId = options.fileId ?? generated.fileId;
|
|
40
|
+
if (contentKey.length !== 32) {
|
|
41
|
+
throw new RangeError('encryptBlob: contentKey must be 32 bytes');
|
|
42
|
+
}
|
|
43
|
+
if (fileId.length !== HEADER_SIZES.FILE_ID) {
|
|
44
|
+
throw new RangeError('encryptBlob: fileId must be 16 bytes');
|
|
45
|
+
}
|
|
46
|
+
// Suite payload (caller-supplied or zero-filled placeholder)
|
|
47
|
+
const suitePayload = options.suitePayloadOverride ??
|
|
48
|
+
new Uint8Array(AES_GCM_V1_SUITE_PAYLOAD_LENGTH);
|
|
49
|
+
if (suitePayload.length !== AES_GCM_V1_SUITE_PAYLOAD_LENGTH) {
|
|
50
|
+
throw new RangeError(`encryptBlob: suitePayloadOverride must be ${AES_GCM_V1_SUITE_PAYLOAD_LENGTH} bytes`);
|
|
51
|
+
}
|
|
52
|
+
const header = await buildAuthenticatedHeader({
|
|
53
|
+
suite: SUITE.AES_256_GCM_V1,
|
|
54
|
+
fileId,
|
|
55
|
+
chunkSize,
|
|
56
|
+
totalChunks,
|
|
57
|
+
plaintextSize,
|
|
58
|
+
suitePayload,
|
|
59
|
+
}, contentKey);
|
|
60
|
+
const ctx = await createChunkContext(contentKey, fileId, totalChunks);
|
|
61
|
+
const parts = [asBlobPart(header)];
|
|
62
|
+
for (let i = 0; i < totalChunks; i += 1) {
|
|
63
|
+
const start = i * chunkSize;
|
|
64
|
+
const end = Math.min(start + chunkSize, plaintextSize);
|
|
65
|
+
const plaintextBytes = new Uint8Array(await blob.slice(start, end).arrayBuffer());
|
|
66
|
+
const ciphertext = await encryptChunk(ctx, i, plaintextBytes);
|
|
67
|
+
parts.push(asBlobPart(uint32BE(ciphertext.length)));
|
|
68
|
+
parts.push(asBlobPart(ciphertext));
|
|
69
|
+
onProgress?.((i + 1) / Math.max(totalChunks, 1));
|
|
70
|
+
}
|
|
71
|
+
if (totalChunks === 0)
|
|
72
|
+
onProgress?.(1);
|
|
73
|
+
return {
|
|
74
|
+
blob: new Blob(parts, { type: 'application/octet-stream' }),
|
|
75
|
+
suite: SUITE.AES_256_GCM_V1,
|
|
76
|
+
fileId,
|
|
77
|
+
chunkSize,
|
|
78
|
+
totalChunks,
|
|
79
|
+
plaintextSize,
|
|
80
|
+
contentKey,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Decrypt a v1 blob produced by `encryptBlob`. Reads the entire blob into
|
|
85
|
+
* memory in chunked passes; for streaming decryption see `streams/`.
|
|
86
|
+
*/
|
|
87
|
+
export async function decryptBlob(options) {
|
|
88
|
+
const { blob, contentKey, onProgress } = options;
|
|
89
|
+
if (!(blob instanceof Blob)) {
|
|
90
|
+
throw new TypeError('decryptBlob: blob must be a Blob or File');
|
|
91
|
+
}
|
|
92
|
+
if (contentKey.length !== 32) {
|
|
93
|
+
throw new RangeError('decryptBlob: contentKey must be 32 bytes');
|
|
94
|
+
}
|
|
95
|
+
// Read enough to parse the header. The header is fixed-length plus a
|
|
96
|
+
// bounded suite_payload (max 65535 bytes). We read up to 4 KiB and
|
|
97
|
+
// fall back to streaming if the suite_payload is unusually large.
|
|
98
|
+
const headerProbeBytes = new Uint8Array(await blob.slice(0, Math.min(blob.size, 4096)).arrayBuffer());
|
|
99
|
+
let parsed;
|
|
100
|
+
try {
|
|
101
|
+
parsed = parseHeader(headerProbeBytes);
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
if (err instanceof HeaderError && err.code === 'header_too_short') {
|
|
105
|
+
// The probe might have been too small. Read the whole blob.
|
|
106
|
+
const allHeaderBytes = new Uint8Array(await blob
|
|
107
|
+
.slice(0, Math.min(blob.size, 4 + 0xffff + 1024))
|
|
108
|
+
.arrayBuffer());
|
|
109
|
+
parsed = parseHeader(allHeaderBytes);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
throw err;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (options.allowedSuites &&
|
|
116
|
+
!options.allowedSuites.includes(parsed.suite)) {
|
|
117
|
+
throw new HeaderError('suite_not_allowed');
|
|
118
|
+
}
|
|
119
|
+
if (parsed.suite !== SUITE.AES_256_GCM_V1) {
|
|
120
|
+
throw new HeaderError(`decryptBlob: this entry point only supports aes-256-gcm-v1; got suite 0x${parsed.suite.toString(16)}`);
|
|
121
|
+
}
|
|
122
|
+
// Validate the suite payload structure (length etc).
|
|
123
|
+
parseAesGcmV1SuitePayload(parsed.suitePayload);
|
|
124
|
+
// Verify header MAC before processing chunks.
|
|
125
|
+
await verifyHeaderMac(parsed, contentKey);
|
|
126
|
+
const ctx = await createChunkContext(contentKey, parsed.fileId, parsed.totalChunks);
|
|
127
|
+
const plaintextParts = [];
|
|
128
|
+
let cursor = parsed.headerLength;
|
|
129
|
+
let bytesEmitted = 0;
|
|
130
|
+
for (let i = 0; i < parsed.totalChunks; i += 1) {
|
|
131
|
+
if (cursor + LENGTH_PREFIX_BYTES > blob.size) {
|
|
132
|
+
throw new HeaderError('chunk_length_truncated');
|
|
133
|
+
}
|
|
134
|
+
const lenBytes = new Uint8Array(await blob.slice(cursor, cursor + LENGTH_PREFIX_BYTES).arrayBuffer());
|
|
135
|
+
cursor += LENGTH_PREFIX_BYTES;
|
|
136
|
+
const cipherLen = readUint32BE(lenBytes);
|
|
137
|
+
// Sanity check: ciphertext for AES-GCM-v1 is exactly chunkSize+tag
|
|
138
|
+
// for non-final chunks. Final chunk is between 1+tag and chunkSize+tag.
|
|
139
|
+
const isFinal = i === parsed.totalChunks - 1;
|
|
140
|
+
const minCipher = 1 + AES_GCM_V1_TAG_BYTES;
|
|
141
|
+
const maxCipher = parsed.chunkSize + AES_GCM_V1_TAG_BYTES;
|
|
142
|
+
if (cipherLen < minCipher || cipherLen > maxCipher) {
|
|
143
|
+
throw new HeaderError('chunk_length_out_of_range');
|
|
144
|
+
}
|
|
145
|
+
if (!isFinal && cipherLen !== maxCipher) {
|
|
146
|
+
throw new HeaderError('non_final_chunk_wrong_size');
|
|
147
|
+
}
|
|
148
|
+
if (cursor + cipherLen > blob.size) {
|
|
149
|
+
throw new HeaderError('chunk_truncated');
|
|
150
|
+
}
|
|
151
|
+
const cipherBytes = new Uint8Array(await blob.slice(cursor, cursor + cipherLen).arrayBuffer());
|
|
152
|
+
cursor += cipherLen;
|
|
153
|
+
const plaintext = await decryptChunk(ctx, i, cipherBytes);
|
|
154
|
+
bytesEmitted += plaintext.length;
|
|
155
|
+
plaintextParts.push(asBlobPart(plaintext));
|
|
156
|
+
onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1));
|
|
157
|
+
}
|
|
158
|
+
// Final consistency: total bytes emitted must equal plaintextSize, and
|
|
159
|
+
// the file must have no trailing garbage past the last chunk.
|
|
160
|
+
if (bytesEmitted !== parsed.plaintextSize) {
|
|
161
|
+
throw new HeaderError('plaintext_size_mismatch');
|
|
162
|
+
}
|
|
163
|
+
if (cursor !== blob.size) {
|
|
164
|
+
throw new HeaderError('trailing_bytes_after_last_chunk');
|
|
165
|
+
}
|
|
166
|
+
if (parsed.totalChunks === 0)
|
|
167
|
+
onProgress?.(1);
|
|
168
|
+
return new Blob(plaintextParts, { type: 'application/octet-stream' });
|
|
169
|
+
}
|
|
170
|
+
export async function encryptBytes(bytes, options) {
|
|
171
|
+
const part = bytes instanceof ArrayBuffer ? bytes : asBlobPart(bytes);
|
|
172
|
+
const blob = new Blob([part]);
|
|
173
|
+
return encryptBlob({ blob, ...options });
|
|
174
|
+
}
|
|
175
|
+
export async function decryptToBytes(options) {
|
|
176
|
+
const blob = await decryptBlob(options);
|
|
177
|
+
return new Uint8Array(await blob.arrayBuffer());
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v1/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EACL,wBAAwB,EACxB,WAAW,EACX,eAAe,EACf,WAAW,GACZ,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EACpB,kBAAkB,EAClB,YAAY,EACZ,YAAY,EACZ,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,kBAAkB,EAElB,YAAY,EACZ,gBAAgB,EAEhB,KAAK,GACN,MAAM,yBAAyB,CAAA;AAChC,OAAO,EACL,UAAU,EACV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ES-256-GCM v1.
|
|
3
|
+
*
|
|
4
|
+
* Per-file content key. Per-chunk AEAD with file_id-bound AAD that
|
|
5
|
+
* authenticates chunk index, total chunks, and the is-final flag. The
|
|
6
|
+
* 12-byte GCM IV is split into a 4-byte file-derived prefix and an 8-byte
|
|
7
|
+
* big-endian counter.
|
|
8
|
+
*/
|
|
9
|
+
import { type ParsedHeader } from '../../internal/types.js';
|
|
10
|
+
export declare const AES_GCM_V1_TAG_BYTES = 16;
|
|
11
|
+
export declare const AES_GCM_V1_IV_BYTES = 12;
|
|
12
|
+
export declare const AES_GCM_V1_KEY_BYTES = 32;
|
|
13
|
+
/** Suite payload layout: 60-byte wrapped key + 12-byte wrap IV = 72 bytes. */
|
|
14
|
+
export declare const AES_GCM_V1_SUITE_PAYLOAD_LENGTH = 72;
|
|
15
|
+
export interface AesGcmV1SuitePayload {
|
|
16
|
+
wrappedKey: Uint8Array;
|
|
17
|
+
wrapIv: Uint8Array;
|
|
18
|
+
}
|
|
19
|
+
export declare function buildAesGcmV1SuitePayload(payload: AesGcmV1SuitePayload): Uint8Array;
|
|
20
|
+
export declare function parseAesGcmV1SuitePayload(bytes: Uint8Array): AesGcmV1SuitePayload;
|
|
21
|
+
/** Generate a fresh per-file content key and file_id. */
|
|
22
|
+
export declare function generateContentMaterial(): {
|
|
23
|
+
contentKey: Uint8Array;
|
|
24
|
+
fileId: Uint8Array;
|
|
25
|
+
};
|
|
26
|
+
export interface AesGcmV1ChunkContext {
|
|
27
|
+
cryptoKey: CryptoKey;
|
|
28
|
+
noncePrefix: Uint8Array;
|
|
29
|
+
totalChunks: number;
|
|
30
|
+
}
|
|
31
|
+
export declare function createChunkContext(contentKey: Uint8Array, fileId: Uint8Array, totalChunks: number): Promise<AesGcmV1ChunkContext>;
|
|
32
|
+
export declare function encryptChunk(ctx: AesGcmV1ChunkContext, chunkIndex: number, plaintext: Uint8Array): Promise<Uint8Array>;
|
|
33
|
+
export declare function decryptChunk(ctx: AesGcmV1ChunkContext, chunkIndex: number, ciphertext: Uint8Array): Promise<Uint8Array>;
|
|
34
|
+
/**
|
|
35
|
+
* Wrap (encrypt) a content key under an envelope key (folder/vault key).
|
|
36
|
+
* This is what the suite_payload's wrappedKey field carries when the
|
|
37
|
+
* caller chooses to embed the wrapped key in the file rather than store
|
|
38
|
+
* it in a database.
|
|
39
|
+
*/
|
|
40
|
+
export declare function wrapContentKey(options: {
|
|
41
|
+
contentKey: Uint8Array;
|
|
42
|
+
envelopeKey: Uint8Array;
|
|
43
|
+
envelopeAad?: Uint8Array;
|
|
44
|
+
}): Promise<{
|
|
45
|
+
wrapped: Uint8Array;
|
|
46
|
+
wrapIv: Uint8Array;
|
|
47
|
+
}>;
|
|
48
|
+
export declare function unwrapContentKey(options: {
|
|
49
|
+
envelopeKey: Uint8Array;
|
|
50
|
+
wrappedField: Uint8Array;
|
|
51
|
+
wrapIv: Uint8Array;
|
|
52
|
+
envelopeAad?: Uint8Array;
|
|
53
|
+
}): Promise<Uint8Array>;
|
|
54
|
+
/** Header parser hook — exposes ParsedHeader convenience for suite consumers. */
|
|
55
|
+
export declare function getSuitePayloadFromHeader(header: ParsedHeader): AesGcmV1SuitePayload;
|
|
56
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v1/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,yBAAyB,CAAA;AAKhC,eAAO,MAAM,oBAAoB,KAAK,CAAA;AACtC,eAAO,MAAM,mBAAmB,KAAK,CAAA;AACrC,eAAO,MAAM,oBAAoB,KAAK,CAAA;AAEtC,8EAA8E;AAC9E,eAAO,MAAM,+BAA+B,KAAK,CAAA;AAEjD,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,UAAU,CAAA;CACnB;AAED,wBAAg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AiF;AACjF,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,YAAY,GACnB,oBAAoB,CAEtB"}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cipher suite 0x01: AES-256-GCM v1.
|
|
3
|
+
*
|
|
4
|
+
* Per-file content key. Per-chunk AEAD with file_id-bound AAD that
|
|
5
|
+
* authenticates chunk index, total chunks, and the is-final flag. The
|
|
6
|
+
* 12-byte GCM IV is split into a 4-byte file-derived prefix and an 8-byte
|
|
7
|
+
* big-endian counter.
|
|
8
|
+
*/
|
|
9
|
+
import { buildChunkAad } from '../../format/header.js';
|
|
10
|
+
import { HKDF_INFO, HEADER_SIZES, } from '../../internal/types.js';
|
|
11
|
+
import { concatBytes, uint64BE } from '../../internal/encoding.js';
|
|
12
|
+
import { hkdfSha256 } from '../../internal/hkdf.js';
|
|
13
|
+
import { getSubtle, randomBytes } from '../../internal/runtime.js';
|
|
14
|
+
export const AES_GCM_V1_TAG_BYTES = 16;
|
|
15
|
+
export const AES_GCM_V1_IV_BYTES = 12;
|
|
16
|
+
export const AES_GCM_V1_KEY_BYTES = 32;
|
|
17
|
+
/** Suite payload layout: 60-byte wrapped key + 12-byte wrap IV = 72 bytes. */
|
|
18
|
+
export const AES_GCM_V1_SUITE_PAYLOAD_LENGTH = 72;
|
|
19
|
+
export function buildAesGcmV1SuitePayload(payload) {
|
|
20
|
+
if (payload.wrappedKey.length !== 60) {
|
|
21
|
+
throw new RangeError('aes-gcm-v1: wrappedKey must be 60 bytes');
|
|
22
|
+
}
|
|
23
|
+
if (payload.wrapIv.length !== AES_GCM_V1_IV_BYTES) {
|
|
24
|
+
throw new RangeError('aes-gcm-v1: wrapIv must be 12 bytes');
|
|
25
|
+
}
|
|
26
|
+
return concatBytes([payload.wrappedKey, payload.wrapIv]);
|
|
27
|
+
}
|
|
28
|
+
export function parseAesGcmV1SuitePayload(bytes) {
|
|
29
|
+
if (bytes.length !== AES_GCM_V1_SUITE_PAYLOAD_LENGTH) {
|
|
30
|
+
throw new RangeError('aes-gcm-v1: suite payload must be 72 bytes');
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
wrappedKey: bytes.slice(0, 60),
|
|
34
|
+
wrapIv: bytes.slice(60, 72),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Derive the per-file chunk key from the content key.
|
|
39
|
+
* Even though the content key is fresh per file, we run it through HKDF
|
|
40
|
+
* so that any future bug that recycles a content key cannot also recycle
|
|
41
|
+
* the chunk key under the same file_id.
|
|
42
|
+
*/
|
|
43
|
+
async function deriveChunkKey(contentKey, fileId) {
|
|
44
|
+
return hkdfSha256({
|
|
45
|
+
ikm: contentKey,
|
|
46
|
+
salt: fileId,
|
|
47
|
+
info: HKDF_INFO.AES_GCM_CHUNK_KEY,
|
|
48
|
+
length: AES_GCM_V1_KEY_BYTES,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Derive the 4-byte nonce prefix from file_id. Stable per file.
|
|
53
|
+
*/
|
|
54
|
+
async function deriveNoncePrefix(fileId) {
|
|
55
|
+
const out = await hkdfSha256({
|
|
56
|
+
ikm: fileId,
|
|
57
|
+
info: HKDF_INFO.AES_GCM_NONCE_PREFIX,
|
|
58
|
+
length: 4,
|
|
59
|
+
});
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
function buildIv(noncePrefix, chunkIndex) {
|
|
63
|
+
const iv = new Uint8Array(AES_GCM_V1_IV_BYTES);
|
|
64
|
+
iv.set(noncePrefix, 0);
|
|
65
|
+
iv.set(uint64BE(chunkIndex), 4);
|
|
66
|
+
return iv;
|
|
67
|
+
}
|
|
68
|
+
async function importAesGcmKey(keyBytes, usages) {
|
|
69
|
+
if (keyBytes.length !== AES_GCM_V1_KEY_BYTES) {
|
|
70
|
+
throw new RangeError('aes-gcm-v1: key must be 32 bytes');
|
|
71
|
+
}
|
|
72
|
+
return getSubtle().importKey('raw', keyBytes, { name: 'AES-GCM' }, false, usages);
|
|
73
|
+
}
|
|
74
|
+
/** Generate a fresh per-file content key and file_id. */
|
|
75
|
+
export function generateContentMaterial() {
|
|
76
|
+
return {
|
|
77
|
+
contentKey: randomBytes(AES_GCM_V1_KEY_BYTES),
|
|
78
|
+
fileId: randomBytes(HEADER_SIZES.FILE_ID),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export async function createChunkContext(contentKey, fileId, totalChunks) {
|
|
82
|
+
const chunkKeyBytes = await deriveChunkKey(contentKey, fileId);
|
|
83
|
+
const noncePrefix = await deriveNoncePrefix(fileId);
|
|
84
|
+
const cryptoKey = await importAesGcmKey(chunkKeyBytes, ['encrypt', 'decrypt']);
|
|
85
|
+
return { cryptoKey, noncePrefix, totalChunks };
|
|
86
|
+
}
|
|
87
|
+
export async function encryptChunk(ctx, chunkIndex, plaintext) {
|
|
88
|
+
const isFinal = chunkIndex === ctx.totalChunks - 1;
|
|
89
|
+
const aad = buildChunkAad(chunkIndex, ctx.totalChunks, isFinal);
|
|
90
|
+
const iv = buildIv(ctx.noncePrefix, chunkIndex);
|
|
91
|
+
const ct = await getSubtle().encrypt({
|
|
92
|
+
name: 'AES-GCM',
|
|
93
|
+
iv: iv,
|
|
94
|
+
additionalData: aad,
|
|
95
|
+
tagLength: 128,
|
|
96
|
+
}, ctx.cryptoKey, plaintext);
|
|
97
|
+
return new Uint8Array(ct);
|
|
98
|
+
}
|
|
99
|
+
export async function decryptChunk(ctx, chunkIndex, ciphertext) {
|
|
100
|
+
const isFinal = chunkIndex === ctx.totalChunks - 1;
|
|
101
|
+
const aad = buildChunkAad(chunkIndex, ctx.totalChunks, isFinal);
|
|
102
|
+
const iv = buildIv(ctx.noncePrefix, chunkIndex);
|
|
103
|
+
const pt = await getSubtle().decrypt({
|
|
104
|
+
name: 'AES-GCM',
|
|
105
|
+
iv: iv,
|
|
106
|
+
additionalData: aad,
|
|
107
|
+
tagLength: 128,
|
|
108
|
+
}, ctx.cryptoKey, ciphertext);
|
|
109
|
+
return new Uint8Array(pt);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Wrap (encrypt) a content key under an envelope key (folder/vault key).
|
|
113
|
+
* This is what the suite_payload's wrappedKey field carries when the
|
|
114
|
+
* caller chooses to embed the wrapped key in the file rather than store
|
|
115
|
+
* it in a database.
|
|
116
|
+
*/
|
|
117
|
+
export async function wrapContentKey(options) {
|
|
118
|
+
const { contentKey, envelopeKey } = options;
|
|
119
|
+
if (contentKey.length !== AES_GCM_V1_KEY_BYTES) {
|
|
120
|
+
throw new RangeError('wrapContentKey: contentKey must be 32 bytes');
|
|
121
|
+
}
|
|
122
|
+
if (envelopeKey.length !== AES_GCM_V1_KEY_BYTES) {
|
|
123
|
+
throw new RangeError('wrapContentKey: envelopeKey must be 32 bytes');
|
|
124
|
+
}
|
|
125
|
+
const wrapIv = randomBytes(AES_GCM_V1_IV_BYTES);
|
|
126
|
+
const wrapKey = await importAesGcmKey(envelopeKey, ['encrypt']);
|
|
127
|
+
const wrapped = await getSubtle().encrypt({
|
|
128
|
+
name: 'AES-GCM',
|
|
129
|
+
iv: wrapIv,
|
|
130
|
+
additionalData: (options.envelopeAad ?? new Uint8Array(0)),
|
|
131
|
+
tagLength: 128,
|
|
132
|
+
}, wrapKey, contentKey);
|
|
133
|
+
// wrapped = 32-byte key + 16-byte tag = 48 bytes; pad to 60 with zeros
|
|
134
|
+
// so the suite_payload size is fixed regardless of whether the file
|
|
135
|
+
// embeds an inline wrapped key or zero-fills the field.
|
|
136
|
+
const wrappedBytes = new Uint8Array(wrapped);
|
|
137
|
+
if (wrappedBytes.length !== 48) {
|
|
138
|
+
throw new Error('wrapContentKey: unexpected wrapped length');
|
|
139
|
+
}
|
|
140
|
+
const padded = new Uint8Array(60);
|
|
141
|
+
padded.set(wrappedBytes, 0);
|
|
142
|
+
return { wrapped: padded, wrapIv };
|
|
143
|
+
}
|
|
144
|
+
export async function unwrapContentKey(options) {
|
|
145
|
+
const { envelopeKey, wrappedField, wrapIv } = options;
|
|
146
|
+
if (envelopeKey.length !== AES_GCM_V1_KEY_BYTES) {
|
|
147
|
+
throw new RangeError('unwrapContentKey: envelopeKey must be 32 bytes');
|
|
148
|
+
}
|
|
149
|
+
if (wrappedField.length !== 60) {
|
|
150
|
+
throw new RangeError('unwrapContentKey: wrappedField must be 60 bytes');
|
|
151
|
+
}
|
|
152
|
+
if (wrapIv.length !== AES_GCM_V1_IV_BYTES) {
|
|
153
|
+
throw new RangeError('unwrapContentKey: wrapIv must be 12 bytes');
|
|
154
|
+
}
|
|
155
|
+
// The first 48 bytes are key+tag; the last 12 are reserved zeros.
|
|
156
|
+
const wrapped = wrappedField.slice(0, 48);
|
|
157
|
+
const wrapKey = await importAesGcmKey(envelopeKey, ['decrypt']);
|
|
158
|
+
const pt = await getSubtle().decrypt({
|
|
159
|
+
name: 'AES-GCM',
|
|
160
|
+
iv: wrapIv,
|
|
161
|
+
additionalData: (options.envelopeAad ?? new Uint8Array(0)),
|
|
162
|
+
tagLength: 128,
|
|
163
|
+
}, wrapKey, wrapped);
|
|
164
|
+
const ptBytes = new Uint8Array(pt);
|
|
165
|
+
if (ptBytes.length !== AES_GCM_V1_KEY_BYTES) {
|
|
166
|
+
throw new Error('unwrapContentKey: bad plaintext length');
|
|
167
|
+
}
|
|
168
|
+
return ptBytes;
|
|
169
|
+
}
|
|
170
|
+
/** Header parser hook — exposes ParsedHeader convenience for suite consumers. */
|
|
171
|
+
export function getSuitePayloadFromHeader(header) {
|
|
172
|
+
return parseAesGcmV1SuitePayload(header.suitePayload);
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v1/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,EACL,S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,CAAC;QAC5C,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAA;IAC3D,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,yBAAyB,CACvC,MAAoB;IAEpB,OAAO,yBAAyB,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;AACvD,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level encrypt/decrypt API for the PQ-hybrid v1 suite.
|
|
3
|
+
*
|
|
4
|
+
* Differs from aes-gcm-v1 / xchacha-v1 because content key derivation is
|
|
5
|
+
* not a single random 32-byte buffer — instead, the combined key is
|
|
6
|
+
* recovered from the on-disk suite payload (PQ ciphertext + wrapped
|
|
7
|
+
* classical share) and the recipient's PQ secret key + envelope key.
|
|
8
|
+
*
|
|
9
|
+
* The header MAC is keyed by the *combined* key, so a successful header
|
|
10
|
+
* MAC verification proves both the PQ KEM and the classical wrap were
|
|
11
|
+
* recovered correctly.
|
|
12
|
+
*/
|
|
13
|
+
import { type EncryptedFile, type ProgressCallback } from '../../internal/types.js';
|
|
14
|
+
export interface PqHybridEncryptOptions {
|
|
15
|
+
blob: Blob;
|
|
16
|
+
/** Recipient's ML-KEM-1024 public key (1568 bytes) */
|
|
17
|
+
recipientPublicKey: Uint8Array;
|
|
18
|
+
/** Classical envelope key under which the classical share is wrapped */
|
|
19
|
+
envelopeKey: Uint8Array;
|
|
20
|
+
fileId?: Uint8Array;
|
|
21
|
+
chunkSize?: number;
|
|
22
|
+
onProgress?: ProgressCallback;
|
|
23
|
+
}
|
|
24
|
+
export declare function encryptBlob(options: PqHybridEncryptOptions): Promise<EncryptedFile & {
|
|
25
|
+
combinedKey: Uint8Array;
|
|
26
|
+
}>;
|
|
27
|
+
export interface PqHybridDecryptOptions {
|
|
28
|
+
blob: Blob;
|
|
29
|
+
/** Recipient's ML-KEM-1024 secret key (3168 bytes) */
|
|
30
|
+
recipientSecretKey: Uint8Array;
|
|
31
|
+
/** Classical envelope key */
|
|
32
|
+
envelopeKey: Uint8Array;
|
|
33
|
+
onProgress?: ProgressCallback;
|
|
34
|
+
}
|
|
35
|
+
export declare function decryptBlob(options: PqHybridDecryptOptions): Promise<Blob>;
|
|
36
|
+
export declare function encryptBytes(bytes: Uint8Array, options: Omit<PqHybridEncryptOptions, 'blob'>): Promise<EncryptedFile & {
|
|
37
|
+
combinedKey: Uint8Array;
|
|
38
|
+
}>;
|
|
39
|
+
export declare function decryptToBytes(options: PqHybridDecryptOptions): Promise<Uint8Array>;
|
|
40
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/suites/pq-hybrid-v1/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAiBH,OAAO,EAEL,KAAK,aAAa,EAGlB,KAAK,gBAAgB,EAEtB,MAAM,yBAAyB,CAAA;AAUhC,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,IAAI,CAAA;IACV,sDAAsD;IACtD,kBAAkB,EAAE,UAAU,CAAA;IAC9B,wEAAwE;IACxE,WAAW,EAAE,UAAU,CAAA;IACvB,MAAM,CAAC,EAAE,UAAU,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,gBAAgB,CAAA;CAC9B;AAED,wBAAsB,WAAW,CAC/B,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,aAAa,GAAG;IAAE,WAAW,EAAE,UAAU,CAAA;CAAE,CAAC,CAsEtD;AAED,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,IAAI,CAAA;IACV,sDAAsD;IACtD,kBAAkB,EAAE,UAAU,CAAA;IAC9B,6BAA6B;IAC7B,WAAW,EAAE,UAAU,CAAA;IACvB,UAAU,CAAC,EAAE,gBAAgB,CAAA;CAC9B;AAED,wBAAsB,WAAW,CAC/B,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,IAAI,CAAC,CAsGf;AAED,wBAAsB,YAAY,CAChC,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,IAAI,CAAC,sBAAsB,EAAE,MAAM,CAAC,GAC5C,OAAO,CAAC,aAAa,GAAG;IAAE,WAAW,EAAE,UAAU,CAAA;CAAE,CAAC,CAGtD;AAED,wBAAsB,cAAc,CAClC,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,UAAU,CAAC,CAGrB"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level encrypt/decrypt API for the PQ-hybrid v1 suite.
|
|
3
|
+
*
|
|
4
|
+
* Differs from aes-gcm-v1 / xchacha-v1 because content key derivation is
|
|
5
|
+
* not a single random 32-byte buffer — instead, the combined key is
|
|
6
|
+
* recovered from the on-disk suite payload (PQ ciphertext + wrapped
|
|
7
|
+
* classical share) and the recipient's PQ secret key + envelope key.
|
|
8
|
+
*
|
|
9
|
+
* The header MAC is keyed by the *combined* key, so a successful header
|
|
10
|
+
* MAC verification proves both the PQ KEM and the classical wrap were
|
|
11
|
+
* recovered correctly.
|
|
12
|
+
*/
|
|
13
|
+
import { buildAuthenticatedHeader, parseHeader, verifyHeaderMac, HeaderError, } from '../../format/header.js';
|
|
14
|
+
import { PQ_HYBRID_V1_SUITE_PAYLOAD_LENGTH, PQ_HYBRID_V1_TAG_BYTES, createChunkContext, decapsulateFromHeader, decryptChunk, encapsulateForRecipient, encryptChunk, } from './index.js';
|
|
15
|
+
import { DEFAULT_CHUNK_SIZE, HEADER_SIZES, MAX_TOTAL_CHUNKS, SUITE, } from '../../internal/types.js';
|
|
16
|
+
import { asBlobPart, readUint32BE, uint32BE, } from '../../internal/encoding.js';
|
|
17
|
+
import { randomBytes } from '../../internal/runtime.js';
|
|
18
|
+
const LENGTH_PREFIX_BYTES = 4;
|
|
19
|
+
export async function encryptBlob(options) {
|
|
20
|
+
const { blob, recipientPublicKey, envelopeKey, onProgress } = options;
|
|
21
|
+
if (!(blob instanceof Blob)) {
|
|
22
|
+
throw new TypeError('encryptBlob: blob must be a Blob or File');
|
|
23
|
+
}
|
|
24
|
+
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
25
|
+
if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
|
|
26
|
+
throw new RangeError('encryptBlob: chunkSize must be a positive integer');
|
|
27
|
+
}
|
|
28
|
+
const plaintextSize = blob.size;
|
|
29
|
+
const totalChunks = plaintextSize === 0 ? 0 : Math.ceil(plaintextSize / chunkSize);
|
|
30
|
+
if (totalChunks > MAX_TOTAL_CHUNKS) {
|
|
31
|
+
throw new RangeError('encryptBlob: file would exceed total_chunks limit');
|
|
32
|
+
}
|
|
33
|
+
const fileId = options.fileId ?? randomBytes(HEADER_SIZES.FILE_ID);
|
|
34
|
+
if (fileId.length !== HEADER_SIZES.FILE_ID) {
|
|
35
|
+
throw new RangeError('encryptBlob: fileId must be 16 bytes');
|
|
36
|
+
}
|
|
37
|
+
// PQ + classical encapsulation produces the suite payload AND the
|
|
38
|
+
// combined key in one step. The combined key is what authenticates the
|
|
39
|
+
// header and encrypts every chunk.
|
|
40
|
+
const { suitePayload, combinedKey } = await encapsulateForRecipient({
|
|
41
|
+
recipientPublicKey,
|
|
42
|
+
envelopeKey,
|
|
43
|
+
fileId,
|
|
44
|
+
});
|
|
45
|
+
const header = await buildAuthenticatedHeader({
|
|
46
|
+
suite: SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1,
|
|
47
|
+
fileId,
|
|
48
|
+
chunkSize,
|
|
49
|
+
totalChunks,
|
|
50
|
+
plaintextSize,
|
|
51
|
+
suitePayload,
|
|
52
|
+
}, combinedKey);
|
|
53
|
+
const ctx = await createChunkContext(combinedKey, fileId, totalChunks);
|
|
54
|
+
const parts = [asBlobPart(header)];
|
|
55
|
+
for (let i = 0; i < totalChunks; i += 1) {
|
|
56
|
+
const start = i * chunkSize;
|
|
57
|
+
const end = Math.min(start + chunkSize, plaintextSize);
|
|
58
|
+
const plaintextBytes = new Uint8Array(await blob.slice(start, end).arrayBuffer());
|
|
59
|
+
const ciphertext = await encryptChunk(ctx, i, plaintextBytes);
|
|
60
|
+
parts.push(asBlobPart(uint32BE(ciphertext.length)));
|
|
61
|
+
parts.push(asBlobPart(ciphertext));
|
|
62
|
+
onProgress?.((i + 1) / Math.max(totalChunks, 1));
|
|
63
|
+
}
|
|
64
|
+
if (totalChunks === 0)
|
|
65
|
+
onProgress?.(1);
|
|
66
|
+
return {
|
|
67
|
+
blob: new Blob(parts, { type: 'application/octet-stream' }),
|
|
68
|
+
suite: SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1,
|
|
69
|
+
fileId,
|
|
70
|
+
chunkSize,
|
|
71
|
+
totalChunks,
|
|
72
|
+
plaintextSize,
|
|
73
|
+
combinedKey,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export async function decryptBlob(options) {
|
|
77
|
+
const { blob, recipientSecretKey, envelopeKey, onProgress } = options;
|
|
78
|
+
if (!(blob instanceof Blob)) {
|
|
79
|
+
throw new TypeError('decryptBlob: blob must be a Blob or File');
|
|
80
|
+
}
|
|
81
|
+
// The header for this suite is large because suite_payload is 1664 bytes.
|
|
82
|
+
// Probe enough bytes to capture it.
|
|
83
|
+
const headerProbeSize = Math.min(blob.size, HEADER_SIZES.MAGIC +
|
|
84
|
+
HEADER_SIZES.SUITE +
|
|
85
|
+
HEADER_SIZES.FLAGS +
|
|
86
|
+
HEADER_SIZES.FILE_ID +
|
|
87
|
+
HEADER_SIZES.CHUNK_SIZE +
|
|
88
|
+
HEADER_SIZES.TOTAL_CHUNKS +
|
|
89
|
+
HEADER_SIZES.PLAINTEXT_SIZE +
|
|
90
|
+
HEADER_SIZES.SUITE_PAYLOAD_LEN +
|
|
91
|
+
PQ_HYBRID_V1_SUITE_PAYLOAD_LENGTH +
|
|
92
|
+
HEADER_SIZES.HEADER_MAC +
|
|
93
|
+
32);
|
|
94
|
+
const headerProbeBytes = new Uint8Array(await blob.slice(0, headerProbeSize).arrayBuffer());
|
|
95
|
+
const parsed = parseHeader(headerProbeBytes);
|
|
96
|
+
if (parsed.suite !== SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1) {
|
|
97
|
+
throw new HeaderError(`decryptBlob: this entry point only supports pq-hybrid-xchacha-mlkem1024-v1; got suite 0x${parsed.suite.toString(16)}`);
|
|
98
|
+
}
|
|
99
|
+
// Recover the combined key — this is where PQ decapsulation happens.
|
|
100
|
+
const { combinedKey } = await decapsulateFromHeader({
|
|
101
|
+
suitePayload: parsed.suitePayload,
|
|
102
|
+
recipientSecretKey,
|
|
103
|
+
envelopeKey,
|
|
104
|
+
fileId: parsed.fileId,
|
|
105
|
+
});
|
|
106
|
+
// The header MAC implicitly verifies that the right combined key was
|
|
107
|
+
// recovered (i.e., the PQ secret + envelope key match the file).
|
|
108
|
+
await verifyHeaderMac(parsed, combinedKey);
|
|
109
|
+
const ctx = await createChunkContext(combinedKey, parsed.fileId, parsed.totalChunks);
|
|
110
|
+
const plaintextParts = [];
|
|
111
|
+
let cursor = parsed.headerLength;
|
|
112
|
+
let bytesEmitted = 0;
|
|
113
|
+
for (let i = 0; i < parsed.totalChunks; i += 1) {
|
|
114
|
+
if (cursor + LENGTH_PREFIX_BYTES > blob.size) {
|
|
115
|
+
throw new HeaderError('chunk_length_truncated');
|
|
116
|
+
}
|
|
117
|
+
const lenBytes = new Uint8Array(await blob.slice(cursor, cursor + LENGTH_PREFIX_BYTES).arrayBuffer());
|
|
118
|
+
cursor += LENGTH_PREFIX_BYTES;
|
|
119
|
+
const cipherLen = readUint32BE(lenBytes);
|
|
120
|
+
const isFinal = i === parsed.totalChunks - 1;
|
|
121
|
+
const minCipher = 1 + PQ_HYBRID_V1_TAG_BYTES;
|
|
122
|
+
const maxCipher = parsed.chunkSize + PQ_HYBRID_V1_TAG_BYTES;
|
|
123
|
+
if (cipherLen < minCipher || cipherLen > maxCipher) {
|
|
124
|
+
throw new HeaderError('chunk_length_out_of_range');
|
|
125
|
+
}
|
|
126
|
+
if (!isFinal && cipherLen !== maxCipher) {
|
|
127
|
+
throw new HeaderError('non_final_chunk_wrong_size');
|
|
128
|
+
}
|
|
129
|
+
if (cursor + cipherLen > blob.size) {
|
|
130
|
+
throw new HeaderError('chunk_truncated');
|
|
131
|
+
}
|
|
132
|
+
const cipherBytes = new Uint8Array(await blob.slice(cursor, cursor + cipherLen).arrayBuffer());
|
|
133
|
+
cursor += cipherLen;
|
|
134
|
+
const plaintext = await decryptChunk(ctx, i, cipherBytes);
|
|
135
|
+
bytesEmitted += plaintext.length;
|
|
136
|
+
plaintextParts.push(asBlobPart(plaintext));
|
|
137
|
+
onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1));
|
|
138
|
+
}
|
|
139
|
+
if (bytesEmitted !== parsed.plaintextSize) {
|
|
140
|
+
throw new HeaderError('plaintext_size_mismatch');
|
|
141
|
+
}
|
|
142
|
+
if (cursor !== blob.size) {
|
|
143
|
+
throw new HeaderError('trailing_bytes_after_last_chunk');
|
|
144
|
+
}
|
|
145
|
+
if (parsed.totalChunks === 0)
|
|
146
|
+
onProgress?.(1);
|
|
147
|
+
return new Blob(plaintextParts, { type: 'application/octet-stream' });
|
|
148
|
+
}
|
|
149
|
+
export async function encryptBytes(bytes, options) {
|
|
150
|
+
const blob = new Blob([asBlobPart(bytes)]);
|
|
151
|
+
return encryptBlob({ blob, ...options });
|
|
152
|
+
}
|
|
153
|
+
export async function decryptToBytes(options) {
|
|
154
|
+
const blob = await decryptBlob(options);
|
|
155
|
+
return new Uint8Array(await blob.arrayBuffer());
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=api.js.map
|