From 24c5a4170d90678dd929f3204329056794d07aea Mon Sep 17 00:00:00 2001 From: Didactic Drunk <1479616+didactic-drunk@users.noreply.github.com> Date: Sun, 8 May 2022 13:21:32 -0700 Subject: [PATCH] Fix Chalsa NONCE_SIZE --- examples/constants.cr | 17 ++++++++++++----- src/sodium/cipher/chalsa.cr | 10 ++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/examples/constants.cr b/examples/constants.cr index 9c05ae3..da56287 100644 --- a/examples/constants.cr +++ b/examples/constants.cr @@ -17,6 +17,14 @@ puts "" {% end %} puts "" +{% for sk in [Sodium::CryptoBox::SecretKey, Sodium::Sign::SecretKey] %} + sk = {{sk.id}}.new + pk = sk.public_key +# puts "#{sk.class} bytesize #{sk.to_slice.bytesize}" + puts "#{pk.class} bytesize #{pk.to_slice.bytesize}" +{% end %} +puts "" + {% for name in %w(KEY_SIZE NONCE_SIZE MAC_SIZE) %} puts "Sodium::SecretBox::{{ name.id }} #{Sodium::SecretBox::{{ name.id }}}" {% end %} @@ -47,9 +55,8 @@ puts "" {% end %} puts "" -{% for sk in [Sodium::CryptoBox::SecretKey, Sodium::Sign::SecretKey] %} - sk = {{sk.id}}.new - pk = sk.public_key - puts "#{sk.class} bytesize #{sk.to_slice.bytesize}" - puts "#{pk.class} bytesize #{pk.to_slice.bytesize}" +{% for name in %w(XChaCha20 ChaCha20Ietf ChaCha20 XSalsa20 Salsa20) %} + c = Sodium::Cipher::{{name.id}}.random +# puts "#{c.class} key_size #{c.key_size}" + puts "#{c.class} nonce_size #{c.nonce_size}" {% end %} diff --git a/src/sodium/cipher/chalsa.cr b/src/sodium/cipher/chalsa.cr index bd76005..053f065 100644 --- a/src/sodium/cipher/chalsa.cr +++ b/src/sodium/cipher/chalsa.cr @@ -14,7 +14,7 @@ module Sodium::Cipher def initialize(key : Crypto::Secret | Bytes, nonce = nil) raise ArgumentError.new("key must be #{key_size} bytes, got #{key.bytesize}") if key.bytesize != key_size - @key = key.is_a?(Crypto::Secret) ? key : Sodium::SecureBuffer.new(key) + @key = key.is_a?(Crypto::Secret) ? key : Sodium::SecureBuffer.copy_from(key) self.nonce = nonce if nonce end @@ -87,9 +87,7 @@ module Sodium::Cipher end end - {% for key, valtup in {"XSalsa20" => {"xsalsa20", false}, "Salsa20" => {"salsa20", false}, "XChaCha20" => {"xchacha20", false}, "ChaCha20Ietf" => {"chacha20_ietf", true}, "ChaCha20" => {"chacha20", false}} %} - {% val = valtup[0] %} - {% ietf = valtup[1] %} + {% for key, val in {"XSalsa20" => "xsalsa20", "Salsa20" => "salsa20", "XChaCha20" => "xchacha20", "ChaCha20Ietf" => "chacha20_ietf", "ChaCha20" => "chacha20"} %} # These classes can be used to generate pseudo-random data from a key, # or as building blocks for implementing custom constructions, but they # are not alternatives to secretbox. @@ -102,8 +100,8 @@ module Sodium::Cipher # # WARNING: Not validated against test vectors. You should probably write some before using this class. class {{ key.id }} < Chalsa - KEY_SIZE = LibSodium.crypto_stream_chacha20_{{ ietf ? "ietf_".id : "".id }}keybytes.to_i32 - NONCE_SIZE = LibSodium.crypto_stream_chacha20_{{ ietf ? "ietf_".id : "".id }}noncebytes.to_i32 + KEY_SIZE = LibSodium.crypto_stream_{{ val.id }}_keybytes.to_i32 + NONCE_SIZE = LibSodium.crypto_stream_{{ val.id }}_noncebytes.to_i32 def self.random new key: Sodium::SecureBuffer.random(KEY_SIZE), nonce: Random::Secure.random_bytes(NONCE_SIZE)