From e82d4416b413b2751839eed0b58ddf1ca6ec2e09 Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Sat, 25 May 2019 17:40:28 -0700 Subject: [PATCH] Add exceptions and error checking. --- src/cox.cr | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/cox.cr b/src/cox.cr index 96169fe..330844f 100644 --- a/src/cox.cr +++ b/src/cox.cr @@ -1,3 +1,15 @@ +require "random/secure" + +module Cox + class Error < ::Exception + end + class VerificationFailed < Error + end + class DecryptionFailed < Error + end +end + + require "./cox/*" module Cox @@ -5,7 +17,9 @@ module Cox data_buffer = data.to_slice data_size = data_buffer.bytesize output_buffer = Bytes.new(data_buffer.bytesize + LibSodium::MAC_BYTES) - LibSodium.crypto_box_easy(output_buffer.to_unsafe, data_buffer, data_size, nonce.pointer, recipient_public_key.pointer, sender_secret_key.pointer) + if LibSodium.crypto_box_easy(output_buffer.to_unsafe, data_buffer, data_size, nonce.pointer, recipient_public_key.pointer, sender_secret_key.pointer) != 0 + raise Error.new("crypto_box_easy") + end output_buffer end @@ -18,7 +32,9 @@ module Cox data_buffer = data.to_slice data_size = data_buffer.bytesize output_buffer = Bytes.new(data_buffer.bytesize - LibSodium::MAC_BYTES) - LibSodium.crypto_box_open_easy(output_buffer.to_unsafe, data_buffer.to_unsafe, data_size, nonce.pointer, sender_public_key.pointer, recipient_secret_key.pointer) + if LibSodium.crypto_box_open_easy(output_buffer.to_unsafe, data_buffer.to_unsafe, data_size, nonce.pointer, sender_public_key.pointer, recipient_secret_key.pointer) != 0 + raise DecryptionFailed.new("crypto_box_open_easy") + end output_buffer end @@ -27,7 +43,9 @@ module Cox message_buffer_size = message_buffer.bytesize signature_output_buffer = Bytes.new(LibSodium::SIGNATURE_BYTES) - LibSodium.crypto_sign_detached(signature_output_buffer.to_unsafe, 0, message_buffer.to_unsafe, message_buffer_size, secret_key.pointer) + if LibSodium.crypto_sign_detached(signature_output_buffer.to_unsafe, 0, message_buffer.to_unsafe, message_buffer_size, secret_key.pointer) != 0 + raise Error.new("crypto_sign_detached") + end signature_output_buffer end