Streamline the AEAD API
This commit is contained in:
parent
b637ba307b
commit
c7720e6ab8
@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||||||
|
|
||||||
- Go through all calls and make them return streamlined exceptions if applicable.
|
- Go through all calls and make them return streamlined exceptions if applicable.
|
||||||
Pretty large change, but OTOH, this ought to happen before a 1.0 release as well.
|
Pretty large change, but OTOH, this ought to happen before a 1.0 release as well.
|
||||||
- AEAD
|
|
||||||
- hash
|
- hash
|
||||||
- kx
|
- kx
|
||||||
- randombytes
|
- randombytes
|
||||||
|
46
c_src/aead.c
46
c_src/aead.c
@ -57,24 +57,20 @@ enacl_crypto_aead_chacha20poly1305_ietf_encrypt(ErlNifEnv *env, int argc,
|
|||||||
if (!enif_alloc_binary(message.size +
|
if (!enif_alloc_binary(message.size +
|
||||||
crypto_aead_chacha20poly1305_ietf_ABYTES,
|
crypto_aead_chacha20poly1305_ietf_ABYTES,
|
||||||
&ciphertext)) {
|
&ciphertext)) {
|
||||||
ret = enacl_error_tuple(env, "alloc_failed");
|
goto err;
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypto_aead_chacha20poly1305_ietf_encrypt(
|
crypto_aead_chacha20poly1305_ietf_encrypt(ciphertext.data, NULL, message.data,
|
||||||
ciphertext.data, NULL, message.data, message.size, ad.data, ad.size,
|
message.size, ad.data, ad.size,
|
||||||
NULL, nonce.data, key.data) < 0) {
|
NULL, nonce.data, key.data);
|
||||||
ret = enacl_error_tuple(env, "aead_chacha20poly1305_ietf_encrypt_failed");
|
|
||||||
goto release;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = enif_make_binary(env, &ciphertext);
|
ret = enif_make_binary(env, &ciphertext);
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
bad_arg:
|
bad_arg:
|
||||||
return enif_make_badarg(env);
|
return enif_make_badarg(env);
|
||||||
release:
|
err:
|
||||||
enif_release_binary(&ciphertext);
|
ret = enacl_internal_error(env);
|
||||||
done:
|
done:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -106,14 +102,13 @@ enacl_crypto_aead_chacha20poly1305_ietf_decrypt(ErlNifEnv *env, int argc,
|
|||||||
if (!enif_alloc_binary(ciphertext.size -
|
if (!enif_alloc_binary(ciphertext.size -
|
||||||
crypto_aead_chacha20poly1305_ietf_ABYTES,
|
crypto_aead_chacha20poly1305_ietf_ABYTES,
|
||||||
&message)) {
|
&message)) {
|
||||||
ret = enacl_error_tuple(env, "alloc_failed");
|
return enacl_internal_error(env);
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypto_aead_chacha20poly1305_ietf_decrypt(
|
if (crypto_aead_chacha20poly1305_ietf_decrypt(
|
||||||
message.data, NULL, NULL, ciphertext.data, ciphertext.size, ad.data,
|
message.data, NULL, NULL, ciphertext.data, ciphertext.size, ad.data,
|
||||||
ad.size, nonce.data, key.data) < 0) {
|
ad.size, nonce.data, key.data) != 0) {
|
||||||
ret = enacl_error_tuple(env, "aead_chacha20poly1305_ietf_decrypt_failed");
|
ret = enacl_error_tuple(env, "failed_verification");
|
||||||
goto release;
|
goto release;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,24 +175,20 @@ enacl_crypto_aead_xchacha20poly1305_ietf_encrypt(ErlNifEnv *env, int argc,
|
|||||||
if (!enif_alloc_binary(message.size +
|
if (!enif_alloc_binary(message.size +
|
||||||
crypto_aead_xchacha20poly1305_ietf_ABYTES,
|
crypto_aead_xchacha20poly1305_ietf_ABYTES,
|
||||||
&ciphertext)) {
|
&ciphertext)) {
|
||||||
ret = enacl_error_tuple(env, "alloc_failed");
|
goto err;
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypto_aead_xchacha20poly1305_ietf_encrypt(
|
crypto_aead_xchacha20poly1305_ietf_encrypt(
|
||||||
ciphertext.data, NULL, message.data, message.size, ad.data, ad.size,
|
ciphertext.data, NULL, message.data, message.size, ad.data, ad.size, NULL,
|
||||||
NULL, nonce.data, key.data) < 0) {
|
nonce.data, key.data);
|
||||||
ret = enacl_error_tuple(env, "aead_xchacha20poly1305_ietf_encrypt_failed");
|
|
||||||
goto release;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = enif_make_binary(env, &ciphertext);
|
ret = enif_make_binary(env, &ciphertext);
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
bad_arg:
|
bad_arg:
|
||||||
return enif_make_badarg(env);
|
return enif_make_badarg(env);
|
||||||
release:
|
err:
|
||||||
enif_release_binary(&ciphertext);
|
ret = enacl_internal_error(env);
|
||||||
done:
|
done:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -229,14 +220,13 @@ enacl_crypto_aead_xchacha20poly1305_ietf_decrypt(ErlNifEnv *env, int argc,
|
|||||||
if (!enif_alloc_binary(ciphertext.size -
|
if (!enif_alloc_binary(ciphertext.size -
|
||||||
crypto_aead_xchacha20poly1305_ietf_ABYTES,
|
crypto_aead_xchacha20poly1305_ietf_ABYTES,
|
||||||
&message)) {
|
&message)) {
|
||||||
ret = enacl_error_tuple(env, "alloc_failed");
|
return enacl_internal_error(env);
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crypto_aead_xchacha20poly1305_ietf_decrypt(
|
if (crypto_aead_xchacha20poly1305_ietf_decrypt(
|
||||||
message.data, NULL, NULL, ciphertext.data, ciphertext.size, ad.data,
|
message.data, NULL, NULL, ciphertext.data, ciphertext.size, ad.data,
|
||||||
ad.size, nonce.data, key.data) < 0) {
|
ad.size, nonce.data, key.data) != 0) {
|
||||||
ret = enacl_error_tuple(env, "aead_xchacha20poly1305_ietf_decrypt_failed");
|
ret = enacl_error_tuple(env, "failed_verification");
|
||||||
goto release;
|
goto release;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -546,6 +546,29 @@ prop_aead_chacha20poly1305_ietf_fail() ->
|
|||||||
end
|
end
|
||||||
end).
|
end).
|
||||||
|
|
||||||
|
%% * aead_xchacha20poly1305_encrypt/4,
|
||||||
|
%% * aead_xchacha20poly1305_decrypt/4,
|
||||||
|
prop_aead_xchacha20poly1305_ietf() ->
|
||||||
|
NPubBytes = enacl:aead_xchacha20poly1305_ietf_NPUBBYTES(),
|
||||||
|
?FORALL({Key, Msg, AD, Nonce},
|
||||||
|
{binary(32), binary(), ?LET(ADBytes, choose(0,16), binary(ADBytes)), binary(NPubBytes)},
|
||||||
|
begin
|
||||||
|
EncryptMsg = enacl:aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key),
|
||||||
|
equals(enacl:aead_xchacha20poly1305_ietf_decrypt(EncryptMsg, AD, Nonce, Key), Msg)
|
||||||
|
end).
|
||||||
|
|
||||||
|
prop_aead_xchacha20poly1305_ietf_fail() ->
|
||||||
|
NPubBytes = enacl:aead_xchacha20poly1305_ietf_NPUBBYTES(),
|
||||||
|
?FORALL({Key, Msg, AD, Nonce},
|
||||||
|
{binary(32), binary(), ?LET(ADBytes, choose(0,16), binary(ADBytes)), binary(NPubBytes)},
|
||||||
|
begin
|
||||||
|
EncryptMsg = enacl:aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key),
|
||||||
|
case enacl:aead_xchacha20poly1305_ietf_decrypt(<<0:8, EncryptMsg/binary>>, AD, Nonce, Key) of
|
||||||
|
{error, _} -> true;
|
||||||
|
_ -> false
|
||||||
|
end
|
||||||
|
end).
|
||||||
|
|
||||||
%% CRYPTO STREAM
|
%% CRYPTO STREAM
|
||||||
%% ------------------------------------------------------------
|
%% ------------------------------------------------------------
|
||||||
%% * stream/3
|
%% * stream/3
|
||||||
|
@ -1152,7 +1152,7 @@ kx_secret_key_size() ->
|
|||||||
%% `AD' using `Key' and `Nonce'. Returns the encrypted message followed by
|
%% `AD' using `Key' and `Nonce'. Returns the encrypted message followed by
|
||||||
%% `aead_chacha20poly1305_ABYTES/0' bytes of MAC.
|
%% `aead_chacha20poly1305_ABYTES/0' bytes of MAC.
|
||||||
%% @end
|
%% @end
|
||||||
-spec aead_chacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary() | {error, term()}
|
-spec aead_chacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary()
|
||||||
when Key :: binary(),
|
when Key :: binary(),
|
||||||
Nonce :: binary(),
|
Nonce :: binary(),
|
||||||
AD :: binary(),
|
AD :: binary(),
|
||||||
@ -1207,7 +1207,7 @@ aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX() ->
|
|||||||
%% `AD' using `Key' and `Nonce'. Returns the encrypted message followed by
|
%% `AD' using `Key' and `Nonce'. Returns the encrypted message followed by
|
||||||
%% `aead_xchacha20poly1305_ABYTES/0' bytes of MAC.
|
%% `aead_xchacha20poly1305_ABYTES/0' bytes of MAC.
|
||||||
%% @end
|
%% @end
|
||||||
-spec aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary() | {error, term()}
|
-spec aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary()
|
||||||
when Key :: binary(),
|
when Key :: binary(),
|
||||||
Nonce :: binary(),
|
Nonce :: binary(),
|
||||||
AD :: binary(),
|
AD :: binary(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user