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

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 (298) hide show
  1. package/QuickCrypto.podspec +34 -0
  2. package/android/CMakeLists.txt +32 -64
  3. package/android/build.gradle +52 -125
  4. package/android/gradle.properties +4 -4
  5. package/android/src/main/cpp/cpp-adapter.cpp +9 -54
  6. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
  7. package/ios/QuickCryptoOnLoad.mm +19 -0
  8. package/lib/commonjs/index.js +39 -39
  9. package/lib/commonjs/index.js.map +1 -1
  10. package/lib/commonjs/random.js +26 -12
  11. package/lib/commonjs/random.js.map +1 -1
  12. package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
  13. package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
  14. package/lib/commonjs/utils/conversion.js +22 -0
  15. package/lib/commonjs/utils/conversion.js.map +1 -0
  16. package/lib/commonjs/utils/index.js +12 -0
  17. package/lib/commonjs/utils/index.js.map +1 -0
  18. package/lib/commonjs/utils/types.js +2 -0
  19. package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
  20. package/lib/module/index.js +38 -38
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/module/random.js +26 -12
  23. package/lib/module/random.js.map +1 -1
  24. package/lib/module/specs/random.nitro.js +4 -0
  25. package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
  26. package/lib/module/utils/conversion.js +16 -0
  27. package/lib/module/utils/conversion.js.map +1 -0
  28. package/lib/module/utils/index.js +8 -0
  29. package/lib/module/utils/index.js.map +1 -0
  30. package/lib/module/utils/types.js +2 -0
  31. package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
  32. package/lib/tsconfig.tsbuildinfo +1 -0
  33. package/lib/typescript/index.d.ts +10 -141
  34. package/lib/typescript/index.d.ts.map +1 -1
  35. package/lib/typescript/random.d.ts +4 -5
  36. package/lib/typescript/random.d.ts.map +1 -1
  37. package/lib/typescript/specs/random.nitro.d.ts +9 -0
  38. package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
  39. package/lib/typescript/utils/conversion.d.ts +4 -0
  40. package/lib/typescript/utils/conversion.d.ts.map +1 -0
  41. package/lib/typescript/utils/index.d.ts +6 -0
  42. package/lib/typescript/utils/index.d.ts.map +1 -0
  43. package/lib/typescript/utils/types.d.ts +5 -0
  44. package/lib/typescript/utils/types.d.ts.map +1 -0
  45. package/package.json +54 -108
  46. package/src/index.ts +38 -48
  47. package/src/random.ts +51 -48
  48. package/src/specs/random.nitro.ts +14 -0
  49. package/src/utils/conversion.ts +16 -0
  50. package/src/utils/index.ts +6 -0
  51. package/src/utils/types.ts +15 -0
  52. package/LICENSE +0 -27
  53. package/README.md +0 -203
  54. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
  55. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
  56. package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
  57. package/cpp/Cipher/MGLCipherHostObject.h +0 -90
  58. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
  59. package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
  60. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
  61. package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
  62. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
  63. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
  64. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
  65. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
  66. package/cpp/Cipher/MGLPublicCipher.h +0 -124
  67. package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
  68. package/cpp/Cipher/MGLRsa.cpp +0 -573
  69. package/cpp/Cipher/MGLRsa.h +0 -109
  70. package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
  71. package/cpp/HMAC/MGLHmacHostObject.h +0 -39
  72. package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
  73. package/cpp/HMAC/MGLHmacInstaller.h +0 -20
  74. package/cpp/Hash/MGLHashHostObject.cpp +0 -147
  75. package/cpp/Hash/MGLHashHostObject.h +0 -53
  76. package/cpp/Hash/MGLHashInstaller.cpp +0 -44
  77. package/cpp/Hash/MGLHashInstaller.h +0 -20
  78. package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
  79. package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
  80. package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
  81. package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
  82. package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
  83. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
  84. package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
  85. package/cpp/JSIUtils/MGLTypedArray.h +0 -160
  86. package/cpp/MGLKeys.cpp +0 -1426
  87. package/cpp/MGLKeys.h +0 -203
  88. package/cpp/MGLQuickCryptoHostObject.cpp +0 -127
  89. package/cpp/MGLQuickCryptoHostObject.h +0 -30
  90. package/cpp/Random/MGLRandomHostObject.cpp +0 -96
  91. package/cpp/Random/MGLRandomHostObject.h +0 -27
  92. package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
  93. package/cpp/Sig/MGLSignHostObjects.h +0 -128
  94. package/cpp/Sig/MGLSignInstaller.cpp +0 -24
  95. package/cpp/Sig/MGLSignInstaller.h +0 -29
  96. package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
  97. package/cpp/Sig/MGLVerifyInstaller.h +0 -22
  98. package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
  99. package/cpp/Utils/MGLDispatchQueue.h +0 -55
  100. package/cpp/Utils/MGLUtils.cpp +0 -295
  101. package/cpp/Utils/MGLUtils.h +0 -358
  102. package/cpp/Utils/base64.h +0 -320
  103. package/cpp/Utils/logs.h +0 -38
  104. package/cpp/Utils/node.h +0 -13
  105. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
  106. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
  107. package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
  108. package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
  109. package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
  110. package/cpp/webcrypto/MGLWebCrypto.h +0 -32
  111. package/cpp/webcrypto/crypto_aes.cpp +0 -516
  112. package/cpp/webcrypto/crypto_aes.h +0 -79
  113. package/cpp/webcrypto/crypto_ec.cpp +0 -424
  114. package/cpp/webcrypto/crypto_ec.h +0 -78
  115. package/cpp/webcrypto/crypto_keygen.cpp +0 -86
  116. package/cpp/webcrypto/crypto_keygen.h +0 -38
  117. package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
  118. package/ios/QuickCryptoModule.h +0 -5
  119. package/ios/QuickCryptoModule.mm +0 -42
  120. package/lib/commonjs/Cipher.js +0 -502
  121. package/lib/commonjs/Cipher.js.map +0 -1
  122. package/lib/commonjs/Hash.js +0 -99
  123. package/lib/commonjs/Hash.js.map +0 -1
  124. package/lib/commonjs/Hashnames.js +0 -87
  125. package/lib/commonjs/Hashnames.js.map +0 -1
  126. package/lib/commonjs/Hmac.js +0 -79
  127. package/lib/commonjs/Hmac.js.map +0 -1
  128. package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
  129. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
  130. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
  131. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  132. package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
  133. package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
  134. package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
  135. package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
  136. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
  137. package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
  138. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
  139. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
  140. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
  141. package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
  142. package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
  143. package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
  144. package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
  145. package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
  146. package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
  147. package/lib/commonjs/Utils.js +0 -608
  148. package/lib/commonjs/Utils.js.map +0 -1
  149. package/lib/commonjs/aes.js +0 -281
  150. package/lib/commonjs/aes.js.map +0 -1
  151. package/lib/commonjs/constants.js +0 -85
  152. package/lib/commonjs/constants.js.map +0 -1
  153. package/lib/commonjs/ec.js +0 -276
  154. package/lib/commonjs/ec.js.map +0 -1
  155. package/lib/commonjs/keygen.js +0 -56
  156. package/lib/commonjs/keygen.js.map +0 -1
  157. package/lib/commonjs/keys.js +0 -492
  158. package/lib/commonjs/keys.js.map +0 -1
  159. package/lib/commonjs/pbkdf2.js +0 -90
  160. package/lib/commonjs/pbkdf2.js.map +0 -1
  161. package/lib/commonjs/rsa.js +0 -248
  162. package/lib/commonjs/rsa.js.map +0 -1
  163. package/lib/commonjs/sig.js +0 -129
  164. package/lib/commonjs/sig.js.map +0 -1
  165. package/lib/commonjs/subtle.js +0 -448
  166. package/lib/commonjs/subtle.js.map +0 -1
  167. package/lib/commonjs/webcrypto.js +0 -14
  168. package/lib/commonjs/webcrypto.js.map +0 -1
  169. package/lib/module/Cipher.js +0 -491
  170. package/lib/module/Cipher.js.map +0 -1
  171. package/lib/module/Hash.js +0 -93
  172. package/lib/module/Hash.js.map +0 -1
  173. package/lib/module/Hashnames.js +0 -85
  174. package/lib/module/Hashnames.js.map +0 -1
  175. package/lib/module/Hmac.js +0 -74
  176. package/lib/module/Hmac.js.map +0 -1
  177. package/lib/module/NativeQuickCrypto/Cipher.js +0 -26
  178. package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
  179. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -49
  180. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  181. package/lib/module/NativeQuickCrypto/aes.js +0 -4
  182. package/lib/module/NativeQuickCrypto/hash.js +0 -2
  183. package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
  184. package/lib/module/NativeQuickCrypto/hmac.js +0 -2
  185. package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
  186. package/lib/module/NativeQuickCrypto/keygen.js +0 -4
  187. package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
  188. package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
  189. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
  190. package/lib/module/NativeQuickCrypto/random.js +0 -2
  191. package/lib/module/NativeQuickCrypto/random.js.map +0 -1
  192. package/lib/module/NativeQuickCrypto/rsa.js +0 -4
  193. package/lib/module/NativeQuickCrypto/sig.js +0 -17
  194. package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
  195. package/lib/module/NativeQuickCrypto/webcrypto.js +0 -4
  196. package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
  197. package/lib/module/Utils.js +0 -539
  198. package/lib/module/Utils.js.map +0 -1
  199. package/lib/module/aes.js +0 -274
  200. package/lib/module/aes.js.map +0 -1
  201. package/lib/module/constants.js +0 -81
  202. package/lib/module/constants.js.map +0 -1
  203. package/lib/module/ec.js +0 -267
  204. package/lib/module/ec.js.map +0 -1
  205. package/lib/module/keygen.js +0 -49
  206. package/lib/module/keygen.js.map +0 -1
  207. package/lib/module/keys.js +0 -477
  208. package/lib/module/keys.js.map +0 -1
  209. package/lib/module/pbkdf2.js +0 -84
  210. package/lib/module/pbkdf2.js.map +0 -1
  211. package/lib/module/rsa.js +0 -242
  212. package/lib/module/rsa.js.map +0 -1
  213. package/lib/module/sig.js +0 -124
  214. package/lib/module/sig.js.map +0 -1
  215. package/lib/module/subtle.js +0 -443
  216. package/lib/module/subtle.js.map +0 -1
  217. package/lib/module/webcrypto.js +0 -10
  218. package/lib/module/webcrypto.js.map +0 -1
  219. package/lib/typescript/Cipher.d.ts +0 -81
  220. package/lib/typescript/Cipher.d.ts.map +0 -1
  221. package/lib/typescript/Hash.d.ts +0 -44
  222. package/lib/typescript/Hash.d.ts.map +0 -1
  223. package/lib/typescript/Hashnames.d.ts +0 -11
  224. package/lib/typescript/Hashnames.d.ts.map +0 -1
  225. package/lib/typescript/Hmac.d.ts +0 -37
  226. package/lib/typescript/Hmac.d.ts.map +0 -1
  227. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
  228. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
  229. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
  230. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
  231. package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
  232. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
  233. package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
  234. package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
  235. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
  236. package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
  237. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
  238. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
  239. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
  240. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
  241. package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
  242. package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
  243. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
  244. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
  245. package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
  246. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
  247. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
  248. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
  249. package/lib/typescript/Utils.d.ts +0 -48
  250. package/lib/typescript/Utils.d.ts.map +0 -1
  251. package/lib/typescript/aes.d.ts +0 -22
  252. package/lib/typescript/aes.d.ts.map +0 -1
  253. package/lib/typescript/constants.d.ts +0 -76
  254. package/lib/typescript/constants.d.ts.map +0 -1
  255. package/lib/typescript/ec.d.ts +0 -7
  256. package/lib/typescript/ec.d.ts.map +0 -1
  257. package/lib/typescript/keygen.d.ts +0 -6
  258. package/lib/typescript/keygen.d.ts.map +0 -1
  259. package/lib/typescript/keys.d.ts +0 -206
  260. package/lib/typescript/keys.d.ts.map +0 -1
  261. package/lib/typescript/pbkdf2.d.ts +0 -12
  262. package/lib/typescript/pbkdf2.d.ts.map +0 -1
  263. package/lib/typescript/rsa.d.ts +0 -12
  264. package/lib/typescript/rsa.d.ts.map +0 -1
  265. package/lib/typescript/sig.d.ts +0 -21
  266. package/lib/typescript/sig.d.ts.map +0 -1
  267. package/lib/typescript/subtle.d.ts +0 -15
  268. package/lib/typescript/subtle.d.ts.map +0 -1
  269. package/lib/typescript/webcrypto.d.ts +0 -9
  270. package/lib/typescript/webcrypto.d.ts.map +0 -1
  271. package/react-native-quick-crypto.podspec +0 -40
  272. package/src/Cipher.ts +0 -832
  273. package/src/Hash.ts +0 -132
  274. package/src/Hashnames.ts +0 -93
  275. package/src/Hmac.ts +0 -106
  276. package/src/NativeQuickCrypto/Cipher.ts +0 -102
  277. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
  278. package/src/NativeQuickCrypto/aes.ts +0 -14
  279. package/src/NativeQuickCrypto/hash.ts +0 -10
  280. package/src/NativeQuickCrypto/hmac.ts +0 -9
  281. package/src/NativeQuickCrypto/keygen.ts +0 -7
  282. package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
  283. package/src/NativeQuickCrypto/random.ts +0 -12
  284. package/src/NativeQuickCrypto/rsa.ts +0 -12
  285. package/src/NativeQuickCrypto/sig.ts +0 -44
  286. package/src/NativeQuickCrypto/webcrypto.ts +0 -72
  287. package/src/Utils.ts +0 -777
  288. package/src/aes.ts +0 -402
  289. package/src/constants.ts +0 -79
  290. package/src/ec.ts +0 -375
  291. package/src/keygen.ts +0 -80
  292. package/src/keys.ts +0 -787
  293. package/src/pbkdf2.ts +0 -169
  294. package/src/rsa.ts +0 -370
  295. package/src/sig.ts +0 -164
  296. package/src/subtle.ts +0 -639
  297. package/src/webcrypto.ts +0 -8
  298. /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
@@ -1,125 +0,0 @@
1
- //
2
- // MGLWebCrypto.cpp
3
- // react-native-quick-crypto
4
- //
5
- // Created by Oscar Franco on 1/12/23.
6
- //
7
-
8
- #include "MGLWebCrypto.h"
9
-
10
- #include <memory>
11
- #include <utility>
12
- #include "MGLKeys.h"
13
-
14
- #ifdef ANDROID
15
- #include "JSIUtils/MGLJSIMacros.h"
16
- #include "Sig/MGLSignHostObjects.h"
17
- #include "Cipher/MGLRsa.h"
18
- #include "Utils/MGLUtils.h"
19
- #include "webcrypto/crypto_aes.h"
20
- #include "webcrypto/crypto_ec.h"
21
- #include "webcrypto/crypto_keygen.h"
22
- #else
23
- #include "MGLJSIMacros.h"
24
- #include "MGLSignHostObjects.h"
25
- #include "MGLRsa.h"
26
- #include "MGLUtils.h"
27
- #include "crypto_aes.h"
28
- #include "crypto_ec.h"
29
- #include "crypto_keygen.h"
30
- #endif
31
-
32
- namespace margelo {
33
-
34
- namespace jsi = facebook::jsi;
35
- namespace react = facebook::react;
36
-
37
- MGLWebCryptoHostObject::MGLWebCryptoHostObject(
38
- std::shared_ptr<react::CallInvoker> jsCallInvoker,
39
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
40
- : MGLSmartHostObject(jsCallInvoker, workerQueue) {
41
-
42
- auto aesCipher = JSIF([=]) {
43
- auto aes = AESCipher();
44
- auto params = aes.GetParamsFromJS(runtime, arguments);
45
- ByteSource out;
46
- WebCryptoCipherStatus status = aes.DoCipher(params, &out);
47
- if (status != WebCryptoCipherStatus::OK) {
48
- throw jsi::JSError(runtime, "error in DoCipher, status: " +
49
- std::to_string(static_cast<int>(status)));
50
- }
51
- return toJSI(runtime, std::move(out));
52
- };
53
-
54
- auto createKeyObjectHandle = JSIF([=]) {
55
- auto keyObjectHandleHostObject = std::make_shared<KeyObjectHandle>();
56
- return jsi::Object::createFromHostObject(runtime, keyObjectHandleHostObject);
57
- };
58
-
59
- auto ecExportKey = JSIF([=]) {
60
- ByteSource out;
61
- std::shared_ptr<KeyObjectHandle> handle =
62
- std::static_pointer_cast<KeyObjectHandle>(
63
- arguments[1].asObject(runtime).getHostObject(runtime));
64
- std::shared_ptr<KeyObjectData> key_data = handle->Data();
65
- WebCryptoKeyExportStatus status = ECDH::doExport(runtime,
66
- key_data,
67
- static_cast<WebCryptoKeyFormat>(arguments[0].asNumber()),
68
- {}, // blank params
69
- &out);
70
- if (status != WebCryptoKeyExportStatus::OK) {
71
- throw jsi::JSError(runtime, "error exporting key, status: " + std::to_string(static_cast<int>(status)));
72
- }
73
- return toJSI(runtime, std::move(out));
74
- };
75
-
76
- auto generateSecretKeySync = JSIF([=]) {
77
- auto skg = new SecretKeyGen();
78
- CHECK(skg->GetParamsFromJS(runtime, arguments));
79
- CHECK(skg->DoKeyGen());
80
- auto out = jsi::Object::createFromHostObject(runtime, skg->GetHandle());
81
- return jsi::Value(std::move(out));
82
- };
83
-
84
- auto rsaCipher = JSIF([=]) {
85
- auto rsa = RSACipher();
86
- auto params = rsa.GetParamsFromJS(runtime, arguments);
87
- ByteSource out;
88
- WebCryptoCipherStatus status = rsa.DoCipher(params, &out);
89
- if (status != WebCryptoCipherStatus::OK) {
90
- throw jsi::JSError(runtime, "error in DoCipher, status: " +
91
- std::to_string(static_cast<int>(status)));
92
- }
93
- return toJSI(runtime, std::move(out));
94
- };
95
-
96
- auto rsaExportKey = JSIF([=]) {
97
- ByteSource out;
98
- auto rsa = new RsaKeyExport();
99
- CHECK(rsa->GetParamsFromJS(runtime, arguments));
100
- WebCryptoKeyExportStatus status = rsa->DoExport(&out);
101
- if (status != WebCryptoKeyExportStatus::OK) {
102
- throw jsi::JSError(runtime, "Error exporting key");
103
- }
104
- return toJSI(runtime, std::move(out));
105
- };
106
-
107
- auto signVerify = JSIF([=]) {
108
- auto ssv = SubtleSignVerify();
109
- auto params = ssv.GetParamsFromJS(runtime, arguments);
110
- ByteSource out;
111
- ssv.DoSignVerify(runtime, params, out);
112
- return ssv.EncodeOutput(runtime, params, out);
113
- };
114
-
115
- this->fields.push_back(buildPair("aesCipher", aesCipher));
116
- this->fields.push_back(buildPair("createKeyObjectHandle", createKeyObjectHandle));
117
- this->fields.push_back(buildPair("ecExportKey", ecExportKey));
118
- this->fields.push_back(GenerateSecretKeyFieldDefinition(jsCallInvoker, workerQueue));
119
- this->fields.push_back(buildPair("generateSecretKeySync", generateSecretKeySync));
120
- this->fields.push_back(buildPair("rsaCipher", rsaCipher));
121
- this->fields.push_back(buildPair("rsaExportKey", rsaExportKey));
122
- this->fields.push_back(buildPair("signVerify", signVerify));
123
- };
124
-
125
- } // namespace margelo
@@ -1,32 +0,0 @@
1
- //
2
- // MGLWebCrypto.hpp
3
- // react-native-quick-crypto
4
- //
5
- // Created by Oscar Franco on 1/12/23.
6
- //
7
-
8
- #ifndef MGLWebCryptoHostObject_h
9
- #define MGLWebCryptoHostObject_h
10
-
11
- #include <jsi/jsi.h>
12
- #include <memory>
13
-
14
- #ifdef ANDROID
15
- #include "JSIUtils/MGLSmartHostObject.h"
16
- #else
17
- #include "MGLSmartHostObject.h"
18
- #endif
19
-
20
- namespace margelo {
21
- namespace jsi = facebook::jsi;
22
-
23
- class MGLWebCryptoHostObject : public MGLSmartHostObject {
24
- public:
25
- MGLWebCryptoHostObject(
26
- std::shared_ptr<react::CallInvoker> jsCallInvoker,
27
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
28
- };
29
-
30
- } // namespace margelo
31
-
32
- #endif /* MGLWebCrypto_hpp */
@@ -1,516 +0,0 @@
1
- #include "crypto_aes.h"
2
-
3
- #ifdef ANDROID
4
- #include "JSIUtils/MGLJSIUtils.h"
5
- #include "Utils/MGLUtils.h"
6
- #else
7
- #include "MGLJSIUtils.h"
8
- #include "MGLUtils.h"
9
- #endif
10
-
11
- namespace margelo {
12
-
13
- namespace {
14
- // Implements general AES encryption and decryption for CBC
15
- // The key_data must be a secret key.
16
- // On success, this function sets out to a new ByteSource
17
- // instance containing the results and returns WebCryptoCipherStatus::OK.
18
- WebCryptoCipherStatus AES_Cipher(const AESCipherConfig& params, ByteSource* out) {
19
- CHECK_NOT_NULL(params.key);
20
- CHECK_EQ(params.key->GetKeyType(), kKeyTypeSecret);
21
-
22
- const int mode = EVP_CIPHER_mode(params.cipher);
23
-
24
- CipherCtxPointer ctx(EVP_CIPHER_CTX_new());
25
- EVP_CIPHER_CTX_init(ctx.get());
26
- if (mode == EVP_CIPH_WRAP_MODE)
27
- EVP_CIPHER_CTX_set_flags(ctx.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
28
-
29
- const bool encrypt = params.mode == AESCipherConfig::Mode::kEncrypt;
30
-
31
- if (!EVP_CipherInit_ex(
32
- ctx.get(),
33
- params.cipher,
34
- nullptr,
35
- nullptr,
36
- nullptr,
37
- encrypt)) {
38
- // Cipher init failed
39
- return WebCryptoCipherStatus::FAILED;
40
- }
41
-
42
- if (mode == EVP_CIPH_GCM_MODE && !EVP_CIPHER_CTX_ctrl(
43
- ctx.get(),
44
- EVP_CTRL_AEAD_SET_IVLEN,
45
- params.iv.size(),
46
- nullptr)) {
47
- return WebCryptoCipherStatus::FAILED;
48
- }
49
-
50
- if (!EVP_CIPHER_CTX_set_key_length(
51
- ctx.get(),
52
- params.key->GetSymmetricKeySize()) ||
53
- !EVP_CipherInit_ex(
54
- ctx.get(),
55
- nullptr,
56
- nullptr,
57
- reinterpret_cast<const unsigned char*>(params.key->GetSymmetricKey().c_str()),
58
- params.iv.data<unsigned char>(),
59
- encrypt)) {
60
- return WebCryptoCipherStatus::FAILED;
61
- }
62
-
63
- size_t tag_len = 0;
64
-
65
- if (mode == EVP_CIPH_GCM_MODE) {
66
- switch (params.mode) {
67
- case AESCipherConfig::Mode::kDecrypt:
68
- // If in decrypt mode, the auth tag must be set in the params.tag.
69
- CHECK(params.tag);
70
- if (!EVP_CIPHER_CTX_ctrl(ctx.get(),
71
- EVP_CTRL_AEAD_SET_TAG,
72
- params.tag.size(),
73
- const_cast<char*>(params.tag.data<char>()))) {
74
- return WebCryptoCipherStatus::FAILED;
75
- }
76
- break;
77
- case AESCipherConfig::Mode::kEncrypt:
78
- // In decrypt mode, we grab the tag length here. We'll use it to
79
- // ensure that that allocated buffer has enough room for both the
80
- // final block and the auth tag. Unlike our other AES-GCM implementation
81
- // in CipherBase, in WebCrypto, the auth tag is concatenated to the end
82
- // of the generated ciphertext and returned in the same ArrayBuffer.
83
- tag_len = params.length;
84
- break;
85
- default:
86
- throw std::runtime_error("Unreachable code in AES_Cipher");
87
- }
88
- }
89
-
90
- size_t total = 0;
91
- int buf_len = params.data.size() + EVP_CIPHER_CTX_block_size(ctx.get()) + tag_len;
92
- int out_len;
93
-
94
- if (mode == EVP_CIPH_GCM_MODE &&
95
- params.additional_data.size() &&
96
- !EVP_CipherUpdate(
97
- ctx.get(),
98
- nullptr,
99
- &out_len,
100
- params.additional_data.data<unsigned char>(),
101
- params.additional_data.size())) {
102
- return WebCryptoCipherStatus::FAILED;
103
- }
104
-
105
- ByteSource::Builder buf(buf_len);
106
-
107
- // In some outdated version of OpenSSL (e.g.
108
- // ubi81_sharedlibs_openssl111fips_x64) may be used in sharedlib mode, the
109
- // logic will be failed when input size is zero. The newly OpenSSL has fixed
110
- // it up. But we still have to regard zero as special in Node.js code to
111
- // prevent old OpenSSL failure.
112
- //
113
- // Refs: https://github.com/openssl/openssl/commit/420cb707b880e4fb649094241371701013eeb15f
114
- // Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244
115
- if (params.data.size() == 0) {
116
- out_len = 0;
117
- } else if (!EVP_CipherUpdate(ctx.get(),
118
- buf.data<unsigned char>(),
119
- &out_len,
120
- params.data.data<unsigned char>(),
121
- params.data.size())) {
122
- return WebCryptoCipherStatus::FAILED;
123
- }
124
-
125
- total += out_len;
126
- CHECK_LE(out_len, buf_len);
127
- out_len = EVP_CIPHER_CTX_block_size(ctx.get());
128
- if (!EVP_CipherFinal_ex(
129
- ctx.get(), buf.data<unsigned char>() + total, &out_len)) {
130
- return WebCryptoCipherStatus::FAILED;
131
- }
132
- total += out_len;
133
-
134
- // If using AES_GCM, grab the generated auth tag and append
135
- // it to the end of the ciphertext.
136
- if (params.mode == AESCipherConfig::Mode::kEncrypt && mode == EVP_CIPH_GCM_MODE) {
137
- if (!EVP_CIPHER_CTX_ctrl(ctx.get(),
138
- EVP_CTRL_AEAD_GET_TAG,
139
- tag_len,
140
- buf.data<unsigned char>() + total))
141
- return WebCryptoCipherStatus::FAILED;
142
- total += tag_len;
143
- }
144
-
145
- // It's possible that we haven't used the full allocated space. Size down.
146
- *out = std::move(buf).release(total);
147
-
148
- return WebCryptoCipherStatus::OK;
149
- }
150
-
151
- // The AES_CTR implementation here takes it's inspiration from the chromium
152
- // implementation here:
153
- // https://github.com/chromium/chromium/blob/7af6cfd/components/webcrypto/algorithms/aes_ctr.cc
154
-
155
- template <typename T>
156
- T CeilDiv(T a, T b) {
157
- return a == 0 ? 0 : 1 + (a - 1) / b;
158
- }
159
-
160
- BignumPointer GetCounter(const AESCipherConfig& params) {
161
- unsigned int remainder = (params.length % CHAR_BIT);
162
- const unsigned char* data = params.iv.data<unsigned char>();
163
-
164
- if (remainder == 0) {
165
- unsigned int byte_length = params.length / CHAR_BIT;
166
- return BignumPointer(BN_bin2bn(
167
- data + params.iv.size() - byte_length,
168
- byte_length,
169
- nullptr));
170
- }
171
-
172
- unsigned int byte_length =
173
- CeilDiv(params.length, static_cast<size_t>(CHAR_BIT));
174
-
175
- std::vector<unsigned char> counter(
176
- data + params.iv.size() - byte_length,
177
- data + params.iv.size());
178
- counter[0] &= ~(0xFF << remainder);
179
-
180
- return BignumPointer(BN_bin2bn(counter.data(), counter.size(), nullptr));
181
- }
182
-
183
- std::vector<unsigned char> BlockWithZeroedCounter(
184
- const AESCipherConfig& params) {
185
- unsigned int length_bytes = params.length / CHAR_BIT;
186
- unsigned int remainder = params.length % CHAR_BIT;
187
-
188
- const unsigned char* data = params.iv.data<unsigned char>();
189
-
190
- std::vector<unsigned char> new_counter_block(data, data + params.iv.size());
191
-
192
- size_t index = new_counter_block.size() - length_bytes;
193
- memset(&new_counter_block.front() + index, 0, length_bytes);
194
-
195
- if (remainder)
196
- new_counter_block[index - 1] &= 0xFF << remainder;
197
-
198
- return new_counter_block;
199
- }
200
-
201
- WebCryptoCipherStatus AES_CTR_Cipher2(
202
- const AESCipherConfig& params,
203
- const ByteSource &in,
204
- unsigned const char* counter,
205
- unsigned char* out) {
206
- CipherCtxPointer ctx(EVP_CIPHER_CTX_new());
207
- const bool encrypt = params.mode == AESCipherConfig::Mode::kEncrypt;
208
-
209
- if (!EVP_CipherInit_ex(
210
- ctx.get(),
211
- params.cipher,
212
- nullptr,
213
- reinterpret_cast<const unsigned char*>(params.key->GetSymmetricKey().c_str()),
214
- counter,
215
- encrypt)) {
216
- // Cipher init failed
217
- return WebCryptoCipherStatus::FAILED;
218
- }
219
-
220
- int out_len = 0;
221
- int final_len = 0;
222
- if (!EVP_CipherUpdate(
223
- ctx.get(),
224
- out,
225
- &out_len,
226
- params.data.data<unsigned char>(),
227
- params.data.size())) {
228
- return WebCryptoCipherStatus::FAILED;
229
- }
230
-
231
- if (!EVP_CipherFinal_ex(ctx.get(), out + out_len, &final_len))
232
- return WebCryptoCipherStatus::FAILED;
233
-
234
- out_len += final_len;
235
- if (static_cast<unsigned>(out_len) != params.data.size())
236
- return WebCryptoCipherStatus::FAILED;
237
-
238
- return WebCryptoCipherStatus::OK;
239
- }
240
-
241
- WebCryptoCipherStatus AES_CTR_Cipher(
242
- const AESCipherConfig& params,
243
- ByteSource* out) {
244
- BignumPointer num_counters(BN_new());
245
- if (!BN_lshift(num_counters.get(), BN_value_one(), params.length))
246
- return WebCryptoCipherStatus::FAILED;
247
-
248
- BignumPointer current_counter = GetCounter(params);
249
-
250
- BignumPointer num_output(BN_new());
251
-
252
- if (!BN_set_word(num_output.get(), CeilDiv(params.data.size(), kAesBlockSize)))
253
- return WebCryptoCipherStatus::FAILED;
254
-
255
- // Just like in chromium's implementation, if the counter will
256
- // be incremented more than there are counter values, we fail.
257
- if (BN_cmp(num_output.get(), num_counters.get()) > 0)
258
- return WebCryptoCipherStatus::FAILED;
259
-
260
- BignumPointer remaining_until_reset(BN_new());
261
- if (!BN_sub(remaining_until_reset.get(),
262
- num_counters.get(),
263
- current_counter.get())) {
264
- return WebCryptoCipherStatus::FAILED;
265
- }
266
-
267
- // Output size is identical to the input size.
268
- ByteSource::Builder buf(params.data.size());
269
-
270
- // Also just like in chromium's implementation, if we can process
271
- // the input without wrapping the counter, we'll do it as a single
272
- // call here. If we can't, we'll fallback to the a two-step approach
273
- if (BN_cmp(remaining_until_reset.get(), num_output.get()) >= 0) {
274
- auto status = AES_CTR_Cipher2(params,
275
- params.data,
276
- params.iv.data<unsigned char>(),
277
- buf.data<unsigned char>());
278
- if (status == WebCryptoCipherStatus::OK) *out = std::move(buf).release();
279
- return status;
280
- }
281
-
282
- BN_ULONG blocks_part1 = BN_get_word(remaining_until_reset.get());
283
- BN_ULONG input_size_part1 = blocks_part1 * kAesBlockSize;
284
-
285
- // Encrypt the first part...
286
- auto status =
287
- AES_CTR_Cipher2(params,
288
- ByteSource::Foreign(params.data.data<char>(), input_size_part1),
289
- params.iv.data<unsigned char>(),
290
- buf.data<unsigned char>());
291
-
292
- if (status != WebCryptoCipherStatus::OK)
293
- return status;
294
-
295
- // Wrap the counter around to zero
296
- std::vector<unsigned char> new_counter_block = BlockWithZeroedCounter(params);
297
-
298
- // Encrypt the second part...
299
- status =
300
- AES_CTR_Cipher2(params,
301
- ByteSource::Foreign(params.data.data<char>() + input_size_part1,
302
- params.data.size() - input_size_part1),
303
- new_counter_block.data(),
304
- buf.data<unsigned char>() + input_size_part1);
305
-
306
- if (status == WebCryptoCipherStatus::OK) *out = std::move(buf).release();
307
-
308
- return status;
309
- }
310
-
311
- bool ValidateIV(
312
- jsi::Runtime &rt,
313
- const jsi::Value &value,
314
- AESCipherConfig *params) {
315
- params->iv = GetByteSourceFromJS(rt, value, "iv");
316
- return true;
317
- }
318
-
319
- bool ValidateCounter(
320
- jsi::Runtime &rt,
321
- const jsi::Value &value,
322
- AESCipherConfig* params) {
323
- CHECK(CheckIsUint32(value)); // Length
324
- params->length = (uint32_t)value.asNumber();
325
- if (params->iv.size() != 16 ||
326
- params->length == 0 ||
327
- params->length > 128) {
328
- throw std::runtime_error("Invalid counter (AES)");
329
- return false;
330
- }
331
- return true;
332
- }
333
-
334
- bool ValidateAuthTag(
335
- jsi::Runtime &rt,
336
- AESCipherConfig::Mode cipher_mode,
337
- const jsi::Value &value_len,
338
- const jsi::Value &value_auth_tag,
339
- AESCipherConfig *params) {
340
- switch (cipher_mode) {
341
- case AESCipherConfig::Mode::kDecrypt: {
342
- ByteSource tag = GetByteSourceFromJS(rt, value_auth_tag, "auth_tag");
343
- params->tag = std::move(tag);
344
- break;
345
- }
346
- case AESCipherConfig::Mode::kEncrypt: {
347
- CHECK(CheckIsUint32(value_len)); // Length
348
- params->length = (uint32_t)value_len.asNumber();
349
- if (params->length > 128) {
350
- throw std::runtime_error("Invalid tag length (AES)");
351
- return false;
352
- }
353
- break;
354
- }
355
- default:
356
- throw std::runtime_error("Unreachable code in ValidateAuthTag (AES)");
357
- }
358
- return true;
359
- }
360
-
361
- bool ValidateAdditionalData(
362
- jsi::Runtime &rt,
363
- const jsi::Value &value,
364
- AESCipherConfig *params) {
365
- // Additional Data
366
- params->additional_data = GetByteSourceFromJS(rt, value, "additional_data");
367
- return true;
368
- }
369
-
370
- void UseDefaultIV(AESCipherConfig* params) {
371
- params->iv = ByteSource::Foreign(kDefaultWrapIV, strlen(kDefaultWrapIV));
372
- }
373
-
374
- } // namespace
375
-
376
- AESCipherConfig AESCipher::GetParamsFromJS(jsi::Runtime &rt,
377
- const jsi::Value *args) {
378
- AESCipherConfig params;
379
- unsigned int offset = 0;
380
-
381
- // mode (encrypt/decrypt)
382
- AESCipherConfig::Mode mode =
383
- static_cast<AESCipherConfig::Mode>(args[offset].getNumber());
384
- params.mode = mode;
385
- offset++;
386
-
387
- // key (handle)
388
- if (!args[offset].isObject()) {
389
- throw std::runtime_error("arg is not a KeyObjectHandle: key");
390
- }
391
- std::shared_ptr<KeyObjectHandle> handle =
392
- std::static_pointer_cast<KeyObjectHandle>(
393
- args[offset].asObject(rt).getHostObject(rt));
394
- params.key = handle->Data();
395
- offset++;
396
-
397
- // data
398
- params.data = GetByteSourceFromJS(rt, args[offset], "data");
399
- offset++;
400
-
401
- // AES Key Variant
402
- if (CheckIsInt32(args[offset])) {
403
- params.variant = static_cast<AESKeyVariant>(args[offset].asNumber());
404
- }
405
- // offset++; // The below variant-dependent params advance offset themselves
406
-
407
- // cipher
408
- int cipher_nid;
409
-
410
- switch (params.variant) {
411
- case kKeyVariantAES_CTR_128:
412
- if (!ValidateIV(rt, args[offset + 1], &params) ||
413
- !ValidateCounter(rt, args[offset + 2], &params)) {
414
- return params;
415
- }
416
- cipher_nid = NID_aes_128_ctr;
417
- break;
418
- case kKeyVariantAES_CTR_192:
419
- if (!ValidateIV(rt, args[offset + 1], &params) ||
420
- !ValidateCounter(rt, args[offset + 2], &params)) {
421
- return params;
422
- }
423
- cipher_nid = NID_aes_192_ctr;
424
- break;
425
- case kKeyVariantAES_CTR_256:
426
- if (!ValidateIV(rt, args[offset + 1], &params) ||
427
- !ValidateCounter(rt, args[offset + 2], &params)) {
428
- return params;
429
- }
430
- cipher_nid = NID_aes_256_ctr;
431
- break;
432
- case kKeyVariantAES_CBC_128:
433
- if (!ValidateIV(rt, args[offset + 1], &params))
434
- return params;
435
- cipher_nid = NID_aes_128_cbc;
436
- break;
437
- case kKeyVariantAES_CBC_192:
438
- if (!ValidateIV(rt, args[offset + 1], &params))
439
- return params;
440
- cipher_nid = NID_aes_192_cbc;
441
- break;
442
- case kKeyVariantAES_CBC_256:
443
- if (!ValidateIV(rt, args[offset + 1], &params))
444
- return params;
445
- cipher_nid = NID_aes_256_cbc;
446
- break;
447
- case kKeyVariantAES_KW_128:
448
- UseDefaultIV(&params);
449
- cipher_nid = NID_id_aes128_wrap;
450
- break;
451
- case kKeyVariantAES_KW_192:
452
- UseDefaultIV(&params);
453
- cipher_nid = NID_id_aes192_wrap;
454
- break;
455
- case kKeyVariantAES_KW_256:
456
- UseDefaultIV(&params);
457
- cipher_nid = NID_id_aes256_wrap;
458
- break;
459
- case kKeyVariantAES_GCM_128:
460
- if (!ValidateIV(rt, args[offset + 1], &params) ||
461
- !ValidateAuthTag(rt, mode, args[offset + 2], args[offset + 3], &params) ||
462
- !ValidateAdditionalData(rt, args[offset + 4], &params)) {
463
- return params;
464
- }
465
- cipher_nid = NID_aes_128_gcm;
466
- break;
467
- case kKeyVariantAES_GCM_192:
468
- if (!ValidateIV(rt, args[offset + 1], &params) ||
469
- !ValidateAuthTag(rt, mode, args[offset + 2], args[offset + 3], &params) ||
470
- !ValidateAdditionalData(rt, args[offset + 4], &params)) {
471
- return params;
472
- }
473
- cipher_nid = NID_aes_192_gcm;
474
- break;
475
- case kKeyVariantAES_GCM_256:
476
- if (!ValidateIV(rt, args[offset + 1], &params) ||
477
- !ValidateAuthTag(rt, mode, args[offset + 2], args[offset + 3], &params) ||
478
- !ValidateAdditionalData(rt, args[offset + 4], &params)) {
479
- return params;
480
- }
481
- cipher_nid = NID_aes_256_gcm;
482
- break;
483
- default:
484
- throw std::runtime_error("Unreachable code in GetParamsFromJS (AES)");
485
- }
486
-
487
- params.cipher = EVP_get_cipherbynid(cipher_nid);
488
- if (params.cipher == nullptr) {
489
- throw std::runtime_error("Unknown cipher (AES)");
490
- return params;
491
- }
492
-
493
- if (params.iv.size() <
494
- static_cast<size_t>(EVP_CIPHER_iv_length(params.cipher))) {
495
- throw std::runtime_error("Invalid IV length (AES)");
496
- return params;
497
- }
498
-
499
- return params;
500
- }
501
-
502
- WebCryptoCipherStatus AESCipher::DoCipher(const AESCipherConfig &params,
503
- ByteSource *out) {
504
- // TODO: threading / async here, as we don't have jsi::Runtime
505
- #define V(name, fn) \
506
- case kKeyVariantAES_ ## name: \
507
- return fn(params, out);
508
- switch (params.variant) {
509
- VARIANTS(V)
510
- default:
511
- throw std::runtime_error("Unreachable code in DoCipher (AES)");
512
- }
513
- #undef V
514
- }
515
-
516
- } // namespace margelo