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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,35 @@ import {
crypto_generichash_KEYBYTES,
crypto_generichash_KEYBYTES_MIN,
crypto_generichash_KEYBYTES_MAX,
crypto_hash,
crypto_hash_BYTES,
crypto_hash_sha256,
crypto_hash_sha256_BYTES,
crypto_hash_sha512,
crypto_hash_sha512_BYTES,
crypto_secretbox_easy,
crypto_secretbox_KEYBYTES,
crypto_secretbox_keygen,
crypto_secretbox_NONCEBYTES,
crypto_secretbox_open_easy,
crypto_secretstream_xchacha20poly1305_ABYTES,
crypto_secretstream_xchacha20poly1305_HEADERBYTES,
crypto_secretstream_xchacha20poly1305_init_pull,
crypto_secretstream_xchacha20poly1305_init_push,
crypto_secretstream_xchacha20poly1305_KEYBYTES,
crypto_secretstream_xchacha20poly1305_keygen,
crypto_secretstream_xchacha20poly1305_pull,
crypto_secretstream_xchacha20poly1305_push,
crypto_secretstream_xchacha20poly1305_TAG_FINAL,
crypto_secretstream_xchacha20poly1305_TAG_MESSAGE,
crypto_secretstream_xchacha20poly1305_TAG_PUSH,
crypto_secretstream_xchacha20poly1305_TAG_REKEY,
crypto_sign_detached,
crypto_sign_keypair,
crypto_sign_SEEDBYTES,
crypto_sign_verify_detached,
from_base64,
memzero,
randombytes_buf,
randombytes_uniform,
to_base64,
Expand Down
504 changes: 492 additions & 12 deletions cpp/react-native-libsodium.cpp

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions example/src/components/TestResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ import '../tests/crypto_box_open_easy_test';
import '../tests/crypto_box_seal_test';
import '../tests/crypto_box_seed_keypair_test';
import '../tests/crypto_generichash_test';
import '../tests/crypto_hash_sha256_test';
import '../tests/crypto_hash_sha512_test';
import '../tests/crypto_hash_test';
import '../tests/crypto_kdf_derive_from_key_test';
import '../tests/crypto_kdf_keygen_test';
import '../tests/crypto_pwhash_test';
import '../tests/crypto_sign_ed25519_pk_to_curve25519_test';
import '../tests/crypto_secretbox_easy_test';
import '../tests/crypto_secretbox_keygen_test';
import '../tests/crypto_secretbox_open_easy_test';
import '../tests/crypto_secretstream_xchacha20poly1305_test';
import '../tests/crypto_sign_detached_test';
import '../tests/crypto_sign_keypair_test';
import '../tests/crypto_sign_seed_keypair_test';
import '../tests/crypto_sign_verify_detached_test';
import '../tests/from_base64_test';
import '../tests/memzero_test';
import '../tests/randombytes_buf_test';
import '../tests/randombytes_uniform_test';
import '../tests/to_base64_test';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
crypto_aead_xchacha20poly1305_ietf_KEYBYTES,
crypto_aead_xchacha20poly1305_ietf_NPUBBYTES,
crypto_aead_xchacha20poly1305_ietf_decrypt,
crypto_aead_xchacha20poly1305_ietf_encrypt,
crypto_aead_xchacha20poly1305_ietf_keygen,
from_string,
randombytes_buf,
Expand Down Expand Up @@ -120,3 +121,94 @@ test('crypto_aead_xchacha20poly1305_ietf_decrypt', () => {
);
}).toThrow();
});

test('crypto_aead_xchacha20poly1305_ietf_decrypt with Uint8Array and null additional_data', () => {
const message = 'Hello, world!';
const key = new Uint8Array([
108, 17, 177, 237, 16, 132, 96, 213, 10, 50, 109, 157, 209, 207, 131, 239,
199, 127, 249, 166, 146, 48, 155, 115, 190, 244, 210, 252, 219, 38, 200,
159,
]);
const publicNonce = new Uint8Array([
137, 27, 59, 167, 152, 253, 53, 78, 125, 80, 246, 158, 107, 239, 217, 210,
3, 212, 219, 223, 63, 14, 97, 107,
]);
// the ciphertext of 'Hello, world!' created with the string additional_data
// 'additional data'
const ciphertext = new Uint8Array([
249, 165, 41, 20, 8, 68, 254, 59, 157, 166, 196, 51, 98, 212, 168, 126, 136,
102, 109, 38, 148, 139, 198, 4, 142, 86, 112, 89, 239,
]);
// the UTF-8 bytes of the string 'additional data'
const additionalDataBytes = new Uint8Array([
97, 100, 100, 105, 116, 105, 111, 110, 97, 108, 32, 100, 97, 116, 97,
]);

// a Uint8Array additional_data must authenticate the exact same bytes as
// its UTF-8 string equivalent
expect(
to_string(
crypto_aead_xchacha20poly1305_ietf_decrypt(
null,
ciphertext,
additionalDataBytes,
publicNonce,
key
)
)
).toEqual(message);

// round-trip with a Uint8Array additional_data that is no valid UTF-8
const roundTripKey = crypto_aead_xchacha20poly1305_ietf_keygen();
const roundTripNonce = randombytes_buf(
crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
);
const roundTripAdditionalData = new Uint8Array([0, 255, 254, 1, 128, 7]);
const roundTripCiphertext = crypto_aead_xchacha20poly1305_ietf_encrypt(
message,
roundTripAdditionalData,
null,
roundTripNonce,
roundTripKey
);
expect(
to_string(
crypto_aead_xchacha20poly1305_ietf_decrypt(
null,
roundTripCiphertext,
roundTripAdditionalData,
roundTripNonce,
roundTripKey
)
)
).toEqual(message);

// a single tampered additional_data byte (first byte flipped from 0 to 1)
// must fail to decrypt
const tamperedAdditionalData = new Uint8Array([1, 255, 254, 1, 128, 7]);
expect(() => {
crypto_aead_xchacha20poly1305_ietf_decrypt(
null,
roundTripCiphertext,
tamperedAdditionalData,
roundTripNonce,
roundTripKey
);
}).toThrow();

// null additional_data
expect(
to_string(
crypto_aead_xchacha20poly1305_ietf_decrypt(
null,
new Uint8Array([
249, 165, 41, 20, 8, 68, 254, 59, 157, 166, 196, 51, 98, 245, 181,
152, 162, 160, 8, 101, 170, 191, 221, 127, 9, 8, 14, 197, 128,
]),
null,
publicNonce,
key
)
)
).toEqual(message);
});
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,55 @@ test('crypto_aead_xchacha20poly1305_ietf_encrypt', () => {
);
}).toThrow();
});

test('crypto_aead_xchacha20poly1305_ietf_encrypt with Uint8Array and null additional_data', () => {
const message = 'Hello, world!';
const key = new Uint8Array([
108, 17, 177, 237, 16, 132, 96, 213, 10, 50, 109, 157, 209, 207, 131, 239,
199, 127, 249, 166, 146, 48, 155, 115, 190, 244, 210, 252, 219, 38, 200,
159,
]);
const publicNonce = new Uint8Array([
137, 27, 59, 167, 152, 253, 53, 78, 125, 80, 246, 158, 107, 239, 217, 210,
3, 212, 219, 223, 63, 14, 97, 107,
]);
// the UTF-8 bytes of the string 'additional data'
const additionalDataBytes = new Uint8Array([
97, 100, 100, 105, 116, 105, 111, 110, 97, 108, 32, 100, 97, 116, 97,
]);

// a Uint8Array additional_data must authenticate the exact same bytes as
// its UTF-8 string equivalent and therefore produce the same ciphertext
expect(
crypto_aead_xchacha20poly1305_ietf_encrypt(
message,
additionalDataBytes,
null,
publicNonce,
key
)
).toEqual(
crypto_aead_xchacha20poly1305_ietf_encrypt(
message,
'additional data',
null,
publicNonce,
key
)
);

expect(
crypto_aead_xchacha20poly1305_ietf_encrypt(
message,
null,
null,
publicNonce,
key
)
).toEqual(
new Uint8Array([
249, 165, 41, 20, 8, 68, 254, 59, 157, 166, 196, 51, 98, 245, 181, 152,
162, 160, 8, 101, 170, 191, 221, 127, 9, 8, 14, 197, 128,
])
);
});
28 changes: 28 additions & 0 deletions example/src/tests/crypto_hash_sha256_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
crypto_hash_sha256,
crypto_hash_sha256_BYTES,
to_hex,
} from 'react-native-libsodium';
import { expect, test } from '../utils/testRunner';

test('crypto_hash_sha256', () => {
// FIPS 180-4 SHA-256 test vector for "abc"
const abcHash =
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';

expect(crypto_hash_sha256('abc').length).toEqual(crypto_hash_sha256_BYTES);
expect(to_hex(crypto_hash_sha256('abc'))).toBe(abcHash);
expect(to_hex(crypto_hash_sha256(new Uint8Array([97, 98, 99])))).toBe(
abcHash
);
expect(crypto_hash_sha256('abc', 'hex')).toBe(abcHash);

// FIPS 180-4 SHA-256 test vector for the two-block message
expect(
to_hex(
crypto_hash_sha256(
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
)
)
).toBe('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1');
});
23 changes: 23 additions & 0 deletions example/src/tests/crypto_hash_sha512_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
crypto_hash,
crypto_hash_sha512,
crypto_hash_sha512_BYTES,
to_hex,
} from 'react-native-libsodium';
import { expect, test } from '../utils/testRunner';

test('crypto_hash_sha512', () => {
// FIPS 180-4 SHA-512 test vector for "abc"
const abcHash =
'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f';

expect(crypto_hash_sha512('abc').length).toEqual(crypto_hash_sha512_BYTES);
expect(to_hex(crypto_hash_sha512('abc'))).toBe(abcHash);
expect(to_hex(crypto_hash_sha512(new Uint8Array([97, 98, 99])))).toBe(
abcHash
);
expect(crypto_hash_sha512('abc', 'hex')).toBe(abcHash);

// crypto_hash is SHA-512, so both must produce the same digest
expect(crypto_hash_sha512('abc')).toEqual(crypto_hash('abc'));
});
24 changes: 24 additions & 0 deletions example/src/tests/crypto_hash_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { crypto_hash, crypto_hash_BYTES, to_hex } from 'react-native-libsodium';
import { expect, test } from '../utils/testRunner';

test('crypto_hash', () => {
// FIPS 180-4 SHA-512 test vector for "abc"
const abcHash =
'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f';

expect(crypto_hash('abc').length).toEqual(crypto_hash_BYTES);
expect(to_hex(crypto_hash('abc'))).toBe(abcHash);
expect(to_hex(crypto_hash(new Uint8Array([97, 98, 99])))).toBe(abcHash);
expect(crypto_hash('abc', 'hex')).toBe(abcHash);

// FIPS 180-4 SHA-512 test vector for the two-block message
expect(
to_hex(
crypto_hash(
'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'
)
)
).toBe(
'8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909'
);
});
Loading