Add support for aead_xchacha20poly1305

This commit is contained in:
ECrownofFire
2018-11-25 09:44:57 -05:00
parent f650c72b02
commit 2b183e1974
3 changed files with 160 additions and 0 deletions
+83
View File
@@ -1433,6 +1433,81 @@ ERL_NIF_TERM enif_crypto_aead_chacha20poly1305_decrypt(ErlNifEnv *env, int argc,
}
/*
* AEAD XChaCha20 Poly1305
*/
static
ERL_NIF_TERM enif_crypto_aead_xchacha20poly1305_KEYBYTES(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) {
return enif_make_int64(env, crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
}
static
ERL_NIF_TERM enif_crypto_aead_xchacha20poly1305_NPUBBYTES(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) {
return enif_make_int64(env, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
}
static
ERL_NIF_TERM enif_crypto_aead_xchacha20poly1305_ABYTES(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) {
return enif_make_int64(env, crypto_aead_xchacha20poly1305_ietf_ABYTES);
}
static
ERL_NIF_TERM enif_crypto_aead_xchacha20poly1305_MESSAGEBYTES_MAX(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) {
return enif_make_int64(env, crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX);
}
static
ERL_NIF_TERM enif_crypto_aead_xchacha20poly1305_encrypt(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) {
ErlNifBinary key, nonce, ad, message, ciphertext;
if ((argc != 4) || (!enif_inspect_binary(env, argv[0], &key))
|| (!enif_inspect_binary(env, argv[1], &nonce))
|| (!enif_inspect_binary(env, argv[2], &ad))
|| (!enif_inspect_binary(env, argv[3], &message))
|| (key.size != crypto_aead_xchacha20poly1305_ietf_KEYBYTES)
|| (nonce.size != crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)) {
return enif_make_badarg(env);
}
if (!enif_alloc_binary(message.size + crypto_aead_xchacha20poly1305_ietf_ABYTES, &ciphertext)) {
return nacl_error_tuple(env, "alloc_failed");
}
if (crypto_aead_xchacha20poly1305_ietf_encrypt(ciphertext.data, NULL, message.data, message.size,
ad.data, ad.size, NULL, nonce.data, key.data) < 0) {
return nacl_error_tuple(env, "aead_xchacha20poly1305_ietf_encrypt_failed");
}
return enif_make_binary(env, &ciphertext);
}
static
ERL_NIF_TERM enif_crypto_aead_xchacha20poly1305_decrypt(ErlNifEnv *env, int argc, ERL_NIF_TERM const argv[]) {
ErlNifBinary key, nonce, ad, message, ciphertext;
if ((argc != 4) || (!enif_inspect_binary(env, argv[0], &key))
|| (!enif_inspect_binary(env, argv[1], &nonce))
|| (!enif_inspect_binary(env, argv[2], &ad))
|| (!enif_inspect_binary(env, argv[3], &ciphertext))
|| (ciphertext.size < crypto_aead_xchacha20poly1305_ietf_ABYTES)
|| (key.size != crypto_aead_xchacha20poly1305_ietf_KEYBYTES)
|| (nonce.size != crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)) {
return enif_make_badarg(env);
}
if (!enif_alloc_binary(ciphertext.size - crypto_aead_xchacha20poly1305_ietf_ABYTES, &message)) {
return nacl_error_tuple(env, "alloc_failed");
}
if (crypto_aead_xchacha20poly1305_ietf_decrypt(message.data, NULL, NULL, ciphertext.data, ciphertext.size,
ad.data, ad.size, nonce.data, key.data) < 0) {
return nacl_error_tuple(env, "aead_xchacha20poly1305_ietf_decrypt_failed");
}
return enif_make_binary(env, &message);
}
/*
* Generic hash
*/
@@ -1752,6 +1827,14 @@ static ErlNifFunc nif_funcs[] = {
erl_nif_dirty_job_cpu_bound_macro("crypto_aead_chacha20poly1305_encrypt", 4, enif_crypto_aead_chacha20poly1305_encrypt),
erl_nif_dirty_job_cpu_bound_macro("crypto_aead_chacha20poly1305_decrypt", 4, enif_crypto_aead_chacha20poly1305_decrypt),
{"crypto_aead_xchacha20poly1305_KEYBYTES", 0, enif_crypto_aead_xchacha20poly1305_KEYBYTES},
{"crypto_aead_xchacha20poly1305_NPUBBYTES", 0, enif_crypto_aead_xchacha20poly1305_NPUBBYTES},
{"crypto_aead_xchacha20poly1305_ABYTES", 0, enif_crypto_aead_xchacha20poly1305_ABYTES},
{"crypto_aead_xchacha20poly1305_MESSAGEBYTES_MAX", 0, enif_crypto_aead_xchacha20poly1305_MESSAGEBYTES_MAX},
erl_nif_dirty_job_cpu_bound_macro("crypto_aead_xchacha20poly1305_encrypt", 4, enif_crypto_aead_xchacha20poly1305_encrypt),
erl_nif_dirty_job_cpu_bound_macro("crypto_aead_xchacha20poly1305_decrypt", 4, enif_crypto_aead_xchacha20poly1305_decrypt),
{"crypto_generichash_BYTES", 0, enif_crypto_generichash_BYTES},
{"crypto_generichash_BYTES_MIN", 0, enif_crypto_generichash_BYTES_MIN},
{"crypto_generichash_BYTES_MAX", 0, enif_crypto_generichash_BYTES_MAX},