-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathecdh_tests.ts
More file actions
92 lines (72 loc) · 2.72 KB
/
Copy pathecdh_tests.ts
File metadata and controls
92 lines (72 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { test } from '../util';
import crypto, { Buffer, getCurves } from 'react-native-quick-crypto';
import { assert } from 'chai';
const SUITE = 'ecdh';
test(SUITE, 'should create ECDH instance with P-256', () => {
const ecdh = crypto.createECDH('prime256v1');
assert.isOk(ecdh);
});
test(SUITE, 'should generate keys for P-256', () => {
const ecdh = crypto.createECDH('prime256v1');
const keys = ecdh.generateKeys();
assert.isOk(keys);
assert.isTrue(Buffer.isBuffer(keys), 'keys should be a Buffer');
assert.isOk(ecdh.getPublicKey());
assert.isOk(ecdh.getPrivateKey());
});
test(SUITE, 'should switch between curves', () => {
const ecdh1 = crypto.createECDH('prime256v1');
ecdh1.generateKeys();
const ecdh2 = crypto.createECDH('secp384r1');
ecdh2.generateKeys();
assert.notEqual(
ecdh1.getPrivateKey().toString('hex'),
ecdh2.getPrivateKey().toString('hex'),
);
});
test(SUITE, 'should compute shared secret', () => {
const alice = crypto.createECDH('prime256v1');
alice.generateKeys();
const bob = crypto.createECDH('prime256v1');
bob.generateKeys();
const aliceSecret = alice.computeSecret(bob.getPublicKey());
const bobSecret = bob.computeSecret(alice.getPublicKey());
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
});
test(SUITE, 'should set private key', () => {
const alice = crypto.createECDH('prime256v1');
alice.generateKeys();
const priv = alice.getPrivateKey();
const alice2 = crypto.createECDH('prime256v1');
alice2.setPrivateKey(priv);
const pub1 = alice.getPublicKey();
const pub2 = alice2.getPublicKey();
assert.strictEqual(pub1.toString('hex'), pub2.toString('hex'));
});
test(SUITE, 'should work with string input', () => {
const alice = crypto.createECDH('prime256v1');
alice.generateKeys();
const bob = crypto.createECDH('prime256v1');
bob.generateKeys();
const bobPubHex = bob.getPublicKey().toString('hex');
const secret = alice.computeSecret(bobPubHex, 'hex');
assert.isOk(secret);
});
test(SUITE, 'getCurves - should return array of supported curves', () => {
const curves = getCurves();
assert.isArray(curves);
assert.isAbove(curves.length, 0, 'should have at least one curve');
const expectedCurves = ['prime256v1', 'secp384r1', 'secp521r1', 'secp256k1'];
for (const curve of expectedCurves) {
assert.include(curves, curve, `should include ${curve}`);
}
const isSorted = curves.every(
(val: string, i: number) => i === 0 || val >= curves[i - 1]!,
);
assert.isTrue(isSorted, 'curves should be sorted alphabetically');
});
test(SUITE, 'getCurves - should match crypto.getCurves()', () => {
const named = getCurves();
const fromDefault = crypto.getCurves();
assert.deepEqual(named, fromDefault);
});