From 1e0de420ad61cccdfc71197c6097e15edcd20e75 Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Tue, 25 Jun 2019 09:29:16 -0700 Subject: [PATCH] Change *_LENGTH and *_BYTES constants to *_SIZE. --- src/cox.cr | 6 +++--- src/cox/kdf.cr | 10 +++++----- src/cox/key_pair.cr | 4 ++-- src/cox/lib_sodium.cr | 20 ++++++++++---------- src/cox/nonce.cr | 8 ++++---- src/cox/public_key.cr | 6 +++--- src/cox/pwhash.cr | 2 +- src/cox/secret_key.cr | 22 +++++++++++----------- src/cox/sign_key_pair.cr | 4 ++-- src/cox/sign_public_key.cr | 6 +++--- src/cox/sign_secret_key.cr | 6 +++--- 11 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/cox.cr b/src/cox.cr index 330844f..b656f22 100644 --- a/src/cox.cr +++ b/src/cox.cr @@ -16,7 +16,7 @@ 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_BYTES) + 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 raise Error.new("crypto_box_easy") end @@ -31,7 +31,7 @@ module Cox def self.decrypt(data, nonce : Nonce, sender_public_key : PublicKey, recipient_secret_key : SecretKey) data_buffer = data.to_slice data_size = data_buffer.bytesize - output_buffer = Bytes.new(data_buffer.bytesize - LibSodium::MAC_BYTES) + 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 raise DecryptionFailed.new("crypto_box_open_easy") end @@ -41,7 +41,7 @@ module Cox def self.sign_detached(message, secret_key : SignSecretKey) message_buffer = message.to_slice message_buffer_size = message_buffer.bytesize - signature_output_buffer = Bytes.new(LibSodium::SIGNATURE_BYTES) + 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 raise Error.new("crypto_sign_detached") diff --git a/src/cox/kdf.cr b/src/cox/kdf.cr index 60fd277..5d333b7 100644 --- a/src/cox/kdf.cr +++ b/src/cox/kdf.cr @@ -3,22 +3,22 @@ module Cox property bytes : Bytes def initialize(bytes : Bytes) - if bytes.bytesize != LibSodium::KDF_KEY_BYTES - raise ArgumentError.new("bytes must be #{LibSodium::KDF_KEY_BYTES}, got #{bytes.bytesize}") + if bytes.bytesize != LibSodium::KDF_KEY_SIZE + raise ArgumentError.new("bytes must be #{LibSodium::KDF_KEY_SIZE}, got #{bytes.bytesize}") end @bytes = bytes end def initialize - @bytes = Random::Secure.random_bytes(LibSodium::KDF_KEY_BYTES) + @bytes = Random::Secure.random_bytes(LibSodium::KDF_KEY_SIZE) end # context must be 8 bytes # subkey_size must be 16..64 bytes as of libsodium 1.0.17 def derive(context, subkey_size, subkey_id = 0) - if context.bytesize != LibSodium::KDF_CONTEXT_BYTES - raise ArgumentError.new("context must be #{LibSodium::KDF_CONTEXT_BYTES}, got #{context.bytesize}") + if context.bytesize != LibSodium::KDF_CONTEXT_SIZE + raise ArgumentError.new("context must be #{LibSodium::KDF_CONTEXT_SIZE}, got #{context.bytesize}") end subkey = Bytes.new subkey_size diff --git a/src/cox/key_pair.cr b/src/cox/key_pair.cr index e0c55dd..3bea256 100644 --- a/src/cox/key_pair.cr +++ b/src/cox/key_pair.cr @@ -13,8 +13,8 @@ module Cox end def self.new - public_key = Bytes.new(PublicKey::KEY_LENGTH) - secret_key = Bytes.new(SecretKey::KEY_LENGTH) + public_key = Bytes.new(PublicKey::KEY_SIZE) + secret_key = Bytes.new(SecretKey::KEY_SIZE) LibSodium.crypto_box_keypair(public_key.to_unsafe, secret_key.to_unsafe) diff --git a/src/cox/lib_sodium.cr b/src/cox/lib_sodium.cr index 9303fbe..11d7312 100644 --- a/src/cox/lib_sodium.cr +++ b/src/cox/lib_sodium.cr @@ -31,16 +31,16 @@ module Cox fun crypto_generichash_blake2b_saltbytes : LibC::SizeT fun crypto_generichash_blake2b_personalbytes : LibC::SizeT - PUBLIC_KEY_BYTES = crypto_box_publickeybytes() - SECRET_KEY_BYTES = crypto_box_secretkeybytes() - NONCE_BYTES = crypto_box_noncebytes() - MAC_BYTES = crypto_box_macbytes() - PUBLIC_SIGN_BYTES = crypto_sign_publickeybytes() - SECRET_SIGN_BYTES = crypto_sign_secretkeybytes() - SIGNATURE_BYTES = crypto_sign_bytes() - KDF_KEY_BYTES = crypto_kdf_keybytes() - KDF_CONTEXT_BYTES = crypto_kdf_contextbytes() - PWHASH_STR_BYTES = crypto_pwhash_strbytes() + PUBLIC_KEY_SIZE = crypto_box_publickeybytes() + SECRET_KEY_SIZE = crypto_box_secretkeybytes() + NONCE_SIZE = crypto_box_noncebytes() + MAC_SIZE = crypto_box_macbytes() + PUBLIC_SIGN_SIZE = crypto_sign_publickeybytes() + SECRET_SIGN_SIZE = crypto_sign_secretkeybytes() + SIGNATURE_SIZE = crypto_sign_bytes() + KDF_KEY_SIZE = crypto_kdf_keybytes() + KDF_CONTEXT_SIZE = crypto_kdf_contextbytes() + PWHASH_STR_SIZE = crypto_pwhash_strbytes() fun crypto_secretbox_easy( output : Pointer(LibC::UChar), diff --git a/src/cox/nonce.cr b/src/cox/nonce.cr index 9c1af28..829448f 100644 --- a/src/cox/nonce.cr +++ b/src/cox/nonce.cr @@ -5,16 +5,16 @@ module Cox class Nonce property bytes : Bytes - NONCE_LENGTH = LibSodium::NONCE_BYTES + NONCE_SIZE = LibSodium::NONCE_SIZE def initialize(@bytes : Bytes) - if bytes.bytesize != NONCE_LENGTH - raise ArgumentError.new("Nonce must be #{NONCE_LENGTH} bytes, got #{bytes.bytesize}") + if bytes.bytesize != NONCE_SIZE + raise ArgumentError.new("Nonce must be #{NONCE_SIZE} bytes, got #{bytes.bytesize}") end end def self.new - new(Random::Secure.random_bytes(NONCE_LENGTH)) + new(Random::Secure.random_bytes(NONCE_SIZE)) end def pointer diff --git a/src/cox/public_key.cr b/src/cox/public_key.cr index 982a074..57feaf8 100644 --- a/src/cox/public_key.cr +++ b/src/cox/public_key.cr @@ -4,11 +4,11 @@ module Cox class PublicKey < Key property bytes : Bytes - KEY_LENGTH = LibSodium::PUBLIC_KEY_BYTES + KEY_SIZE = LibSodium::PUBLIC_KEY_SIZE def initialize(@bytes : Bytes) - if bytes.bytesize != KEY_LENGTH - raise ArgumentError.new("Public key must be #{KEY_LENGTH} bytes, got #{bytes.bytesize}") + if bytes.bytesize != KEY_SIZE + raise ArgumentError.new("Public key must be #{KEY_SIZE} bytes, got #{bytes.bytesize}") end end end diff --git a/src/cox/pwhash.cr b/src/cox/pwhash.cr index 944da48..fb933c8 100644 --- a/src/cox/pwhash.cr +++ b/src/cox/pwhash.cr @@ -17,7 +17,7 @@ module Cox property memlimit = MEMLIMIT_INTERACTIVE def hash_str(pass) - outstr = Bytes.new LibSodium::PWHASH_STR_BYTES + outstr = Bytes.new LibSodium::PWHASH_STR_SIZE if LibSodium.crypto_pwhash_str(outstr, pass, pass.bytesize, @opslimit, @memlimit) != 0 raise Cox::Error.new("crypto_pwhash_str") end diff --git a/src/cox/secret_key.cr b/src/cox/secret_key.cr index bc50276..10960af 100644 --- a/src/cox/secret_key.cr +++ b/src/cox/secret_key.cr @@ -4,17 +4,17 @@ module Cox class SecretKey < Key property bytes : Bytes - KEY_LENGTH = LibSodium::SECRET_KEY_BYTES - MAC_BYTES = LibSodium::MAC_BYTES + KEY_SIZE = LibSodium::SECRET_KEY_SIZE + MAC_SIZE = LibSodium::MAC_SIZE def initialize(@bytes : Bytes) - if bytes.bytesize != KEY_LENGTH - raise ArgumentError.new("Secret key must be #{KEY_LENGTH} bytes, got #{bytes.bytesize}") + if bytes.bytesize != KEY_SIZE + raise ArgumentError.new("Secret key must be #{KEY_SIZE} bytes, got #{bytes.bytesize}") end end def self.random - new Random::Secure.random_bytes(KEY_LENGTH) + new Random::Secure.random_bytes(KEY_SIZE) end def encrypt_easy(data) @@ -32,13 +32,13 @@ module Cox end def encrypt_easy(data : Bytes, nonce : Nonce) : Bytes - output = Bytes.new(data.bytesize + MAC_BYTES) + output = Bytes.new(data.bytesize + MAC_SIZE) encrypt_easy(data, output, nonce) end def encrypt_easy(src : Bytes, dst : Bytes, nonce : Nonce) : Bytes - if dst.bytesize != (src.bytesize + MAC_BYTES) - raise ArgumentError.new("dst.bytesize must be src.bytesize + MAC_BYTES, got #{dst.bytesize}") + 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 raise Cox::Error.new("crypto_secretbox_easy") @@ -47,15 +47,15 @@ module Cox end def decrypt_easy(data : Bytes, nonce : Nonce) : Bytes - output_size = data.bytesize - MAC_BYTES + output_size = data.bytesize - MAC_SIZE raise Cox::DecryptionFailed.new("encrypted data too small #{data.bytesize}") if output_size <= 0 output = Bytes.new output_size decrypt_easy(data, output, nonce) end def decrypt_easy(src : Bytes, dst : Bytes, nonce : Nonce) : Bytes - if dst.bytesize != (src.bytesize - MAC_BYTES) - raise ArgumentError.new("dst.bytesize must be src.bytesize - MAC_BYTES, got #{dst.bytesize}") + 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 raise Cox::DecryptionFailed.new("crypto_secretbox_easy") diff --git a/src/cox/sign_key_pair.cr b/src/cox/sign_key_pair.cr index 0b64882..f58b00e 100644 --- a/src/cox/sign_key_pair.cr +++ b/src/cox/sign_key_pair.cr @@ -13,8 +13,8 @@ module Cox end def self.new - public_key = Bytes.new(SignPublicKey::KEY_LENGTH) - secret_key = Bytes.new(SignSecretKey::KEY_LENGTH) + public_key = Bytes.new(SignPublicKey::KEY_SIZE) + secret_key = Bytes.new(SignSecretKey::KEY_SIZE) LibSodium.crypto_sign_keypair(public_key.to_unsafe, secret_key.to_unsafe) diff --git a/src/cox/sign_public_key.cr b/src/cox/sign_public_key.cr index 030c3c7..64fcfba 100644 --- a/src/cox/sign_public_key.cr +++ b/src/cox/sign_public_key.cr @@ -4,11 +4,11 @@ module Cox class SignPublicKey < Key property bytes : Bytes - KEY_LENGTH = LibSodium::PUBLIC_SIGN_BYTES + KEY_SIZE = LibSodium::PUBLIC_SIGN_SIZE def initialize(@bytes : Bytes) - if bytes.bytesize != KEY_LENGTH - raise ArgumentError.new("Public key must be #{KEY_LENGTH} bytes, got #{bytes.bytesize}") + if bytes.bytesize != KEY_SIZE + raise ArgumentError.new("Public key must be #{KEY_SIZE} bytes, got #{bytes.bytesize}") end end end diff --git a/src/cox/sign_secret_key.cr b/src/cox/sign_secret_key.cr index 332099b..be36c0c 100644 --- a/src/cox/sign_secret_key.cr +++ b/src/cox/sign_secret_key.cr @@ -4,11 +4,11 @@ module Cox class SignSecretKey < Key property bytes : Bytes - KEY_LENGTH = LibSodium::SECRET_SIGN_BYTES + KEY_SIZE = LibSodium::SECRET_SIGN_SIZE def initialize(@bytes : Bytes) - if bytes.bytesize != KEY_LENGTH - raise ArgumentError.new("Secret key must be #{KEY_LENGTH} bytes, got #{bytes.bytesize}") + if bytes.bytesize != KEY_SIZE + raise ArgumentError.new("Secret key must be #{KEY_SIZE} bytes, got #{bytes.bytesize}") end end end