Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leo_ssec: Added wrapper for SSE #13

Merged
merged 1 commit into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/leo_ssec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
-module(leo_ssec).
-author("kunal.tyagi").

-export([encrypt_object/2, decrypt_object/4]).
-export([gen_salt/1, gen_hash/3,
verify_key/3,
verify_block_encryption/3, verify_stream_encryption/3,
block_encrypt_data/2, block_encrypt_data/3,
block_encrypt_data/4,
block_decrypt_data/2, block_decrypt_data/3,
block_decrypt_data/4,
stream_encrypt_data/3, stream_encrypt_data/4,
stream_decrypt_data/3, stream_decrypt_data/4,
verify_ssec_algorithm/1, verify_ssec_key/2
Expand All @@ -41,6 +44,40 @@
%% API
%%-------------------------------------------------------------------------

%% @doc High Level Wrapper for SSE-C encryption
-spec(encrypt_object(Object, UserKey) ->
{ok, Cipher, Hash, Salt} when Object::crypto:io_data(),
UserKey::crypto:io_data(),
Cipher::binary(),
Hash::binary(),
Salt::binary()).
encrypt_object(Object, UserKey) ->
{ok, Salt} = gen_salt(32),
<<IV:16/binary, _/binary>> = Salt,
{sha256, Hash} = gen_hash(sha256, UserKey, Salt),
AlgoMeta = {aes_cbc256, rfc5652, 16},
Cipher = block_encrypt_data(UserKey, Object, AlgoMeta, IV),
{ok, Cipher, Hash, Salt}.

%% @doc High Level Wrapper for SSE-C decryption
-spec(decrypt_object(Cipher, UserKey, Hash, Salt) ->
{error, invalid}|{ok, Object}
when Object::crypto:io_data(),
UserKey::crypto:io_data(),
Cipher::binary(),
Hash::binary(),
Salt::binary()).
decrypt_object(Cipher, UserKey, Hash, Salt) ->
case verify_key(UserKey, Salt, {sha256, Hash}) of
false ->
{error, invalid};
true ->
<<IV:16/binary, _/binary>> = Salt,
AlgoMeta = {aes_cbc256, rfc5652, 16},
Object = block_decrypt_data(UserKey, Cipher, AlgoMeta, IV),
{ok, Object}
end.

%% @doc Contains all details about the algorithm and related options
%% Ensures simple encryption, decryption and storage op options
%% {AlgorithmType, PaddingType, MaxBitsToPad}
Expand Down Expand Up @@ -127,6 +164,20 @@ block_encrypt_data(UserKey, Data, AlgoMetaData) ->
PadData = pad(Pad, PadLen, Data),
crypto:block_encrypt(Algo, UserKey, PadData).

%% @doc block encrypt data using key and custome settings with IV
%%
-spec(block_encrypt_data(UserKey, Data, AlgoMetaData, IV) ->
CipherPadData when UserKey::crypto:block_key(),
Data::crypto:io_data(),
AlgoMetaData::algo_metadata(),
IV::crypto:io_data(),
CipherPadData::binary()).
block_encrypt_data(UserKey, Data, AlgoMetaData, IV) ->
{Algo, Pad, PadLen} = AlgoMetaData,
PadData = pad(Pad, PadLen, Data),
crypto:block_encrypt(Algo, UserKey, IV, PadData).


%% @doc stream encrypt data using user key
%%
-spec(stream_encrypt_data(State, Data) ->
Expand Down Expand Up @@ -182,6 +233,19 @@ block_decrypt_data(UserKey, Data, AlgoMetaData) ->
PadData = crypto:block_decrypt(Algo, UserKey, Data),
unpad(Pad, PadData).

%% @doc block decrypt data using key and custome settings with IV
%%
-spec(block_decrypt_data(UserKey, Data, AlgoMetaData, IV) ->
PlainData when UserKey::crypto:block_key(),
Data::binary(),
AlgoMetaData::algo_metadata(),
IV::binary(),
PlainData::iodata()).
block_decrypt_data(UserKey, Data, AlgoMetaData, IV) ->
{Algo, Pad, _PadLen} = AlgoMetaData,
PadData = crypto:block_decrypt(Algo, UserKey, IV, Data),
unpad(Pad, PadData).

%% @doc stream decrypt data using the user key
%%
-spec(stream_decrypt_data(UserKey, Data, AlgoMetaData) ->
Expand Down
15 changes: 13 additions & 2 deletions test/leo_ssec_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ run_test_() ->
{"test verify_ssec_algorithm/1",
{timeout, timer:seconds(3), fun test_verify_ssec_algorithm/0}},
{"test verify_ssec_key/1",
{timeout, timer:seconds(3), fun test_verify_ssec_key/0}}
{timeout, timer:seconds(3), fun test_verify_ssec_key/0}},
{"test encrypt_object/2 and decrypt_object/4",
{timeout, timer:seconds(3),
fun() -> check_wrapper(fun check_en_de/1, 1, 256) end}}
]}.

%% Test 1
Expand Down Expand Up @@ -128,7 +131,7 @@ test_verify_ssec_key() ->
?assertMatch({false, _}, leo_ssec:verify_ssec_key(Base64Key1,
{md5, "ABCD" ++ Base64Checksum1})).

%% Test 6
%% Test 7
-spec(verify_pad_unpad_test_() -> boolean()).
verify_pad_unpad_test_() ->
[
Expand All @@ -155,4 +158,12 @@ verify_pad_unpad_test_() ->
end
].

%% Test 8
check_en_de(Len) ->
RBin = crypto:strong_rand_bytes(Len),
Key = crypto:strong_rand_bytes(32),
{ok, Cipher, Hash, Salt} = leo_ssec:encrypt_object(RBin, Key),
{ok, RBin} = leo_ssec:decrypt_object(Cipher, Key, Hash, Salt),
ok.

-endif.