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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {
crypto_sign_keypair,
crypto_sign_SEEDBYTES,
crypto_sign_verify_detached,
crypto_scalarmult,
from_base64,
randombytes_buf,
randombytes_uniform,
Expand Down
38 changes: 38 additions & 0 deletions cpp/react-native-libsodium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1539,5 +1539,43 @@ namespace ReactNativeLibsodium
});

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

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

std::string privateKeyArgumentName = "privateKey";
unsigned int privateKeyArgumentPosition = 0;
validateIsArrayBuffer(functionName, runtime, arguments[privateKeyArgumentPosition], privateKeyArgumentName, true);

std::string publicKeyArgumentName = "publicKey";
unsigned int publicKeyArgumentPosition = 1;
validateIsArrayBuffer(functionName, runtime, arguments[publicKeyArgumentPosition], publicKeyArgumentName, true);

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

if (privateKey.length(runtime) != crypto_secretbox_KEYBYTES)
{
throw jsi::JSError(runtime, "invalid private key length");
}
if (publicKey.length(runtime) != crypto_secretbox_KEYBYTES)
{
throw jsi::JSError(runtime, "invalid public key length");
}

std::vector<uint8_t> result;
result.resize(crypto_scalarmult_BYTES);

crypto_scalarmult(result.data(), privateKey.data(runtime), publicKey.data(runtime));

return arrayBufferAsObject(runtime, result);
});

jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_scalarmult", std::move(jsi_crypto_scalarmult));
}
} // namespace ReactNativeLibsodium
19 changes: 19 additions & 0 deletions src/lib.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ declare global {
info: string,
length: number
): ArrayBuffer;
function jsi_crypto_scalarmult(
privateKey: ArrayBuffer,
publicKey: ArrayBuffer
): ArrayBuffer;
}

export const crypto_auth_BYTES = global.jsi_crypto_auth_BYTES;
Expand Down Expand Up @@ -795,6 +799,20 @@ export function _unstable_crypto_kdf_hkdf_sha256_expand(
);
}

export function crypto_scalarmult(
privateKey: Uint8Array,
publicKey: Uint8Array,
outputFormat?: Uint8ArrayOutputFormat | null,
) {
let result: ArrayBuffer;
result = global.jsi_crypto_scalarmult(
privateKey.buffer,
publicKey.buffer,
);

return convertToOutputFormat(result, outputFormat);
}

// add no-op ready to match the libsodium-wrappers API
export const ready: Promise<void> = new Promise((resolve) => resolve());

Expand Down Expand Up @@ -844,6 +862,7 @@ export default {
crypto_sign_detached,
crypto_sign_keypair,
crypto_sign_verify_detached,
crypto_scalarmult,
from_base64,
randombytes_buf,
randombytes_uniform,
Expand Down