|
| 1 | +import * as sjcl from '@bitgo/sjcl'; |
| 2 | +import assert from 'assert'; |
| 3 | + |
| 4 | +import { decryptV1WithCrypto, V1_MAX_ITER } from '../../src'; |
| 5 | +import { KEYCARD_BOX_A, KEYCARD_BOX_B, KEYCARD_PASSWORD, KEYCARD_PLAINTEXT_PREFIX } from './fixtures/keycard'; |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 7 | +const browserCrypto = require('crypto-browserify'); |
| 8 | + |
| 9 | +/** |
| 10 | + * sjcl.encrypt's typings require salt/iv, but the runtime picks them from |
| 11 | + * sjcl.random when omitted. Feed real random words so the call type-checks |
| 12 | + * without an `as` cast. |
| 13 | + */ |
| 14 | +function sjclEncrypt(password: string, plaintext: string, params: sjcl.SjclCipherParams): string { |
| 15 | + const salt = sjcl.random.randomWords(2); |
| 16 | + const iv = sjcl.random.randomWords(4); |
| 17 | + return sjcl.encrypt(password, plaintext, { ...params, salt, iv }); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Exercises the real `decryptV1WithCrypto` code path with `crypto-browserify` |
| 22 | + * injected as the crypto module. This is exactly what webpack bundles for the |
| 23 | + * browser (its `crypto` shim), so a green test here proves the browser build |
| 24 | + * stays byte-compatible with SJCL-produced envelopes and the Node path. |
| 25 | + */ |
| 26 | +function decryptV1Browser(password: string, ciphertext: string): Promise<string> { |
| 27 | + return decryptV1WithCrypto(password, ciphertext, browserCrypto); |
| 28 | +} |
| 29 | + |
| 30 | +describe('decryptV1 browser path (crypto-browserify shim)', () => { |
| 31 | + const password = 'myPassword'; |
| 32 | + const plaintext = 'Hello, Browser!'; |
| 33 | + |
| 34 | + it('produces the same plaintext as sjcl.decrypt', async () => { |
| 35 | + const ciphertext = sjclEncrypt(password, plaintext, { iter: 10000, ks: 256, ts: 64, mode: 'ccm' }); |
| 36 | + assert.strictEqual(await decryptV1Browser(password, ciphertext), sjcl.decrypt(password, ciphertext)); |
| 37 | + }); |
| 38 | + |
| 39 | + it('handles adata (AAD)', async () => { |
| 40 | + const ciphertext = sjclEncrypt(password, plaintext, { |
| 41 | + iter: 10000, |
| 42 | + ks: 256, |
| 43 | + ts: 64, |
| 44 | + mode: 'ccm', |
| 45 | + adata: 'ctx-A', |
| 46 | + }); |
| 47 | + assert.strictEqual(await decryptV1Browser(password, ciphertext), plaintext); |
| 48 | + }); |
| 49 | + |
| 50 | + it('handles UTF-8 password + plaintext', async () => { |
| 51 | + const utf8Password = 'pässwörd中文🔐'; |
| 52 | + const utf8Plaintext = 'passphrase: 秘密キー ☃🔑'; |
| 53 | + const ciphertext = sjclEncrypt(utf8Password, utf8Plaintext, { iter: 10000, ks: 256, ts: 64, mode: 'ccm' }); |
| 54 | + assert.strictEqual(await decryptV1Browser(utf8Password, ciphertext), utf8Plaintext); |
| 55 | + }); |
| 56 | + |
| 57 | + it('handles large plaintext (>64 KiB, forces L=3 nonce framing)', async () => { |
| 58 | + const large = 'x'.repeat(70_000); |
| 59 | + const ciphertext = sjclEncrypt(password, large, { iter: 1000, ks: 256, ts: 64, mode: 'ccm' }); |
| 60 | + assert.strictEqual(await decryptV1Browser(password, ciphertext), large); |
| 61 | + }); |
| 62 | + |
| 63 | + it('handles aes-128 envelopes', async () => { |
| 64 | + const ciphertext = sjclEncrypt(password, plaintext, { iter: 10000, ks: 128, ts: 64, mode: 'ccm' }); |
| 65 | + assert.strictEqual(await decryptV1Browser(password, ciphertext), plaintext); |
| 66 | + }); |
| 67 | + |
| 68 | + it('handles 128-bit tag envelopes', async () => { |
| 69 | + const ciphertext = sjclEncrypt(password, plaintext, { iter: 10000, ks: 256, ts: 128, mode: 'ccm' }); |
| 70 | + assert.strictEqual(await decryptV1Browser(password, ciphertext), plaintext); |
| 71 | + }); |
| 72 | + |
| 73 | + it('rejects wrong password', async () => { |
| 74 | + const ciphertext = sjclEncrypt(password, plaintext, { iter: 10000, ks: 256, ts: 64, mode: 'ccm' }); |
| 75 | + await assert.rejects(() => decryptV1Browser('wrongPassword', ciphertext)); |
| 76 | + }); |
| 77 | + |
| 78 | + it('rejects envelope with iter above cap before running PBKDF2', async () => { |
| 79 | + const ciphertext = sjclEncrypt(password, plaintext, { iter: 10000, ks: 256, ts: 64, mode: 'ccm' }); |
| 80 | + const envelope = JSON.parse(ciphertext); |
| 81 | + envelope.iter = V1_MAX_ITER + 1; |
| 82 | + const start = Date.now(); |
| 83 | + await assert.rejects(() => decryptV1Browser(password, JSON.stringify(envelope)), /iter/); |
| 84 | + assert.ok(Date.now() - start < 100, 'must reject before any KDF work'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('parity across 50 randomised inputs', async () => { |
| 88 | + const { randomBytes } = await import('crypto'); |
| 89 | + for (let i = 0; i < 50; i++) { |
| 90 | + const pw = randomBytes(16).toString('hex'); |
| 91 | + const pt = randomBytes(1 + Math.floor(Math.random() * 500)).toString('base64'); |
| 92 | + const ciphertext = sjclEncrypt(pw, pt, { iter: 1000, ks: 256, ts: 64, mode: 'ccm' }); |
| 93 | + assert.strictEqual(await decryptV1Browser(pw, ciphertext), pt, `iteration ${i}`); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it('Box A + Box B: shim decrypt matches SJCL byte-for-byte', async () => { |
| 98 | + for (const [label, ct] of [ |
| 99 | + ['A', KEYCARD_BOX_A], |
| 100 | + ['B', KEYCARD_BOX_B], |
| 101 | + ] as const) { |
| 102 | + const sjclResult = sjcl.decrypt(KEYCARD_PASSWORD, ct); |
| 103 | + const shimResult = await decryptV1Browser(KEYCARD_PASSWORD, ct); |
| 104 | + assert.strictEqual(shimResult, sjclResult, `Box ${label} mismatch`); |
| 105 | + assert.ok(shimResult.startsWith(KEYCARD_PLAINTEXT_PREFIX)); |
| 106 | + } |
| 107 | + }); |
| 108 | +}); |
0 commit comments