Skip to content
Open
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
43 changes: 43 additions & 0 deletions cpp/react-native-libsodium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,49 @@ namespace ReactNativeLibsodium

jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_box_seal_open", std::move(jsi_crypto_box_seal_open));

auto jsi_crypto_box_beforenm = jsi::Function::createFromHostFunction(
jsiRuntime,
jsi::PropNameID::forUtf8(jsiRuntime, "jsi_crypto_box_beforenm"),
2,
[](jsi::Runtime &runtime, const jsi::Value &thisValue, const jsi::Value *arguments, size_t count) -> jsi::Value
{
const std::string functionName = "crypto_box_beforenm";

std::string publicKeyArgumentName = "publicKey";
unsigned int publicKeyArgumentPosition = 0;
JsiArgType publicKeyArgType = validateIsStringOrArrayBuffer(functionName, runtime, arguments[publicKeyArgumentPosition], publicKeyArgumentName, true);

std::string privateKeyArgumentName = "privateKey";
unsigned int privateKeyArgumentPosition = 1;
JsiArgType privateKeyArgType = validateIsStringOrArrayBuffer(functionName, runtime, arguments[privateKeyArgumentPosition], privateKeyArgumentName, true);

auto publicKey = arguments[publicKeyArgumentPosition].asObject(runtime).getArrayBuffer(runtime);
auto privateKey = arguments[privateKeyArgumentPosition].asObject(runtime).getArrayBuffer(runtime);

if (publicKey.length(runtime) != crypto_box_PUBLICKEYBYTES)
{
throw jsi::JSError(runtime, "invalid publicKey length");
}

if (privateKey.length(runtime) != crypto_box_SECRETKEYBYTES)
{
throw jsi::JSError(runtime, "invalid privateKey length");
}

// Allocate memory for the shared secret
std::vector<uint8_t> sharedSecret(crypto_box_BEFORENMBYTES);

int result = crypto_box_beforenm(
sharedSecret.data(),
publicKey.data(runtime),
privateKey.data(runtime));

throwOnBadResult(functionName, runtime, result);
return arrayBufferAsObject(runtime, sharedSecret);
});

jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_box_beforenm", std::move(jsi_crypto_box_beforenm));

auto jsi_crypto_pwhash = jsi::Function::createFromHostFunction(
jsiRuntime,
jsi::PropNameID::forUtf8(jsiRuntime, "jsi_crypto_pwhash"),
Expand Down
12 changes: 12 additions & 0 deletions src/lib.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ declare global {
publicKey: ArrayBuffer,
secretKey: ArrayBuffer
): ArrayBuffer;
function jsi_crypto_box_beforenm(
message: ArrayBuffer,
publicKey: ArrayBuffer
): ArrayBuffer;
function jsi_crypto_generichash(
hashLength: number,
message: string | ArrayBuffer,
Expand Down Expand Up @@ -621,6 +625,13 @@ export function crypto_box_seal_open(
return convertToOutputFormat(result, outputFormat);
}

export function crypto_box_beforenm(
publicKey: Uint8Array,
privateKey: Uint8Array
) {
return global.jsi_crypto_box_beforenm(publicKey.buffer, privateKey.buffer);
}

export function crypto_generichash(
hash_length: number,
message: string | Uint8Array,
Expand Down Expand Up @@ -860,6 +871,7 @@ export default {
crypto_box_open_easy,
crypto_box_PUBLICKEYBYTES,
crypto_box_SECRETKEYBYTES,
crypto_box_beforenm,
crypto_generichash,
crypto_generichash_BYTES,
crypto_generichash_BYTES_MIN,
Expand Down