Skip to content

Commit af4906b

Browse files
committed
fix: update tests, benchmarks, and implementation coverage
1 parent 8ea6129 commit af4906b

8 files changed

Lines changed: 204 additions & 159 deletions

File tree

.docs/implementation-coverage.md

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ These algorithms provide quantum-resistant cryptography.
3434
*`decipher.setAuthTag(buffer[, encoding])`
3535
*`decipher.setAutoPadding([autoPadding])`
3636
*`decipher.update(data[, inputEncoding][, outputEncoding])`
37-
* Class: `DiffieHellman`
38-
* `diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])`
39-
* `diffieHellman.generateKeys([encoding])`
40-
* `diffieHellman.getGenerator([encoding])`
41-
* `diffieHellman.getPrime([encoding])`
42-
* `diffieHellman.getPrivateKey([encoding])`
43-
* `diffieHellman.getPublicKey([encoding])`
44-
* `diffieHellman.setPrivateKey(privateKey[, encoding])`
45-
* `diffieHellman.setPublicKey(publicKey[, encoding])`
37+
* Class: `DiffieHellman`
38+
* `diffieHellman.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])`
39+
* `diffieHellman.generateKeys([encoding])`
40+
* `diffieHellman.getGenerator([encoding])`
41+
* `diffieHellman.getPrime([encoding])`
42+
* `diffieHellman.getPrivateKey([encoding])`
43+
* `diffieHellman.getPublicKey([encoding])`
44+
* `diffieHellman.setPrivateKey(privateKey[, encoding])`
45+
* `diffieHellman.setPublicKey(publicKey[, encoding])`
4646
*`diffieHellman.verifyError`
47-
* Class: `DiffieHellmanGroup`
48-
* Class: `ECDH`
47+
* Class: `DiffieHellmanGroup`
48+
* Class: `ECDH`
4949
* ❌ static `ECDH.convertKey(key, curve[, inputEncoding[, outputEncoding[, format]]])`
50-
* `ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])`
51-
* `ecdh.generateKeys([encoding[, format]])`
52-
* `ecdh.getPrivateKey([encoding])`
53-
* `ecdh.getPublicKey([encoding][, format])`
54-
* `ecdh.setPrivateKey(privateKey[, encoding])`
55-
* `ecdh.setPublicKey(publicKey[, encoding])`
50+
* `ecdh.computeSecret(otherPublicKey[, inputEncoding][, outputEncoding])`
51+
* `ecdh.generateKeys([encoding[, format]])`
52+
* `ecdh.getPrivateKey([encoding])`
53+
* `ecdh.getPublicKey([encoding][, format])`
54+
* `ecdh.setPrivateKey(privateKey[, encoding])`
55+
* `ecdh.setPublicKey(publicKey[, encoding])`
5656
* ✅ Class: `Hash`
5757
*`hash.copy([options])`
5858
*`hash.digest([encoding])`
@@ -109,10 +109,10 @@ These algorithms provide quantum-resistant cryptography.
109109
*`crypto.constants`
110110
*`crypto.createCipheriv(algorithm, key, iv[, options])`
111111
*`crypto.createDecipheriv(algorithm, key, iv[, options])`
112-
* `crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])`
113-
* `crypto.createDiffieHellman(primeLength[, generator])`
114-
* `crypto.createDiffieHellmanGroup(name)`
115-
* `crypto.createECDH(curveName)`
112+
* `crypto.createDiffieHellman(prime[, primeEncoding][, generator][, generatorEncoding])`
113+
* `crypto.createDiffieHellman(primeLength[, generator])`
114+
* `crypto.getDiffieHellman(groupName)`
115+
* `crypto.createECDH(curveName)`
116116
*`crypto.createHash(algorithm[, options])`
117117
*`crypto.createHmac(algorithm, key[, options])`
118118
*`crypto.createPrivateKey(key)`
@@ -133,7 +133,6 @@ These algorithms provide quantum-resistant cryptography.
133133
*`crypto.getCipherInfo(nameOrNid[, options])`
134134
*`crypto.getCiphers()`
135135
*`crypto.getCurves()`
136-
*`crypto.getDiffieHellman(groupName)`
137136
*`crypto.getFips()`
138137
*`crypto.getHashes()`
139138
*`crypto.getRandomValues(typedArray)`
@@ -165,8 +164,8 @@ These algorithms provide quantum-resistant cryptography.
165164
## `crypto.diffieHellman`
166165
| type | Status |
167166
| --------- | :----: |
168-
| `dh` | |
169-
| `ec` | |
167+
| `dh` | |
168+
| `ec` | |
170169
| `x448` ||
171170
| `x25519` ||
172171

bun.lock

Lines changed: 118 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2773,7 +2773,7 @@ SPEC CHECKSUMS:
27732773
glog: 5683914934d5b6e4240e497e0f4a3b42d1854183
27742774
hermes-engine: 4f8246b1f6d79f625e0d99472d1f3a71da4d28ca
27752775
NitroModules: 1715fe0e22defd9e2cdd48fb5e0dbfd01af54bec
2776-
QuickCrypto: a90c6474ef5d724d14cc452d8d23146621e0fb99
2776+
QuickCrypto: d2f42ab176e55442e803586b4621d5c21e0f0f19
27772777
RCT-Folly: 846fda9475e61ec7bcbf8a3fe81edfcaeb090669
27782778
RCTDeprecation: c4b9e2fd0ab200e3af72b013ed6113187c607077
27792779
RCTRequired: e97dd5dafc1db8094e63bc5031e0371f092ae92a

example/ios/QuickCryptoExample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@
398398
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
399399
);
400400
OTHER_LDFLAGS = "$(inherited)";
401-
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
401+
REACT_NATIVE_PATH = "${PODS_ROOT}/../../../node_modules/react-native";
402402
SDKROOT = iphoneos;
403403
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
404404
SWIFT_ENABLE_EXPLICIT_MODULES = NO;

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"react-native-fast-encoder": "0.3.1",
4141
"react-native-nitro-modules": "0.29.1",
4242
"react-native-quick-base64": "2.2.2",
43-
"react-native-quick-crypto": "1.0.7",
43+
"react-native-quick-crypto": "workspace:*",
4444
"react-native-safe-area-context": "5.6.2",
4545
"react-native-screens": "4.18.0",
4646
"react-native-vector-icons": "10.3.0",

example/src/benchmarks/ecdh/ecdh.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ const ecdh_p256_genKeys: BenchFn = () => {
1717
ecdh.generateKeys();
1818
})
1919
.add('@noble/curves', () => {
20-
p256.utils.randomPrivateKey();
20+
// Generate private key and derive public key for fair comparison
21+
const priv = p256.utils.randomPrivateKey();
22+
p256.getPublicKey(priv);
2123
});
2224

2325
bench.warmupTime = 100;

example/src/tests/dh/dh_tests.ts

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,87 @@ import { assert } from 'chai';
55

66
const SUITE = 'dh';
77

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-
});
8+
// RFC 3526 MODP Group 14 prime (2048-bit) for testing with explicit prime
9+
const MODP14_PRIME =
10+
'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' +
11+
'29024E088A67CC74020BBEA63B139B22514A08798E3404DD' +
12+
'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' +
13+
'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' +
14+
'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D' +
15+
'C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F' +
16+
'83655D23DCA3AD961C62F356208552BB9ED529077096966D' +
17+
'670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B' +
18+
'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9' +
19+
'DE2BCBF6955817183995497CEA956AE515D2261898FA0510' +
20+
'15728E5A8AACAA68FFFFFFFFFFFFFFFF';
1521

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);
22+
test(
23+
SUITE,
24+
'should create DiffieHellman with prime and numeric generator',
25+
() => {
26+
const prime = Buffer.from(MODP14_PRIME, 'hex');
27+
const dh = crypto.createDiffieHellman(prime, 2);
2928

30-
assert.strictEqual(dh.getPrime('hex'), prime.toString('hex').toLowerCase());
31-
assert.strictEqual(
32-
dh.getGenerator('hex'),
33-
generator.toString('hex').toLowerCase(),
34-
);
35-
});
29+
assert.strictEqual(dh.getPrime('hex'), prime.toString('hex').toLowerCase());
30+
assert.strictEqual(dh.getGenerator('hex'), '02');
31+
},
32+
);
33+
34+
test(
35+
SUITE,
36+
'should create DiffieHellman with prime and Buffer generator',
37+
() => {
38+
const prime = Buffer.from(MODP14_PRIME, 'hex');
39+
const generator = Buffer.from([2]);
40+
const dh = crypto.createDiffieHellman(prime, generator);
41+
42+
assert.strictEqual(dh.getPrime('hex'), prime.toString('hex').toLowerCase());
43+
assert.strictEqual(
44+
dh.getGenerator('hex'),
45+
generator.toString('hex').toLowerCase(),
46+
);
47+
},
48+
);
3649

3750
test(SUITE, 'should compute shared secret', () => {
38-
const alice = crypto.createDiffieHellman(512);
39-
const aliceKeys = alice.generateKeys();
51+
const alice = crypto.getDiffieHellman('modp14');
52+
alice.generateKeys();
4053

41-
const bob = crypto.createDiffieHellman(
42-
alice.getPrime(),
43-
alice.getGenerator(),
44-
);
45-
const bobKeys = bob.generateKeys();
54+
const bob = crypto.getDiffieHellman('modp14');
55+
bob.generateKeys();
4656

47-
const aliceSecret = alice.computeSecret(bobKeys);
48-
const bobSecret = bob.computeSecret(aliceKeys);
57+
const aliceSecret = alice.computeSecret(bob.getPublicKey());
58+
const bobSecret = bob.computeSecret(alice.getPublicKey());
4959

5060
assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex'));
5161
});
5262

5363
test(SUITE, 'should set keys', () => {
54-
const alice = crypto.createDiffieHellman(512);
64+
const alice = crypto.getDiffieHellman('modp14');
5565
alice.generateKeys();
5666

57-
const dh2 = crypto.createDiffieHellman(
67+
const bob = crypto.createDiffieHellman(
5868
alice.getPrime(),
5969
alice.getGenerator(),
6070
);
61-
dh2.setPublicKey(alice.getPublicKey());
62-
dh2.setPrivateKey(alice.getPrivateKey());
71+
bob.setPublicKey(alice.getPublicKey());
72+
bob.setPrivateKey(alice.getPrivateKey());
6373

64-
assert.strictEqual(dh2.getPublicKey('hex'), alice.getPublicKey('hex'));
65-
assert.strictEqual(dh2.getPrivateKey('hex'), alice.getPrivateKey('hex'));
74+
assert.strictEqual(bob.getPublicKey('hex'), alice.getPublicKey('hex'));
75+
assert.strictEqual(bob.getPrivateKey('hex'), alice.getPrivateKey('hex'));
6676
});
6777

6878
test(SUITE, 'should create DiffieHellman from standard group', () => {
6979
const dh = crypto.getDiffieHellman('modp14');
7080
assert.isOk(dh);
7181
const prime = dh.getPrime();
7282
assert.isTrue(Buffer.isBuffer(prime));
73-
// modp14 is 2048-bit group
7483
assert.strictEqual(prime.length, 256);
7584
assert.strictEqual(dh.getGenerator('hex'), '02');
7685
});
86+
87+
test(SUITE, 'should reject prime length below 2048 bits', () => {
88+
assert.throws(() => {
89+
crypto.createDiffieHellman(512);
90+
}, /prime length must be at least 2048 bits/);
91+
});

example/src/tests/ecdh/ecdh_tests.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ test(SUITE, 'should set private key', () => {
5353
const alice2 = crypto.createECDH('prime256v1');
5454
alice2.setPrivateKey(priv);
5555

56-
// Public key should be derived/set (depending on impl, but usually settable)
57-
// In our implementation setPrivateKey derives public key?
58-
// Let's check consistency.
59-
// If setPrivateKey derives public key, we can check it matches
6056
const pub1 = alice.getPublicKey();
6157
const pub2 = alice2.getPublicKey();
6258

0 commit comments

Comments
 (0)