From f64e62d16e6d867bbe43d37bac032cbbbe51637a Mon Sep 17 00:00:00 2001 From: Jesper Louis Andersen <jesper.louis.andersen@gmail.com> Date: Tue, 25 Nov 2014 14:10:24 +0100 Subject: [PATCH] Provide correctness test cases for the verification codes. --- eqc_test/enacl_eqc.erl | 50 ++++++++++++++++++++++++++++++++++++++++++ src/enacl.erl | 14 +++++++++--- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/eqc_test/enacl_eqc.erl b/eqc_test/enacl_eqc.erl index 48ecb82..4062501 100644 --- a/eqc_test/enacl_eqc.erl +++ b/eqc_test/enacl_eqc.erl @@ -216,3 +216,53 @@ prop_crypto_hash_neq() -> enacl:hash(X) /= enacl:hash(Y) )). +%% STRING COMPARISON +%% ------------------------- + +verify_pair_bad(Sz) -> + ?LET(X, elements([fst, snd]), + case X of + fst -> + {?SUCHTHAT(B, binary(), byte_size(B) /= Sz), binary(Sz)}; + snd -> + {binary(Sz), ?SUCHTHAT(B, binary(), byte_size(B) /= Sz)} + end). + +verify_pair_good(Sz) -> + oneof([ + ?LET(Bin, binary(Sz), {Bin, Bin}), + ?SUCHTHAT({X, Y}, {binary(Sz), binary(Sz)}, X /= Y)]). + +verify_pair(Sz) -> + fault(verify_pair_bad(Sz), verify_pair_good(Sz)). + +verify_pair_valid(Sz, X, Y) -> + byte_size(X) == Sz andalso byte_size(Y) == Sz. + +prop_verify_16() -> + ?FORALL({X, Y}, verify_pair(16), + case verify_pair_valid(16, X, Y) of + true -> + equals(X == Y, enacl:verify_16(X, Y)); + false -> + try + enacl:verify_16(X, Y), + false + catch + error:badarg -> true + end + end). + +prop_verify_32() -> + ?FORALL({X, Y}, verify_pair(32), + case verify_pair_valid(32, X, Y) of + true -> + equals(X == Y, enacl:verify_32(X, Y)); + false -> + try + enacl:verify_32(X, Y), + false + catch + error:badarg -> true + end + end). \ No newline at end of file diff --git a/src/enacl.erl b/src/enacl.erl index e1aebd8..bcad62e 100644 --- a/src/enacl.erl +++ b/src/enacl.erl @@ -19,12 +19,20 @@ ]). -export([ - hash/1 + hash/1, + verify_16/2, + verify_32/2 ]). -hash(Bin) -> - enacl_nif:crypto_hash(Bin). +%% Low level helper functions +%% ----------------- +hash(Bin) -> enacl_nif:crypto_hash(Bin). +verify_16(X, Y) -> enacl_nif:crypto_verify_16(X, Y). +verify_32(X, Y) -> enacl_nif:crypto_verify_32(X, Y). + +%% Public Key Crypto +%% --------------------- box_keypair() -> enacl_nif:crypto_box_keypair(). -- GitLab