Expose AEAD ChaCha20 Poly1305 functionality

Note: We expose the modern IETF version but still name it plain
aead_chacha20poly1305.
This commit is contained in:
Hans Svensson
2018-03-01 08:42:31 +01:00
parent a5dab7acf0
commit 9dfbe8cc90
3 changed files with 175 additions and 0 deletions
+62
View File
@@ -54,6 +54,13 @@
stream_chacha20/3,
stream_chacha20_xor/3,
aead_chacha20poly1305_encrypt/4,
aead_chacha20poly1305_decrypt/4,
aead_chacha20poly1305_KEYBYTES/0,
aead_chacha20poly1305_NONCEBYTES/0,
aead_chacha20poly1305_ABYTES/0,
aead_chacha20poly1305_MESSAGEBYTES_MAX/0,
stream_key_size/0,
stream_nonce_size/0,
stream/3,
@@ -997,7 +1004,62 @@ kx_public_key_size() ->
kx_secret_key_size() ->
enacl_nif:crypto_kx_SECRETKEYBYTES().
%% AEAD ChaCha20 Poly1305
%% ----------------------
%% @doc aead_chacha20poly1305_encrypt/4 encrypts `Message` with additional data
%% `AD` using `Key` and `Nonce`. Returns the encrypted message followed by
%% `aead_chacha20poly1305_ABYTES/0` bytes of MAC.
%% @end
-spec aead_chacha20poly1305_encrypt(Key, Nonce, AD, Msg) -> binary() | {error, term()}
when Key :: binary(),
Nonce :: pos_integer(),
AD :: binary(),
Msg :: binary().
aead_chacha20poly1305_encrypt(Key, Nonce, AD, Msg) ->
NonceBin = <<0:32, Nonce:64/little-unsigned-integer>>,
enacl_nif:crypto_aead_chacha20poly1305_encrypt(Key, NonceBin, AD, Msg).
%% @doc aead_chacha20poly1305_decrypt/4 decrypts ciphertext `CT` with additional
%% data `AD` using `Key` and `Nonce`. Note: `CipherText` should contain
%% `aead_chacha20poly1305_ABYTES/0` bytes that is the MAC. Returns the decrypted
%% message.
%% @end
-spec aead_chacha20poly1305_decrypt(Key, Nonce, AD, CT) -> binary() | {error, term()}
when Key :: binary(),
Nonce :: pos_integer(),
AD :: binary(),
CT :: binary().
aead_chacha20poly1305_decrypt(Key, Nonce, AD, CT) ->
NonceBin = <<0:32, Nonce:64/little-unsigned-integer>>,
enacl_nif:crypto_aead_chacha20poly1305_decrypt(Key, NonceBin, AD, CT).
%% @doc aead_chacha20poly1305_KEYBYTES/0 returns the number of bytes
%% of the key used in AEAD ChaCha20 Poly1305 encryption/decryption.
%% @end
-spec aead_chacha20poly1305_KEYBYTES() -> pos_integer().
aead_chacha20poly1305_KEYBYTES() ->
enacl_nif:crypto_aead_chacha20poly1305_KEYBYTES().
%% @doc aead_chacha20poly1305_NONCEBYTES/0 returns the number of bytes
%% of the Nonce in AEAD ChaCha20 Poly1305 encryption/decryption.
%% @end
-spec aead_chacha20poly1305_NONCEBYTES() -> pos_integer().
aead_chacha20poly1305_NONCEBYTES() ->
enacl_nif:crypto_aead_chacha20poly1305_NPUBBYTES().
%% @doc aead_chacha20poly1305_ABYTES/0 returns the number of bytes
%% of the MAC in AEAD ChaCha20 Poly1305 encryption/decryption.
%% @end
-spec aead_chacha20poly1305_ABYTES() -> pos_integer().
aead_chacha20poly1305_ABYTES() ->
enacl_nif:crypto_aead_chacha20poly1305_ABYTES().
%% @doc aead_chacha20poly1305_MESSAGEBYTES_MAX/0 returns the max number of bytes
%% allowed in a message in AEAD ChaCha20 Poly1305 encryption/decryption.
%% @end
-spec aead_chacha20poly1305_MESSAGEBYTES_MAX() -> pos_integer().
aead_chacha20poly1305_MESSAGEBYTES_MAX() ->
enacl_nif:crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX().
%% Obtaining random bytes
+14
View File
@@ -67,6 +67,13 @@
crypto_stream_xor/3,
crypto_stream_xor_b/3,
crypto_aead_chacha20poly1305_encrypt/4,
crypto_aead_chacha20poly1305_decrypt/4,
crypto_aead_chacha20poly1305_KEYBYTES/0,
crypto_aead_chacha20poly1305_NPUBBYTES/0,
crypto_aead_chacha20poly1305_ABYTES/0,
crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX/0,
crypto_auth_BYTES/0,
crypto_auth_KEYBYTES/0,
@@ -240,6 +247,13 @@ crypto_stream_b(_Bytes, _Nonce, _Key) -> erlang:nif_error(nif_not_loaded).
crypto_stream_xor(_M, _Nonce, _Key) -> erlang:nif_error(nif_not_loaded).
crypto_stream_xor_b(_M, _Nonce, _Key) -> erlang:nif_error(nif_not_loaded).
crypto_aead_chacha20poly1305_encrypt(_Key, _Nonce, _AD, _Message) -> erlang:nif_error(nif_not_loaded).
crypto_aead_chacha20poly1305_decrypt(_Key, _Nonce, _AD, _Message) -> erlang:nif_error(nif_not_loaded).
crypto_aead_chacha20poly1305_KEYBYTES() -> erlang:nif_error(nif_not_loaded).
crypto_aead_chacha20poly1305_NPUBBYTES() -> erlang:nif_error(nif_not_loaded).
crypto_aead_chacha20poly1305_ABYTES() -> erlang:nif_error(nif_not_loaded).
crypto_aead_chacha20poly1305_MESSAGEBYTES_MAX() -> erlang:nif_error(nif_not_loaded).
crypto_auth_BYTES() -> erlang:nif_error(nif_not_loaded).
crypto_auth_KEYBYTES() -> erlang:nif_error(nif_not_loaded).
crypto_auth(_Msg, _Key) -> erlang:nif_error(nif_not_loaded).