Write test cases for the streaming API.

This commit is contained in:
Jesper Louis Andersen 2014-11-26 14:24:14 +01:00
parent 6354ea4f69
commit 6a707aef57
2 changed files with 48 additions and 1 deletions

View File

@ -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) ->

View File

@ -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()).