Merge remote-tracking branch 'kuon/patch-1'

This commit is contained in:
Jesper Louis Andersen
2020-06-09 12:08:21 +02:00
4 changed files with 84 additions and 28 deletions
+31 -18
View File
@@ -13,8 +13,8 @@
#include "pwhash.h"
#include "randombytes.h"
#include "secret.h"
#include "sign.h"
#include "secretstream.h"
#include "sign.h"
#ifdef ERL_NIF_DIRTY_JOB_CPU_BOUND
#define erl_nif_dirty_job_cpu_bound_macro(a, b, c) \
@@ -288,7 +288,7 @@ static ErlNifFunc nif_funcs[] = {
{"sodium_memzero", 1, enif_sodium_memzero},
{"crypto_pwhash_SALTBYTES", 0, enacl_crypto_pwhash_SALTBYTES},
erl_nif_dirty_job_cpu_bound_macro("crypto_pwhash", 4, enacl_crypto_pwhash),
erl_nif_dirty_job_cpu_bound_macro("crypto_pwhash", 5, enacl_crypto_pwhash),
erl_nif_dirty_job_cpu_bound_macro("crypto_pwhash_str", 3,
enacl_crypto_pwhash_str),
erl_nif_dirty_job_cpu_bound_macro("crypto_pwhash_str_verify", 2,
@@ -380,22 +380,35 @@ static ErlNifFunc nif_funcs[] = {
erl_nif_dirty_job_cpu_bound_macro("crypto_generichash_final", 1,
enacl_crypto_generichash_final),
{"crypto_secretstream_xchacha20poly1305_ABYTES", 0, enacl_crypto_secretstream_xchacha20poly1305_ABYTES},
{"crypto_secretstream_xchacha20poly1305_HEADERBYTES", 0, enacl_crypto_secretstream_xchacha20poly1305_HEADERBYTES},
{"crypto_secretstream_xchacha20poly1305_KEYBYTES", 0, enacl_crypto_secretstream_xchacha20poly1305_KEYBYTES},
{"crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX", 0, enacl_crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX},
{"crypto_secretstream_xchacha20poly1305_TAG_MESSAGE", 0, enacl_crypto_secretstream_xchacha20poly1305_TAG_MESSAGE},
{"crypto_secretstream_xchacha20poly1305_TAG_PUSH", 0, enacl_crypto_secretstream_xchacha20poly1305_TAG_PUSH},
{"crypto_secretstream_xchacha20poly1305_TAG_REKEY", 0, enacl_crypto_secretstream_xchacha20poly1305_TAG_REKEY},
{"crypto_secretstream_xchacha20poly1305_TAG_FINAL", 0, enacl_crypto_secretstream_xchacha20poly1305_TAG_FINAL},
{"crypto_secretstream_xchacha20poly1305_keygen", 0, enacl_crypto_secretstream_xchacha20poly1305_keygen},
{"crypto_secretstream_xchacha20poly1305_init_push", 1, enacl_crypto_secretstream_xchacha20poly1305_init_push},
{"crypto_secretstream_xchacha20poly1305_init_pull", 2, enacl_crypto_secretstream_xchacha20poly1305_init_pull},
{"crypto_secretstream_xchacha20poly1305_rekey", 1, enacl_crypto_secretstream_xchacha20poly1305_rekey},
erl_nif_dirty_job_cpu_bound_macro("crypto_secretstream_xchacha20poly1305_push", 4,
{"crypto_secretstream_xchacha20poly1305_ABYTES", 0,
enacl_crypto_secretstream_xchacha20poly1305_ABYTES},
{"crypto_secretstream_xchacha20poly1305_HEADERBYTES", 0,
enacl_crypto_secretstream_xchacha20poly1305_HEADERBYTES},
{"crypto_secretstream_xchacha20poly1305_KEYBYTES", 0,
enacl_crypto_secretstream_xchacha20poly1305_KEYBYTES},
{"crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX", 0,
enacl_crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX},
{"crypto_secretstream_xchacha20poly1305_TAG_MESSAGE", 0,
enacl_crypto_secretstream_xchacha20poly1305_TAG_MESSAGE},
{"crypto_secretstream_xchacha20poly1305_TAG_PUSH", 0,
enacl_crypto_secretstream_xchacha20poly1305_TAG_PUSH},
{"crypto_secretstream_xchacha20poly1305_TAG_REKEY", 0,
enacl_crypto_secretstream_xchacha20poly1305_TAG_REKEY},
{"crypto_secretstream_xchacha20poly1305_TAG_FINAL", 0,
enacl_crypto_secretstream_xchacha20poly1305_TAG_FINAL},
{"crypto_secretstream_xchacha20poly1305_keygen", 0,
enacl_crypto_secretstream_xchacha20poly1305_keygen},
{"crypto_secretstream_xchacha20poly1305_init_push", 1,
enacl_crypto_secretstream_xchacha20poly1305_init_push},
{"crypto_secretstream_xchacha20poly1305_init_pull", 2,
enacl_crypto_secretstream_xchacha20poly1305_init_pull},
{"crypto_secretstream_xchacha20poly1305_rekey", 1,
enacl_crypto_secretstream_xchacha20poly1305_rekey},
erl_nif_dirty_job_cpu_bound_macro(
"crypto_secretstream_xchacha20poly1305_push", 4,
enacl_crypto_secretstream_xchacha20poly1305_push),
erl_nif_dirty_job_cpu_bound_macro("crypto_secretstream_xchacha20poly1305_pull", 3,
enacl_crypto_secretstream_xchacha20poly1305_pull)
};
erl_nif_dirty_job_cpu_bound_macro(
"crypto_secretstream_xchacha20poly1305_pull", 3,
enacl_crypto_secretstream_xchacha20poly1305_pull)};
ERL_NIF_INIT(enacl_nif, nif_funcs, enacl_crypto_load, NULL, NULL, NULL);
+31 -4
View File
@@ -57,22 +57,49 @@ static size_t enacl_pwhash_memlimit(ErlNifEnv *env, ERL_NIF_TERM arg) {
return 0;
}
ERL_NIF_TERM enacl_crypto_pwhash_SALTBYTES(ErlNifEnv *env, int argc,
ERL_NIF_TERM const argv[]) {
return enif_make_int64(env, crypto_pwhash_SALTBYTES);
}
static int enacl_pwhash_alg(ErlNifEnv *env, ERL_NIF_TERM arg) {
ERL_NIF_TERM a;
int r;
if (enif_is_atom(env, arg)) {
a = enif_make_atom(env, "default");
if (enif_is_identical(a, arg)) {
return crypto_pwhash_ALG_DEFAULT;
}
a = enif_make_atom(env, "argon2i13");
if (enif_is_identical(a, arg)) {
return crypto_pwhash_ALG_ARGON2I13;
}
a = enif_make_atom(env, "argon2id13");
if (enif_is_identical(a, arg)) {
return crypto_pwhash_ALG_ARGON2ID13;
}
} else if (enif_get_int(env, arg, &r)) {
return r;
}
return 0;
}
ERL_NIF_TERM enacl_crypto_pwhash(ErlNifEnv *env, int argc,
ERL_NIF_TERM const argv[]) {
ErlNifBinary h, p, s;
size_t o, m;
int alg;
// Validate the arguments
if ((argc != 4) || (!enif_inspect_iolist_as_binary(env, argv[0], &p)) ||
if ((argc != 5) || (!enif_inspect_iolist_as_binary(env, argv[0], &p)) ||
(!enif_inspect_binary(env, argv[1], &s)) ||
!(o = enacl_pwhash_opslimit(env, argv[2])) ||
!(m = enacl_pwhash_memlimit(env, argv[3]))) {
!(m = enacl_pwhash_memlimit(env, argv[3])) ||
!(alg = enacl_pwhash_alg(env, argv[4]))) {
return enif_make_badarg(env);
}
@@ -93,7 +120,7 @@ ERL_NIF_TERM enacl_crypto_pwhash(ErlNifEnv *env, int argc,
}
if (crypto_pwhash(h.data, h.size, (char *)p.data, p.size, s.data, o, m,
crypto_pwhash_ALG_DEFAULT) != 0) {
alg) != 0) {
/* out of memory */
enif_release_binary(&h);
return enacl_internal_error(env);