3 const crypto
= require('crypto');
4 const zlib
= require('zlib');
5 const { promisify
} = require('util');
6 const { base64ToBase64URL
, base64URLToBase64
} = require('@squeep/base64url');
7 const common
= require('./common');
8 const allVersions
= require('./version-parameters');
9 const { performance
} = require('perf_hooks');
11 const _fileScope
= common
.fileScope(__filename
);
13 const brotliCompressAsync
= promisify(zlib
.brotliCompress
);
14 const brotliDecompressAsync
= promisify(zlib
.brotliDecompress
);
15 const deflateRawAsync
= promisify(zlib
.deflateRaw
);
16 const inflateRawAsync
= promisify(zlib
.inflateRaw
);
17 const scryptAsync
= promisify(crypto
.scrypt
);
20 * Only you will know what's inside your...
22 * |\/ | | | __| __| _ \ __| | __ \ _ \ \ /
23 * | | | |\__ \ | __/ | | | | | ( |` <
24 * _| _|\__, |____/\__|\___|_| \__, |____/ \___/ _/\_\
27 * Our very own way of converting a buffer or serializable object
28 * to and from an opaque and web-safe representation, which we can
29 * let anyone see without disclosing the contents, nor allowing it
30 * to be modified without detection.
32 * The result is a Base64URL-encoded byte-array consisting of:
33 * - version, indicating format and encryption algorithm
34 * - flags, indicating state of the contents
35 * - iv/nonce, randomness for the algorithm
36 * - salt, applied to a secret to derive a key
37 * - tag, additional room for encryption to validate the previous fields
38 * - payload, encrypted version of possibly compressed and serialized object
43 const availableFlags
= {
44 Brotli: (1<<0), // Slightly better compression, but slow
45 Flate: (1<<1), // Slightly worse compression, but much faster especially with larger payloads. Prefer this.
46 FutureCompression: (1<<0)|(1<<1), // Something else, like zstd maybe.
54 BufferPayload: (1<<7), // Payload is a buffer, not an object
56 const compressionFlagsMask
= (availableFlags
.Flate
| availableFlags
.Brotli
);
57 const compressionFlagsShift
= 0;
58 const payloadFlagsMask
= (availableFlags
.BufferPayload
);
59 const payloadFlagsShift
= 7;
64 * @param {Console} logger
65 * @param {Object} options
66 * @param {String|String[]} options.encryptionSecret - if an array, will always encrypt with first secret, will attempt to decrypt with all; useful for rolling secrets
67 * @param {Number=} options.defaultFlags
69 constructor(logger
, options
= {}) {
71 this.options
= options
;
73 this.secrets
= common
.ensureArray(options
.encryptionSecret
);
74 if (!this.secrets
.length
) {
75 throw new Error('missing encryption secret');
78 // Filter any unavailable algorithms
79 const availableCiphers
= crypto
.getCiphers();
80 this.versionParameters
= Object
.entries(allVersions
).reduce((acc
, [v
, p
]) => {
81 if (availableCiphers
.includes(p
.algorithm
)) {
82 acc
[v
] = p
; // eslint-disable-line security/detect-object-injection
88 this.bestVersion
= Number(Object
.keys(this.versionParameters
).sort().pop());
89 if (Number
.isNaN(this.bestVersion
)) {
90 throw new Error('no supported versions available');
93 this.Flags
= availableFlags
;
94 this.defaultFlags
= 'defaultFlags' in options
? options
.defaultFlags : availableFlags
.Flate
;
95 if (this.defaultFlags
< 0 || this.defaultFlags
> 255) {
96 throw new RangeError('Invalid default flag value');
102 * Parse the bits out of the flags.
104 static _decodeFlags(flags
) {
106 compression: (flags
& compressionFlagsMask
) >> compressionFlagsShift
,
107 payloadIsBuffer: Boolean((flags
& payloadFlagsMask
) >> payloadFlagsShift
),
113 * Put contents into a mysterious box.
114 * @param {Object|Buffer} contents
117 async
pack(contents
, version
= this.bestVersion
, flags
= this.defaultFlags
) {
118 const _scope
= _fileScope('pack');
120 start: performance
.now(),
128 if (!(version
in this.versionParameters
)) {
129 throw new RangeError(`MysteryBox format version ${version} not supported`);
131 // eslint-disable-next-line security/detect-object-injection
132 const v
= this.versionParameters
[version
];
134 const { compression
} = MysteryBox
._decodeFlags(flags
);
136 if (Buffer
.isBuffer(contents
)) {
137 flags
|= this.Flags
.BufferPayload
;
139 contents
= JSON
.stringify(contents
);
142 const versionBuffer
= Buffer
.alloc(v
.versionBytes
);
143 versionBuffer
.writeUInt8(v
.version
, 0);
145 const [iv
, salt
] = await Promise
.all([
148 ].map((b
) => common
.randomBytesAsync(b
)));
150 timingsMs
.preCompress
= performance
.now();
151 let compressedContents
;
152 switch (compression
) {
153 case 0: // No compression requested
154 compressedContents
= contents
;
156 case this.Flags
.Brotli:
157 compressedContents
= await
brotliCompressAsync(contents
);
159 case this.Flags
.Flate:
160 compressedContents
= await
deflateRawAsync(contents
);
163 timingsMs
.postCompress
= timingsMs
.preCrypt
= performance
.now();
166 if (compressedContents
.length
>= contents
.length
) {
167 flags
= flags
& ~compressionFlagsMask
;
170 payload
= compressedContents
;
172 const flagsBuffer
= Buffer
.alloc(v
.flagsBytes
);
173 flagsBuffer
.writeUInt8(flags
, 0);
175 // Authenticate all this data
176 const aadBuffer
= Buffer
.concat([versionBuffer
, flagsBuffer
, iv
, salt
]);
178 // Always encrypt with first secret
179 const secret
= this.secrets
[0];
180 const key
= await
scryptAsync(secret
, salt
, v
.keyBytes
);
181 const cipher
= crypto
.createCipheriv(v
.algorithm
, key
, iv
, v
.algOptions
);
182 cipher
.setAAD(aadBuffer
);
183 const encrypted
= cipher
.update(payload
);
184 const final
= cipher
.final();
185 const tag
= cipher
.getAuthTag();
187 const merged
= Buffer
.concat([versionBuffer
, flagsBuffer
, iv
, salt
, tag
, encrypted
, final
]).toString('base64');
188 const result
= base64ToBase64URL(merged
);
189 timingsMs
.end
= timingsMs
.postCrypt
= performance
.now();
191 this.logger
.debug(_scope
, 'statistics', { version
, flags: this._prettyFlags(flags
), serialized: contents
.length
, compressed: payload
.length
, encoded: result
.length
, ...MysteryBox
._timingsLog(timingsMs
) });
198 * Take contents out of a mysterious box.
199 * @param {String} box - Base64URL encoded payload
203 const _scope
= _fileScope('unpack');
205 start: performance
.now(),
214 throw new RangeError('nothing to unpack');
217 const raw
= Buffer
.from(base64URLToBase64(box
), 'base64');
220 const version
= raw
.subarray(offset
, 1).readUInt8(0);
221 if (!(version
in this.versionParameters
)) {
222 throw new RangeError('unsupported version');
224 // eslint-disable-next-line security/detect-object-injection
225 const v
= this.versionParameters
[version
];
226 offset
+= v
.versionBytes
;
228 const minBytes
= v
.versionBytes
+ v
.flagsBytes
+ v
.ivBytes
+ v
.saltBytes
+ v
.tagBytes
;
229 if (raw
.length
< minBytes
) {
230 throw new RangeError('not enough to unpack');
233 const flags
= raw
.subarray(offset
, offset
+ v
.flagsBytes
).readUInt8(0);
234 offset
+= v
.flagsBytes
;
236 const { compression
, payloadIsBuffer
} = MysteryBox
._decodeFlags(flags
);
238 const iv
= raw
.subarray(offset
, offset
+ v
.ivBytes
);
241 const salt
= raw
.subarray(offset
, offset
+ v
.saltBytes
);
242 offset
+= v
.saltBytes
;
244 const aad
= raw
.subarray(0, offset
); // Everything up to here
246 const tag
= raw
.subarray(offset
, offset
+ v
.tagBytes
);
247 offset
+= v
.tagBytes
;
249 const encrypted
= raw
.subarray(offset
);
251 timingsMs
.preCrypt
= performance
.now();
256 for await (const secret
of this.secrets
) {
257 const key
= await
scryptAsync(secret
, salt
, v
.keyBytes
);
258 const decipher
= crypto
.createDecipheriv(v
.algorithm
, key
, iv
, v
.algOptions
);
259 decipher
.setAAD(aad
);
260 decipher
.setAuthTag(tag
);
263 decrypted
= Buffer
.concat([decipher
.update(encrypted
), decipher
.final()]);
276 timingsMs
.preCompress
= timingsMs
.postCrypt
= performance
.now();
277 switch (compression
) {
278 case 0: // No compression
281 case this.Flags
.Brotli:
282 payload
= await
brotliDecompressAsync(decrypted
);
284 case this.Flags
.Flate:
285 payload
= await
inflateRawAsync(decrypted
);
288 timingsMs
.end
= timingsMs
.postCompress
= performance
.now();
290 if (!payloadIsBuffer
) {
291 payload
= JSON
.parse(payload
.toString('utf8'));
294 this.logger
.debug(_scope
, 'statistics', { version
, flags: this._prettyFlags(flags
), ...MysteryBox
._timingsLog(timingsMs
) });
301 * Pretty-print flag values
302 * @param {Number} flags
305 _prettyFlags(flags
) {
306 const flagNames
= Object
.entries(this.Flags
).reduce((acc
, cur
) => {
307 const [flagName
, flagValue
] = cur
;
308 if ((flags
& flagValue
) === flagValue
) {
313 return `0x${flags.toString(16).padStart(2, '0')} [${flagNames.join(',')}]`;
318 * Everyone loves numbers.
319 * @param {Object} timingsMs
322 static _timingsLog({ start
, preCompress
, postCompress
, preCrypt
, postCrypt
, end
}) {
324 totalMs: end
- start
,
325 compressMs: postCompress
- preCompress
,
326 cryptMs: postCrypt
- preCrypt
,
332 // Expose for stubbing in tests
333 MysteryBox
._test
= { crypto
};
335 module
.exports
= MysteryBox
;