ddae8155e1cc29cf385e0390a7ca1c178f392d2d
3 const { EventEmitter
} = require('events');
4 const crypto
= require('crypto');
5 const zlib
= require('zlib');
6 const { promisify
} = require('util');
7 const common
= require('./common');
8 const { MysteryBoxError
} = require('./errors');
9 const allVersions
= require('./version-parameters');
10 const { performance
} = require('perf_hooks');
11 const { name: packageName
, version: packageVersion
} = require('../package.json');
18 const brotliCompressAsync
= promisify(zlib
.brotliCompress
);
19 const brotliDecompressAsync
= promisify(zlib
.brotliDecompress
);
20 const deflateRawAsync
= promisify(zlib
.deflateRaw
);
21 const inflateRawAsync
= promisify(zlib
.inflateRaw
);
22 const scryptAsync
= promisify(crypto
.scrypt
);
25 * Only you will know what's inside your...
27 * |\/ | | | __| __| _ \ __| | __ \ _ \ \ /
28 * | | | |\__ \ | __/ | | | | | ( |` <
29 * _| _|\__, |____/\__|\___|_| \__, |____/ \___/ _/\_\
32 * Our very own way of converting a buffer or serializable object
33 * to and from an opaque and web-safe representation, which we can
34 * let anyone see without disclosing the contents, nor allowing it
35 * to be modified without detection.
37 * The result is a Base64URL-encoded byte-array consisting of:
38 * - version, indicating format and encryption algorithm
39 * - flags, indicating state of the contents
40 * - iv/nonce, randomness for the algorithm
41 * - salt, applied to a secret to derive a key
42 * - tag, additional room for encryption to validate the previous fields
43 * - payload, encrypted version of possibly compressed and serialized object
48 const availableFlags
= {
49 Brotli: (1<<0), // Slightly better compression, but slow
50 Flate: (1<<1), // Slightly worse compression, but much faster especially with larger payloads. Prefer this.
51 FutureCompression: (1<<0)|(1<<1), // Something else, like zstd maybe.
59 BufferPayload: (1<<7), // Payload is a buffer, not an object
61 const compressionFlagsMask
= (availableFlags
.Flate
| availableFlags
.Brotli
);
62 const compressionFlagsShift
= 0;
63 const payloadFlagsMask
= (availableFlags
.BufferPayload
);
64 const payloadFlagsShift
= 7;
67 class MysteryBox
extends EventEmitter
{
69 * @param {Object} options
70 * @param {String|String[]} options.encryptionSecret - if an array, will always encrypt with first secret, will attempt to decrypt with all; useful for rolling secrets
71 * @param {Number=} options.defaultFlags
73 constructor(options
= {}, ...args
) {
75 this.secrets
= common
.ensureArray(options
.encryptionSecret
);
76 if (!this.secrets
.length
) {
77 throw new MysteryBoxError('missing encryption secret');
80 // Filter any unavailable algorithms
81 const availableCiphers
= crypto
.getCiphers();
82 const availableHashes
= crypto
.getHashes();
83 // Add legacy scrypt to available hashes for filtering key derivations
84 availableHashes
.push(allVersions
.KD
.SCRYPT
);
85 this.versionParameters
= Object
.entries(allVersions
).reduce((acc
, [v
, p
]) => {
86 const validCipher
= availableCiphers
.includes(p
.algorithm
);
87 const validKeyDeriver
= availableHashes
.includes(p
.keyDeriver
);
88 if (validCipher
&& validKeyDeriver
) {
89 acc
[v
] = p
; // eslint-disable-line security/detect-object-injection
95 this.bestVersion
= Number(Object
.keys(this.versionParameters
).sort().pop());
96 if (Number
.isNaN(this.bestVersion
)) {
97 throw new MysteryBoxError('no supported versions available');
100 this.Flags
= availableFlags
;
101 this.defaultFlags
= 'defaultFlags' in options
? options
.defaultFlags : availableFlags
.Flate
;
102 if (this.defaultFlags
< 0 || this.defaultFlags
> 255) {
103 throw new MysteryBoxError('Invalid default flag value');
109 * Parse the bits out of the flags.
111 static _decodeFlags(flags
) {
113 compression: (flags
& compressionFlagsMask
) >> compressionFlagsShift
,
114 payloadIsBuffer: Boolean((flags
& payloadFlagsMask
) >> payloadFlagsShift
),
122 static async
_keyFromSecret(deriver
, secret
, salt
, keyBytes
) {
124 case allVersions
.KD
.SHAKE256: {
125 const hash
= crypto
.createHash(allVersions
.KD
.SHAKE256
, { outputLength: keyBytes
});
128 return hash
.digest();
131 case allVersions
.KD
.BLAKE2B512: {
132 const hash
= crypto
.createHash(allVersions
.KD
.BLAKE2B512
);
135 const digest
= hash
.digest();
136 // should assert that keyBytes <= 64
137 // but not concerned about that for now
138 // until we have a new algorithm with bigger key size
139 return digest
.subarray(0, keyBytes
);
142 case allVersions
.KD
.SCRYPT:
143 return scryptAsync(secret
, salt
, keyBytes
);
146 throw new MysteryBoxError('unsupported key deriver');
152 * Return bits and bit mask for given number of encoded bytes.
153 * @param {Number} numBytes
156 static _versionHeaderBits(numBytes
) {
157 // Round up to 8 bits in result, just to be proper.
158 const resultBits
= (((numBytes
+ 7) >> 3) << 3) >>> 0;
160 headerValue: ((0xff << (resultBits
- numBytes
+ 1)) & 0xff) >>> 0,
161 headerMask: ((0xff << (resultBits
- numBytes
)) & 0xff) >>> 0,
167 * Parse a byte into the total number of bytes in the packed number,
168 * returning that and the new value for the first byte, to update in the
169 * buffer before parsing as an unsigned integer.
170 * Number of packed bytes is indicated by location of first leading 0 bit.
171 * Support for numbers larger than 127 is of dubious practicality, but here
173 * @param {Number} firstByte
176 static _versionHeaderDecode(firstByte
) {
177 for (let numBytes
= 1; numBytes
<= 8; numBytes
++) {
181 } = MysteryBox
._versionHeaderBits(numBytes
);
182 if (((firstByte
& headerMask
) >>> 0) === headerValue
) {
183 const restMask
= (~headerMask
& 0xff) >>> 0;
186 firstByte: (firstByte
& restMask
) >>> 0,
190 // Nine bytes would be an extravagence.
191 throw new MysteryBoxError(`unsupported version header (0x${firstByte.toString(16)})`);
196 * Decode leading bytes of buffer as version identifier.
197 * In the first byte, the position of the first unset bit indicates how
198 * many total bytes comprise the version, in big-endian encoding.
199 * Returns decoded version and number of bytes used to decode.
200 * Only supports up to 6-byte numbers, and of those, only up to 4398046511103.
201 * @param {Buffer} buf - N.B. will be mogrified
204 static _versionDecode(buf
) {
205 const headerByte
= buf
.readUInt8(0);
206 const { numBytes: versionBytes
, firstByte
} = MysteryBox
._versionHeaderDecode(headerByte
);
207 if (versionBytes
=== 1) {
214 if (versionBytes
> 6) {
215 throw new MysteryBoxError(`unsupported version (${versionBytes} bytes)`);
218 // Otherwise, update the masked first byte and parse the rest of the buffer.
221 version: buf
.readUIntBE(0, versionBytes
),
228 * Encode a version identifier into a buffer of a variable number of bytes.
229 * @param {Number} version
232 static _versionEncode(version
) {
233 let versionBytes
= 0;
235 if (version
<= 0x7f) { // 0-127
237 } else if (version
<= 0x3fff) { // 128-16383
239 } else if (version
<= 0x1fffff) { // 16384-2097151
241 } else if (version
<= 0x0fffffff) { // 2097152-268435455
243 } else if (version
<= 0x07ffffffff) { // 268435456-34359738367
245 } else if (version
<= 0x03ffffffffff) { // 34359738368-4398046511103
248 throw new MysteryBoxError(`version too large to encode (${version})`);
251 const buffer
= Buffer
.alloc(versionBytes
);
252 buffer
.writeUIntBE(version
, 0, versionBytes
);
253 const headerByte
= ((0xff << (8 - versionBytes
+ 1)) & 0xff) >>> 0;
254 buffer
[0] = (buffer
[0] | headerByte
) >>> 0;
264 * Stats tracked when packing/unpacking boxes.
267 static _newStats(method
) {
274 serializedBytes: undefined,
275 compressedBytes: undefined,
278 start: performance
.now(),
290 * Put contents into a mysterious box.
291 * @param {Object|Buffer} contents
292 * @param {Number=} version
293 * @param {Number=} flags
296 async
pack(contents
, version
= this.bestVersion
, flags
= this.defaultFlags
) {
297 const { stats
, timingsMs
} = MysteryBox
._newStats('pack');
299 if (!(version
in this.versionParameters
)) {
300 throw new MysteryBoxError(`MysteryBox format version ${version} not supported`);
302 // eslint-disable-next-line security/detect-object-injection
303 const v
= this.versionParameters
[version
];
304 stats
.version
= version
;
306 const { compression
, payloadIsBuffer
} = MysteryBox
._decodeFlags(flags
);
308 if (Buffer
.isBuffer(contents
)) {
309 // Ensure payloadIsBuffer flag is set when contents are indeed a Buffer
310 flags
|= this.Flags
.BufferPayload
;
312 if (payloadIsBuffer
) {
313 // Flag is set, but contents are not a Buffer? Try to coerce contents.
314 contents
= Buffer
.from(contents
);
316 // Otherwise attempt to serialize the object
317 contents
= JSON
.stringify(contents
);
320 stats
.serializedBytes
= Buffer
.byteLength(contents
);
322 const { buffer: versionBuffer
, versionBytes
} = MysteryBox
._versionEncode(v
.version
);
323 if (versionBytes
!== v
.versionBytes
) {
324 throw new MysteryBoxError('internal inconsistency, mismatched version byte length');
327 const [iv
, salt
] = await Promise
.all([
330 ].map((b
) => common
.randomBytesAsync(b
)));
332 timingsMs
.preCompress
= performance
.now();
333 let compressedContents
;
334 switch (compression
) {
335 case 0: // No compression requested
336 compressedContents
= contents
;
338 case this.Flags
.Brotli:
339 compressedContents
= await
brotliCompressAsync(contents
);
341 case this.Flags
.Flate:
342 compressedContents
= await
deflateRawAsync(contents
);
345 stats
.compressedBytes
= Buffer
.byteLength(compressedContents
);
346 // const compressionRatio = stats.compressedBytes / stats.serializedBytes;
347 timingsMs
.postCompress
= timingsMs
.preCrypt
= performance
.now();
350 if (stats
.compressedBytes
>= stats
.serializedBytes
) {
351 // If compression is not beneficial enough, or detrimental, do not use
352 flags
= flags
& ~compressionFlagsMask
;
354 stats
.compressedBytes
= undefined;
356 payload
= compressedContents
;
359 const flagsBuffer
= Buffer
.alloc(v
.flagsBytes
);
360 flagsBuffer
.writeUInt8(flags
, 0);
361 stats
.flagsRaw
= flags
;
362 stats
.flags
= this._prettyFlags(flags
);
364 // Authenticate all this data
365 const aadBuffer
= Buffer
.concat([versionBuffer
, flagsBuffer
, iv
, salt
]);
367 // Always encrypt with first secret
368 const secret
= this.secrets
[0];
369 const key
= await MysteryBox
._keyFromSecret(v
.keyDeriver
, secret
, salt
, v
.keyBytes
);
370 const cipher
= crypto
.createCipheriv(v
.algorithm
, key
, iv
, v
.algOptions
);
371 cipher
.setAAD(aadBuffer
);
372 const encrypted
= cipher
.update(payload
);
373 const final
= cipher
.final();
374 const tag
= cipher
.getAuthTag();
376 const result
= Buffer
.concat([versionBuffer
, flagsBuffer
, iv
, salt
, tag
, encrypted
, final
]).toString('base64url');
377 timingsMs
.end
= timingsMs
.postCrypt
= performance
.now();
379 this.emit('statistics', { ...stats
, ...MysteryBox
._timingsLog(timingsMs
), ...packageInfo
});
386 * Take contents out of a mysterious box.
387 * @param {String} box - Base64URL encoded payload
391 const { stats
, timingsMs
} = MysteryBox
._newStats('unpack');
394 throw new MysteryBoxError('nothing to unpack');
397 const raw
= Buffer
.from(box
, 'base64url');
400 const { version
, versionBytes
} = MysteryBox
._versionDecode(raw
);
401 if (!(version
in this.versionParameters
)) {
402 throw new MysteryBoxError(`unsupported version (${version})`);
404 // eslint-disable-next-line security/detect-object-injection
405 const v
= this.versionParameters
[version
];
407 if (v
.versionBytes
!== versionBytes
) {
408 throw new MysteryBoxError('internal inconsistency, mismatched version byte length');
410 offset
+= v
.versionBytes
;
411 stats
.version
= version
;
413 const minBytes
= v
.versionBytes
+ v
.flagsBytes
+ v
.ivBytes
+ v
.saltBytes
+ v
.tagBytes
;
414 if (raw
.length
< minBytes
) {
415 throw new MysteryBoxError('not enough to unpack');
418 const flags
= raw
.subarray(offset
, offset
+ v
.flagsBytes
).readUInt8(0);
419 offset
+= v
.flagsBytes
;
420 stats
.flagsRaw
= flags
;
422 const { compression
, payloadIsBuffer
} = MysteryBox
._decodeFlags(flags
);
424 const iv
= raw
.subarray(offset
, offset
+ v
.ivBytes
);
427 const salt
= raw
.subarray(offset
, offset
+ v
.saltBytes
);
428 offset
+= v
.saltBytes
;
430 const aad
= raw
.subarray(0, offset
); // Everything up to here
432 const tag
= raw
.subarray(offset
, offset
+ v
.tagBytes
);
433 offset
+= v
.tagBytes
;
435 const encrypted
= raw
.subarray(offset
);
437 timingsMs
.preCrypt
= performance
.now();
442 for await (const secret
of this.secrets
) {
443 const key
= await MysteryBox
._keyFromSecret(v
.keyDeriver
, secret
, salt
, v
.keyBytes
);
444 const decipher
= crypto
.createDecipheriv(v
.algorithm
, key
, iv
, v
.algOptions
);
445 decipher
.setAAD(aad
);
446 decipher
.setAuthTag(tag
);
449 decrypted
= Buffer
.concat([decipher
.update(encrypted
), decipher
.final()]);
462 timingsMs
.preCompress
= timingsMs
.postCrypt
= performance
.now();
463 switch (compression
) {
464 case 0: // No compression
467 case this.Flags
.Brotli:
468 payload
= await
brotliDecompressAsync(decrypted
);
470 case this.Flags
.Flate:
471 payload
= await
inflateRawAsync(decrypted
);
474 timingsMs
.end
= timingsMs
.postCompress
= performance
.now();
475 stats
.serializedBytes
= payload
.byteLength
;
477 stats
.compressedBytes
= decrypted
.byteLength
;
480 if (!payloadIsBuffer
) {
481 payload
= JSON
.parse(payload
.toString('utf8'));
484 stats
.flags
= this._prettyFlags(flags
);
485 this.emit('statistics', { ...stats
, ...MysteryBox
._timingsLog(timingsMs
), ...packageInfo
});
492 * Pretty-print flag values
493 * @param {Number} flags
496 _prettyFlags(flags
) {
497 const flagNames
= Object
.entries(this.Flags
).reduce((acc
, cur
) => {
498 const [flagName
, flagValue
] = cur
;
499 if ((flags
& flagValue
) === flagValue
) {
504 return `0x${flags.toString(16).padStart(2, '0')} [${flagNames.join(',')}]`;
509 * Everyone loves numbers.
510 * @param {Object} timingsMs
513 static _timingsLog({ start
, preCompress
, postCompress
, preCrypt
, postCrypt
, end
}) {
515 totalMs: end
- start
,
516 compressMs: postCompress
- preCompress
,
517 cryptMs: postCrypt
- preCrypt
,
523 module
.exports
= MysteryBox
;