Skip to content

Commit b274004

Browse files
ronickgrenanmav
andauthored
Feat/allow enable disable sodium (#702)
Co-authored-by: Renan Mav <renan.mav@hotmail.com>
1 parent 5cccdae commit b274004

9 files changed

Lines changed: 88 additions & 37 deletions

File tree

example/android/gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ newArchEnabled=true
3939
# Use this property to enable or disable the Hermes JS engine.
4040
# If set to false, you will be using JSC instead.
4141
hermesEnabled=true
42+
43+
# Use this property to enable or disable the libsodium support.
44+
sodiumEnabled=true

example/ios/Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ENV['RCT_NEW_ARCH_ENABLED'] = '1'
2+
ENV['SODIUM_ENABLED'] = '1'
23

34
# Resolve react_native_pods.rb with node to allow for hoisting
45
require Pod::Executable.execute_command('node', ['-p',

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,7 @@ SPEC CHECKSUMS:
19951995
hermes-engine: 9e868dc7be781364296d6ee2f56d0c1a9ef0bb11
19961996
NitroModules: fdc6fcf8f397091615951004ae81022b759e27bc
19971997
OpenSSL-Universal: 6082b0bf950e5636fe0d78def171184e2b3899c2
1998-
QuickCrypto: 283efc5368936a4731c55b5a6409588d550dd0b1
1998+
QuickCrypto: e8b3449a68c118319ce3bbf4f570547029342a16
19991999
RCT-Folly: ea9d9256ba7f9322ef911169a9f696e5857b9e17
20002000
RCTDeprecation: ebe712bb05077934b16c6bf25228bdec34b64f83
20012001
RCTRequired: ca91e5dd26b64f577b528044c962baf171c6b716
@@ -2059,6 +2059,6 @@ SPEC CHECKSUMS:
20592059
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
20602060
Yoga: feb4910aba9742cfedc059e2b2902e22ffe9954a
20612061

2062-
PODFILE CHECKSUM: bcfd7840a8f657993e5e9504fdac2d1397cbc14a
2062+
PODFILE CHECKSUM: 63c4c1ca9d3ee6394f468b17e19efd3e548b5357
20632063

2064-
COCOAPODS: 1.16.2
2064+
COCOAPODS: 1.15.2

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"format": "prettier --check \"**/*.{js,ts,tsx}\"",
1616
"format:fix": "prettier --write \"**/*.{js,ts,tsx}\"",
1717
"start": "react-native start",
18-
"pods": "RCT_NEW_ARCH_ENABLED=1 bundle exec pod install --project-directory=ios",
18+
"pods": "bundle exec pod install --project-directory=ios",
1919
"build:android": "cd android && ./gradlew assembleDebug --no-daemon --console=plain -PreactNativeArchitectures=arm64-v8a",
2020
"build:ios": "cd ios && xcodebuild -workspace QuickCrytpExample.xcworkspace -scheme QuickCrytpExample -configuration Debug -sdk iphonesimulator CC=clang CPLUSPLUS=clang++ LD=clang LDPLUSPLUS=clang++ GCC_OPTIMIZATION_LEVEL=0 GCC_PRECOMPILE_PREFIX_HEADER=YES ASSETCATALOG_COMPILER_OPTIMIZATION=time DEBUG_INFORMATION_FORMAT=dwarf COMPILER_INDEX_STORE_ENABLE=NO"
2121
},

packages/react-native-quick-crypto/QuickCrypto.podspec

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,43 @@ Pod::Spec.new do |s|
1919

2020
s.source = { :git => "https://github.com/margelo/react-native-quick-crypto.git", :tag => "#{s.version}" }
2121

22-
# cocoapod for Sodium has not been updated for a while, so we need to build it ourselves
23-
# https://github.com/jedisct1/swift-sodium/issues/264#issuecomment-2864963850
24-
s.prepare_command = <<-CMD
25-
# Create ios directory if it doesn't exist
26-
mkdir -p ios
22+
sodium_enabled = ENV['SODIUM_ENABLED'] == '1'
23+
Pod::UI.puts("[QuickCrypto] Has libsodium #{sodium_enabled ? "enabled" : "disabled"}!")
2724

28-
# Download libsodium
29-
curl -L -s -o ios/libsodium.tar.gz https://download.libsodium.org/libsodium/releases/libsodium-1.0.20-stable.tar.gz
25+
if sodium_enabled
26+
# cocoapod for Sodium has not been updated for a while, so we need to build it ourselves
27+
# https://github.com/jedisct1/swift-sodium/issues/264#issuecomment-2864963850
28+
s.prepare_command = <<-CMD
29+
# Create ios directory if it doesn't exist
30+
mkdir -p ios
3031
31-
# Clean previous extraction
32-
rm -rf ios/libsodium-stable
32+
# Download libsodium
33+
curl -L -s -o ios/libsodium.tar.gz https://download.libsodium.org/libsodium/releases/libsodium-1.0.20-stable.tar.gz
3334
34-
# Extract the full tarball
35-
tar -xzf ios/libsodium.tar.gz -C ios
35+
# Clean previous extraction
36+
rm -rf ios/libsodium-stable
3637
37-
# Run configure and make to generate all headers including private ones
38-
cd ios/libsodium-stable && \
39-
./configure --disable-shared --enable-static && \
40-
make -j$(sysctl -n hw.ncpu)
38+
# Extract the full tarball
39+
tar -xzf ios/libsodium.tar.gz -C ios
4140
42-
# Cleanup
43-
cd ../../
44-
rm -f ios/libsodium.tar.gz
45-
CMD
41+
# Run configure and make to generate all headers including private ones
42+
cd ios/libsodium-stable && \
43+
./configure --disable-shared --enable-static && \
44+
make -j$(sysctl -n hw.ncpu)
4645
47-
s.source_files = [
46+
# Cleanup
47+
cd ../../
48+
rm -f ios/libsodium.tar.gz
49+
CMD
50+
else
51+
s.prepare_command = <<-CMD
52+
# Clean up libsodium files if they exist
53+
rm -rf ios/libsodium-stable
54+
rm -f ios/libsodium.tar.gz
55+
CMD
56+
end
57+
58+
base_source_files = [
4859
# implementation (Swift)
4960
"ios/**/*.{swift}",
5061
# ios (Objective-C++)
@@ -55,25 +66,35 @@ Pod::Spec.new do |s|
5566
"deps/**/*.{hpp,cpp}",
5667
# dependencies (C)
5768
"deps/**/*.{h,c}",
58-
# libsodium sources
59-
"ios/libsodium-stable/src/libsodium/**/*.{h,c}"
6069
]
6170

62-
s.pod_target_xcconfig = {
71+
if sodium_enabled
72+
base_source_files += ["ios/libsodium-stable/src/libsodium/**/*.{h,c}"]
73+
end
74+
75+
s.source_files = base_source_files
76+
77+
xcconfig = {
6378
# C++ compiler flags, mainly for folly.
6479
"GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) FOLLY_NO_CONFIG FOLLY_CFG_NO_COROUTINES",
6580
# Set C++ standard to C++20
6681
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
67-
# Header search paths for vendored libsodium
68-
"HEADER_SEARCH_PATHS" => [
82+
"CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES" => "YES"
83+
}
84+
85+
if sodium_enabled
86+
sodium_headers = [
6987
"\"$(PODS_TARGET_SRCROOT)/ios/libsodium-stable/src/libsodium/include\"",
7088
"\"$(PODS_TARGET_SRCROOT)/ios/libsodium-stable/src/libsodium/include/sodium\"",
7189
"\"$(PODS_TARGET_SRCROOT)/ios/libsodium-stable\"",
7290
"\"$(PODS_ROOT)/../../packages/react-native-quick-crypto/ios/libsodium-stable/src/libsodium/include\"",
7391
"\"$(PODS_ROOT)/../../packages/react-native-quick-crypto/ios/libsodium-stable/src/libsodium/include/sodium\""
74-
].join(' '),
75-
"CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES" => "YES"
76-
}
92+
]
93+
xcconfig["HEADER_SEARCH_PATHS"] = sodium_headers.join(' ')
94+
xcconfig["GCC_PREPROCESSOR_DEFINITIONS"] = "$(inherited) BLSALLOC_SODIUM=1"
95+
end
96+
97+
s.pod_target_xcconfig = xcconfig
7798

7899
# Add all files generated by Nitrogen
79100
load "nitrogen/generated/ios/QuickCrypto+autolinking.rb"

packages/react-native-quick-crypto/android/CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,24 @@ include_directories(
4141
find_library(LOG_LIB log)
4242

4343
find_package(openssl REQUIRED CONFIG)
44-
find_package(sodium REQUIRED CONFIG)
4544

4645
# Link all libraries together
4746
target_link_libraries(
4847
${PACKAGE_NAME}
4948
${LOG_LIB} # <-- Logcat logger
5049
android # <-- Android core
5150
openssl::crypto # <-- OpenSSL (Crypto)
52-
sodium::sodium # <-- libsodium (Crypto)
5351
)
5452

53+
if(SODIUM_ENABLED)
54+
add_definitions(-DBLSALLOC_SODIUM)
55+
find_package(sodium REQUIRED CONFIG)
56+
target_link_libraries(
57+
${PACKAGE_NAME}
58+
sodium::sodium
59+
)
60+
endif()
61+
5562
if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
5663
target_link_libraries(
5764
${PACKAGE_NAME}

packages/react-native-quick-crypto/android/build.gradle

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ def getExtOrIntegerDefault(name) {
3737
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["QuickCrypto_" + name]).toInteger()
3838
}
3939

40+
def sodiumEnabled = hasProperty('sodiumEnabled') ? project.property('sodiumEnabled').toBoolean() : false // Default to false
41+
logger.warn("[QuickCrypto] Has libsodium ${sodiumEnabled ? "enabled" : "disabled"}!")
42+
4043
android {
4144
namespace "com.margelo.nitro.quickcrypto"
4245
ndkVersion getExtOrDefault("ndkVersion")
@@ -50,7 +53,7 @@ android {
5053
externalNativeBuild {
5154
cmake {
5255
cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
53-
arguments "-DANDROID_STL=c++_shared"
56+
arguments "-DANDROID_STL=c++_shared", "-DSODIUM_ENABLED=${sodiumEnabled}"
5457
abiFilters (*reactNativeArchitectures())
5558

5659
buildTypes {
@@ -140,8 +143,10 @@ dependencies {
140143
// Add a dependency on OpenSSL
141144
implementation 'io.github.ronickg:openssl:3.3.2'
142145

143-
// Add a dependency on libsodium
144-
implementation 'io.github.ronickg:sodium:1.0.20'
146+
if (sodiumEnabled) {
147+
// Add a dependency on libsodium
148+
implementation 'io.github.ronickg:sodium:1.0.20'
149+
}
145150
}
146151

147152
if (isNewArchitectureEnabled()) {

packages/react-native-quick-crypto/cpp/cipher/XSalsa20Cipher.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,28 @@ void XSalsa20Cipher::init(const std::shared_ptr<ArrayBuffer> cipher_key, const s
3434
* xsalsa20 call to sodium implementation
3535
*/
3636
std::shared_ptr<ArrayBuffer> XSalsa20Cipher::update(const std::shared_ptr<ArrayBuffer>& data) {
37+
#ifndef BLSALLOC_SODIUM
38+
throw std::runtime_error("XSalsa20Cipher: libsodium must be enabled to use this cipher (BLSALLOC_SODIUM is not defined).");
39+
#else
3740
auto native_data = ToNativeArrayBuffer(data);
3841
auto output = new uint8_t[native_data->size()];
3942
int result = crypto_stream_xor(output, native_data->data(), native_data->size(), nonce, key);
4043
if (result != 0) {
4144
throw std::runtime_error("XSalsa20Cipher: Failed to update");
4245
}
4346
return std::make_shared<NativeArrayBuffer>(output, native_data->size(), [=]() { delete[] output; });
47+
#endif
4448
}
4549

4650
/**
4751
* xsalsa20 does not have a final step, returns empty buffer
4852
*/
4953
std::shared_ptr<ArrayBuffer> XSalsa20Cipher::final() {
54+
#ifndef BLSALLOC_SODIUM
55+
throw std::runtime_error("XSalsa20Cipher: libsodium must be enabled to use this cipher (BLSALLOC_SODIUM is not defined).");
56+
#else
5057
return std::make_shared<NativeArrayBuffer>(nullptr, 0, nullptr);
58+
#endif
5159
}
5260

5361
} // namespace margelo::nitro::crypto

packages/react-native-quick-crypto/cpp/cipher/XSalsa20Cipher.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#pragma once
22

3+
#if BLSALLOC_SODIUM
34
#include "sodium.h"
5+
#else
6+
// Define XSalsa20 constants when sodium is disabled (for compilation purposes)
7+
#define crypto_stream_KEYBYTES 32 // XSalsa20 key size (32 bytes)
8+
#define crypto_stream_NONCEBYTES 24 // XSalsa20 nonce size (24 bytes)
9+
#endif
410

511
#include "HybridCipher.hpp"
612
#include "NitroModules/ArrayBuffer.hpp"

0 commit comments

Comments
 (0)