asherah 2.0.0 → 3.0.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/binding.gyp +10 -11
- package/package.json +14 -11
- package/scripts/download-libraries.sh +261 -46
- package/src/asherah.cc +530 -442
- package/src/asherah.d.ts +7 -0
- package/src/asherah_async_worker.h +65 -0
- package/src/cobhan_buffer.h +237 -0
- package/src/cobhan_buffer_napi.h +196 -0
- package/src/logging.cc +26 -180
- package/src/logging.h +44 -43
- package/src/napi_utils.h +160 -0
- package/src/scoped_allocate.h +53 -0
- package/src/asherah.h +0 -14
- package/src/cobhan.cc +0 -2
- package/src/cobhan.h +0 -133
- package/src/cobhan_napi_interop.h +0 -244
package/src/asherah.cc
CHANGED
|
@@ -1,545 +1,640 @@
|
|
|
1
|
-
|
|
2
|
-
#
|
|
3
|
-
#include "
|
|
1
|
+
|
|
2
|
+
#define USE_SCOPE_ALLOCATE_BUFFER 1
|
|
3
|
+
#include "asherah_async_worker.h"
|
|
4
|
+
#include "cobhan_buffer_napi.h"
|
|
4
5
|
#include "hints.h"
|
|
5
|
-
#include "
|
|
6
|
-
#include
|
|
7
|
-
#include
|
|
6
|
+
#include "libasherah.h"
|
|
7
|
+
#include "logging_napi.h"
|
|
8
|
+
#include "napi_utils.h"
|
|
9
|
+
#ifdef USE_SCOPED_ALLOCATE_BUFFER
|
|
10
|
+
#include "scoped_allocate.h"
|
|
11
|
+
#endif
|
|
12
|
+
#include <atomic>
|
|
8
13
|
#include <napi.h>
|
|
9
14
|
|
|
15
|
+
#ifndef NAPI_CPP_EXCEPTIONS
|
|
16
|
+
#error Support for C++ exceptions is required
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
static volatile std::atomic<int32_t> setup_state{0};
|
|
20
|
+
|
|
10
21
|
class Asherah : public Napi::Addon<Asherah> {
|
|
11
22
|
public:
|
|
12
|
-
Asherah(Napi::Env env, Napi::Object exports) {
|
|
13
|
-
DefineAddon(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
Asherah(Napi::Env env, Napi::Object exports) : logger(env, "asherah-node") {
|
|
24
|
+
DefineAddon(
|
|
25
|
+
exports,
|
|
26
|
+
{
|
|
27
|
+
InstanceMethod("setup", &Asherah::SetupAsherahSync),
|
|
28
|
+
InstanceMethod("setup_async", &Asherah::SetupAsherahAsync),
|
|
29
|
+
InstanceMethod("encrypt", &Asherah::EncryptSync),
|
|
30
|
+
InstanceMethod("encrypt_async", &Asherah::EncryptAsync),
|
|
31
|
+
InstanceMethod("encrypt_string", &Asherah::EncryptSync),
|
|
32
|
+
InstanceMethod("encrypt_string_async", &Asherah::EncryptAsync),
|
|
33
|
+
InstanceMethod("decrypt", &Asherah::DecryptSync),
|
|
34
|
+
InstanceMethod("decrypt_async", &Asherah::DecryptAsync),
|
|
35
|
+
InstanceMethod("decrypt_string", &Asherah::DecryptStringSync),
|
|
36
|
+
InstanceMethod("decrypt_string_async",
|
|
37
|
+
&Asherah::DecryptStringAsync),
|
|
38
|
+
InstanceMethod("shutdown", &Asherah::ShutdownAsherahSync),
|
|
39
|
+
InstanceMethod("shutdown_async", &Asherah::ShutdownAsherahAsync),
|
|
40
|
+
InstanceMethod("set_max_stack_alloc_item_size",
|
|
41
|
+
&Asherah::SetMaxStackAllocItemSize),
|
|
42
|
+
InstanceMethod("set_safety_padding_overhead",
|
|
43
|
+
&Asherah::SetSafetyPaddingOverhead),
|
|
44
|
+
InstanceMethod("get_setup_status", &Asherah::GetSetupStatus),
|
|
45
|
+
InstanceMethod("set_log_hook", &Asherah::SetLogHook),
|
|
46
|
+
});
|
|
25
47
|
}
|
|
26
48
|
|
|
27
49
|
private:
|
|
28
|
-
size_t est_intermediate_key_overhead;
|
|
50
|
+
size_t est_intermediate_key_overhead = 0;
|
|
29
51
|
size_t maximum_stack_alloc_size = 2048;
|
|
30
52
|
|
|
31
53
|
int32_t verbose_flag = 0;
|
|
32
|
-
int32_t setup_state = 0;
|
|
33
|
-
std::mutex asherah_lock;
|
|
34
54
|
Napi::FunctionReference log_hook;
|
|
35
|
-
|
|
55
|
+
LoggerNapi logger;
|
|
36
56
|
|
|
37
|
-
|
|
38
|
-
std::lock_guard<std::mutex> lock(asherah_lock);
|
|
57
|
+
#pragma region Published Node Addon Methods
|
|
39
58
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
59
|
+
void SetupAsherahSync(const Napi::CallbackInfo &info) {
|
|
60
|
+
Napi::Env env = info.Env();
|
|
61
|
+
Napi::HandleScope scope(env);
|
|
62
|
+
try {
|
|
63
|
+
Napi::String config_string;
|
|
64
|
+
size_t product_id_length;
|
|
65
|
+
size_t service_name_length;
|
|
66
|
+
|
|
67
|
+
BeginSetupAsherah(env, __func__, info, config_string, product_id_length,
|
|
68
|
+
service_name_length);
|
|
69
|
+
|
|
70
|
+
#ifdef USE_SCOPED_ALLOCATE_BUFFER
|
|
71
|
+
char *config_cbuffer;
|
|
72
|
+
size_t config_cbuffer_size =
|
|
73
|
+
CobhanBufferNapi::StringToAllocationSize(env, config_string);
|
|
74
|
+
SCOPED_ALLOCATE_BUFFER(logger, config_cbuffer, config_cbuffer_size,
|
|
75
|
+
maximum_stack_alloc_size, __func__);
|
|
76
|
+
|
|
77
|
+
CobhanBufferNapi config(env, config_string, config_cbuffer,
|
|
78
|
+
config_cbuffer_size);
|
|
79
|
+
#else
|
|
80
|
+
CobhanBufferNapi config(env, config_string);
|
|
81
|
+
#endif
|
|
82
|
+
|
|
83
|
+
// extern GoInt32 SetupJson(void* configJson);
|
|
84
|
+
GoInt32 result = SetupJson(config);
|
|
85
|
+
EndSetupAsherah(result, product_id_length, service_name_length);
|
|
86
|
+
} catch (const std::exception &e) {
|
|
87
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
50
88
|
}
|
|
51
|
-
|
|
52
|
-
logger.set_log_hook(info[0].As<Napi::Function>());
|
|
53
89
|
}
|
|
54
90
|
|
|
55
|
-
|
|
56
|
-
|
|
91
|
+
Napi::Value SetupAsherahAsync(const Napi::CallbackInfo &info) {
|
|
92
|
+
Napi::Env env = info.Env();
|
|
93
|
+
Napi::HandleScope scope(env);
|
|
94
|
+
try {
|
|
95
|
+
Napi::String config_string;
|
|
96
|
+
size_t product_id_length;
|
|
97
|
+
size_t service_name_length;
|
|
57
98
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
99
|
+
BeginSetupAsherah(env, __func__, info, config_string, product_id_length,
|
|
100
|
+
service_name_length);
|
|
61
101
|
|
|
62
|
-
|
|
63
|
-
logger.log_error_and_throw(__func__, "setup called twice");
|
|
64
|
-
}
|
|
102
|
+
CobhanBufferNapi config(env, config_string);
|
|
65
103
|
|
|
66
|
-
|
|
67
|
-
|
|
104
|
+
auto worker = new SetupAsherahWorker(env, this, config, product_id_length,
|
|
105
|
+
service_name_length);
|
|
106
|
+
worker->Queue();
|
|
107
|
+
return worker->Promise();
|
|
108
|
+
} catch (const std::exception &e) {
|
|
109
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
68
110
|
}
|
|
111
|
+
}
|
|
69
112
|
|
|
70
|
-
|
|
71
|
-
Napi::Object config_json;
|
|
113
|
+
void ShutdownAsherahSync(const Napi::CallbackInfo &info) {
|
|
72
114
|
Napi::Env env = info.Env();
|
|
73
|
-
Napi::
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
Napi::Function parse = json.Get("parse").As<Napi::Function>();
|
|
81
|
-
config_json = parse.Call(json, {config}).As<Napi::Object>();
|
|
82
|
-
} else {
|
|
83
|
-
logger.log_error_and_throw(__func__, "Wrong argument type");
|
|
115
|
+
Napi::HandleScope scope(env);
|
|
116
|
+
try {
|
|
117
|
+
BeginShutdownAsherah(env, __func__, info);
|
|
118
|
+
Shutdown();
|
|
119
|
+
EndShutdownAsherah();
|
|
120
|
+
} catch (const std::exception &e) {
|
|
121
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
84
122
|
}
|
|
123
|
+
}
|
|
85
124
|
|
|
86
|
-
|
|
87
|
-
Napi::
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
logger.set_verbose_flag(verbose_flag);
|
|
97
|
-
logger.debug_log(__func__,
|
|
98
|
-
"verbose_flag: " + std::to_string(verbose_flag));
|
|
99
|
-
} else {
|
|
100
|
-
verbose_flag = 0;
|
|
101
|
-
logger.set_verbose_flag(verbose_flag);
|
|
102
|
-
logger.debug_log(__func__, "verbose_flag: defaulting to false");
|
|
125
|
+
Napi::Value ShutdownAsherahAsync(const Napi::CallbackInfo &info) {
|
|
126
|
+
Napi::Env env = info.Env();
|
|
127
|
+
Napi::HandleScope scope(env);
|
|
128
|
+
try {
|
|
129
|
+
BeginShutdownAsherah(env, __func__, info);
|
|
130
|
+
auto worker = new ShutdownAsherahWorker(env, this);
|
|
131
|
+
worker->Queue();
|
|
132
|
+
return worker->Promise();
|
|
133
|
+
} catch (const std::exception &e) {
|
|
134
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
103
135
|
}
|
|
136
|
+
}
|
|
104
137
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
138
|
+
// This is the exported sync Encrypt function
|
|
139
|
+
Napi::Value EncryptSync(const Napi::CallbackInfo &info) {
|
|
140
|
+
Napi::Env env = info.Env();
|
|
141
|
+
Napi::HandleScope scope(env);
|
|
142
|
+
Napi::String output_string;
|
|
143
|
+
try {
|
|
144
|
+
Napi::String partition_id_string;
|
|
145
|
+
Napi::Value input_value;
|
|
146
|
+
|
|
147
|
+
BeginEncryptToJson(env, __func__, info, partition_id_string, input_value);
|
|
148
|
+
|
|
149
|
+
#ifdef USE_SCOPED_ALLOCATE_BUFFER
|
|
150
|
+
char *partition_id_cbuffer;
|
|
151
|
+
size_t partition_id_cbuffer_size =
|
|
152
|
+
CobhanBufferNapi::StringToAllocationSize(env, partition_id_string);
|
|
153
|
+
SCOPED_ALLOCATE_BUFFER(logger, partition_id_cbuffer,
|
|
154
|
+
partition_id_cbuffer_size,
|
|
155
|
+
maximum_stack_alloc_size, __func__);
|
|
156
|
+
|
|
157
|
+
char *input_cbuffer;
|
|
158
|
+
size_t input_cbuffer_size =
|
|
159
|
+
CobhanBufferNapi::ValueToAllocationSize(env, input_value);
|
|
160
|
+
SCOPED_ALLOCATE_BUFFER(logger, input_cbuffer, input_cbuffer_size,
|
|
161
|
+
maximum_stack_alloc_size, __func__);
|
|
162
|
+
|
|
163
|
+
CobhanBufferNapi partition_id(env, partition_id_string,
|
|
164
|
+
partition_id_cbuffer,
|
|
165
|
+
partition_id_cbuffer_size);
|
|
166
|
+
CobhanBufferNapi input(env, input_value, input_cbuffer,
|
|
167
|
+
input_cbuffer_size);
|
|
168
|
+
#else
|
|
169
|
+
CobhanBufferNapi partition_id(env, partition_id_string);
|
|
170
|
+
CobhanBufferNapi input(env, input_value);
|
|
171
|
+
#endif
|
|
172
|
+
|
|
173
|
+
size_t partition_id_data_len_bytes = partition_id.get_data_len_bytes();
|
|
174
|
+
size_t input_data_len_bytes = input.get_data_len_bytes();
|
|
175
|
+
size_t asherah_output_size_bytes = EstimateAsherahOutputSize(
|
|
176
|
+
input_data_len_bytes, partition_id_data_len_bytes);
|
|
177
|
+
|
|
178
|
+
#ifdef USE_SCOPED_ALLOCATE_BUFFER
|
|
179
|
+
char *output_cobhan_buffer;
|
|
180
|
+
size_t output_size_bytes =
|
|
181
|
+
CobhanBuffer::DataSizeToAllocationSize(asherah_output_size_bytes);
|
|
182
|
+
SCOPED_ALLOCATE_BUFFER(logger, output_cobhan_buffer, output_size_bytes,
|
|
183
|
+
maximum_stack_alloc_size, __func__);
|
|
184
|
+
CobhanBufferNapi output(output_cobhan_buffer, output_size_bytes);
|
|
185
|
+
#else
|
|
186
|
+
CobhanBufferNapi output(asherah_output_size_bytes);
|
|
187
|
+
#endif
|
|
188
|
+
|
|
189
|
+
GoInt32 result = EncryptToJson(partition_id, input, output);
|
|
190
|
+
|
|
191
|
+
EndEncryptToJson(env, output, result, output_string);
|
|
192
|
+
|
|
193
|
+
} catch (const std::exception &e) {
|
|
194
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return output_string;
|
|
198
|
+
}
|
|
110
199
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
200
|
+
// This is the exported async Encrypt function
|
|
201
|
+
Napi::Value EncryptAsync(const Napi::CallbackInfo &info) {
|
|
202
|
+
Napi::Env env = info.Env();
|
|
203
|
+
Napi::HandleScope scope(env);
|
|
204
|
+
try {
|
|
205
|
+
Napi::String partition_id_string;
|
|
206
|
+
Napi::Value input_value;
|
|
207
|
+
BeginEncryptToJson(env, __func__, info, partition_id_string, input_value);
|
|
116
208
|
|
|
117
|
-
|
|
118
|
-
|
|
209
|
+
CobhanBufferNapi partition_id(env, partition_id_string);
|
|
210
|
+
CobhanBufferNapi input(env, input_value);
|
|
119
211
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
212
|
+
size_t partition_id_data_len_bytes = partition_id.get_data_len_bytes();
|
|
213
|
+
size_t input_data_len_bytes = input.get_data_len_bytes();
|
|
214
|
+
size_t asherah_output_size_bytes = EstimateAsherahOutputSize(
|
|
215
|
+
input_data_len_bytes, partition_id_data_len_bytes);
|
|
123
216
|
|
|
124
|
-
|
|
125
|
-
logger.log_error_and_throw(
|
|
126
|
-
__func__, "Failed post-call canary check for config_cobhan_buffer");
|
|
127
|
-
}
|
|
217
|
+
CobhanBufferNapi output(asherah_output_size_bytes);
|
|
128
218
|
|
|
129
|
-
|
|
130
|
-
|
|
219
|
+
auto worker =
|
|
220
|
+
new EncryptAsherahWorker(env, this, partition_id, input, output);
|
|
221
|
+
worker->Queue();
|
|
222
|
+
return worker->Promise();
|
|
223
|
+
} catch (const std::exception &e) {
|
|
224
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
131
225
|
}
|
|
132
|
-
setup_state = 1;
|
|
133
226
|
}
|
|
134
227
|
|
|
135
|
-
Napi::Value
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
228
|
+
Napi::Value DecryptSync(const Napi::CallbackInfo &info) {
|
|
229
|
+
Napi::Env env = info.Env();
|
|
230
|
+
Napi::HandleScope scope(env);
|
|
231
|
+
Napi::Object output_value;
|
|
232
|
+
try {
|
|
233
|
+
Napi::String partition_id_string;
|
|
234
|
+
Napi::Value input_value;
|
|
235
|
+
|
|
236
|
+
BeginDecryptFromJson(env, __func__, info, partition_id_string,
|
|
237
|
+
input_value);
|
|
238
|
+
|
|
239
|
+
#ifdef USE_SCOPED_ALLOCATE_BUFFER
|
|
240
|
+
char *partition_id_cbuffer;
|
|
241
|
+
size_t partition_id_cbuffer_size =
|
|
242
|
+
CobhanBufferNapi::StringToAllocationSize(env, partition_id_string);
|
|
243
|
+
SCOPED_ALLOCATE_BUFFER(logger, partition_id_cbuffer,
|
|
244
|
+
partition_id_cbuffer_size,
|
|
245
|
+
maximum_stack_alloc_size, __func__);
|
|
246
|
+
|
|
247
|
+
char *input_cbuffer;
|
|
248
|
+
size_t input_cbuffer_size =
|
|
249
|
+
CobhanBufferNapi::ValueToAllocationSize(env, input_value);
|
|
250
|
+
SCOPED_ALLOCATE_BUFFER(logger, input_cbuffer, input_cbuffer_size,
|
|
251
|
+
maximum_stack_alloc_size, __func__);
|
|
252
|
+
|
|
253
|
+
CobhanBufferNapi partition_id(env, partition_id_string,
|
|
254
|
+
partition_id_cbuffer,
|
|
255
|
+
partition_id_cbuffer_size);
|
|
256
|
+
CobhanBufferNapi input(env, input_value, input_cbuffer,
|
|
257
|
+
input_cbuffer_size);
|
|
258
|
+
|
|
259
|
+
char *output_cobhan_buffer;
|
|
260
|
+
size_t output_size_bytes =
|
|
261
|
+
CobhanBuffer::DataSizeToAllocationSize(input.get_data_len_bytes());
|
|
262
|
+
SCOPED_ALLOCATE_BUFFER(logger, output_cobhan_buffer, output_size_bytes,
|
|
263
|
+
maximum_stack_alloc_size, __func__);
|
|
264
|
+
CobhanBufferNapi output(output_cobhan_buffer, output_size_bytes);
|
|
265
|
+
#else
|
|
266
|
+
CobhanBufferNapi partition_id(env, partition_id_string);
|
|
267
|
+
CobhanBufferNapi input(env, input_value);
|
|
268
|
+
CobhanBufferNapi output(input.get_data_len_bytes());
|
|
269
|
+
#endif
|
|
270
|
+
|
|
271
|
+
// extern GoInt32 DecryptFromJson(void* partitionIdPtr, void* jsonPtr,
|
|
272
|
+
// void* dataPtr);
|
|
273
|
+
GoInt32 result = DecryptFromJson(partition_id, input, output);
|
|
274
|
+
|
|
275
|
+
CheckResult(result);
|
|
276
|
+
|
|
277
|
+
output_value = output.ToBuffer(env); // NOLINT(*-slicing)
|
|
278
|
+
} catch (const std::exception &e) {
|
|
279
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
280
|
+
}
|
|
281
|
+
return output_value;
|
|
282
|
+
}
|
|
149
283
|
|
|
150
|
-
|
|
151
|
-
|
|
284
|
+
Napi::Value DecryptAsync(const Napi::CallbackInfo &info) {
|
|
285
|
+
Napi::Env env = info.Env();
|
|
286
|
+
Napi::HandleScope scope(env);
|
|
287
|
+
try {
|
|
288
|
+
Napi::String partition_id_string;
|
|
289
|
+
Napi::Value input_value;
|
|
290
|
+
BeginDecryptFromJson(env, __func__, info, partition_id_string,
|
|
291
|
+
input_value);
|
|
292
|
+
|
|
293
|
+
CobhanBufferNapi partition_id(env, partition_id_string);
|
|
294
|
+
CobhanBufferNapi input(env, input_value);
|
|
295
|
+
|
|
296
|
+
CobhanBufferNapi output(input.get_data_len_bytes());
|
|
297
|
+
auto worker = new DecryptFromJsonWorker<Napi::Buffer<unsigned char>>(
|
|
298
|
+
env, this, partition_id, input, output);
|
|
299
|
+
worker->Queue();
|
|
300
|
+
return worker->Promise();
|
|
301
|
+
} catch (const std::exception &e) {
|
|
302
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
152
303
|
}
|
|
304
|
+
}
|
|
153
305
|
|
|
306
|
+
Napi::Value DecryptStringSync(const Napi::CallbackInfo &info) {
|
|
154
307
|
Napi::Env env = info.Env();
|
|
308
|
+
Napi::HandleScope scope(env);
|
|
309
|
+
Napi::String output_string;
|
|
310
|
+
try {
|
|
311
|
+
NapiUtils::RequireParameterCount(info, 2);
|
|
312
|
+
|
|
313
|
+
if (unlikely(!info[0].IsString() || !info[1].IsString())) {
|
|
314
|
+
logger.log_error_and_throw(__func__, "Wrong argument types");
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
Napi::String partition_id_string;
|
|
318
|
+
Napi::Value input_value;
|
|
319
|
+
BeginDecryptFromJson(env, __func__, info, partition_id_string,
|
|
320
|
+
input_value);
|
|
321
|
+
|
|
322
|
+
#ifdef USE_SCOPED_ALLOCATE_BUFFER
|
|
323
|
+
char *partition_id_cbuffer;
|
|
324
|
+
size_t partition_id_cbuffer_size =
|
|
325
|
+
CobhanBufferNapi::StringToAllocationSize(env, partition_id_string);
|
|
326
|
+
SCOPED_ALLOCATE_BUFFER(logger, partition_id_cbuffer,
|
|
327
|
+
partition_id_cbuffer_size,
|
|
328
|
+
maximum_stack_alloc_size, __func__);
|
|
329
|
+
|
|
330
|
+
char *input_cbuffer;
|
|
331
|
+
size_t input_cbuffer_size =
|
|
332
|
+
CobhanBufferNapi::ValueToAllocationSize(env, input_value);
|
|
333
|
+
SCOPED_ALLOCATE_BUFFER(logger, input_cbuffer, input_cbuffer_size,
|
|
334
|
+
maximum_stack_alloc_size, __func__);
|
|
335
|
+
|
|
336
|
+
CobhanBufferNapi partition_id(env, partition_id_string,
|
|
337
|
+
partition_id_cbuffer,
|
|
338
|
+
partition_id_cbuffer_size);
|
|
339
|
+
CobhanBufferNapi input(env, input_value, input_cbuffer,
|
|
340
|
+
input_cbuffer_size);
|
|
341
|
+
|
|
342
|
+
CobhanBufferNapi output(input.get_data_len_bytes());
|
|
343
|
+
#else
|
|
344
|
+
CobhanBufferNapi partition_id(env, partition_id_string);
|
|
345
|
+
CobhanBufferNapi input(env, input_value);
|
|
346
|
+
CobhanBufferNapi output(input.get_data_len_bytes());
|
|
347
|
+
#endif
|
|
348
|
+
|
|
349
|
+
GoInt32 result = DecryptFromJson(partition_id, input, output);
|
|
350
|
+
|
|
351
|
+
EndDecryptFromJson(env, output, result, output_string);
|
|
352
|
+
} catch (const std::exception &e) {
|
|
353
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
354
|
+
}
|
|
355
|
+
return output_string;
|
|
356
|
+
}
|
|
155
357
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
partition_id_copied_bytes, maximum_stack_alloc_size, __func__);
|
|
162
|
-
|
|
163
|
-
Napi::Buffer<unsigned char> input_napi_buffer =
|
|
164
|
-
info[1].As<Napi::Buffer<unsigned char>>();
|
|
165
|
-
char *input_cobhan_buffer;
|
|
166
|
-
size_t input_copied_bytes;
|
|
167
|
-
NAPI_BUFFER_TO_CBUFFER(env, logger, input_napi_buffer, input_cobhan_buffer,
|
|
168
|
-
input_copied_bytes, maximum_stack_alloc_size,
|
|
169
|
-
__func__);
|
|
170
|
-
|
|
171
|
-
Napi::String output = EncryptCobhanBufferToJson(
|
|
172
|
-
env, partition_id_copied_bytes, input_copied_bytes,
|
|
173
|
-
partition_id_cobhan_buffer, input_cobhan_buffer);
|
|
174
|
-
|
|
175
|
-
if (unlikely(verbose_flag)) {
|
|
176
|
-
logger.debug_log(__func__, "finished");
|
|
177
|
-
}
|
|
358
|
+
Napi::Value DecryptStringAsync(const Napi::CallbackInfo &info) {
|
|
359
|
+
Napi::Env env = info.Env();
|
|
360
|
+
Napi::HandleScope scope(env);
|
|
361
|
+
try {
|
|
362
|
+
NapiUtils::RequireParameterCount(info, 2);
|
|
178
363
|
|
|
179
|
-
|
|
180
|
-
|
|
364
|
+
if (unlikely(!info[0].IsString() || !info[1].IsString())) {
|
|
365
|
+
logger.log_error_and_throw(__func__, "Wrong argument types");
|
|
366
|
+
}
|
|
181
367
|
|
|
182
|
-
|
|
183
|
-
|
|
368
|
+
Napi::String partition_id_string;
|
|
369
|
+
Napi::Value input_value;
|
|
370
|
+
BeginDecryptFromJson(env, __func__, info, partition_id_string,
|
|
371
|
+
input_value);
|
|
184
372
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
373
|
+
CobhanBufferNapi partition_id(env, partition_id_string);
|
|
374
|
+
CobhanBufferNapi input(env, input_value);
|
|
188
375
|
|
|
189
|
-
|
|
190
|
-
logger.log_error_and_throw(__func__, "setup() not called");
|
|
191
|
-
}
|
|
376
|
+
CobhanBufferNapi output(input.get_data_len_bytes());
|
|
192
377
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
378
|
+
auto worker = new DecryptFromJsonWorker<Napi::String>(
|
|
379
|
+
env, this, partition_id, input, output);
|
|
380
|
+
worker->Queue();
|
|
196
381
|
|
|
197
|
-
|
|
198
|
-
|
|
382
|
+
return worker->Promise();
|
|
383
|
+
} catch (const std::exception &e) {
|
|
384
|
+
logger.log_error_and_throw(__func__, e.what());
|
|
199
385
|
}
|
|
386
|
+
}
|
|
200
387
|
|
|
201
|
-
|
|
388
|
+
void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) {
|
|
389
|
+
NapiUtils::RequireParameterCount(info, 1);
|
|
202
390
|
|
|
203
|
-
Napi::
|
|
204
|
-
|
|
205
|
-
size_t partition_id_copied_bytes;
|
|
206
|
-
NAPI_STRING_TO_CBUFFER(
|
|
207
|
-
env, logger, partition_id, partition_id_cobhan_buffer,
|
|
208
|
-
partition_id_copied_bytes, maximum_stack_alloc_size, __func__);
|
|
209
|
-
|
|
210
|
-
Napi::String input = info[1].As<Napi::String>();
|
|
211
|
-
char *input_cobhan_buffer;
|
|
212
|
-
size_t input_copied_bytes;
|
|
213
|
-
NAPI_STRING_TO_CBUFFER(env, logger, input, input_cobhan_buffer,
|
|
214
|
-
input_copied_bytes, maximum_stack_alloc_size,
|
|
215
|
-
__func__);
|
|
216
|
-
|
|
217
|
-
Napi::String output = EncryptCobhanBufferToJson(
|
|
218
|
-
env, partition_id_copied_bytes, input_copied_bytes,
|
|
219
|
-
partition_id_cobhan_buffer, input_cobhan_buffer);
|
|
220
|
-
|
|
221
|
-
if (unlikely(verbose_flag)) {
|
|
222
|
-
logger.debug_log(__func__, "finished");
|
|
223
|
-
}
|
|
391
|
+
Napi::Number item_size = info[0].ToNumber();
|
|
392
|
+
auto new_size = (size_t)item_size.Int32Value();
|
|
224
393
|
|
|
225
|
-
|
|
394
|
+
maximum_stack_alloc_size = new_size;
|
|
226
395
|
}
|
|
227
396
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
char *partition_id_cobhan_buffer,
|
|
231
|
-
char *input_cobhan_buffer) {
|
|
397
|
+
void SetSafetyPaddingOverhead(const Napi::CallbackInfo &info) {
|
|
398
|
+
NapiUtils::RequireParameterCount(info, 1);
|
|
232
399
|
|
|
233
|
-
|
|
234
|
-
|
|
400
|
+
// Napi::Number safety_padding_number = info[0].ToNumber();
|
|
401
|
+
// auto new_safety_padding_bytes = (size_t)
|
|
402
|
+
// safety_padding_number.Int32Value(); Safety padding size is now fixed -
|
|
403
|
+
// ignore the input set_safety_padding_bytes(new_safety_padding_bytes);
|
|
404
|
+
}
|
|
235
405
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
406
|
+
Napi::Value
|
|
407
|
+
GetSetupStatus(const Napi::CallbackInfo
|
|
408
|
+
&info) { // NOLINT(*-convert-member-functions-to-static)
|
|
409
|
+
int32_t setup_status = setup_state.load(std::memory_order_acquire);
|
|
410
|
+
return Napi::Boolean::New(info.Env(), setup_status != 0);
|
|
411
|
+
}
|
|
240
412
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
maximum_stack_alloc_size, __func__);
|
|
413
|
+
void SetLogHook(const Napi::CallbackInfo &info) {
|
|
414
|
+
NapiUtils::RequireParameterCount(info, 1);
|
|
244
415
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
logger.log_error_and_throw(
|
|
248
|
-
__func__,
|
|
249
|
-
"Failed initial canary check for partition_id_cobhan_buffer");
|
|
250
|
-
}
|
|
251
|
-
char *input_canary_ptr = get_canary_ptr(input_cobhan_buffer);
|
|
252
|
-
if (unlikely(!check_canary_ptr(logger, input_canary_ptr))) {
|
|
253
|
-
logger.log_error_and_throw(
|
|
254
|
-
__func__, "Failed initial canary check for input_cobhan_buffer");
|
|
255
|
-
}
|
|
256
|
-
char *output_canary_ptr = get_canary_ptr(output_cobhan_buffer);
|
|
257
|
-
if (unlikely(!check_canary_ptr(logger, output_canary_ptr))) {
|
|
258
|
-
logger.log_error_and_throw(
|
|
259
|
-
__func__, "Failed initial canary check for output_cobhan_buffer");
|
|
416
|
+
if (unlikely(!info[0].IsFunction())) {
|
|
417
|
+
logger.log_error_and_throw(__func__, "Wrong argument type");
|
|
260
418
|
}
|
|
261
419
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
420
|
+
logger.set_log_hook(info[0].As<Napi::Function>());
|
|
421
|
+
}
|
|
265
422
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
423
|
+
void BeginSetupAsherah(const Napi::Env &env, const char *func_name,
|
|
424
|
+
const Napi::CallbackInfo &info,
|
|
425
|
+
Napi::String &config_string, size_t &product_id_length,
|
|
426
|
+
size_t &service_name_length) {
|
|
427
|
+
RequireAsherahNotSetup(func_name);
|
|
270
428
|
|
|
271
|
-
|
|
272
|
-
logger.debug_log(__func__, "Returning from asherah-cobhan EncryptToJson");
|
|
273
|
-
}
|
|
429
|
+
NapiUtils::RequireParameterCount(info, 1);
|
|
274
430
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
__func__,
|
|
278
|
-
"Failed post-call canary check for partition_id_cobhan_buffer");
|
|
279
|
-
}
|
|
280
|
-
if (unlikely(!check_canary_ptr(logger, input_canary_ptr))) {
|
|
281
|
-
logger.log_error_and_throw(
|
|
282
|
-
__func__, "Failed post-call canary check for input_cobhan_buffer");
|
|
283
|
-
}
|
|
284
|
-
if (unlikely(!check_canary_ptr(logger, output_canary_ptr))) {
|
|
285
|
-
logger.log_error_and_throw(
|
|
286
|
-
__func__, "Failed post-call canary check for output_cobhan_buffer");
|
|
287
|
-
}
|
|
431
|
+
Napi::Object config_json;
|
|
432
|
+
NapiUtils::AsJsonObjectAndString(env, info[0], config_string, config_json);
|
|
288
433
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
434
|
+
Napi::String product_id;
|
|
435
|
+
NapiUtils::GetStringProperty(config_json, "ProductID", product_id);
|
|
436
|
+
product_id_length = NapiUtils::GetUtf8StringLength(env, product_id);
|
|
437
|
+
|
|
438
|
+
Napi::String service_name;
|
|
439
|
+
NapiUtils::GetStringProperty(config_json, "ServiceName", service_name);
|
|
440
|
+
service_name_length = NapiUtils::GetUtf8StringLength(env, service_name);
|
|
292
441
|
|
|
293
|
-
|
|
294
|
-
|
|
442
|
+
bool verbose;
|
|
443
|
+
NapiUtils::GetBooleanProperty(config_json, "Verbose", verbose, false);
|
|
444
|
+
verbose_flag = verbose;
|
|
295
445
|
}
|
|
296
446
|
|
|
297
|
-
|
|
298
|
-
|
|
447
|
+
void EndSetupAsherah(GoInt32 result, size_t product_id_length,
|
|
448
|
+
size_t service_name_length) {
|
|
449
|
+
CheckResult(result);
|
|
299
450
|
|
|
300
|
-
|
|
301
|
-
logger.debug_log(__func__, "called");
|
|
302
|
-
}
|
|
451
|
+
est_intermediate_key_overhead = product_id_length + service_name_length;
|
|
303
452
|
|
|
304
|
-
|
|
305
|
-
|
|
453
|
+
auto old_setup_state = setup_state.exchange(1, std::memory_order_acq_rel);
|
|
454
|
+
if (unlikely(old_setup_state != 0)) {
|
|
455
|
+
logger.log_error_and_throw(__func__, "lost race to mark setup_state!");
|
|
306
456
|
}
|
|
457
|
+
}
|
|
307
458
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
459
|
+
void BeginEncryptToJson(const Napi::Env &env, const char *func_name,
|
|
460
|
+
const Napi::CallbackInfo &info,
|
|
461
|
+
Napi::String &partition_id, Napi::Value &input) {
|
|
462
|
+
RequireAsherahSetup(func_name);
|
|
311
463
|
|
|
312
|
-
|
|
313
|
-
logger.log_error_and_throw(__func__, "Wrong argument types");
|
|
314
|
-
}
|
|
464
|
+
NapiUtils::RequireParameterCount(info, 2);
|
|
315
465
|
|
|
316
|
-
|
|
466
|
+
partition_id = NapiUtils::RequireParameterString(env, func_name, info[0]);
|
|
467
|
+
input = NapiUtils::RequireParameterStringOrBuffer(env, func_name, info[1]);
|
|
468
|
+
}
|
|
317
469
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
NAPI_STRING_TO_CBUFFER(
|
|
322
|
-
env, logger, partition_id, partition_id_cobhan_buffer,
|
|
323
|
-
partition_id_copied_bytes, maximum_stack_alloc_size, __func__);
|
|
324
|
-
|
|
325
|
-
Napi::String input = info[1].As<Napi::String>();
|
|
326
|
-
char *input_cobhan_buffer;
|
|
327
|
-
size_t input_copied_bytes;
|
|
328
|
-
NAPI_STRING_TO_CBUFFER(env, logger, input, input_cobhan_buffer,
|
|
329
|
-
input_copied_bytes, maximum_stack_alloc_size,
|
|
330
|
-
__func__);
|
|
331
|
-
|
|
332
|
-
char *output_cobhan_buffer;
|
|
333
|
-
ALLOCATE_CBUFFER(logger, output_cobhan_buffer, input_copied_bytes,
|
|
334
|
-
maximum_stack_alloc_size, __func__);
|
|
335
|
-
|
|
336
|
-
char *partition_id_canary_ptr = get_canary_ptr(partition_id_cobhan_buffer);
|
|
337
|
-
if (unlikely(!check_canary_ptr(logger, partition_id_canary_ptr))) {
|
|
338
|
-
logger.log_error_and_throw(
|
|
339
|
-
__func__,
|
|
340
|
-
"Failed initial canary check for partition_id_cobhan_buffer");
|
|
341
|
-
}
|
|
342
|
-
char *input_canary_ptr = get_canary_ptr(input_cobhan_buffer);
|
|
343
|
-
if (unlikely(!check_canary_ptr(logger, input_canary_ptr))) {
|
|
344
|
-
logger.log_error_and_throw(
|
|
345
|
-
__func__, "Failed initial canary check for input_cobhan_buffer");
|
|
346
|
-
}
|
|
347
|
-
char *output_canary_ptr = get_canary_ptr(output_cobhan_buffer);
|
|
348
|
-
if (unlikely(!check_canary_ptr(logger, output_canary_ptr))) {
|
|
349
|
-
logger.log_error_and_throw(
|
|
350
|
-
__func__, "Failed initial canary check for output_cobhan_buffer");
|
|
351
|
-
}
|
|
470
|
+
void EndEncryptToJson(Napi::Env env, CobhanBufferNapi &output, GoInt32 result,
|
|
471
|
+
Napi::String &output_string) {
|
|
472
|
+
CheckResult(result);
|
|
352
473
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
}
|
|
474
|
+
output_string = output.ToString(env);
|
|
475
|
+
}
|
|
356
476
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
input_cobhan_buffer, output_cobhan_buffer);
|
|
477
|
+
void EndEncryptToJson(Napi::Env env, CobhanBufferNapi &output, GoInt32 result,
|
|
478
|
+
Napi::Buffer<unsigned char> &output_buffer) {
|
|
479
|
+
CheckResult(result);
|
|
361
480
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
"Returned from asherah-cobhan DecryptFromJson");
|
|
365
|
-
}
|
|
481
|
+
output_buffer = output.ToBuffer(env);
|
|
482
|
+
}
|
|
366
483
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
if (unlikely(!check_canary_ptr(logger, input_canary_ptr))) {
|
|
373
|
-
logger.log_error_and_throw(
|
|
374
|
-
__func__, "Failed post-call canary check for input_cobhan_buffer");
|
|
375
|
-
}
|
|
376
|
-
if (unlikely(!check_canary_ptr(logger, output_canary_ptr))) {
|
|
377
|
-
logger.log_error_and_throw(
|
|
378
|
-
__func__, "Failed post-call canary check for output_cobhan_buffer");
|
|
379
|
-
}
|
|
484
|
+
void BeginDecryptFromJson(const Napi::Env &env, const char *func_name,
|
|
485
|
+
const Napi::CallbackInfo &info,
|
|
486
|
+
Napi::String &partition_id, Napi::Value &input) {
|
|
487
|
+
RequireAsherahSetup(func_name);
|
|
380
488
|
|
|
381
|
-
|
|
382
|
-
logger.log_error_and_throw(__func__, AsherahCobhanErrorToString(result));
|
|
383
|
-
}
|
|
489
|
+
NapiUtils::RequireParameterCount(info, 2);
|
|
384
490
|
|
|
385
|
-
|
|
386
|
-
|
|
491
|
+
partition_id = NapiUtils::RequireParameterString(env, func_name, info[0]);
|
|
492
|
+
input = NapiUtils::RequireParameterStringOrBuffer(env, func_name, info[1]);
|
|
493
|
+
}
|
|
387
494
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
495
|
+
void EndDecryptFromJson(Napi::Env &env, CobhanBufferNapi &output,
|
|
496
|
+
GoInt32 result,
|
|
497
|
+
Napi::Buffer<unsigned char> &output_buffer) {
|
|
498
|
+
CheckResult(result);
|
|
391
499
|
|
|
392
|
-
|
|
500
|
+
output_buffer = output.ToBuffer(env);
|
|
393
501
|
}
|
|
394
502
|
|
|
395
|
-
|
|
396
|
-
|
|
503
|
+
void EndDecryptFromJson(Napi::Env &env, CobhanBufferNapi &output,
|
|
504
|
+
GoInt32 result, Napi::String &output_string) {
|
|
505
|
+
CheckResult(result);
|
|
397
506
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
}
|
|
507
|
+
output_string = output.ToString(env);
|
|
508
|
+
}
|
|
401
509
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
510
|
+
void BeginShutdownAsherah(const Napi::Env &, const char *func_name,
|
|
511
|
+
const Napi::CallbackInfo &info) {
|
|
512
|
+
RequireAsherahSetup(func_name);
|
|
513
|
+
NapiUtils::RequireParameterCount(info, 0);
|
|
514
|
+
}
|
|
405
515
|
|
|
406
|
-
|
|
407
|
-
|
|
516
|
+
void EndShutdownAsherah() {
|
|
517
|
+
auto old_setup_state = setup_state.exchange(0, std::memory_order_acq_rel);
|
|
518
|
+
if (unlikely(old_setup_state == 0)) {
|
|
519
|
+
logger.log_error_and_throw(__func__, "lost race to mark setup_state!");
|
|
408
520
|
}
|
|
521
|
+
}
|
|
409
522
|
|
|
410
|
-
|
|
411
|
-
logger.log_error_and_throw(__func__, "Wrong argument types");
|
|
412
|
-
}
|
|
523
|
+
#pragma endregion Begin / End Methods
|
|
413
524
|
|
|
414
|
-
|
|
525
|
+
#pragma region AsyncWorkers
|
|
415
526
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
char *input_cobhan_buffer;
|
|
425
|
-
size_t input_copied_bytes;
|
|
426
|
-
NAPI_STRING_TO_CBUFFER(env, logger, input, input_cobhan_buffer,
|
|
427
|
-
input_copied_bytes, maximum_stack_alloc_size,
|
|
428
|
-
__func__);
|
|
429
|
-
|
|
430
|
-
char *output_cobhan_buffer;
|
|
431
|
-
ALLOCATE_CBUFFER(logger, output_cobhan_buffer, input_copied_bytes,
|
|
432
|
-
maximum_stack_alloc_size, __func__);
|
|
433
|
-
|
|
434
|
-
char *partition_id_canary_ptr = get_canary_ptr(partition_id_cobhan_buffer);
|
|
435
|
-
if (unlikely(!check_canary_ptr(logger, partition_id_canary_ptr))) {
|
|
436
|
-
logger.log_error_and_throw(
|
|
437
|
-
__func__,
|
|
438
|
-
"Failed initial canary check for partition_id_cobhan_buffer");
|
|
439
|
-
}
|
|
440
|
-
char *input_canary_ptr = get_canary_ptr(input_cobhan_buffer);
|
|
441
|
-
if (unlikely(!check_canary_ptr(logger, input_canary_ptr))) {
|
|
442
|
-
logger.log_error_and_throw(
|
|
443
|
-
__func__, "Failed initial canary check for input_cobhan_buffer");
|
|
444
|
-
}
|
|
445
|
-
char *output_canary_ptr = get_canary_ptr(output_cobhan_buffer);
|
|
446
|
-
if (unlikely(!check_canary_ptr(logger, output_canary_ptr))) {
|
|
447
|
-
logger.log_error_and_throw(
|
|
448
|
-
__func__, "Failed initial canary check for output_cobhan_buffer");
|
|
449
|
-
}
|
|
527
|
+
class SetupAsherahWorker : public AsherahAsyncWorker<GoInt32> {
|
|
528
|
+
public:
|
|
529
|
+
SetupAsherahWorker(Napi::Env env, Asherah *instance,
|
|
530
|
+
CobhanBufferNapi &config, size_t product_id_length,
|
|
531
|
+
size_t service_name_length)
|
|
532
|
+
: AsherahAsyncWorker<GoInt32>(env, instance), config(std::move(config)),
|
|
533
|
+
product_id_length(product_id_length),
|
|
534
|
+
service_name_length(service_name_length) {}
|
|
450
535
|
|
|
451
|
-
|
|
452
|
-
|
|
536
|
+
GoInt32 ExecuteTask() override { return SetupJson(config); }
|
|
537
|
+
|
|
538
|
+
Napi::Value OnOKTask(Napi::Env &env) override {
|
|
539
|
+
asherah->EndSetupAsherah(result, product_id_length, service_name_length);
|
|
540
|
+
return env.Undefined();
|
|
453
541
|
}
|
|
454
542
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
543
|
+
private:
|
|
544
|
+
CobhanBufferNapi config;
|
|
545
|
+
size_t product_id_length;
|
|
546
|
+
size_t service_name_length;
|
|
547
|
+
};
|
|
459
548
|
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
549
|
+
class EncryptAsherahWorker : public AsherahAsyncWorker<GoInt32> {
|
|
550
|
+
public:
|
|
551
|
+
EncryptAsherahWorker(const Napi::Env &env, Asherah *instance,
|
|
552
|
+
CobhanBufferNapi &partition_id,
|
|
553
|
+
CobhanBufferNapi &input, CobhanBufferNapi &output)
|
|
554
|
+
: AsherahAsyncWorker(env, instance),
|
|
555
|
+
partition_id(std::move(partition_id)), input(std::move(input)),
|
|
556
|
+
output(std::move(output)) {}
|
|
464
557
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}
|
|
470
|
-
if (unlikely(!check_canary_ptr(logger, input_canary_ptr))) {
|
|
471
|
-
logger.log_error_and_throw(
|
|
472
|
-
__func__, "Failed post-call canary check for input_cobhan_buffer");
|
|
473
|
-
}
|
|
474
|
-
if (unlikely(!check_canary_ptr(logger, output_canary_ptr))) {
|
|
475
|
-
logger.log_error_and_throw(
|
|
476
|
-
__func__, "Failed post-call canary check for output_cobhan_buffer");
|
|
558
|
+
// extern GoInt32 EncryptToJson(void* partitionIdPtr, void* dataPtr,
|
|
559
|
+
// void* jsonPtr);
|
|
560
|
+
GoInt32 ExecuteTask() override {
|
|
561
|
+
return EncryptToJson(partition_id, input, output);
|
|
477
562
|
}
|
|
478
563
|
|
|
479
|
-
|
|
480
|
-
|
|
564
|
+
Napi::Value OnOKTask(Napi::Env &env) override {
|
|
565
|
+
Napi::String output_string;
|
|
566
|
+
asherah->EndEncryptToJson(env, output, result, output_string);
|
|
567
|
+
return output_string;
|
|
481
568
|
}
|
|
482
569
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
570
|
+
private:
|
|
571
|
+
CobhanBufferNapi partition_id;
|
|
572
|
+
CobhanBufferNapi input;
|
|
573
|
+
CobhanBufferNapi output;
|
|
574
|
+
};
|
|
488
575
|
|
|
489
|
-
|
|
490
|
-
|
|
576
|
+
template <typename T>
|
|
577
|
+
class DecryptFromJsonWorker : public AsherahAsyncWorker<GoInt32> {
|
|
578
|
+
public:
|
|
579
|
+
DecryptFromJsonWorker(const Napi::Env &env, Asherah *instance,
|
|
580
|
+
CobhanBufferNapi &partition_id,
|
|
581
|
+
CobhanBufferNapi &input, CobhanBufferNapi &output)
|
|
582
|
+
: AsherahAsyncWorker(env, instance),
|
|
583
|
+
partition_id(std::move(partition_id)), input(std::move(input)),
|
|
584
|
+
output(std::move(output)) {}
|
|
491
585
|
|
|
492
|
-
|
|
493
|
-
|
|
586
|
+
GoInt32 ExecuteTask() override {
|
|
587
|
+
return DecryptFromJson(partition_id, input, output);
|
|
588
|
+
}
|
|
494
589
|
|
|
495
|
-
|
|
496
|
-
|
|
590
|
+
Napi::Value OnOKTask(Napi::Env &env) override {
|
|
591
|
+
T output_result;
|
|
592
|
+
asherah->EndDecryptFromJson(env, output, result, output_result);
|
|
593
|
+
return output_result;
|
|
497
594
|
}
|
|
498
595
|
|
|
499
|
-
|
|
596
|
+
protected:
|
|
597
|
+
CobhanBufferNapi partition_id;
|
|
598
|
+
CobhanBufferNapi input;
|
|
599
|
+
CobhanBufferNapi output;
|
|
600
|
+
};
|
|
500
601
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
602
|
+
class ShutdownAsherahWorker : public AsherahAsyncWorker<GoInt32> {
|
|
603
|
+
public:
|
|
604
|
+
using AsherahAsyncWorker::AsherahAsyncWorker;
|
|
504
605
|
|
|
505
606
|
// extern void Shutdown();
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
logger.debug_log(__func__, "Returned from asherah-cobhan Shutdown");
|
|
607
|
+
GoInt32 ExecuteTask() override {
|
|
608
|
+
Shutdown();
|
|
609
|
+
return 0;
|
|
510
610
|
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) {
|
|
514
|
-
std::lock_guard<std::mutex> lock(asherah_lock);
|
|
515
611
|
|
|
516
|
-
|
|
517
|
-
|
|
612
|
+
Napi::Value OnOKTask(Napi::Env &env) override {
|
|
613
|
+
asherah->EndShutdownAsherah();
|
|
614
|
+
return env.Undefined();
|
|
518
615
|
}
|
|
616
|
+
};
|
|
519
617
|
|
|
520
|
-
|
|
521
|
-
logger.log_error_and_throw(__func__, "Wrong number of arguments");
|
|
522
|
-
}
|
|
618
|
+
#pragma endregion AsyncWorkers
|
|
523
619
|
|
|
524
|
-
|
|
620
|
+
#pragma region Helpers
|
|
525
621
|
|
|
526
|
-
|
|
622
|
+
void CheckResult(GoInt32 result) {
|
|
623
|
+
if (unlikely(result < 0)) {
|
|
624
|
+
logger.log_error_and_throw(__func__, AsherahCobhanErrorToString(result));
|
|
625
|
+
}
|
|
527
626
|
}
|
|
528
627
|
|
|
529
|
-
void
|
|
530
|
-
std::
|
|
531
|
-
|
|
532
|
-
if (unlikely(verbose_flag)) {
|
|
533
|
-
logger.debug_log(__func__, "called");
|
|
628
|
+
void RequireAsherahSetup(const char *func_name) {
|
|
629
|
+
if (unlikely(setup_state.load(std::memory_order_acquire) == 0)) {
|
|
630
|
+
logger.log_error_and_throw(func_name, "setup() not called");
|
|
534
631
|
}
|
|
632
|
+
}
|
|
535
633
|
|
|
536
|
-
|
|
537
|
-
|
|
634
|
+
void RequireAsherahNotSetup(const char *func_name) {
|
|
635
|
+
if (unlikely(setup_state.load(std::memory_order_acquire) != 0)) {
|
|
636
|
+
logger.log_error_and_throw(func_name, "setup() already called");
|
|
538
637
|
}
|
|
539
|
-
|
|
540
|
-
Napi::Number safety_padding_number = info[0].ToNumber();
|
|
541
|
-
|
|
542
|
-
set_safety_padding_bytes((size_t)safety_padding_number.Int32Value());
|
|
543
638
|
}
|
|
544
639
|
|
|
545
640
|
__attribute__((always_inline)) inline size_t
|
|
@@ -549,25 +644,16 @@ private:
|
|
|
549
644
|
const double base64_overhead = 1.34;
|
|
550
645
|
|
|
551
646
|
// Add one rather than using std::ceil to round up
|
|
552
|
-
|
|
553
|
-
(double(data_byte_len + est_encryption_overhead) *
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
std::string log_msg =
|
|
560
|
-
__func__ + ("(" + std::to_string(data_byte_len)) + ", " +
|
|
561
|
-
std::to_string(partition_byte_len) +
|
|
562
|
-
") est_data_byte_len: " + std::to_string(est_data_byte_len) +
|
|
563
|
-
" asherah_output_size_bytes: " +
|
|
564
|
-
std::to_string(asherah_output_size_bytes);
|
|
565
|
-
logger.debug_log(__func__, log_msg);
|
|
566
|
-
}
|
|
567
|
-
return asherah_output_size_bytes;
|
|
647
|
+
size_t est_data_byte_len =
|
|
648
|
+
size_t(double(data_byte_len + est_encryption_overhead) *
|
|
649
|
+
base64_overhead) +
|
|
650
|
+
1;
|
|
651
|
+
|
|
652
|
+
return est_envelope_overhead + est_intermediate_key_overhead +
|
|
653
|
+
partition_byte_len + est_data_byte_len;
|
|
568
654
|
}
|
|
569
655
|
|
|
570
|
-
__attribute__((always_inline)) inline const char *
|
|
656
|
+
__attribute__((always_inline)) static inline const char *
|
|
571
657
|
AsherahCobhanErrorToString(int32_t error) {
|
|
572
658
|
switch (error) {
|
|
573
659
|
case 0:
|
|
@@ -606,6 +692,8 @@ private:
|
|
|
606
692
|
return "Unknown error";
|
|
607
693
|
}
|
|
608
694
|
}
|
|
695
|
+
|
|
696
|
+
#pragma endregion Helpers
|
|
609
697
|
};
|
|
610
698
|
|
|
611
699
|
NODE_API_NAMED_ADDON('asherah', Asherah)
|