'use strict'; /** * Unit tests for server/crypto.js * Verifies the hybrid encrypt/decrypt round-trip. */ const { ensureServerKeys, hybridDecrypt, hybridEncryptForClient, getServerPublicKeyJwk } = require('../server/crypto'); beforeAll(() => { ensureServerKeys(); }); describe('hybrid encrypt / decrypt round-trip', () => { test('encrypting and decrypting a string returns the original', () => { const plaintext = JSON.stringify({ username: 'alice', password: 'hunter22' }); // Get the server public key and create a temporary Node.js key to simulate // what the client would do — encrypt with server pub, decrypt with server priv const crypto = require('crypto'); const jwk = getServerPublicKeyJwk(); const pubKey = crypto.createPublicKey({ key: jwk, format: 'jwk' }); // Simulate client-side hybrid encrypt targeting the server const aesKey = crypto.randomBytes(32); const iv = crypto.randomBytes(12); const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, iv); const ciphertext = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()]); const tag = cipher.getAuthTag(); const encryptedKey = crypto.publicEncrypt( { key: pubKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, aesKey, ); const payload = { encryptedKey: encryptedKey.toString('base64'), iv: iv.toString('base64'), ciphertext: ciphertext.toString('base64'), tag: tag.toString('base64'), }; const result = hybridDecrypt(payload); expect(result).toBe(plaintext); }); test('hybridEncryptForClient round-trip using Node crypto', () => { const crypto = require('crypto'); const plaintext = 'test-token-value'; // Generate a throwaway RSA key pair to act as the "client" const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem' }, }); const clientPubJwk = crypto.createPublicKey(publicKey).export({ format: 'jwk' }); const payload = hybridEncryptForClient(plaintext, clientPubJwk); // Decrypt using the client private key const encryptedKey = Buffer.from(payload.encryptedKey, 'base64'); const iv = Buffer.from(payload.iv, 'base64'); const ciphertext = Buffer.from(payload.ciphertext, 'base64'); const tag = Buffer.from(payload.tag, 'base64'); const rawAes = crypto.privateDecrypt( { key: privateKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, encryptedKey, ); const decipher = crypto.createDecipheriv('aes-256-gcm', rawAes, iv); decipher.setAuthTag(tag); const decrypted = Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('utf8'); expect(decrypted).toBe(plaintext); }); test('hybridDecrypt throws on tampered ciphertext', () => { const crypto = require('crypto'); const jwk = getServerPublicKeyJwk(); const pubKey = crypto.createPublicKey({ key: jwk, format: 'jwk' }); const aesKey = crypto.randomBytes(32); const iv = crypto.randomBytes(12); const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, iv); const ct = Buffer.concat([cipher.update('hello', 'utf8'), cipher.final()]); const tag = cipher.getAuthTag(); const ek = crypto.publicEncrypt( { key: pubKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, aesKey, ); // Flip a byte in the ciphertext ct[0] ^= 0xff; const payload = { encryptedKey: ek.toString('base64'), iv: iv.toString('base64'), ciphertext: ct.toString('base64'), tag: tag.toString('base64'), }; expect(() => hybridDecrypt(payload)).toThrow(); }); });