Some tests for cipher_state

This commit is contained in:
Hans Svensson 2018-03-01 11:44:42 +01:00
parent d78c774b55
commit 0d563884eb
4 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,30 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2018, Aeternity Anstalt
%%%-------------------------------------------------------------------
-module(enoise_chiper_state_tests).
-include_lib("eunit/include/eunit.hrl").
chachapoly_test() ->
#{ key := Key, nonce := Nonce, ad := AD, mac := MAC,
pt := PlainText, ct := CipherText } = test_utils:chacha_data(),
PTLen = byte_size(PlainText),
CTLen = byte_size(CipherText),
MACLen = byte_size(MAC),
CS0 = enoise_cipher_state:init(Key, 'ChaChaPoly'),
CS1 = enoise_cipher_state:set_nonce(CS0, Nonce),
{ok, _CS2, <<CipherText0:CTLen/binary, MAC0:MACLen/binary>>} =
enoise_cipher_state:encrypt_with_ad(CS1, AD, PlainText),
?assertMatch(CipherText, CipherText0),
?assertMatch(MAC, MAC0),
{ok, _CS3, <<PlainText0:PTLen/binary>>} =
enoise_cipher_state:decrypt_with_ad(CS1, AD, <<CipherText/binary, MAC/binary>>),
?assertMatch(PlainText, PlainText0),
ok.

View File

@ -0,0 +1,30 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2018, Aeternity Anstalt
%%%-------------------------------------------------------------------
-module(enoise_crypto_tests).
-include_lib("eunit/include/eunit.hrl").
chachapoly_test() ->
#{ key := Key, nonce := Nonce, ad := AD, mac := MAC,
pt := PlainText, ct := CipherText } = test_utils:chacha_data(),
PTLen = byte_size(PlainText),
CTLen = byte_size(CipherText),
MACLen = byte_size(MAC),
%% Sanity check
?assert(PTLen == CTLen),
<<CipherText0:CTLen/binary, MAC0:MACLen/binary>> =
enoise_crypto:encrypt('ChaChaPoly', Key, Nonce, AD, PlainText),
?assertMatch(CipherText, CipherText0),
?assertMatch(MAC, MAC0),
<<PlainText0:PTLen/binary>> =
enoise_crypto:decrypt('ChaChaPoly', Key, Nonce, AD, <<CipherText/binary, MAC/binary>>),
?assertMatch(PlainText, PlainText0),
ok.

17
test/enoise_tests.erl Normal file
View File

@ -0,0 +1,17 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2018, Aeternity Anstalt
%%%-------------------------------------------------------------------
-module(enoise_tests).
-include_lib("eunit/include/eunit.hrl").
-include("enoise.hrl").
%% connect_test() ->
%% TestProtocol = #noise_protocol{ },
%% {ok, EConn} = enoise:connect("localhost", 7890, [{noise, TestProtocol}, {pre_comm, <<0,0,0,0,2>>}], 1000),
%% enoise:close(EConn).

35
test/test_utils.erl Normal file
View File

@ -0,0 +1,35 @@
%%%-------------------------------------------------------------------
%%% @copyright (C) 2018, Aeternity Anstalt
%%%-------------------------------------------------------------------
-module(test_utils).
-compile([export_all, nowarn_export_all]).
%% -- Test data --------------------------------------------------------------
chacha_data() ->
#{ key => hex_str_to_bin("0x1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0")
, nonce => 16#0807060504030201
, ad => hex_str_to_bin("0xf33388860000000000004e91")
, pt => hex_str_to_bin("0x496e7465726e65742d4472616674732061726520647261667420646f63756d65"
"6e74732076616c696420666f722061206d6178696d756d206f6620736978206d"
"6f6e74687320616e64206d617920626520757064617465642c207265706c6163"
"65642c206f72206f62736f6c65746564206279206f7468657220646f63756d65"
"6e747320617420616e792074696d652e20497420697320696e617070726f7072"
"6961746520746f2075736520496e7465726e65742d4472616674732061732072"
"65666572656e6365206d6174657269616c206f7220746f206369746520746865"
"6d206f74686572207468616e206173202fe2809c776f726b20696e2070726f67"
"726573732e2fe2809d")
, ct => hex_str_to_bin("0x64a0861575861af460f062c79be643bd5e805cfd345cf389f108670ac76c8cb2"
"4c6cfc18755d43eea09ee94e382d26b0bdb7b73c321b0100d4f03b7f355894cf"
"332f830e710b97ce98c8a84abd0b948114ad176e008d33bd60f982b1ff37c855"
"9797a06ef4f0ef61c186324e2b3506383606907b6a7c02b0f9f6157b53c867e4"
"b9166c767b804d46a59b5216cde7a4e99040c5a40433225ee282a1b0a06c523e"
"af4534d7f83fa1155b0047718cbc546a0d072b04b3564eea1b422273f548271a"
"0bb2316053fa76991955ebd63159434ecebb4e466dae5a1073a6727627097a10"
"49e617d91d361094fa68f0ff77987130305beaba2eda04df997b714d6c6f2c29"
"a6ad5cb4022b02709b")
, mac => hex_str_to_bin("0xeead9d67890cbb22392336fea1851f38") }.
hex_str_to_bin("0x" ++ Rest) ->
<< <<(list_to_integer([C], 16)):4>> || C <- Rest >>.