Change *_LENGTH and *_BYTES constants to *_SIZE.

This commit is contained in:
Didactic Drunk 2019-06-25 09:29:16 -07:00
parent 7ea8ce856f
commit 1e0de420ad
11 changed files with 47 additions and 47 deletions

View File

@ -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")

View File

@ -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

View File

@ -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)

View File

@ -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),

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")

View File

@ -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)

View File

@ -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

View File

@ -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