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

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 (309) hide show
  1. package/QuickCrypto.podspec +34 -0
  2. package/README.md +13 -46
  3. package/android/CMakeLists.txt +32 -64
  4. package/android/build.gradle +52 -125
  5. package/android/gradle.properties +4 -4
  6. package/android/src/main/cpp/cpp-adapter.cpp +9 -54
  7. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
  8. package/cpp/random/HybridRandom.cpp +34 -0
  9. package/cpp/random/HybridRandom.hpp +44 -0
  10. package/cpp/utils/Utils.hpp +9 -0
  11. package/ios/QuickCryptoOnLoad.mm +19 -0
  12. package/lib/commonjs/index.js +39 -39
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/random.js +26 -12
  15. package/lib/commonjs/random.js.map +1 -1
  16. package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
  17. package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
  18. package/lib/commonjs/utils/conversion.js +22 -0
  19. package/lib/commonjs/utils/conversion.js.map +1 -0
  20. package/lib/commonjs/utils/index.js +12 -0
  21. package/lib/commonjs/utils/index.js.map +1 -0
  22. package/lib/commonjs/utils/types.js +2 -0
  23. package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
  24. package/lib/module/index.js +38 -38
  25. package/lib/module/index.js.map +1 -1
  26. package/lib/module/random.js +26 -12
  27. package/lib/module/random.js.map +1 -1
  28. package/lib/module/specs/random.nitro.js +4 -0
  29. package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
  30. package/lib/module/utils/conversion.js +16 -0
  31. package/lib/module/utils/conversion.js.map +1 -0
  32. package/lib/module/utils/index.js +8 -0
  33. package/lib/module/utils/index.js.map +1 -0
  34. package/lib/module/utils/types.js +2 -0
  35. package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
  36. package/lib/tsconfig.tsbuildinfo +1 -0
  37. package/lib/typescript/index.d.ts +10 -141
  38. package/lib/typescript/index.d.ts.map +1 -1
  39. package/lib/typescript/random.d.ts +4 -5
  40. package/lib/typescript/random.d.ts.map +1 -1
  41. package/lib/typescript/specs/random.nitro.d.ts +9 -0
  42. package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
  43. package/lib/typescript/utils/conversion.d.ts +4 -0
  44. package/lib/typescript/utils/conversion.d.ts.map +1 -0
  45. package/lib/typescript/utils/index.d.ts +6 -0
  46. package/lib/typescript/utils/index.d.ts.map +1 -0
  47. package/lib/typescript/utils/types.d.ts +5 -0
  48. package/lib/typescript/utils/types.d.ts.map +1 -0
  49. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +45 -0
  50. package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +26 -0
  51. package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +57 -0
  52. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +9 -0
  53. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +25 -0
  54. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +29 -0
  55. package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +23 -0
  56. package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +62 -0
  57. package/package.json +50 -106
  58. package/src/index.ts +38 -48
  59. package/src/random.ts +51 -48
  60. package/src/specs/random.nitro.ts +14 -0
  61. package/src/utils/conversion.ts +16 -0
  62. package/src/utils/index.ts +6 -0
  63. package/src/utils/types.ts +15 -0
  64. package/LICENSE +0 -27
  65. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
  66. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
  67. package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
  68. package/cpp/Cipher/MGLCipherHostObject.h +0 -90
  69. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
  70. package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
  71. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
  72. package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
  73. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
  74. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
  75. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
  76. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
  77. package/cpp/Cipher/MGLPublicCipher.h +0 -124
  78. package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
  79. package/cpp/Cipher/MGLRsa.cpp +0 -573
  80. package/cpp/Cipher/MGLRsa.h +0 -109
  81. package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
  82. package/cpp/HMAC/MGLHmacHostObject.h +0 -39
  83. package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
  84. package/cpp/HMAC/MGLHmacInstaller.h +0 -20
  85. package/cpp/Hash/MGLHashHostObject.cpp +0 -147
  86. package/cpp/Hash/MGLHashHostObject.h +0 -53
  87. package/cpp/Hash/MGLHashInstaller.cpp +0 -44
  88. package/cpp/Hash/MGLHashInstaller.h +0 -20
  89. package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
  90. package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
  91. package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
  92. package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
  93. package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
  94. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
  95. package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
  96. package/cpp/JSIUtils/MGLTypedArray.h +0 -160
  97. package/cpp/MGLKeys.cpp +0 -1426
  98. package/cpp/MGLKeys.h +0 -203
  99. package/cpp/MGLQuickCryptoHostObject.cpp +0 -127
  100. package/cpp/MGLQuickCryptoHostObject.h +0 -30
  101. package/cpp/Random/MGLRandomHostObject.cpp +0 -96
  102. package/cpp/Random/MGLRandomHostObject.h +0 -27
  103. package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
  104. package/cpp/Sig/MGLSignHostObjects.h +0 -128
  105. package/cpp/Sig/MGLSignInstaller.cpp +0 -24
  106. package/cpp/Sig/MGLSignInstaller.h +0 -29
  107. package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
  108. package/cpp/Sig/MGLVerifyInstaller.h +0 -22
  109. package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
  110. package/cpp/Utils/MGLDispatchQueue.h +0 -55
  111. package/cpp/Utils/MGLUtils.cpp +0 -295
  112. package/cpp/Utils/MGLUtils.h +0 -358
  113. package/cpp/Utils/base64.h +0 -320
  114. package/cpp/Utils/logs.h +0 -38
  115. package/cpp/Utils/node.h +0 -13
  116. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
  117. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
  118. package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
  119. package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
  120. package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
  121. package/cpp/webcrypto/MGLWebCrypto.h +0 -32
  122. package/cpp/webcrypto/crypto_aes.cpp +0 -516
  123. package/cpp/webcrypto/crypto_aes.h +0 -79
  124. package/cpp/webcrypto/crypto_ec.cpp +0 -424
  125. package/cpp/webcrypto/crypto_ec.h +0 -78
  126. package/cpp/webcrypto/crypto_keygen.cpp +0 -86
  127. package/cpp/webcrypto/crypto_keygen.h +0 -38
  128. package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
  129. package/ios/QuickCryptoModule.h +0 -5
  130. package/ios/QuickCryptoModule.mm +0 -42
  131. package/lib/commonjs/Cipher.js +0 -502
  132. package/lib/commonjs/Cipher.js.map +0 -1
  133. package/lib/commonjs/Hash.js +0 -99
  134. package/lib/commonjs/Hash.js.map +0 -1
  135. package/lib/commonjs/Hashnames.js +0 -87
  136. package/lib/commonjs/Hashnames.js.map +0 -1
  137. package/lib/commonjs/Hmac.js +0 -79
  138. package/lib/commonjs/Hmac.js.map +0 -1
  139. package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
  140. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
  141. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
  142. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  143. package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
  144. package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
  145. package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
  146. package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
  147. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
  148. package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
  149. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
  150. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
  151. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
  152. package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
  153. package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
  154. package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
  155. package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
  156. package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
  157. package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
  158. package/lib/commonjs/Utils.js +0 -608
  159. package/lib/commonjs/Utils.js.map +0 -1
  160. package/lib/commonjs/aes.js +0 -281
  161. package/lib/commonjs/aes.js.map +0 -1
  162. package/lib/commonjs/constants.js +0 -85
  163. package/lib/commonjs/constants.js.map +0 -1
  164. package/lib/commonjs/ec.js +0 -276
  165. package/lib/commonjs/ec.js.map +0 -1
  166. package/lib/commonjs/keygen.js +0 -56
  167. package/lib/commonjs/keygen.js.map +0 -1
  168. package/lib/commonjs/keys.js +0 -492
  169. package/lib/commonjs/keys.js.map +0 -1
  170. package/lib/commonjs/pbkdf2.js +0 -90
  171. package/lib/commonjs/pbkdf2.js.map +0 -1
  172. package/lib/commonjs/rsa.js +0 -248
  173. package/lib/commonjs/rsa.js.map +0 -1
  174. package/lib/commonjs/sig.js +0 -129
  175. package/lib/commonjs/sig.js.map +0 -1
  176. package/lib/commonjs/subtle.js +0 -448
  177. package/lib/commonjs/subtle.js.map +0 -1
  178. package/lib/commonjs/webcrypto.js +0 -14
  179. package/lib/commonjs/webcrypto.js.map +0 -1
  180. package/lib/module/Cipher.js +0 -491
  181. package/lib/module/Cipher.js.map +0 -1
  182. package/lib/module/Hash.js +0 -93
  183. package/lib/module/Hash.js.map +0 -1
  184. package/lib/module/Hashnames.js +0 -85
  185. package/lib/module/Hashnames.js.map +0 -1
  186. package/lib/module/Hmac.js +0 -74
  187. package/lib/module/Hmac.js.map +0 -1
  188. package/lib/module/NativeQuickCrypto/Cipher.js +0 -26
  189. package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
  190. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -49
  191. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  192. package/lib/module/NativeQuickCrypto/aes.js +0 -4
  193. package/lib/module/NativeQuickCrypto/hash.js +0 -2
  194. package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
  195. package/lib/module/NativeQuickCrypto/hmac.js +0 -2
  196. package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
  197. package/lib/module/NativeQuickCrypto/keygen.js +0 -4
  198. package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
  199. package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
  200. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
  201. package/lib/module/NativeQuickCrypto/random.js +0 -2
  202. package/lib/module/NativeQuickCrypto/random.js.map +0 -1
  203. package/lib/module/NativeQuickCrypto/rsa.js +0 -4
  204. package/lib/module/NativeQuickCrypto/sig.js +0 -17
  205. package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
  206. package/lib/module/NativeQuickCrypto/webcrypto.js +0 -4
  207. package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
  208. package/lib/module/Utils.js +0 -539
  209. package/lib/module/Utils.js.map +0 -1
  210. package/lib/module/aes.js +0 -274
  211. package/lib/module/aes.js.map +0 -1
  212. package/lib/module/constants.js +0 -81
  213. package/lib/module/constants.js.map +0 -1
  214. package/lib/module/ec.js +0 -267
  215. package/lib/module/ec.js.map +0 -1
  216. package/lib/module/keygen.js +0 -49
  217. package/lib/module/keygen.js.map +0 -1
  218. package/lib/module/keys.js +0 -477
  219. package/lib/module/keys.js.map +0 -1
  220. package/lib/module/pbkdf2.js +0 -84
  221. package/lib/module/pbkdf2.js.map +0 -1
  222. package/lib/module/rsa.js +0 -242
  223. package/lib/module/rsa.js.map +0 -1
  224. package/lib/module/sig.js +0 -124
  225. package/lib/module/sig.js.map +0 -1
  226. package/lib/module/subtle.js +0 -443
  227. package/lib/module/subtle.js.map +0 -1
  228. package/lib/module/webcrypto.js +0 -10
  229. package/lib/module/webcrypto.js.map +0 -1
  230. package/lib/typescript/Cipher.d.ts +0 -81
  231. package/lib/typescript/Cipher.d.ts.map +0 -1
  232. package/lib/typescript/Hash.d.ts +0 -44
  233. package/lib/typescript/Hash.d.ts.map +0 -1
  234. package/lib/typescript/Hashnames.d.ts +0 -11
  235. package/lib/typescript/Hashnames.d.ts.map +0 -1
  236. package/lib/typescript/Hmac.d.ts +0 -37
  237. package/lib/typescript/Hmac.d.ts.map +0 -1
  238. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
  239. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
  240. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
  241. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
  242. package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
  243. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
  244. package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
  245. package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
  246. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
  247. package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
  248. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
  249. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
  250. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
  251. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
  252. package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
  253. package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
  254. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
  255. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
  256. package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
  257. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
  258. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
  259. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
  260. package/lib/typescript/Utils.d.ts +0 -48
  261. package/lib/typescript/Utils.d.ts.map +0 -1
  262. package/lib/typescript/aes.d.ts +0 -22
  263. package/lib/typescript/aes.d.ts.map +0 -1
  264. package/lib/typescript/constants.d.ts +0 -76
  265. package/lib/typescript/constants.d.ts.map +0 -1
  266. package/lib/typescript/ec.d.ts +0 -7
  267. package/lib/typescript/ec.d.ts.map +0 -1
  268. package/lib/typescript/keygen.d.ts +0 -6
  269. package/lib/typescript/keygen.d.ts.map +0 -1
  270. package/lib/typescript/keys.d.ts +0 -206
  271. package/lib/typescript/keys.d.ts.map +0 -1
  272. package/lib/typescript/pbkdf2.d.ts +0 -12
  273. package/lib/typescript/pbkdf2.d.ts.map +0 -1
  274. package/lib/typescript/rsa.d.ts +0 -12
  275. package/lib/typescript/rsa.d.ts.map +0 -1
  276. package/lib/typescript/sig.d.ts +0 -21
  277. package/lib/typescript/sig.d.ts.map +0 -1
  278. package/lib/typescript/subtle.d.ts +0 -15
  279. package/lib/typescript/subtle.d.ts.map +0 -1
  280. package/lib/typescript/webcrypto.d.ts +0 -9
  281. package/lib/typescript/webcrypto.d.ts.map +0 -1
  282. package/react-native-quick-crypto.podspec +0 -40
  283. package/src/Cipher.ts +0 -832
  284. package/src/Hash.ts +0 -132
  285. package/src/Hashnames.ts +0 -93
  286. package/src/Hmac.ts +0 -106
  287. package/src/NativeQuickCrypto/Cipher.ts +0 -102
  288. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
  289. package/src/NativeQuickCrypto/aes.ts +0 -14
  290. package/src/NativeQuickCrypto/hash.ts +0 -10
  291. package/src/NativeQuickCrypto/hmac.ts +0 -9
  292. package/src/NativeQuickCrypto/keygen.ts +0 -7
  293. package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
  294. package/src/NativeQuickCrypto/random.ts +0 -12
  295. package/src/NativeQuickCrypto/rsa.ts +0 -12
  296. package/src/NativeQuickCrypto/sig.ts +0 -44
  297. package/src/NativeQuickCrypto/webcrypto.ts +0 -72
  298. package/src/Utils.ts +0 -777
  299. package/src/aes.ts +0 -402
  300. package/src/constants.ts +0 -79
  301. package/src/ec.ts +0 -375
  302. package/src/keygen.ts +0 -80
  303. package/src/keys.ts +0 -787
  304. package/src/pbkdf2.ts +0 -169
  305. package/src/rsa.ts +0 -370
  306. package/src/sig.ts +0 -164
  307. package/src/subtle.ts +0 -639
  308. package/src/webcrypto.ts +0 -8
  309. /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
@@ -1,79 +0,0 @@
1
- #ifndef crypto_aes_h
2
- #define crypto_aes_h
3
-
4
- #include <jsi/jsi.h>
5
-
6
- #include "MGLKeys.h"
7
- #ifdef ANDROID
8
- #include "Utils/MGLUtils.h"
9
- #else
10
- #include "MGLUtils.h"
11
- #endif
12
-
13
- namespace margelo {
14
-
15
- namespace jsi = facebook::jsi;
16
-
17
- constexpr size_t kAesBlockSize = 16;
18
- constexpr unsigned kNoAuthTagLength = static_cast<unsigned>(-1);
19
- constexpr const char* kDefaultWrapIV = "\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6";
20
-
21
- #define VARIANTS(V) \
22
- V(CTR_128, AES_CTR_Cipher) \
23
- V(CTR_192, AES_CTR_Cipher) \
24
- V(CTR_256, AES_CTR_Cipher) \
25
- V(CBC_128, AES_Cipher) \
26
- V(CBC_192, AES_Cipher) \
27
- V(CBC_256, AES_Cipher) \
28
- V(GCM_128, AES_Cipher) \
29
- V(GCM_192, AES_Cipher) \
30
- V(GCM_256, AES_Cipher) \
31
- V(KW_128, AES_Cipher) \
32
- V(KW_192, AES_Cipher) \
33
- V(KW_256, AES_Cipher)
34
-
35
- enum AESKeyVariant {
36
- #define V(name, _) kKeyVariantAES_ ## name,
37
- VARIANTS(V)
38
- #undef V
39
- };
40
-
41
- struct AESCipherConfig final {
42
- enum Mode {
43
- kEncrypt,
44
- kDecrypt,
45
- // kWrapKey,
46
- // kUnwrapKey,
47
- };
48
-
49
- Mode mode;
50
- AESKeyVariant variant;
51
- std::shared_ptr<KeyObjectData> key;
52
- ByteSource data;
53
- const EVP_CIPHER* cipher;
54
- ByteSource iv; // Used for both iv or counter
55
- size_t length;
56
- ByteSource tag; // Used only for authenticated modes (GCM)
57
- ByteSource additional_data;
58
-
59
- AESCipherConfig() = default;
60
-
61
- // AESCipherConfig(AESCipherConfig&& other) noexcept;
62
-
63
- // AESCipherConfig& operator=(AESCipherConfig&& other) noexcept;
64
-
65
- // void MemoryInfo(MemoryTracker* tracker) const override;
66
- // SET_MEMORY_INFO_NAME(AESCipherConfig)
67
- // SET_SELF_SIZE(AESCipherConfig)
68
- };
69
-
70
- class AESCipher {
71
- public:
72
- AESCipher() {}
73
- AESCipherConfig GetParamsFromJS(jsi::Runtime &rt, const jsi::Value *args);
74
- WebCryptoCipherStatus DoCipher(const AESCipherConfig &params, ByteSource *out);
75
- };
76
-
77
- } // namespace margelo
78
-
79
- #endif // crypto_aes_h
@@ -1,424 +0,0 @@
1
- //
2
- // crypto_ec.cpp
3
- // BEMCheckBox
4
- //
5
- // Created by Oscar Franco on 30/11/23.
6
- //
7
-
8
- #include "crypto_ec.h"
9
- #include <iostream>
10
- #include <openssl/ec.h>
11
- #include <string>
12
- #include <utility>
13
-
14
- namespace margelo {
15
- namespace jsi = facebook::jsi;
16
-
17
- int GetCurveFromName(const char* name) {
18
- int nid = EC_curve_nist2nid(name);
19
- if (nid == NID_undef)
20
- nid = OBJ_sn2nid(name);
21
- return nid;
22
- }
23
-
24
- ECPointPointer ECDH::BufferToPoint(jsi::Runtime &rt,
25
- const EC_GROUP* group,
26
- jsi::ArrayBuffer &buf) {
27
- int r;
28
-
29
- ECPointPointer pub(EC_POINT_new(group));
30
- if (!pub) {
31
- throw std::runtime_error(
32
- "Failed to allocate EC_POINT for a public key");
33
- return pub;
34
- }
35
-
36
- // TODO(osp) re-insert this check
37
- // if (UNLIKELY(!input.CheckSizeInt32())) {
38
- // THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");
39
- // return ECPointPointer();
40
- // }
41
- r = EC_POINT_oct2point(
42
- group,
43
- pub.get(),
44
- buf.data(rt),
45
- buf.size(rt),
46
- nullptr);
47
-
48
- if (!r) {
49
- return ECPointPointer();
50
- }
51
- return pub;
52
- }
53
-
54
- WebCryptoKeyExportStatus ECDH::doExport(jsi::Runtime &rt,
55
- std::shared_ptr<KeyObjectData> key_data,
56
- WebCryptoKeyFormat format,
57
- const ECKeyExportConfig& params,
58
- ByteSource* out) {
59
- CHECK_NE(key_data->GetKeyType(), kKeyTypeSecret);
60
-
61
- switch (format) {
62
- case kWebCryptoKeyFormatRaw:
63
- return EC_Raw_Export(key_data.get(), params, out);
64
- case kWebCryptoKeyFormatPKCS8:
65
- if (key_data->GetKeyType() != kKeyTypePrivate)
66
- return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
67
- return PKEY_PKCS8_Export(key_data.get(), out);
68
- case kWebCryptoKeyFormatSPKI: {
69
- if (key_data->GetKeyType() != kKeyTypePublic)
70
- throw std::runtime_error("Invalid type public to be exported");
71
-
72
- ManagedEVPPKey m_pkey = key_data->GetAsymmetricKey();
73
- if (EVP_PKEY_id(m_pkey.get()) != EVP_PKEY_EC) {
74
- return PKEY_SPKI_Export(key_data.get(), out);
75
- } else {
76
- // Ensure exported key is in uncompressed point format.
77
- // The temporary EC key is so we can have i2d_PUBKEY_bio() write out
78
- // the header but it is a somewhat silly hoop to jump through because
79
- // the header is for all practical purposes a static 26 byte sequence
80
- // where only the second byte changes.
81
-
82
- const EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(m_pkey.get());
83
- const EC_GROUP* group = EC_KEY_get0_group(ec_key);
84
- const EC_POINT* point = EC_KEY_get0_public_key(ec_key);
85
- const point_conversion_form_t form =
86
- POINT_CONVERSION_UNCOMPRESSED;
87
- const size_t need =
88
- EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
89
- if (need == 0) {
90
- throw std::runtime_error("Failed to export EC key");
91
- }
92
- ByteSource::Builder data(need);
93
- const size_t have = EC_POINT_point2oct(group,
94
- point, form, data.data<unsigned char>(), need, nullptr);
95
- if (have == 0) {
96
- throw std::runtime_error("Failed to export EC key");
97
- }
98
- ECKeyPointer ec(EC_KEY_new());
99
- CHECK_EQ(1, EC_KEY_set_group(ec.get(), group));
100
- ECPointPointer uncompressed(EC_POINT_new(group));
101
- CHECK_EQ(1,
102
- EC_POINT_oct2point(group,
103
- uncompressed.get(),
104
- data.data<unsigned char>(),
105
- data.size(),
106
- nullptr));
107
- CHECK_EQ(1, EC_KEY_set_public_key(ec.get(),
108
- uncompressed.get()));
109
- EVPKeyPointer pkey(EVP_PKEY_new());
110
- CHECK_EQ(1, EVP_PKEY_set1_EC_KEY(pkey.get(), ec.get()));
111
- BIOPointer bio(BIO_new(BIO_s_mem()));
112
- CHECK(bio);
113
- if (!i2d_PUBKEY_bio(bio.get(), pkey.get())) {
114
- throw std::runtime_error("Failed to export EC key");
115
- }
116
- *out = ByteSource::FromBIO(bio);
117
- return WebCryptoKeyExportStatus::OK;
118
- }
119
- }
120
- default:
121
- throw std::runtime_error("Un-reachable export code");
122
- }
123
- }
124
-
125
- WebCryptoKeyExportStatus EC_Raw_Export(KeyObjectData* key_data,
126
- const ECKeyExportConfig& params,
127
- ByteSource* out) {
128
- ManagedEVPPKey m_pkey = key_data->GetAsymmetricKey();
129
- CHECK(m_pkey);
130
- // std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
131
-
132
- const EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(m_pkey.get());
133
-
134
- size_t len = 0;
135
-
136
- if (ec_key == nullptr) {
137
- typedef int (*export_fn)(const EVP_PKEY*, unsigned char*, size_t* len);
138
- export_fn fn = nullptr;
139
- switch (key_data->GetKeyType()) {
140
- case kKeyTypePrivate:
141
- fn = EVP_PKEY_get_raw_private_key;
142
- break;
143
- case kKeyTypePublic:
144
- fn = EVP_PKEY_get_raw_public_key;
145
- break;
146
- case kKeyTypeSecret:
147
- throw std::runtime_error("unreachable code in EC_Raw_Export");
148
- }
149
- CHECK_NOT_NULL(fn);
150
- // Get the size of the raw key data
151
- if (fn(m_pkey.get(), nullptr, &len) == 0)
152
- return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
153
- ByteSource::Builder data(len);
154
- if (fn(m_pkey.get(), data.data<unsigned char>(), &len) == 0)
155
- return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
156
- *out = std::move(data).release(len);
157
- } else {
158
- if (key_data->GetKeyType() != kKeyTypePublic)
159
- return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
160
- const EC_GROUP* group = EC_KEY_get0_group(ec_key);
161
- const EC_POINT* point = EC_KEY_get0_public_key(ec_key);
162
- point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
163
-
164
- // Get the allocated data size...
165
- len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr);
166
- if (len == 0)
167
- return WebCryptoKeyExportStatus::FAILED;
168
- ByteSource::Builder data(len);
169
- size_t check_len = EC_POINT_point2oct(
170
- group, point, form, data.data<unsigned char>(), len, nullptr);
171
- if (check_len == 0)
172
- return WebCryptoKeyExportStatus::FAILED;
173
-
174
- CHECK_EQ(len, check_len);
175
- *out = std::move(data).release();
176
- }
177
-
178
- return WebCryptoKeyExportStatus::OK;
179
- }
180
-
181
- jsi::Value ExportJWKEcKey(jsi::Runtime &rt,
182
- std::shared_ptr<KeyObjectData> key,
183
- jsi::Object &target) {
184
- ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
185
- // std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
186
- CHECK_EQ(EVP_PKEY_id(m_pkey.get()), EVP_PKEY_EC);
187
-
188
- const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(m_pkey.get());
189
- CHECK_NOT_NULL(ec);
190
-
191
- const EC_POINT* pub = EC_KEY_get0_public_key(ec);
192
- const EC_GROUP* group = EC_KEY_get0_group(ec);
193
-
194
- int degree_bits = EC_GROUP_get_degree(group);
195
- int degree_bytes =
196
- (degree_bits / CHAR_BIT) + (7 + (degree_bits % CHAR_BIT)) / 8;
197
-
198
- BignumPointer x(BN_new());
199
- BignumPointer y(BN_new());
200
-
201
- if (!EC_POINT_get_affine_coordinates(group, pub, x.get(), y.get(), nullptr)) {
202
- throw jsi::JSError(rt, "Failed to get elliptic-curve point coordinates");
203
- }
204
-
205
- target.setProperty(rt, "kty", "EC");
206
- target.setProperty(rt, "x", EncodeBignum(x.get(), degree_bytes, true));
207
- target.setProperty(rt, "y", EncodeBignum(y.get(), degree_bytes, true));
208
-
209
- std::string crv_name;
210
- const int nid = EC_GROUP_get_curve_name(group);
211
- switch (nid) {
212
- case NID_X9_62_prime256v1:
213
- crv_name = "P-256";
214
- break;
215
- case NID_secp256k1:
216
- crv_name = "secp256k1";
217
- break;
218
- case NID_secp384r1:
219
- crv_name = "P-384";
220
- break;
221
- case NID_secp521r1:
222
- crv_name = "P-521";
223
- break;
224
- default: {
225
- throw jsi::JSError(rt, "Unsupported JWK EC curve: %s.", OBJ_nid2sn(nid));
226
- return jsi::Value::undefined();
227
- }
228
- }
229
- target.setProperty(rt, "crv", crv_name);
230
-
231
- if (key->GetKeyType() == kKeyTypePrivate) {
232
- const BIGNUM* pvt = EC_KEY_get0_private_key(ec);
233
- target.setProperty(rt, "d", EncodeBignum(pvt, degree_bytes, true));
234
- }
235
-
236
- return std::move(target);
237
- }
238
-
239
- std::shared_ptr<KeyObjectData> ImportJWKEcKey(jsi::Runtime &rt,
240
- jsi::Object &jwk,
241
- jsi::Value &namedCurve) {
242
- // curve name
243
- if (namedCurve.isUndefined()) {
244
- throw jsi::JSError(rt, "Invalid Named Curve");
245
- return std::shared_ptr<KeyObjectData>();
246
- }
247
- std::string curve = namedCurve.asString(rt).utf8(rt);
248
-
249
- int nid = GetCurveFromName(curve.c_str());
250
- if (nid == NID_undef) { // Unknown curve
251
- throw jsi::JSError(rt, "Invalid Named Curve: " + curve);
252
- return std::shared_ptr<KeyObjectData>();
253
- }
254
-
255
- jsi::Value x_value = jwk.getProperty(rt, "x");
256
- jsi::Value y_value = jwk.getProperty(rt, "y");
257
- jsi::Value d_value = jwk.getProperty(rt, "d");
258
-
259
- if (!x_value.isString() ||
260
- !y_value.isString() ||
261
- (!d_value.isUndefined() && !d_value.isString())) {
262
- throw jsi::JSError(rt, "Invalid JWK EC key 0");
263
- }
264
-
265
- KeyType type = d_value.isString() ? kKeyTypePrivate : kKeyTypePublic;
266
-
267
- ECKeyPointer ec(EC_KEY_new_by_curve_name(nid));
268
- if (!ec) {
269
- throw jsi::JSError(rt, "Invalid JWK EC key 1");
270
- }
271
-
272
- ByteSource x = ByteSource::FromEncodedString(rt,
273
- x_value.asString(rt).utf8(rt),
274
- encoding::BASE64URL);
275
- ByteSource y = ByteSource::FromEncodedString(rt,
276
- y_value.asString(rt).utf8(rt),
277
- encoding::BASE64URL);
278
-
279
- int r = EC_KEY_set_public_key_affine_coordinates(ec.get(),
280
- x.ToBN().get(),
281
- y.ToBN().get());
282
- if (!r) {
283
- throw jsi::JSError(rt, "Invalid JWK EC key 2");
284
- }
285
-
286
- if (type == kKeyTypePrivate) {
287
- ByteSource d = ByteSource::FromEncodedString(rt, d_value.asString(rt).utf8(rt));
288
- if (!EC_KEY_set_private_key(ec.get(), d.ToBN().get())) {
289
- throw jsi::JSError(rt, "Invalid JWK EC key 3");
290
- return std::shared_ptr<KeyObjectData>();
291
- }
292
- }
293
-
294
- EVPKeyPointer pkey(EVP_PKEY_new());
295
- CHECK_EQ(EVP_PKEY_set1_EC_KEY(pkey.get(), ec.get()), 1);
296
-
297
- return KeyObjectData::CreateAsymmetric(type, ManagedEVPPKey(std::move(pkey)));
298
- }
299
-
300
- jsi::Value GetEcKeyDetail(jsi::Runtime &rt,
301
- std::shared_ptr<KeyObjectData> key) {
302
- jsi::Object target = jsi::Object(rt);
303
- ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
304
- // std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
305
- CHECK_EQ(EVP_PKEY_id(m_pkey.get()), EVP_PKEY_EC);
306
-
307
- const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(m_pkey.get());
308
- CHECK_NOT_NULL(ec);
309
-
310
- const EC_GROUP* group = EC_KEY_get0_group(ec);
311
- int nid = EC_GROUP_get_curve_name(group);
312
-
313
- jsi::String value = jsi::String::createFromUtf8(rt, OBJ_nid2sn(nid));
314
- target.setProperty(rt, "namedCurve", value);
315
- return target;
316
- }
317
-
318
- EcKeyPairGenConfig prepareEcKeyGenConfig(jsi::Runtime &rt,
319
- const jsi::Value *args)
320
- {
321
- EcKeyPairGenConfig config = EcKeyPairGenConfig();
322
-
323
- // curve name
324
- std::string curveName = args[1].asString(rt).utf8(rt);
325
- config.curve_nid = GetCurveFromName(curveName.c_str());
326
-
327
- // encoding
328
- if (CheckIsInt32(args[2].asNumber())) {
329
- int encoding = static_cast<int>(args[2].asNumber());
330
- if (encoding != OPENSSL_EC_NAMED_CURVE &&
331
- encoding != OPENSSL_EC_EXPLICIT_CURVE) {
332
- throw jsi::JSError(rt, "Invalid param_encoding specified");
333
- } else {
334
- config.param_encoding = encoding;
335
- }
336
- } else {
337
- throw jsi::JSError(rt, "Invalid param_encoding specified (not int)");
338
- }
339
-
340
- // rest of args for encoding
341
- unsigned int offset = 3;
342
-
343
- config.public_key_encoding = ManagedEVPPKey::GetPublicKeyEncodingFromJs(
344
- rt, args, &offset, kKeyContextGenerate);
345
-
346
- auto private_key_encoding = ManagedEVPPKey::GetPrivateKeyEncodingFromJs(
347
- rt, args, &offset, kKeyContextGenerate);
348
-
349
- if (!private_key_encoding.IsEmpty()) {
350
- config.private_key_encoding = private_key_encoding.Release();
351
- }
352
-
353
- return config;
354
- }
355
-
356
- EVPKeyCtxPointer setup(std::shared_ptr<EcKeyPairGenConfig> config) {
357
- EVPKeyCtxPointer key_ctx;
358
- switch (config->curve_nid) {
359
- case EVP_PKEY_ED25519:
360
- // Fall through
361
- case EVP_PKEY_ED448:
362
- // Fall through
363
- case EVP_PKEY_X25519:
364
- // Fall through
365
- case EVP_PKEY_X448:
366
- key_ctx.reset(EVP_PKEY_CTX_new_id(config->curve_nid, nullptr));
367
- break;
368
- default: {
369
- EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr));
370
- EVP_PKEY* raw_params = nullptr;
371
- if (!param_ctx ||
372
- EVP_PKEY_paramgen_init(param_ctx.get()) <= 0 ||
373
- EVP_PKEY_CTX_set_ec_paramgen_curve_nid(
374
- param_ctx.get(), config->curve_nid) <= 0 ||
375
- EVP_PKEY_CTX_set_ec_param_enc(
376
- param_ctx.get(), config->param_encoding) <= 0 ||
377
- EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0) {
378
- return EVPKeyCtxPointer();
379
- }
380
- EVPKeyPointer key_params(raw_params);
381
- key_ctx.reset(EVP_PKEY_CTX_new(key_params.get(), nullptr));
382
- }
383
- }
384
-
385
- if (key_ctx && EVP_PKEY_keygen_init(key_ctx.get()) <= 0)
386
- key_ctx.reset();
387
-
388
- return key_ctx;
389
- }
390
-
391
- std::pair<jsi::Value, jsi::Value> generateEcKeyPair(jsi::Runtime& runtime,
392
- std::shared_ptr<EcKeyPairGenConfig> config)
393
- {
394
- // TODO: this is all copied from MGLRsa.cpp - template it up like Node?
395
-
396
- EVPKeyCtxPointer ctx = setup(config);
397
-
398
- if (!ctx) {
399
- throw jsi::JSError(runtime, "Error on key generation job");
400
- }
401
-
402
- // Generate the key
403
- EVP_PKEY* pkey = nullptr;
404
- if (!EVP_PKEY_keygen(ctx.get(), &pkey)) {
405
- throw jsi::JSError(runtime, "Error generating key");
406
- }
407
-
408
- config->key = ManagedEVPPKey(EVPKeyPointer(pkey));
409
-
410
- jsi::Value publicBuffer =
411
- ManagedEVPPKey::ToEncodedPublicKey(runtime, std::move(config->key),
412
- config->public_key_encoding);
413
- jsi::Value privateBuffer =
414
- ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
415
- config->private_key_encoding);
416
-
417
- if (publicBuffer.isUndefined() || privateBuffer.isUndefined()) {
418
- throw jsi::JSError(runtime, "Failed to encode public and/or private key (EC)");
419
- }
420
-
421
- return {std::move(publicBuffer), std::move(privateBuffer)};
422
- }
423
-
424
- } // namespace margelo
@@ -1,78 +0,0 @@
1
- //
2
- // crypto_ec.hpp
3
- // BEMCheckBox
4
- //
5
- // Created by Oscar Franco on 30/11/23.
6
- //
7
-
8
- #ifndef crypto_ec_h
9
- #define crypto_ec_h
10
-
11
- #include <jsi/jsi.h>
12
- #include <openssl/ec.h>
13
- #include <memory>
14
- #include "MGLKeys.h"
15
- #ifdef ANDROID
16
- #include "Utils/MGLUtils.h"
17
- #include "JSIUtils/MGLJSIUtils.h"
18
- #else
19
- #include "MGLUtils.h"
20
- #include "MGLJSIUtils.h"
21
- #endif
22
-
23
-
24
- namespace margelo {
25
- namespace jsi = facebook::jsi;
26
-
27
- // There is currently no additional information that the
28
- // ECKeyExport needs to collect, but we need to provide
29
- // the base struct anyway.
30
- struct ECKeyExportConfig final {};
31
-
32
- class ECDH final {
33
- public:
34
- static ECPointPointer BufferToPoint(jsi::Runtime &rt,
35
- const EC_GROUP* group,
36
- jsi::ArrayBuffer &buf);
37
-
38
- static WebCryptoKeyExportStatus doExport(jsi::Runtime &rt,
39
- std::shared_ptr<KeyObjectData> key_data,
40
- WebCryptoKeyFormat format,
41
- const ECKeyExportConfig &params,
42
- ByteSource* out);
43
- };
44
-
45
- WebCryptoKeyExportStatus EC_Raw_Export(KeyObjectData* key_data,
46
- const ECKeyExportConfig &params,
47
- ByteSource* out);
48
-
49
- jsi::Value ExportJWKEcKey(jsi::Runtime &rt,
50
- std::shared_ptr<KeyObjectData> key,
51
- jsi::Object &target);
52
-
53
- std::shared_ptr<KeyObjectData> ImportJWKEcKey(jsi::Runtime &rt,
54
- jsi::Object &jwk,
55
- jsi::Value &namedCurve);
56
-
57
- jsi::Value GetEcKeyDetail(jsi::Runtime &rt,
58
- std::shared_ptr<KeyObjectData> key);
59
-
60
- struct EcKeyPairGenConfig {
61
- PublicKeyEncodingConfig public_key_encoding;
62
- PrivateKeyEncodingConfig private_key_encoding;
63
- ManagedEVPPKey key;
64
-
65
- int curve_nid;
66
- int param_encoding;
67
- };
68
-
69
- EcKeyPairGenConfig prepareEcKeyGenConfig(jsi::Runtime& runtime,
70
- const jsi::Value* arguments);
71
-
72
- std::pair<jsi::Value, jsi::Value> generateEcKeyPair(jsi::Runtime& runtime,
73
- std::shared_ptr<EcKeyPairGenConfig> config);
74
-
75
-
76
- } // namespace margelo
77
-
78
- #endif /* crypto_ec_hpp */
@@ -1,86 +0,0 @@
1
- #include "crypto_keygen.h"
2
-
3
- #ifdef ANDROID
4
- #include "JSIUtils/MGLJSIMacros.h"
5
- #include "JSIUtils/MGLJSIUtils.h"
6
- #include "Utils/MGLUtils.h"
7
- #else
8
- #include "MGLJSIMacros.h"
9
- #include "MGLJSIUtils.h"
10
- #include "MGLUtils.h"
11
- #endif
12
-
13
- namespace margelo {
14
-
15
- FieldDefinition GenerateSecretKeyFieldDefinition(
16
- std::shared_ptr<react::CallInvoker> jsCallInvoker,
17
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
18
- return buildPair(
19
- "generateSecretKey", JSIF([=]) {
20
- auto skg = new SecretKeyGen();
21
- CHECK(skg->GetParamsFromJS(runtime, arguments));
22
- // make and return a promise
23
- auto promiseConstructor = runtime.global().getPropertyAsFunction(runtime, "Promise");
24
- auto promise = promiseConstructor.callAsConstructor(
25
- runtime,
26
- jsi::Function::createFromHostFunction(
27
- runtime,
28
- jsi::PropNameID::forAscii(runtime, "executor"),
29
- 2,
30
- [&jsCallInvoker, skg](
31
- jsi::Runtime &runtime, const jsi::Value &,
32
- const jsi::Value *promiseArgs, size_t) -> jsi::Value {
33
- auto resolve = std::make_shared<jsi::Value>(runtime, promiseArgs[0]);
34
- auto reject = std::make_shared<jsi::Value>(runtime, promiseArgs[1]);
35
- try {
36
- jsCallInvoker->invokeAsync([&runtime, resolve, skg]() {
37
- if (skg->DoKeyGen()) {
38
- auto res = jsi::Object::createFromHostObject(runtime, skg->GetHandle());
39
- resolve->asObject(runtime).asFunction(runtime).call(runtime, std::move(res));
40
- } else {
41
- throw std::runtime_error("Error generating key");
42
- }
43
- });
44
- } catch (std::exception e) {
45
- jsCallInvoker->invokeAsync([&runtime, reject, e]() {
46
- auto res = jsi::String::createFromUtf8(runtime, e.what());
47
- reject->asObject(runtime).asFunction(runtime).call(runtime, std::move(res));
48
- });
49
- }
50
- return {};
51
- }
52
- )
53
- );
54
- return promise;
55
- });
56
- };
57
-
58
- bool SecretKeyGen::GetParamsFromJS(jsi::Runtime &rt, const jsi::Value *args) {
59
- SecretKeyGenConfig params;
60
- unsigned int offset = 0;
61
-
62
- // length
63
- CHECK(CheckIsUint32(args[offset]));
64
- uint32_t bits = (uint32_t)args[offset].asNumber();
65
- params.length = bits / CHAR_BIT;
66
-
67
- this->params_ = std::move(params);
68
- return true;
69
- }
70
-
71
- bool SecretKeyGen::DoKeyGen() {
72
- // TODO: threading / async here, as we don't have jsi::Runtime
73
- ByteSource::Builder bytes(this->params_.length);
74
- if (CSPRNG(bytes.data<unsigned char>(), this->params_.length).is_err())
75
- return false;
76
- auto key_data = std::move(bytes).release();
77
- this->key_ = KeyObjectData::CreateSecret(std::move(key_data));
78
- return true;
79
- }
80
-
81
- std::shared_ptr<KeyObjectHandle> SecretKeyGen::GetHandle() {
82
- auto handle = KeyObjectHandle::Create(this->key_);
83
- return handle;
84
- }
85
-
86
- } // namespace margelo
@@ -1,38 +0,0 @@
1
- #ifndef crypto_keygen_h
2
- #define crypto_keygen_h
3
-
4
- #include <jsi/jsi.h>
5
-
6
- #include "MGLKeys.h"
7
- #ifdef ANDROID
8
- #include "Utils/MGLUtils.h"
9
- #else
10
- #include "MGLUtils.h"
11
- #endif
12
-
13
- namespace margelo {
14
-
15
- namespace jsi = facebook::jsi;
16
-
17
- FieldDefinition GenerateSecretKeyFieldDefinition(
18
- std::shared_ptr<react::CallInvoker> jsCallInvoker,
19
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
20
-
21
- struct SecretKeyGenConfig {
22
- size_t length; // in bytes
23
- SecretKeyGenConfig() = default;
24
- };
25
-
26
- class SecretKeyGen {
27
- public:
28
- bool GetParamsFromJS(jsi::Runtime &rt, const jsi::Value *args);
29
- bool DoKeyGen();
30
- std::shared_ptr<KeyObjectHandle> GetHandle();
31
- private:
32
- SecretKeyGenConfig params_;
33
- std::shared_ptr<KeyObjectData> key_;
34
- };
35
-
36
- } // namespace margelo
37
-
38
- #endif // crypto_keygen_h