Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging #69

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 67 additions & 36 deletions src/asherah.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ void setup(const Napi::CallbackInfo &info) {
Napi::String product_id = config_json.Get("ProductID").As<Napi::String>();
Napi::String service_name = config_json.Get("ServiceName").As<Napi::String>();

est_intermediate_key_overhead = product_id.Utf8Value().length() + service_name.Utf8Value().length();
est_intermediate_key_overhead =
product_id.Utf8Value().length() + service_name.Utf8Value().length();

Napi::Value verbose = config_json.Get("Verbose");
if (likely(verbose.IsBoolean())) {
Expand Down Expand Up @@ -82,7 +83,8 @@ void setup(const Napi::CallbackInfo &info) {
config_cobhan_buffer = config_cobhan_buffer_unique_ptr.get();
}
if (unlikely(config_cobhan_buffer == nullptr)) {
log_error_and_throw(env, "setup", "Failed to allocate config cobhan buffer");
log_error_and_throw(env, "setup",
"Failed to allocate config cobhan buffer");
return;
}

Expand Down Expand Up @@ -145,7 +147,7 @@ Napi::Value encrypt_to_json(Napi::Env &env, size_t partition_bytes,
}
if (unlikely(output_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt_to_json",
"Failed to allocate cobhan output buffer");
"Failed to allocate cobhan output buffer");
}

if (unlikely(verbose_flag)) {
Expand Down Expand Up @@ -195,7 +197,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
return log_error_and_throw(env, "encrypt",
"Failed to get partition_id utf8 length");
"Failed to get partition_id utf8 length");
}
if (unlikely(partition_utf8_byte_length == 0)) {
return log_error_and_throw(env, "encrypt", "partition_id is empty");
Expand All @@ -221,7 +223,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
}
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt",
"Failed to allocate partitionId cobhan buffer");
"Failed to allocate partitionId cobhan buffer");
}

// Copy
Expand All @@ -231,7 +233,7 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
&partition_copied_bytes);
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt",
"Failed to copy partitionId to cobhan buffer");
"Failed to copy partitionId to cobhan buffer");
}

// Determine size
Expand Down Expand Up @@ -269,11 +271,18 @@ Napi::Value encrypt(const Napi::CallbackInfo &info) {
copy_nbuffer_to_cbuffer(env, input_napi_buffer, input_cobhan_buffer);
if (unlikely(input_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt",
"Failed to copy input buffer to cobhan buffer");
"Failed to copy input buffer to cobhan buffer");
}

return encrypt_to_json(env, partition_copied_bytes, input_byte_length,
partition_id_cobhan_buffer, input_cobhan_buffer);
Napi::Value output =
encrypt_to_json(env, partition_copied_bytes, input_byte_length,
partition_id_cobhan_buffer, input_cobhan_buffer);

if (unlikely(verbose_flag)) {
debug_log("encrypt", "finished");
}

return output;
}

Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
Expand All @@ -288,7 +297,8 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
}

if (unlikely(info.Length() < 2)) {
return log_error_and_throw(env, "encrypt_string", "Wrong number of arguments");
return log_error_and_throw(env, "encrypt_string",
"Wrong number of arguments");
}

if (unlikely(!info[0].IsString() || !info[1].IsString())) {
Expand All @@ -301,7 +311,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
return log_error_and_throw(env, "encrypt_string",
"Failed to get partition_id utf8 length");
"Failed to get partition_id utf8 length");
}
if (unlikely(partition_utf8_byte_length == 0)) {
return log_error_and_throw(env, "encrypt_string", "partition_id is empty");
Expand All @@ -327,7 +337,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
}
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt_string",
"Failed to allocate partitionId cobhan buffer");
"Failed to allocate partitionId cobhan buffer");
}

// Copy
Expand All @@ -337,7 +347,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
&partition_copied_bytes);
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt_string",
"Failed to copy partitionId to cobhan buffer");
"Failed to copy partitionId to cobhan buffer");
}

// Determine size
Expand All @@ -346,7 +356,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
input_utf8_byte_length = nstring_utf8_byte_length(env, input);
if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
return log_error_and_throw(env, "encrypt_string",
"Failed to get input utf8 length");
"Failed to get input utf8 length");
}
if (unlikely(input_utf8_byte_length == 0)) {
return log_error_and_throw(env, "encrypt_string", "input is empty");
Expand All @@ -371,7 +381,7 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
}
if (unlikely(input_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt_string",
"Failed to allocate input cobhan buffer");
"Failed to allocate input cobhan buffer");
}

// Copy
Expand All @@ -381,11 +391,18 @@ Napi::Value encrypt_string(const Napi::CallbackInfo &info) {
input_cobhan_buffer, &input_copied_bytes);
if (unlikely(input_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "encrypt_string",
"Failed to copy input to cobhan buffer");
"Failed to copy input to cobhan buffer");
}

return encrypt_to_json(env, partition_copied_bytes, input_utf8_byte_length,
partition_id_cobhan_buffer, input_cobhan_buffer);
Napi::Value output =
encrypt_to_json(env, partition_copied_bytes, input_utf8_byte_length,
partition_id_cobhan_buffer, input_cobhan_buffer);

if (unlikely(verbose_flag)) {
debug_log("encrypt_string", "finished");
}

return output;
}

Napi::Value decrypt(const Napi::CallbackInfo &info) {
Expand Down Expand Up @@ -413,7 +430,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
return log_error_and_throw(env, "decrypt",
"Failed to get partition_id utf8 length");
"Failed to get partition_id utf8 length");
}
if (unlikely(partition_utf8_byte_length == 0)) {
return log_error_and_throw(env, "decrypt", "partition_id is empty");
Expand All @@ -439,7 +456,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
}
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt",
"Failed to allocate partition_id cobhan buffer");
"Failed to allocate partition_id cobhan buffer");
}

// Copy
Expand All @@ -448,15 +465,16 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
&partition_copied_bytes);
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt",
"Failed to copy partition_id to cobhan buffer");
"Failed to copy partition_id to cobhan buffer");
}

// Determine size
size_t input_utf8_byte_length;
Napi::String input = info[1].As<Napi::String>();
input_utf8_byte_length = nstring_utf8_byte_length(env, input);
if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
return log_error_and_throw(env, "decrypt", "Failed to get input utf8 length");
return log_error_and_throw(env, "decrypt",
"Failed to get input utf8 length");
}
if (unlikely(input_utf8_byte_length == 0)) {
return log_error_and_throw(env, "decrypt", "input is empty");
Expand Down Expand Up @@ -486,7 +504,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
}
if (unlikely(input_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt",
"Failed to allocate input cobhan buffer");
"Failed to allocate input cobhan buffer");
}

// Copy
Expand All @@ -496,7 +514,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
input_cobhan_buffer, &input_copied_bytes);
if (unlikely(input_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt",
"Failed to copy input to cobhan buffer");
"Failed to copy input to cobhan buffer");
}

char *output_cobhan_buffer;
Expand All @@ -517,7 +535,7 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
}
if (unlikely(output_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt",
"Failed to allocate cobhan output buffer");
"Failed to allocate cobhan output buffer");
}

if (unlikely(verbose_flag)) {
Expand All @@ -538,7 +556,14 @@ Napi::Value decrypt(const Napi::CallbackInfo &info) {
return log_error_and_throw(env, "decrypt", std::to_string(result));
}

return cbuffer_to_nbuffer(env, output_cobhan_buffer);
Napi::Buffer<unsigned char> output =
cbuffer_to_nbuffer(env, output_cobhan_buffer);

if (unlikely(verbose_flag)) {
debug_log("decrypt", "finished");
}

return output;
}

Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
Expand All @@ -553,7 +578,8 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
}

if (unlikely(info.Length() < 2)) {
return log_error_and_throw(env, "decrypt_string", "Wrong number of arguments");
return log_error_and_throw(env, "decrypt_string",
"Wrong number of arguments");
}

if (unlikely(!info[0].IsString() || !info[1].IsString())) {
Expand All @@ -566,7 +592,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
partition_utf8_byte_length = nstring_utf8_byte_length(env, partition_id);
if (unlikely(partition_utf8_byte_length == (size_t)(-1))) {
return log_error_and_throw(env, "decrypt_string",
"Failed to get partition_id utf8 length");
"Failed to get partition_id utf8 length");
}
if (unlikely(partition_utf8_byte_length == 0)) {
return log_error_and_throw(env, "decrypt_string", "partition_id is empty");
Expand All @@ -592,7 +618,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
}
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt_string",
"Failed to allocate partitionId cobhan buffer");
"Failed to allocate partitionId cobhan buffer");
}

// Copy
Expand All @@ -602,7 +628,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
&partition_copied_bytes);
if (unlikely(partition_id_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt_string",
"Failed to copy partitionId to cobhan buffer");
"Failed to copy partitionId to cobhan buffer");
}

// Determine size
Expand All @@ -611,7 +637,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
input_utf8_byte_length = nstring_utf8_byte_length(env, input);
if (unlikely(input_utf8_byte_length == (size_t)(-1))) {
return log_error_and_throw(env, "decrypt_string",
"Failed to get input utf8 length");
"Failed to get input utf8 length");
}
if (unlikely(input_utf8_byte_length == 0)) {
return log_error_and_throw(env, "decrypt_string", "input is empty");
Expand All @@ -636,7 +662,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
}
if (unlikely(input_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt_string",
"Failed to allocate input cobhan buffer");
"Failed to allocate input cobhan buffer");
}

// Copy
Expand All @@ -646,7 +672,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
input_cobhan_buffer, &input_copied_bytes);
if (unlikely(input_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt_string",
"Failed to copy input to cobhan buffer");
"Failed to copy input to cobhan buffer");
}

char *output_cobhan_buffer;
Expand All @@ -667,7 +693,7 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
}
if (unlikely(output_cobhan_buffer == nullptr)) {
return log_error_and_throw(env, "decrypt_string",
"Failed to allocate cobhan output buffer");
"Failed to allocate cobhan output buffer");
}

if (unlikely(verbose_flag)) {
Expand All @@ -689,6 +715,11 @@ Napi::Value decrypt_string(const Napi::CallbackInfo &info) {
}

Napi::Value output = cbuffer_to_nstring(env, output_cobhan_buffer);

if (unlikely(verbose_flag)) {
debug_log("decrypt_string", "finished");
}

return output;
}

Expand Down Expand Up @@ -720,7 +751,7 @@ void set_max_stack_alloc_item_size(const Napi::CallbackInfo &info) {

if (unlikely(info.Length() < 1)) {
log_error_and_throw(env, "set_max_stack_alloc_item_size",
"Wrong number of arguments");
"Wrong number of arguments");
return;
}

Expand All @@ -738,7 +769,7 @@ void set_safety_padding_overhead(const Napi::CallbackInfo &info) {

if (unlikely(info.Length() < 1)) {
log_error_and_throw(env, "set_safety_padding_overhead",
"Wrong number of arguments");
"Wrong number of arguments");
return;
}

Expand Down
Loading
Loading