edhoc 1.2.2 → 1.3.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/dist/edhoc.d.ts +4 -0
- package/dist/edhoc.d.ts.map +1 -1
- package/include/{LibEDHOC.h → Binding.h} +60 -40
- package/include/EdhocComposeAsyncWorker.h +8 -22
- package/include/EdhocCredentialManager.h +9 -25
- package/include/EdhocCryptoManager.h +27 -43
- package/include/EdhocEadManager.h +3 -4
- package/include/EdhocExportOscoreAsyncWorker.h +10 -27
- package/include/EdhocKeyExporterAsyncWorker.h +8 -28
- package/include/EdhocKeyUpdateAsyncWorker.h +7 -24
- package/include/EdhocProcessAsyncWorker.h +11 -36
- package/include/RunningContext.h +102 -0
- package/include/Utils.h +0 -43
- package/package.json +1 -2
- package/prebuilds/android-arm/edhoc.armv7.node +0 -0
- package/prebuilds/android-arm64/edhoc.armv8.node +0 -0
- package/prebuilds/darwin-arm64/edhoc.node +0 -0
- package/prebuilds/darwin-x64/edhoc.node +0 -0
- package/prebuilds/linux-arm/edhoc.armv6.node +0 -0
- package/prebuilds/linux-arm/edhoc.armv7.node +0 -0
- package/prebuilds/linux-arm64/edhoc.armv8.node +0 -0
- package/prebuilds/linux-x64/edhoc.glibc.node +0 -0
- package/prebuilds/linux-x64/edhoc.musl.node +0 -0
- package/prebuilds/win32-ia32/edhoc.node +0 -0
- package/prebuilds/win32-x64/edhoc.node +0 -0
- package/src/Binding.cpp +434 -0
- package/src/EdhocComposeAsyncWorker.cpp +15 -26
- package/src/EdhocCredentialManager.cpp +50 -69
- package/src/EdhocCryptoManager.cpp +158 -332
- package/src/EdhocEadManager.cpp +11 -11
- package/src/EdhocExportOscoreAsyncWorker.cpp +18 -28
- package/src/EdhocKeyExporterAsyncWorker.cpp +11 -21
- package/src/EdhocKeyUpdateAsyncWorker.cpp +8 -18
- package/src/EdhocProcessAsyncWorker.cpp +28 -35
- package/src/RunningContext.cpp +95 -0
- package/src/Utils.cpp +1 -38
- package/test/basic.test.ts +57 -3
- package/include/UserContext.h +0 -77
- package/src/LibEDHOC.cpp +0 -408
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
#include <stdexcept>
|
|
7
7
|
#include <thread>
|
|
8
8
|
|
|
9
|
-
#include "
|
|
9
|
+
#include "RunningContext.h"
|
|
10
10
|
#include "Utils.h"
|
|
11
11
|
|
|
12
12
|
static constexpr const char* kErrorInvalidUint8ArrayLength = "Returned Uint8Array length exceeds buffer length.";
|
|
@@ -29,27 +29,17 @@ static constexpr const char* kErrorResultObjectExpected = "Expected result to be
|
|
|
29
29
|
static constexpr const char* kErrorKeysExpectedAsBuffers = "Expected keys to be buffers.";
|
|
30
30
|
static constexpr const char* kErrorPrivateKeyLengthExceeds = "Private key length exceeds buffer size.";
|
|
31
31
|
static constexpr const char* kErrorObjectExpected = "Object expected";
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
static constexpr const char* kMakeKeyPair = "makeKeyPair";
|
|
36
|
-
static constexpr const char* kKeyAgreement = "keyAgreement";
|
|
37
|
-
static constexpr const char* kSign = "sign";
|
|
38
|
-
static constexpr const char* kVerify = "verify";
|
|
39
|
-
static constexpr const char* kExtract = "extract";
|
|
40
|
-
static constexpr const char* kExpand = "expand";
|
|
41
|
-
static constexpr const char* kEncrypt = "encrypt";
|
|
42
|
-
static constexpr const char* kDecrypt = "decrypt";
|
|
43
|
-
static constexpr const char* kHash = "hash";
|
|
44
|
-
|
|
45
|
-
EdhocCryptoManager::EdhocCryptoManager(Napi::Object& jsCryptoManager) {
|
|
46
|
-
if (!jsCryptoManager.IsObject()) {
|
|
32
|
+
|
|
33
|
+
EdhocCryptoManager::EdhocCryptoManager(Napi::Object& jsCryptoManager, Napi::Object& jsEdhoc) {
|
|
34
|
+
if (!jsCryptoManager.IsObject() || !jsEdhoc.IsObject()) {
|
|
47
35
|
Napi::Error::New(jsCryptoManager.Env(), kErrorObjectExpected).ThrowAsJavaScriptException();
|
|
48
36
|
}
|
|
49
37
|
cryptoManagerRef = Napi::Persistent(jsCryptoManager);
|
|
38
|
+
edhocRef = Napi::Weak(jsEdhoc);
|
|
50
39
|
|
|
51
40
|
keys.import_key = &EdhocCryptoManager::ImportKey;
|
|
52
41
|
keys.destroy_key = &EdhocCryptoManager::DestroyKey;
|
|
42
|
+
|
|
53
43
|
crypto.make_key_pair = &EdhocCryptoManager::MakeKeyPair;
|
|
54
44
|
crypto.key_agreement = &EdhocCryptoManager::KeyAgreement;
|
|
55
45
|
crypto.signature = &EdhocCryptoManager::Sign;
|
|
@@ -63,64 +53,25 @@ EdhocCryptoManager::EdhocCryptoManager(Napi::Object& jsCryptoManager) {
|
|
|
63
53
|
|
|
64
54
|
EdhocCryptoManager::~EdhocCryptoManager() {
|
|
65
55
|
cryptoManagerRef.Reset();
|
|
56
|
+
edhocRef.Reset();
|
|
66
57
|
for (auto& ref : bufferReferences) {
|
|
67
58
|
ref.Reset();
|
|
68
59
|
}
|
|
69
60
|
bufferReferences.clear();
|
|
70
61
|
}
|
|
71
62
|
|
|
72
|
-
void EdhocCryptoManager::SetupAsyncFunctions() {
|
|
73
|
-
SetFunction(kImportKey, importTsfn);
|
|
74
|
-
SetFunction(kDestroyKey, destroyTsfn);
|
|
75
|
-
SetFunction(kMakeKeyPair, makeKeyPairTsfn);
|
|
76
|
-
SetFunction(kKeyAgreement, keyAgreementTsfn);
|
|
77
|
-
SetFunction(kSign, signTsfn);
|
|
78
|
-
SetFunction(kVerify, verifyTsfn);
|
|
79
|
-
SetFunction(kExtract, extractTsfn);
|
|
80
|
-
SetFunction(kExpand, expandTsfn);
|
|
81
|
-
SetFunction(kEncrypt, encryptTsfn);
|
|
82
|
-
SetFunction(kDecrypt, decryptTsfn);
|
|
83
|
-
SetFunction(kHash, hashTsfn);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
void EdhocCryptoManager::CleanupAsyncFunctions() {
|
|
87
|
-
importTsfn.Release();
|
|
88
|
-
destroyTsfn.Release();
|
|
89
|
-
makeKeyPairTsfn.Release();
|
|
90
|
-
keyAgreementTsfn.Release();
|
|
91
|
-
signTsfn.Release();
|
|
92
|
-
verifyTsfn.Release();
|
|
93
|
-
extractTsfn.Release();
|
|
94
|
-
expandTsfn.Release();
|
|
95
|
-
encryptTsfn.Release();
|
|
96
|
-
decryptTsfn.Release();
|
|
97
|
-
hashTsfn.Release();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
void EdhocCryptoManager::SetFunction(const char* name, Napi::ThreadSafeFunction& tsfn) {
|
|
101
|
-
Napi::Env env = cryptoManagerRef.Env();
|
|
102
|
-
Napi::HandleScope scope(env);
|
|
103
|
-
Napi::Function jsFunction = cryptoManagerRef.Value().Get(name).As<Napi::Function>();
|
|
104
|
-
if (!jsFunction.IsFunction()) {
|
|
105
|
-
Napi::Error::New(env, kErrorFunctionExpected).ThrowAsJavaScriptException();
|
|
106
|
-
}
|
|
107
|
-
tsfn = Napi::ThreadSafeFunction::New(env, jsFunction, name, 0, 1);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
63
|
int EdhocCryptoManager::ImportKey(void* user_context,
|
|
111
64
|
enum edhoc_key_type key_type,
|
|
112
65
|
const uint8_t* raw_key,
|
|
113
66
|
size_t raw_key_length,
|
|
114
67
|
void* key_id) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return cryptoManager->callImportKey(user_context, key_type, raw_key, raw_key_length, key_id);
|
|
68
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
69
|
+
return context->GetCryptoManager()->callImportKey(context, key_type, raw_key, raw_key_length, key_id);
|
|
118
70
|
}
|
|
119
71
|
|
|
120
72
|
int EdhocCryptoManager::DestroyKey(void* user_context, void* key_id) {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return cryptoManager->callDestroyKey(user_context, key_id);
|
|
73
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
74
|
+
return context->GetCryptoManager()->callDestroyKey(context, key_id);
|
|
124
75
|
}
|
|
125
76
|
|
|
126
77
|
int EdhocCryptoManager::MakeKeyPair(void* user_context,
|
|
@@ -131,9 +82,8 @@ int EdhocCryptoManager::MakeKeyPair(void* user_context,
|
|
|
131
82
|
uint8_t* public_key,
|
|
132
83
|
size_t public_key_size,
|
|
133
84
|
size_t* public_key_length) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return cryptoManager->callMakeKeyPair(user_context, key_id, private_key, private_key_size, private_key_length,
|
|
85
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
86
|
+
return context->GetCryptoManager()->callMakeKeyPair(context, key_id, private_key, private_key_size, private_key_length,
|
|
137
87
|
public_key, public_key_size, public_key_length);
|
|
138
88
|
}
|
|
139
89
|
|
|
@@ -144,9 +94,8 @@ int EdhocCryptoManager::KeyAgreement(void* user_context,
|
|
|
144
94
|
uint8_t* shared_secret,
|
|
145
95
|
size_t shared_secret_size,
|
|
146
96
|
size_t* shared_secret_length) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
return cryptoManager->callKeyAgreement(user_context, key_id, peer_public_key, peer_public_key_length, shared_secret,
|
|
97
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
98
|
+
return context->GetCryptoManager()->callKeyAgreement(context, key_id, peer_public_key, peer_public_key_length, shared_secret,
|
|
150
99
|
shared_secret_size, shared_secret_length);
|
|
151
100
|
}
|
|
152
101
|
|
|
@@ -157,9 +106,8 @@ int EdhocCryptoManager::Sign(void* user_context,
|
|
|
157
106
|
uint8_t* signature,
|
|
158
107
|
size_t signature_size,
|
|
159
108
|
size_t* signature_length) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return cryptoManager->callSign(user_context, key_id, input, input_length, signature, signature_size,
|
|
109
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
110
|
+
return context->GetCryptoManager()->callSign(context, key_id, input, input_length, signature, signature_size,
|
|
163
111
|
signature_length);
|
|
164
112
|
}
|
|
165
113
|
|
|
@@ -169,9 +117,8 @@ int EdhocCryptoManager::Verify(void* user_context,
|
|
|
169
117
|
size_t input_length,
|
|
170
118
|
const uint8_t* signature,
|
|
171
119
|
size_t signature_length) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return cryptoManager->callVerify(user_context, key_id, input, input_length, signature, signature_length);
|
|
120
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
121
|
+
return context->GetCryptoManager()->callVerify(context, key_id, input, input_length, signature, signature_length);
|
|
175
122
|
}
|
|
176
123
|
|
|
177
124
|
int EdhocCryptoManager::Extract(void* user_context,
|
|
@@ -181,10 +128,9 @@ int EdhocCryptoManager::Extract(void* user_context,
|
|
|
181
128
|
uint8_t* pseudo_random_key,
|
|
182
129
|
size_t pseudo_random_key_size,
|
|
183
130
|
size_t* pseudo_random_key_length) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
pseudo_random_key_length);
|
|
131
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
132
|
+
return context->GetCryptoManager()->callExtract(context, key_id, salt, salt_len, pseudo_random_key,
|
|
133
|
+
pseudo_random_key_size, pseudo_random_key_length);
|
|
188
134
|
}
|
|
189
135
|
|
|
190
136
|
int EdhocCryptoManager::Expand(void* user_context,
|
|
@@ -193,9 +139,8 @@ int EdhocCryptoManager::Expand(void* user_context,
|
|
|
193
139
|
size_t info_length,
|
|
194
140
|
uint8_t* output_keying_material,
|
|
195
141
|
size_t output_keying_material_length) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
return cryptoManager->callExpand(user_context, key_id, info, info_length, output_keying_material,
|
|
142
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
143
|
+
return context->GetCryptoManager()->callExpand(context, key_id, info, info_length, output_keying_material,
|
|
199
144
|
output_keying_material_length);
|
|
200
145
|
}
|
|
201
146
|
|
|
@@ -210,9 +155,8 @@ int EdhocCryptoManager::Encrypt(void* user_context,
|
|
|
210
155
|
uint8_t* ciphertext,
|
|
211
156
|
size_t ciphertext_size,
|
|
212
157
|
size_t* ciphertext_length) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
return cryptoManager->callEncrypt(user_context, key_id, nonce, nonce_length, additional_data, additional_data_length,
|
|
158
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
159
|
+
return context->GetCryptoManager()->callEncrypt(context, key_id, nonce, nonce_length, additional_data, additional_data_length,
|
|
216
160
|
plaintext, plaintext_length, ciphertext, ciphertext_size, ciphertext_length);
|
|
217
161
|
}
|
|
218
162
|
|
|
@@ -227,9 +171,8 @@ int EdhocCryptoManager::Decrypt(void* user_context,
|
|
|
227
171
|
uint8_t* plaintext,
|
|
228
172
|
size_t plaintext_size,
|
|
229
173
|
size_t* plaintext_length) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
return cryptoManager->callDecrypt(user_context, key_id, nonce, nonce_length, additional_data, additional_data_length,
|
|
174
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
175
|
+
return context->GetCryptoManager()->callDecrypt(context, key_id, nonce, nonce_length, additional_data, additional_data_length,
|
|
233
176
|
ciphertext, ciphertext_length, plaintext, plaintext_size, plaintext_length);
|
|
234
177
|
}
|
|
235
178
|
|
|
@@ -239,20 +182,17 @@ int EdhocCryptoManager::Hash(void* user_context,
|
|
|
239
182
|
uint8_t* hash,
|
|
240
183
|
size_t hash_size,
|
|
241
184
|
size_t* hash_length) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
return cryptoManager->callHash(user_context, input, input_length, hash, hash_size, hash_length);
|
|
185
|
+
RunningContext* context = static_cast<RunningContext*>(const_cast<void*>(user_context));
|
|
186
|
+
return context->GetCryptoManager()->callHash(context, input, input_length, hash, hash_size, hash_length);
|
|
245
187
|
}
|
|
246
188
|
|
|
247
|
-
int EdhocCryptoManager::callImportKey(
|
|
189
|
+
int EdhocCryptoManager::callImportKey(RunningContext* runningContext,
|
|
248
190
|
enum edhoc_key_type key_type,
|
|
249
191
|
const uint8_t* raw_key,
|
|
250
192
|
size_t raw_key_length,
|
|
251
193
|
void* key_id_ptr) {
|
|
252
|
-
std::promise<int> promise;
|
|
253
|
-
std::future<int> future = promise.get_future();
|
|
254
194
|
|
|
255
|
-
auto successHandler = [&
|
|
195
|
+
auto successHandler = [&key_id_ptr](Napi::Env env, Napi::Value result) {
|
|
256
196
|
Napi::HandleScope scope(env);
|
|
257
197
|
uint8_t* key_id = static_cast<uint8_t*>(key_id_ptr);
|
|
258
198
|
|
|
@@ -262,7 +202,6 @@ int EdhocCryptoManager::callImportKey(const void* user_context,
|
|
|
262
202
|
throw std::runtime_error(kErrorInvalidUint8ArrayLength);
|
|
263
203
|
}
|
|
264
204
|
memcpy(key_id, resultArray.Data(), resultArray.ElementLength());
|
|
265
|
-
promise.set_value(EDHOC_SUCCESS);
|
|
266
205
|
} else if (result.IsNumber()) {
|
|
267
206
|
uint32_t num = result.As<Napi::Number>().Int64Value();
|
|
268
207
|
uint8_t tempBuffer[CONFIG_LIBEDHOC_KEY_ID_LEN];
|
|
@@ -275,71 +214,54 @@ int EdhocCryptoManager::callImportKey(const void* user_context,
|
|
|
275
214
|
|
|
276
215
|
memcpy(key_id, tempBuffer, encodedLength);
|
|
277
216
|
memset(key_id + encodedLength, 0, CONFIG_LIBEDHOC_KEY_ID_LEN - encodedLength);
|
|
278
|
-
promise.set_value(EDHOC_SUCCESS);
|
|
279
217
|
} else {
|
|
280
218
|
throw std::runtime_error(kErrorExpectUint8ArrayOrNumber);
|
|
281
219
|
}
|
|
220
|
+
return EDHOC_SUCCESS;
|
|
282
221
|
};
|
|
283
222
|
|
|
284
|
-
auto
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
Napi::Buffer<uint8_t>::New(env, const_cast<uint8_t*>(raw_key), raw_key_length)};
|
|
291
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
292
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
293
|
-
errorHandler);
|
|
223
|
+
auto argumentsHandler = [this, key_type, raw_key, raw_key_length](Napi::Env env) {
|
|
224
|
+
return std::vector<napi_value> {
|
|
225
|
+
this->edhocRef.Value(),
|
|
226
|
+
Napi::Number::New(env, static_cast<int>(key_type)),
|
|
227
|
+
Napi::Buffer<uint8_t>::New(env, const_cast<uint8_t*>(raw_key), raw_key_length)
|
|
228
|
+
};
|
|
294
229
|
};
|
|
295
230
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
future.wait();
|
|
299
|
-
return future.get();
|
|
231
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "importKey", argumentsHandler, successHandler);
|
|
300
232
|
}
|
|
301
233
|
|
|
302
|
-
int EdhocCryptoManager::callDestroyKey(
|
|
303
|
-
std::promise<int> promise;
|
|
304
|
-
std::future<int> future = promise.get_future();
|
|
305
|
-
|
|
234
|
+
int EdhocCryptoManager::callDestroyKey(RunningContext* runningContext, void* key_id) {
|
|
306
235
|
// Timeout thread to ensure the callback is called
|
|
307
236
|
std::shared_ptr<bool> callbackCompleted = std::make_shared<bool>(false);
|
|
308
|
-
std::thread timeoutThread([callbackCompleted
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
});
|
|
314
|
-
timeoutThread.detach();
|
|
315
|
-
|
|
316
|
-
auto successHandler = [
|
|
237
|
+
// std::thread timeoutThread([callbackCompleted]() {
|
|
238
|
+
// std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
239
|
+
// if (!*callbackCompleted) {
|
|
240
|
+
// promise.set_value(EDHOC_ERROR_GENERIC_ERROR);
|
|
241
|
+
// }
|
|
242
|
+
// });
|
|
243
|
+
// timeoutThread.detach();
|
|
244
|
+
|
|
245
|
+
auto successHandler = [](Napi::Env env, Napi::Value result) {
|
|
317
246
|
Napi::HandleScope scope(env);
|
|
318
247
|
if (!result.IsBoolean()) {
|
|
319
248
|
throw std::runtime_error(kErrorExpectBoolean);
|
|
320
249
|
}
|
|
321
|
-
|
|
250
|
+
return result.As<Napi::Boolean>().Value() ? EDHOC_SUCCESS : EDHOC_ERROR_GENERIC_ERROR;
|
|
322
251
|
};
|
|
323
252
|
|
|
324
|
-
auto
|
|
325
|
-
Napi::Env env, Napi::Function jsCallback) {
|
|
253
|
+
auto argumentsHandler = [this, &key_id, callbackCompleted](Napi::Env env) {
|
|
326
254
|
*callbackCompleted = true;
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
332
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
333
|
-
errorHandler);
|
|
255
|
+
return std::vector<napi_value> {
|
|
256
|
+
this->edhocRef.Value(),
|
|
257
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN)
|
|
258
|
+
};
|
|
334
259
|
};
|
|
335
260
|
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
future.wait();
|
|
339
|
-
return future.get();
|
|
261
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "destroyKey", argumentsHandler, successHandler);
|
|
340
262
|
}
|
|
341
263
|
|
|
342
|
-
int EdhocCryptoManager::callMakeKeyPair(
|
|
264
|
+
int EdhocCryptoManager::callMakeKeyPair(RunningContext* runningContext,
|
|
343
265
|
const void* key_id,
|
|
344
266
|
uint8_t* private_key,
|
|
345
267
|
size_t private_key_size,
|
|
@@ -347,11 +269,8 @@ int EdhocCryptoManager::callMakeKeyPair(const void* user_context,
|
|
|
347
269
|
uint8_t* public_key,
|
|
348
270
|
size_t public_key_size,
|
|
349
271
|
size_t* public_key_length) {
|
|
350
|
-
std::promise<int> promise;
|
|
351
|
-
std::future<int> future = promise.get_future();
|
|
352
272
|
|
|
353
|
-
auto successHandler = [&
|
|
354
|
-
&public_key_length](Napi::Env env, Napi::Value result) {
|
|
273
|
+
auto successHandler = [&private_key, private_key_size, &private_key_length, &public_key, public_key_size, &public_key_length](Napi::Env env, Napi::Value result) {
|
|
355
274
|
Napi::HandleScope scope(env);
|
|
356
275
|
if (!result.IsObject()) {
|
|
357
276
|
throw std::runtime_error(kErrorResultObjectExpected);
|
|
@@ -381,40 +300,30 @@ int EdhocCryptoManager::callMakeKeyPair(const void* user_context,
|
|
|
381
300
|
memcpy(public_key, publicKeyBuffer.Data(), publicKeyBuffer.Length());
|
|
382
301
|
*public_key_length = publicKeyBuffer.Length();
|
|
383
302
|
|
|
384
|
-
|
|
303
|
+
return EDHOC_SUCCESS;
|
|
385
304
|
};
|
|
386
305
|
|
|
387
|
-
auto
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
Napi::Number::New(env, static_cast<size_t>(public_key_size))};
|
|
395
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
396
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
397
|
-
errorHandler);
|
|
306
|
+
auto argumentsHandler = [this, &key_id, private_key_size, public_key_size](Napi::Env env) {
|
|
307
|
+
return std::vector<napi_value> {
|
|
308
|
+
this->edhocRef.Value(),
|
|
309
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
310
|
+
Napi::Number::New(env, static_cast<size_t>(private_key_size)),
|
|
311
|
+
Napi::Number::New(env, static_cast<size_t>(public_key_size))
|
|
312
|
+
};
|
|
398
313
|
};
|
|
399
314
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
future.wait();
|
|
403
|
-
return future.get();
|
|
315
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "makeKeyPair", argumentsHandler, successHandler);
|
|
404
316
|
}
|
|
405
317
|
|
|
406
|
-
int EdhocCryptoManager::callKeyAgreement(
|
|
318
|
+
int EdhocCryptoManager::callKeyAgreement(RunningContext* runningContext,
|
|
407
319
|
const void* key_id,
|
|
408
320
|
const uint8_t* peer_public_key,
|
|
409
321
|
size_t peer_public_key_length,
|
|
410
322
|
uint8_t* shared_secret,
|
|
411
323
|
size_t shared_secret_size,
|
|
412
324
|
size_t* shared_secret_length) {
|
|
413
|
-
std::promise<int> promise;
|
|
414
|
-
std::future<int> future = promise.get_future();
|
|
415
325
|
|
|
416
|
-
auto successHandler = [&
|
|
417
|
-
Napi::Value result) {
|
|
326
|
+
auto successHandler = [&shared_secret, shared_secret_size, &shared_secret_length](Napi::Env env, Napi::Value result) {
|
|
418
327
|
Napi::HandleScope scope(env);
|
|
419
328
|
if (!result.IsBuffer()) {
|
|
420
329
|
throw std::runtime_error(kErrorExpectBuffer);
|
|
@@ -426,42 +335,30 @@ int EdhocCryptoManager::callKeyAgreement(const void* user_context,
|
|
|
426
335
|
memcpy(shared_secret, sharedSecretBuffer.Data(), sharedSecretBuffer.Length());
|
|
427
336
|
*shared_secret_length = sharedSecretBuffer.Length();
|
|
428
337
|
|
|
429
|
-
|
|
338
|
+
return EDHOC_SUCCESS;
|
|
430
339
|
};
|
|
431
340
|
|
|
432
|
-
auto
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
Napi::Buffer<uint8_t>::Copy(env, peer_public_key, peer_public_key_length),
|
|
439
|
-
Napi::Number::New(env, static_cast<size_t>(shared_secret_size)),
|
|
341
|
+
auto argumentsHandler = [this, &key_id, &peer_public_key, peer_public_key_length, shared_secret_size](Napi::Env env) {
|
|
342
|
+
return std::vector<napi_value> {
|
|
343
|
+
this->edhocRef.Value(),
|
|
344
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
345
|
+
Napi::Buffer<uint8_t>::Copy(env, peer_public_key, peer_public_key_length),
|
|
346
|
+
Napi::Number::New(env, static_cast<size_t>(shared_secret_size))
|
|
440
347
|
};
|
|
441
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
442
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
443
|
-
errorHandler);
|
|
444
348
|
};
|
|
445
349
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
future.wait();
|
|
449
|
-
return future.get();
|
|
350
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "keyAgreement", argumentsHandler, successHandler);
|
|
450
351
|
}
|
|
451
352
|
|
|
452
|
-
int EdhocCryptoManager::callSign(
|
|
353
|
+
int EdhocCryptoManager::callSign(RunningContext* runningContext,
|
|
453
354
|
const void* key_id,
|
|
454
355
|
const uint8_t* input,
|
|
455
356
|
size_t input_length,
|
|
456
357
|
uint8_t* signature,
|
|
457
358
|
size_t signature_size,
|
|
458
359
|
size_t* signature_length) {
|
|
459
|
-
std::promise<int> promise;
|
|
460
|
-
std::future<int> future = promise.get_future();
|
|
461
|
-
|
|
462
|
-
const uint8_t* kid = static_cast<const uint8_t*>(key_id);
|
|
463
360
|
|
|
464
|
-
auto successHandler = [&
|
|
361
|
+
auto successHandler = [&signature, signature_size, &signature_length](Napi::Env env, Napi::Value result) {
|
|
465
362
|
Napi::HandleScope scope(env);
|
|
466
363
|
if (!result.IsBuffer()) {
|
|
467
364
|
throw std::runtime_error(kErrorExpectBuffer);
|
|
@@ -473,77 +370,56 @@ int EdhocCryptoManager::callSign(const void* user_context,
|
|
|
473
370
|
memcpy(signature, signatureBuffer.Data(), signatureBuffer.Length());
|
|
474
371
|
*signature_length = signatureBuffer.Length();
|
|
475
372
|
|
|
476
|
-
|
|
373
|
+
return EDHOC_SUCCESS;
|
|
477
374
|
};
|
|
478
375
|
|
|
479
|
-
auto
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
Napi::Number::New(env, static_cast<size_t>(signature_size))};
|
|
487
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
488
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
489
|
-
errorHandler);
|
|
376
|
+
auto argumentsHandler = [this, &key_id, &input, input_length, signature_size](Napi::Env env) {
|
|
377
|
+
return std::vector<napi_value> {
|
|
378
|
+
this->edhocRef.Value(),
|
|
379
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
380
|
+
Napi::Buffer<uint8_t>::Copy(env, input, input_length),
|
|
381
|
+
Napi::Number::New(env, static_cast<size_t>(signature_size))
|
|
382
|
+
};
|
|
490
383
|
};
|
|
491
384
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
future.wait();
|
|
495
|
-
return future.get();
|
|
385
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "sign", argumentsHandler, successHandler);
|
|
496
386
|
}
|
|
497
387
|
|
|
498
|
-
int EdhocCryptoManager::callVerify(
|
|
388
|
+
int EdhocCryptoManager::callVerify(RunningContext* runningContext,
|
|
499
389
|
const void* key_id,
|
|
500
390
|
const uint8_t* input,
|
|
501
391
|
size_t input_length,
|
|
502
392
|
const uint8_t* signature,
|
|
503
393
|
size_t signature_length) {
|
|
504
|
-
std::promise<int> promise;
|
|
505
|
-
std::future<int> future = promise.get_future();
|
|
506
394
|
|
|
507
|
-
auto successHandler = [
|
|
395
|
+
auto successHandler = [](Napi::Env env, Napi::Value result) {
|
|
508
396
|
Napi::HandleScope scope(env);
|
|
509
397
|
if (!result.IsBoolean()) {
|
|
510
398
|
throw std::runtime_error(kErrorExpectBooleanVerify);
|
|
511
399
|
}
|
|
512
|
-
|
|
400
|
+
return result.As<Napi::Boolean>().Value() ? EDHOC_SUCCESS : EDHOC_ERROR_CRYPTO_FAILURE;
|
|
513
401
|
};
|
|
514
402
|
|
|
515
|
-
auto
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
Napi::Buffer<uint8_t>::Copy(env, input, input_length),
|
|
522
|
-
Napi::Buffer<uint8_t>::Copy(env, signature, signature_length),
|
|
403
|
+
auto argumentsHandler = [this, &key_id, &input, input_length, &signature, signature_length](Napi::Env env) {
|
|
404
|
+
return std::vector<napi_value> {
|
|
405
|
+
this->edhocRef.Value(),
|
|
406
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
407
|
+
Napi::Buffer<uint8_t>::Copy(env, input, input_length),
|
|
408
|
+
Napi::Buffer<uint8_t>::Copy(env, signature, signature_length)
|
|
523
409
|
};
|
|
524
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
525
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
526
|
-
errorHandler);
|
|
527
410
|
};
|
|
528
411
|
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
future.wait();
|
|
532
|
-
return future.get();
|
|
412
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "verify", argumentsHandler, successHandler);
|
|
533
413
|
}
|
|
534
414
|
|
|
535
|
-
int EdhocCryptoManager::callExtract(
|
|
415
|
+
int EdhocCryptoManager::callExtract(RunningContext* runningContext,
|
|
536
416
|
const void* key_id,
|
|
537
417
|
const uint8_t* salt,
|
|
538
418
|
size_t salt_len,
|
|
539
419
|
uint8_t* pseudo_random_key,
|
|
540
420
|
size_t pseudo_random_key_size,
|
|
541
421
|
size_t* pseudo_random_key_length) {
|
|
542
|
-
|
|
543
|
-
std::future<int> future = promise.get_future();
|
|
544
|
-
|
|
545
|
-
auto successHandler = [&promise, &pseudo_random_key, pseudo_random_key_size, &pseudo_random_key_length](
|
|
546
|
-
Napi::Env env, Napi::Value result) {
|
|
422
|
+
auto successHandler = [&pseudo_random_key, pseudo_random_key_size, &pseudo_random_key_length](Napi::Env env, Napi::Value result) {
|
|
547
423
|
Napi::HandleScope scope(env);
|
|
548
424
|
if (!result.IsBuffer()) {
|
|
549
425
|
throw std::runtime_error(kErrorExpectBuffer);
|
|
@@ -555,39 +431,28 @@ int EdhocCryptoManager::callExtract(const void* user_context,
|
|
|
555
431
|
memcpy(pseudo_random_key, randomKeyBuffer.Data(), randomKeyBuffer.Length());
|
|
556
432
|
*pseudo_random_key_length = randomKeyBuffer.Length();
|
|
557
433
|
|
|
558
|
-
|
|
434
|
+
return EDHOC_SUCCESS;
|
|
559
435
|
};
|
|
560
436
|
|
|
561
|
-
auto
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
Napi::Number::New(env, static_cast<size_t>(pseudo_random_key_size))};
|
|
569
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
570
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
571
|
-
errorHandler);
|
|
437
|
+
auto argumentsHandler = [this, &key_id, &salt, salt_len, pseudo_random_key_size](Napi::Env env) {
|
|
438
|
+
return std::vector<napi_value> {
|
|
439
|
+
this->edhocRef.Value(),
|
|
440
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
441
|
+
Napi::Buffer<uint8_t>::Copy(env, salt, salt_len),
|
|
442
|
+
Napi::Number::New(env, static_cast<size_t>(pseudo_random_key_size))
|
|
443
|
+
};
|
|
572
444
|
};
|
|
573
445
|
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
future.wait();
|
|
577
|
-
return future.get();
|
|
446
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "extract", argumentsHandler, successHandler);
|
|
578
447
|
}
|
|
579
448
|
|
|
580
|
-
int EdhocCryptoManager::callExpand(
|
|
449
|
+
int EdhocCryptoManager::callExpand(RunningContext* runningContext,
|
|
581
450
|
const void* key_id,
|
|
582
451
|
const uint8_t* info,
|
|
583
452
|
size_t info_length,
|
|
584
453
|
uint8_t* output_keying_material,
|
|
585
454
|
size_t output_keying_material_length) {
|
|
586
|
-
|
|
587
|
-
std::future<int> future = promise.get_future();
|
|
588
|
-
|
|
589
|
-
auto successHandler = [&promise, &output_keying_material, output_keying_material_length](Napi::Env env,
|
|
590
|
-
Napi::Value result) {
|
|
455
|
+
auto successHandler = [&output_keying_material, output_keying_material_length](Napi::Env env, Napi::Value result) {
|
|
591
456
|
Napi::HandleScope scope(env);
|
|
592
457
|
if (!result.IsBuffer()) {
|
|
593
458
|
throw std::runtime_error(kErrorExpectBuffer);
|
|
@@ -598,29 +463,22 @@ int EdhocCryptoManager::callExpand(const void* user_context,
|
|
|
598
463
|
}
|
|
599
464
|
memcpy(output_keying_material, outputBuffer.Data(), outputBuffer.Length());
|
|
600
465
|
|
|
601
|
-
|
|
466
|
+
return EDHOC_SUCCESS;
|
|
602
467
|
};
|
|
603
468
|
|
|
604
|
-
auto
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
Napi::Number::New(env, static_cast<size_t>(output_keying_material_length))};
|
|
612
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
613
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
614
|
-
errorHandler);
|
|
469
|
+
auto argumentsHandler = [this, &key_id, &info, info_length, output_keying_material_length](Napi::Env env) {
|
|
470
|
+
return std::vector<napi_value> {
|
|
471
|
+
this->edhocRef.Value(),
|
|
472
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
473
|
+
Napi::Buffer<uint8_t>::Copy(env, info, info_length),
|
|
474
|
+
Napi::Number::New(env, static_cast<size_t>(output_keying_material_length))
|
|
475
|
+
};
|
|
615
476
|
};
|
|
616
477
|
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
future.wait();
|
|
620
|
-
return future.get();
|
|
478
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "expand", argumentsHandler, successHandler);
|
|
621
479
|
}
|
|
622
480
|
|
|
623
|
-
int EdhocCryptoManager::callEncrypt(
|
|
481
|
+
int EdhocCryptoManager::callEncrypt(RunningContext* runningContext,
|
|
624
482
|
const void* key_id,
|
|
625
483
|
const uint8_t* nonce,
|
|
626
484
|
size_t nonce_length,
|
|
@@ -631,11 +489,7 @@ int EdhocCryptoManager::callEncrypt(const void* user_context,
|
|
|
631
489
|
uint8_t* ciphertext,
|
|
632
490
|
size_t ciphertext_size,
|
|
633
491
|
size_t* ciphertext_length) {
|
|
634
|
-
|
|
635
|
-
std::future<int> future = promise.get_future();
|
|
636
|
-
|
|
637
|
-
auto successHandler = [&promise, &ciphertext, ciphertext_size, &ciphertext_length](Napi::Env env,
|
|
638
|
-
Napi::Value result) {
|
|
492
|
+
auto successHandler = [&ciphertext, ciphertext_size, &ciphertext_length](Napi::Env env, Napi::Value result) {
|
|
639
493
|
Napi::HandleScope scope(env);
|
|
640
494
|
if (!result.IsBuffer()) {
|
|
641
495
|
throw std::runtime_error(kErrorExpectBuffer);
|
|
@@ -647,32 +501,25 @@ int EdhocCryptoManager::callEncrypt(const void* user_context,
|
|
|
647
501
|
memcpy(ciphertext, ciphertextBuffer.Data(), ciphertextBuffer.Length());
|
|
648
502
|
*ciphertext_length = ciphertextBuffer.Length();
|
|
649
503
|
|
|
650
|
-
|
|
504
|
+
return EDHOC_SUCCESS;
|
|
651
505
|
};
|
|
652
506
|
|
|
653
|
-
auto
|
|
654
|
-
additional_data_length, &plaintext, plaintext_length, ciphertext_size
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
Napi::Number::New(env, static_cast<size_t>(ciphertext_size))};
|
|
664
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
665
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, args, successHandler,
|
|
666
|
-
errorHandler);
|
|
507
|
+
auto argumentsHandler = [this, &key_id, &nonce, nonce_length, &additional_data,
|
|
508
|
+
additional_data_length, &plaintext, plaintext_length, ciphertext_size](Napi::Env env) {
|
|
509
|
+
return std::vector<napi_value> {
|
|
510
|
+
this->edhocRef.Value(),
|
|
511
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
512
|
+
Napi::Buffer<uint8_t>::Copy(env, nonce, nonce_length),
|
|
513
|
+
Napi::Buffer<uint8_t>::Copy(env, additional_data, additional_data_length),
|
|
514
|
+
Napi::Buffer<uint8_t>::Copy(env, plaintext, plaintext_length),
|
|
515
|
+
Napi::Number::New(env, static_cast<size_t>(ciphertext_size))
|
|
516
|
+
};
|
|
667
517
|
};
|
|
668
518
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
future.wait();
|
|
672
|
-
return future.get();
|
|
519
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "encrypt", argumentsHandler, successHandler);
|
|
673
520
|
}
|
|
674
521
|
|
|
675
|
-
int EdhocCryptoManager::callDecrypt(
|
|
522
|
+
int EdhocCryptoManager::callDecrypt(RunningContext* runningContext,
|
|
676
523
|
const void* key_id,
|
|
677
524
|
const uint8_t* nonce,
|
|
678
525
|
size_t nonce_length,
|
|
@@ -683,10 +530,7 @@ int EdhocCryptoManager::callDecrypt(const void* user_context,
|
|
|
683
530
|
uint8_t* plaintext,
|
|
684
531
|
size_t plaintext_size,
|
|
685
532
|
size_t* plaintext_length) {
|
|
686
|
-
|
|
687
|
-
std::future<int> future = promise.get_future();
|
|
688
|
-
|
|
689
|
-
auto successHandler = [&promise, &plaintext, plaintext_size, plaintext_length](Napi::Env env, Napi::Value result) {
|
|
533
|
+
auto successHandler = [&plaintext, plaintext_size, &plaintext_length](Napi::Env env, Napi::Value result) {
|
|
690
534
|
Napi::HandleScope scope(env);
|
|
691
535
|
if (!result.IsBuffer()) {
|
|
692
536
|
throw std::runtime_error(kErrorExpectBuffer);
|
|
@@ -698,41 +542,31 @@ int EdhocCryptoManager::callDecrypt(const void* user_context,
|
|
|
698
542
|
memcpy(plaintext, plaintextBuffer.Data(), plaintextBuffer.Length());
|
|
699
543
|
*plaintext_length = plaintextBuffer.Length();
|
|
700
544
|
|
|
701
|
-
|
|
545
|
+
return EDHOC_SUCCESS;
|
|
702
546
|
};
|
|
703
547
|
|
|
704
|
-
auto
|
|
705
|
-
additional_data_length, &ciphertext, &ciphertext_length, plaintext_size
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
Napi::Number::New(env, static_cast<size_t>(plaintext_size))};
|
|
715
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
716
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, arguments, successHandler,
|
|
717
|
-
errorHandler);
|
|
548
|
+
auto argumentsHandler = [this, &key_id, &nonce, nonce_length, &additional_data,
|
|
549
|
+
additional_data_length, &ciphertext, &ciphertext_length, plaintext_size](Napi::Env env) {
|
|
550
|
+
return std::vector<napi_value> {
|
|
551
|
+
this->edhocRef.Value(),
|
|
552
|
+
Napi::Buffer<uint8_t>::Copy(env, static_cast<const uint8_t*>(key_id), CONFIG_LIBEDHOC_KEY_ID_LEN),
|
|
553
|
+
Napi::Buffer<uint8_t>::Copy(env, nonce, nonce_length),
|
|
554
|
+
Napi::Buffer<uint8_t>::Copy(env, additional_data, additional_data_length),
|
|
555
|
+
Napi::Buffer<uint8_t>::Copy(env, ciphertext, ciphertext_length),
|
|
556
|
+
Napi::Number::New(env, static_cast<size_t>(plaintext_size))
|
|
557
|
+
};
|
|
718
558
|
};
|
|
719
559
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
future.wait();
|
|
723
|
-
return future.get();
|
|
560
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "decrypt", argumentsHandler, successHandler);
|
|
724
561
|
}
|
|
725
562
|
|
|
726
|
-
int EdhocCryptoManager::callHash(
|
|
563
|
+
int EdhocCryptoManager::callHash(RunningContext* runningContext,
|
|
727
564
|
const uint8_t* input,
|
|
728
565
|
size_t input_length,
|
|
729
566
|
uint8_t* hash,
|
|
730
567
|
size_t hash_size,
|
|
731
568
|
size_t* hash_length) {
|
|
732
|
-
|
|
733
|
-
std::future<int> future = promise.get_future();
|
|
734
|
-
|
|
735
|
-
auto successHandler = [&promise, &hash, hash_size, &hash_length](Napi::Env env, Napi::Value result) {
|
|
569
|
+
auto successHandler = [&hash, hash_size, &hash_length](Napi::Env env, Napi::Value result) {
|
|
736
570
|
Napi::HandleScope scope(env);
|
|
737
571
|
if (!result.IsBuffer()) {
|
|
738
572
|
throw std::runtime_error(kErrorExpectBuffer);
|
|
@@ -744,24 +578,16 @@ int EdhocCryptoManager::callHash(const void* user_context,
|
|
|
744
578
|
memcpy(hash, hashBuffer.Data(), hashBuffer.Length());
|
|
745
579
|
*hash_length = hashBuffer.Length();
|
|
746
580
|
|
|
747
|
-
|
|
581
|
+
return EDHOC_SUCCESS;
|
|
748
582
|
};
|
|
749
583
|
|
|
750
|
-
auto
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
Napi::Buffer<uint8_t>::Copy(env, input, input_length),
|
|
756
|
-
Napi::Number::New(env, static_cast<size_t>(hash_size)),
|
|
584
|
+
auto argumentsHandler = [this, &input, input_length, hash_size](Napi::Env env) {
|
|
585
|
+
return std::vector<napi_value> {
|
|
586
|
+
this->edhocRef.Value(),
|
|
587
|
+
Napi::Buffer<uint8_t>::Copy(env, input, input_length),
|
|
588
|
+
Napi::Number::New(env, static_cast<size_t>(hash_size))
|
|
757
589
|
};
|
|
758
|
-
auto errorHandler = Utils::CreatePromiseErrorHandler<int>(promise, EDHOC_ERROR_GENERIC_ERROR);
|
|
759
|
-
Utils::InvokeJSFunctionWithPromiseHandling(env, cryptoManagerRef.Value(), jsCallback, arguments, successHandler,
|
|
760
|
-
errorHandler);
|
|
761
590
|
};
|
|
762
591
|
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
future.wait();
|
|
766
|
-
return future.get();
|
|
592
|
+
return runningContext->ThreadSafeBlockingCall(cryptoManagerRef, "hash", argumentsHandler, successHandler);
|
|
767
593
|
}
|