BlueGit

timfa filemanager

Files

filemanager / tests / crypto.test.js

crypto.test.js

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