Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdy committed May 9, 2024
1 parent 3127602 commit a18b1e0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 118 deletions.
111 changes: 33 additions & 78 deletions src/asherah.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
#include "logging.h"
#include "napi_utils.h"
#include "scoped_allocate.h"
#include <iostream>
#include <mutex>
#include <napi.h>

static volatile std::atomic<int32_t> setup_state{0};

class Asherah : public Napi::Addon<Asherah> {
public:
Asherah(Napi::Env, Napi::Object exports) {
Asherah(Napi::Env env, Napi::Object exports) : logger(env, "asherah-node") {
DefineAddon(
exports,
{
Expand All @@ -26,7 +24,8 @@ class Asherah : public Napi::Addon<Asherah> {
InstanceMethod("decrypt", &Asherah::DecryptSync),
InstanceMethod("decrypt_async", &Asherah::DecryptAsync),
InstanceMethod("decrypt_string", &Asherah::DecryptStringSync),
InstanceMethod("decrypt_string_async", &Asherah::DecryptStringAsync),
InstanceMethod("decrypt_string_async",
&Asherah::DecryptStringAsync),
InstanceMethod("shutdown", &Asherah::ShutdownAsherahSync),
InstanceMethod("shutdown_async", &Asherah::ShutdownAsherahAsync),
InstanceMethod("set_max_stack_alloc_item_size",
Expand Down Expand Up @@ -105,9 +104,6 @@ class Asherah : public Napi::Addon<Asherah> {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}
BeginShutdownAsherah();
Shutdown();
EndShutdownAsherah();
Expand All @@ -120,10 +116,6 @@ class Asherah : public Napi::Addon<Asherah> {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}

BeginShutdownAsherah();
auto worker = new ShutdownAsherahWorker(env, this);
worker->Queue();
Expand Down Expand Up @@ -180,8 +172,7 @@ class Asherah : public Napi::Addon<Asherah> {
try {
Napi::String partition_id_string;
Napi::Value input_value;
BeginEncryptToJson(env, __func__, info, partition_id_string,
input_value);
BeginEncryptToJson(env, __func__, info, partition_id_string, input_value);

CobhanBufferNapi partition_id(env, partition_id_string);
CobhanBufferNapi input(env, input_value);
Expand Down Expand Up @@ -269,14 +260,6 @@ class Asherah : public Napi::Addon<Asherah> {
Napi::HandleScope scope(env);
Napi::String output_string;
try {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}

if (unlikely(setup_state.load(std::memory_order_relaxed) == 0)) {
logger.log_error_and_throw(__func__, "setup() not called");
}

if (unlikely(info.Length() < 2)) {
logger.log_error_and_throw(__func__, "Wrong number of arguments");
}
Expand Down Expand Up @@ -308,14 +291,6 @@ class Asherah : public Napi::Addon<Asherah> {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
try {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}

if (unlikely(setup_state == 0)) {
logger.log_error_and_throw(__func__, "setup() not called");
}

if (unlikely(info.Length() < 2)) {
logger.log_error_and_throw(__func__, "Wrong number of arguments");
}
Expand All @@ -334,8 +309,8 @@ class Asherah : public Napi::Addon<Asherah> {

CobhanBufferNapi output(input.get_data_len_bytes());

auto worker =
new DecryptFromJsonToStringWorker(env, this, partition_id, input, output);
auto worker = new DecryptFromJsonToStringWorker(env, this, partition_id,
input, output);
worker->Queue();

return worker->Promise();
Expand All @@ -344,11 +319,7 @@ class Asherah : public Napi::Addon<Asherah> {
}
}

void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}

void SetMaxStackAllocItemSize(const Napi::CallbackInfo &info) {
if (unlikely(info.Length() < 1)) {
logger.log_error_and_throw(__func__, "Wrong number of arguments");
}
Expand All @@ -360,19 +331,33 @@ class Asherah : public Napi::Addon<Asherah> {
}

void SetSafetyPaddingOverhead(const Napi::CallbackInfo &info) {

if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
if (unlikely(info.Length() < 1)) {
logger.log_error_and_throw(__func__, "Wrong number of arguments");
}

// Napi::Number safety_padding_number = info[0].ToNumber();
// auto new_safety_padding_bytes = (size_t)
// safety_padding_number.Int32Value(); Safety padding size is now fixed -
// ignore the input set_safety_padding_bytes(new_safety_padding_bytes);
}

Napi::Value
GetSetupStatus(const Napi::CallbackInfo
&info) { // NOLINT(*-convert-member-functions-to-static)
return Napi::Boolean::New(info.Env(),
setup_state.load(std::memory_order_acquire) != 0);
}

void SetLogHook(const Napi::CallbackInfo &info) {
if (unlikely(info.Length() < 1)) {
logger.log_error_and_throw(__func__, "Wrong number of arguments");
}

//Napi::Number safety_padding_number = info[0].ToNumber();
//auto new_safety_padding_bytes = (size_t) safety_padding_number.Int32Value();
//Safety padding size is now fixed - ignore the input
//set_safety_padding_bytes(new_safety_padding_bytes);
if (unlikely(!info[0].IsFunction())) {
logger.log_error_and_throw(__func__, "Wrong argument type");
}

logger.set_log_hook(info[0].As<Napi::Function>());
}

void BeginSetupAsherah(const Napi::Env &env, const char *func_name,
Expand Down Expand Up @@ -504,20 +489,12 @@ class Asherah : public Napi::Addon<Asherah> {
}

void BeginShutdownAsherah() {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}

if (unlikely(setup_state.load(std::memory_order_acquire) == 0)) {
logger.log_error_and_throw(__func__, "setup() not called");
}
}

void EndShutdownAsherah() {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}

auto old_setup_state = setup_state.exchange(0, std::memory_order_acq_rel);
if (unlikely(old_setup_state == 0)) {
logger.log_error_and_throw(__func__, "setup() not called");
Expand Down Expand Up @@ -576,9 +553,11 @@ class Asherah : public Napi::Addon<Asherah> {
class DecryptFromJsonWorker : public AsherahAsyncWorker<GoInt32> {
public:
DecryptFromJsonWorker(const Napi::Env &env, Asherah *instance,
CobhanBufferNapi &partition_id, CobhanBufferNapi &input, CobhanBufferNapi &output)
CobhanBufferNapi &partition_id,
CobhanBufferNapi &input, CobhanBufferNapi &output)
: AsherahAsyncWorker(env, instance),
partition_id(std::move(partition_id)), input(std::move(input)), output(std::move(output)) {}
partition_id(std::move(partition_id)), input(std::move(input)),
output(std::move(output)) {}

// extern GoInt32 DecryptFromJson(void* partitionIdPtr, void* jsonPtr,
// void* dataPtr);
Expand Down Expand Up @@ -613,8 +592,7 @@ class Asherah : public Napi::Addon<Asherah> {
CobhanBufferNapi &partition_id,
CobhanBufferNapi &input,
CobhanBufferNapi &output)
: DecryptFromJsonWorker(env, instance, partition_id, input,
output) {}
: DecryptFromJsonWorker(env, instance, partition_id, input, output) {}

Napi::Value OnOKTask(Napi::Env &env) override {
Napi::String output_string;
Expand Down Expand Up @@ -706,29 +684,6 @@ class Asherah : public Napi::Addon<Asherah> {
return "Unknown error";
}
}

void SetLogHook(const Napi::CallbackInfo &info) {
if (unlikely(verbose_flag)) {
logger.debug_log(__func__, "called");
}

if (unlikely(info.Length() < 1)) {
logger.log_error_and_throw(__func__, "Wrong number of arguments");
}

if (unlikely(!info[0].IsFunction())) {
logger.log_error_and_throw(__func__, "Wrong argument type");
}

logger.set_log_hook(info[0].As<Napi::Function>());
}

Napi::Value
GetSetupStatus(const Napi::CallbackInfo
&info) { // NOLINT(*-convert-member-functions-to-static)
return Napi::Boolean::New(info.Env(),
setup_state.load(std::memory_order_acquire) != 0);
}
};

NODE_API_NAMED_ADDON('asherah', Asherah)
1 change: 0 additions & 1 deletion src/asherah_async_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#ifndef ASHERAH_ASYNC_WORKER_H
#define ASHERAH_ASYNC_WORKER_H

#include <iostream>
#include <napi.h>
#include <stdexcept>

Expand Down
11 changes: 6 additions & 5 deletions src/cobhan_buffer.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef ASHERAH_NODE_COBHAN_BUFFER_H
#define ASHERAH_NODE_COBHAN_BUFFER_H
#ifndef COBHAN_BUFFER_H
#define COBHAN_BUFFER_H

#include <cstdint>
#include <cstring> // for std::memcpy
#include <limits> // for std::numeric_limits
#include <stdexcept> // for std::runtime_error, std::invalid_argument
#include <iostream> // for std::cerr, std::terminate

class CobhanBuffer {
public:
Expand Down Expand Up @@ -55,8 +56,8 @@ class CobhanBuffer {

~CobhanBuffer() {
if (!verify_canaries()) {
std::cerr << "asherah-node: Memory corruption detected: Canary values "
"are corrupted."
std::cerr << "CobhanBuffer: Memory corruption detected: Canary values "
"are corrupted. Terminating process."
<< std::endl
<< std::flush;
std::terminate();
Expand Down Expand Up @@ -229,4 +230,4 @@ class CobhanBuffer {
static_cast<size_t>(std::numeric_limits<int32_t>::max());
};

#endif // ASHERAH_NODE_COBHAN_BUFFER_H
#endif // COBHAN_BUFFER_H
Loading

0 comments on commit a18b1e0

Please sign in to comment.