Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion example/src/tests/ecdh/ecdh_tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from '../util';
import crypto, { Buffer } from 'react-native-quick-crypto';
import crypto, { Buffer, getCurves } from 'react-native-quick-crypto';
import { assert } from 'chai';

const SUITE = 'ecdh';
Expand Down Expand Up @@ -68,3 +68,25 @@ test(SUITE, 'should work with string input', () => {
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);
});
21 changes: 21 additions & 0 deletions packages/react-native-quick-crypto/cpp/ec/HybridEcKeyPair.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <NitroModules/ArrayBuffer.hpp>
#include <NitroModules/Promise.hpp>
#include <algorithm>
#include <memory>
#include <openssl/bio.h>
#include <openssl/buffer.h>
Expand Down Expand Up @@ -425,4 +426,24 @@ void HybridEcKeyPair::checkKeyPair() {
}
}

std::vector<std::string> HybridEcKeyPair::getSupportedCurves() {
const size_t count = EC_get_builtin_curves(nullptr, 0);
std::vector<EC_builtin_curve> curves(count);
if (EC_get_builtin_curves(curves.data(), count) != count) {
throw std::runtime_error("Failed to enumerate EC curves");
}

std::vector<std::string> names;
names.reserve(count);
for (const auto& curve : curves) {
const char* sn = OBJ_nid2sn(curve.nid);
if (sn != nullptr) {
names.emplace_back(sn);
}
}

std::sort(names.begin(), names.end());
return names;
}

} // namespace margelo::nitro::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class HybridEcKeyPair : public HybridEcKeyPairSpec {
std::shared_ptr<ArrayBuffer> sign(const std::shared_ptr<ArrayBuffer>& data, const std::string& hashAlgorithm) override;
bool verify(const std::shared_ptr<ArrayBuffer>& data, const std::shared_ptr<ArrayBuffer>& signature,
const std::string& hashAlgorithm) override;
std::vector<std::string> getSupportedCurves() override;

protected:
void checkKeyPair();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions packages/react-native-quick-crypto/src/ec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ import {
import { Buffer } from '@craftzdog/react-native-buffer';
import { ECDH } from './ecdh';

class EcUtils {
private static _native: EcKeyPair | undefined;
private static get native(): EcKeyPair {
if (!this._native) {
this._native = NitroModules.createHybridObject<EcKeyPair>('EcKeyPair');
}
return this._native;
}
public static getSupportedCurves(): string[] {
return this.native.getSupportedCurves();
}
}

export function getCurves(): string[] {
return EcUtils.getSupportedCurves();
}

export class Ec {
native: EcKeyPair;

Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-quick-crypto/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as scrypt from './scrypt';
import * as random from './random';
import * as ecdh from './ecdh';
import * as dh from './diffie-hellman';
import { getCurves } from './ec';
import { constants } from './constants';

// utils import
Expand All @@ -39,6 +40,7 @@ const QuickCrypto = {
...dh,
...utils,
...subtle,
getCurves,
constants,
Buffer,
};
Expand Down Expand Up @@ -81,6 +83,7 @@ export * from './pbkdf2';
export * from './scrypt';
export * from './random';
export * from './ecdh';
export { getCurves } from './ec';
export * from './diffie-hellman';
export * from './utils';
export * from './subtle';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ export interface EcKeyPair
signature: ArrayBuffer,
hashAlgorithm: string,
): boolean;

getSupportedCurves(): string[];
}
Loading