@substrate-system/crypto-stream 0.0.33

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.
Files changed (43) hide show
  1. package/LICENSE +99 -0
  2. package/README.md +366 -0
  3. package/dist/concat-streams.cjs +59 -0
  4. package/dist/ece.cjs +375 -0
  5. package/dist/extract-transformer.cjs +55 -0
  6. package/dist/index.cjs +28 -0
  7. package/dist/keychain.cjs +344 -0
  8. package/dist/slice-transformer.cjs +75 -0
  9. package/dist/src/concat-streams.d.ts +9 -0
  10. package/dist/src/concat-streams.d.ts.map +1 -0
  11. package/dist/src/concat-streams.js +46 -0
  12. package/dist/src/concat-streams.js.map +1 -0
  13. package/dist/src/ece.d.ts +66 -0
  14. package/dist/src/ece.d.ts.map +1 -0
  15. package/dist/src/ece.js +373 -0
  16. package/dist/src/ece.js.map +1 -0
  17. package/dist/src/extract-transformer.d.ts +18 -0
  18. package/dist/src/extract-transformer.d.ts.map +1 -0
  19. package/dist/src/extract-transformer.js +40 -0
  20. package/dist/src/extract-transformer.js.map +1 -0
  21. package/dist/src/index.d.ts +3 -0
  22. package/dist/src/index.d.ts.map +1 -0
  23. package/dist/src/index.js +3 -0
  24. package/dist/src/index.js.map +1 -0
  25. package/dist/src/keychain.d.ts +103 -0
  26. package/dist/src/keychain.d.ts.map +1 -0
  27. package/dist/src/keychain.js +267 -0
  28. package/dist/src/keychain.js.map +1 -0
  29. package/dist/src/slice-transformer.d.ts +19 -0
  30. package/dist/src/slice-transformer.d.ts.map +1 -0
  31. package/dist/src/slice-transformer.js +58 -0
  32. package/dist/src/slice-transformer.js.map +1 -0
  33. package/dist/src/transform-stream.d.ts +11 -0
  34. package/dist/src/transform-stream.d.ts.map +1 -0
  35. package/dist/src/transform-stream.js +136 -0
  36. package/dist/src/transform-stream.js.map +1 -0
  37. package/dist/src/util.d.ts +27 -0
  38. package/dist/src/util.d.ts.map +1 -0
  39. package/dist/src/util.js +49 -0
  40. package/dist/src/util.js.map +1 -0
  41. package/dist/transform-stream.cjs +159 -0
  42. package/dist/util.cjs +57 -0
  43. package/package.json +86 -0
@@ -0,0 +1,373 @@
1
+ /**
2
+ * Encryption and decryption streams for "Encrypted Content-Encoding for HTTP"
3
+ * specification. See: https://tools.ietf.org/html/rfc8188
4
+ */
5
+ import { concatStreams } from './concat-streams.js';
6
+ import { transformStream } from './transform-stream.js';
7
+ import { SliceTransformer } from './slice-transformer.js';
8
+ import { webcrypto } from '@substrate-system/one-webcrypto';
9
+ import { ExtractTransformer } from './extract-transformer.js';
10
+ import { generateSalt, asBufferSource } from './util.js';
11
+ const MODE_ENCRYPT = 'encrypt';
12
+ const MODE_DECRYPT = 'decrypt';
13
+ export const KEY_LENGTH = 16;
14
+ export const TAG_LENGTH = 16;
15
+ const NONCE_LENGTH = 12;
16
+ const RECORD_SIZE = 64 * 1024;
17
+ const HEADER_LENGTH = KEY_LENGTH + 4 + 1; // salt + record size + idlen
18
+ const encoder = new TextEncoder();
19
+ class ECETransformer {
20
+ mode;
21
+ secretKey;
22
+ rs;
23
+ salt;
24
+ seekOpts;
25
+ seq;
26
+ prevChunk;
27
+ nonceBase;
28
+ key;
29
+ constructor(mode, secretKey, rs, salt, seekOpts = {}) {
30
+ if (mode !== MODE_ENCRYPT && mode !== MODE_DECRYPT) {
31
+ throw new Error("mode must be either 'encrypt' or 'decrypt'");
32
+ }
33
+ checkSecretKey(secretKey);
34
+ if (salt != null && salt.byteLength !== KEY_LENGTH) {
35
+ throw new Error('Invalid salt length');
36
+ }
37
+ this.mode = mode;
38
+ this.secretKey = secretKey;
39
+ this.rs = rs;
40
+ this.salt = salt;
41
+ // seekOpts can contain (for decryption only):
42
+ // startSeq: first record sequence number
43
+ // endSeq: last record sequence number + 1 (exclusive)
44
+ // endsPrematurely: true if the last record should have non-final padding
45
+ this.seekOpts = seekOpts;
46
+ // sequence number. -1 is the header, 0 is the first data chunk
47
+ this.seq = -1;
48
+ this.prevChunk = null;
49
+ this.nonceBase = null;
50
+ this.key = null;
51
+ }
52
+ async generateKey() {
53
+ return webcrypto.subtle.deriveKey({
54
+ name: 'HKDF',
55
+ hash: 'SHA-256',
56
+ salt: this.salt,
57
+ info: encoder.encode('Content-Encoding: aes128gcm\0')
58
+ }, this.secretKey, {
59
+ name: 'AES-GCM',
60
+ length: KEY_LENGTH * 8
61
+ }, false, ['encrypt', 'decrypt']);
62
+ }
63
+ async generateNonceBase() {
64
+ const nonceBaseBuf = await webcrypto.subtle.deriveBits({
65
+ name: 'HKDF',
66
+ hash: 'SHA-256',
67
+ salt: this.salt,
68
+ info: encoder.encode('Content-Encoding: nonce\0')
69
+ }, this.secretKey, NONCE_LENGTH * 8);
70
+ return new Uint8Array(nonceBaseBuf);
71
+ }
72
+ generateNonce(seq) {
73
+ if (seq > 0xffffffff) {
74
+ throw new Error('record sequence number exceeds limit');
75
+ }
76
+ if (!this.nonceBase)
77
+ throw new Error('Not nonce base');
78
+ const nonce = this.nonceBase.slice();
79
+ const dv = new DataView(nonce.buffer, nonce.byteOffset, nonce.byteLength);
80
+ const m = dv.getUint32(nonce.byteLength - 4);
81
+ const xor = (m ^ seq) >>> 0; // forces unsigned int xor
82
+ dv.setUint32(nonce.byteLength - 4, xor);
83
+ return nonce;
84
+ }
85
+ pad(data, isLast) {
86
+ const len = data.byteLength;
87
+ if (len + TAG_LENGTH >= this.rs) {
88
+ throw new Error('data too large for record size');
89
+ }
90
+ let padding;
91
+ if (isLast) {
92
+ padding = Uint8Array.of(2);
93
+ }
94
+ else {
95
+ padding = new Uint8Array(this.rs - len - TAG_LENGTH);
96
+ padding[0] = 1;
97
+ }
98
+ const result = new Uint8Array(data.byteLength + padding.byteLength);
99
+ result.set(data, 0);
100
+ result.set(padding, data.byteLength);
101
+ return result;
102
+ }
103
+ unpad(data, isLast) {
104
+ for (let i = data.byteLength - 1; i >= 0; i -= 1) {
105
+ if (data[i] !== 0) {
106
+ if (isLast) {
107
+ if (data[i] !== 2) {
108
+ throw new Error('delimiter of final record is not 2');
109
+ }
110
+ }
111
+ else {
112
+ if (data[i] !== 1) {
113
+ throw new Error('delimiter of not final record is not 1');
114
+ }
115
+ }
116
+ return data.slice(0, i);
117
+ }
118
+ }
119
+ throw new Error('no delimiter found');
120
+ }
121
+ createHeader() {
122
+ if (!this.salt)
123
+ throw new Error('Not salt');
124
+ const header = new Uint8Array(HEADER_LENGTH);
125
+ header.set(this.salt);
126
+ const dv = new DataView(header.buffer, header.byteOffset, header.byteLength);
127
+ dv.setUint32(KEY_LENGTH, this.rs);
128
+ return header;
129
+ }
130
+ readHeader(buffer) {
131
+ if (buffer.byteLength !== HEADER_LENGTH) {
132
+ throw new Error('chunk is not expected header length');
133
+ }
134
+ const header = { salt: null, rs: null };
135
+ const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
136
+ header.salt = buffer.slice(0, KEY_LENGTH);
137
+ header.rs = dv.getUint32(KEY_LENGTH);
138
+ const idlen = dv.getUint8(KEY_LENGTH + 4);
139
+ if (idlen !== 0) {
140
+ throw new Error('Implementation does not support non-zero idlen');
141
+ }
142
+ return header;
143
+ }
144
+ async encryptRecord(record, seq, isLast) {
145
+ const nonce = this.generateNonce(seq);
146
+ const paddedRecord = this.pad(record, isLast);
147
+ if (!this.key)
148
+ throw new Error('not key'); // for TS
149
+ const encryptedRecordBuf = await webcrypto.subtle.encrypt({
150
+ name: 'AES-GCM',
151
+ iv: nonce,
152
+ tagLength: TAG_LENGTH * 8
153
+ }, this.key, paddedRecord);
154
+ return new Uint8Array(encryptedRecordBuf);
155
+ }
156
+ async decryptRecord(encryptedRecord, seq, isLast) {
157
+ if (!this.key)
158
+ throw new Error('not key'); // for TS
159
+ const nonce = this.generateNonce(seq);
160
+ const paddedRecordBuf = await webcrypto.subtle.decrypt({
161
+ name: 'AES-GCM',
162
+ iv: nonce,
163
+ tagLength: TAG_LENGTH * 8
164
+ }, this.key, asBufferSource(encryptedRecord));
165
+ const paddedRecord = new Uint8Array(paddedRecordBuf);
166
+ return this.unpad(paddedRecord, isLast);
167
+ }
168
+ async start(controller) {
169
+ if (this.mode === MODE_ENCRYPT) {
170
+ this.key = await this.generateKey();
171
+ this.nonceBase = await this.generateNonceBase();
172
+ controller.enqueue(this.createHeader());
173
+ this.seq += 1;
174
+ }
175
+ }
176
+ async transformPrevChunk(isLast, controller) {
177
+ if (!this.prevChunk)
178
+ throw new Error('not this.prevChunk');
179
+ if (this.mode === MODE_ENCRYPT) {
180
+ controller.enqueue(await this.encryptRecord(this.prevChunk, this.seq, isLast));
181
+ }
182
+ else {
183
+ if (this.seq === -1) {
184
+ if (!this.prevChunk)
185
+ throw new Error('not this.prevChunk');
186
+ // the first chunk during decryption contains only the header
187
+ const header = this.readHeader(this.prevChunk);
188
+ this.salt = asBufferSource(header.salt);
189
+ if (this.rs !== null && this.rs !== header.rs) {
190
+ throw new Error('Record size declared in constructor does not match record size in encrypted stream');
191
+ }
192
+ this.rs = header.rs;
193
+ this.key = await this.generateKey();
194
+ this.nonceBase = await this.generateNonceBase();
195
+ const startSeq = this.seekOpts.startSeq;
196
+ if (startSeq != null && startSeq > 0) {
197
+ // update the sequence number if decryption doesn't start
198
+ // at seq = 0
199
+ this.seq += startSeq;
200
+ }
201
+ }
202
+ else {
203
+ let expectEndPadding = false;
204
+ if (isLast) {
205
+ // verify encrypted stream length even when seeking
206
+ const endSeq = this.seekOpts.endSeq;
207
+ if (endSeq != null && endSeq !== this.seq + 1) {
208
+ throw new Error('Incorrect encrypted stream length');
209
+ }
210
+ // if the stream ends prematurely, expect a non-end padding byte
211
+ expectEndPadding = !this.seekOpts.endsPrematurely;
212
+ }
213
+ controller.enqueue(await this.decryptRecord(this.prevChunk, this.seq, expectEndPadding));
214
+ }
215
+ }
216
+ this.seq += 1;
217
+ }
218
+ async transform(chunk, controller) {
219
+ if (this.prevChunk) {
220
+ await this.transformPrevChunk(false, controller);
221
+ }
222
+ this.prevChunk = chunk;
223
+ }
224
+ async flush(controller) {
225
+ if (this.prevChunk) {
226
+ await this.transformPrevChunk(true, controller);
227
+ }
228
+ }
229
+ }
230
+ /**
231
+ * Given a plaintext size, return the corresponding encrypted size.
232
+ *
233
+ * plaintextSize: int containing plaintext size
234
+ * rs: int containing record size, optional
235
+ */
236
+ export function encryptedSize(plaintextSize, rs = RECORD_SIZE) {
237
+ if (!Number.isInteger(plaintextSize)) {
238
+ throw new TypeError('plaintextSize');
239
+ }
240
+ if (!Number.isInteger(rs)) {
241
+ throw new TypeError('rs');
242
+ }
243
+ const chunkMetaLength = TAG_LENGTH + 1; // Chunk metadata, tag and delimiter
244
+ return (HEADER_LENGTH +
245
+ plaintextSize +
246
+ chunkMetaLength * Math.ceil(plaintextSize / (rs - chunkMetaLength)));
247
+ }
248
+ /**
249
+ * Given an encrypted size, return the corresponding plaintext size.
250
+ *
251
+ * encryptedSize: int containing encrypted size
252
+ * rs: int containing record size, optional
253
+ */
254
+ export function plaintextSize(encryptedSize, rs = RECORD_SIZE) {
255
+ if (!Number.isInteger(encryptedSize)) {
256
+ throw new TypeError('encryptedSize');
257
+ }
258
+ if (!Number.isInteger(rs)) {
259
+ throw new TypeError('rs');
260
+ }
261
+ const chunkMetaLength = TAG_LENGTH + 1; // Chunk metadata, tag and delimiter
262
+ const encryptedRecordsSize = encryptedSize - HEADER_LENGTH;
263
+ return (encryptedRecordsSize -
264
+ chunkMetaLength *
265
+ Math.ceil(encryptedRecordsSize / rs));
266
+ }
267
+ /**
268
+ * Given a plaintext stream `input`, return an encrypted stream.
269
+ *
270
+ * input: a ReadableStream containing data to be transformed
271
+ * secretKey: CryptoKey containing secret key of size KEY_LENGTH
272
+ * rs: int containing record size, optional
273
+ * salt: Uint8Array containing salt of KEY_LENGTH length, optional
274
+ */
275
+ export function encryptStream(input, secretKey, rs = RECORD_SIZE, salt = generateSalt(KEY_LENGTH)) {
276
+ const stream = transformStream(input, new SliceTransformer(rs - TAG_LENGTH - 1)).readable;
277
+ return transformStream(stream, new ECETransformer(MODE_ENCRYPT, secretKey, rs, salt)).readable;
278
+ }
279
+ /**
280
+ * Given an encrypted stream `input`, return a plaintext stream.
281
+ *
282
+ * input: a ReadableStream containing data to be transformed
283
+ * secretKey: CryptoKey containing secret key of size KEY_LENGTH
284
+ * rs: int containing record size, optional
285
+ */
286
+ export function decryptStream(input, secretKey, rs = RECORD_SIZE) {
287
+ const stream = transformStream(input, new SliceTransformer(HEADER_LENGTH, rs)).readable;
288
+ return transformStream(stream, new ECETransformer(MODE_DECRYPT, secretKey, rs, null)).readable;
289
+ }
290
+ /**
291
+ * Given a desired plaintext byte range specified by `offset` and `length`,
292
+ * and the total size of the encrypted stream in `totalEncryptedLength`,
293
+ * provides a mechanism to decrypt that range.
294
+ *
295
+ * To decrypt an arbitrary plaintext range, the client will need to supply
296
+ * multiple (currently always two) ranges of encrypted data.
297
+ * `decryptStreamRange` returns a promise that resolves to an object containing
298
+ * `ranges`, which is an array of { offset, length } entries specifying the
299
+ * needed encrypted byte ranges, and `encrypt`, a callback function.
300
+ *
301
+ * Once the client has gathered an array `streams` of encrypted ReadableStreams,
302
+ * one for each of these ranges, it should call `decrypt(streams)`. This will
303
+ * then return the final plaintext ReadableStream.
304
+ *
305
+ * secretKey: CryptoKey containing secret key of size KEY_LENGTH
306
+ * offset: int containing plaintext byte offset at which to start decryption
307
+ * length: int containing the number of plaintext bytes to decrypt
308
+ * totalEncryptedLength: The total number of bytes in the encrypted stream
309
+ * rs: int containing record size, optional
310
+ */
311
+ export function decryptStreamRange(secretKey, offset, length, totalEncryptedLength, rs = RECORD_SIZE) {
312
+ if (!Number.isInteger(rs)) {
313
+ throw new TypeError('Missing record size (rs)');
314
+ }
315
+ // Chunk metadata, tag and delimiter
316
+ const chunkMetaLength = TAG_LENGTH + 1;
317
+ // First record needed to decrypt the range
318
+ const startRecord = Math.floor(offset / (rs - chunkMetaLength));
319
+ const offsetInStartRecord = offset % (rs - chunkMetaLength);
320
+ // Record after the last record needed to decrypt the range
321
+ const endRecord = Math.ceil((offset + length) / (rs - chunkMetaLength));
322
+ // Range needed for data (not header) stream
323
+ const dataOffset = HEADER_LENGTH + startRecord * rs;
324
+ let dataEnd = HEADER_LENGTH + endRecord * rs; // exclusive
325
+ // Determine if the stream ends at the end of the encrypted file.
326
+ // This is necessary to correctly validate the padding of the final record.
327
+ const endsPrematurely = dataEnd < totalEncryptedLength;
328
+ if (!endsPrematurely) {
329
+ dataEnd = totalEncryptedLength;
330
+ }
331
+ return {
332
+ ranges: [
333
+ {
334
+ offset: 0,
335
+ length: HEADER_LENGTH
336
+ }, {
337
+ offset: dataOffset,
338
+ length: dataEnd - dataOffset
339
+ }
340
+ ],
341
+ decrypt: (streams) => {
342
+ if (!(streams.every(stream => stream instanceof ReadableStream))) {
343
+ throw new TypeError('Stream is not a ReadableStream');
344
+ }
345
+ // Combine the header and data streams, and then slice how
346
+ // ECETransformer expects
347
+ const encryptedStream = transformStream(concatStreams(streams), new SliceTransformer(HEADER_LENGTH, rs)).readable;
348
+ // Plaintext stream of needed records
349
+ const plaintextStream = transformStream(encryptedStream, new ECETransformer(MODE_DECRYPT, secretKey, rs, null, {
350
+ startSeq: startRecord,
351
+ endSeq: endRecord,
352
+ endsPrematurely
353
+ })).readable;
354
+ // Extract the exact needed bytes from the plaintext stream
355
+ return transformStream(plaintextStream, new ExtractTransformer(offsetInStartRecord, length)).readable;
356
+ }
357
+ };
358
+ }
359
+ function checkSecretKey(secretKey) {
360
+ if (secretKey.type !== 'secret') {
361
+ throw new Error('Invalid key: type must be "secret"');
362
+ }
363
+ if (secretKey.algorithm.name !== 'HKDF') {
364
+ throw new Error('Invalid key: algorithm must be HKDF');
365
+ }
366
+ if (!secretKey.usages.includes('deriveKey')) {
367
+ throw new Error('Invalid key: usages must include deriveKey');
368
+ }
369
+ if (!secretKey.usages.includes('deriveBits')) {
370
+ throw new Error('Invalid key: usages must include deriveBits');
371
+ }
372
+ }
373
+ //# sourceMappingURL=ece.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ece.js","sourceRoot":"","sources":["../../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KAAK,MAAM,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;IAC1D,CAAC;IACD,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAA;IACjE,CAAC;IACD,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAA;IAClE,CAAC;AACL,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Tranform stream that extracts `length` bytes starting at `offset` and turns
3
+ * that result into a new stream. If the input stream ends before `length` bytes,
4
+ * the output stream will error.
5
+ *
6
+ * offset: The number of bytes to skip before starting the output stream
7
+ * length: The number of bytes to include in the output stream. All further
8
+ * input data will be discarded.
9
+ */
10
+ export declare class ExtractTransformer {
11
+ extractStart: number;
12
+ extractEnd: number;
13
+ offset: number;
14
+ constructor(offset: number, length: number);
15
+ transform(chunk: Uint8Array, controller: TransformStreamDefaultController): void;
16
+ flush(controller: TransformStreamDefaultController): void;
17
+ }
18
+ //# sourceMappingURL=extract-transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-transformer.d.ts","sourceRoot":"","sources":["../../src/extract-transformer.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,qBAAa,kBAAkB;IAC3B,YAAY,EAAC,MAAM,CAAA;IACnB,UAAU,EAAC,MAAM,CAAA;IACjB,MAAM,EAAC,MAAM,CAAA;gBAEA,MAAM,EAAC,MAAM,EAAE,MAAM,EAAC,MAAM;IAQzC,SAAS,CAAE,KAAK,EAAC,UAAU,EAAE,UAAU,EAAC,gCAAgC;IAkBxE,KAAK,CAAE,UAAU,EAAC,gCAAgC;CAOrD"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Tranform stream that extracts `length` bytes starting at `offset` and turns
3
+ * that result into a new stream. If the input stream ends before `length` bytes,
4
+ * the output stream will error.
5
+ *
6
+ * offset: The number of bytes to skip before starting the output stream
7
+ * length: The number of bytes to include in the output stream. All further
8
+ * input data will be discarded.
9
+ */
10
+ export class ExtractTransformer {
11
+ extractStart;
12
+ extractEnd;
13
+ offset;
14
+ constructor(offset, length) {
15
+ // desired range to extract
16
+ this.extractStart = offset;
17
+ this.extractEnd = offset + length; // exclusive end
18
+ this.offset = 0; // current offset into input stream
19
+ }
20
+ transform(chunk, controller) {
21
+ // The start and end of `chunk` relative to the entire input stream
22
+ const chunkStart = this.offset;
23
+ const chunkEnd = this.offset + chunk.byteLength; // exclusive end
24
+ this.offset = chunkEnd;
25
+ // What part of `chunk` belongs in the output stream?
26
+ const sliceStart = Math.max(this.extractStart - chunkStart, 0);
27
+ const sliceEnd = Math.min(this.extractEnd - chunkStart, chunk.byteLength);
28
+ // This chunk is entirely outside the range to extract
29
+ if (sliceStart >= chunk.byteLength || sliceEnd <= 0) {
30
+ return;
31
+ }
32
+ controller.enqueue(chunk.subarray(sliceStart, sliceEnd));
33
+ }
34
+ flush(controller) {
35
+ if (this.offset < this.extractEnd) {
36
+ controller.error(new Error('Stream passed through ExtractTransformer ended early'));
37
+ }
38
+ }
39
+ }
40
+ //# sourceMappingURL=extract-transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-transformer.js","sourceRoot":"","sources":["../../src/extract-transformer.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,OAAO,kBAAkB;IAC3B,YAAY,CAAO;IACnB,UAAU,CAAO;IACjB,MAAM,CAAO;IAEb,YAAa,MAAa,EAAE,MAAa;QACrC,2BAA2B;QAC3B,IAAI,CAAC,YAAY,GAAG,MAAM,CAAA;QAC1B,IAAI,CAAC,UAAU,GAAG,MAAM,GAAG,MAAM,CAAA,CAAE,gBAAgB;QAEnD,IAAI,CAAC,MAAM,GAAG,CAAC,CAAA,CAAE,mCAAmC;IACxD,CAAC;IAED,SAAS,CAAE,KAAgB,EAAE,UAA2C;QACpE,mEAAmE;QACnE,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAA;QAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,UAAU,CAAA,CAAE,gBAAgB;QACjE,IAAI,CAAC,MAAM,GAAG,QAAQ,CAAA;QAEtB,qDAAqD;QACrD,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,GAAG,UAAU,EAAE,CAAC,CAAC,CAAA;QAC9D,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,GAAG,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAA;QAEzE,sDAAsD;QACtD,IAAI,UAAU,IAAI,KAAK,CAAC,UAAU,IAAI,QAAQ,IAAI,CAAC,EAAE,CAAC;YAClD,OAAM;QACV,CAAC;QAED,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAA;IAC5D,CAAC;IAED,KAAK,CAAE,UAA2C;QAC9C,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,UAAU,EAAE,CAAC;YAChC,UAAU,CAAC,KAAK,CACZ,IAAI,KAAK,CAAC,sDAAsD,CAAC,CACpE,CAAA;QACL,CAAC;IACL,CAAC;CACJ"}
@@ -0,0 +1,3 @@
1
+ export { Keychain, plaintextSize, encryptedSize } from './keychain.js';
2
+ export { transformStream } from './transform-stream.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { Keychain, plaintextSize, encryptedSize } from './keychain.js';
2
+ export { transformStream } from './transform-stream.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA"}
@@ -0,0 +1,103 @@
1
+ export { encryptedSize, plaintextSize } from './ece.js';
2
+ export declare class Keychain {
3
+ key: Uint8Array<ArrayBuffer>;
4
+ salt: Uint8Array<ArrayBuffer>;
5
+ mainKeyPromise: Promise<CryptoKey>;
6
+ metaKeyPromise: Promise<CryptoKey>;
7
+ authTokenPromise: Promise<Uint8Array>;
8
+ constructor(key?: string | Uint8Array, salt?: string | Uint8Array);
9
+ /**
10
+ * Get an authentication header as a static method.
11
+ */
12
+ static AuthHeader(secretKey: string, salt: string | Uint8Array): Promise<string>;
13
+ static Header(writeToken: string): string;
14
+ /**
15
+ * Get the main key as a `base64url` encoded string
16
+ */
17
+ get keyB64(): string;
18
+ /**
19
+ * Get the salt as base64 string
20
+ */
21
+ get saltB64(): string;
22
+ /**
23
+ * get a promise for the auth token.
24
+ * @returns {Promise<Uint8Array>}
25
+ */
26
+ authToken(): Promise<Uint8Array>;
27
+ /**
28
+ * Get the auth token as a base64 string
29
+ */
30
+ authTokenB64(): Promise<string>;
31
+ /**
32
+ * Get a header string: `Bearer sync-v1 ${authTokenB64}`.
33
+ * Pass in a token, or else this will use the `authToken` derived from
34
+ * the main key.
35
+ */
36
+ authHeader(tokenString?: string): Promise<string>;
37
+ /**
38
+ * Set the auth token
39
+ * @param authToken The new token
40
+ */
41
+ setAuthToken(authToken?: string | Uint8Array): void;
42
+ /**
43
+ * Take a stream, return an encrypted stream.
44
+ * @param stream Input stream
45
+ * @returns {Promise<ReadableStream>}
46
+ */
47
+ encryptStream(stream: ReadableStream<Uint8Array>): Promise<ReadableStream<Uint8Array>>;
48
+ /**
49
+ * Encrypt and return some data; don't stream.
50
+ *
51
+ * NOTE: This generates a new key each time it is called, via
52
+ * `this.generateKey`.
53
+ *
54
+ * @param bytes
55
+ * @param {{ iv?:Uint8Array, size?:number }} [opts] Optional params,
56
+ * `iv` and `size`. If `size` is omitted, default is 16 bytes. `iv` is
57
+ * a random 12 bits, will be generated if not passed in.
58
+ * @returns {Promise<Uint8Array>}
59
+ */
60
+ encryptBytes(bytes: ArrayBuffer | Uint8Array, opts?: {
61
+ iv?: Uint8Array;
62
+ size?: number;
63
+ }): Promise<Uint8Array>;
64
+ /**
65
+ * Decrypt in memory, not streaming.
66
+ */
67
+ decryptBytes(bytes: Uint8Array): Promise<ArrayBuffer>;
68
+ /**
69
+ * Derive a new AES-GCM key from the main key.
70
+ *
71
+ * @param {number} [keyLength] Optional size for the key, in bytes, eg,
72
+ * `16` or `32`.
73
+ * @returns {Promise<CryptoKey>}
74
+ */
75
+ generateKey(keyLength?: number): Promise<CryptoKey>;
76
+ /**
77
+ * Take an encrypted stream, return a decrypted stream.
78
+ * @param encryptedStream The input (encrypted) stream
79
+ * @returns The decrypted stream
80
+ */
81
+ decryptStream(encryptedStream: ReadableStream<Uint8Array>): Promise<ReadableStream<Uint8Array>>;
82
+ /**
83
+ * Returns an object containing `ranges`, an array of objects
84
+ * containing `offset` and `length` integers specifying the encrypted byte
85
+ * ranges that are needed to decrypt the client's specified range, and a
86
+ * `decrypt` function.
87
+ *
88
+ * @param {number} offset Integer
89
+ * @param {number} length Integer
90
+ * @param {number} totalEncryptedLength Integer
91
+ * @returns {Promise<{ ranges, decrypt }>}
92
+ */
93
+ decryptStreamRange(offset: number, length: number, totalEncryptedLength: number): Promise<{
94
+ ranges: {
95
+ offset: number;
96
+ length: number;
97
+ }[];
98
+ decrypt: (streams: ReadableStream[]) => ReadableStream;
99
+ }>;
100
+ encryptMeta(meta: Uint8Array): Promise<Uint8Array>;
101
+ decryptMeta(ivEncryptedMeta: Uint8Array): Promise<Uint8Array>;
102
+ }
103
+ //# sourceMappingURL=keychain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keychain.d.ts","sourceRoot":"","sources":["../../src/keychain.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAKvD,qBAAa,QAAQ;IACjB,GAAG,EAAC,UAAU,CAAC,WAAW,CAAC,CAAA;IAC3B,IAAI,EAAC,UAAU,CAAC,WAAW,CAAC,CAAA;IAC5B,cAAc,EAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IACjC,cAAc,EAAC,OAAO,CAAC,SAAS,CAAC,CAAA;IACjC,gBAAgB,EAAC,OAAO,CAAC,UAAU,CAAC,CAAA;gBAEvB,GAAG,CAAC,EAAC,MAAM,GAAC,UAAU,EAAE,IAAI,CAAC,EAAC,MAAM,GAAC,UAAU;IA+C5D;;OAEG;WACU,UAAU,CAAE,SAAS,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,GAAC,UAAU;IAwBjE,MAAM,CAAC,MAAM,CAAE,UAAU,EAAC,MAAM;IAIhC;;OAEG;IACH,IAAI,MAAM,IAAI,MAAM,CAEnB;IAED;;OAEG;IACH,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED;;;OAGG;IACG,SAAS,IAAI,OAAO,CAAC,UAAU,CAAC;IAItC;;OAEG;IACG,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;IAKrC;;;;OAIG;IACG,UAAU,CAAE,WAAW,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,MAAM,CAAC;IAQtD;;;OAGG;IACH,YAAY,CAAE,SAAS,CAAC,EAAC,MAAM,GAAC,UAAU,GAAE,IAAI;IAIhD;;;;OAIG;IACG,aAAa,CACf,MAAM,EAAC,cAAc,CAAC,UAAU,CAAC,GACnC,OAAO,CAAC,cAAc,CAAC,UAAU,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