react-native-quick-crypto 1.0.7 → 1.0.8
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/QuickCrypto.podspec +3 -0
- package/android/CMakeLists.txt +4 -0
- package/cpp/dh/HybridDiffieHellman.cpp +438 -0
- package/cpp/dh/HybridDiffieHellman.hpp +41 -0
- package/cpp/ecdh/HybridECDH.cpp +306 -0
- package/cpp/ecdh/HybridECDH.hpp +42 -0
- package/cpp/utils/QuickCryptoUtils.hpp +14 -0
- package/lib/commonjs/dh-groups.js +29 -0
- package/lib/commonjs/dh-groups.js.map +1 -0
- package/lib/commonjs/diffie-hellman.js +147 -0
- package/lib/commonjs/diffie-hellman.js.map +1 -0
- package/lib/commonjs/ec.js +68 -180
- package/lib/commonjs/ec.js.map +1 -1
- package/lib/commonjs/ecdh.js +71 -0
- package/lib/commonjs/ecdh.js.map +1 -0
- package/lib/commonjs/index.js +26 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys/generateKeyPair.js.map +1 -1
- package/lib/commonjs/keys/index.js +12 -0
- package/lib/commonjs/keys/index.js.map +1 -1
- package/lib/commonjs/keys/signVerify.js +42 -0
- package/lib/commonjs/keys/signVerify.js.map +1 -1
- package/lib/commonjs/specs/diffie-hellman.nitro.js +6 -0
- package/lib/commonjs/specs/diffie-hellman.nitro.js.map +1 -0
- package/lib/commonjs/specs/ecdh.nitro.js +6 -0
- package/lib/commonjs/specs/ecdh.nitro.js.map +1 -0
- package/lib/commonjs/subtle.js +2 -0
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/module/dh-groups.js +25 -0
- package/lib/module/dh-groups.js.map +1 -0
- package/lib/module/diffie-hellman.js +140 -0
- package/lib/module/diffie-hellman.js.map +1 -0
- package/lib/module/ec.js +65 -178
- package/lib/module/ec.js.map +1 -1
- package/lib/module/ecdh.js +65 -0
- package/lib/module/ecdh.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys/generateKeyPair.js.map +1 -1
- package/lib/module/keys/index.js +2 -2
- package/lib/module/keys/index.js.map +1 -1
- package/lib/module/keys/signVerify.js +40 -0
- package/lib/module/keys/signVerify.js.map +1 -1
- package/lib/module/specs/diffie-hellman.nitro.js +4 -0
- package/lib/module/specs/diffie-hellman.nitro.js.map +1 -0
- package/lib/module/specs/ecdh.nitro.js +4 -0
- package/lib/module/specs/ecdh.nitro.js.map +1 -0
- package/lib/module/subtle.js +3 -1
- package/lib/module/subtle.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/typescript/dh-groups.d.ts +5 -0
- package/lib/typescript/dh-groups.d.ts.map +1 -0
- package/lib/typescript/diffie-hellman.d.ts +16 -0
- package/lib/typescript/diffie-hellman.d.ts.map +1 -0
- package/lib/typescript/ec.d.ts +2 -1
- package/lib/typescript/ec.d.ts.map +1 -1
- package/lib/typescript/ecdh.d.ts +16 -0
- package/lib/typescript/ecdh.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +11 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keys/generateKeyPair.d.ts.map +1 -1
- package/lib/typescript/keys/index.d.ts +2 -2
- package/lib/typescript/keys/index.d.ts.map +1 -1
- package/lib/typescript/keys/signVerify.d.ts +6 -0
- package/lib/typescript/keys/signVerify.d.ts.map +1 -1
- package/lib/typescript/specs/diffie-hellman.nitro.d.ts +17 -0
- package/lib/typescript/specs/diffie-hellman.nitro.d.ts.map +1 -0
- package/lib/typescript/specs/ecdh.nitro.d.ts +14 -0
- package/lib/typescript/specs/ecdh.nitro.d.ts.map +1 -0
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +2 -0
- package/nitrogen/generated/android/QuickCryptoOnLoad.cpp +20 -0
- package/nitrogen/generated/ios/QuickCryptoAutolinking.mm +20 -0
- package/nitrogen/generated/shared/c++/HybridDiffieHellmanSpec.cpp +30 -0
- package/nitrogen/generated/shared/c++/HybridDiffieHellmanSpec.hpp +72 -0
- package/nitrogen/generated/shared/c++/HybridECDHSpec.cpp +27 -0
- package/nitrogen/generated/shared/c++/HybridECDHSpec.hpp +70 -0
- package/package.json +1 -1
- package/src/dh-groups.ts +27 -0
- package/src/diffie-hellman.ts +191 -0
- package/src/ec.ts +73 -177
- package/src/ecdh.ts +76 -0
- package/src/index.ts +6 -0
- package/src/keys/generateKeyPair.ts +11 -2
- package/src/keys/index.ts +10 -1
- package/src/keys/signVerify.ts +84 -0
- package/src/specs/diffie-hellman.nitro.ts +15 -0
- package/src/specs/ecdh.nitro.ts +11 -0
- package/src/subtle.ts +8 -1
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
#include "HybridECDH.hpp"
|
|
2
|
+
#include "QuickCryptoUtils.hpp"
|
|
3
|
+
#include <NitroModules/ArrayBuffer.hpp>
|
|
4
|
+
#include <openssl/bn.h>
|
|
5
|
+
#include <openssl/ec.h>
|
|
6
|
+
#include <openssl/err.h>
|
|
7
|
+
#include <openssl/evp.h>
|
|
8
|
+
#include <openssl/obj_mac.h>
|
|
9
|
+
#include <stdexcept>
|
|
10
|
+
|
|
11
|
+
namespace margelo::nitro::crypto {
|
|
12
|
+
|
|
13
|
+
// Smart pointer type aliases for RAII
|
|
14
|
+
using EVP_PKEY_CTX_ptr = std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)>;
|
|
15
|
+
using EC_KEY_ptr = std::unique_ptr<EC_KEY, decltype(&EC_KEY_free)>;
|
|
16
|
+
using EC_POINT_ptr = std::unique_ptr<EC_POINT, decltype(&EC_POINT_free)>;
|
|
17
|
+
using BN_ptr = std::unique_ptr<BIGNUM, decltype(&BN_free)>;
|
|
18
|
+
|
|
19
|
+
// Suppress deprecation warnings for EC_KEY_* functions
|
|
20
|
+
// These APIs work but are deprecated in OpenSSL 3.x
|
|
21
|
+
#pragma clang diagnostic push
|
|
22
|
+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
23
|
+
|
|
24
|
+
void HybridECDH::init(const std::string& curveName) {
|
|
25
|
+
int nid = getCurveNid(curveName);
|
|
26
|
+
if (nid == NID_undef) {
|
|
27
|
+
throw std::runtime_error("ECDH: unknown curve name: " + curveName);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
EC_GROUP_ptr group(EC_GROUP_new_by_curve_name(nid), EC_GROUP_free);
|
|
31
|
+
if (!group) {
|
|
32
|
+
throw std::runtime_error("ECDH: failed to create EC group for curve: " + curveName);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_curveName = curveName;
|
|
36
|
+
_curveNid = nid;
|
|
37
|
+
_group = std::move(group);
|
|
38
|
+
_pkey.reset();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
std::shared_ptr<ArrayBuffer> HybridECDH::generateKeys() {
|
|
42
|
+
ensureInitialized();
|
|
43
|
+
|
|
44
|
+
EVP_PKEY_CTX_ptr ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), EVP_PKEY_CTX_free);
|
|
45
|
+
if (!ctx) {
|
|
46
|
+
throw std::runtime_error("ECDH: failed to create keygen context");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (EVP_PKEY_keygen_init(ctx.get()) <= 0) {
|
|
50
|
+
throw std::runtime_error("ECDH: failed to initialize key generation");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), _curveNid) <= 0) {
|
|
54
|
+
throw std::runtime_error("ECDH: failed to set curve");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
EVP_PKEY* pkey = nullptr;
|
|
58
|
+
if (EVP_PKEY_keygen(ctx.get(), &pkey) <= 0) {
|
|
59
|
+
throw std::runtime_error("ECDH: failed to generate key pair");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_pkey.reset(pkey);
|
|
63
|
+
|
|
64
|
+
return getPublicKey();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
std::shared_ptr<ArrayBuffer> HybridECDH::computeSecret(const std::shared_ptr<ArrayBuffer>& otherPublicKey) {
|
|
68
|
+
ensureInitialized();
|
|
69
|
+
if (!_pkey) {
|
|
70
|
+
throw std::runtime_error("ECDH: private key not set");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Create EC_POINT from the peer's public key bytes
|
|
74
|
+
EC_POINT_ptr point(EC_POINT_new(_group.get()), EC_POINT_free);
|
|
75
|
+
if (!point) {
|
|
76
|
+
throw std::runtime_error("ECDH: failed to create EC point");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (EC_POINT_oct2point(_group.get(), point.get(), otherPublicKey->data(), otherPublicKey->size(), nullptr) != 1) {
|
|
80
|
+
throw std::runtime_error("ECDH: failed to decode peer public key");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Create EC_KEY for the peer
|
|
84
|
+
EC_KEY_ptr ecKey(EC_KEY_new(), EC_KEY_free);
|
|
85
|
+
if (!ecKey) {
|
|
86
|
+
throw std::runtime_error("ECDH: failed to create EC_KEY");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (EC_KEY_set_group(ecKey.get(), _group.get()) != 1) {
|
|
90
|
+
throw std::runtime_error("ECDH: failed to set EC group");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (EC_KEY_set_public_key(ecKey.get(), point.get()) != 1) {
|
|
94
|
+
throw std::runtime_error("ECDH: failed to set peer public key");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Create EVP_PKEY for the peer
|
|
98
|
+
EVP_PKEY_ptr peerPkey(EVP_PKEY_new(), EVP_PKEY_free);
|
|
99
|
+
if (!peerPkey) {
|
|
100
|
+
throw std::runtime_error("ECDH: failed to create peer EVP_PKEY");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// EVP_PKEY_assign_EC_KEY takes ownership of ecKey on success
|
|
104
|
+
if (EVP_PKEY_assign_EC_KEY(peerPkey.get(), ecKey.get()) != 1) {
|
|
105
|
+
throw std::runtime_error("ECDH: failed to assign EC_KEY to EVP_PKEY");
|
|
106
|
+
}
|
|
107
|
+
ecKey.release(); // EVP_PKEY now owns the EC_KEY
|
|
108
|
+
|
|
109
|
+
// Derive shared secret using EVP API
|
|
110
|
+
EVP_PKEY_CTX_ptr ctx(EVP_PKEY_CTX_new(_pkey.get(), nullptr), EVP_PKEY_CTX_free);
|
|
111
|
+
if (!ctx) {
|
|
112
|
+
throw std::runtime_error("ECDH: failed to create derive context");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (EVP_PKEY_derive_init(ctx.get()) <= 0) {
|
|
116
|
+
throw std::runtime_error("ECDH: failed to initialize key derivation");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (EVP_PKEY_derive_set_peer(ctx.get(), peerPkey.get()) <= 0) {
|
|
120
|
+
throw std::runtime_error("ECDH: failed to set peer key for derivation");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Get required buffer size
|
|
124
|
+
size_t secretLen = 0;
|
|
125
|
+
if (EVP_PKEY_derive(ctx.get(), nullptr, &secretLen) <= 0) {
|
|
126
|
+
throw std::runtime_error("ECDH: failed to get shared secret length");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Derive the shared secret
|
|
130
|
+
std::vector<uint8_t> secret(secretLen);
|
|
131
|
+
if (EVP_PKEY_derive(ctx.get(), secret.data(), &secretLen) <= 0) {
|
|
132
|
+
throw std::runtime_error("ECDH: failed to derive shared secret");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
secret.resize(secretLen);
|
|
136
|
+
|
|
137
|
+
return ToNativeArrayBuffer(secret);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
std::shared_ptr<ArrayBuffer> HybridECDH::getPrivateKey() {
|
|
141
|
+
if (!_pkey) {
|
|
142
|
+
throw std::runtime_error("ECDH: no key set");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(_pkey.get());
|
|
146
|
+
if (!ec) {
|
|
147
|
+
throw std::runtime_error("ECDH: key is not an EC key");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const BIGNUM* priv = EC_KEY_get0_private_key(ec);
|
|
151
|
+
if (!priv) {
|
|
152
|
+
throw std::runtime_error("ECDH: no private key available");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
int len = BN_num_bytes(priv);
|
|
156
|
+
std::vector<uint8_t> buf(len);
|
|
157
|
+
BN_bn2bin(priv, buf.data());
|
|
158
|
+
|
|
159
|
+
return ToNativeArrayBuffer(buf);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
void HybridECDH::setPrivateKey(const std::shared_ptr<ArrayBuffer>& privateKey) {
|
|
163
|
+
ensureInitialized();
|
|
164
|
+
|
|
165
|
+
// Create new EC_KEY
|
|
166
|
+
EC_KEY_ptr ecKey(EC_KEY_new(), EC_KEY_free);
|
|
167
|
+
if (!ecKey) {
|
|
168
|
+
throw std::runtime_error("ECDH: failed to create EC_KEY");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (EC_KEY_set_group(ecKey.get(), _group.get()) != 1) {
|
|
172
|
+
throw std::runtime_error("ECDH: failed to set EC group");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Convert private key bytes to BIGNUM
|
|
176
|
+
BN_ptr privBn(BN_bin2bn(privateKey->data(), static_cast<int>(privateKey->size()), nullptr), BN_free);
|
|
177
|
+
if (!privBn) {
|
|
178
|
+
throw std::runtime_error("ECDH: failed to convert private key");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Calculate public key from private key
|
|
182
|
+
EC_POINT_ptr pubPoint(EC_POINT_new(_group.get()), EC_POINT_free);
|
|
183
|
+
if (!pubPoint) {
|
|
184
|
+
throw std::runtime_error("ECDH: failed to create EC point");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (EC_POINT_mul(_group.get(), pubPoint.get(), privBn.get(), nullptr, nullptr, nullptr) != 1) {
|
|
188
|
+
throw std::runtime_error("ECDH: failed to compute public key from private key");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Set keys on EC_KEY (these functions copy the values, so we still own privBn and pubPoint)
|
|
192
|
+
if (EC_KEY_set_private_key(ecKey.get(), privBn.get()) != 1) {
|
|
193
|
+
throw std::runtime_error("ECDH: failed to set private key");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (EC_KEY_set_public_key(ecKey.get(), pubPoint.get()) != 1) {
|
|
197
|
+
throw std::runtime_error("ECDH: failed to set public key");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Create new EVP_PKEY
|
|
201
|
+
EVP_PKEY_ptr pkey(EVP_PKEY_new(), EVP_PKEY_free);
|
|
202
|
+
if (!pkey) {
|
|
203
|
+
throw std::runtime_error("ECDH: failed to create EVP_PKEY");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// EVP_PKEY_assign_EC_KEY takes ownership of ecKey on success
|
|
207
|
+
if (EVP_PKEY_assign_EC_KEY(pkey.get(), ecKey.get()) != 1) {
|
|
208
|
+
throw std::runtime_error("ECDH: failed to assign EC_KEY to EVP_PKEY");
|
|
209
|
+
}
|
|
210
|
+
ecKey.release(); // EVP_PKEY now owns the EC_KEY
|
|
211
|
+
|
|
212
|
+
_pkey = std::move(pkey);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
std::shared_ptr<ArrayBuffer> HybridECDH::getPublicKey() {
|
|
216
|
+
if (!_pkey) {
|
|
217
|
+
throw std::runtime_error("ECDH: no key set");
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(_pkey.get());
|
|
221
|
+
if (!ec) {
|
|
222
|
+
throw std::runtime_error("ECDH: key is not an EC key");
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const EC_POINT* point = EC_KEY_get0_public_key(ec);
|
|
226
|
+
const EC_GROUP* group = EC_KEY_get0_group(ec);
|
|
227
|
+
if (!point || !group) {
|
|
228
|
+
throw std::runtime_error("ECDH: incomplete key");
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Get uncompressed public key size
|
|
232
|
+
size_t len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED, nullptr, 0, nullptr);
|
|
233
|
+
if (len == 0) {
|
|
234
|
+
throw std::runtime_error("ECDH: failed to get public key length");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
std::vector<uint8_t> buf(len);
|
|
238
|
+
if (EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED, buf.data(), len, nullptr) == 0) {
|
|
239
|
+
throw std::runtime_error("ECDH: failed to encode public key");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return ToNativeArrayBuffer(buf);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
void HybridECDH::setPublicKey(const std::shared_ptr<ArrayBuffer>& publicKey) {
|
|
246
|
+
ensureInitialized();
|
|
247
|
+
|
|
248
|
+
// Create EC_POINT from the public key bytes
|
|
249
|
+
EC_POINT_ptr point(EC_POINT_new(_group.get()), EC_POINT_free);
|
|
250
|
+
if (!point) {
|
|
251
|
+
throw std::runtime_error("ECDH: failed to create EC point");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (EC_POINT_oct2point(_group.get(), point.get(), publicKey->data(), publicKey->size(), nullptr) != 1) {
|
|
255
|
+
throw std::runtime_error("ECDH: invalid public key");
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Create new EC_KEY
|
|
259
|
+
EC_KEY_ptr ecKey(EC_KEY_new(), EC_KEY_free);
|
|
260
|
+
if (!ecKey) {
|
|
261
|
+
throw std::runtime_error("ECDH: failed to create EC_KEY");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (EC_KEY_set_group(ecKey.get(), _group.get()) != 1) {
|
|
265
|
+
throw std::runtime_error("ECDH: failed to set EC group");
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (EC_KEY_set_public_key(ecKey.get(), point.get()) != 1) {
|
|
269
|
+
throw std::runtime_error("ECDH: failed to set public key");
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Create new EVP_PKEY
|
|
273
|
+
EVP_PKEY_ptr pkey(EVP_PKEY_new(), EVP_PKEY_free);
|
|
274
|
+
if (!pkey) {
|
|
275
|
+
throw std::runtime_error("ECDH: failed to create EVP_PKEY");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// EVP_PKEY_assign_EC_KEY takes ownership of ecKey on success
|
|
279
|
+
if (EVP_PKEY_assign_EC_KEY(pkey.get(), ecKey.get()) != 1) {
|
|
280
|
+
throw std::runtime_error("ECDH: failed to assign EC_KEY to EVP_PKEY");
|
|
281
|
+
}
|
|
282
|
+
ecKey.release(); // EVP_PKEY now owns the EC_KEY
|
|
283
|
+
|
|
284
|
+
_pkey = std::move(pkey);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
void HybridECDH::ensureInitialized() const {
|
|
288
|
+
if (_curveNid == 0 || !_group) {
|
|
289
|
+
throw std::runtime_error("ECDH: not initialized");
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
int HybridECDH::getCurveNid(const std::string& name) {
|
|
294
|
+
int nid = OBJ_txt2nid(name.c_str());
|
|
295
|
+
if (nid == NID_undef) {
|
|
296
|
+
nid = OBJ_sn2nid(name.c_str());
|
|
297
|
+
}
|
|
298
|
+
if (nid == NID_undef) {
|
|
299
|
+
nid = OBJ_ln2nid(name.c_str());
|
|
300
|
+
}
|
|
301
|
+
return nid;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
#pragma clang diagnostic pop
|
|
305
|
+
|
|
306
|
+
} // namespace margelo::nitro::crypto
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <memory>
|
|
4
|
+
#include <openssl/ec.h>
|
|
5
|
+
#include <openssl/evp.h>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#include "HybridECDHSpec.hpp"
|
|
10
|
+
|
|
11
|
+
namespace margelo::nitro::crypto {
|
|
12
|
+
|
|
13
|
+
using namespace facebook;
|
|
14
|
+
using margelo::nitro::ArrayBuffer;
|
|
15
|
+
|
|
16
|
+
using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)>;
|
|
17
|
+
using EC_GROUP_ptr = std::unique_ptr<EC_GROUP, decltype(&EC_GROUP_free)>;
|
|
18
|
+
|
|
19
|
+
class HybridECDH : public HybridECDHSpec {
|
|
20
|
+
public:
|
|
21
|
+
HybridECDH() : HybridObject("ECDH"), _pkey(nullptr, EVP_PKEY_free), _group(nullptr, EC_GROUP_free) {}
|
|
22
|
+
virtual ~HybridECDH() = default;
|
|
23
|
+
|
|
24
|
+
void init(const std::string& curveName) override;
|
|
25
|
+
std::shared_ptr<ArrayBuffer> generateKeys() override;
|
|
26
|
+
std::shared_ptr<ArrayBuffer> computeSecret(const std::shared_ptr<ArrayBuffer>& otherPublicKey) override;
|
|
27
|
+
std::shared_ptr<ArrayBuffer> getPrivateKey() override;
|
|
28
|
+
void setPrivateKey(const std::shared_ptr<ArrayBuffer>& privateKey) override;
|
|
29
|
+
std::shared_ptr<ArrayBuffer> getPublicKey() override;
|
|
30
|
+
void setPublicKey(const std::shared_ptr<ArrayBuffer>& publicKey) override;
|
|
31
|
+
|
|
32
|
+
private:
|
|
33
|
+
EVP_PKEY_ptr _pkey;
|
|
34
|
+
EC_GROUP_ptr _group;
|
|
35
|
+
std::string _curveName;
|
|
36
|
+
int _curveNid = 0;
|
|
37
|
+
|
|
38
|
+
void ensureInitialized() const;
|
|
39
|
+
static int getCurveNid(const std::string& name);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
} // namespace margelo::nitro::crypto
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
#include <limits>
|
|
6
6
|
#include <openssl/err.h>
|
|
7
7
|
#include <string>
|
|
8
|
+
#include <vector>
|
|
8
9
|
|
|
9
10
|
#include "Macros.hpp"
|
|
10
11
|
#include <NitroModules/ArrayBuffer.hpp>
|
|
@@ -44,6 +45,19 @@ inline std::shared_ptr<margelo::nitro::NativeArrayBuffer> ToNativeArrayBuffer(st
|
|
|
44
45
|
return std::make_shared<margelo::nitro::NativeArrayBuffer>(data, size, [=]() { delete[] data; });
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
inline std::shared_ptr<margelo::nitro::NativeArrayBuffer> ToNativeArrayBuffer(const std::vector<uint8_t>& vec) {
|
|
49
|
+
size_t size = vec.size();
|
|
50
|
+
uint8_t* data = new uint8_t[size];
|
|
51
|
+
memcpy(data, vec.data(), size);
|
|
52
|
+
return std::make_shared<margelo::nitro::NativeArrayBuffer>(data, size, [=]() { delete[] data; });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
inline std::shared_ptr<margelo::nitro::NativeArrayBuffer> ToNativeArrayBuffer(const uint8_t* ptr, size_t size) {
|
|
56
|
+
uint8_t* data = new uint8_t[size];
|
|
57
|
+
memcpy(data, ptr, size);
|
|
58
|
+
return std::make_shared<margelo::nitro::NativeArrayBuffer>(data, size, [=]() { delete[] data; });
|
|
59
|
+
}
|
|
60
|
+
|
|
47
61
|
inline bool CheckIsUint32(double value) {
|
|
48
62
|
return (value >= std::numeric_limits<uint32_t>::lowest() && value <= std::numeric_limits<uint32_t>::max());
|
|
49
63
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DH_GROUPS = void 0;
|
|
7
|
+
const DH_GROUPS = exports.DH_GROUPS = {
|
|
8
|
+
modp14: {
|
|
9
|
+
prime: 'ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff',
|
|
10
|
+
generator: '02'
|
|
11
|
+
},
|
|
12
|
+
modp15: {
|
|
13
|
+
prime: 'ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a93ad2caffffffffffffffff',
|
|
14
|
+
generator: '02'
|
|
15
|
+
},
|
|
16
|
+
modp16: {
|
|
17
|
+
prime: 'ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c934063199ffffffffffffffff',
|
|
18
|
+
generator: '02'
|
|
19
|
+
},
|
|
20
|
+
modp17: {
|
|
21
|
+
prime: 'ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dcc4024ffffffffffffffff',
|
|
22
|
+
generator: '02'
|
|
23
|
+
},
|
|
24
|
+
modp18: {
|
|
25
|
+
prime: 'ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dbe115974a3926f12fee5e438777cb6a932df8cd8bec4d073b931ba3bc832b68d9dd300741fa7bf8afc47ed2576f6936ba424663aab639c5ae4f5683423b4742bf1c978238f16cbe39d652de3fdb8befc848ad922222e04a4037c0713eb57a81a23f0c73473fc646cea306b4bcbc8862f8385ddfa9d4b7fa2c087e879683303ed5bdd3a062b3cf5b3a278a66d2a13f83f44f82ddf310ee074ab6a364597e899a0255dc164f31cc50846851df9ab48195ded7ea1b1d510bd7ee74d73faf36bc31ecfa268359046f4eb879f924009438b481c6cd7889a002ed5ee382bc9190da6fc026e479558e4475677e9aa9e3050e2765694dfc81f56e880b96e7160c980dd98edd3dfffffffffffffffff',
|
|
26
|
+
generator: '02'
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=dh-groups.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["DH_GROUPS","exports","modp14","prime","generator","modp15","modp16","modp17","modp18"],"sourceRoot":"../../src","sources":["dh-groups.ts"],"mappings":";;;;;;AAAO,MAAMA,SAA+D,GAAAC,OAAA,CAAAD,SAAA,GAAG;EAC7EE,MAAM,EAAE;IACNC,KAAK,EACH,kgBAAkgB;IACpgBC,SAAS,EAAE;EACb,CAAC;EACDC,MAAM,EAAE;IACNF,KAAK,EACH,kwBAAkwB;IACpwBC,SAAS,EAAE;EACb,CAAC;EACDE,MAAM,EAAE;IACNH,KAAK,EACH,kgCAAkgC;IACpgCC,SAAS,EAAE;EACb,CAAC;EACDG,MAAM,EAAE;IACNJ,KAAK,EACH,kgDAAkgD;IACpgDC,SAAS,EAAE;EACb,CAAC;EACDI,MAAM,EAAE;IACNL,KAAK,EACH,kgEAAkgE;IACpgEC,SAAS,EAAE;EACb;AACF,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.DiffieHellman = void 0;
|
|
7
|
+
exports.createDiffieHellman = createDiffieHellman;
|
|
8
|
+
exports.getDiffieHellman = getDiffieHellman;
|
|
9
|
+
var _reactNativeNitroModules = require("react-native-nitro-modules");
|
|
10
|
+
var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
|
|
11
|
+
var _dhGroups = require("./dh-groups");
|
|
12
|
+
class DiffieHellman {
|
|
13
|
+
constructor(sizeOrPrime, generator, encoding) {
|
|
14
|
+
this._hybrid = _reactNativeNitroModules.NitroModules.createHybridObject('DiffieHellman');
|
|
15
|
+
if (typeof sizeOrPrime === 'number') {
|
|
16
|
+
const gen = typeof generator === 'number' ? generator : 2;
|
|
17
|
+
this._hybrid.initWithSize(sizeOrPrime, gen);
|
|
18
|
+
} else {
|
|
19
|
+
let primeBuf;
|
|
20
|
+
if (_reactNativeBuffer.Buffer.isBuffer(sizeOrPrime)) {
|
|
21
|
+
primeBuf = sizeOrPrime;
|
|
22
|
+
} else {
|
|
23
|
+
primeBuf = _reactNativeBuffer.Buffer.from(sizeOrPrime, encoding);
|
|
24
|
+
}
|
|
25
|
+
let genBuf;
|
|
26
|
+
if (generator === undefined) {
|
|
27
|
+
genBuf = _reactNativeBuffer.Buffer.from([2]);
|
|
28
|
+
} else if (typeof generator === 'number') {
|
|
29
|
+
genBuf = _reactNativeBuffer.Buffer.from([generator]);
|
|
30
|
+
} else if (_reactNativeBuffer.Buffer.isBuffer(generator)) {
|
|
31
|
+
genBuf = generator;
|
|
32
|
+
} else {
|
|
33
|
+
genBuf = _reactNativeBuffer.Buffer.from(generator, encoding);
|
|
34
|
+
}
|
|
35
|
+
this._hybrid.init(primeBuf.buffer, genBuf.buffer);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
generateKeys(encoding) {
|
|
39
|
+
const keys = _reactNativeBuffer.Buffer.from(this._hybrid.generateKeys());
|
|
40
|
+
if (encoding) return keys.toString(encoding);
|
|
41
|
+
return keys;
|
|
42
|
+
}
|
|
43
|
+
computeSecret(otherPublicKey, inputEncoding, outputEncoding) {
|
|
44
|
+
let keyBuf;
|
|
45
|
+
if (_reactNativeBuffer.Buffer.isBuffer(otherPublicKey)) {
|
|
46
|
+
keyBuf = otherPublicKey;
|
|
47
|
+
} else {
|
|
48
|
+
keyBuf = _reactNativeBuffer.Buffer.from(otherPublicKey, inputEncoding);
|
|
49
|
+
}
|
|
50
|
+
const secret = _reactNativeBuffer.Buffer.from(this._hybrid.computeSecret(keyBuf.buffer));
|
|
51
|
+
if (outputEncoding) return secret.toString(outputEncoding);
|
|
52
|
+
return secret;
|
|
53
|
+
}
|
|
54
|
+
getPrime(encoding) {
|
|
55
|
+
const p = _reactNativeBuffer.Buffer.from(this._hybrid.getPrime());
|
|
56
|
+
if (encoding) return p.toString(encoding);
|
|
57
|
+
return p;
|
|
58
|
+
}
|
|
59
|
+
getGenerator(encoding) {
|
|
60
|
+
const g = _reactNativeBuffer.Buffer.from(this._hybrid.getGenerator());
|
|
61
|
+
if (encoding) return g.toString(encoding);
|
|
62
|
+
return g;
|
|
63
|
+
}
|
|
64
|
+
getPublicKey(encoding) {
|
|
65
|
+
const p = _reactNativeBuffer.Buffer.from(this._hybrid.getPublicKey());
|
|
66
|
+
if (encoding) return p.toString(encoding);
|
|
67
|
+
return p;
|
|
68
|
+
}
|
|
69
|
+
getPrivateKey(encoding) {
|
|
70
|
+
const p = _reactNativeBuffer.Buffer.from(this._hybrid.getPrivateKey());
|
|
71
|
+
if (encoding) return p.toString(encoding);
|
|
72
|
+
return p;
|
|
73
|
+
}
|
|
74
|
+
setPublicKey(publicKey, encoding) {
|
|
75
|
+
let keyBuf;
|
|
76
|
+
if (_reactNativeBuffer.Buffer.isBuffer(publicKey)) {
|
|
77
|
+
keyBuf = publicKey;
|
|
78
|
+
} else {
|
|
79
|
+
keyBuf = _reactNativeBuffer.Buffer.from(publicKey, encoding);
|
|
80
|
+
}
|
|
81
|
+
this._hybrid.setPublicKey(keyBuf.buffer);
|
|
82
|
+
}
|
|
83
|
+
setPrivateKey(privateKey, encoding) {
|
|
84
|
+
let keyBuf;
|
|
85
|
+
if (_reactNativeBuffer.Buffer.isBuffer(privateKey)) {
|
|
86
|
+
keyBuf = privateKey;
|
|
87
|
+
} else {
|
|
88
|
+
keyBuf = _reactNativeBuffer.Buffer.from(privateKey, encoding);
|
|
89
|
+
}
|
|
90
|
+
this._hybrid.setPrivateKey(keyBuf.buffer);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.DiffieHellman = DiffieHellman;
|
|
94
|
+
function createDiffieHellman(primeOrSize, primeEncodingOrGenerator, generator, _generatorEncoding) {
|
|
95
|
+
if (typeof primeOrSize === 'number') {
|
|
96
|
+
const gen = typeof primeEncodingOrGenerator === 'number' ? primeEncodingOrGenerator : 2;
|
|
97
|
+
return new DiffieHellman(primeOrSize, gen);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Standardize arguments for String/Buffer prime
|
|
101
|
+
// createDiffieHellman(prime, [encoding], [generator], [encoding])
|
|
102
|
+
|
|
103
|
+
let prime;
|
|
104
|
+
let generatorVal;
|
|
105
|
+
if (_reactNativeBuffer.Buffer.isBuffer(primeOrSize)) {
|
|
106
|
+
prime = primeOrSize;
|
|
107
|
+
// 2nd arg is generator if not string (encoding)
|
|
108
|
+
if (primeEncodingOrGenerator !== undefined && typeof primeEncodingOrGenerator !== 'string') {
|
|
109
|
+
generatorVal = primeEncodingOrGenerator;
|
|
110
|
+
} else if (generator !== undefined) {
|
|
111
|
+
generatorVal = generator;
|
|
112
|
+
} else {
|
|
113
|
+
generatorVal = 2;
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
// String prime
|
|
117
|
+
const encoding = typeof primeEncodingOrGenerator === 'string' ? primeEncodingOrGenerator : 'utf8'; // Defaulting to utf8 or hex? Node default is 'binary' usually but utf8 safer for TS. Node docs say: "If no encoding is specified, 'binary' is used."
|
|
118
|
+
// We'll trust user passed encoding if it's a string, otherwise handle it.
|
|
119
|
+
prime = _reactNativeBuffer.Buffer.from(primeOrSize, encoding);
|
|
120
|
+
|
|
121
|
+
// Generator handling in this case
|
|
122
|
+
if (generator !== undefined) {
|
|
123
|
+
generatorVal = generator;
|
|
124
|
+
if (typeof generator === 'string' && _generatorEncoding) {
|
|
125
|
+
generatorVal = _reactNativeBuffer.Buffer.from(generator, _generatorEncoding);
|
|
126
|
+
} else if (typeof generator === 'string') {
|
|
127
|
+
// string with no encoding, assume same as prime? or utf8?
|
|
128
|
+
generatorVal = _reactNativeBuffer.Buffer.from(generator, encoding);
|
|
129
|
+
}
|
|
130
|
+
} else if (typeof primeEncodingOrGenerator !== 'string' && primeEncodingOrGenerator !== undefined) {
|
|
131
|
+
// 2nd arg was generator
|
|
132
|
+
generatorVal = primeEncodingOrGenerator;
|
|
133
|
+
} else {
|
|
134
|
+
generatorVal = 2;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return new DiffieHellman(prime, generatorVal);
|
|
138
|
+
}
|
|
139
|
+
function getDiffieHellman(groupName) {
|
|
140
|
+
const group = _dhGroups.DH_GROUPS[groupName];
|
|
141
|
+
if (!group) {
|
|
142
|
+
throw new Error(`Unknown group: ${groupName}`);
|
|
143
|
+
}
|
|
144
|
+
// group.prime and group.generator are hex strings
|
|
145
|
+
return new DiffieHellman(group.prime, group.generator, 'hex');
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=diffie-hellman.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_reactNativeNitroModules","require","_reactNativeBuffer","_dhGroups","DiffieHellman","constructor","sizeOrPrime","generator","encoding","_hybrid","NitroModules","createHybridObject","gen","initWithSize","primeBuf","Buffer","isBuffer","from","genBuf","undefined","init","buffer","generateKeys","keys","toString","computeSecret","otherPublicKey","inputEncoding","outputEncoding","keyBuf","secret","getPrime","p","getGenerator","g","getPublicKey","getPrivateKey","setPublicKey","publicKey","setPrivateKey","privateKey","exports","createDiffieHellman","primeOrSize","primeEncodingOrGenerator","_generatorEncoding","prime","generatorVal","getDiffieHellman","groupName","group","DH_GROUPS","Error"],"sourceRoot":"../../src","sources":["diffie-hellman.ts"],"mappings":";;;;;;;;AAAA,IAAAA,wBAAA,GAAAC,OAAA;AAEA,IAAAC,kBAAA,GAAAD,OAAA;AACA,IAAAE,SAAA,GAAAF,OAAA;AAEO,MAAMG,aAAa,CAAC;EAGzBC,WAAWA,CACTC,WAAqC,EACrCC,SAAoC,EACpCC,QAAyB,EACzB;IACA,IAAI,CAACC,OAAO,GACVC,qCAAY,CAACC,kBAAkB,CAAyB,eAAe,CAAC;IAE1E,IAAI,OAAOL,WAAW,KAAK,QAAQ,EAAE;MACnC,MAAMM,GAAG,GAAG,OAAOL,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