Skip to content

Commit

Permalink
Switch .to_unsafe to .to_slice
Browse files Browse the repository at this point in the history
Remove use of .pointer
  • Loading branch information
didactic-drunk committed Jun 28, 2019
1 parent be5b250 commit a0f15b7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 34 deletions.
25 changes: 25 additions & 0 deletions scripts/git/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /bin/sh
#
# This script ensures Crystal code is correctly formatted before committing it.
# It won't apply any format changes automatically.
#
# Only staged files (the ones to be committed) are being processed, but each file is checked
# entirely as it is stored on disc, even parts that are not staged.
#
# To use this script, it needs to be installed in the local git repository. For example by running
# `ln -s scripts/git/pre-commit .git/hooks` in the root folder.
#
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.

changed_cr_files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.cr$')

[ -z "$changed_cr_files" ] && exit 0

if [ -x bin/crystal ]; then
# use bin/crystal wrapper when available to run local compiler build
exec bin/crystal tool format --check $changed_cr_files >&2
else
exec crystal tool format --check $changed_cr_files >&2
fi
13 changes: 6 additions & 7 deletions src/cox.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ module Cox
end
end

require "./cox/*"
require "./cox/**"

module Cox
def self.encrypt(data, nonce : Nonce, recipient_public_key : PublicKey, sender_secret_key : SecretKey)
data_buffer = data.to_slice
data_size = data_buffer.bytesize
output_buffer = Bytes.new(data_buffer.bytesize + LibSodium::MAC_SIZE)
if LibSodium.crypto_box_easy(output_buffer.to_unsafe, data_buffer, data_size, nonce.pointer, recipient_public_key.pointer, sender_secret_key.pointer) != 0
if LibSodium.crypto_box_easy(output_buffer.to_slice, data_buffer, data_size, nonce.to_slice, recipient_public_key.to_slice, sender_secret_key.to_slice) != 0
raise Error.new("crypto_box_easy")
end
output_buffer
Expand All @@ -33,7 +33,7 @@ module Cox
data_buffer = data.to_slice
data_size = data_buffer.bytesize
output_buffer = Bytes.new(data_buffer.bytesize - LibSodium::MAC_SIZE)
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
if LibSodium.crypto_box_open_easy(output_buffer.to_slice, data_buffer.to_slice, data_size, nonce.to_slice, sender_public_key.to_slice, recipient_secret_key.to_slice) != 0
raise DecryptionFailed.new("crypto_box_open_easy")
end
output_buffer
Expand All @@ -44,7 +44,7 @@ module Cox
message_buffer_size = message_buffer.bytesize
signature_output_buffer = Bytes.new(LibSodium::SIGNATURE_SIZE)

if LibSodium.crypto_sign_detached(signature_output_buffer.to_unsafe, 0, message_buffer.to_unsafe, message_buffer_size, secret_key.pointer) != 0
if LibSodium.crypto_sign_detached(signature_output_buffer.to_slice, 0, message_buffer.to_slice, message_buffer_size, secret_key.to_slice) != 0
raise Error.new("crypto_sign_detached")
end
signature_output_buffer
Expand All @@ -55,12 +55,11 @@ module Cox
message_buffer = message.to_slice
message_buffer_size = message_buffer.bytesize

verified = LibSodium.crypto_sign_verify_detached(signature_buffer.to_unsafe, message_buffer.to_unsafe, message_buffer_size, public_key.pointer)
verified = LibSodium.crypto_sign_verify_detached(signature_buffer.to_slice, message_buffer.to_slice, message_buffer_size, public_key.to_slice)
verified.zero?
end
end

if Cox::LibSodium.sodium_init == -1
STDERR.puts("Failed to init libsodium")
exit(1)
abort "Failed to init libsodium"
end
10 changes: 2 additions & 8 deletions src/cox/kdf.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Cox
class Kdf
property bytes : Bytes

delegate to_slice, to: @bytes

def initialize(bytes : Bytes)
if bytes.bytesize != LibSodium::KDF_KEY_SIZE
raise ArgumentError.new("bytes must be #{LibSodium::KDF_KEY_SIZE}, got #{bytes.bytesize}")
Expand All @@ -28,14 +30,6 @@ module Cox
subkey
end

def pointer
bytes.to_unsafe
end

def pointer(size)
bytes.pointer(size)
end

def to_base64
Base64.encode(bytes)
end
Expand Down
8 changes: 1 addition & 7 deletions src/cox/key.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ module Cox
abstract class Key
abstract def bytes

def pointer
bytes.to_unsafe
end

def pointer(size)
bytes.pointer(size)
end
delegate to_slice, to: @bytes

def to_base64
Base64.encode(bytes)
Expand Down
13 changes: 3 additions & 10 deletions src/cox/nonce.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ require "random/secure"

module Cox
class Nonce
property bytes : Bytes

NONCE_SIZE = LibSodium::NONCE_SIZE

property bytes : Bytes
delegate to_slice, to: @bytes

def initialize(@bytes : Bytes)
if bytes.bytesize != NONCE_SIZE
raise ArgumentError.new("Nonce must be #{NONCE_SIZE} bytes, got #{bytes.bytesize}")
Expand All @@ -16,13 +17,5 @@ module Cox
def self.new
new(Random::Secure.random_bytes(NONCE_SIZE))
end

def pointer
bytes.to_unsafe
end

def pointer(size)
bytes.pointer(size)
end
end
end
4 changes: 2 additions & 2 deletions src/cox/secret_key.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module Cox
if dst.bytesize != (src.bytesize + MAC_SIZE)
raise ArgumentError.new("dst.bytesize must be src.bytesize + MAC_SIZE, got #{dst.bytesize}")
end
if LibSodium.crypto_secretbox_easy(dst, src, src.bytesize, nonce.pointer, @bytes) != 0
if LibSodium.crypto_secretbox_easy(dst, src, src.bytesize, nonce.to_slice, @bytes) != 0
raise Cox::Error.new("crypto_secretbox_easy")
end
dst
Expand All @@ -57,7 +57,7 @@ module Cox
if dst.bytesize != (src.bytesize - MAC_SIZE)
raise ArgumentError.new("dst.bytesize must be src.bytesize - MAC_SIZE, got #{dst.bytesize}")
end
if LibSodium.crypto_secretbox_open_easy(dst, src, src.bytesize, nonce.pointer, @bytes) != 0
if LibSodium.crypto_secretbox_open_easy(dst, src, src.bytesize, nonce.to_slice, @bytes) != 0
raise Cox::DecryptionFailed.new("crypto_secretbox_easy")
end
dst
Expand Down

0 comments on commit a0f15b7

Please sign in to comment.