react-native-quick-crypto 0.7.0-rc.9 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +51 -14
  2. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +25 -9
  3. package/cpp/Cipher/MGLRsa.cpp +13 -12
  4. package/cpp/Cipher/MGLRsa.h +2 -8
  5. package/cpp/JSIUtils/MGLJSIUtils.h +9 -0
  6. package/cpp/MGLKeys.cpp +174 -149
  7. package/cpp/MGLKeys.h +18 -13
  8. package/cpp/Sig/MGLSignHostObjects.cpp +284 -421
  9. package/cpp/Sig/MGLSignHostObjects.h +40 -0
  10. package/cpp/Utils/MGLUtils.cpp +0 -41
  11. package/cpp/Utils/MGLUtils.h +27 -6
  12. package/cpp/webcrypto/MGLWebCrypto.cpp +14 -4
  13. package/cpp/webcrypto/crypto_ec.cpp +106 -0
  14. package/cpp/webcrypto/crypto_ec.h +18 -0
  15. package/lib/commonjs/Cipher.js +138 -95
  16. package/lib/commonjs/Cipher.js.map +1 -1
  17. package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -8
  18. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  19. package/lib/commonjs/NativeQuickCrypto/sig.js +17 -0
  20. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
  21. package/lib/commonjs/Utils.js +15 -1
  22. package/lib/commonjs/Utils.js.map +1 -1
  23. package/lib/commonjs/ec.js +79 -91
  24. package/lib/commonjs/ec.js.map +1 -1
  25. package/lib/commonjs/keys.js +10 -24
  26. package/lib/commonjs/keys.js.map +1 -1
  27. package/lib/commonjs/random.js +6 -0
  28. package/lib/commonjs/random.js.map +1 -1
  29. package/lib/commonjs/subtle.js +114 -0
  30. package/lib/commonjs/subtle.js.map +1 -1
  31. package/lib/module/Cipher.js +136 -93
  32. package/lib/module/Cipher.js.map +1 -1
  33. package/lib/module/NativeQuickCrypto/Cipher.js +10 -7
  34. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  35. package/lib/module/NativeQuickCrypto/sig.js +13 -0
  36. package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
  37. package/lib/module/Utils.js +12 -0
  38. package/lib/module/Utils.js.map +1 -1
  39. package/lib/module/ec.js +76 -93
  40. package/lib/module/ec.js.map +1 -1
  41. package/lib/module/keys.js +8 -24
  42. package/lib/module/keys.js.map +1 -1
  43. package/lib/module/random.js +6 -0
  44. package/lib/module/random.js.map +1 -1
  45. package/lib/module/subtle.js +115 -1
  46. package/lib/module/subtle.js.map +1 -1
  47. package/lib/typescript/Cipher.d.ts +23 -13
  48. package/lib/typescript/Cipher.d.ts.map +1 -1
  49. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +11 -6
  50. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
  51. package/lib/typescript/NativeQuickCrypto/sig.d.ts +10 -0
  52. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +1 -1
  53. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +2 -0
  54. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
  55. package/lib/typescript/Utils.d.ts +1 -0
  56. package/lib/typescript/Utils.d.ts.map +1 -1
  57. package/lib/typescript/ec.d.ts +3 -1
  58. package/lib/typescript/ec.d.ts.map +1 -1
  59. package/lib/typescript/index.d.ts +11 -8
  60. package/lib/typescript/index.d.ts.map +1 -1
  61. package/lib/typescript/keys.d.ts +12 -1
  62. package/lib/typescript/keys.d.ts.map +1 -1
  63. package/lib/typescript/random.d.ts +2 -1
  64. package/lib/typescript/random.d.ts.map +1 -1
  65. package/lib/typescript/subtle.d.ts +4 -1
  66. package/lib/typescript/subtle.d.ts.map +1 -1
  67. package/package.json +1 -1
  68. package/src/Cipher.ts +139 -75
  69. package/src/NativeQuickCrypto/Cipher.ts +14 -14
  70. package/src/NativeQuickCrypto/sig.ts +27 -0
  71. package/src/NativeQuickCrypto/webcrypto.ts +2 -0
  72. package/src/Utils.ts +12 -0
  73. package/src/ec.ts +114 -90
  74. package/src/keys.ts +26 -31
  75. package/src/random.ts +12 -1
  76. package/src/subtle.ts +157 -1
@@ -10,6 +10,7 @@
10
10
  #include <iostream>
11
11
  #include <memory>
12
12
  #include <mutex>
13
+ #include <string>
13
14
  #include <thread>
14
15
  #include <utility>
15
16
 
@@ -18,9 +19,11 @@
18
19
  #ifdef ANDROID
19
20
  #include "JSIUtils/MGLJSIMacros.h"
20
21
  #include "JSIUtils/MGLTypedArray.h"
22
+ #include "webcrypto/crypto_ec.h"
21
23
  #else
22
24
  #include "MGLJSIMacros.h"
23
25
  #include "MGLTypedArray.h"
26
+ #include "crypto_ec.h"
24
27
  #endif
25
28
 
26
29
  using namespace facebook;
@@ -29,24 +32,44 @@ namespace margelo {
29
32
 
30
33
  std::mutex m;
31
34
 
32
- // Current implementation only supports RSA schemes (check line config.variant =
33
- // ) As more encryption schemes are added this will require an abstraction that
34
- // supports more schemes
35
35
  FieldDefinition getGenerateKeyPairFieldDefinition(
36
36
  std::shared_ptr<react::CallInvoker> jsCallInvoker,
37
37
  std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
38
38
  return buildPair(
39
39
  "generateKeyPair", JSIF([=]) {
40
- auto config = std::make_shared<RsaKeyPairGenConfig>(
40
+
41
+ KeyVariant variant =
42
+ static_cast<KeyVariant>((int)arguments[0].asNumber());
43
+ std::shared_ptr<RsaKeyPairGenConfig> rsaConfig;
44
+ std::shared_ptr<EcKeyPairGenConfig> ecConfig;
45
+
46
+ // switch on variant to get proper config from arguments
47
+ // outside of lambda 🤮
48
+ if (variant == kvRSA_SSA_PKCS1_v1_5 ||
49
+ variant == kvRSA_PSS ||
50
+ variant == kvRSA_OAEP
51
+ ) {
52
+ rsaConfig = std::make_shared<RsaKeyPairGenConfig>(
41
53
  prepareRsaKeyGenConfig(runtime, arguments));
54
+ } else
55
+ if (variant == kvEC) {
56
+ ecConfig = std::make_shared<EcKeyPairGenConfig>(
57
+ prepareEcKeyGenConfig(runtime, arguments));
58
+ } else {
59
+ throw std::runtime_error("KeyVariant not implemented"
60
+ + std::to_string((int)variant));
61
+ }
62
+
42
63
  auto promiseConstructor =
43
64
  runtime.global().getPropertyAsFunction(runtime, "Promise");
44
65
 
45
66
  auto promise = promiseConstructor.callAsConstructor(
46
67
  runtime,
47
68
  jsi::Function::createFromHostFunction(
48
- runtime, jsi::PropNameID::forAscii(runtime, "executor"), 2,
49
- [&jsCallInvoker, config](
69
+ runtime,
70
+ jsi::PropNameID::forAscii(runtime, "executor"),
71
+ 4,
72
+ [&jsCallInvoker, variant, rsaConfig, ecConfig](
50
73
  jsi::Runtime &runtime, const jsi::Value &,
51
74
  const jsi::Value *promiseArgs, size_t) -> jsi::Value {
52
75
  auto resolve =
@@ -54,19 +77,33 @@ FieldDefinition getGenerateKeyPairFieldDefinition(
54
77
  auto reject =
55
78
  std::make_shared<jsi::Value>(runtime, promiseArgs[1]);
56
79
 
57
- std::thread t([&runtime, resolve, reject,
58
- jsCallInvoker, config]() {
80
+ std::thread t([&runtime, resolve, reject, jsCallInvoker,
81
+ variant, rsaConfig, ecConfig]() {
59
82
  m.lock();
60
83
  try {
61
- jsCallInvoker->invokeAsync([&runtime, config, resolve]() {
62
- auto keys = generateRSAKeyPair(runtime, config);
63
- auto publicKey = toJSI(runtime, keys.first);
64
- auto privateKey = toJSI(runtime, keys.second);
84
+ jsCallInvoker->invokeAsync([&runtime, resolve,
85
+ variant, rsaConfig, ecConfig]() {
86
+ std::pair<jsi::Value, jsi::Value> keys;
87
+
88
+ // switch on variant to get proper generateKeyPair
89
+ if (variant == kvRSA_SSA_PKCS1_v1_5 ||
90
+ variant == kvRSA_PSS ||
91
+ variant == kvRSA_OAEP
92
+ ) {
93
+ keys = generateRsaKeyPair(runtime, rsaConfig);
94
+ } else
95
+ if (variant == kvEC) {
96
+ keys = generateEcKeyPair(runtime, ecConfig);
97
+ } else {
98
+ throw std::runtime_error("KeyVariant not implemented"
99
+ + std::to_string((int)variant));
100
+ }
101
+
65
102
  auto res = jsi::Array::createWithElements(
66
103
  runtime,
67
104
  jsi::Value::undefined(),
68
- publicKey,
69
- privateKey);
105
+ keys.first,
106
+ keys.second);
70
107
  resolve->asObject(runtime).asFunction(runtime).call(
71
108
  runtime, std::move(res));
72
109
  });
@@ -17,31 +17,47 @@
17
17
  #include "JSIUtils/MGLJSIMacros.h"
18
18
  #include "JSIUtils/MGLJSIUtils.h"
19
19
  #include "JSIUtils/MGLTypedArray.h"
20
+ #include "webcrypto/crypto_ec.h"
20
21
  #else
21
22
  #include "MGLJSIMacros.h"
22
23
  #include "MGLJSIUtils.h"
23
24
  #include "MGLTypedArray.h"
25
+ #include "crypto_ec.h"
24
26
  #endif
25
27
 
26
28
  using namespace facebook;
27
29
 
28
30
  namespace margelo {
29
31
 
30
- // Current implementation only supports RSA schemes (check line config.variant =
31
- // ) As more encryption schemes are added this will require an abstraction that
32
- // supports more schemes
33
32
  FieldDefinition getGenerateKeyPairSyncFieldDefinition(
34
33
  std::shared_ptr<react::CallInvoker> jsCallInvoker,
35
34
  std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
36
35
  return buildPair(
37
36
  "generateKeyPairSync", JSIF([=]) {
38
- auto config = std::make_shared<RsaKeyPairGenConfig>(
39
- prepareRsaKeyGenConfig(runtime, arguments));
40
- auto keys = generateRSAKeyPair(runtime, std::move(config));
41
- auto publicKey = toJSI(runtime, keys.first);
42
- auto privateKey = toJSI(runtime, keys.second);
37
+ std::pair<jsi::Value, jsi::Value> keys;
38
+ KeyVariant variant =
39
+ static_cast<KeyVariant>((int)arguments[0].asNumber());
40
+
41
+ // switch on variant to get proper config/genKeyPair
42
+ if (variant == kvRSA_SSA_PKCS1_v1_5 ||
43
+ variant == kvRSA_PSS ||
44
+ variant == kvRSA_OAEP
45
+ ) {
46
+ auto config = std::make_shared<RsaKeyPairGenConfig>(
47
+ prepareRsaKeyGenConfig(runtime, arguments));
48
+ keys = generateRsaKeyPair(runtime, config);
49
+ } else
50
+ if (variant == kvEC) {
51
+ auto config = std::make_shared<EcKeyPairGenConfig>(
52
+ prepareEcKeyGenConfig(runtime, arguments));
53
+ keys = generateEcKeyPair(runtime, config);
54
+ } else {
55
+ throw std::runtime_error("KeyVariant not implemented: " +
56
+ std::to_string((int)variant));
57
+ }
58
+ // keys.first = publicKey keys.second = privateKey
43
59
  return jsi::Array::createWithElements(
44
- runtime, jsi::Value::undefined(), publicKey, privateKey);
60
+ runtime, jsi::Value::undefined(), keys.first, keys.second);
45
61
  });
46
62
  }
47
63
  } // namespace margelo
@@ -21,7 +21,7 @@ namespace jsi = facebook::jsi;
21
21
 
22
22
  EVPKeyCtxPointer setup(std::shared_ptr<RsaKeyPairGenConfig> config) {
23
23
  EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new_id(
24
- config->variant == kKeyVariantRSA_PSS ? EVP_PKEY_RSA_PSS : EVP_PKEY_RSA,
24
+ config->variant == kvRSA_PSS ? EVP_PKEY_RSA_PSS : EVP_PKEY_RSA,
25
25
  nullptr));
26
26
 
27
27
  if (EVP_PKEY_keygen_init(ctx.get()) <= 0) return EVPKeyCtxPointer();
@@ -43,7 +43,7 @@ EVPKeyCtxPointer setup(std::shared_ptr<RsaKeyPairGenConfig> config) {
43
43
  bn.release();
44
44
  }
45
45
 
46
- if (config->variant == kKeyVariantRSA_PSS) {
46
+ if (config->variant == kvRSA_PSS) {
47
47
  if (config->md != nullptr &&
48
48
  EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx.get(), config->md) <= 0) {
49
49
  return EVPKeyCtxPointer();
@@ -94,12 +94,12 @@ RsaKeyPairGenConfig prepareRsaKeyGenConfig(jsi::Runtime& runtime,
94
94
  // CHECK(args[*offset + 1]->IsUint32()); // Modulus bits
95
95
  // CHECK(args[*offset + 2]->IsUint32()); // Exponent
96
96
  config.variant =
97
- static_cast<RSAKeyVariant>((int)arguments[offset].asNumber());
97
+ static_cast<KeyVariant>((int)arguments[offset].asNumber());
98
98
 
99
99
  // TODO(osp)
100
- // CHECK_IMPLIES(params->params.variant != kKeyVariantRSA_PSS,
100
+ // CHECK_IMPLIES(params->params.variant != kvRSA_PSS,
101
101
  // args.Length() == 10);
102
- // CHECK_IMPLIES(params->params.variant == kKeyVariantRSA_PSS,
102
+ // CHECK_IMPLIES(params->params.variant == kvRSA_PSS,
103
103
  // args.Length() == 13);
104
104
  config.modulus_bits =
105
105
  static_cast<unsigned int>(arguments[offset + 1].asNumber());
@@ -107,7 +107,7 @@ RsaKeyPairGenConfig prepareRsaKeyGenConfig(jsi::Runtime& runtime,
107
107
 
108
108
  offset += 3;
109
109
 
110
- if (config.variant == kKeyVariantRSA_PSS) {
110
+ if (config.variant == kvRSA_PSS) {
111
111
  if (!arguments[offset].isUndefined()) {
112
112
  // TODO(osp) CHECK(string)
113
113
  config.md = EVP_get_digestbyname(
@@ -153,8 +153,9 @@ RsaKeyPairGenConfig prepareRsaKeyGenConfig(jsi::Runtime& runtime,
153
153
  return config;
154
154
  }
155
155
 
156
- std::pair<JSVariant, JSVariant> generateRSAKeyPair(
156
+ std::pair<jsi::Value, jsi::Value> generateRsaKeyPair(
157
157
  jsi::Runtime& runtime, std::shared_ptr<RsaKeyPairGenConfig> config) {
158
+ // TODO: this is all copied into crypto_ec.cpp - template it up like Node?
158
159
  CheckEntropy();
159
160
 
160
161
  EVPKeyCtxPointer ctx = setup(config);
@@ -171,18 +172,18 @@ std::pair<JSVariant, JSVariant> generateRSAKeyPair(
171
172
 
172
173
  config->key = ManagedEVPPKey(EVPKeyPointer(pkey));
173
174
 
174
- OptionJSVariant publicBuffer =
175
+ jsi::Value publicBuffer =
175
176
  ManagedEVPPKey::ToEncodedPublicKey(runtime, std::move(config->key),
176
177
  config->public_key_encoding);
177
- OptionJSVariant privateBuffer =
178
+ jsi::Value privateBuffer =
178
179
  ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
179
180
  config->private_key_encoding);
180
181
 
181
- if (!publicBuffer.has_value() || !privateBuffer.has_value()) {
182
- throw jsi::JSError(runtime, "Failed to encode public and/or private key");
182
+ if (publicBuffer.isUndefined() || privateBuffer.isUndefined()) {
183
+ throw jsi::JSError(runtime, "Failed to encode public and/or private key (RSA)");
183
184
  }
184
185
 
185
- return {std::move(publicBuffer.value()), std::move(privateBuffer.value())};
186
+ return {std::move(publicBuffer), std::move(privateBuffer)};
186
187
  }
187
188
 
188
189
  jsi::Value ExportJWKRsaKey(jsi::Runtime &rt,
@@ -25,12 +25,6 @@ namespace margelo {
25
25
 
26
26
  namespace jsi = facebook::jsi;
27
27
 
28
- enum RSAKeyVariant {
29
- kKeyVariantRSA_SSA_PKCS1_v1_5,
30
- kKeyVariantRSA_PSS,
31
- kKeyVariantRSA_OAEP
32
- };
33
-
34
28
  // On node there is a complete madness of structs/classes that encapsulate and
35
29
  // initialize the data in a generic manner this is to be later be used to
36
30
  // generate the keys in a thread-safe manner (I think) I'm however too dumb and
@@ -43,7 +37,7 @@ struct RsaKeyPairGenConfig {
43
37
  PrivateKeyEncodingConfig private_key_encoding;
44
38
  ManagedEVPPKey key;
45
39
 
46
- RSAKeyVariant variant;
40
+ KeyVariant variant;
47
41
  unsigned int modulus_bits;
48
42
  unsigned int exponent;
49
43
 
@@ -57,7 +51,7 @@ struct RsaKeyPairGenConfig {
57
51
  RsaKeyPairGenConfig prepareRsaKeyGenConfig(jsi::Runtime& runtime,
58
52
  const jsi::Value* arguments);
59
53
 
60
- std::pair<JSVariant, JSVariant> generateRSAKeyPair(
54
+ std::pair<jsi::Value, jsi::Value> generateRsaKeyPair(
61
55
  jsi::Runtime& runtime, std::shared_ptr<RsaKeyPairGenConfig> config);
62
56
 
63
57
  jsi::Value ExportJWKRsaKey(jsi::Runtime &rt,
@@ -9,6 +9,7 @@
9
9
  #define MGLJSIUtils_h
10
10
 
11
11
  #include <jsi/jsi.h>
12
+ #include <limits>
12
13
 
13
14
  namespace jsi = facebook::jsi;
14
15
 
@@ -21,4 +22,12 @@ inline bool CheckSizeInt32(jsi::Runtime &runtime, jsi::ArrayBuffer &buffer) {
21
22
  return buffer.size(runtime) <= INT_MAX;
22
23
  }
23
24
 
25
+ inline bool CheckIsInt32(const jsi::Value &value) {
26
+ if (!value.isNumber()) {
27
+ return false;
28
+ }
29
+ double d = value.asNumber();
30
+ return (d >= std::numeric_limits<int32_t>::lowest() && d <= std::numeric_limits<int32_t>::max());
31
+ }
32
+
24
33
  #endif /* MGLJSIUtils_h */