react-native-quick-crypto 1.0.0-beta.1 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (173) hide show
  1. package/QuickCrypto.podspec +17 -4
  2. package/README.md +172 -0
  3. package/android/CMakeLists.txt +28 -17
  4. package/android/build.gradle +36 -3
  5. package/android/src/main/cpp/cpp-adapter.cpp +3 -10
  6. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +15 -10
  7. package/cpp/ed25519/HybridEdKeyPair.cpp +268 -0
  8. package/cpp/ed25519/HybridEdKeyPair.hpp +85 -0
  9. package/cpp/pbkdf2/HybridPbkdf2.cpp +72 -0
  10. package/cpp/pbkdf2/HybridPbkdf2.hpp +35 -0
  11. package/cpp/random/HybridRandom.cpp +59 -0
  12. package/cpp/random/HybridRandom.hpp +33 -0
  13. package/cpp/utils/Utils.hpp +20 -0
  14. package/deps/fastpbkdf2/fastpbkdf2.c +352 -0
  15. package/deps/fastpbkdf2/fastpbkdf2.h +68 -0
  16. package/lib/commonjs/ed.js +42 -0
  17. package/lib/commonjs/ed.js.map +1 -0
  18. package/lib/commonjs/index.js +61 -10
  19. package/lib/commonjs/index.js.map +1 -1
  20. package/lib/commonjs/keys/classes.js +191 -0
  21. package/lib/commonjs/keys/classes.js.map +1 -0
  22. package/lib/commonjs/keys/generateKeyPair.js +148 -0
  23. package/lib/commonjs/keys/generateKeyPair.js.map +1 -0
  24. package/lib/commonjs/keys/index.js +62 -0
  25. package/lib/commonjs/keys/index.js.map +1 -0
  26. package/lib/commonjs/keys/signVerify.js +41 -0
  27. package/lib/commonjs/keys/signVerify.js.map +1 -0
  28. package/lib/commonjs/keys/utils.js +118 -0
  29. package/lib/commonjs/keys/utils.js.map +1 -0
  30. package/lib/commonjs/pbkdf2.js +89 -0
  31. package/lib/commonjs/pbkdf2.js.map +1 -0
  32. package/lib/commonjs/random.js +3 -3
  33. package/lib/commonjs/random.js.map +1 -1
  34. package/lib/commonjs/specs/edKeyPair.nitro.js +6 -0
  35. package/lib/commonjs/specs/edKeyPair.nitro.js.map +1 -0
  36. package/lib/commonjs/specs/keyObjectHandle.nitro.js +6 -0
  37. package/lib/commonjs/specs/keyObjectHandle.nitro.js.map +1 -0
  38. package/lib/commonjs/specs/pbkdf2.nitro.js +6 -0
  39. package/lib/commonjs/specs/pbkdf2.nitro.js.map +1 -0
  40. package/lib/commonjs/utils/conversion.js +101 -6
  41. package/lib/commonjs/utils/conversion.js.map +1 -1
  42. package/lib/commonjs/utils/errors.js +14 -0
  43. package/lib/commonjs/utils/errors.js.map +1 -0
  44. package/lib/commonjs/utils/hashnames.js +90 -0
  45. package/lib/commonjs/utils/hashnames.js.map +1 -0
  46. package/lib/commonjs/utils/index.js +54 -5
  47. package/lib/commonjs/utils/index.js.map +1 -1
  48. package/lib/commonjs/utils/types.js +38 -0
  49. package/lib/commonjs/utils/types.js.map +1 -1
  50. package/lib/commonjs/utils/validation.js +25 -0
  51. package/lib/commonjs/utils/validation.js.map +1 -0
  52. package/lib/module/ed.js +37 -0
  53. package/lib/module/ed.js.map +1 -0
  54. package/lib/module/index.js +16 -9
  55. package/lib/module/index.js.map +1 -1
  56. package/lib/module/keys/classes.js +182 -0
  57. package/lib/module/keys/classes.js.map +1 -0
  58. package/lib/module/keys/generateKeyPair.js +148 -0
  59. package/lib/module/keys/generateKeyPair.js.map +1 -0
  60. package/lib/module/keys/index.js +29 -0
  61. package/lib/module/keys/index.js.map +1 -0
  62. package/lib/module/keys/signVerify.js +41 -0
  63. package/lib/module/keys/signVerify.js.map +1 -0
  64. package/lib/module/keys/utils.js +110 -0
  65. package/lib/module/keys/utils.js.map +1 -0
  66. package/lib/module/pbkdf2.js +83 -0
  67. package/lib/module/pbkdf2.js.map +1 -0
  68. package/lib/module/random.js +1 -1
  69. package/lib/module/random.js.map +1 -1
  70. package/lib/module/specs/edKeyPair.nitro.js +4 -0
  71. package/lib/module/specs/edKeyPair.nitro.js.map +1 -0
  72. package/lib/module/specs/keyObjectHandle.nitro.js +4 -0
  73. package/lib/module/specs/keyObjectHandle.nitro.js.map +1 -0
  74. package/lib/module/specs/pbkdf2.nitro.js +4 -0
  75. package/lib/module/specs/pbkdf2.nitro.js.map +1 -0
  76. package/lib/module/utils/conversion.js +99 -8
  77. package/lib/module/utils/conversion.js.map +1 -1
  78. package/lib/module/utils/errors.js +10 -0
  79. package/lib/module/utils/errors.js.map +1 -0
  80. package/lib/module/utils/hashnames.js +88 -0
  81. package/lib/module/utils/hashnames.js.map +1 -0
  82. package/lib/module/utils/index.js +5 -5
  83. package/lib/module/utils/index.js.map +1 -1
  84. package/lib/module/utils/types.js +40 -0
  85. package/lib/module/utils/types.js.map +1 -1
  86. package/lib/module/utils/validation.js +19 -0
  87. package/lib/module/utils/validation.js.map +1 -0
  88. package/lib/tsconfig.tsbuildinfo +1 -1
  89. package/lib/typescript/ed.d.ts +17 -0
  90. package/lib/typescript/ed.d.ts.map +1 -0
  91. package/lib/typescript/index.d.ts +50 -9
  92. package/lib/typescript/index.d.ts.map +1 -1
  93. package/lib/typescript/keys/classes.d.ts +38 -0
  94. package/lib/typescript/keys/classes.d.ts.map +1 -0
  95. package/lib/typescript/keys/generateKeyPair.d.ts +1 -0
  96. package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -0
  97. package/lib/typescript/keys/index.d.ts +4 -0
  98. package/lib/typescript/keys/index.d.ts.map +1 -0
  99. package/lib/typescript/keys/signVerify.d.ts +1 -0
  100. package/lib/typescript/keys/signVerify.d.ts.map +1 -0
  101. package/lib/typescript/keys/utils.d.ts +32 -0
  102. package/lib/typescript/keys/utils.d.ts.map +1 -0
  103. package/lib/typescript/pbkdf2.d.ts +12 -0
  104. package/lib/typescript/pbkdf2.d.ts.map +1 -0
  105. package/lib/typescript/random.d.ts +5 -5
  106. package/lib/typescript/random.d.ts.map +1 -1
  107. package/lib/typescript/specs/edKeyPair.nitro.d.ts +16 -0
  108. package/lib/typescript/specs/edKeyPair.nitro.d.ts.map +1 -0
  109. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts +15 -0
  110. package/lib/typescript/specs/keyObjectHandle.nitro.d.ts.map +1 -0
  111. package/lib/typescript/specs/pbkdf2.nitro.d.ts +9 -0
  112. package/lib/typescript/specs/pbkdf2.nitro.d.ts.map +1 -0
  113. package/lib/typescript/utils/conversion.d.ts +23 -2
  114. package/lib/typescript/utils/conversion.d.ts.map +1 -1
  115. package/lib/typescript/utils/errors.d.ts +7 -0
  116. package/lib/typescript/utils/errors.d.ts.map +1 -0
  117. package/lib/typescript/utils/hashnames.d.ts +11 -0
  118. package/lib/typescript/utils/hashnames.d.ts.map +1 -0
  119. package/lib/typescript/utils/index.d.ts +5 -5
  120. package/lib/typescript/utils/index.d.ts.map +1 -1
  121. package/lib/typescript/utils/types.d.ts +158 -1
  122. package/lib/typescript/utils/types.d.ts.map +1 -1
  123. package/lib/typescript/utils/validation.d.ts +8 -0
  124. package/lib/typescript/utils/validation.d.ts.map +1 -0
  125. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +65 -0
  126. package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +27 -0
  127. package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +64 -0
  128. package/nitrogen/generated/android/QuickCryptoOnLoad.hpp +25 -0
  129. package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +58 -0
  130. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +17 -0
  131. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +27 -0
  132. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +38 -0
  133. package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +55 -0
  134. package/nitrogen/generated/ios/QuickCryptoAutolinking.swift +12 -0
  135. package/nitrogen/generated/shared/c++/CFRGKeyPairType.hpp +86 -0
  136. package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.cpp +29 -0
  137. package/nitrogen/generated/shared/c++/HybridEdKeyPairSpec.hpp +74 -0
  138. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.cpp +27 -0
  139. package/nitrogen/generated/shared/c++/HybridKeyObjectHandleSpec.hpp +93 -0
  140. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.cpp +22 -0
  141. package/nitrogen/generated/shared/c++/HybridPbkdf2Spec.hpp +66 -0
  142. package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +22 -0
  143. package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +65 -0
  144. package/nitrogen/generated/shared/c++/JWK.hpp +162 -0
  145. package/nitrogen/generated/shared/c++/JWKkty.hpp +86 -0
  146. package/nitrogen/generated/shared/c++/JWKuse.hpp +78 -0
  147. package/nitrogen/generated/shared/c++/KFormatType.hpp +65 -0
  148. package/nitrogen/generated/shared/c++/KeyDetail.hpp +93 -0
  149. package/nitrogen/generated/shared/c++/KeyEncoding.hpp +66 -0
  150. package/nitrogen/generated/shared/c++/KeyType.hpp +65 -0
  151. package/nitrogen/generated/shared/c++/KeyUsage.hpp +102 -0
  152. package/nitrogen/generated/shared/c++/NamedCurve.hpp +82 -0
  153. package/package.json +55 -34
  154. package/src/ed.ts +79 -0
  155. package/src/index.ts +15 -9
  156. package/src/keys/classes.ts +211 -0
  157. package/src/keys/generateKeyPair.ts +146 -0
  158. package/src/keys/index.ts +42 -0
  159. package/src/keys/signVerify.ts +39 -0
  160. package/src/keys/utils.ts +184 -0
  161. package/src/pbkdf2.ts +154 -0
  162. package/src/random.ts +19 -23
  163. package/src/specs/edKeyPair.nitro.ts +41 -0
  164. package/src/specs/keyObjectHandle.nitro.ts +32 -0
  165. package/src/specs/pbkdf2.nitro.ts +18 -0
  166. package/src/specs/random.nitro.ts +2 -2
  167. package/src/utils/conversion.ts +116 -9
  168. package/src/utils/errors.ts +15 -0
  169. package/src/utils/hashnames.ts +96 -0
  170. package/src/utils/index.ts +5 -6
  171. package/src/utils/types.ts +263 -3
  172. package/src/utils/validation.ts +35 -0
  173. package/ios/QuickCryptoOnLoad.mm +0 -19
@@ -2,7 +2,7 @@ require "json"
2
2
 
3
3
  package = JSON.parse(File.read(File.join(__dir__, "package.json")))
4
4
 
5
- Pod::UI.puts "[QuickCrypto] crypto just got quicker 💨"
5
+ Pod::UI.puts "[QuickCrypto] 💨 crypto just got quicker"
6
6
 
7
7
  Pod::Spec.new do |s|
8
8
  s.name = "QuickCrypto"
@@ -12,7 +12,11 @@ Pod::Spec.new do |s|
12
12
  s.license = package["license"]
13
13
  s.authors = package["authors"]
14
14
 
15
- s.platforms = { :ios => min_ios_version_supported }
15
+ s.ios.deployment_target = 16.0 # min_ios_version_supported (see https://github.com/Expensify/App/pull/53149#issuecomment-2562786065)
16
+ s.visionos.deployment_target = 1.0
17
+ s.macos.deployment_target = 10.13
18
+ s.tvos.deployment_target = 13.4
19
+
16
20
  s.source = { :git => "https://github.com/margelo/react-native-quick-crypto.git", :tag => "#{s.version}" }
17
21
 
18
22
  s.source_files = [
@@ -22,13 +26,22 @@ Pod::Spec.new do |s|
22
26
  "ios/**/*.{h,m,mm}",
23
27
  # implementation (C++)
24
28
  "cpp/**/*.{hpp,cpp}",
29
+ # dependencies (C++)
30
+ "deps/**/*.{hpp,cpp}",
31
+ # dependencies (C)
32
+ "deps/**/*.{h,c}",
25
33
  ]
26
34
 
35
+ if ENV["USE_FRAMEWORKS"]
36
+ s.dependency "React-Core"
37
+ add_dependency(s, "React-jsinspector", :framework_name => "jsinspector_modern")
38
+ add_dependency(s, "React-rendererconsistency", :framework_name => "React_rendererconsistency")
39
+ end
40
+
27
41
  # Add all files generated by Nitrogen
28
42
  load 'nitrogen/generated/ios/QuickCrypto+autolinking.rb'
29
43
  add_nitrogen_files(s)
30
44
 
31
- install_modules_dependencies(s)
32
45
  s.dependency "OpenSSL-Universal"
33
-
46
+ install_modules_dependencies(s)
34
47
  end
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ <a href="https://margelo.io">
2
+ <img src="./docs/img/banner.svg" width="100%" />
3
+ </a>
4
+
5
+ # ⚡️ react-native-quick-crypto
6
+
7
+ A fast implementation of Node's `crypto` module.
8
+
9
+ > Note: This version `1.x` is undergoing a major refactor, porting to New Architecture, Bridgeless, and [`Nitro Modules`](https://github.com/mrousavy/react-native-nitro) and is incomplete compared to the `0.x` version. Status, as always, will be represented in [implementation-coverage.md](../main/docs/implementation-coverage.md).
10
+
11
+ > Note: Minimum supported version of React Native is `0.75`. If you need to use earlier versions, please use `0.x` versions of this library.
12
+
13
+ ## Features
14
+
15
+ Unlike any other current JS-based polyfills, react-native-quick-crypto is written in C/C++ JSI and provides much greater performance - especially on mobile devices.
16
+ QuickCrypto can be used as a drop-in replacement for your Web3/Crypto apps to speed up common cryptography functions.
17
+
18
+ - 🏎️ Up to 58x faster than all other solutions
19
+ - ⚡️ Lightning fast implementation with pure C++ and JSI, instead of JS
20
+ - 🧪 Well tested in JS and C++ (OpenSSL)
21
+ - 💰 Made for crypto apps and Wallets
22
+ - 🔢 Secure native compiled cryptography
23
+ - 🔁 Easy drop-in replacement for [crypto-browserify](https://github.com/browserify/crypto-browserify) or [react-native-crypto](https://github.com/tradle/react-native-crypto)
24
+
25
+ ## Versions
26
+
27
+ | Version | RN Architecture | Modules |
28
+ | ------- | ------ | ------- |
29
+ | `1.x` | new [->](https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md) | Nitro Modules [->](https://github.com/margelo/react-native-nitro) |
30
+ | `0.x` | old | Bridge & JSI |
31
+
32
+ ## Benchmarks
33
+
34
+ For example, creating a Wallet using ethers.js uses complex algorithms to generate a private-key/mnemonic-phrase pair:
35
+
36
+ ```ts
37
+ const start = performance.now();
38
+ const wallet = ethers.Wallet.createRandom();
39
+ const end = performance.now();
40
+ console.log(`Creating a Wallet took ${end - start} ms.`);
41
+ ```
42
+
43
+ **Without** react-native-quick-crypto 🐢:
44
+
45
+ ```
46
+ Creating a Wallet took 16862 ms
47
+ ```
48
+
49
+ **With** react-native-quick-crypto ⚡️:
50
+
51
+ ```
52
+ Creating a Wallet took 289 ms
53
+ ```
54
+
55
+ ---
56
+
57
+ ## Installation
58
+
59
+ <h3>
60
+ React Native  <a href="#"><img src="./docs/img/react-native.png" height="15" /></a>
61
+ </h3>
62
+
63
+ ```sh
64
+ bun add react-native-quick-crypto react-native-nitro-modules
65
+ cd ios && pod install
66
+ ```
67
+
68
+ <h3>
69
+ Expo  <a href="#"><img src="./docs/img/expo.png" height="12" /></a>
70
+ </h3>
71
+
72
+ ```sh
73
+ expo install react-native-quick-crypto
74
+ expo prebuild
75
+ ```
76
+
77
+ Optional: override `global.Buffer` and `global.crypto` in your application as early as possible for example in index.js.
78
+
79
+ ```ts
80
+ import { install } from 'react-native-quick-crypto';
81
+
82
+ install();
83
+ ```
84
+
85
+ ## Replace `crypto-browserify`
86
+
87
+ If you are using a library that depends on `crypto`, instead of polyfilling it with `crypto-browserify` (or `react-native-crypto`) you can use `react-native-quick-crypto` for a fully native implementation. This way you can get much faster crypto operations with just a single-line change!
88
+
89
+ ### Using metro config
90
+
91
+ Use the [`resolveRequest`](https://facebook.github.io/metro/docs/resolution#resolverequest-customresolver) configuration option in your `metro.config.js`
92
+
93
+ ```js
94
+ config.resolver.resolveRequest = (context, moduleName, platform) => {
95
+ if (moduleName === 'crypto') {
96
+ // when importing crypto, resolve to react-native-quick-crypto
97
+ return context.resolveRequest(
98
+ context,
99
+ 'react-native-quick-crypto',
100
+ platform,
101
+ )
102
+ }
103
+ // otherwise chain to the standard Metro resolver.
104
+ return context.resolveRequest(context, moduleName, platform)
105
+ }
106
+ ```
107
+
108
+ ### Using babel-plugin-module-resolver
109
+
110
+ You need to install `babel-plugin-module-resolver`, it's a babel plugin that will alias any imports in the code with the values you pass to it. It tricks any module that will try to import certain dependencies with the native versions we require for React Native.
111
+
112
+ ```sh
113
+ yarn add --dev babel-plugin-module-resolver
114
+ ```
115
+
116
+ Then, in your `babel.config.js`, add the plugin to swap the `crypto`, `stream` and `buffer` dependencies:
117
+
118
+ ```diff
119
+ module.exports = {
120
+ presets: ['module:metro-react-native-babel-preset'],
121
+ plugins: [
122
+ + [
123
+ + 'module-resolver',
124
+ + {
125
+ + alias: {
126
+ + 'crypto': 'react-native-quick-crypto',
127
+ + 'stream': 'readable-stream',
128
+ + 'buffer': '@craftzdog/react-native-buffer',
129
+ + },
130
+ + },
131
+ + ],
132
+ ...
133
+ ],
134
+ };
135
+ ```
136
+
137
+ Then restart your bundler using `yarn start --reset-cache`.
138
+
139
+ ## Usage
140
+
141
+ For example, to hash a string with SHA256 you can do the following:
142
+
143
+ ```ts
144
+ import QuickCrypto from 'react-native-quick-crypto';
145
+
146
+ const hashed = QuickCrypto.createHash('sha256')
147
+ .update('Damn, Margelo writes hella good software!')
148
+ .digest('hex');
149
+ ```
150
+
151
+ ## Limitations
152
+
153
+ As the library uses JSI for synchronous native methods access, remote debugging (e.g. with Chrome) is no longer possible. Instead, you should use [Flipper](https://fbflipper.com).
154
+
155
+ Not all cryptographic algorithms are supported yet. See the [implementation coverage](./docs/implementation-coverage.md) document for more details. If you need a specific algorithm, please open a `feature request` issue and we'll see what we can do.
156
+
157
+ ## Community Discord
158
+
159
+ [Join the Margelo Community Discord](https://discord.gg/6CSHz2qAvA) to chat about react-native-quick-crypto or other Margelo libraries.
160
+
161
+ ## Adopting at scale
162
+
163
+ react-native-quick-crypto was built at Margelo, an elite app development agency. For enterprise support or other business inquiries, contact us at <a href="mailto:hello@margelo.io?subject=Adopting react-native-quick-crypto at scale">hello@margelo.io</a>!
164
+
165
+ ## Contributing
166
+
167
+ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
168
+
169
+ ## License
170
+
171
+ - react-native-quick-crypto is licensed under MIT.
172
+ - react-native-quick-crypto is heavily inspired by NodeJS Crypto, which is licensed under [nodejs/LICENSE](https://github.com/nodejs/node/blob/main/LICENSE).
@@ -1,8 +1,7 @@
1
+ project(QuickCrypto)
1
2
  cmake_minimum_required(VERSION 3.9.0)
2
3
 
3
- project(QuickCrypto)
4
4
  set(PACKAGE_NAME QuickCrypto)
5
-
6
5
  set(CMAKE_VERBOSE_MAKEFILE ON)
7
6
  set(CMAKE_CXX_STANDARD 20)
8
7
 
@@ -10,17 +9,28 @@ set(CMAKE_CXX_STANDARD 20)
10
9
  add_library(
11
10
  ${PACKAGE_NAME} SHARED
12
11
  src/main/cpp/cpp-adapter.cpp
12
+ ../cpp/ed25519/HybridEdKeyPair.cpp
13
+ ../cpp/pbkdf2/HybridPbkdf2.cpp
13
14
  ../cpp/random/HybridRandom.cpp
15
+ ../deps/fastpbkdf2/fastpbkdf2.c
14
16
  )
15
17
 
16
- include_directories(../cpp)
18
+ # add Nitrogen specs
17
19
  include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/QuickCrypto+autolinking.cmake)
18
20
 
21
+ # local includes
22
+ include_directories(
23
+ "src/main/cpp"
24
+ "../cpp/ed25519"
25
+ "../cpp/pbkdf2"
26
+ "../cpp/random"
27
+ "../cpp/utils"
28
+ "../deps/fastpbkdf2"
29
+ )
30
+
19
31
  # Third party libraries (Prefabs)
20
32
  find_library(LOG_LIB log)
21
33
 
22
- find_package(ReactAndroid REQUIRED CONFIG)
23
- find_package(fbjni REQUIRED CONFIG)
24
34
  find_package(openssl REQUIRED CONFIG)
25
35
 
26
36
  # Link all libraries together
@@ -28,17 +38,18 @@ target_link_libraries(
28
38
  ${PACKAGE_NAME}
29
39
  ${LOG_LIB} # <-- Logcat logger
30
40
  android # <-- Android core
31
- fbjni::fbjni # <-- Facebook C++ JNI helpers
32
41
  openssl::crypto # <-- OpenSSL (Crypto)
33
- ReactAndroid::jsi
34
- ReactAndroid::turbomodulejsijni
35
- ReactAndroid::react_nativemodule_core
36
- ReactAndroid::react_render_core
37
- ReactAndroid::runtimeexecutor
38
- ReactAndroid::fabricjni
39
- ReactAndroid::react_debug
40
- ReactAndroid::react_render_core
41
- ReactAndroid::react_render_componentregistry
42
- ReactAndroid::rrc_view
43
- ReactAndroid::folly_runtime
44
42
  )
43
+
44
+ if(ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
45
+ target_link_libraries(
46
+ ${PACKAGE_NAME}
47
+ ReactAndroid::reactnative # <-- RN: Native Modules umbrella prefab
48
+ )
49
+ else()
50
+ target_link_libraries(
51
+ ${PACKAGE_NAME}
52
+ ReactAndroid::turbomodulejsijni
53
+ ReactAndroid::react_nativemodule_core # <-- RN: React Native native module core
54
+ )
55
+ endif()
@@ -7,7 +7,7 @@ buildscript {
7
7
  }
8
8
 
9
9
  dependencies {
10
- classpath "com.android.tools.build:gradle:8.2.2"
10
+ classpath "com.android.tools.build:gradle:8.7.3"
11
11
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
12
12
  }
13
13
  }
@@ -49,9 +49,18 @@ android {
49
49
 
50
50
  externalNativeBuild {
51
51
  cmake {
52
- cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
52
+ cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
53
53
  arguments "-DANDROID_STL=c++_shared"
54
54
  abiFilters (*reactNativeArchitectures())
55
+
56
+ buildTypes {
57
+ debug {
58
+ cppFlags "-O1 -g"
59
+ }
60
+ release {
61
+ cppFlags "-O2"
62
+ }
63
+ }
55
64
  }
56
65
  }
57
66
  }
@@ -67,6 +76,27 @@ android {
67
76
  prefab true
68
77
  }
69
78
 
79
+ packagingOptions {
80
+ excludes = [
81
+ "META-INF",
82
+ "META-INF/**",
83
+ "**/libc++_shared.so",
84
+ "**/libfbjni.so",
85
+ "**/libjsi.so",
86
+ "**/libfolly_json.so",
87
+ "**/libfolly_runtime.so",
88
+ "**/libglog.so",
89
+ "**/libhermes.so",
90
+ "**/libhermes-executor-debug.so",
91
+ "**/libhermes_executor.so",
92
+ "**/libreactnative.so",
93
+ "**/libreactnativejni.so",
94
+ "**/libturbomodulejsijni.so",
95
+ "**/libreact_nativemodule_core.so",
96
+ "**/libjscexecutor.so"
97
+ ]
98
+ }
99
+
70
100
  buildTypes {
71
101
  release {
72
102
  minifyEnabled false
@@ -104,8 +134,11 @@ dependencies {
104
134
  //noinspection GradleDynamicVersion
105
135
  implementation "com.facebook.react:react-native:+"
106
136
 
137
+ // Add a dependency on NitroModules
107
138
  implementation project(":react-native-nitro-modules")
108
- implementation "com.android.ndk.thirdparty:openssl:1.1.1q-beta-1"
139
+
140
+ // Add a dependency on OpenSSL
141
+ implementation 'io.github.ronickg:openssl:3.3.2'
109
142
  }
110
143
 
111
144
  if (isNewArchitectureEnabled()) {
@@ -1,15 +1,8 @@
1
+ #include <fbjni/fbjni.h>
1
2
  #include <jni.h>
2
3
 
3
- #include "random/HybridRandom.hpp"
4
- #include <NitroModules/HybridObjectRegistry.hpp>
5
-
6
- using namespace margelo::nitro::crypto;
7
- using namespace margelo::crypto;
4
+ #include "QuickCryptoOnLoad.hpp"
8
5
 
9
6
  JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*) {
10
- HybridObjectRegistry::registerHybridObjectConstructor(
11
- "Random", []() -> std::shared_ptr<HybridObject> { return std::make_shared<HybridRandom>();
12
- });
13
-
14
- return JNI_VERSION_1_2;
7
+ return facebook::jni::initialize(vm, [=] { margelo::nitro::crypto::initialize(vm); });
15
8
  }
@@ -1,27 +1,21 @@
1
1
  package com.margelo.nitro.quickcrypto;
2
2
 
3
3
  import android.util.Log;
4
+
4
5
  import androidx.annotation.Nullable;
5
6
 
6
7
  import com.facebook.react.bridge.NativeModule;
7
8
  import com.facebook.react.bridge.ReactApplicationContext;
8
9
  import com.facebook.react.module.model.ReactModuleInfoProvider;
9
10
  import com.facebook.react.TurboReactPackage;
11
+ import com.margelo.nitro.core.HybridObject;
12
+ import com.margelo.nitro.core.HybridObjectRegistry;
10
13
 
11
14
  import java.util.HashMap;
15
+ import java.util.function.Supplier;
12
16
 
13
17
  public class QuickCryptoPackage extends TurboReactPackage {
14
18
  private static final String TAG = "QuickCrypto";
15
- static {
16
- try {
17
- Log.i(TAG, "Loading C++ library...");
18
- System.loadLibrary(TAG);
19
- Log.i(TAG, "Successfully loaded C++ library!");
20
- } catch (Throwable e) {
21
- Log.e(TAG, "Failed to load C++ library! Is it properly installed and linked?", e);
22
- throw e;
23
- }
24
- }
25
19
 
26
20
  @Nullable
27
21
  @Override
@@ -35,4 +29,15 @@ public class QuickCryptoPackage extends TurboReactPackage {
35
29
  return new HashMap<>();
36
30
  };
37
31
  }
32
+
33
+ static {
34
+ try {
35
+ Log.i(TAG, "Loading C++ library...");
36
+ System.loadLibrary(TAG);
37
+ Log.i(TAG, "Successfully loaded C++ library!");
38
+ } catch (Throwable e) {
39
+ Log.e(TAG, "Failed to load C++ library! Is it properly installed and linked?", e);
40
+ throw e;
41
+ }
42
+ }
38
43
  }
@@ -0,0 +1,268 @@
1
+ #include "HybridEdKeyPair.hpp"
2
+
3
+ #include <memory>
4
+ #include <string>
5
+
6
+ namespace margelo::nitro::crypto {
7
+
8
+ std::shared_ptr<Promise<void>>
9
+ HybridEdKeyPair::generateKeyPair(
10
+ double publicFormat,
11
+ double publicType,
12
+ double privateFormat,
13
+ double privateType,
14
+ const std::optional<std::string>& cipher,
15
+ const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase
16
+ ) {
17
+ // get owned NativeArrayBuffers before passing to sync function
18
+ std::optional<std::shared_ptr<ArrayBuffer>> nativePassphrase = std::nullopt;
19
+ if (passphrase.has_value()) {
20
+ nativePassphrase = ToNativeArrayBuffer(passphrase.value());
21
+ }
22
+
23
+ return Promise<void>::async(
24
+ [this, publicFormat, publicType, privateFormat, privateType, cipher,
25
+ nativePassphrase]() {
26
+ this->generateKeyPairSync(
27
+ publicFormat,
28
+ publicType,
29
+ privateFormat,
30
+ privateType,
31
+ cipher,
32
+ nativePassphrase
33
+ );
34
+ }
35
+ );
36
+ }
37
+
38
+ void
39
+ HybridEdKeyPair::generateKeyPairSync(
40
+ double publicFormat,
41
+ double publicType,
42
+ double privateFormat,
43
+ double privateType,
44
+ const std::optional<std::string>& cipher,
45
+ const std::optional<std::shared_ptr<ArrayBuffer>>& passphrase
46
+ ) {
47
+ EVP_PKEY_CTX* pctx;
48
+
49
+ // key context
50
+ pctx = EVP_PKEY_CTX_new_from_name(nullptr, this->curve.c_str(), nullptr);
51
+ if (pctx == nullptr) {
52
+ throw std::runtime_error("Invalid curve name: " + this->curve);
53
+ }
54
+
55
+ // keygen init
56
+ if (EVP_PKEY_keygen_init(pctx) <= 0) {
57
+ EVP_PKEY_CTX_free(pctx);
58
+ throw std::runtime_error("Failed to initialize keygen");
59
+ }
60
+
61
+ // generate key
62
+ EVP_PKEY_keygen(pctx, &this->pkey);
63
+ if (this->pkey == nullptr) {
64
+ EVP_PKEY_CTX_free(pctx);
65
+ throw std::runtime_error("Failed to generate key");
66
+ }
67
+
68
+ // cleanup
69
+ EVP_PKEY_CTX_free(pctx);
70
+ }
71
+
72
+
73
+ std::shared_ptr<Promise<std::shared_ptr<ArrayBuffer>>>
74
+ HybridEdKeyPair::sign(
75
+ const std::shared_ptr<ArrayBuffer>& message,
76
+ const std::optional<std::shared_ptr<ArrayBuffer>>& key
77
+ ) {
78
+ // get owned NativeArrayBuffer before passing to sync function
79
+ auto nativeMessage = ToNativeArrayBuffer(message);
80
+ std::optional<std::shared_ptr<ArrayBuffer>> nativeKey = std::nullopt;
81
+ if (key.has_value()) {
82
+ nativeKey = ToNativeArrayBuffer(key.value());
83
+ }
84
+
85
+ return Promise<std::shared_ptr<ArrayBuffer>>::async([this, nativeMessage, nativeKey]() {
86
+ return this->signSync(nativeMessage, nativeKey);
87
+ }
88
+ );
89
+ }
90
+
91
+ std::shared_ptr<ArrayBuffer>
92
+ HybridEdKeyPair::signSync(
93
+ const std::shared_ptr<ArrayBuffer>& message,
94
+ const std::optional<std::shared_ptr<ArrayBuffer>>& key
95
+ ) {
96
+
97
+ size_t sig_len = 0;
98
+ uint8_t* sig = NULL;
99
+ EVP_MD_CTX* md_ctx = nullptr;
100
+ EVP_PKEY_CTX* pkey_ctx = nullptr;
101
+
102
+ // get key to use for signing
103
+ EVP_PKEY* pkey = this->importPrivateKey(key);
104
+
105
+ // key context
106
+ md_ctx = EVP_MD_CTX_new();
107
+ if (md_ctx == nullptr) {
108
+ EVP_MD_CTX_free(md_ctx);
109
+ throw std::runtime_error("Error creating signing context");
110
+ }
111
+
112
+ pkey_ctx = EVP_PKEY_CTX_new_from_name(nullptr, this->curve.c_str(), nullptr);
113
+ if (pkey_ctx == nullptr) {
114
+ EVP_PKEY_CTX_free(pkey_ctx);
115
+ throw std::runtime_error("Error creating signing context: " + this->curve);
116
+ }
117
+
118
+ if (EVP_DigestSignInit(md_ctx, &pkey_ctx, NULL, NULL, pkey) <= 0) {
119
+ EVP_MD_CTX_free(md_ctx);
120
+ char* err = ERR_error_string(ERR_get_error(), NULL);
121
+ throw std::runtime_error("Failed to initialize signing: " + std::string(err));
122
+ }
123
+
124
+ // Calculate the required size for the signature by passing a NULL buffer.
125
+ if (EVP_DigestSign(md_ctx, NULL, &sig_len, message.get()->data(), message.get()->size()) <= 0) {
126
+ EVP_MD_CTX_free(md_ctx);
127
+ throw std::runtime_error("Failed to calculate signature size");
128
+ }
129
+ sig = new uint8_t[sig_len];
130
+
131
+ // Actually calculate the signature
132
+ if (EVP_DigestSign(md_ctx, sig, &sig_len, message.get()->data(), message.get()->size()) <= 0) {
133
+ EVP_MD_CTX_free(md_ctx);
134
+ throw std::runtime_error("Failed to calculate signature");
135
+ }
136
+
137
+ // return value for JS
138
+ std::shared_ptr<ArrayBuffer> signature = std::make_shared<NativeArrayBuffer>(
139
+ sig,
140
+ sig_len,
141
+ [=]() { delete[] sig; }
142
+ );
143
+
144
+ // Clean up
145
+ EVP_MD_CTX_free(md_ctx);
146
+
147
+ return signature;
148
+ }
149
+
150
+ std::shared_ptr<Promise<bool>>
151
+ HybridEdKeyPair::verify(
152
+ const std::shared_ptr<ArrayBuffer>& signature,
153
+ const std::shared_ptr<ArrayBuffer>& message,
154
+ const std::optional<std::shared_ptr<ArrayBuffer>>& key
155
+ ) {
156
+ // get owned NativeArrayBuffers before passing to sync function
157
+ auto nativeSignature = ToNativeArrayBuffer(signature);
158
+ auto nativeMessage = ToNativeArrayBuffer(message);
159
+ std::optional<std::shared_ptr<ArrayBuffer>> nativeKey = std::nullopt;
160
+ if (key.has_value()) {
161
+ nativeKey = ToNativeArrayBuffer(key.value());
162
+ }
163
+
164
+ return Promise<bool>::async([this, nativeSignature, nativeMessage, nativeKey]() {
165
+ return this->verifySync(nativeSignature, nativeMessage, nativeKey);
166
+ }
167
+ );
168
+ }
169
+
170
+ bool
171
+ HybridEdKeyPair::verifySync(
172
+ const std::shared_ptr<ArrayBuffer>& signature,
173
+ const std::shared_ptr<ArrayBuffer>& message,
174
+ const std::optional<std::shared_ptr<ArrayBuffer>>& key
175
+ ) {
176
+ // get key to use for verifying
177
+ EVP_PKEY* pkey = this->importPrivateKey(key);
178
+
179
+ EVP_MD_CTX* md_ctx = nullptr;
180
+ EVP_PKEY_CTX* pkey_ctx = nullptr;
181
+
182
+ // key context
183
+ md_ctx = EVP_MD_CTX_new();
184
+ if (md_ctx == nullptr) {
185
+ EVP_MD_CTX_free(md_ctx);
186
+ throw std::runtime_error("Error creating verify context");
187
+ }
188
+
189
+ pkey_ctx = EVP_PKEY_CTX_new_from_name(nullptr, this->curve.c_str(), nullptr);
190
+ if (pkey_ctx == nullptr) {
191
+ EVP_PKEY_CTX_free(pkey_ctx);
192
+ throw std::runtime_error("Error creating verify context: " + this->curve);
193
+ }
194
+
195
+ if (EVP_DigestVerifyInit(md_ctx, &pkey_ctx, NULL, NULL, pkey) <= 0) {
196
+ EVP_MD_CTX_free(md_ctx);
197
+ char* err = ERR_error_string(ERR_get_error(), NULL);
198
+ throw std::runtime_error("Failed to initialize verify: " + std::string(err));
199
+ }
200
+
201
+ // verify
202
+ auto res = EVP_DigestVerify(
203
+ md_ctx,
204
+ signature.get()->data(), signature.get()->size(),
205
+ message.get()->data(), message.get()->size()
206
+ );
207
+
208
+ //return value for JS
209
+ if (res < 0) {
210
+ EVP_MD_CTX_free(md_ctx);
211
+ throw std::runtime_error("Failed to verify");
212
+ }
213
+ return res == 1; // true if 1, false if 0
214
+ }
215
+
216
+ std::shared_ptr<ArrayBuffer>
217
+ HybridEdKeyPair::getPublicKey() {
218
+ this->checkKeyPair();
219
+ size_t len = 32;
220
+ uint8_t* publ = new uint8_t[len];
221
+ EVP_PKEY_get_raw_public_key(this->pkey, publ, &len);
222
+
223
+ return std::make_shared<NativeArrayBuffer>(publ, len, [=]() { delete[] publ; });
224
+ }
225
+
226
+ std::shared_ptr<ArrayBuffer>
227
+ HybridEdKeyPair::getPrivateKey() {
228
+ this->checkKeyPair();
229
+ size_t len = 32;
230
+ uint8_t* priv = new uint8_t[len];
231
+ EVP_PKEY_get_raw_private_key(this->pkey, priv, &len);
232
+
233
+ return std::make_shared<NativeArrayBuffer>(priv, len, [=]() { delete[] priv; });
234
+ }
235
+
236
+ void
237
+ HybridEdKeyPair::checkKeyPair() {
238
+ if (this->pkey == nullptr) {
239
+ throw std::runtime_error("Keypair not initialized");
240
+ }
241
+ }
242
+
243
+ void
244
+ HybridEdKeyPair::setCurve(const std::string& curve) {
245
+ this->curve = curve;
246
+ }
247
+
248
+ EVP_PKEY*
249
+ HybridEdKeyPair::importPrivateKey(const std::optional<std::shared_ptr<ArrayBuffer>>& key) {
250
+ EVP_PKEY* pkey = nullptr;
251
+ if (key.has_value()) {
252
+ pkey = EVP_PKEY_new_raw_private_key(
253
+ EVP_PKEY_ED25519, // TODO: use this->curve somehow
254
+ NULL,
255
+ key.value()->data(),
256
+ 32
257
+ );
258
+ if (pkey == nullptr) {
259
+ throw std::runtime_error("Failed to read private key");
260
+ }
261
+ } else {
262
+ this->checkKeyPair();
263
+ pkey = this->pkey;
264
+ }
265
+ return pkey;
266
+ }
267
+
268
+ } // namespace margelo::nitro::crypto