BlueGit

timfa filemanager

Files

filemanager / tests / crypto.test.js

crypto.test.js

formatting
timfa Tim Falken · pushed by timfa · 6 months ago
68827177 History
crypto.test.js 3.7 KiB Download Raw
1'use strict';
2
3/**
4 * Unit tests for server/crypto.js
5 * Verifies the hybrid encrypt/decrypt round-trip.
6 */
7
8const { ensureServerKeys, hybridDecrypt, hybridEncryptForClient, getServerPublicKeyJwk } = require('../server/crypto');
9
10beforeAll(() =>
11{
12 ensureServerKeys();
13});
14
15describe('hybrid encrypt / decrypt round-trip', () =>
16{
17 test('encrypting and decrypting a string returns the original', () =>
18 {
19 const plaintext = JSON.stringify({ username: 'alice', password: 'hunter22' });
20
21 // Get the server public key and create a temporary Node.js key to simulate
22 // what the client would do — encrypt with server pub, decrypt with server priv
23 const crypto = require('crypto');
24 const jwk = getServerPublicKeyJwk();
25 const pubKey = crypto.createPublicKey({ key: jwk, format: 'jwk' });
26
27 // Simulate client-side hybrid encrypt targeting the server
28 const aesKey = crypto.randomBytes(32);
29 const iv = crypto.randomBytes(12);
30 const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, iv);
31 const ciphertext = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]);
32 const tag = cipher.getAuthTag();
33 const encryptedKey = crypto.publicEncrypt(
34 { key: pubKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' },
35 aesKey,
36 );
37
38 const payload = {
39 encryptedKey: encryptedKey.toString('base64'),
40 iv: iv.toString('base64'),
41 ciphertext: ciphertext.toString('base64'),
42 tag: tag.toString('base64'),
43 };
44
45 const result = hybridDecrypt(payload);
46 expect(result).toBe(plaintext);
47 });
48
49 test('hybridEncryptForClient round-trip using Node crypto', () =>
50 {
51 const crypto = require('crypto');
52 const plaintext = 'test-token-value';
53
54 // Generate a throwaway RSA key pair to act as the "client"
55 const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
56 modulusLength: 2048,
57 publicKeyEncoding: { type: 'spki', format: 'pem' },
58 privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
59 });
60
61 const clientPubJwk = crypto.createPublicKey(publicKey).export({ format: 'jwk' });
62 const payload = hybridEncryptForClient(plaintext, clientPubJwk);
63
64 // Decrypt using the client private key
65 const encryptedKey = Buffer.from(payload.encryptedKey, 'base64');
66 const iv = Buffer.from(payload.iv, 'base64');
67 const ciphertext = Buffer.from(payload.ciphertext, 'base64');
68 const tag = Buffer.from(payload.tag, 'base64');
69
70 const rawAes = crypto.privateDecrypt(
71 { key: privateKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' },
72 encryptedKey,
73 );
74 const decipher = crypto.createDecipheriv('aes-256-gcm', rawAes, iv);
75 decipher.setAuthTag(tag);
76 const decrypted = Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8');
77
78 expect(decrypted).toBe(plaintext);
79 });
80
81 test('hybridDecrypt throws on tampered ciphertext', () =>
82 {
83 const crypto = require('crypto');
84 const jwk = getServerPublicKeyJwk();
85 const pubKey = crypto.createPublicKey({ key: jwk, format: 'jwk' });
86 const aesKey = crypto.randomBytes(32);
87 const iv = crypto.randomBytes(12);
88 const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, iv);
89 const ct = Buffer.concat([cipher.update('hello', 'utf8'), cipher.final()]);
90 const tag = cipher.getAuthTag();
91 const ek = crypto.publicEncrypt(
92 { key: pubKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' },
93 aesKey,
94 );
95
96 // Flip a byte in the ciphertext
97 ct[0] ^= 0xff;
98
99 const payload = {
100 encryptedKey: ek.toString('base64'),
101 iv: iv.toString('base64'),
102 ciphertext: ct.toString('base64'),
103 tag: tag.toString('base64'),
104 };
105
106 expect(() => hybridDecrypt(payload)).toThrow();
107 });
108});