-
Notifications
You must be signed in to change notification settings - Fork 111
feat: Add DH and ECDH support #862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
athexweb3
wants to merge
12
commits into
margelo:main
Choose a base branch
from
athexweb3:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
479ac22
feat: Add DH and ECDH support
athexweb3 4c4d9fb
docs: Fix Expo logo in README to support light/dark mode
athexweb3 650321b
docs: implement proper documentation
athexweb3 428b2a8
docs: add missing required files for README
athexweb3 da727cb
docs: address PR review feedback
athexweb3 536d9a3
docs: address additional PR feedback
athexweb3 886236b
docs: address additional PR feedback
athexweb3 3c9a3c2
docs: remove duplicate images
athexweb3 1ad1dcf
docs: enhance contribution and documentation guides
athexweb3 bec19a1
docs: fix layout issues
athexweb3 adf7990
chore: ignore all docs build artifacts
athexweb3 80bc890
Merge branch 'docs'
athexweb3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import rnqc from 'react-native-quick-crypto'; | ||
| // @ts-expect-error crypto-browserify missing types | ||
| import browserify from 'crypto-browserify'; | ||
| import type { BenchFn } from '../../types/benchmarks'; | ||
| import { Bench } from 'tinybench'; | ||
|
|
||
| const dh_modp14_genKeys: BenchFn = () => { | ||
| const bench = new Bench({ | ||
| name: 'DH modp14 KeyGen', | ||
| time: 200, | ||
| iterations: 10, // Cap iterations for slow JS implementations | ||
| }); | ||
|
|
||
| bench | ||
| .add('rnqc', () => { | ||
| const dh = rnqc.getDiffieHellman('modp14'); | ||
| dh.generateKeys(); | ||
| }) | ||
| .add('browserify', () => { | ||
| const dh = browserify.getDiffieHellman('modp14'); | ||
| dh.generateKeys(); | ||
| }); | ||
|
|
||
| bench.warmupTime = 100; | ||
| return bench; | ||
| }; | ||
|
|
||
| const dh_modp14_computeSecret: BenchFn = () => { | ||
| const bench = new Bench({ | ||
| name: 'DH modp14 Compute', | ||
| time: 200, | ||
| iterations: 10, | ||
| }); | ||
|
|
||
| const alice = rnqc.getDiffieHellman('modp14'); | ||
| alice.generateKeys(); | ||
| const bob = rnqc.getDiffieHellman('modp14'); | ||
| bob.generateKeys(); | ||
| const bobPub = bob.getPublicKey(); | ||
|
|
||
| const bAlice = browserify.getDiffieHellman('modp14'); | ||
| bAlice.generateKeys(); | ||
| const bBob = browserify.getDiffieHellman('modp14'); | ||
| bBob.generateKeys(); | ||
| const bBobPub = bBob.getPublicKey(); | ||
|
|
||
| bench | ||
| .add('rnqc', () => { | ||
| alice.computeSecret(bobPub); | ||
| }) | ||
| .add('browserify', () => { | ||
| bAlice.computeSecret(bBobPub); | ||
| }); | ||
|
|
||
| bench.warmupTime = 100; | ||
| return bench; | ||
| }; | ||
|
|
||
| export default [dh_modp14_genKeys, dh_modp14_computeSecret]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import rnqc from 'react-native-quick-crypto'; | ||
| import { p256 } from '@noble/curves/p256'; | ||
| import type { BenchFn } from '../../types/benchmarks'; | ||
| import { Bench } from 'tinybench'; | ||
|
|
||
| const TIME_MS = 1000; | ||
|
|
||
| const ecdh_p256_genKeys: BenchFn = () => { | ||
| const bench = new Bench({ | ||
| name: 'ECDH P-256 KeyGen', | ||
| time: TIME_MS, | ||
| }); | ||
|
|
||
| bench | ||
| .add('rnqc', () => { | ||
| const ecdh = rnqc.createECDH('prime256v1'); | ||
| ecdh.generateKeys(); | ||
| }) | ||
| .add('@noble/curves', () => { | ||
| p256.utils.randomPrivateKey(); | ||
| }); | ||
|
|
||
| bench.warmupTime = 100; | ||
| return bench; | ||
| }; | ||
|
|
||
| const ecdh_p256_computeSecret: BenchFn = () => { | ||
| const bench = new Bench({ | ||
| name: 'ECDH P-256 Compute', | ||
| time: TIME_MS, | ||
| }); | ||
|
|
||
| const alice = rnqc.createECDH('prime256v1'); | ||
| alice.generateKeys(); | ||
| const bob = rnqc.createECDH('prime256v1'); | ||
| bob.generateKeys(); | ||
| const bobPub = bob.getPublicKey(); | ||
|
|
||
| const nobleAlicePriv = p256.utils.randomPrivateKey(); | ||
| const nobleBobPriv = p256.utils.randomPrivateKey(); | ||
| const nobleBobPub = p256.getPublicKey(nobleBobPriv); | ||
|
|
||
| bench | ||
| .add('rnqc', () => { | ||
| alice.computeSecret(bobPub); | ||
| }) | ||
| .add('@noble/curves', () => { | ||
| p256.getSharedSecret(nobleAlicePriv, nobleBobPub); | ||
| }); | ||
|
|
||
| bench.warmupTime = 100; | ||
| return bench; | ||
| }; | ||
|
|
||
| export default [ecdh_p256_genKeys, ecdh_p256_computeSecret]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { test } from '../util'; | ||
| import { Buffer } from '@craftzdog/react-native-buffer'; | ||
| import crypto from 'react-native-quick-crypto'; | ||
| import { assert } from 'chai'; | ||
|
|
||
| const SUITE = 'dh'; | ||
|
|
||
| test(SUITE, 'should create DiffieHellman with size', () => { | ||
| const dh = crypto.createDiffieHellman(512); | ||
| const prime = dh.getPrime(); | ||
| assert.isOk(prime); | ||
| // Size check approx | ||
| assert.isAtLeast(prime.length, 64); | ||
| }); | ||
|
|
||
| test(SUITE, 'should create DiffieHellman with prime', () => { | ||
| // 512-bit prime (Group 1 from RFC 2409) | ||
| const prime = Buffer.from( | ||
| 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1' + | ||
| '29024E088A67CC74020BBEA63B139B22514A08798E3404DD' + | ||
| 'EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245' + | ||
| 'E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + | ||
| 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381' + | ||
| 'FFFFFFFFFFFFFFFF', | ||
| 'hex', | ||
| ); | ||
| const generator = Buffer.from([2]); | ||
| const dh = crypto.createDiffieHellman(prime, generator); | ||
|
|
||
| assert.strictEqual(dh.getPrime('hex'), prime.toString('hex').toLowerCase()); | ||
| assert.strictEqual( | ||
| dh.getGenerator('hex'), | ||
| generator.toString('hex').toLowerCase(), | ||
| ); | ||
| }); | ||
|
|
||
| test(SUITE, 'should compute shared secret', () => { | ||
| const alice = crypto.createDiffieHellman(512); | ||
| const aliceKeys = alice.generateKeys(); | ||
|
|
||
| const bob = crypto.createDiffieHellman( | ||
| alice.getPrime(), | ||
| alice.getGenerator(), | ||
| ); | ||
| const bobKeys = bob.generateKeys(); | ||
|
|
||
| const aliceSecret = alice.computeSecret(bobKeys); | ||
| const bobSecret = bob.computeSecret(aliceKeys); | ||
|
|
||
| assert.strictEqual(aliceSecret.toString('hex'), bobSecret.toString('hex')); | ||
| }); | ||
|
|
||
| test(SUITE, 'should set keys', () => { | ||
| const alice = crypto.createDiffieHellman(512); | ||
| alice.generateKeys(); | ||
|
|
||
| const dh2 = crypto.createDiffieHellman( | ||
| alice.getPrime(), | ||
| alice.getGenerator(), | ||
| ); | ||
| dh2.setPublicKey(alice.getPublicKey()); | ||
| dh2.setPrivateKey(alice.getPrivateKey()); | ||
|
|
||
| assert.strictEqual(dh2.getPublicKey('hex'), alice.getPublicKey('hex')); | ||
| assert.strictEqual(dh2.getPrivateKey('hex'), alice.getPrivateKey('hex')); | ||
| }); | ||
|
|
||
| test(SUITE, 'should create DiffieHellman from standard group', () => { | ||
| const dh = crypto.getDiffieHellman('modp14'); | ||
| assert.isOk(dh); | ||
| const prime = dh.getPrime(); | ||
| assert.isTrue(Buffer.isBuffer(prime)); | ||
| // modp14 is 2048-bit group | ||
| assert.strictEqual(prime.length, 256); | ||
| assert.strictEqual(dh.getGenerator('hex'), '02'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { test } from '../util'; | ||
| import { Buffer } from '@craftzdog/react-native-buffer'; | ||
| import crypto 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); | ||
|
|
||
| // Public key should be derived/set (depending on impl, but usually settable) | ||
| // In our implementation setPrivateKey derives public key? | ||
| // Let's check consistency. | ||
| // If setPrivateKey derives public key, we can check it matches | ||
| 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); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,6 +130,9 @@ Pod::Spec.new do |s| | |
| cpp_headers = [ | ||
| "\"$(PODS_TARGET_SRCROOT)/cpp/utils\"", | ||
| "\"$(PODS_TARGET_SRCROOT)/cpp/hkdf\"", | ||
| "\"$(PODS_TARGET_SRCROOT)/cpp/dh\"", | ||
| "\"$(PODS_TARGET_SRCROOT)/cpp/ecdh\"", | ||
| "\"$(PODS_TARGET_SRCROOT)/nitrogen/generated/shared/c++\"", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. required? There are no other nitro ones, are there? |
||
| "\"$(PODS_TARGET_SRCROOT)/deps/ncrypto/include\"", | ||
| "\"$(PODS_TARGET_SRCROOT)/deps/blake3/c\"", | ||
| "\"$(PODS_TARGET_SRCROOT)/deps/fastpbkdf2\"" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to fix this on your local somehow.