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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
70 changes: 51 additions & 19 deletions bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions example/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
presets: ['module:@react-native/babel-preset', '@babel/preset-typescript'],
parserOpts: { plugins: ['decorators'] },
plugins: [
['@babel/plugin-transform-class-static-block'],
[
Expand Down
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
"crypto-browserify": "3.12.0",
"event-target-polyfill": "0.0.4",
"events": "3.3.0",
"nitro-core": "1.0.0-alpha.nightly.138.g43e09c161101",
"react": "19.3.0",
"react-native": "0.88.1",
"react-native-bouncy-checkbox": "2.1.10",
"react-native-mmkv": "4.3.2",
"react-native-nitro-modules": "0.36.5",
"react-native-nitro-modules": "1.0.0-alpha.nightly.138.g43e09c161101",
"react-native-quick-base64": "3.0.0",
"react-native-quick-crypto": "workspace:*",
"react-native-safe-area-context": "5.11.1",
Expand Down
12 changes: 6 additions & 6 deletions example/src/tests/slhdsa/raw_private_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
sign,
verify,
type SlhDsaKeyPairType,
isSlhDsaPrivateKeyFactoryHandle,
type KeyObjectHandle,
type SlhDsaPrivateKeyFactoryHandle,
} from 'react-native-quick-crypto';
import { NitroModules } from 'react-native-nitro-modules';
import { KeyObjectHandle as NativeKeyObjectHandle } from 'react-native-quick-crypto/src/specs/keyObjectHandle.nitro';
import { expect } from 'chai';
import { assertThrowsAsync, test } from '../util';

Expand Down Expand Up @@ -198,10 +198,10 @@ test(SUITE, 'imports validated handles with bounded admission', async () => {
'SLH-DSA raw private keys require createPrivateKeyAsync()',
);

const factory =
NitroModules.createHybridObject<SlhDsaPrivateKeyFactoryHandle>(
'KeyObjectHandle',
);
const factory = new NativeKeyObjectHandle();
if (!isSlhDsaPrivateKeyFactoryHandle(factory)) {
throw new Error('SLH-DSA raw private key import is unavailable');
}
try {
for (const value of [null, 1, {}, new Uint8Array(64)]) {
await assertThrowsAsync(
Expand Down
3 changes: 3 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": ["@react-native/typescript-config", "../config/tsconfig.json"],
"compilerOptions": {
"resolvePackageJsonImports": true
},
"include": [
"index.ts",
"app.json",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"eslint-plugin-react-native": "5.0.0",
"husky": "9.1.7",
"lint-staged": "16.1.6",
"nitrogen": "0.33.2",
"nitrogen": "1.0.0-alpha.nightly.138.g43e09c161101",
"prettier": "3.5.3",
"release-it": "20.0.1",
"typescript": "5.8.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native-quick-crypto/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ add_library(

target_compile_definitions(${PACKAGE_NAME} PRIVATE NCRYPTO_EVP_ONLY=1)

# add Nitrogen specs
# add Nitrogen specs (bare output links nitro-core, which RN hosts provide via the shim)
set(nitro-core_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake/nitro-core")
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/QuickCrypto+autolinking.cmake)

# local includes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# React Native hosts have no nitro-core prefab package. Map the bare target name onto
# react-native-nitro-modules so Nitrogen's "bare" +autolinking.cmake links unchanged.
find_package(react-native-nitro-modules REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)
if(NOT TARGET nitro-core::NitroModules)
add_library(nitro-core::NitroModules INTERFACE IMPORTED)
target_link_libraries(nitro-core::NitroModules INTERFACE
react-native-nitro-modules::NitroModules ReactAndroid::jsi ReactAndroid::reactnative)
target_compile_definitions(nitro-core::NitroModules INTERFACE
RN_SERIALIZABLE_STATE FOLLY_NO_CONFIG=1 FOLLY_HAVE_CLOCK_GETTIME=1 FOLLY_USE_LIBCPP=1
FOLLY_CFG_NO_COROUTINES=1 FOLLY_MOBILE=1 FOLLY_HAVE_RECVMMSG=1 FOLLY_HAVE_PTHREAD=1
FOLLY_HAVE_XSI_STRERROR_R=1)
endif()
set(nitro-core_FOUND TRUE)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <fbjni/fbjni.h>
#include <jni.h>

#include "QuickCryptoOnLoad.hpp"
#include <QuickCrypto/private/QuickCryptoOnLoad.hpp>

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
return facebook::jni::initialize(vm, [=] { margelo::nitro::crypto::initialize(vm); });
return facebook::jni::initialize(vm, [] { margelo::nitro::crypto::registerAllNatives(); });
}
3 changes: 2 additions & 1 deletion packages/react-native-quick-crypto/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
presets: ['module:@react-native/babel-preset'],
presets: ['module:react-native-builder-bob/babel-preset'],
parserOpts: { plugins: ['decorators'] },
};
46 changes: 20 additions & 26 deletions packages/react-native-quick-crypto/cpp/argon2/HybridArgon2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ static ncrypto::Argon2Type parseAlgorithm(const std::string& algo) {
throw std::runtime_error("Unknown argon2 algorithm: " + algo);
}

static std::shared_ptr<ArrayBuffer> hashImpl(const std::string& algorithm, const std::shared_ptr<ArrayBuffer>& message,
const std::shared_ptr<ArrayBuffer>& nonce, double parallelism, double tagLength, double memory,
double passes, double version, const std::optional<std::shared_ptr<ArrayBuffer>>& secret,
const std::optional<std::shared_ptr<ArrayBuffer>>& associatedData) {
static ArrayBuffer hashImpl(const std::string& algorithm, const ArrayBuffer& message, const ArrayBuffer& nonce, double parallelism,
double tagLength, double memory, double passes, double version, const std::optional<ArrayBuffer>& secret,
const std::optional<ArrayBuffer>& associatedData) {

auto type = parseAlgorithm(algorithm);

Expand All @@ -39,20 +38,19 @@ static std::shared_ptr<ArrayBuffer> hashImpl(const std::string& algorithm, const
uint32_t passesU = validateUInt<uint32_t>(passes, "Argon2 passes");
uint32_t versionU = validateUInt<uint32_t>(version, "Argon2 version");

ncrypto::Buffer<const char> passBuf{message->size() > 0 ? reinterpret_cast<const char*>(message->data()) : "", message->size()};
ncrypto::Buffer<const char> passBuf{message.size() > 0 ? reinterpret_cast<const char*>(message.data()) : "", message.size()};

ncrypto::Buffer<const unsigned char> saltBuf{nonce->size() > 0 ? reinterpret_cast<const unsigned char*>(nonce->data())
: reinterpret_cast<const unsigned char*>(""),
nonce->size()};
ncrypto::Buffer<const unsigned char> saltBuf{
nonce.size() > 0 ? reinterpret_cast<const unsigned char*>(nonce.data()) : reinterpret_cast<const unsigned char*>(""), nonce.size()};

ncrypto::Buffer<const unsigned char> secretBuf{nullptr, 0};
if (secret.has_value() && secret.value()->size() > 0) {
secretBuf = {reinterpret_cast<const unsigned char*>(secret.value()->data()), secret.value()->size()};
if (secret.has_value() && secret.value().size() > 0) {
secretBuf = {reinterpret_cast<const unsigned char*>(secret.value().data()), secret.value().size()};
}

ncrypto::Buffer<const unsigned char> adBuf{nullptr, 0};
if (associatedData.has_value() && associatedData.value()->size() > 0) {
adBuf = {reinterpret_cast<const unsigned char*>(associatedData.value()->data()), associatedData.value()->size()};
if (associatedData.has_value() && associatedData.value().size() > 0) {
adBuf = {reinterpret_cast<const unsigned char*>(associatedData.value().data()), associatedData.value().size()};
}

auto result = ncrypto::argon2(passBuf, saltBuf, parallelismU, tagLengthU, memoryU, passesU, versionU, secretBuf, adBuf, type);
Expand All @@ -69,37 +67,33 @@ static std::shared_ptr<ArrayBuffer> hashImpl(const std::string& algorithm, const
#endif // OPENSSL_NO_ARGON2
#endif // OPENSSL_VERSION_NUMBER

std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
HybridArgon2::hash(const std::string& algorithm, const std::shared_ptr<ArrayBuffer>& message, const std::shared_ptr<ArrayBuffer>& nonce,
double parallelism, double tagLength, double memory, double passes, double version,
const std::optional<std::shared_ptr<ArrayBuffer>>& secret,
const std::optional<std::shared_ptr<ArrayBuffer>>& associatedData) {
Promise<ArrayBuffer> HybridArgon2::hash(const std::string& algorithm, const ArrayBuffer& message, const ArrayBuffer& nonce,
double parallelism, double tagLength, double memory, double passes, double version,
const std::optional<ArrayBuffer>& secret, const std::optional<ArrayBuffer>& associatedData) {
#if OPENSSL_VERSION_NUMBER >= 0x30200000L && !defined(OPENSSL_NO_ARGON2)
auto nativeMessage = ToNativeArrayBuffer(message);
auto nativeNonce = ToNativeArrayBuffer(nonce);
std::optional<std::shared_ptr<ArrayBuffer>> nativeSecret;
std::optional<ArrayBuffer> nativeSecret;
if (secret.has_value()) {
nativeSecret = ToNativeArrayBuffer(secret.value());
}
std::optional<std::shared_ptr<ArrayBuffer>> nativeAd;
std::optional<ArrayBuffer> nativeAd;
if (associatedData.has_value()) {
nativeAd = ToNativeArrayBuffer(associatedData.value());
}

return Promise<std::shared_ptr<ArrayBuffer>>::async([algorithm, nativeMessage, nativeNonce, parallelism, tagLength, memory, passes,
version, nativeSecret = std::move(nativeSecret), nativeAd = std::move(nativeAd)]() {
return Promise<ArrayBuffer>::async([algorithm, nativeMessage, nativeNonce, parallelism, tagLength, memory, passes, version,
nativeSecret = std::move(nativeSecret), nativeAd = std::move(nativeAd)]() {
return hashImpl(algorithm, nativeMessage, nativeNonce, parallelism, tagLength, memory, passes, version, nativeSecret, nativeAd);
});
#else
throw std::runtime_error("Argon2 is not supported (requires OpenSSL 3.2+)");
#endif
}

std::shared_ptr<ArrayBuffer> HybridArgon2::hashSync(const std::string& algorithm, const std::shared_ptr<ArrayBuffer>& message,
const std::shared_ptr<ArrayBuffer>& nonce, double parallelism, double tagLength,
double memory, double passes, double version,
const std::optional<std::shared_ptr<ArrayBuffer>>& secret,
const std::optional<std::shared_ptr<ArrayBuffer>>& associatedData) {
ArrayBuffer HybridArgon2::hashSync(const std::string& algorithm, const ArrayBuffer& message, const ArrayBuffer& nonce, double parallelism,
double tagLength, double memory, double passes, double version, const std::optional<ArrayBuffer>& secret,
const std::optional<ArrayBuffer>& associatedData) {
#if OPENSSL_VERSION_NUMBER >= 0x30200000L && !defined(OPENSSL_NO_ARGON2)
return hashImpl(algorithm, message, nonce, parallelism, tagLength, memory, passes, version, secret, associatedData);
#else
Expand Down
20 changes: 7 additions & 13 deletions packages/react-native-quick-crypto/cpp/argon2/HybridArgon2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@
#include <optional>
#include <string>

#include "HybridArgon2Spec.hpp"
#include <QuickCrypto/HybridArgon2Spec.hpp>

namespace margelo::nitro::crypto {

using namespace facebook;

class HybridArgon2 : public HybridArgon2Spec {
public:
HybridArgon2() : HybridObject(TAG) {}
static Promise<ArrayBuffer> hash(const std::string& algorithm, const ArrayBuffer& message, const ArrayBuffer& nonce, double parallelism,
double tagLength, double memory, double passes, double version, const std::optional<ArrayBuffer>& secret,
const std::optional<ArrayBuffer>& associatedData);

public:
std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>> hash(const std::string& algorithm, const std::shared_ptr<ArrayBuffer>& message,
const std::shared_ptr<ArrayBuffer>& nonce, double parallelism,
double tagLength, double memory, double passes, double version,
const std::optional<std::shared_ptr<ArrayBuffer>>& secret,
const std::optional<std::shared_ptr<ArrayBuffer>>& associatedData) override;

std::shared_ptr<ArrayBuffer> hashSync(const std::string& algorithm, const std::shared_ptr<ArrayBuffer>& message,
const std::shared_ptr<ArrayBuffer>& nonce, double parallelism, double tagLength, double memory,
double passes, double version, const std::optional<std::shared_ptr<ArrayBuffer>>& secret,
const std::optional<std::shared_ptr<ArrayBuffer>>& associatedData) override;
static ArrayBuffer hashSync(const std::string& algorithm, const ArrayBuffer& message, const ArrayBuffer& nonce, double parallelism,
double tagLength, double memory, double passes, double version, const std::optional<ArrayBuffer>& secret,
const std::optional<ArrayBuffer>& associatedData);
};

} // namespace margelo::nitro::crypto
17 changes: 7 additions & 10 deletions packages/react-native-quick-crypto/cpp/blake3/HybridBlake3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ void HybridBlake3::initHash() {
initialized = true;
}

void HybridBlake3::initKeyed(const std::shared_ptr<ArrayBuffer>& keyBuffer) {
if (!keyBuffer || keyBuffer->size() != BLAKE3_KEY_LEN) {
void HybridBlake3::initKeyed(const ArrayBuffer& keyBuffer) {
if (keyBuffer.size() != BLAKE3_KEY_LEN) {
throw std::runtime_error("BLAKE3 key must be exactly 32 bytes");
}

std::array<uint8_t, BLAKE3_KEY_LEN> keyArray;
std::memcpy(keyArray.data(), keyBuffer->data(), BLAKE3_KEY_LEN);
std::memcpy(keyArray.data(), keyBuffer.data(), BLAKE3_KEY_LEN);

blake3_hasher_init_keyed(&hasher, keyArray.data());
mode = Mode::Keyed;
Expand All @@ -44,17 +44,14 @@ void HybridBlake3::initDeriveKey(const std::string& ctx) {
initialized = true;
}

void HybridBlake3::update(const std::shared_ptr<ArrayBuffer>& data) {
void HybridBlake3::update(const ArrayBuffer& data) {
if (!initialized) {
throw std::runtime_error("BLAKE3 hasher not initialized");
}
if (!data) {
return;
}
blake3_hasher_update(&hasher, data->data(), data->size());
blake3_hasher_update(&hasher, dataOrEmpty(data), data.size());
}

std::shared_ptr<ArrayBuffer> HybridBlake3::digest(std::optional<double> length) {
ArrayBuffer HybridBlake3::digest(const std::optional<double>& length) {
if (!initialized) {
throw std::runtime_error("BLAKE3 hasher not initialized");
}
Expand All @@ -72,7 +69,7 @@ std::shared_ptr<ArrayBuffer> HybridBlake3::digest(std::optional<double> length)
blake3_hasher_finalize(&hasher, output.get(), outLen);

uint8_t* raw_ptr = output.get();
return std::make_shared<margelo::nitro::NativeArrayBuffer>(output.release(), outLen, [raw_ptr]() { delete[] raw_ptr; });
return ArrayBuffer::wrap(output.release(), outLen, [raw_ptr]() { delete[] raw_ptr; });
}

void HybridBlake3::reset() {
Expand Down
12 changes: 6 additions & 6 deletions packages/react-native-quick-crypto/cpp/blake3/HybridBlake3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
#include <optional>
#include <string>

#include "HybridBlake3Spec.hpp"
#include "blake3.h"
#include <QuickCrypto/HybridBlake3Spec.hpp>

namespace margelo::nitro::crypto {

class HybridBlake3 : public HybridBlake3Spec {
public:
HybridBlake3() : HybridObject(TAG) {}
HybridBlake3() = default;
~HybridBlake3() = default;

public:
void initHash() override;
void initKeyed(const std::shared_ptr<ArrayBuffer>& key) override;
void initKeyed(const ArrayBuffer& key) override;
void initDeriveKey(const std::string& context) override;
void update(const std::shared_ptr<ArrayBuffer>& data) override;
std::shared_ptr<ArrayBuffer> digest(std::optional<double> length) override;
void update(const ArrayBuffer& data) override;
ArrayBuffer digest(const std::optional<double>& length) override;
void reset() override;
std::shared_ptr<HybridBlake3Spec> copy() override;
std::string getVersion() override;
static std::string getVersion();

private:
blake3_hasher hasher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,30 @@

namespace margelo::nitro::crypto {

bool HybridCertificate::verifySpkac(const std::shared_ptr<ArrayBuffer>& spkac) {
return ncrypto::VerifySpkac(reinterpret_cast<const char*>(spkac->data()), spkac->size());
bool HybridCertificate::verifySpkac(const ArrayBuffer& spkac) {
return ncrypto::VerifySpkac(reinterpret_cast<const char*>(dataOrEmpty(spkac)), spkac.size());
}

std::shared_ptr<ArrayBuffer> HybridCertificate::exportPublicKey(const std::shared_ptr<ArrayBuffer>& spkac) {
auto bio = ncrypto::ExportPublicKey(reinterpret_cast<const char*>(spkac->data()), spkac->size());
ArrayBuffer HybridCertificate::exportPublicKey(const ArrayBuffer& spkac) {
auto bio = ncrypto::ExportPublicKey(reinterpret_cast<const char*>(dataOrEmpty(spkac)), spkac.size());

if (!bio) {
auto empty = new uint8_t[0];
return std::make_shared<NativeArrayBuffer>(empty, 0, [empty]() { delete[] empty; });
return ArrayBuffer::empty();
}

BUF_MEM* mem = bio;
if (!mem || mem->length == 0) {
auto empty = new uint8_t[0];
return std::make_shared<NativeArrayBuffer>(empty, 0, [empty]() { delete[] empty; });
return ArrayBuffer::empty();
}

return ToNativeArrayBuffer(reinterpret_cast<const uint8_t*>(mem->data), mem->length);
}

std::shared_ptr<ArrayBuffer> HybridCertificate::exportChallenge(const std::shared_ptr<ArrayBuffer>& spkac) {
auto buf = ncrypto::ExportChallenge(reinterpret_cast<const char*>(spkac->data()), spkac->size());
ArrayBuffer HybridCertificate::exportChallenge(const ArrayBuffer& spkac) {
auto buf = ncrypto::ExportChallenge(reinterpret_cast<const char*>(dataOrEmpty(spkac)), spkac.size());

if (buf.data == nullptr) {
auto empty = new uint8_t[0];
return std::make_shared<NativeArrayBuffer>(empty, 0, [empty]() { delete[] empty; });
return ArrayBuffer::empty();
}

auto result = ToNativeArrayBuffer(reinterpret_cast<const uint8_t*>(buf.data), buf.len);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#pragma once

#include "HybridCertificateSpec.hpp"
#include <QuickCrypto/HybridCertificateSpec.hpp>

namespace margelo::nitro::crypto {

class HybridCertificate : public HybridCertificateSpec {
public:
HybridCertificate() : HybridObject(TAG) {}

bool verifySpkac(const std::shared_ptr<ArrayBuffer>& spkac) override;
std::shared_ptr<ArrayBuffer> exportPublicKey(const std::shared_ptr<ArrayBuffer>& spkac) override;
std::shared_ptr<ArrayBuffer> exportChallenge(const std::shared_ptr<ArrayBuffer>& spkac) override;
static bool verifySpkac(const ArrayBuffer& spkac);
static ArrayBuffer exportPublicKey(const ArrayBuffer& spkac);
static ArrayBuffer exportChallenge(const ArrayBuffer& spkac);
};

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