2019-06-30 02:21:00 +02:00
|
|
|
require "../../spec_helper"
|
2019-07-01 15:24:26 +02:00
|
|
|
require "../../../src/sodium/digest/blake2b"
|
2019-06-30 08:47:54 +02:00
|
|
|
require "json"
|
2019-05-29 22:31:54 +02:00
|
|
|
|
2019-06-30 08:47:54 +02:00
|
|
|
# From https://github.com/BLAKE2/BLAKE2/tree/master/testvectors
|
2019-05-29 22:31:54 +02:00
|
|
|
test_vectors = [
|
|
|
|
{
|
2019-06-28 02:20:02 +02:00
|
|
|
key: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
|
|
|
|
input: "",
|
2019-05-29 22:31:54 +02:00
|
|
|
output: "10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568",
|
|
|
|
},
|
|
|
|
{
|
2019-06-28 02:20:02 +02:00
|
|
|
key: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f",
|
|
|
|
input: "00",
|
2019-05-29 22:31:54 +02:00
|
|
|
output: "961f6dd1e4dd30f63901690c512e78e4b45e4742ed197c3c5e45c549fd25f2e4187b0bc9fe30492b16b0d0bc4ef9b0f34c7003fac09a5ef1532e69430234cebd",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2019-06-30 08:47:54 +02:00
|
|
|
# From https://github.com/emilbayes/blake2b/blob/master/test-vectors.json
|
|
|
|
buf = File.read(Path[__DIR__].join("blake2b-test-vectors.json").to_s)
|
|
|
|
more_vectors = Array(Hash(String, String | Int32)).from_json(buf).map do |h|
|
|
|
|
{
|
|
|
|
input: h["input"].to_s,
|
|
|
|
output: h["out"].to_s,
|
|
|
|
out_len: h["outlen"].to_i,
|
|
|
|
key: h["key"].to_s,
|
|
|
|
salt: h["salt"].to_s,
|
|
|
|
personal: h["personal"].to_s,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
describe Sodium::Digest::Blake2b do
|
2019-05-29 22:31:54 +02:00
|
|
|
it "test vectors" do
|
|
|
|
test_vectors.each do |vec|
|
2019-06-29 01:17:09 +02:00
|
|
|
d = Sodium::Digest::Blake2b.new 64, key: vec[:key].hexbytes
|
2019-05-29 22:31:54 +02:00
|
|
|
d.update vec[:input].hexbytes
|
2020-05-19 08:13:50 +02:00
|
|
|
d.hexfinal.should eq vec[:output]
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|
2019-06-30 08:47:54 +02:00
|
|
|
|
|
|
|
more_vectors.each do |vec|
|
|
|
|
salt = vec[:salt].empty? ? nil : vec[:salt].hexbytes
|
|
|
|
personal = vec[:personal].empty? ? nil : vec[:personal].hexbytes
|
|
|
|
d = Sodium::Digest::Blake2b.new vec[:out_len], key: vec[:key].hexbytes, salt: salt, personal: personal
|
|
|
|
d.update vec[:input].hexbytes
|
2020-05-19 08:13:50 +02:00
|
|
|
d.hexfinal.should eq vec[:output]
|
2019-06-30 08:47:54 +02:00
|
|
|
end
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "produces different output with different salt or personal params" do
|
2019-06-29 01:17:09 +02:00
|
|
|
key = Bytes.new Sodium::Digest::Blake2b::KEY_SIZE
|
|
|
|
salt = Bytes.new Sodium::Digest::Blake2b::SALT_SIZE
|
|
|
|
salt2 = Bytes.new Sodium::Digest::Blake2b::SALT_SIZE
|
2019-05-29 22:31:54 +02:00
|
|
|
salt2 = salt.dup
|
|
|
|
salt2[0] = 1
|
2019-06-29 01:17:09 +02:00
|
|
|
personal = Bytes.new Sodium::Digest::Blake2b::PERSONAL_SIZE
|
2019-05-29 22:31:54 +02:00
|
|
|
personal2 = personal.dup
|
|
|
|
personal2[0] = 1
|
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
d = Sodium::Digest::Blake2b.new key: key, salt: salt, personal: personal
|
2019-05-29 22:31:54 +02:00
|
|
|
d.update "foo".to_slice
|
2020-05-19 08:13:50 +02:00
|
|
|
output = d.hexfinal
|
2019-05-29 22:31:54 +02:00
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
d = Sodium::Digest::Blake2b.new key: key, salt: salt2, personal: personal
|
2019-05-29 22:31:54 +02:00
|
|
|
d.update "foo".to_slice
|
2020-05-19 08:13:50 +02:00
|
|
|
saltout = d.hexfinal
|
2019-05-29 22:31:54 +02:00
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
d = Sodium::Digest::Blake2b.new key: key, salt: salt, personal: personal2
|
2019-05-29 22:31:54 +02:00
|
|
|
d.update "foo".to_slice
|
2020-05-19 08:13:50 +02:00
|
|
|
personalout = d.hexfinal
|
2019-05-29 22:31:54 +02:00
|
|
|
|
|
|
|
output.should_not eq saltout
|
|
|
|
output.should_not eq personalout
|
|
|
|
saltout.should_not eq personalout
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises on invalid " do
|
|
|
|
expect_raises ArgumentError do
|
2019-06-29 01:17:09 +02:00
|
|
|
Sodium::Digest::Blake2b.new key: Bytes.new(128)
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
expect_raises ArgumentError do
|
2019-06-29 01:17:09 +02:00
|
|
|
Sodium::Digest::Blake2b.new salt: Bytes.new(1)
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
expect_raises ArgumentError do
|
2019-06-29 01:17:09 +02:00
|
|
|
Sodium::Digest::Blake2b.new salt: Bytes.new(128)
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
expect_raises ArgumentError do
|
2019-06-29 01:17:09 +02:00
|
|
|
Sodium::Digest::Blake2b.new personal: Bytes.new(128)
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|
2020-06-29 01:30:17 +02:00
|
|
|
|
|
|
|
d = Sodium::Digest::Blake2b.new
|
|
|
|
expect_raises ArgumentError do
|
|
|
|
d.hexfinal Bytes.new(1)
|
|
|
|
end
|
|
|
|
expect_raises ArgumentError do
|
|
|
|
d.hexfinal Bytes.new(256)
|
|
|
|
end
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|
2019-11-25 15:44:33 +01:00
|
|
|
|
2020-06-29 01:30:17 +02:00
|
|
|
it "can't final twice or update after final" do
|
|
|
|
d = Sodium::Digest::Blake2b.new
|
|
|
|
d.hexfinal
|
|
|
|
|
|
|
|
expect_raises Digest::FinalizedError do
|
|
|
|
d.hexfinal
|
|
|
|
end
|
|
|
|
|
|
|
|
expect_raises Digest::FinalizedError do
|
|
|
|
d.update Bytes.new(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "dups" do
|
|
|
|
data = "foo".to_slice
|
|
|
|
d1 = Sodium::Digest::Blake2b.new
|
|
|
|
d2 = d1.dup
|
|
|
|
d3 = d2.dup
|
|
|
|
|
|
|
|
d1.update data
|
|
|
|
h1 = d1.hexfinal
|
|
|
|
|
|
|
|
d2.update data
|
|
|
|
h2 = d2.hexfinal
|
|
|
|
|
|
|
|
d3.update data
|
|
|
|
h3 = d3.hexfinal
|
|
|
|
|
|
|
|
h1.should eq h2
|
|
|
|
h1.should eq h3
|
2019-11-25 15:44:33 +01:00
|
|
|
end
|
2019-05-29 22:31:54 +02:00
|
|
|
end
|