react-native-quick-crypto 0.7.0-rc.8 → 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.
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +51 -14
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +25 -9
- package/cpp/Cipher/MGLRsa.cpp +13 -12
- package/cpp/Cipher/MGLRsa.h +2 -8
- package/cpp/JSIUtils/MGLJSIUtils.h +9 -0
- package/cpp/MGLKeys.cpp +174 -149
- package/cpp/MGLKeys.h +18 -13
- package/cpp/Sig/MGLSignHostObjects.cpp +284 -421
- package/cpp/Sig/MGLSignHostObjects.h +40 -0
- package/cpp/Utils/MGLUtils.cpp +0 -41
- package/cpp/Utils/MGLUtils.h +27 -6
- package/cpp/webcrypto/MGLWebCrypto.cpp +14 -4
- package/cpp/webcrypto/crypto_ec.cpp +106 -0
- package/cpp/webcrypto/crypto_ec.h +18 -0
- package/lib/commonjs/Cipher.js +138 -95
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -8
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js +17 -0
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/commonjs/Utils.js +15 -1
- package/lib/commonjs/Utils.js.map +1 -1
- package/lib/commonjs/ec.js +79 -91
- package/lib/commonjs/ec.js.map +1 -1
- package/lib/commonjs/keys.js +28 -39
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/random.js +0 -1
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/subtle.js +114 -0
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/module/Cipher.js +136 -93
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +10 -7
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/sig.js +13 -0
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/module/Utils.js +12 -0
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/ec.js +76 -93
- package/lib/module/ec.js.map +1 -1
- package/lib/module/keys.js +26 -39
- package/lib/module/keys.js.map +1 -1
- package/lib/module/random.js +0 -1
- package/lib/module/random.js.map +1 -1
- package/lib/module/subtle.js +115 -1
- package/lib/module/subtle.js.map +1 -1
- package/lib/typescript/Cipher.d.ts +23 -13
- package/lib/typescript/Cipher.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +11 -6
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/sig.d.ts +10 -0
- package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +2 -0
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
- package/lib/typescript/Utils.d.ts +3 -1
- package/lib/typescript/Utils.d.ts.map +1 -1
- package/lib/typescript/ec.d.ts +3 -1
- package/lib/typescript/ec.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +10 -7
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keys.d.ts +12 -1
- package/lib/typescript/keys.d.ts.map +1 -1
- package/lib/typescript/random.d.ts +1 -1
- package/lib/typescript/random.d.ts.map +1 -1
- package/lib/typescript/subtle.d.ts +4 -1
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Cipher.ts +139 -75
- package/src/NativeQuickCrypto/Cipher.ts +14 -14
- package/src/NativeQuickCrypto/sig.ts +27 -0
- package/src/NativeQuickCrypto/webcrypto.ts +2 -0
- package/src/Utils.ts +24 -1
- package/src/ec.ts +114 -90
- package/src/keys.ts +53 -57
- package/src/random.ts +1 -11
- 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
|
-
|
|
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,
|
|
49
|
-
|
|
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
|
-
|
|
80
|
+
std::thread t([&runtime, resolve, reject, jsCallInvoker,
|
|
81
|
+
variant, rsaConfig, ecConfig]() {
|
|
59
82
|
m.lock();
|
|
60
83
|
try {
|
|
61
|
-
jsCallInvoker->invokeAsync([&runtime,
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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
|
-
|
|
69
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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(),
|
|
60
|
+
runtime, jsi::Value::undefined(), keys.first, keys.second);
|
|
45
61
|
});
|
|
46
62
|
}
|
|
47
63
|
} // namespace margelo
|
package/cpp/Cipher/MGLRsa.cpp
CHANGED
|
@@ -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 ==
|
|
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 ==
|
|
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<
|
|
97
|
+
static_cast<KeyVariant>((int)arguments[offset].asNumber());
|
|
98
98
|
|
|
99
99
|
// TODO(osp)
|
|
100
|
-
// CHECK_IMPLIES(params->params.variant !=
|
|
100
|
+
// CHECK_IMPLIES(params->params.variant != kvRSA_PSS,
|
|
101
101
|
// args.Length() == 10);
|
|
102
|
-
// CHECK_IMPLIES(params->params.variant ==
|
|
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 ==
|
|
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<
|
|
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
|
-
|
|
175
|
+
jsi::Value publicBuffer =
|
|
175
176
|
ManagedEVPPKey::ToEncodedPublicKey(runtime, std::move(config->key),
|
|
176
177
|
config->public_key_encoding);
|
|
177
|
-
|
|
178
|
+
jsi::Value privateBuffer =
|
|
178
179
|
ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
|
|
179
180
|
config->private_key_encoding);
|
|
180
181
|
|
|
181
|
-
if (
|
|
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
|
|
186
|
+
return {std::move(publicBuffer), std::move(privateBuffer)};
|
|
186
187
|
}
|
|
187
188
|
|
|
188
189
|
jsi::Value ExportJWKRsaKey(jsi::Runtime &rt,
|
package/cpp/Cipher/MGLRsa.h
CHANGED
|
@@ -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
|
-
|
|
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<
|
|
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 */
|