react-native-quick-crypto 0.6.1 → 0.7.0-rc.0

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 (196) hide show
  1. package/README.md +70 -18
  2. package/android/CMakeLists.txt +58 -61
  3. package/android/build.gradle +105 -53
  4. package/android/gradle/wrapper/gradle-wrapper.properties +1 -1
  5. package/android/gradle.properties +5 -5
  6. package/android/src/main/{AndroidManifest.xml → AndroidManifestNew.xml} +1 -2
  7. package/cpp/Cipher/MGLCipherHostObject.cpp +31 -5
  8. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +21 -31
  9. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +4 -17
  10. package/cpp/Cipher/MGLPublicCipher.h +1 -1
  11. package/cpp/Cipher/MGLPublicCipherInstaller.h +6 -6
  12. package/cpp/Cipher/MGLRsa.cpp +224 -11
  13. package/cpp/Cipher/MGLRsa.h +13 -3
  14. package/cpp/Hash/MGLHashHostObject.cpp +1 -1
  15. package/cpp/Hash/MGLHashInstaller.cpp +2 -4
  16. package/cpp/JSIUtils/MGLJSIMacros.h +10 -0
  17. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +1 -1
  18. package/cpp/MGLKeys.cpp +415 -471
  19. package/cpp/MGLKeys.h +70 -2
  20. package/cpp/MGLQuickCryptoHostObject.cpp +9 -0
  21. package/cpp/Random/MGLRandomHostObject.cpp +9 -2
  22. package/cpp/Sig/MGLSignHostObjects.cpp +1 -1
  23. package/cpp/Utils/MGLUtils.cpp +104 -32
  24. package/cpp/Utils/MGLUtils.h +172 -143
  25. package/cpp/Utils/node.h +13 -0
  26. package/cpp/webcrypto/MGLWebCrypto.cpp +63 -0
  27. package/cpp/webcrypto/MGLWebCrypto.h +34 -0
  28. package/cpp/webcrypto/crypto_ec.cpp +334 -0
  29. package/cpp/webcrypto/crypto_ec.h +65 -0
  30. package/ios/QuickCrypto.xcodeproj/project.pbxproj +4 -4
  31. package/lib/commonjs/@types/crypto-browserify.d.js.map +1 -1
  32. package/lib/commonjs/Cipher.js +53 -194
  33. package/lib/commonjs/Cipher.js.map +1 -1
  34. package/lib/commonjs/Hash.js +29 -29
  35. package/lib/commonjs/Hash.js.map +1 -1
  36. package/lib/commonjs/Hashnames.js +75 -0
  37. package/lib/commonjs/Hashnames.js.map +1 -0
  38. package/lib/commonjs/Hmac.js +6 -29
  39. package/lib/commonjs/Hmac.js.map +1 -1
  40. package/lib/commonjs/NativeQuickCrypto/Cipher.js +3 -5
  41. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  42. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +11 -20
  43. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  44. package/lib/commonjs/NativeQuickCrypto/hash.js.map +1 -1
  45. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +1 -1
  46. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +1 -1
  47. package/lib/commonjs/NativeQuickCrypto/random.js.map +1 -1
  48. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
  49. package/lib/commonjs/NativeQuickCrypto/webcrypto.js +6 -0
  50. package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +1 -0
  51. package/lib/commonjs/QuickCrypto.js +5 -14
  52. package/lib/commonjs/QuickCrypto.js.map +1 -1
  53. package/lib/commonjs/Utils.js +360 -48
  54. package/lib/commonjs/Utils.js.map +1 -1
  55. package/lib/commonjs/aes.js +324 -0
  56. package/lib/commonjs/aes.js.map +1 -0
  57. package/lib/commonjs/constants.js +1 -2
  58. package/lib/commonjs/constants.js.map +1 -1
  59. package/lib/commonjs/ec.js +288 -0
  60. package/lib/commonjs/ec.js.map +1 -0
  61. package/lib/commonjs/index.js +6 -10
  62. package/lib/commonjs/index.js.map +1 -1
  63. package/lib/commonjs/keys.js +280 -48
  64. package/lib/commonjs/keys.js.map +1 -1
  65. package/lib/commonjs/pbkdf2.js +44 -18
  66. package/lib/commonjs/pbkdf2.js.map +1 -1
  67. package/lib/commonjs/random.js +49 -68
  68. package/lib/commonjs/random.js.map +1 -1
  69. package/lib/commonjs/rsa.js +329 -0
  70. package/lib/commonjs/rsa.js.map +1 -0
  71. package/lib/commonjs/sig.js +13 -54
  72. package/lib/commonjs/sig.js.map +1 -1
  73. package/lib/commonjs/subtle.js +271 -0
  74. package/lib/commonjs/subtle.js.map +1 -0
  75. package/lib/module/@types/crypto-browserify.d.js.map +1 -1
  76. package/lib/module/Cipher.js +53 -188
  77. package/lib/module/Cipher.js.map +1 -1
  78. package/lib/module/Hash.js +27 -21
  79. package/lib/module/Hash.js.map +1 -1
  80. package/lib/module/Hashnames.js +71 -0
  81. package/lib/module/Hashnames.js.map +1 -0
  82. package/lib/module/Hmac.js +4 -21
  83. package/lib/module/Hmac.js.map +1 -1
  84. package/lib/module/NativeQuickCrypto/Cipher.js +3 -4
  85. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  86. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +11 -17
  87. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  88. package/lib/module/NativeQuickCrypto/hash.js.map +1 -1
  89. package/lib/module/NativeQuickCrypto/hmac.js.map +1 -1
  90. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +1 -1
  91. package/lib/module/NativeQuickCrypto/random.js.map +1 -1
  92. package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
  93. package/lib/module/NativeQuickCrypto/webcrypto.js +2 -0
  94. package/lib/module/NativeQuickCrypto/webcrypto.js.map +1 -0
  95. package/lib/module/QuickCrypto.js +2 -0
  96. package/lib/module/QuickCrypto.js.map +1 -1
  97. package/lib/module/Utils.js +319 -33
  98. package/lib/module/Utils.js.map +1 -1
  99. package/lib/module/aes.js +317 -0
  100. package/lib/module/aes.js.map +1 -0
  101. package/lib/module/constants.js.map +1 -1
  102. package/lib/module/ec.js +282 -0
  103. package/lib/module/ec.js.map +1 -0
  104. package/lib/module/index.js +7 -4
  105. package/lib/module/index.js.map +1 -1
  106. package/lib/module/keys.js +279 -43
  107. package/lib/module/keys.js.map +1 -1
  108. package/lib/module/pbkdf2.js +44 -13
  109. package/lib/module/pbkdf2.js.map +1 -1
  110. package/lib/module/random.js +46 -54
  111. package/lib/module/random.js.map +1 -1
  112. package/lib/module/rsa.js +323 -0
  113. package/lib/module/rsa.js.map +1 -0
  114. package/lib/module/sig.js +13 -46
  115. package/lib/module/sig.js.map +1 -1
  116. package/lib/module/subtle.js +265 -0
  117. package/lib/module/subtle.js.map +1 -0
  118. package/lib/typescript/src/Cipher.d.ts +72 -0
  119. package/lib/typescript/src/Cipher.d.ts.map +1 -0
  120. package/lib/typescript/{Hash.d.ts → src/Hash.d.ts} +8 -7
  121. package/lib/typescript/src/Hash.d.ts.map +1 -0
  122. package/lib/typescript/src/Hashnames.d.ts +11 -0
  123. package/lib/typescript/src/Hashnames.d.ts.map +1 -0
  124. package/lib/typescript/{Hmac.d.ts → src/Hmac.d.ts} +3 -3
  125. package/lib/typescript/src/Hmac.d.ts.map +1 -0
  126. package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts +34 -0
  127. package/lib/typescript/src/NativeQuickCrypto/Cipher.d.ts.map +1 -0
  128. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/NativeQuickCrypto.d.ts +3 -0
  129. package/lib/typescript/src/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -0
  130. package/lib/typescript/src/NativeQuickCrypto/hash.d.ts +7 -0
  131. package/lib/typescript/src/NativeQuickCrypto/hash.d.ts.map +1 -0
  132. package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts +6 -0
  133. package/lib/typescript/src/NativeQuickCrypto/hmac.d.ts.map +1 -0
  134. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/pbkdf2.d.ts +2 -1
  135. package/lib/typescript/src/NativeQuickCrypto/pbkdf2.d.ts.map +1 -0
  136. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/random.d.ts +2 -1
  137. package/lib/typescript/src/NativeQuickCrypto/random.d.ts.map +1 -0
  138. package/lib/typescript/{NativeQuickCrypto → src/NativeQuickCrypto}/sig.d.ts +5 -4
  139. package/lib/typescript/src/NativeQuickCrypto/sig.d.ts.map +1 -0
  140. package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts +27 -0
  141. package/lib/typescript/src/NativeQuickCrypto/webcrypto.d.ts.map +1 -0
  142. package/lib/typescript/{QuickCrypto.d.ts → src/QuickCrypto.d.ts} +16 -8
  143. package/lib/typescript/src/QuickCrypto.d.ts.map +1 -0
  144. package/lib/typescript/src/Utils.d.ts +44 -0
  145. package/lib/typescript/src/Utils.d.ts.map +1 -0
  146. package/lib/typescript/src/aes.d.ts +5 -0
  147. package/lib/typescript/src/aes.d.ts.map +1 -0
  148. package/lib/typescript/{constants.d.ts → src/constants.d.ts} +1 -0
  149. package/lib/typescript/src/constants.d.ts.map +1 -0
  150. package/lib/typescript/src/ec.d.ts +5 -0
  151. package/lib/typescript/src/ec.d.ts.map +1 -0
  152. package/lib/typescript/{index.d.ts → src/index.d.ts} +33 -27
  153. package/lib/typescript/src/index.d.ts.map +1 -0
  154. package/lib/typescript/src/keys.d.ts +154 -0
  155. package/lib/typescript/src/keys.d.ts.map +1 -0
  156. package/lib/typescript/src/pbkdf2.d.ts +12 -0
  157. package/lib/typescript/src/pbkdf2.d.ts.map +1 -0
  158. package/lib/typescript/{random.d.ts → src/random.d.ts} +7 -6
  159. package/lib/typescript/src/random.d.ts.map +1 -0
  160. package/lib/typescript/src/rsa.d.ts +4 -0
  161. package/lib/typescript/src/rsa.d.ts.map +1 -0
  162. package/lib/typescript/{sig.d.ts → src/sig.d.ts} +3 -3
  163. package/lib/typescript/src/sig.d.ts.map +1 -0
  164. package/lib/typescript/src/subtle.d.ts +11 -0
  165. package/lib/typescript/src/subtle.d.ts.map +1 -0
  166. package/package.json +35 -30
  167. package/react-native-quick-crypto.podspec +4 -3
  168. package/src/Cipher.ts +103 -100
  169. package/src/Hash.ts +42 -6
  170. package/src/Hashnames.ts +91 -0
  171. package/src/Hmac.ts +3 -3
  172. package/src/NativeQuickCrypto/Cipher.ts +1 -0
  173. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +2 -0
  174. package/src/NativeQuickCrypto/webcrypto.ts +46 -0
  175. package/src/QuickCrypto.ts +2 -0
  176. package/src/Utils.ts +409 -5
  177. package/src/aes.ts +365 -0
  178. package/src/ec.ts +351 -0
  179. package/src/keys.ts +428 -54
  180. package/src/pbkdf2.ts +84 -11
  181. package/src/random.ts +37 -24
  182. package/src/rsa.ts +396 -0
  183. package/src/sig.ts +3 -2
  184. package/src/subtle.ts +358 -0
  185. package/lib/commonjs/@types/stream-browserify.d.js +0 -2
  186. package/lib/commonjs/@types/stream-browserify.d.js.map +0 -1
  187. package/lib/module/@types/stream-browserify.d.js +0 -2
  188. package/lib/module/@types/stream-browserify.d.js.map +0 -1
  189. package/lib/typescript/Cipher.d.ts +0 -87
  190. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -32
  191. package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -6
  192. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -5
  193. package/lib/typescript/Utils.d.ts +0 -23
  194. package/lib/typescript/keys.d.ts +0 -60
  195. package/lib/typescript/pbkdf2.d.ts +0 -9
  196. package/src/@types/stream-browserify.d.ts +0 -4
package/README.md CHANGED
@@ -9,20 +9,20 @@ A fast implementation of Node's `crypto` module.
9
9
  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.
10
10
  QuickCrypto can be used as a drop-in replacement for your Web3/Crypto apps to speed up common cryptography functions.
11
11
 
12
- * 🏎️ Up to 58x faster than all other solutions
13
- * ⚡️ Lightning fast implementation with pure C++ and JSI, instead of JS
14
- * 🧪 Well tested in JS and C++ (OpenSSL)
15
- * 💰 Made for crypto apps and Wallets
16
- * 🔢 Secure native compiled cryptography
17
- * 🔁 Easy drop-in replacement for [crypto-browserify](https://github.com/crypto-browserify/crypto-browserify) or [react-native-crypto](https://github.com/tradle/react-native-crypto)
12
+ - 🏎️ Up to 58x faster than all other solutions
13
+ - ⚡️ Lightning fast implementation with pure C++ and JSI, instead of JS
14
+ - 🧪 Well tested in JS and C++ (OpenSSL)
15
+ - 💰 Made for crypto apps and Wallets
16
+ - 🔢 Secure native compiled cryptography
17
+ - 🔁 Easy drop-in replacement for [crypto-browserify](https://github.com/browserify/crypto-browserify) or [react-native-crypto](https://github.com/tradle/react-native-crypto)
18
18
 
19
19
  For example, creating a Wallet using ethers.js uses complex algorithms to generate a private-key/mnemonic-phrase pair:
20
20
 
21
21
  ```ts
22
- const start = performance.now()
23
- const wallet = ethers.Wallet.createRandom()
24
- const end = performance.now()
25
- console.log(`Creating a Wallet took ${end - start} ms.`)
22
+ const start = performance.now();
23
+ const wallet = ethers.Wallet.createRandom();
24
+ const end = performance.now();
25
+ console.log(`Creating a Wallet took ${end - start} ms.`);
26
26
  ```
27
27
 
28
28
  **Without** react-native-quick-crypto 🐢:
@@ -65,7 +65,34 @@ expo prebuild
65
65
 
66
66
  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!
67
67
 
68
- In your `babel.config.js`, add a module resolver to replace `crypto` with `react-native-quick-crypto`:
68
+ ### Using metro config
69
+
70
+ Use the [`resolveRequest`](https://facebook.github.io/metro/docs/resolution#resolverequest-customresolver) configuration option in your `metro.config.js`
71
+
72
+ ```
73
+ config.resolver.resolveRequest = (context, moduleName, platform) => {
74
+ if (moduleName === 'crypto') {
75
+ // when importing crypto, resolve to react-native-quick-crypto
76
+ return context.resolveRequest(
77
+ context,
78
+ 'react-native-quick-crypto',
79
+ platform,
80
+ )
81
+ }
82
+ // otherwise chain to the standard Metro resolver.
83
+ return context.resolveRequest(context, moduleName, platform)
84
+ }
85
+ ```
86
+
87
+ ### Using babel-plugin-module-resolver
88
+
89
+ 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.
90
+
91
+ ```sh
92
+ yarn add --dev babel-plugin-module-resolver
93
+ ```
94
+
95
+ Then, in your `babel.config.js`, add the plugin to swap the `crypto`, `stream` and `buffer` dependencies:
69
96
 
70
97
  ```diff
71
98
  module.exports = {
@@ -76,7 +103,7 @@ module.exports = {
76
103
  + {
77
104
  + alias: {
78
105
  + 'crypto': 'react-native-quick-crypto',
79
- + 'stream': 'stream-browserify',
106
+ + 'stream': 'readable-stream',
80
107
  + 'buffer': '@craftzdog/react-native-buffer',
81
108
  + },
82
109
  + },
@@ -88,22 +115,43 @@ module.exports = {
88
115
 
89
116
  Then restart your bundler using `yarn start --reset-cache`.
90
117
 
91
- Now, all imports for `crypto` will be resolved as `react-native-quick-crypto` instead.
92
-
93
- > 💡 Since react-native-quick-crypto depends on `stream` and `buffer`, we can resolve those to `stream-browserify` and @craftzdog's `react-native-buffer` (which is faster than `buffer` because it uses JSI for base64 encoding and decoding).
94
-
95
118
  ## Usage
96
119
 
97
120
  For example, to hash a string with SHA256 you can do the following:
98
121
 
99
122
  ```ts
100
- import Crypto from 'react-native-quick-crypto'
123
+ import Crypto from 'react-native-quick-crypto';
101
124
 
102
125
  const hashed = Crypto.createHash('sha256')
103
126
  .update('Damn, Margelo writes hella good software!')
104
- .digest('hex')
127
+ .digest('hex');
105
128
  ```
106
129
 
130
+ ## Android build errors
131
+
132
+ If you get an error similar to this:
133
+
134
+ ```
135
+ Execution failed for task ':app:mergeDebugNativeLibs'.
136
+ > A failure occurred while executing com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction
137
+ > 2 files found with path 'lib/arm64-v8a/libcrypto.so' from inputs:
138
+ - /Users/osp/Developer/mac_test/node_modules/react-native-quick-crypto/android/build/intermediates/library_jni/debug/jni/arm64-v8a/libcrypto.so
139
+ - /Users/osp/.gradle/caches/transforms-3/e13f88164840fe641a466d05cd8edac7/transformed/jetified-flipper-0.182.0/jni/arm64-v8a/libcrypto.so
140
+ ```
141
+
142
+ It means you have a transitive dependency where two libraries depend on OpenSSL and are generating a `libcrypto.so` file. You can get around this issue by adding the following in your `app/build.gradle`:
143
+
144
+ ```groovy
145
+ packagingOptions {
146
+ // Should prevent clashes with other libraries that use OpenSSL
147
+ pickFirst '**/libcrypto.so'
148
+ }
149
+ ```
150
+
151
+ > This caused by flipper which also depends on OpenSSL
152
+
153
+ This just tells Gradle to grab whatever OpenSSL version it finds first and link against that, but as you can imagine this is not correct if the packages depend on different OpenSSL versions (quick-crypto depends on `com.android.ndk.thirdparty:openssl:1.1.1q-beta-1`). You should make sure all the OpenSSL versions match and you have no conflicts or errors.
154
+
107
155
  ---
108
156
 
109
157
  ## Sponsors
@@ -154,6 +202,10 @@ const hashed = Crypto.createHash('sha256')
154
202
 
155
203
  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).
156
204
 
205
+ ## Community Discord
206
+
207
+ [Join the Margelo Community Discord](https://discord.gg/6CSHz2qAvA) to chat about react-native-quick-crypto or other Margelo libraries.
208
+
157
209
  ## Adopting at scale
158
210
 
159
211
  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>!
@@ -1,82 +1,79 @@
1
- project(react-native-quick-crypto)
2
- cmake_minimum_required(VERSION 3.9.0)
1
+ project(ReactNativeQuickCrypto)
2
+ cmake_minimum_required(VERSION 3.10.2)
3
3
 
4
4
  set(PACKAGE_NAME "reactnativequickcrypto")
5
5
  set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
6
+ set(CMAKE_VERBOSE_MAKEFILE ON)
6
7
  set(CMAKE_CXX_STANDARD 17)
8
+ set(NODE_MODULES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../node_modules")
7
9
 
8
- # TODO(osp) remove before release
9
10
  # set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
10
11
  # set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
11
12
 
12
- # Consume shared libraries and headers from prefabs
13
+ # Third party libraries (Prefabs)
13
14
  find_package(fbjni REQUIRED CONFIG)
14
15
  find_package(ReactAndroid REQUIRED CONFIG)
15
-
16
- include_directories(
17
- ../cpp
18
- "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
19
- "${NODE_MODULES_DIR}/react-native/ReactCommon"
20
- "${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
21
- "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
22
- "${NODE_MODULES_DIR}/react-native/ReactCommon/turbomodule/core"
23
- "${NODE_MODULES_DIR}/react-native/ReactCommon/react/nativemodule/core"
24
- )
16
+ find_package(openssl REQUIRED CONFIG)
17
+ find_library(LOG_LIB log)
25
18
 
26
19
  add_library(
27
- ${PACKAGE_NAME}
28
- SHARED
29
- "src/main/cpp/cpp-adapter.cpp"
30
- "../cpp/MGLQuickCryptoHostObject.cpp"
31
- "../cpp/JSIUtils/MGLTypedArray.cpp"
32
- "../cpp/Utils/MGLDispatchQueue.cpp"
33
- "../cpp/JSIUtils/MGLThreadAwareHostObject.cpp"
34
- "../cpp/JSIUtils/MGLSmartHostObject.cpp"
35
- "../cpp/HMAC/MGLHmacInstaller.cpp"
36
- "../cpp/HMAC/MGLHmacHostObject.cpp"
37
- "../cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp"
38
- "../cpp/fastpbkdf2/fastpbkdf2.c"
39
- "../cpp/Random/MGLRandomHostObject.cpp"
40
- "../cpp/Hash/MGLHashInstaller.cpp"
41
- "../cpp/Hash/MGLHashHostObject.cpp"
42
- "../cpp/Cipher/MGLCipherHostObject.cpp"
43
- "../cpp/Cipher/MGLCreateCipherInstaller.cpp"
44
- "../cpp/Cipher/MGLCreateDecipherInstaller.cpp"
45
- "../cpp/MGLKeys.cpp"
46
- "../cpp/Utils/MGLUtils.cpp"
47
- "../cpp/Cipher/MGLRsa.cpp"
48
- "../cpp/Cipher/MGLGenerateKeyPairInstaller.cpp"
49
- "../cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp"
50
- "../cpp/Sig/MGLSignInstaller.cpp"
51
- "../cpp/Sig/MGLVerifyInstaller.cpp"
52
- "../cpp/Sig/MGLSignHostObjects.cpp"
20
+ ${PACKAGE_NAME}
21
+ SHARED
22
+ "src/main/cpp/cpp-adapter.cpp"
23
+ "../cpp/MGLQuickCryptoHostObject.cpp"
24
+ "../cpp/JSIUtils/MGLTypedArray.cpp"
25
+ "../cpp/Utils/MGLDispatchQueue.cpp"
26
+ "../cpp/JSIUtils/MGLThreadAwareHostObject.cpp"
27
+ "../cpp/JSIUtils/MGLSmartHostObject.cpp"
28
+ "../cpp/HMAC/MGLHmacInstaller.cpp"
29
+ "../cpp/HMAC/MGLHmacHostObject.cpp"
30
+ "../cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp"
31
+ "../cpp/fastpbkdf2/fastpbkdf2.c"
32
+ "../cpp/Random/MGLRandomHostObject.cpp"
33
+ "../cpp/Hash/MGLHashInstaller.cpp"
34
+ "../cpp/Hash/MGLHashHostObject.cpp"
35
+ "../cpp/Cipher/MGLCipherHostObject.cpp"
36
+ "../cpp/Cipher/MGLCreateCipherInstaller.cpp"
37
+ "../cpp/Cipher/MGLCreateDecipherInstaller.cpp"
38
+ "../cpp/MGLKeys.cpp"
39
+ "../cpp/Utils/MGLUtils.cpp"
40
+ "../cpp/Cipher/MGLRsa.cpp"
41
+ "../cpp/Cipher/MGLGenerateKeyPairInstaller.cpp"
42
+ "../cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp"
43
+ "../cpp/Sig/MGLSignInstaller.cpp"
44
+ "../cpp/Sig/MGLVerifyInstaller.cpp"
45
+ "../cpp/Sig/MGLSignHostObjects.cpp"
46
+ "../cpp/webcrypto/MGLWebCrypto.cpp"
47
+ "../cpp/webcrypto/crypto_ec.cpp"
53
48
  )
54
49
 
55
- set_target_properties(
56
- ${PACKAGE_NAME}
57
- PROPERTIES
58
- CXX_STANDARD 17
59
- CXX_EXTENSIONS OFF
60
- POSITION_INDEPENDENT_CODE ON
50
+ include_directories(
51
+ "../node_modules/react-native-quick-base64/cpp"
61
52
  )
62
53
 
63
- file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
64
-
65
- find_library(
66
- log-lib
67
- log
54
+ target_include_directories(
55
+ ${PACKAGE_NAME}
56
+ PRIVATE
57
+ "../cpp"
58
+ "src/main/cpp"
59
+ "${NODE_MODULES_DIR}/react-native/ReactCommon"
60
+ "${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
61
+ "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
62
+ "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
63
+ "${NODE_MODULES_DIR}/react-native/ReactCommon/turbomodule/core"
64
+ "${NODE_MODULES_DIR}/react-native/ReactCommon/react/nativemodule/core"
68
65
  )
69
66
 
70
- find_package(openssl REQUIRED CONFIG)
67
+ #file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
71
68
 
72
69
  target_link_libraries(
73
- ${PACKAGE_NAME}
74
- ReactAndroid::turbomodulejsijni
75
- fbjni::fbjni
76
- ${log-lib}
77
- ReactAndroid::jsi
78
- ReactAndroid::reactnativejni
79
- ReactAndroid::react_nativemodule_core
80
- android
81
- openssl::crypto
70
+ ${PACKAGE_NAME}
71
+ ReactAndroid::turbomodulejsijni
72
+ fbjni::fbjni # <-- fbjni
73
+ ${LOG_LIB} # <-- Logcat logger
74
+ ReactAndroid::jsi # <-- RN: JSI
75
+ ReactAndroid::reactnativejni # <-- RN: React Native JNI bindings
76
+ ReactAndroid::react_nativemodule_core # <-- RN: React Native native module core
77
+ android # <-- Android JNI core
78
+ openssl::crypto # <-- OpenSSL (Crypto)
82
79
  )
@@ -1,79 +1,121 @@
1
1
  import java.nio.file.Paths
2
+ import com.android.Version
2
3
 
3
- static def findNodeModules(baseDir) {
4
- def basePath = baseDir.toPath().normalize()
5
- // Node's module resolution algorithm searches up to the root directory,
6
- // after which the base path will be null
7
- while (basePath) {
8
- def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
9
- def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
10
- if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
11
- return nodeModulesPath.toString()
12
- }
13
- basePath = basePath.getParent()
14
- }
15
- throw new GradleException("QuickCrypto: Failed to find node_modules/ path!")
16
- }
17
-
18
- def nodeModules = findNodeModules(projectDir)
19
- logger.warn("QuickCrypto: node_modules/ found at: ${nodeModules}")
4
+ def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
5
+ def agpVersionMajor = agpVersion.tokenize('.')[0].toInteger()
6
+ def agpVersionMinor = agpVersion.tokenize('.')[1].toInteger()
20
7
 
21
8
  buildscript {
22
9
  repositories {
23
- google()
24
- jcenter()
25
10
  maven {
26
11
  url "https://plugins.gradle.org/m2/"
27
12
  }
13
+ mavenCentral()
14
+ google()
28
15
  }
29
16
 
30
17
  dependencies {
31
- classpath 'com.android.tools.build:gradle:4.2.2'
18
+ classpath("com.android.tools.build:gradle:8.3.1")
32
19
  }
33
20
  }
34
21
 
35
- apply plugin: 'com.android.library'
22
+ def isNewArchitectureEnabled() {
23
+ // To opt-in for the New Architecture, you can either:
24
+ // - Set `newArchEnabled` to true inside the `gradle.properties` file
25
+ // - Invoke gradle with `-newArchEnabled=true`
26
+ // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
27
+ return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
28
+ }
29
+
30
+ def resolveBuildType() {
31
+ Gradle gradle = getGradle()
32
+ String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()
36
33
 
37
- def getExtOrDefault(name) {
38
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['QuickCrypto_' + name]
34
+ return tskReqStr.contains('Release') ? 'release' : 'debug'
39
35
  }
40
36
 
41
- def getExtOrIntegerDefault(name) {
42
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['QuickCrypto_' + name]).toInteger()
37
+ if (isNewArchitectureEnabled()) {
38
+ apply plugin: "com.facebook.react"
39
+ }
40
+ apply plugin: 'com.android.library'
41
+
42
+ def safeExtGet(prop, fallback) {
43
+ rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
43
44
  }
44
45
 
45
46
  def reactNativeArchitectures() {
46
- def value = project.getProperties().get("reactNativeArchitectures")
47
- return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
47
+ def value = project.getProperties().get("reactNativeArchitectures")
48
+ return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
48
49
  }
49
50
 
50
- def reactProperties = new Properties()
51
- file("$nodeModules/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
51
+ // static def findNodeModules(baseDir) {
52
+ // def basePath = baseDir.toPath().normalize()
53
+ // // Node's module resolution algorithm searches up to the root directory,
54
+ // // after which the base path will be null
55
+ // while (basePath) {
56
+ // def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
57
+ // def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
58
+ // if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
59
+ // return nodeModulesPath.toString()
60
+ // }
61
+ // basePath = basePath.getParent()
62
+ // }
63
+ // throw new GradleException("react-native-quick-crypto: Failed to find node_modules/ path!")
64
+ // }
65
+
66
+ // def nodeModules = findNodeModules(projectDir)
67
+
68
+ repositories {
69
+ google()
70
+ mavenCentral()
71
+ }
52
72
 
53
73
  android {
54
- compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
55
- buildToolsVersion getExtOrDefault('buildToolsVersion')
56
- ndkVersion getExtOrDefault('ndkVersion')
74
+ compileSdkVersion safeExtGet("compileSdkVersion", 31)
75
+
76
+ if ((agpVersionMajor == 7 && agpVersionMinor >= 3) || agpVersionMajor >= 8) {
77
+ // Namespace support was added in 7.3.0
78
+ namespace "com.margelo.quickcrypto"
79
+
80
+ sourceSets {
81
+ main {
82
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
83
+ }
84
+ }
85
+ }
86
+
87
+ if (agpVersionMajor >= 8) {
88
+ buildFeatures {
89
+ buildConfig = true
90
+ }
91
+ }
92
+
93
+ // Used to override the NDK path/version on internal CI or by allowing
94
+ // users to customize the NDK path/version from their root project (e.g. for M1 support)
95
+ if (rootProject.hasProperty("ndkPath")) {
96
+ ndkPath rootProject.ext.ndkPath
97
+ }
98
+ if (rootProject.hasProperty("ndkVersion")) {
99
+ ndkVersion rootProject.ext.ndkVersion
100
+ }
57
101
 
58
102
  buildFeatures {
59
103
  prefab true
60
104
  }
61
105
 
62
106
  defaultConfig {
63
- minSdkVersion 21
64
- targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
107
+ minSdkVersion safeExtGet('minSdkVersion', 23)
108
+ targetSdkVersion safeExtGet('targetSdkVersion', 31)
65
109
  versionCode 1
66
110
  versionName "1.0"
111
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
67
112
  externalNativeBuild {
68
113
  cmake {
69
- cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID", "-DANDROID"
70
- arguments '-DANDROID_STL=c++_shared',
71
- "-DNODE_MODULES_DIR=${nodeModules}"
114
+ cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
115
+ arguments '-DANDROID_STL=c++_shared' //, "-DNODE_MODULES_DIR=${nodeModules}",
116
+ abiFilters (*reactNativeArchitectures())
72
117
  }
73
118
  }
74
- ndk {
75
- abiFilters (*reactNativeArchitectures())
76
- }
77
119
  }
78
120
 
79
121
  externalNativeBuild {
@@ -83,6 +125,7 @@ android {
83
125
  }
84
126
 
85
127
  packagingOptions {
128
+ doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
86
129
  excludes = [
87
130
  "**/libc++_shared.so",
88
131
  "**/libfbjni.so",
@@ -91,12 +134,13 @@ android {
91
134
  "**/libreact_nativemodule_core.so",
92
135
  "**/libturbomodulejsijni.so",
93
136
  "**/MANIFEST.MF",
94
- ""
95
137
  ]
96
- doNotStrip '**/*.so'
97
138
  }
98
139
 
99
140
  buildTypes {
141
+ release {
142
+ minifyEnabled false
143
+ }
100
144
  debug {
101
145
  packagingOptions {
102
146
  doNotStrip '**/*.so'
@@ -106,11 +150,8 @@ android {
106
150
  jniDebuggable true
107
151
  renderscriptDebuggable true
108
152
  }
109
-
110
- release {
111
- minifyEnabled false
112
- }
113
153
  }
154
+
114
155
  lintOptions {
115
156
  disable 'GradleCompatible'
116
157
  }
@@ -119,9 +160,6 @@ android {
119
160
  targetCompatibility JavaVersion.VERSION_1_8
120
161
  }
121
162
 
122
- buildFeatures {
123
- prefab true
124
- }
125
163
  }
126
164
 
127
165
  repositories {
@@ -130,9 +168,23 @@ repositories {
130
168
  }
131
169
 
132
170
  dependencies {
171
+ //noinspection GradleDynamicVersion
172
+ implementation "com.facebook.react:react-android:+"
133
173
  // https://mvnrepository.com/artifact/com.android.ndk.thirdparty/openssl
134
- implementation 'com.android.ndk.thirdparty:openssl:1.1.1l-beta-1'
135
-
136
- implementation "com.facebook.react:react-android:"
137
- implementation "com.facebook.react:hermes-android:"
174
+ implementation 'com.android.ndk.thirdparty:openssl:1.1.1q-beta-1'
138
175
  }
176
+
177
+ // Resolves "LOCAL_SRC_FILES points to a missing file, Check that libfb.so exists or that its path is correct".
178
+ tasks.whenTaskAdded { task ->
179
+ if (task.name.contains("configureCMakeDebug")) {
180
+ rootProject.getTasksByName("packageReactNdkDebugLibs", true).forEach {
181
+ task.dependsOn(it)
182
+ }
183
+ }
184
+ // We want to add a dependency for both configureCMakeRelease and configureCMakeRelWithDebInfo
185
+ if (task.name.contains("configureCMakeRel")) {
186
+ rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
187
+ task.dependsOn(it)
188
+ }
189
+ }
190
+ }
@@ -1,5 +1,5 @@
1
1
  distributionBase=GRADLE_USER_HOME
2
2
  distributionPath=wrapper/dists
3
- distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-bin.zip
3
+ distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
4
4
  zipStoreBase=GRADLE_USER_HOME
5
5
  zipStorePath=wrapper/dists
@@ -1,6 +1,6 @@
1
- QuickCrypto_compileSdkVersion=30
2
- QuickCrypto_buildToolsVersion=30.0.2
3
- QuickCrypto_targetSdkVersion=30
1
+ QuickCrypto_compileSdkVersion=31
2
+ QuickCrypto_targetSdkVersion=31
3
+ QuickCrypto_ndkversion=21.4.7075529
4
+ QuickCrypto_minSdkVersion=23
5
+
4
6
  android.useAndroidX=true
5
- # Enables Prefab
6
- #android.enablePrefab=true
@@ -1,4 +1,3 @@
1
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
- package="com.margelo.quickcrypto">
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
2
 
4
3
  </manifest>
@@ -329,10 +329,12 @@ void MGLCipherHostObject::installMethods() {
329
329
  ok = EVP_CipherFinal_ex(ctx_, out.getBuffer(runtime).data(runtime),
330
330
  &out_len) == 1;
331
331
 
332
+ // Additional operations for authenticated modes
332
333
  if (ok && isCipher_ && IsAuthenticatedMode()) {
333
- // In GCM mode, the authentication tag length can be specified in
334
- // advance, but defaults to 16 bytes when encrypting. In CCM and OCB
335
- // mode, it must always be given by the user.
334
+ // In GCM mode: default to 16 bytes.
335
+ // In CCM, OCB mode: must be provided by user.
336
+
337
+ // Logic for default auth tag length
336
338
  if (auth_tag_len_ == kNoAuthTagLength) {
337
339
  // TODO(osp) check
338
340
  // CHECK(mode == EVP_CIPH_GCM_MODE);
@@ -391,8 +393,8 @@ void MGLCipherHostObject::installMethods() {
391
393
  plaintext_len =
392
394
  (int)args.getProperty(runtime, "plaintextLength").asNumber();
393
395
  } else {
394
- throw new jsi::JSError(runtime,
395
- "plaintextLength property needs to be a number");
396
+ throw jsi::JSError(runtime,
397
+ "plaintextLength property needs to be a number");
396
398
  }
397
399
  }
398
400
 
@@ -446,6 +448,28 @@ void MGLCipherHostObject::installMethods() {
446
448
  return EVP_CIPHER_CTX_set_padding(ctx_, arguments[0].getBool());
447
449
  }));
448
450
 
451
+
452
+ // getAuthTag
453
+ this->fields.push_back(buildPair(
454
+ "getAuthTag", JSIF([this]) {
455
+ if (ctx_) {
456
+ throw jsi::JSError(runtime, "Cannot getAuthTag while encryption in progress.");
457
+ }
458
+ if (!isCipher_) {
459
+ throw jsi::JSError(runtime, "Cannot getAuthTag in decryption mode.");
460
+ }
461
+ if (auth_tag_len_ == kNoAuthTagLength) {
462
+ throw jsi::JSError(runtime, "Authentication tag not set or not available. Make sure to call 'final' before getting the authentication tag.");
463
+ }
464
+
465
+ MGLTypedArray<MGLTypedArrayKind::Uint8Array> authTagArray(runtime, auth_tag_len_);
466
+ auto buffer = authTagArray.getBuffer(runtime);
467
+ auto dataPtr = buffer.data(runtime);
468
+ std::memcpy(dataPtr, auth_tag_, auth_tag_len_);
469
+
470
+ return authTagArray;
471
+ }));
472
+
449
473
  // setAuthTag
450
474
  this->fields.push_back(buildPair(
451
475
  "setAuthTag", JSIF([=]) {
@@ -577,6 +601,8 @@ bool MGLCipherHostObject::InitAuthenticated(const char *cipher_type, int iv_len,
577
601
  // TODO(tniessen) Support CCM decryption in FIPS mode
578
602
 
579
603
  #if OPENSSL_VERSION_MAJOR >= 3
604
+ // TODO: not sure where kind_ comes from in next line, but as we bump
605
+ // OpenSSL version we will need to look at Node.js code and figure it out.
580
606
  if (mode == EVP_CIPH_CCM_MODE && kind_ == kDecipher &&
581
607
  EVP_default_properties_is_fips_enabled(nullptr)) {
582
608
  #else