Skip to content

Commit c7720e6

Browse files
committed
Streamline the AEAD API
1 parent b637ba3 commit c7720e6

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
- Go through all calls and make them return streamlined exceptions if applicable.
1111
Pretty large change, but OTOH, this ought to happen before a 1.0 release as well.
12-
- AEAD
1312
- hash
1413
- kx
1514
- randombytes

c_src/aead.c

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,20 @@ enacl_crypto_aead_chacha20poly1305_ietf_encrypt(ErlNifEnv *env, int argc,
5757
if (!enif_alloc_binary(message.size +
5858
crypto_aead_chacha20poly1305_ietf_ABYTES,
5959
&ciphertext)) {
60-
ret = enacl_error_tuple(env, "alloc_failed");
61-
goto done;
60+
goto err;
6261
}
6362

64-
if (crypto_aead_chacha20poly1305_ietf_encrypt(
65-
ciphertext.data, NULL, message.data, message.size, ad.data, ad.size,
66-
NULL, nonce.data, key.data) < 0) {
67-
ret = enacl_error_tuple(env, "aead_chacha20poly1305_ietf_encrypt_failed");
68-
goto release;
69-
}
63+
crypto_aead_chacha20poly1305_ietf_encrypt(ciphertext.data, NULL, message.data,
64+
message.size, ad.data, ad.size,
65+
NULL, nonce.data, key.data);
7066

7167
ret = enif_make_binary(env, &ciphertext);
7268
goto done;
7369

7470
bad_arg:
7571
return enif_make_badarg(env);
76-
release:
77-
enif_release_binary(&ciphertext);
72+
err:
73+
ret = enacl_internal_error(env);
7874
done:
7975
return ret;
8076
}
@@ -106,14 +102,13 @@ enacl_crypto_aead_chacha20poly1305_ietf_decrypt(ErlNifEnv *env, int argc,
106102
if (!enif_alloc_binary(ciphertext.size -
107103
crypto_aead_chacha20poly1305_ietf_ABYTES,
108104
&message)) {
109-
ret = enacl_error_tuple(env, "alloc_failed");
110-
goto done;
105+
return enacl_internal_error(env);
111106
}
112107

113108
if (crypto_aead_chacha20poly1305_ietf_decrypt(
114109
message.data, NULL, NULL, ciphertext.data, ciphertext.size, ad.data,
115-
ad.size, nonce.data, key.data) < 0) {
116-
ret = enacl_error_tuple(env, "aead_chacha20poly1305_ietf_decrypt_failed");
110+
ad.size, nonce.data, key.data) != 0) {
111+
ret = enacl_error_tuple(env, "failed_verification");
117112
goto release;
118113
}
119114

@@ -180,24 +175,20 @@ enacl_crypto_aead_xchacha20poly1305_ietf_encrypt(ErlNifEnv *env, int argc,
180175
if (!enif_alloc_binary(message.size +
181176
crypto_aead_xchacha20poly1305_ietf_ABYTES,
182177
&ciphertext)) {
183-
ret = enacl_error_tuple(env, "alloc_failed");
184-
goto done;
178+
goto err;
185179
}
186180

187-
if (crypto_aead_xchacha20poly1305_ietf_encrypt(
188-
ciphertext.data, NULL, message.data, message.size, ad.data, ad.size,
189-
NULL, nonce.data, key.data) < 0) {
190-
ret = enacl_error_tuple(env, "aead_xchacha20poly1305_ietf_encrypt_failed");
191-
goto release;
192-
}
181+
crypto_aead_xchacha20poly1305_ietf_encrypt(
182+
ciphertext.data, NULL, message.data, message.size, ad.data, ad.size, NULL,
183+
nonce.data, key.data);
193184

194185
ret = enif_make_binary(env, &ciphertext);
195186
goto done;
196187

197188
bad_arg:
198189
return enif_make_badarg(env);
199-
release:
200-
enif_release_binary(&ciphertext);
190+
err:
191+
ret = enacl_internal_error(env);
201192
done:
202193
return ret;
203194
}
@@ -229,14 +220,13 @@ enacl_crypto_aead_xchacha20poly1305_ietf_decrypt(ErlNifEnv *env, int argc,
229220
if (!enif_alloc_binary(ciphertext.size -
230221
crypto_aead_xchacha20poly1305_ietf_ABYTES,
231222
&message)) {
232-
ret = enacl_error_tuple(env, "alloc_failed");
233-
goto done;
223+
return enacl_internal_error(env);
234224
}
235225

236226
if (crypto_aead_xchacha20poly1305_ietf_decrypt(
237227
message.data, NULL, NULL, ciphertext.data, ciphertext.size, ad.data,
238-
ad.size, nonce.data, key.data) < 0) {
239-
ret = enacl_error_tuple(env, "aead_xchacha20poly1305_ietf_decrypt_failed");
228+
ad.size, nonce.data, key.data) != 0) {
229+
ret = enacl_error_tuple(env, "failed_verification");
240230
goto release;
241231
}
242232

eqc_test/enacl_eqc.erl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,29 @@ prop_aead_chacha20poly1305_ietf_fail() ->
546546
end
547547
end).
548548

549+
%% * aead_xchacha20poly1305_encrypt/4,
550+
%% * aead_xchacha20poly1305_decrypt/4,
551+
prop_aead_xchacha20poly1305_ietf() ->
552+
NPubBytes = enacl:aead_xchacha20poly1305_ietf_NPUBBYTES(),
553+
?FORALL({Key, Msg, AD, Nonce},
554+
{binary(32), binary(), ?LET(ADBytes, choose(0,16), binary(ADBytes)), binary(NPubBytes)},
555+
begin
556+
EncryptMsg = enacl:aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key),
557+
equals(enacl:aead_xchacha20poly1305_ietf_decrypt(EncryptMsg, AD, Nonce, Key), Msg)
558+
end).
559+
560+
prop_aead_xchacha20poly1305_ietf_fail() ->
561+
NPubBytes = enacl:aead_xchacha20poly1305_ietf_NPUBBYTES(),
562+
?FORALL({Key, Msg, AD, Nonce},
563+
{binary(32), binary(), ?LET(ADBytes, choose(0,16), binary(ADBytes)), binary(NPubBytes)},
564+
begin
565+
EncryptMsg = enacl:aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key),
566+
case enacl:aead_xchacha20poly1305_ietf_decrypt(<<0:8, EncryptMsg/binary>>, AD, Nonce, Key) of
567+
{error, _} -> true;
568+
_ -> false
569+
end
570+
end).
571+
549572
%% CRYPTO STREAM
550573
%% ------------------------------------------------------------
551574
%% * stream/3

src/enacl.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ kx_secret_key_size() ->
11521152
%% `AD' using `Key' and `Nonce'. Returns the encrypted message followed by
11531153
%% `aead_chacha20poly1305_ABYTES/0' bytes of MAC.
11541154
%% @end
1155-
-spec aead_chacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary() | {error, term()}
1155+
-spec aead_chacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary()
11561156
when Key :: binary(),
11571157
Nonce :: binary(),
11581158
AD :: binary(),
@@ -1207,7 +1207,7 @@ aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX() ->
12071207
%% `AD' using `Key' and `Nonce'. Returns the encrypted message followed by
12081208
%% `aead_xchacha20poly1305_ABYTES/0' bytes of MAC.
12091209
%% @end
1210-
-spec aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary() | {error, term()}
1210+
-spec aead_xchacha20poly1305_ietf_encrypt(Msg, AD, Nonce, Key) -> binary()
12111211
when Key :: binary(),
12121212
Nonce :: binary(),
12131213
AD :: binary(),

0 commit comments

Comments
 (0)