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/src/asherah.cc CHANGED
@@ -1,545 +1,640 @@
1
- #include "asherah.h"
2
- #include "../lib/libasherah.h"
3
- #include "cobhan_napi_interop.h"
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 "logging.h"
6
- #include <iostream>
7
- #include <mutex>
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(exports,
14
- {InstanceMethod("setup", &Asherah::SetupAsherah),
15
- InstanceMethod("encrypt", &Asherah::Encrypt),
16
- InstanceMethod("encrypt_string", &Asherah::EncryptString),
17
- InstanceMethod("decrypt", &Asherah::Decrypt),
18
- InstanceMethod("decrypt_string", &Asherah::DecryptString),
19
- InstanceMethod("shutdown", &Asherah::ShutdownAsherah),
20
- InstanceMethod("set_max_stack_alloc_item_size",
21
- &Asherah::SetMaxStackAllocItemSize),
22
- InstanceMethod("set_safety_padding_overhead",
23
- &Asherah::SetSafetyPaddingOverhead),
24
- InstanceMethod("set_log_hook", &Asherah::SetLogHook)});
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
- Logger logger;
55
+ LoggerNapi logger;
36
56
 
37
- void SetLogHook(const Napi::CallbackInfo &info) {
38
- std::lock_guard<std::mutex> lock(asherah_lock);
57
+ #pragma region Published Node Addon Methods
39
58
 
40
- if (unlikely(verbose_flag)) {
41
- logger.debug_log(__func__, "called");
42
- }
43
-
44
- if (unlikely(info.Length() < 1)) {
45
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
46
- }
47
-
48
- if (unlikely(!info[0].IsFunction())) {
49
- logger.log_error_and_throw(__func__, "Wrong argument type");
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
- void SetupAsherah(const Napi::CallbackInfo &info) {
56
- std::lock_guard<std::mutex> lock(asherah_lock);
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
- if (unlikely(verbose_flag)) {
59
- logger.debug_log(__func__, "called");
60
- }
99
+ BeginSetupAsherah(env, __func__, info, config_string, product_id_length,
100
+ service_name_length);
61
101
 
62
- if (unlikely(setup_state == 1)) {
63
- logger.log_error_and_throw(__func__, "setup called twice");
64
- }
102
+ CobhanBufferNapi config(env, config_string);
65
103
 
66
- if (unlikely(info.Length() < 1)) {
67
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
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
- Napi::String config;
71
- Napi::Object config_json;
113
+ void ShutdownAsherahSync(const Napi::CallbackInfo &info) {
72
114
  Napi::Env env = info.Env();
73
- Napi::Object json = env.Global().Get("JSON").As<Napi::Object>();
74
- if (likely(info[0].IsObject())) {
75
- config_json = info[0].As<Napi::Object>();
76
- Napi::Function stringify = json.Get("stringify").As<Napi::Function>();
77
- config = stringify.Call(json, {config_json}).As<Napi::String>();
78
- } else if (likely(info[0].IsString())) {
79
- config = info[0].As<Napi::String>();
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
- Napi::String product_id = config_json.Get("ProductID").As<Napi::String>();
87
- Napi::String service_name =
88
- config_json.Get("ServiceName").As<Napi::String>();
89
-
90
- est_intermediate_key_overhead =
91
- product_id.Utf8Value().length() + service_name.Utf8Value().length();
92
-
93
- Napi::Value verbose = config_json.Get("Verbose");
94
- if (likely(verbose.IsBoolean())) {
95
- verbose_flag = verbose.As<Napi::Boolean>().Value();
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
- char *config_cobhan_buffer;
106
- size_t config_copied_bytes;
107
- NAPI_STRING_TO_CBUFFER(env, logger, config, config_cobhan_buffer,
108
- config_copied_bytes, maximum_stack_alloc_size,
109
- __func__);
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
- char *config_canary_ptr = get_canary_ptr(config_cobhan_buffer);
112
- if (unlikely(!check_canary_ptr(logger, config_canary_ptr))) {
113
- logger.log_error_and_throw(
114
- __func__, "Failed initial canary check for config_cobhan_buffer");
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
- // extern GoInt32 SetupJson(void* configJson);
118
- GoInt32 result = SetupJson(config_cobhan_buffer);
209
+ CobhanBufferNapi partition_id(env, partition_id_string);
210
+ CobhanBufferNapi input(env, input_value);
119
211
 
120
- if (unlikely(verbose_flag)) {
121
- logger.debug_log(__func__, "Returned from asherah-cobhan SetupJson");
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
- if (unlikely(!check_canary_ptr(logger, config_canary_ptr))) {
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
- if (unlikely(result < 0)) {
130
- logger.log_error_and_throw(__func__, AsherahCobhanErrorToString(result));
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 Encrypt(const Napi::CallbackInfo &info) {
136
- std::lock_guard<std::mutex> lock(asherah_lock);
137
-
138
- if (unlikely(verbose_flag)) {
139
- logger.debug_log(__func__, "called");
140
- }
141
-
142
- if (unlikely(setup_state == 0)) {
143
- logger.log_error_and_throw(__func__, "setup() not called");
144
- }
145
-
146
- if (unlikely(info.Length() < 2)) {
147
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
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
- if (unlikely(!info[0].IsString() || !info[1].IsBuffer())) {
151
- logger.log_error_and_throw(__func__, "Wrong argument types");
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
- Napi::String partition_id = info[0].As<Napi::String>();
157
- char *partition_id_cobhan_buffer;
158
- size_t partition_id_copied_bytes;
159
- NAPI_STRING_TO_CBUFFER(
160
- env, logger, partition_id, partition_id_cobhan_buffer,
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
- return output;
180
- }
364
+ if (unlikely(!info[0].IsString() || !info[1].IsString())) {
365
+ logger.log_error_and_throw(__func__, "Wrong argument types");
366
+ }
181
367
 
182
- Napi::Value EncryptString(const Napi::CallbackInfo &info) {
183
- std::lock_guard<std::mutex> lock(asherah_lock);
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
- if (unlikely(verbose_flag)) {
186
- logger.debug_log(__func__, "called");
187
- }
373
+ CobhanBufferNapi partition_id(env, partition_id_string);
374
+ CobhanBufferNapi input(env, input_value);
188
375
 
189
- if (unlikely(setup_state == 0)) {
190
- logger.log_error_and_throw(__func__, "setup() not called");
191
- }
376
+ CobhanBufferNapi output(input.get_data_len_bytes());
192
377
 
193
- if (unlikely(info.Length() < 2)) {
194
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
195
- }
378
+ auto worker = new DecryptFromJsonWorker<Napi::String>(
379
+ env, this, partition_id, input, output);
380
+ worker->Queue();
196
381
 
197
- if (unlikely(!info[0].IsString() || !info[1].IsString())) {
198
- logger.log_error_and_throw(__func__, "Wrong argument types");
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
- Napi::Env env = info.Env();
388
+ void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) {
389
+ NapiUtils::RequireParameterCount(info, 1);
202
390
 
203
- Napi::String partition_id = info[0].As<Napi::String>();
204
- char *partition_id_cobhan_buffer;
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
- return output;
394
+ maximum_stack_alloc_size = new_size;
226
395
  }
227
396
 
228
- Napi::String EncryptCobhanBufferToJson(Napi::Env &env, size_t partition_bytes,
229
- size_t data_bytes,
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
- size_t asherah_output_size_bytes =
234
- EstimateAsherahOutputSize(data_bytes, partition_bytes);
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
- if (unlikely(verbose_flag)) {
237
- logger.debug_log(__func__, " asherah_output_size_bytes " +
238
- std::to_string(asherah_output_size_bytes));
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
- char *output_cobhan_buffer;
242
- ALLOCATE_CBUFFER(logger, output_cobhan_buffer, asherah_output_size_bytes,
243
- maximum_stack_alloc_size, __func__);
413
+ void SetLogHook(const Napi::CallbackInfo &info) {
414
+ NapiUtils::RequireParameterCount(info, 1);
244
415
 
245
- char *partition_id_canary_ptr = get_canary_ptr(partition_id_cobhan_buffer);
246
- if (unlikely(!check_canary_ptr(logger, partition_id_canary_ptr))) {
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
- if (unlikely(verbose_flag)) {
263
- logger.debug_log(__func__, "Calling asherah-cobhan EncryptToJson");
264
- }
420
+ logger.set_log_hook(info[0].As<Napi::Function>());
421
+ }
265
422
 
266
- // extern GoInt32 EncryptToJson(void* partitionIdPtr, void* dataPtr, void*
267
- // jsonPtr);
268
- GoInt32 result = EncryptToJson(partition_id_cobhan_buffer,
269
- input_cobhan_buffer, output_cobhan_buffer);
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
- if (unlikely(verbose_flag)) {
272
- logger.debug_log(__func__, "Returning from asherah-cobhan EncryptToJson");
273
- }
429
+ NapiUtils::RequireParameterCount(info, 1);
274
430
 
275
- if (unlikely(!check_canary_ptr(logger, partition_id_canary_ptr))) {
276
- logger.log_error_and_throw(
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
- if (unlikely(result < 0)) {
290
- logger.log_error_and_throw(__func__, AsherahCobhanErrorToString(result));
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
- Napi::String output = cbuffer_to_nstring(env, logger, output_cobhan_buffer);
294
- return output;
442
+ bool verbose;
443
+ NapiUtils::GetBooleanProperty(config_json, "Verbose", verbose, false);
444
+ verbose_flag = verbose;
295
445
  }
296
446
 
297
- Napi::Value Decrypt(const Napi::CallbackInfo &info) {
298
- std::lock_guard<std::mutex> lock(asherah_lock);
447
+ void EndSetupAsherah(GoInt32 result, size_t product_id_length,
448
+ size_t service_name_length) {
449
+ CheckResult(result);
299
450
 
300
- if (unlikely(verbose_flag)) {
301
- logger.debug_log(__func__, "called");
302
- }
451
+ est_intermediate_key_overhead = product_id_length + service_name_length;
303
452
 
304
- if (unlikely(setup_state == 0)) {
305
- logger.log_error_and_throw(__func__, "setup() not called");
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
- if (unlikely(info.Length() < 2)) {
309
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
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
- if (unlikely(!info[0].IsString() || !info[1].IsString())) {
313
- logger.log_error_and_throw(__func__, "Wrong argument types");
314
- }
464
+ NapiUtils::RequireParameterCount(info, 2);
315
465
 
316
- Napi::Env env = info.Env();
466
+ partition_id = NapiUtils::RequireParameterString(env, func_name, info[0]);
467
+ input = NapiUtils::RequireParameterStringOrBuffer(env, func_name, info[1]);
468
+ }
317
469
 
318
- Napi::String partition_id = info[0].As<Napi::String>();
319
- char *partition_id_cobhan_buffer;
320
- size_t partition_id_copied_bytes;
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
- if (unlikely(verbose_flag)) {
354
- logger.debug_log(__func__, "Calling asherah-cobhan DecryptFromJson");
355
- }
474
+ output_string = output.ToString(env);
475
+ }
356
476
 
357
- // extern GoInt32 DecryptFromJson(void* partitionIdPtr, void* jsonPtr, void*
358
- // dataPtr);
359
- GoInt32 result = DecryptFromJson(partition_id_cobhan_buffer,
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
- if (unlikely(verbose_flag)) {
363
- logger.debug_log(__func__,
364
- "Returned from asherah-cobhan DecryptFromJson");
365
- }
481
+ output_buffer = output.ToBuffer(env);
482
+ }
366
483
 
367
- if (unlikely(!check_canary_ptr(logger, partition_id_canary_ptr))) {
368
- logger.log_error_and_throw(
369
- __func__,
370
- "Failed post-call canary check for partition_id_cobhan_buffer");
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
- if (unlikely(result < 0)) {
382
- logger.log_error_and_throw(__func__, AsherahCobhanErrorToString(result));
383
- }
489
+ NapiUtils::RequireParameterCount(info, 2);
384
490
 
385
- Napi::Buffer<unsigned char> output =
386
- cbuffer_to_nbuffer(env, logger, output_cobhan_buffer);
491
+ partition_id = NapiUtils::RequireParameterString(env, func_name, info[0]);
492
+ input = NapiUtils::RequireParameterStringOrBuffer(env, func_name, info[1]);
493
+ }
387
494
 
388
- if (unlikely(verbose_flag)) {
389
- logger.debug_log(__func__, "finished");
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
- return output;
500
+ output_buffer = output.ToBuffer(env);
393
501
  }
394
502
 
395
- Napi::Value DecryptString(const Napi::CallbackInfo &info) {
396
- std::lock_guard<std::mutex> lock(asherah_lock);
503
+ void EndDecryptFromJson(Napi::Env &env, CobhanBufferNapi &output,
504
+ GoInt32 result, Napi::String &output_string) {
505
+ CheckResult(result);
397
506
 
398
- if (unlikely(verbose_flag)) {
399
- logger.debug_log(__func__, "called");
400
- }
507
+ output_string = output.ToString(env);
508
+ }
401
509
 
402
- if (unlikely(setup_state == 0)) {
403
- logger.log_error_and_throw(__func__, "setup() not called");
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
- if (unlikely(info.Length() < 2)) {
407
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
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
- if (unlikely(!info[0].IsString() || !info[1].IsString())) {
411
- logger.log_error_and_throw(__func__, "Wrong argument types");
412
- }
523
+ #pragma endregion Begin / End Methods
413
524
 
414
- Napi::Env env = info.Env();
525
+ #pragma region AsyncWorkers
415
526
 
416
- Napi::String partition_id = info[0].As<Napi::String>();
417
- char *partition_id_cobhan_buffer;
418
- size_t partition_id_copied_bytes;
419
- NAPI_STRING_TO_CBUFFER(
420
- env, logger, partition_id, partition_id_cobhan_buffer,
421
- partition_id_copied_bytes, maximum_stack_alloc_size, __func__);
422
-
423
- Napi::String input = info[1].As<Napi::String>();
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
- if (unlikely(verbose_flag)) {
452
- logger.debug_log(__func__, "Calling asherah-cobhan DecryptFromJson");
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
- // extern GoInt32 DecryptFromJson(void* partitionIdPtr, void* jsonPtr, void*
456
- // dataPtr);
457
- GoInt32 result = DecryptFromJson(partition_id_cobhan_buffer,
458
- input_cobhan_buffer, output_cobhan_buffer);
543
+ private:
544
+ CobhanBufferNapi config;
545
+ size_t product_id_length;
546
+ size_t service_name_length;
547
+ };
459
548
 
460
- if (unlikely(verbose_flag)) {
461
- logger.debug_log(__func__,
462
- "Returned from asherah-cobhan DecryptFromJson");
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
- if (unlikely(!check_canary_ptr(logger, partition_id_canary_ptr))) {
466
- logger.log_error_and_throw(
467
- __func__,
468
- "Failed post-call canary check for partition_id_cobhan_buffer");
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
- if (unlikely(result < 0)) {
480
- logger.log_error_and_throw(__func__, AsherahCobhanErrorToString(result));
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
- Napi::String output = cbuffer_to_nstring(env, logger, output_cobhan_buffer);
484
-
485
- if (unlikely(verbose_flag)) {
486
- logger.debug_log(__func__, "finished");
487
- }
570
+ private:
571
+ CobhanBufferNapi partition_id;
572
+ CobhanBufferNapi input;
573
+ CobhanBufferNapi output;
574
+ };
488
575
 
489
- return output;
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
- void ShutdownAsherah(const Napi::CallbackInfo &info) {
493
- std::lock_guard<std::mutex> lock(asherah_lock);
586
+ GoInt32 ExecuteTask() override {
587
+ return DecryptFromJson(partition_id, input, output);
588
+ }
494
589
 
495
- if (unlikely(verbose_flag)) {
496
- logger.debug_log(__func__, "called");
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
- setup_state = 0;
596
+ protected:
597
+ CobhanBufferNapi partition_id;
598
+ CobhanBufferNapi input;
599
+ CobhanBufferNapi output;
600
+ };
500
601
 
501
- if (unlikely(verbose_flag)) {
502
- logger.debug_log(__func__, "Calling asherah-cobhan Shutdown");
503
- }
602
+ class ShutdownAsherahWorker : public AsherahAsyncWorker<GoInt32> {
603
+ public:
604
+ using AsherahAsyncWorker::AsherahAsyncWorker;
504
605
 
505
606
  // extern void Shutdown();
506
- Shutdown();
507
-
508
- if (unlikely(verbose_flag)) {
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
- if (unlikely(verbose_flag)) {
517
- logger.debug_log(__func__, "called");
612
+ Napi::Value OnOKTask(Napi::Env &env) override {
613
+ asherah->EndShutdownAsherah();
614
+ return env.Undefined();
518
615
  }
616
+ };
519
617
 
520
- if (unlikely(info.Length() < 1)) {
521
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
522
- }
618
+ #pragma endregion AsyncWorkers
523
619
 
524
- Napi::Number item_size = info[0].ToNumber();
620
+ #pragma region Helpers
525
621
 
526
- maximum_stack_alloc_size = (size_t)item_size.Int32Value();
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 SetSafetyPaddingOverhead(const Napi::CallbackInfo &info) {
530
- std::lock_guard<std::mutex> lock(asherah_lock);
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
- if (unlikely(info.Length() < 1)) {
537
- logger.log_error_and_throw(__func__, "Wrong number of arguments");
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
- double est_data_byte_len =
553
- (double(data_byte_len + est_encryption_overhead) * base64_overhead) + 1;
554
-
555
- size_t asherah_output_size_bytes =
556
- size_t(est_envelope_overhead + est_intermediate_key_overhead +
557
- partition_byte_len + est_data_byte_len);
558
- if (unlikely(verbose_flag)) {
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)