Commit 6a707aef authored by Jesper Louis Andersen's avatar Jesper Louis Andersen
Browse files

Write test cases for the streaming API.

parent 6354ea4f
......@@ -177,6 +177,42 @@ prop_secretbox_failure_integrity() ->
equals(Err, {error, failed_verification})
end).
prop_stream_correct() ->
?FORALL({Len, Nonce, Key},
{int(),
fault_rate(1, 40, nonce()),
fault_rate(1, 40, secret_key())},
case Len >= 0 andalso nonce_valid(Nonce) andalso secret_key_valid(Key) of
true ->
CipherStream = enacl:stream(Len, Nonce, Key),
equals(Len, byte_size(CipherStream));
false ->
try
enacl:stream(Len, Nonce, Key),
false
catch
error:badarg -> true
end
end).
prop_stream_xor_correct() ->
?FORALL({Msg, Nonce, Key},
{binary(),
fault_rate(1, 40, nonce()),
fault_rate(1, 40, secret_key())},
case nonce_valid(Nonce) andalso secret_key_valid(Key) of
true ->
CipherText = enacl:stream_xor(Msg, Nonce, Key),
equals(Msg, enacl:stream_xor(CipherText, Nonce, Key));
false ->
try
enacl:stream_xor(Msg, Nonce, Key),
false
catch
error:badarg -> true
end
end).
%% HASHING
%% ---------------------------
diff_pair(Sz) ->
......
......@@ -31,9 +31,13 @@
secretbox/3,
secretbox_open/3,
secretbox_nonce_size/0,
secretbox_key_size/0
secretbox_key_size/0,
stream/3,
stream_xor/3
]).
%% Low-level functions
-export([
hash/1,
verify_16/2,
......@@ -145,6 +149,13 @@ secretbox_nonce_size() ->
secretbox_key_size() ->
enacl_nif:crypto_secretbox_KEYBYTES().
stream(Len, Nonce, Key) when is_integer(Len), Len >= 0 ->
enacl_nif:crypto_stream(Len, Nonce, Key);
stream(_, _, _) -> error(badarg).
stream_xor(Msg, Nonce, Key) ->
enacl_nif:crypto_stream_xor(Msg, Nonce, Key).
%% Helpers
p_zerobytes() ->
binary:copy(<<0>>, enacl_nif:crypto_box_ZEROBYTES()).
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment