Skip to content

Commit a8614e4

Browse files
authored
Merge pull request #73 from serenity-kit/sairanjit-feat/add-methods
Sairanjit feat/add methods
2 parents 2bba41c + 3500eba commit a8614e4

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

cpp/CPPLINT.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ linelength=200
1414
# does not make sense for our project setup since this is not a large cpp codebase.
1515
#
1616
# Disabled runtime/int because uint64_t didn't work with the Android build
17-
filter=-runtime/references,-legal/copyright,-build/include_subdir,-whitespace/braces,-whitespace/newline,-whitespace/ending_newline,-readability/fn_size,-runtime/int,-whitespace/comments
17+
filter=-runtime/references,-legal/copyright,-build/include_subdir,-whitespace/braces,-whitespace/newline,-whitespace/ending_newline,-readability/fn_size,-runtime/int,-whitespace/comments,-whitespace/indent_namespace

cpp/react-native-libsodium.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ namespace ReactNativeLibsodium
213213
jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_kdf_hkdf_sha256_BYTES_MAX", static_cast<int>(crypto_kdf_hkdf_sha256_BYTES_MAX));
214214
jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_kdf_hkdf_sha256_BYTES_MIN", static_cast<int>(crypto_kdf_hkdf_sha256_BYTES_MIN));
215215
jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_kdf_hkdf_sha256_KEYBYTES", static_cast<int>(crypto_kdf_hkdf_sha256_KEYBYTES));
216+
jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_pwhash_ALG_ARGON2ID13", static_cast<int>(crypto_pwhash_ALG_ARGON2ID13));
216217

217218
auto jsi_from_base64_to_arraybuffer = jsi::Function::createFromHostFunction(
218219
jsiRuntime,
@@ -1196,6 +1197,33 @@ namespace ReactNativeLibsodium
11961197

11971198
jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_pwhash", std::move(jsi_crypto_pwhash));
11981199

1200+
auto jsi_crypto_sign_ed25519_pk_to_curve25519 = jsi::Function::createFromHostFunction(
1201+
jsiRuntime,
1202+
jsi::PropNameID::forUtf8(jsiRuntime, "jsi_crypto_sign_ed25519_pk_to_curve25519"),
1203+
1,
1204+
[](jsi::Runtime &runtime, const jsi::Value &thisValue, const jsi::Value *arguments, size_t count) -> jsi::Value
1205+
{
1206+
const std::string functionName = "crypto_sign_ed25519_pk_to_curve25519";
1207+
1208+
std::string publicKeyArgumentName = "publicKey";
1209+
unsigned int publicKeyArgumentPosition = 0;
1210+
validateIsArrayBuffer(functionName, runtime, arguments[publicKeyArgumentPosition], publicKeyArgumentName, true);
1211+
1212+
auto publicKeyArrayBuffer = arguments[publicKeyArgumentPosition].asObject(runtime).getArrayBuffer(runtime);
1213+
1214+
std::vector<uint8_t> publicKey(crypto_sign_PUBLICKEYBYTES);
1215+
int result = -1;
1216+
1217+
result = crypto_sign_ed25519_pk_to_curve25519(
1218+
publicKey.data(),
1219+
publicKeyArrayBuffer.data(runtime));
1220+
1221+
throwOnBadResult(functionName, runtime, result);
1222+
return arrayBufferAsObject(runtime, publicKey);
1223+
});
1224+
1225+
jsiRuntime.global().setProperty(jsiRuntime, "jsi_crypto_sign_ed25519_pk_to_curve25519", std::move(jsi_crypto_sign_ed25519_pk_to_curve25519));
1226+
11991227
auto jsi_crypto_kdf_derive_from_key = jsi::Function::createFromHostFunction(
12001228
jsiRuntime,
12011229
jsi::PropNameID::forUtf8(jsiRuntime, "jsi_crypto_kdf_derive_from_key"),

example/src/components/TestResults.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import '../tests/crypto_generichash_test';
1818
import '../tests/crypto_kdf_derive_from_key_test';
1919
import '../tests/crypto_kdf_keygen_test';
2020
import '../tests/crypto_pwhash_test';
21+
import '../tests/crypto_sign_ed25519_pk_to_curve25519_test';
2122
import '../tests/crypto_secretbox_easy_test';
2223
import '../tests/crypto_secretbox_keygen_test';
2324
import '../tests/crypto_secretbox_open_easy_test';

example/src/tests/constants_test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
crypto_secretbox_KEYBYTES,
1414
crypto_secretbox_NONCEBYTES,
1515
crypto_sign_SEEDBYTES,
16+
crypto_pwhash_ALG_ARGON2ID13,
1617
} from 'react-native-libsodium';
1718
import { expect, test } from '../utils/testRunner';
1819

@@ -31,4 +32,5 @@ test('constants', () => {
3132
expect(_unstable_crypto_kdf_hkdf_sha256_KEYBYTES).toEqual(32);
3233
expect(_unstable_crypto_kdf_hkdf_sha256_BYTES_MIN).toEqual(0);
3334
expect(_unstable_crypto_kdf_hkdf_sha256_BYTES_MAX).toEqual(8160);
35+
expect(crypto_pwhash_ALG_ARGON2ID13).toEqual(2);
3436
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { crypto_sign_ed25519_pk_to_curve25519 } from 'react-native-libsodium';
2+
import { isEqualUint8Array } from '../utils/isEqualUint8Array';
3+
import { expect, test } from '../utils/testRunner';
4+
5+
test('crypto_sign_ed25519_pk_to_curve25519', () => {
6+
const publicKey = new Uint8Array([
7+
38, 187, 152, 175, 122, 23, 12, 100, 83, 68, 221, 23, 158, 24, 170, 13, 234,
8+
4, 53, 212, 90, 147, 161, 67, 243, 45, 175, 177, 59, 239, 38, 65,
9+
]);
10+
11+
expect(
12+
isEqualUint8Array(
13+
crypto_sign_ed25519_pk_to_curve25519(publicKey),
14+
new Uint8Array([
15+
1, 123, 90, 189, 215, 54, 174, 97, 2, 183, 14, 184, 18, 115, 105, 142,
16+
141, 119, 109, 227, 130, 213, 21, 35, 162, 131, 125, 189, 213, 158, 9,
17+
17,
18+
])
19+
)
20+
).toBe(true);
21+
});

src/lib.native.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ declare global {
5151
var jsi_crypto_kdf_hkdf_sha256_BYTES_MAX: number;
5252
var jsi_crypto_kdf_hkdf_sha256_BYTES_MIN: number;
5353
var jsi_crypto_kdf_hkdf_sha256_KEYBYTES: number;
54+
var jsi_crypto_pwhash_ALG_ARGON2ID13: number;
5455

5556
function jsi_crypto_auth(
5657
message: string | ArrayBuffer,
@@ -145,6 +146,9 @@ declare global {
145146
memLimit: number,
146147
algorithm: number
147148
): ArrayBuffer;
149+
function jsi_crypto_sign_ed25519_pk_to_curve25519(
150+
publicKey: ArrayBuffer
151+
): ArrayBuffer;
148152
function jsi_crypto_kdf_derive_from_key(
149153
subkeyLength: number,
150154
subkeyId: number,
@@ -221,6 +225,8 @@ export const _unstable_crypto_kdf_hkdf_sha256_BYTES_MIN =
221225
global.jsi_crypto_kdf_hkdf_sha256_BYTES_MIN;
222226
export const _unstable_crypto_kdf_hkdf_sha256_KEYBYTES =
223227
global.jsi_crypto_kdf_hkdf_sha256_KEYBYTES;
228+
export const crypto_pwhash_ALG_ARGON2ID13 =
229+
global.jsi_crypto_pwhash_ALG_ARGON2ID13;
224230

225231
export const from_base64 = (
226232
input: string,
@@ -685,6 +691,15 @@ export function crypto_pwhash(
685691
);
686692
return convertToOutputFormat(result, outputFormat);
687693
}
694+
export function crypto_sign_ed25519_pk_to_curve25519(
695+
publicKey: Uint8Array,
696+
outputFormat?: Uint8ArrayOutputFormat | null
697+
) {
698+
const result = global.jsi_crypto_sign_ed25519_pk_to_curve25519(
699+
publicKey.buffer
700+
);
701+
return convertToOutputFormat(result, outputFormat);
702+
}
688703

689704
export function crypto_kdf_derive_from_key(
690705
subkey_len: number,
@@ -857,12 +872,14 @@ export default {
857872
crypto_kdf_KEYBYTES,
858873
crypto_kdf_keygen,
859874
crypto_pwhash,
875+
crypto_sign_ed25519_pk_to_curve25519,
860876
crypto_pwhash_ALG_DEFAULT,
861877
crypto_pwhash_BYTES_MAX,
862878
crypto_pwhash_BYTES_MIN,
863879
crypto_pwhash_MEMLIMIT_INTERACTIVE,
864880
crypto_pwhash_OPSLIMIT_INTERACTIVE,
865881
crypto_pwhash_SALTBYTES,
882+
crypto_pwhash_ALG_ARGON2ID13,
866883
crypto_secretbox_easy,
867884
crypto_secretbox_KEYBYTES,
868885
crypto_secretbox_keygen,

0 commit comments

Comments
 (0)