Skip to content

Commit f373405

Browse files
authored
fix: ed25519 verify uses public key, not private (#690)
1 parent 83d2aed commit f373405

15 files changed

Lines changed: 100 additions & 50 deletions

File tree

.github/dependabot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ updates:
1717
- "@react-native-community/cli-platform-android"
1818
- "@react-native-community/cli-platform-ios"
1919
- "@types/react"
20+
- "chai"
21+
- "@types/chai"
2022
- package-ecosystem: "bun"
2123
target-branch: "0.x"
2224
directory: "/"

.rules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Every time you choose to apply a rule(s), explicitly state the rule(s) in the ou
2727
- Use modern C++ features.
2828
- Attempt to reduce the amount of code rather than add more.
2929
- Prefer iteration and modularization over code duplication.
30+
- Do not add comments unless explicitly told to do so.
3031

3132
## TypeScript Best Practices
3233

bun.lock

Lines changed: 17 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
786 Bytes
Loading

docs/test_suite_results_ios.png

2.52 KB
Loading

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@react-navigation/native": "6.1.18",
2828
"@react-navigation/native-stack": "6.11.0",
2929
"buffer": "6.0.3",
30-
"chai": "5.2.0",
30+
"chai": "<5.0.0",
3131
"crypto-browserify": "^3.12.0",
3232
"event-target-polyfill": "^0.0.4",
3333
"events": "3.3.0",
@@ -58,7 +58,7 @@
5858
"@react-native/metro-config": "0.76.1",
5959
"@react-native/typescript-config": "0.76.1",
6060
"@tsconfig/react-native": "^3.0.5",
61-
"@types/chai": "5.2.1",
61+
"@types/chai": "<5.0.0",
6262
"@types/react": "18.3.3",
6363
"@types/react-native-vector-icons": "^6.4.18",
6464
"@types/react-test-renderer": "18.3.0",

example/src/tests/ed25519/ed25519_tests.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unused-expressions */
22
import { Ed, randomBytes, ab2str } from 'react-native-quick-crypto';
33
import { Buffer } from '@craftzdog/react-native-buffer';
4-
// import type {
5-
// // KeyObject,
6-
// // CFRGKeyPairType,
7-
// // GenerateKeyPairCallback,
8-
// GenerateKeyPairOptions,
9-
// // KeyPairKey,
10-
// } from 'react-native-quick-crypto';
114
import { expect } from 'chai';
125
import { test } from '../util';
136

@@ -90,20 +83,39 @@ test(SUITE, 'sign/verify - switched args does not verify', async () => {
9083
});
9184

9285
test(SUITE, 'sign/verify - non-internally generated private key', async () => {
86+
const pub = Buffer.from(
87+
'e106bf015ad54a64022295c7af2c35f9511eb37264a7722a9642eaac6c59a494',
88+
'hex',
89+
);
90+
const priv = Buffer.from(
91+
'5f27e170afc5091c4933d980c5fe86af997b91375115c6ee2c0fe4ea12400ed0',
92+
'hex',
93+
);
94+
95+
const ed2 = new Ed('ed25519', {});
96+
const signature = await ed2.sign(data1.buffer, priv);
97+
const verified = await ed2.verify(signature, data1.buffer, pub);
98+
expect(verified).to.be.true;
99+
});
100+
101+
test(SUITE, 'sign/verify - bad signature', async () => {
93102
let ed1: Ed | null = new Ed('ed25519', {});
94103
await ed1.generateKeyPair();
104+
const pub = ed1.getPublicKey();
95105
const priv = ed1.getPrivateKey();
96106
ed1 = null;
97107

98108
const ed2 = new Ed('ed25519', {});
99109
const signature = await ed2.sign(data1.buffer, priv);
100-
const verified = await ed2.verify(signature, data1.buffer, priv);
101-
expect(verified).to.be.true;
110+
const signature2 = randomBytes(64).buffer;
111+
expect(ab2str(signature2)).not.to.equal(ab2str(signature));
112+
const verified = await ed2.verify(signature2, data1.buffer, pub);
113+
expect(verified).to.be.false;
102114
});
103115

104116
test(
105117
SUITE,
106-
'sign/verify - bad non-internally generated private key',
118+
'sign/verify - bad verify with private key, not public',
107119
async () => {
108120
let ed1: Ed | null = new Ed('ed25519', {});
109121
await ed1.generateKeyPair();
@@ -112,9 +124,7 @@ test(
112124

113125
const ed2 = new Ed('ed25519', {});
114126
const signature = await ed2.sign(data1.buffer, priv);
115-
const signature2 = randomBytes(64).buffer;
116-
expect(ab2str(signature2)).not.to.equal(ab2str(signature));
117-
const verified = await ed2.verify(signature2, data1.buffer, priv);
127+
const verified = await ed2.verify(signature, data1.buffer, priv);
118128
expect(verified).to.be.false;
119129
},
120130
);
@@ -124,10 +134,11 @@ test(SUITE, 'sign/verify - Uint8Arrays', () => {
124134

125135
const ed1 = new Ed('ed25519', {});
126136
ed1.generateKeyPairSync();
137+
const pub = new Uint8Array(ed1.getPublicKey());
127138
const priv = new Uint8Array(ed1.getPrivateKey());
128139

129140
const ed2 = new Ed('ed25519', {});
130141
const signature = new Uint8Array(ed2.signSync(encode(data), priv));
131-
const verified = ed2.verifySync(signature, encode(data), priv);
142+
const verified = ed2.verifySync(signature, encode(data), pub);
132143
expect(verified).to.be.true;
133144
});

example/src/tests/pbkdf2/pbkdf2_tests.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import { test } from '../util';
55
import { fixtures, type Fixture } from './fixtures';
66

7-
import crypto, { ab2str } from 'react-native-quick-crypto';
7+
import crypto from 'react-native-quick-crypto';
88
import type { BinaryLike, HashAlgorithm } from 'react-native-quick-crypto';
99

1010
type TestFixture = [string, string, number, number, string];
@@ -36,7 +36,7 @@ const SUITE = 'pbkdf2';
3636
function (err, result) {
3737
expect(err).to.be.null;
3838
expect(result).not.to.be.null;
39-
expect(ab2str(result as ArrayBuffer)).to.equal(expected);
39+
expect(result?.toString('hex')).to.equal(expected);
4040
},
4141
);
4242
};
@@ -76,7 +76,7 @@ const SUITE = 'pbkdf2';
7676

7777
test(SUITE, 'handles buffers', () => {
7878
const resultSync = crypto.pbkdf2Sync('password', 'salt', 1, 32);
79-
expect(ab2str(resultSync)).to.equal(
79+
expect(resultSync?.toString('hex')).to.equal(
8080
'0c60c80f961f0e71f3a9b524af6012062fe037a6e0f0eb94fe8fc46bdc637164',
8181
);
8282

@@ -186,7 +186,7 @@ algos.forEach(function (algorithm) {
186186
function (err, result) {
187187
expect(err).to.be.null;
188188
expect(result).not.to.be.null;
189-
expect(ab2str(result as ArrayBuffer)).to.equal(expected);
189+
expect(result?.toString('hex')).to.equal(expected);
190190
},
191191
);
192192
});
@@ -199,7 +199,7 @@ algos.forEach(function (algorithm) {
199199
f.dkLen as number,
200200
algorithm as HashAlgorithm,
201201
);
202-
expect(ab2str(result)).to.equal(expected);
202+
expect(result?.toString('hex')).to.equal(expected);
203203
});
204204
});
205205

example/tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
"index.ts",
88
"app.json",
99
"src",
10-
"**.*.ts",
11-
"**.*.tsx",
10+
"./**/*.ts",
11+
"./**/*.tsx",
12+
"../packages/react-native-quick-crypto/src/**/*.ts"
1213
],
1314
"compilerOptions": {
1415
"jsx": "react",

0 commit comments

Comments
 (0)