Add Blake2 testvectors and Blake2b tests
This commit is contained in:
parent
4bbf8dd004
commit
0b1c99cad1
18434
test/blake2_testvectors.json
Normal file
18434
test/blake2_testvectors.json
Normal file
File diff suppressed because it is too large
Load Diff
44
test/eblake2_tests.erl
Normal file
44
test/eblake2_tests.erl
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
%%%=============================================================================
|
||||||
|
%%% @copyright (C) 2019, Hans Svensson
|
||||||
|
%%% @doc
|
||||||
|
%%% Unit tests for the eblake2 module
|
||||||
|
%%%
|
||||||
|
%%% @end
|
||||||
|
%%%=============================================================================
|
||||||
|
-module(eblake2_tests).
|
||||||
|
-ifdef(TEST).
|
||||||
|
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
|
blake2b_test_() ->
|
||||||
|
{"Tests for BLAKE2b hash implementation",
|
||||||
|
[ fun() -> blake2b(TC) end || TC <- filter_test_vectors(<<"blake2b">>) ]}.
|
||||||
|
|
||||||
|
blake2b(_TC = #{in := Msg, key := Key, out := ExpectedOut}) ->
|
||||||
|
?assertEqual(eblake2:blake2b(byte_size(ExpectedOut), Msg, Key), {ok, ExpectedOut}).
|
||||||
|
|
||||||
|
%% Helper functions
|
||||||
|
test_vectors() ->
|
||||||
|
parse_test_vectors("test/blake2_testvectors.json").
|
||||||
|
|
||||||
|
parse_test_vectors(File) ->
|
||||||
|
{ok, Bin} = file:read_file(File),
|
||||||
|
Vectors = jsx:decode(Bin, [{labels, atom}, return_maps]),
|
||||||
|
HexToBin = fun(<<>>) -> <<>>;
|
||||||
|
(HexStrBin) ->
|
||||||
|
Size = byte_size(HexStrBin) div 2,
|
||||||
|
<<(binary_to_integer(HexStrBin, 16)):Size/unit:8>>
|
||||||
|
end,
|
||||||
|
FixBin = fun(TC = #{ in := In0, key := Key0, out := Out0 }) ->
|
||||||
|
TC#{ in := HexToBin(In0),
|
||||||
|
key := HexToBin(Key0),
|
||||||
|
out := HexToBin(Out0) }
|
||||||
|
end,
|
||||||
|
lists:map(FixBin, Vectors).
|
||||||
|
|
||||||
|
filter_test_vectors(Algo) ->
|
||||||
|
[ X || X = #{ hash := Algo1 } <- test_vectors(), Algo == Algo1 ].
|
||||||
|
|
||||||
|
-endif.
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user