-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.ts
More file actions
461 lines (404 loc) · 11.8 KB
/
encryption.ts
File metadata and controls
461 lines (404 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import crypto from "node:crypto";
/*
* Configuration Constants
*/
/**
* Constants for PBKDF2-HMAC-SHA256 key derivation.
*
* We are using PBKDF2 with SHA-256, 600000 iterations, and a 128-bit salt in accordance with
* {@link https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf NIST}
* and
* {@link https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#pbkdf2 OWASP}
* recommendations.
*
* The key length is 256 bits since we are using AES-256-GCM.
*/
const PBKDF2_HMAC_SHA256_KEY_DERIVATION = {
DIGEST: "sha256",
ITERATIONS: 600_000,
KEY_LENGTH_BYTES: 32, // 256 bits
SALT_LENGTH_BYTES: 16, // 128 bits
} as const;
/**
* Constants for AES-256-GCM encryption.
*
* We are using a 96-bit IV and a 128-bit tag in accordance with
* {@link https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf NIST}
* recommendations.
*/
const AES_256_GCM_ENCRYPTION = {
ALGORITHM: "aes-256-gcm",
IV_LENGTH_BYTES: 12, // 96 bits
TAG_LENGTH_BYTES: 16, // 128 bits
} as const;
/*
* Branded Types for Cryptographic Buffers
*/
interface PassphraseBrand {
readonly Passphrase: unique symbol;
}
type Passphrase_t = Buffer & PassphraseBrand;
interface SaltBrand {
readonly Salt: unique symbol;
}
type Salt_t = Buffer & SaltBrand;
interface TagBrand {
readonly Tag: unique symbol;
}
type Tag_t = Buffer & TagBrand;
interface IVBrand {
readonly IV: unique symbol;
}
type IV_t = Buffer & IVBrand;
interface CiphertextBrand {
readonly Ciphertext: unique symbol;
}
type Ciphertext_t = Buffer & CiphertextBrand;
interface PlaintextBrand {
readonly Plaintext: unique symbol;
}
type Plaintext_t = Buffer & PlaintextBrand;
interface SymmetricKeyBrand {
readonly SymmetricKey: unique symbol;
}
type SymmetricKey_t = Buffer & SymmetricKeyBrand;
/*
* Interface Definitions for Cryptographic Operations
*/
interface KeyDerivationOutput_t {
key: SymmetricKey_t;
}
interface KeyEncryptionOutput_t {
tag: Tag_t;
iv: IV_t;
ciphertext: Ciphertext_t;
}
interface KeyDecryptionOutput_t {
plaintext: Plaintext_t;
}
type PassphraseEncryptionOutput_t = KeyEncryptionOutput_t & {
salt: Salt_t;
};
type PassphraseDecryptionOutput_t = KeyDecryptionOutput_t;
interface EncodedPassphraseEncryptionOutputBrand {
readonly EncodedPassphraseEncryptionOutput: unique symbol;
}
type EncodedPassphraseEncryptionOutput_t = string &
EncodedPassphraseEncryptionOutputBrand;
/*
* Type Validation Functions
*/
function isPassphrase(value: unknown): value is Passphrase_t {
if (!Buffer.isBuffer(value)) return false;
// The passphrase can be any length, so we don't have anything else to check.
return true;
}
function isSalt(value: unknown): value is Salt_t {
if (!Buffer.isBuffer(value)) return false;
if (value.length !== PBKDF2_HMAC_SHA256_KEY_DERIVATION.SALT_LENGTH_BYTES)
return false;
return true;
}
function isTag(value: unknown): value is Tag_t {
if (!Buffer.isBuffer(value)) return false;
if (value.length !== AES_256_GCM_ENCRYPTION.TAG_LENGTH_BYTES) return false;
return true;
}
function isIV(value: unknown): value is IV_t {
if (!Buffer.isBuffer(value)) return false;
if (value.length !== AES_256_GCM_ENCRYPTION.IV_LENGTH_BYTES) return false;
return true;
}
function isCiphertext(value: unknown): value is Ciphertext_t {
if (!Buffer.isBuffer(value)) return false;
// The ciphertext can be any length, so we don't have anything else to check.
return true;
}
function isPlaintext(value: unknown): value is Plaintext_t {
if (!Buffer.isBuffer(value)) return false;
// The plaintext can be any length, so we don't have anything else to check.
return true;
}
function isSymmetricKey(value: unknown): value is SymmetricKey_t {
if (!Buffer.isBuffer(value)) return false;
if (value.length !== PBKDF2_HMAC_SHA256_KEY_DERIVATION.KEY_LENGTH_BYTES)
return false;
return true;
}
function isPassphraseEncryptionOutput(
value: unknown,
): value is PassphraseEncryptionOutput_t {
// The input should be a non-null object
if (!(value && typeof value === "object")) return false;
// The object should have these properties
if (!("salt" in value)) return false;
if (!("iv" in value)) return false;
if (!("tag" in value)) return false;
if (!("ciphertext" in value)) return false;
// The properties should be the correct type
if (!isSalt(value.salt)) return false;
if (!isIV(value.iv)) return false;
if (!isTag(value.tag)) return false;
if (!isCiphertext(value.ciphertext)) return false;
return true;
}
function isEncodedPassphraseEncryptionOutput(
value: unknown,
): value is EncodedPassphraseEncryptionOutput_t {
if (typeof value !== "string") return false;
const parts = value.split(".");
if (parts.length !== 4) return false;
const [salt, iv, tag, ciphertext] = parts;
if (!isSalt(Buffer.from(salt, "base64"))) return false;
if (!isIV(Buffer.from(iv, "base64"))) return false;
if (!isTag(Buffer.from(tag, "base64"))) return false;
if (!isCiphertext(Buffer.from(ciphertext, "base64"))) return false;
return true;
}
/*
* Type Conversion Functions
*/
function asPassphrase(value: unknown): Passphrase_t {
if (!isPassphrase(value)) {
throw new Error("Invalid passphrase.");
}
return value;
}
function asSalt(value: unknown): Salt_t {
if (!isSalt(value)) {
throw new Error("Invalid salt.");
}
return value;
}
function asTag(value: unknown): Tag_t {
if (!isTag(value)) {
throw new Error("Invalid tag.");
}
return value;
}
function asIV(value: unknown): IV_t {
if (!isIV(value)) {
throw new Error("Invalid IV.");
}
return value;
}
function asCiphertext(value: unknown): Ciphertext_t {
if (!isCiphertext(value)) {
throw new Error("Invalid ciphertext.");
}
return value;
}
function asPlaintext(value: unknown): Plaintext_t {
if (!isPlaintext(value)) {
throw new Error("Invalid plaintext.");
}
return value;
}
function asSymmetricKey(value: unknown): SymmetricKey_t {
if (!isSymmetricKey(value)) {
throw new Error("Invalid symmetric key.");
}
return value;
}
function asPassphraseEncryptionOutput(
value: unknown,
): PassphraseEncryptionOutput_t {
if (!isPassphraseEncryptionOutput(value)) {
throw new Error("Invalid encryption output.");
}
return value;
}
/**
* Validates and brands a given value as a properly-encoded encryption output.
*
* @param value the value to validate and brand
* @returns the branded value
*/
function asEncodedPassphraseEncryptionOutput(
value: unknown,
): EncodedPassphraseEncryptionOutput_t {
if (!isEncodedPassphraseEncryptionOutput(value)) {
throw new Error("Invalid encoded encryption output.");
}
return value;
}
/*
* Utility Functions for Generating Cryptographic Values
*/
function generateIV(): IV_t {
return asIV(crypto.randomBytes(AES_256_GCM_ENCRYPTION.IV_LENGTH_BYTES));
}
function generateSalt(): Salt_t {
return asSalt(
crypto.randomBytes(PBKDF2_HMAC_SHA256_KEY_DERIVATION.SALT_LENGTH_BYTES),
);
}
/*
* Core Cryptographic Functions
*/
function deriveKeyFromPassphrase(
passphrase: Passphrase_t,
salt: Salt_t,
): KeyDerivationOutput_t {
return {
key: asSymmetricKey(
crypto.pbkdf2Sync(
passphrase,
salt,
PBKDF2_HMAC_SHA256_KEY_DERIVATION.ITERATIONS,
PBKDF2_HMAC_SHA256_KEY_DERIVATION.KEY_LENGTH_BYTES,
PBKDF2_HMAC_SHA256_KEY_DERIVATION.DIGEST,
),
),
};
}
function encryptWithKey(
key: SymmetricKey_t,
plaintext: Plaintext_t,
): KeyEncryptionOutput_t {
const iv = generateIV();
const cipher = crypto.createCipheriv(
AES_256_GCM_ENCRYPTION.ALGORITHM,
key,
iv,
{
authTagLength: AES_256_GCM_ENCRYPTION.TAG_LENGTH_BYTES,
},
);
const ciphertext = asCiphertext(
Buffer.concat([cipher.update(plaintext), cipher.final()]),
);
const tag = asTag(cipher.getAuthTag());
return {
tag,
iv,
ciphertext,
};
}
function decryptWithKey(
key: SymmetricKey_t,
iv: IV_t,
tag: Tag_t,
ciphertext: Ciphertext_t,
): KeyDecryptionOutput_t {
const decipher = crypto.createDecipheriv(
AES_256_GCM_ENCRYPTION.ALGORITHM,
key,
iv,
{
authTagLength: tag.length,
},
);
decipher.setAuthTag(tag);
const plaintext = asPlaintext(
Buffer.concat([decipher.update(ciphertext), decipher.final()]),
);
return { plaintext };
}
function encryptWithPassphrase(
passphrase: Passphrase_t,
plaintext: Plaintext_t,
): PassphraseEncryptionOutput_t {
const salt = generateSalt();
const { key } = deriveKeyFromPassphrase(passphrase, salt);
const { tag, iv, ciphertext } = encryptWithKey(key, plaintext);
return { salt, iv, tag, ciphertext };
}
function decryptWithPassphrase(
passphrase: Passphrase_t,
salt: Salt_t,
iv: IV_t,
tag: Tag_t,
ciphertext: Ciphertext_t,
): PassphraseDecryptionOutput_t {
const { key } = deriveKeyFromPassphrase(passphrase, salt);
return decryptWithKey(key, iv, tag, ciphertext);
}
/*
* Encoding and Decoding Functions
*/
function encodeEncryptedToken(
salt: Salt_t,
iv: IV_t,
tag: Tag_t,
ciphertext: Ciphertext_t,
): string {
const encodedSalt = salt.toString("base64");
const encodedIV = iv.toString("base64");
const encodedTag = tag.toString("base64");
const encodedCiphertext = ciphertext.toString("base64");
return `${encodedSalt}.${encodedIV}.${encodedTag}.${encodedCiphertext}`;
}
function decodeEncryptedToken(
encodedToken: EncodedPassphraseEncryptionOutput_t,
): PassphraseEncryptionOutput_t {
const parts = encodedToken.split(".");
if (parts.length !== 4) {
throw new Error("Expected 4 components in encoded token.");
}
const [encodedSalt, encodedIV, encodedTag, encodedCiphertext] = parts;
const salt = asSalt(Buffer.from(encodedSalt, "base64"));
const iv = asIV(Buffer.from(encodedIV, "base64"));
const tag = asTag(Buffer.from(encodedTag, "base64"));
const ciphertext = asCiphertext(Buffer.from(encodedCiphertext, "base64"));
return { salt, iv, tag, ciphertext };
}
function encodeEncryptionOutput(
encryptionOutput: PassphraseEncryptionOutput_t,
): EncodedPassphraseEncryptionOutput_t {
return asEncodedPassphraseEncryptionOutput(
encodeEncryptedToken(
encryptionOutput.salt,
encryptionOutput.iv,
encryptionOutput.tag,
encryptionOutput.ciphertext,
),
);
}
function decodeEncryptionOutput(
encodedOutput: EncodedPassphraseEncryptionOutput_t,
): PassphraseEncryptionOutput_t {
return asPassphraseEncryptionOutput(decodeEncryptedToken(encodedOutput));
}
/*
* API for encrypting and decrypting OAuth tokens
*/
/**
* Encrypt the OAuth token using the embed secret.
*
* @param embedSecret the embed secret to use for encryption
* @param oauthToken the OAuth token to encrypt
* @returns the encrypted token, encoded as a string
*/
export function encrypt(embedSecret: string, oauthToken: string): string {
const passphrase = asPassphrase(Buffer.from(embedSecret, "utf8"));
const plaintext = asPlaintext(Buffer.from(oauthToken, "utf8"));
const encryptionOutput = encryptWithPassphrase(passphrase, plaintext);
return encodeEncryptionOutput(encryptionOutput);
}
/**
* Decrypt the OAuth token using the embed secret.
*
* @param embedSecret the embed secret to use for decryption
* @param encryptedToken the encrypted OAuth token to decrypt
* @returns the decrypted token
*/
export function decrypt(embedSecret: string, encryptedToken: string): string {
const passphrase = asPassphrase(Buffer.from(embedSecret, "utf8"));
const encryptionOutput = decodeEncryptionOutput(
asEncodedPassphraseEncryptionOutput(encryptedToken),
);
const { plaintext } = decryptWithPassphrase(
passphrase,
encryptionOutput.salt,
encryptionOutput.iv,
encryptionOutput.tag,
encryptionOutput.ciphertext,
);
return plaintext.toString("utf8");
}
export const _testExports = {
PBKDF2_HMAC_SHA256_KEY_DERIVATION,
AES_256_GCM_ENCRYPTION,
asEncodedPassphraseEncryptionOutput,
};