react-native-quick-crypto 0.7.0 → 0.7.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 (133) hide show
  1. package/README.md +11 -63
  2. package/android/CMakeLists.txt +2 -0
  3. package/cpp/Cipher/MGLRsa.cpp +179 -3
  4. package/cpp/Cipher/MGLRsa.h +40 -0
  5. package/cpp/JSIUtils/MGLJSIUtils.h +8 -0
  6. package/cpp/MGLKeys.cpp +41 -43
  7. package/cpp/MGLKeys.h +9 -2
  8. package/cpp/MGLQuickCryptoHostObject.cpp +6 -6
  9. package/cpp/Sig/MGLSignHostObjects.cpp +22 -15
  10. package/cpp/Utils/MGLUtils.cpp +71 -1
  11. package/cpp/Utils/MGLUtils.h +55 -1
  12. package/cpp/webcrypto/MGLWebCrypto.cpp +89 -37
  13. package/cpp/webcrypto/MGLWebCrypto.h +5 -7
  14. package/cpp/webcrypto/crypto_aes.cpp +516 -0
  15. package/cpp/webcrypto/crypto_aes.h +79 -0
  16. package/cpp/webcrypto/crypto_ec.cpp +4 -20
  17. package/cpp/webcrypto/crypto_ec.h +0 -5
  18. package/cpp/webcrypto/crypto_keygen.cpp +86 -0
  19. package/cpp/webcrypto/crypto_keygen.h +38 -0
  20. package/lib/commonjs/Cipher.js +3 -1
  21. package/lib/commonjs/Cipher.js.map +1 -1
  22. package/lib/commonjs/Hashnames.js +20 -8
  23. package/lib/commonjs/Hashnames.js.map +1 -1
  24. package/lib/commonjs/NativeQuickCrypto/Cipher.js +13 -1
  25. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  26. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  27. package/lib/commonjs/NativeQuickCrypto/aes.js +6 -0
  28. package/lib/commonjs/NativeQuickCrypto/aes.js.map +1 -0
  29. package/lib/commonjs/NativeQuickCrypto/keygen.js +6 -0
  30. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +1 -0
  31. package/lib/commonjs/NativeQuickCrypto/rsa.js +6 -0
  32. package/lib/commonjs/NativeQuickCrypto/rsa.js.map +1 -0
  33. package/lib/commonjs/Utils.js +30 -6
  34. package/lib/commonjs/Utils.js.map +1 -1
  35. package/lib/commonjs/aes.js +184 -227
  36. package/lib/commonjs/aes.js.map +1 -1
  37. package/lib/commonjs/index.js +12 -2
  38. package/lib/commonjs/index.js.map +1 -1
  39. package/lib/commonjs/keygen.js +56 -0
  40. package/lib/commonjs/keygen.js.map +1 -0
  41. package/lib/commonjs/keys.js +74 -5
  42. package/lib/commonjs/keys.js.map +1 -1
  43. package/lib/commonjs/rsa.js +115 -196
  44. package/lib/commonjs/rsa.js.map +1 -1
  45. package/lib/commonjs/sig.js.map +1 -1
  46. package/lib/commonjs/subtle.js +140 -78
  47. package/lib/commonjs/subtle.js.map +1 -1
  48. package/lib/commonjs/webcrypto.js +14 -0
  49. package/lib/commonjs/webcrypto.js.map +1 -0
  50. package/lib/module/Cipher.js +3 -1
  51. package/lib/module/Cipher.js.map +1 -1
  52. package/lib/module/Hashnames.js +20 -8
  53. package/lib/module/Hashnames.js.map +1 -1
  54. package/lib/module/NativeQuickCrypto/Cipher.js +12 -0
  55. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  56. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  57. package/lib/module/NativeQuickCrypto/aes.js +2 -0
  58. package/lib/module/NativeQuickCrypto/aes.js.map +1 -0
  59. package/lib/module/NativeQuickCrypto/keygen.js +2 -0
  60. package/lib/module/NativeQuickCrypto/keygen.js.map +1 -0
  61. package/lib/module/NativeQuickCrypto/rsa.js +2 -0
  62. package/lib/module/NativeQuickCrypto/rsa.js.map +1 -0
  63. package/lib/module/Utils.js +26 -5
  64. package/lib/module/Utils.js.map +1 -1
  65. package/lib/module/aes.js +183 -228
  66. package/lib/module/aes.js.map +1 -1
  67. package/lib/module/index.js +11 -2
  68. package/lib/module/index.js.map +1 -1
  69. package/lib/module/keygen.js +47 -0
  70. package/lib/module/keygen.js.map +1 -0
  71. package/lib/module/keys.js +68 -4
  72. package/lib/module/keys.js.map +1 -1
  73. package/lib/module/rsa.js +115 -198
  74. package/lib/module/rsa.js.map +1 -1
  75. package/lib/module/sig.js.map +1 -1
  76. package/lib/module/subtle.js +143 -82
  77. package/lib/module/subtle.js.map +1 -1
  78. package/lib/module/webcrypto.js +8 -0
  79. package/lib/module/webcrypto.js.map +1 -0
  80. package/lib/typescript/Cipher.d.ts +0 -1
  81. package/lib/typescript/Cipher.d.ts.map +1 -1
  82. package/lib/typescript/Hash.d.ts.map +1 -1
  83. package/lib/typescript/Hashnames.d.ts +2 -2
  84. package/lib/typescript/Hashnames.d.ts.map +1 -1
  85. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +5 -0
  86. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
  87. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +4 -1
  88. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +1 -1
  89. package/lib/typescript/NativeQuickCrypto/aes.d.ts +5 -0
  90. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +1 -0
  91. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +4 -0
  92. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +1 -0
  93. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +5 -0
  94. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +1 -0
  95. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +12 -2
  96. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
  97. package/lib/typescript/Utils.d.ts +4 -4
  98. package/lib/typescript/Utils.d.ts.map +1 -1
  99. package/lib/typescript/aes.d.ts +18 -1
  100. package/lib/typescript/aes.d.ts.map +1 -1
  101. package/lib/typescript/ec.d.ts.map +1 -1
  102. package/lib/typescript/index.d.ts +27 -24
  103. package/lib/typescript/index.d.ts.map +1 -1
  104. package/lib/typescript/keygen.d.ts +6 -0
  105. package/lib/typescript/keygen.d.ts.map +1 -0
  106. package/lib/typescript/keys.d.ts +58 -17
  107. package/lib/typescript/keys.d.ts.map +1 -1
  108. package/lib/typescript/rsa.d.ts +9 -1
  109. package/lib/typescript/rsa.d.ts.map +1 -1
  110. package/lib/typescript/sig.d.ts +3 -17
  111. package/lib/typescript/sig.d.ts.map +1 -1
  112. package/lib/typescript/subtle.d.ts +6 -5
  113. package/lib/typescript/subtle.d.ts.map +1 -1
  114. package/lib/typescript/webcrypto.d.ts +9 -0
  115. package/lib/typescript/webcrypto.d.ts.map +1 -0
  116. package/package.json +12 -12
  117. package/src/Cipher.ts +1 -1
  118. package/src/Hashnames.ts +23 -21
  119. package/src/NativeQuickCrypto/Cipher.ts +32 -0
  120. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +6 -0
  121. package/src/NativeQuickCrypto/aes.ts +14 -0
  122. package/src/NativeQuickCrypto/keygen.ts +7 -0
  123. package/src/NativeQuickCrypto/rsa.ts +12 -0
  124. package/src/NativeQuickCrypto/webcrypto.ts +26 -2
  125. package/src/Utils.ts +37 -8
  126. package/src/aes.ts +259 -222
  127. package/src/index.ts +10 -1
  128. package/src/keygen.ts +80 -0
  129. package/src/keys.ts +143 -30
  130. package/src/rsa.ts +161 -187
  131. package/src/sig.ts +7 -23
  132. package/src/subtle.ts +211 -93
  133. package/src/webcrypto.ts +8 -0
@@ -55,11 +55,15 @@ bool ApplyRSAOptions(const ManagedEVPPKey& pkey, EVP_PKEY_CTX* pkctx,
55
55
  return true;
56
56
  }
57
57
 
58
- std::optional<jsi::Value> Node_SignFinal(jsi::Runtime& runtime,
59
- EVPMDPointer&& mdctx,
60
- const ManagedEVPPKey& pkey,
61
- int padding,
62
- std::optional<int> pss_salt_len) {
58
+ std::optional<MGLTypedArray<MGLTypedArrayKind::Uint8Array>> Node_SignFinal(
59
+ jsi::Runtime& runtime,
60
+ EVPMDPointer&& mdctx,
61
+ const ManagedEVPPKey& pkey,
62
+ int padding,
63
+ std::optional<int> pss_salt_len
64
+ ) {
65
+ std::optional<MGLTypedArray<MGLTypedArrayKind::Uint8Array>> ret;
66
+
63
67
  unsigned char m[EVP_MAX_MD_SIZE];
64
68
  unsigned int m_len;
65
69
 
@@ -81,13 +85,14 @@ std::optional<jsi::Value> Node_SignFinal(jsi::Runtime& runtime,
81
85
  &sig_len, m, m_len)) {
82
86
  CHECK_LE(sig_len, sig.size(runtime));
83
87
 
84
- // do this bits need to be trimmed? I think so
88
+ // (osp) do these bits need to be trimmed? I think so
85
89
  // if (sig_len == 0)
86
90
  // sig = ArrayBuffer::NewBackingStore(env->isolate(), 0);
87
91
  // else
88
92
  // sig = BackingStore::Reallocate(env->isolate(), std::move(sig),
89
93
  // sig_len);
90
- return sig;
94
+ ret.emplace(std::move(sig));
95
+ return ret;
91
96
  }
92
97
 
93
98
  return {};
@@ -281,16 +286,18 @@ SignBase::SignResult SignBase::SignFinal(jsi::Runtime& runtime,
281
286
  EVPMDPointer mdctx = std::move(mdctx_);
282
287
 
283
288
  if (!ValidateDSAParameters(pkey.get())) return SignResult(kSignPrivateKey);
284
-
285
- std::optional<jsi::Value> buffer =
289
+ ByteSource bs;
290
+ std::optional<MGLTypedArray<MGLTypedArrayKind::Uint8Array>> buffer =
286
291
  Node_SignFinal(runtime, std::move(mdctx), pkey, padding, salt_len);
287
292
  Error error = buffer.has_value() ? kSignOk : kSignPrivateKey;
288
- // TODO(osp) enable this
289
- // if (error == kSignOk && dsa_sig_enc == kSigEncP1363) {
290
- // buffer = ConvertSignatureToP1363(env(), pkey, std::move(buffer));
291
- // CHECK_NOT_NULL(buffer->Data());
292
- // }
293
- return SignResult(error, std::move(buffer.value()));
293
+ if (error == kSignOk) {
294
+ bs = ByteSource::FromBuffer(runtime, buffer.value().getBuffer(runtime));
295
+ if (dsa_sig_enc == kSigEncP1363) {
296
+ bs = ConvertSignatureToP1363(pkey, std::move(bs));
297
+ }
298
+ CHECK_NOT_NULL(bs.data<unsigned char>());
299
+ }
300
+ return SignResult(error, toJSI(runtime, std::move(bs)));
294
301
  }
295
302
 
296
303
  SignBase::Error SignBase::VerifyFinal(const ManagedEVPPKey& pkey,
@@ -184,8 +184,29 @@ ByteSource ByteSource::Foreign(const void* data, size_t size) {
184
184
  return ByteSource(data, nullptr, size);
185
185
  }
186
186
 
187
+ ByteSource ByteSource::FromBN(const BIGNUM* bn, size_t size) {
188
+ std::vector<uint8_t> buf(size);
189
+ CHECK_EQ(BN_bn2binpad(bn, buf.data(), size), size);
190
+ ByteSource::Builder out(size);
191
+ memcpy(out.data<void>(), buf.data(), size);
192
+ return std::move(out).release();
193
+ }
194
+
195
+ ByteSource GetByteSourceFromJS(jsi::Runtime &rt,
196
+ const jsi::Value &value,
197
+ std::string name) {
198
+ if (!value.isObject() || !value.asObject(rt).isArrayBuffer(rt)) {
199
+ throw jsi::JSError(rt, "arg is not an array buffer: " + name);
200
+ }
201
+ ByteSource data = ByteSource::FromStringOrBuffer(rt, value);
202
+ if (data.size() > INT_MAX) {
203
+ throw jsi::JSError(rt, "arg is too big (> int32): " + name);
204
+ }
205
+ return data;
206
+ }
207
+
187
208
  std::string EncodeBignum(const BIGNUM* bn,
188
- int size,
209
+ size_t size,
189
210
  bool url) {
190
211
  if (size == 0)
191
212
  size = BN_num_bytes(bn);
@@ -222,4 +243,53 @@ std::string DecodeBase64(const std::string &in, bool remove_linebreaks) {
222
243
  return base64_decode(in, remove_linebreaks);
223
244
  }
224
245
 
246
+ MUST_USE_RESULT CSPRNGResult CSPRNG(void* buffer, size_t length) {
247
+ unsigned char* buf = static_cast<unsigned char*>(buffer);
248
+ do {
249
+ if (1 == RAND_status()) {
250
+ #if OPENSSL_VERSION_MAJOR >= 3
251
+ if (1 == RAND_bytes_ex(nullptr, buf, length, 0)) return {true};
252
+ #else
253
+ while (length > INT_MAX && 1 == RAND_bytes(buf, INT_MAX)) {
254
+ buf += INT_MAX;
255
+ length -= INT_MAX;
256
+ }
257
+ if (length <= INT_MAX && 1 == RAND_bytes(buf, static_cast<int>(length)))
258
+ return {true};
259
+ #endif
260
+ }
261
+ #if OPENSSL_VERSION_MAJOR >= 3
262
+ const auto code = ERR_peek_last_error();
263
+ // A misconfigured OpenSSL 3 installation may report 1 from RAND_poll()
264
+ // and RAND_status() but fail in RAND_bytes() if it cannot look up
265
+ // a matching algorithm for the CSPRNG.
266
+ if (ERR_GET_LIB(code) == ERR_LIB_RAND) {
267
+ const auto reason = ERR_GET_REASON(code);
268
+ if (reason == RAND_R_ERROR_INSTANTIATING_DRBG ||
269
+ reason == RAND_R_UNABLE_TO_FETCH_DRBG ||
270
+ reason == RAND_R_UNABLE_TO_CREATE_DRBG) {
271
+ return {false};
272
+ }
273
+ }
274
+ #endif
275
+ } while (1 == RAND_poll());
276
+
277
+ return {false};
278
+ }
279
+
280
+ bool SetRsaOaepLabel(const EVPKeyCtxPointer& ctx, const ByteSource& label) {
281
+ if (label.size() != 0) {
282
+ // OpenSSL takes ownership of the label, so we need to create a copy.
283
+ void* label_copy = OPENSSL_memdup(label.data(), label.size());
284
+ CHECK_NOT_NULL(label_copy);
285
+ int ret = EVP_PKEY_CTX_set0_rsa_oaep_label(
286
+ ctx.get(), static_cast<unsigned char*>(label_copy), label.size());
287
+ if (ret <= 0) {
288
+ OPENSSL_free(label_copy);
289
+ return false;
290
+ }
291
+ }
292
+ return true;
293
+ }
294
+
225
295
  } // namespace margelo
@@ -50,6 +50,27 @@ using EVPMDPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>;
50
50
  using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
51
51
  using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
52
52
  using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;
53
+ using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
54
+
55
+
56
+ #ifdef __GNUC__
57
+ #define MUST_USE_RESULT __attribute__((warn_unused_result))
58
+ #else
59
+ #define MUST_USE_RESULT
60
+ #endif
61
+
62
+ struct CSPRNGResult {
63
+ const bool ok;
64
+ MUST_USE_RESULT bool is_ok() const { return ok; }
65
+ MUST_USE_RESULT bool is_err() const { return !ok; }
66
+ };
67
+
68
+ // Either succeeds with exactly |length| bytes of cryptographically
69
+ // strong pseudo-random data, or fails. This function may block.
70
+ // Don't assume anything about the contents of |buffer| on error.
71
+ // As a special case, |length == 0| can be used to check if the CSPRNG
72
+ // is properly seeded without consuming entropy.
73
+ MUST_USE_RESULT CSPRNGResult CSPRNG(void* buffer, size_t length);
53
74
 
54
75
  template <typename T>
55
76
  class NonCopyableMaybe {
@@ -198,6 +219,8 @@ class ByteSource {
198
219
 
199
220
  static ByteSource FromBIO(const BIOPointer& bio);
200
221
 
222
+ static ByteSource FromBN(const BIGNUM* bn, size_t size);
223
+
201
224
  // static ByteSource NullTerminatedCopy(Environment* env,
202
225
  // v8::Local<v8::Value> value);
203
226
  //
@@ -281,13 +304,19 @@ inline jsi::Value toJSI(jsi::Runtime& rt, ByteSource value) {
281
304
  return o;
282
305
  }
283
306
 
307
+ ByteSource GetByteSourceFromJS(jsi::Runtime &rt,
308
+ const jsi::Value &value,
309
+ std::string name);
310
+
284
311
  std::string EncodeBignum(const BIGNUM* bn,
285
- int size,
312
+ size_t size,
286
313
  bool url = false);
287
314
 
288
315
  std::string EncodeBase64(const std::string data, bool url = false);
289
316
  std::string DecodeBase64(const std::string &in, bool remove_linebreaks = false);
290
317
 
318
+ bool SetRsaOaepLabel(const EVPKeyCtxPointer& ctx, const ByteSource& label);
319
+
291
320
  // TODO: until shared, keep in sync with JS side (src/NativeQuickCrypto/Cipher.ts)
292
321
  enum KeyVariant {
293
322
  kvRSA_SSA_PKCS1_v1_5,
@@ -299,6 +328,31 @@ enum KeyVariant {
299
328
  kvDH,
300
329
  };
301
330
 
331
+ enum FnMode {
332
+ kAsync,
333
+ kSync,
334
+ };
335
+
336
+ enum WebCryptoKeyFormat {
337
+ kWebCryptoKeyFormatRaw,
338
+ kWebCryptoKeyFormatPKCS8,
339
+ kWebCryptoKeyFormatSPKI,
340
+ kWebCryptoKeyFormatJWK
341
+ };
342
+
343
+ enum WebCryptoCipherMode {
344
+ kEncrypt,
345
+ kDecrypt,
346
+ // kWrapKey,
347
+ // kUnwrapKey,
348
+ };
349
+
350
+ enum class WebCryptoCipherStatus {
351
+ OK,
352
+ INVALID_KEY_TYPE,
353
+ FAILED
354
+ };
355
+
302
356
  } // namespace margelo
303
357
 
304
358
  #endif /* MGLUtils_h */
@@ -14,60 +14,112 @@
14
14
  #ifdef ANDROID
15
15
  #include "JSIUtils/MGLJSIMacros.h"
16
16
  #include "Sig/MGLSignHostObjects.h"
17
+ #include "Cipher/MGLRsa.h"
17
18
  #include "Utils/MGLUtils.h"
19
+ #include "webcrypto/crypto_aes.h"
18
20
  #include "webcrypto/crypto_ec.h"
21
+ #include "webcrypto/crypto_keygen.h"
19
22
  #else
20
23
  #include "MGLJSIMacros.h"
21
24
  #include "MGLSignHostObjects.h"
25
+ #include "MGLRsa.h"
22
26
  #include "MGLUtils.h"
27
+ #include "crypto_aes.h"
23
28
  #include "crypto_ec.h"
29
+ #include "crypto_keygen.h"
24
30
  #endif
25
31
 
26
32
  namespace margelo {
33
+
27
34
  namespace jsi = facebook::jsi;
28
35
  namespace react = facebook::react;
29
36
 
30
- jsi::Value createWebCryptoObject(jsi::Runtime &rt) {
31
- auto obj = jsi::Object(rt);
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
+ };
32
58
 
33
- auto createKeyObjectHandle = HOSTFN("createKeyObjectHandle", 0) {
34
- auto keyObjectHandleHostObject =
35
- std::make_shared<KeyObjectHandle>();
36
- return jsi::Object::createFromHostObject(rt, keyObjectHandleHostObject);
37
- });
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
+ };
38
75
 
39
- auto ecExportKey = HOSTFN("ecExportKey", 2) {
40
- ByteSource out;
41
- std::shared_ptr<KeyObjectHandle> handle =
42
- std::static_pointer_cast<KeyObjectHandle>(
43
- args[1].asObject(rt).getHostObject(rt));
44
- std::shared_ptr<KeyObjectData> key_data = handle->Data();
45
- WebCryptoKeyExportStatus status = ECDH::doExport(rt,
46
- key_data,
47
- static_cast<WebCryptoKeyFormat>(args[0].asNumber()),
48
- {}, // blank params
49
- &out);
50
- if (status != WebCryptoKeyExportStatus::OK) {
51
- throw jsi::JSError(rt, "error exporting key, status: " + std::to_string(static_cast<int>(status)));
52
- }
53
- return toJSI(rt, std::move(out));
54
- });
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
+ };
55
83
 
56
- auto signVerify = HOSTFN("signVerify", 4) {
57
- auto ssv = SubtleSignVerify();
58
- auto params = ssv.GetParamsFromJS(rt, args);
59
- ByteSource out;
60
- ssv.DoSignVerify(rt, params, out);
61
- return ssv.EncodeOutput(rt, params, out);
62
- });
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
+ };
63
95
 
64
- obj.setProperty(rt,
65
- "createKeyObjectHandle",
66
- std::move(createKeyObjectHandle));
67
- obj.setProperty(rt, "ecExportKey", std::move(ecExportKey));
68
- obj.setProperty(rt, "signVerify", std::move(signVerify));
69
- return obj;
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));
70
123
  };
71
124
 
72
125
  } // namespace margelo
73
-
@@ -20,15 +20,13 @@
20
20
  namespace margelo {
21
21
  namespace jsi = facebook::jsi;
22
22
 
23
- enum WebCryptoKeyFormat {
24
- kWebCryptoKeyFormatRaw,
25
- kWebCryptoKeyFormatPKCS8,
26
- kWebCryptoKeyFormatSPKI,
27
- kWebCryptoKeyFormatJWK
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
28
  };
29
29
 
30
- jsi::Value createWebCryptoObject(jsi::Runtime &rt);
31
-
32
30
  } // namespace margelo
33
31
 
34
32
  #endif /* MGLWebCrypto_hpp */