|
| 1 | +import { test } from '../util'; |
| 2 | +import { Buffer } from '@craftzdog/react-native-buffer'; |
| 3 | +import crypto from 'react-native-quick-crypto'; |
| 4 | +import { assert } from 'chai'; |
| 5 | + |
| 6 | +const SUITE = 'dh'; |
| 7 | + |
| 8 | +test(SUITE, 'should create DiffieHellman with size', () => { |
| 9 | + const dh = crypto.createDiffieHellman(512); |
| 10 | + const prime = dh.getPrime(); |
| 11 | + assert.isOk(prime); |
| 12 | + // Size check approx |
| 13 | + assert.isAtLeast(prime.length, 64); |
| 14 | +}); |
| 15 | + |
| 16 | +test(SUITE, 'should create DiffieHellman with prime', () => { |
| 17 | + // 512-bit prime (Group 1 from RFC 2409) |
| 18 | + const prime = Buffer.from( |
| 19 | + 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + |
| 20 | + '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + |
| 21 | + 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + |
| 22 | + 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + |
| 23 | + 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381' + |
| 24 | + 'FFFFFFFFFFFFFFFF', |
| 25 | + 'hex', |
| 26 | + ); |
| 27 | + const generator = Buffer.from([2]); |
| 28 | + const dh = crypto.createDiffieHellman(prime, generator); |
| 29 | + |
| 30 | + assert.strictEqual(dh.getPrime('hex'), prime.toString('hex').toLowerCase()); |
| 31 | + assert.strictEqual( |
| 32 | + dh.getGenerator('hex'), |
| 33 | + generator.toString('hex').toLowerCase(), |
| 34 | + ); |
| 35 | +}); |
| 36 | + |
| 37 | +test(SUITE, 'should compute shared secret', () => { |
| 38 | + const alice = crypto.createDiffieHellman(512); |
| 39 | + const aliceKeys = alice.generateKeys(); |
| 40 | + |
| 41 | + const bob = crypto.createDiffieHellman( |
| 42 | + alice.getPrime(), |
| 43 | + alice.getGenerator(), |
| 44 | + ); |
| 45 | + const bobKeys = bob.generateKeys(); |
| 46 | + |
| 47 | + const aliceSecret = alice.computeSecret(bobKeys); |
| 48 | + const bobSecret = bob.computeSecret(aliceKeys); |
| 49 | + |
| 50 | + assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex')); |
| 51 | +}); |
| 52 | + |
| 53 | +test(SUITE, 'should set keys', () => { |
| 54 | + const alice = crypto.createDiffieHellman(512); |
| 55 | + alice.generateKeys(); |
| 56 | + |
| 57 | + const dh2 = crypto.createDiffieHellman( |
| 58 | + alice.getPrime(), |
| 59 | + alice.getGenerator(), |
| 60 | + ); |
| 61 | + dh2.setPublicKey(alice.getPublicKey()); |
| 62 | + dh2.setPrivateKey(alice.getPrivateKey()); |
| 63 | + |
| 64 | + assert.strictEqual(dh2.getPublicKey('hex'), alice.getPublicKey('hex')); |
| 65 | + assert.strictEqual(dh2.getPrivateKey('hex'), alice.getPrivateKey('hex')); |
| 66 | +}); |
| 67 | + |
| 68 | +test(SUITE, 'should create DiffieHellman from standard group', () => { |
| 69 | + const dh = crypto.getDiffieHellman('modp14'); |
| 70 | + assert.isOk(dh); |
| 71 | + const prime = dh.getPrime(); |
| 72 | + assert.isTrue(Buffer.isBuffer(prime)); |
| 73 | + // modp14 is 2048-bit group |
| 74 | + assert.strictEqual(prime.length, 256); |
| 75 | + assert.strictEqual(dh.getGenerator('hex'), '02'); |
| 76 | +}); |
0 commit comments