Sodium::Digest::Blake2b Log.warn on small key size.

master
Didactic Drunk 2020-06-28 16:32:52 -07:00
parent f1a225b03b
commit 8134714804
2 changed files with 12 additions and 1 deletions

View File

@ -1,10 +1,18 @@
require "benchmark" require "benchmark"
require "option_parser"
require "../src/sodium" require "../src/sodium"
require "openssl" require "openssl"
require "openssl/digest" require "openssl/digest"
output_size = 64 output_size = 64
sizes = [16, 64, 256, 1024, 8192, 16384] sizes = [16, 64, 256, 1024, 8192, 16384]
optp = OptionParser.new
optp.on("--output-size", "default: 64") { |arg| output_size = arg.to_i }
optp.on("--input-sizes=ARG", "comma separated list of input sizes") { |arg| sizes = arg.split(",").map(&.to_i).to_a }
# optp.on("", "") { |arg| }
optp.parse
bufs = sizes.map { |size| Bytes.new size }.to_a bufs = sizes.map { |size| Bytes.new size }.to_a
puts "Compare against 'openssl speed digestname'" puts "Compare against 'openssl speed digestname'"

View File

@ -61,9 +61,12 @@ module Sodium::Digest
# `key`, `salt`, and `personal` are all optional. Many other libsodium bindings don't support them. # `key`, `salt`, and `personal` are all optional. Many other libsodium bindings don't support them.
# Check the other implementation(s) you need to interoperate with before using. # Check the other implementation(s) you need to interoperate with before using.
def initialize(@digest_size : Int32 = OUT_SIZE, key : Bytes? | SecureBuffer? = nil, salt : Bytes? = nil, personal : Bytes? = nil) def initialize(@digest_size : Int32 = OUT_SIZE, key : Bytes? | SecureBuffer? = nil, salt : Bytes? = nil, personal : Bytes? = nil)
if k = key if (k = key) && k.bytesize > 0
k = k.to_slice k = k.to_slice
raise ArgumentError.new("key larger than KEY_SIZE_MAX(#{KEY_SIZE_MAX}), got #{k.bytesize}") if k.bytesize > KEY_SIZE_MAX raise ArgumentError.new("key larger than KEY_SIZE_MAX(#{KEY_SIZE_MAX}), got #{k.bytesize}") if k.bytesize > KEY_SIZE_MAX
# Test vectors contain small key sizes. Small keys shouldn't be used... Wtf?
Log.warn &.emit("key smaller than KEY_SIZE_MIN(#{KEY_SIZE_MIN}), got #{k.bytesize}") if k.bytesize < KEY_SIZE_MIN
# raise ArgumentError.new("key smaller than KEY_SIZE_MIN(#{KEY_SIZE_MIN}), got #{k.bytesize}") if k.bytesize < KEY_SIZE_MIN
@key_size = k.bytesize @key_size = k.bytesize
k.copy_to @key.to_slice k.copy_to @key.to_slice
end end