-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathecdh_tests.ts
More file actions
155 lines (120 loc) · 4.61 KB
/
ecdh_tests.ts
File metadata and controls
155 lines (120 loc) · 4.61 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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, 'should set private key and compute secret for P-384', () => {
const alice = crypto.createECDH('secp384r1');
alice.generateKeys();
const priv = alice.getPrivateKey();
const alice2 = crypto.createECDH('secp384r1');
alice2.setPrivateKey(priv);
assert.strictEqual(
alice.getPublicKey().toString('hex'),
alice2.getPublicKey().toString('hex'),
);
const bob = crypto.createECDH('secp384r1');
bob.generateKeys();
const secret1 = alice.computeSecret(bob.getPublicKey());
const secret2 = alice2.computeSecret(bob.getPublicKey());
assert.strictEqual(secret1.toString('hex'), secret2.toString('hex'));
});
test(SUITE, 'should set private key and compute secret for P-521', () => {
const alice = crypto.createECDH('secp521r1');
alice.generateKeys();
const priv = alice.getPrivateKey();
const alice2 = crypto.createECDH('secp521r1');
alice2.setPrivateKey(priv);
assert.strictEqual(
alice.getPublicKey().toString('hex'),
alice2.getPublicKey().toString('hex'),
);
const bob = crypto.createECDH('secp521r1');
bob.generateKeys();
const secret1 = alice.computeSecret(bob.getPublicKey());
const secret2 = alice2.computeSecret(bob.getPublicKey());
assert.strictEqual(secret1.toString('hex'), secret2.toString('hex'));
});
test(SUITE, 'should set private key and compute secret for secp256k1', () => {
const alice = crypto.createECDH('secp256k1');
alice.generateKeys();
const priv = alice.getPrivateKey();
const alice2 = crypto.createECDH('secp256k1');
alice2.setPrivateKey(priv);
assert.strictEqual(
alice.getPublicKey().toString('hex'),
alice2.getPublicKey().toString('hex'),
);
const bob = crypto.createECDH('secp256k1');
bob.generateKeys();
const secret1 = alice.computeSecret(bob.getPublicKey());
const secret2 = alice2.computeSecret(bob.getPublicKey());
assert.strictEqual(secret1.toString('hex'), secret2.toString('hex'));
});
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);
});