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
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Standard: c++14
# Indentation
IndentWidth: 2
ColumnLimit: 140
AccessModifierOffset: -1

# Includes
SortIncludes: CaseSensitive
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/validate-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up clang-format
run: sudo apt-get install -y clang-format
- name: Run clang-format check
run: |
find packages/react-native-quick-crypto/cpp packages/react-native-quick-crypto/android/src/main/cpp \
-regex '.*\.\(cpp\|hpp\|cc\|cxx\|h\)' \
-exec clang-format --style=file --dry-run --Werror {} +
- uses: reviewdog/action-cpplint@master
with:
github_token: ${{ secrets.github_token }}
reporter: github-pr-review
flags: --linelength=230
flags: --linelength=140
targets: --recursive packages/react-native-quick-crypto/cpp packages/react-native-quick-crypto/android/src/main/cpp
filter: "-legal/copyright\
,-readability/todo\
Expand All @@ -35,4 +42,5 @@ jobs:
,-build/include_order\
,-whitespace/indent_namespace\
,-whitespace/parens\
,-build/include_what_you_use\
"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "1.0.0-beta.12",
"scripts": {
"check-all": "./scripts/clang-format.sh",
"clang-format": "./scripts/clang-format.sh",
"clean": "bun --filter='*' clean",
"specs": "bun --filter='react-native-quick-crypto' specs",
"bundle-install": "bun --filter='react-native-quick-crypto-example' bundle-install",
Expand Down
121 changes: 32 additions & 89 deletions packages/react-native-quick-crypto/cpp/ed25519/HybridEdKeyPair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,23 @@

namespace margelo::nitro::crypto {

std::shared_ptr<Promise<void>>
HybridEdKeyPair::generateKeyPair(
double publicFormat,
double publicType,
double privateFormat,
double privateType,
const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase
) {
std::shared_ptr<Promise<void>> HybridEdKeyPair::generateKeyPair(double publicFormat, double publicType, double privateFormat,
double privateType, const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase) {
// get owned NativeArrayBuffers before passing to sync function
std::optional<std::shared_ptr<ArrayBuffer>> nativePassphrase = std::nullopt;
if (passphrase.has_value()) {
nativePassphrase = ToNativeArrayBuffer(passphrase.value());
}

return Promise<void>::async(
[this, publicFormat, publicType, privateFormat, privateType, cipher,
nativePassphrase]() {
this->generateKeyPairSync(
publicFormat,
publicType,
privateFormat,
privateType,
cipher,
nativePassphrase
);
}
);
return Promise<void>::async([this, publicFormat, publicType, privateFormat, privateType, cipher, nativePassphrase]() {
this->generateKeyPairSync(publicFormat, publicType, privateFormat, privateType, cipher, nativePassphrase);
});
}

void
HybridEdKeyPair::generateKeyPairSync(
double publicFormat,
double publicType,
double privateFormat,
double privateType,
const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase
) {
void HybridEdKeyPair::generateKeyPairSync(double publicFormat, double publicType, double privateFormat, double privateType,
const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase) {
EVP_PKEY_CTX* pctx;

// key context
Expand All @@ -69,30 +47,21 @@ HybridEdKeyPair::generateKeyPairSync(
EVP_PKEY_CTX_free(pctx);
}


std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
HybridEdKeyPair::sign(
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) {
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> HybridEdKeyPair::sign(const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) {
// get owned NativeArrayBuffer before passing to sync function
auto nativeMessage = ToNativeArrayBuffer(message);
std::optional<std::shared_ptr<ArrayBuffer>> nativeKey = std::nullopt;
if (key.has_value()) {
nativeKey = ToNativeArrayBuffer(key.value());
}

return Promise<std::shared_ptr<ArrayBuffer>>::async([this, nativeMessage, nativeKey]() {
return this->signSync(nativeMessage, nativeKey);
}
);
return Promise<std::shared_ptr<ArrayBuffer>>::async(
[this, nativeMessage, nativeKey]() { return this->signSync(nativeMessage, nativeKey); });
}

std::shared_ptr<ArrayBuffer>
HybridEdKeyPair::signSync(
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) {
std::shared_ptr<ArrayBuffer> HybridEdKeyPair::signSync(const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) {

size_t sig_len = 0;
uint8_t* sig = NULL;
Expand Down Expand Up @@ -135,24 +104,17 @@ HybridEdKeyPair::signSync(
}

// return value for JS
std::shared_ptr<ArrayBuffer> signature = std::make_shared<NativeArrayBuffer>(
sig,
sig_len,
[=]() { delete[] sig; }
);
std::shared_ptr<ArrayBuffer> signature = std::make_shared<NativeArrayBuffer>(sig, sig_len, [=]() { delete[] sig; });

// Clean up
EVP_MD_CTX_free(md_ctx);

return signature;
}

std::shared_ptr<Promise<bool>>
HybridEdKeyPair::verify(
const std::shared_ptr<ArrayBuffer>& signature,
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) {
std::shared_ptr<Promise<bool>> HybridEdKeyPair::verify(const std::shared_ptr<ArrayBuffer>& signature,
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) {
// get owned NativeArrayBuffers before passing to sync function
auto nativeSignature = ToNativeArrayBuffer(signature);
auto nativeMessage = ToNativeArrayBuffer(message);
Expand All @@ -161,18 +123,12 @@ HybridEdKeyPair::verify(
nativeKey = ToNativeArrayBuffer(key.value());
}

return Promise<bool>::async([this, nativeSignature, nativeMessage, nativeKey]() {
return this->verifySync(nativeSignature, nativeMessage, nativeKey);
}
);
return Promise<bool>::async(
[this, nativeSignature, nativeMessage, nativeKey]() { return this->verifySync(nativeSignature, nativeMessage, nativeKey); });
}

bool
HybridEdKeyPair::verifySync(
const std::shared_ptr<ArrayBuffer>& signature,
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) {
bool HybridEdKeyPair::verifySync(const std::shared_ptr<ArrayBuffer>& signature, const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) {
// get key to use for verifying
EVP_PKEY* pkey = this->importPrivateKey(key);

Expand All @@ -199,22 +155,17 @@ HybridEdKeyPair::verifySync(
}

// verify
auto res = EVP_DigestVerify(
md_ctx,
signature.get()->data(), signature.get()->size(),
message.get()->data(), message.get()->size()
);
auto res = EVP_DigestVerify(md_ctx, signature.get()->data(), signature.get()->size(), message.get()->data(), message.get()->size());

//return value for JS
// return value for JS
if (res < 0) {
EVP_MD_CTX_free(md_ctx);
throw std::runtime_error("Failed to verify");
}
return res == 1; // true if 1, false if 0
}

std::shared_ptr<ArrayBuffer>
HybridEdKeyPair::getPublicKey() {
std::shared_ptr<ArrayBuffer> HybridEdKeyPair::getPublicKey() {
this->checkKeyPair();
size_t len = 32;
uint8_t* publ = new uint8_t[len];
Expand All @@ -223,8 +174,7 @@ HybridEdKeyPair::getPublicKey() {
return std::make_shared<NativeArrayBuffer>(publ, len, [=]() { delete[] publ; });
}

std::shared_ptr<ArrayBuffer>
HybridEdKeyPair::getPrivateKey() {
std::shared_ptr<ArrayBuffer> HybridEdKeyPair::getPrivateKey() {
this->checkKeyPair();
size_t len = 32;
uint8_t* priv = new uint8_t[len];
Expand All @@ -233,28 +183,21 @@ HybridEdKeyPair::getPrivateKey() {
return std::make_shared<NativeArrayBuffer>(priv, len, [=]() { delete[] priv; });
}

void
HybridEdKeyPair::checkKeyPair() {
void HybridEdKeyPair::checkKeyPair() {
if (this->pkey == nullptr) {
throw std::runtime_error("Keypair not initialized");
}
}

void
HybridEdKeyPair::setCurve(const std::string& curve) {
void HybridEdKeyPair::setCurve(const std::string& curve) {
this->curve = curve;
}

EVP_PKEY*
HybridEdKeyPair::importPrivateKey(const std::optional<std::shared_ptr<ArrayBuffer>>& key) {
EVP_PKEY* HybridEdKeyPair::importPrivateKey(const std::optional<std::shared_ptr<ArrayBuffer>>& key) {
EVP_PKEY* pkey = nullptr;
if (key.has_value()) {
pkey = EVP_PKEY_new_raw_private_key(
EVP_PKEY_ED25519, // TODO: use this->curve somehow
NULL,
key.value()->data(),
32
);
pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, // TODO: use this->curve somehow
NULL, key.value()->data(), 32);
if (pkey == nullptr) {
throw std::runtime_error("Failed to read private key");
}
Expand Down
78 changes: 24 additions & 54 deletions packages/react-native-quick-crypto/cpp/ed25519/HybridEdKeyPair.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <openssl/evp.h>
#include <openssl/err.h>
#include <memory>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <string>

#include "HybridEdKeyPairSpec.hpp"
Expand All @@ -16,58 +16,30 @@ class HybridEdKeyPair : public HybridEdKeyPairSpec {

public:
// Methods
std::shared_ptr<Promise<void>>
generateKeyPair(
double publicFormat,
double publicType,
double privateFormat,
double privateType,
const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase
) override;

void
generateKeyPairSync(
double publicFormat,
double publicType,
double privateFormat,
double privateType,
const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase
) override;

std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
sign(
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) override;

std::shared_ptr<ArrayBuffer>
signSync(
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) override;

std::shared_ptr<Promise<bool>>
verify(
const std::shared_ptr<ArrayBuffer>& signature,
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) override;

bool
verifySync(
const std::shared_ptr<ArrayBuffer>& signature,
const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key
) override;
std::shared_ptr<Promise<void>> generateKeyPair(double publicFormat, double publicType, double privateFormat, double privateType,
const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase) override;

void generateKeyPairSync(double publicFormat, double publicType, double privateFormat, double privateType,
const std::optional<std::string>& cipher,
const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase) override;

std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> sign(const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) override;

std::shared_ptr<ArrayBuffer> signSync(const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) override;

std::shared_ptr<Promise<bool>> verify(const std::shared_ptr<ArrayBuffer>& signature, const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) override;

bool verifySync(const std::shared_ptr<ArrayBuffer>& signature, const std::shared_ptr<ArrayBuffer>& message,
const std::optional<std::shared_ptr<ArrayBuffer>>& key) override;

protected:
std::shared_ptr<ArrayBuffer>
getPublicKey() override;
std::shared_ptr<ArrayBuffer> getPublicKey() override;

std::shared_ptr<ArrayBuffer>
getPrivateKey() override;
std::shared_ptr<ArrayBuffer> getPrivateKey() override;

void checkKeyPair();

Expand All @@ -77,9 +49,7 @@ class HybridEdKeyPair : public HybridEdKeyPairSpec {
std::string curve;
EVP_PKEY* pkey = nullptr;

EVP_PKEY* importPrivateKey(
const std::optional<std::shared_ptr<ArrayBuffer>>& key
);
EVP_PKEY* importPrivateKey(const std::optional<std::shared_ptr<ArrayBuffer>>& key);
};

} // namespace margelo::nitro::crypto
Loading