From eefde926e5c4310c5c39809ca7a5f87c194c19a0 Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Wed, 13 Dec 2017 17:21:12 +0800 Subject: [PATCH 01/12] pwhash implementation work in progress --- lib/salty/nif.ex | 6 ++++++ src/salty_nif.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index dce4a24..860e363 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -117,6 +117,12 @@ defmodule Salty.Nif do def core_hchacha20(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def core_hsalsa20(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_PASSWD_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) + def generichash_blake2b_BYTES_MIN, do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES_MAX, do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/src/salty_nif.c b/src/salty_nif.c index 30a97f2..e7deae5 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -428,7 +428,7 @@ ERL_NIF_TERM atom_primitive_sign;*/ static int salty_onload(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) { /* register the safe resource types for keys and private data */ - + /* cache atom values */ atom_ok = enif_make_atom(env, "ok"); atom_error = enif_make_atom(env, "error"); @@ -1174,6 +1174,44 @@ SALTY_FUNC(core_hsalsa20, 3) DO SALTY_CALL(crypto_core_hsalsa20(out.data, in.data, key.data, con.data), out); END_OK_WITH(out); +/** + * PWHASH Argon2i + */ +// SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); +// SALTY_CONST_INT64(pwhash_argon2id_ALG_ARGON2ID13); +// SALTY_CONST_INT64(pwhash_ALG_ARGON2ID13); +// SALTY_CONST_INT64(pwhash_argon2id_BYTES_MIN); +// SALTY_CONST_INT64(pwhash_argon2id_BYTES_MAX); +SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MIN); +// SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MAX); +SALTY_CONST_INT64(pwhash_argon2id_SALTBYTES); +// SALTY_CONST_INT64(pwhash_argon2id_STRBYTES); +// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); +// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MAX); +// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MIN); +// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MAX); +SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_INTERACTIVE); +SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_INTERACTIVE); +// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MODERATE); +// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MODERATE); +// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_SENSITIVE); +// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_SENSITIVE); + +SALTY_FUNC(pwhash, 6) DO + SALTY_INPUT_UINT64(0, outlen); + SALTY_INPUT_BIN(1, password, crypto_pwhash_argon2id_PASSWD_MIN); + SALTY_INPUT_BIN(2, salt, crypto_pwhash_argon2id_SALTBYTES); + SALTY_INPUT_UINT64(3, opslimit); + SALTY_INPUT_UINT64(4, memlimit); + SALTY_INPUT_UINT64(5, alg); + + SALTY_OUTPUT_BIN(hash, outlen); + + SALTY_CALL(crypto_pwhash( + hash.data, outlen, (const char *) password.data, password.size, salt.data, opslimit, memlimit, alg), + hash); +END_OK_WITH(hash); + /** * GENERICHASH Blake2b */ @@ -2030,7 +2068,7 @@ salty_exports[] = { SALTY_EXPORT_FUNC(aead_chacha20poly1305_encrypt, 5), SALTY_EXPORT_FUNC(aead_chacha20poly1305_encrypt_detached, 5), SALTY_EXPORT_FUNC(aead_chacha20poly1305_decrypt_detached, 6), - + SALTY_EXPORT_CONS(aead_chacha20poly1305_ietf_KEYBYTES, 0), SALTY_EXPORT_CONS(aead_chacha20poly1305_ietf_NSECBYTES, 0), SALTY_EXPORT_CONS(aead_chacha20poly1305_ietf_NPUBBYTES, 0), @@ -2115,6 +2153,12 @@ salty_exports[] = { SALTY_EXPORT_FUNC(core_hchacha20, 3), SALTY_EXPORT_FUNC(core_hsalsa20, 3), + SALTY_EXPORT_CONS(pwhash_argon2id_PASSWD_MIN, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_SALTBYTES, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_FUNC(pwhash, 6), + SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MIN, 0), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MAX, 0), SALTY_EXPORT_CONS(generichash_blake2b_BYTES, 0), From dfb3ee96a57e8307a26396d93fc13b7c396a2c57 Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Mon, 18 Dec 2017 16:12:33 +0800 Subject: [PATCH 02/12] implementating pwhash_str --- lib/salty/nif.ex | 2 ++ src/salty_nif.c | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index 860e363..c57a24f 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -119,9 +119,11 @@ defmodule Salty.Nif do def pwhash_argon2id_PASSWD_MIN, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_str(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES_MIN, do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES_MAX, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/src/salty_nif.c b/src/salty_nif.c index e7deae5..0dd41ba 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1185,7 +1185,7 @@ END_OK_WITH(out); SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MIN); // SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MAX); SALTY_CONST_INT64(pwhash_argon2id_SALTBYTES); -// SALTY_CONST_INT64(pwhash_argon2id_STRBYTES); +SALTY_CONST_INT64(pwhash_argon2id_STRBYTES); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MAX); // SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MIN); @@ -1212,6 +1212,18 @@ SALTY_FUNC(pwhash, 6) DO hash); END_OK_WITH(hash); +SALTY_FUNC(pwhash_str, 3) DO + SALTY_INPUT_BIN(0, password, crypto_pwhash_argon2id_PASSWD_MIN); + SALTY_INPUT_UINT64(1, opslimit); + SALTY_INPUT_UINT64(2, memlimit); + + SALTY_OUTPUT_BIN(hash, crypto_pwhash_argon2id_STRBYTES); + + SALTY_CALL(crypto_pwhash_str( + (char *) hash.data, (const char *) password.data, password.size, opslimit, memlimit), + hash); +END_OK_WITH(hash); + /** * GENERICHASH Blake2b */ @@ -2157,7 +2169,9 @@ salty_exports[] = { SALTY_EXPORT_CONS(pwhash_argon2id_SALTBYTES, 0), SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), SALTY_EXPORT_FUNC(pwhash, 6), + SALTY_EXPORT_FUNC(pwhash_str, 3), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MIN, 0), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MAX, 0), From 8652e2728d28e67cd3f61a9e4911556e16bbd45b Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 19 Dec 2017 10:37:28 +0800 Subject: [PATCH 03/12] implementation pwhash_str_alg work in progress --- lib/salty/nif.ex | 4 ++++ src/salty_nif.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index c57a24f..eef6e93 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -122,8 +122,12 @@ defmodule Salty.Nif do def pwhash_argon2id_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_ALG_ARGON2I13, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_str_alg(_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES_MIN, do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES_MAX, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/src/salty_nif.c b/src/salty_nif.c index 0dd41ba..9d6a195 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1177,9 +1177,9 @@ END_OK_WITH(out); /** * PWHASH Argon2i */ -// SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); -// SALTY_CONST_INT64(pwhash_argon2id_ALG_ARGON2ID13); -// SALTY_CONST_INT64(pwhash_ALG_ARGON2ID13); +SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); +SALTY_CONST_INT64(pwhash_argon2id_ALG_ARGON2ID13); +SALTY_CONST_INT64(pwhash_ALG_ARGON2ID13); // SALTY_CONST_INT64(pwhash_argon2id_BYTES_MIN); // SALTY_CONST_INT64(pwhash_argon2id_BYTES_MAX); SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MIN); @@ -1208,8 +1208,8 @@ SALTY_FUNC(pwhash, 6) DO SALTY_OUTPUT_BIN(hash, outlen); SALTY_CALL(crypto_pwhash( - hash.data, outlen, (const char *) password.data, password.size, salt.data, opslimit, memlimit, alg), - hash); + hash.data, outlen, (const char *) password.data, password.size, salt.data, opslimit, memlimit, alg), + hash); END_OK_WITH(hash); SALTY_FUNC(pwhash_str, 3) DO @@ -1220,8 +1220,23 @@ SALTY_FUNC(pwhash_str, 3) DO SALTY_OUTPUT_BIN(hash, crypto_pwhash_argon2id_STRBYTES); SALTY_CALL(crypto_pwhash_str( - (char *) hash.data, (const char *) password.data, password.size, opslimit, memlimit), - hash); + (char *) hash.data, (const char *) password.data, password.size, + opslimit, memlimit), + hash); +END_OK_WITH(hash); + +SALTY_FUNC(pwhash_str_alg, 4) DO + SALTY_INPUT_BIN(0, password, crypto_pwhash_argon2id_PASSWD_MIN); + SALTY_INPUT_UINT64(1, opslimit); + SALTY_INPUT_UINT64(2, memlimit); + SALTY_INPUT_UINT64(3, alg); + + SALTY_OUTPUT_BIN(hash, crypto_pwhash_argon2i_STRBYTES); + + SALTY_CALL(crypto_pwhash_str_alg( + (char *) hash.data, (const char *) password.data, password.size, + opslimit, memlimit, alg), + hash); END_OK_WITH(hash); /** @@ -2170,8 +2185,12 @@ salty_exports[] = { SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_ALG_ARGON2I13, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_ALG_ARGON2ID13, 0), + SALTY_EXPORT_CONS(pwhash_ALG_ARGON2ID13, 0), SALTY_EXPORT_FUNC(pwhash, 6), SALTY_EXPORT_FUNC(pwhash_str, 3), + SALTY_EXPORT_FUNC(pwhash_str_alg, 4), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MIN, 0), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MAX, 0), From d1d33827948f6dbcf98a3d48d1ab3efed2b82f7c Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Wed, 20 Dec 2017 19:34:20 -0800 Subject: [PATCH 04/12] adding str_verify and needs_rehash --- lib/salty/nif.ex | 2 ++ src/salty_nif.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index eef6e93..1b228dd 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -128,6 +128,8 @@ defmodule Salty.Nif do def pwhash(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str_alg(_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_str_verify(_,_), do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_str_needs_rehash(_,_), do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES_MIN, do: :erlang.exit(:salty_nif_not_loaded) def generichash_blake2b_BYTES_MAX, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/src/salty_nif.c b/src/salty_nif.c index 9d6a195..da4530d 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1239,6 +1239,26 @@ SALTY_FUNC(pwhash_str_alg, 4) DO hash); END_OK_WITH(hash); +SALTY_FUNC(pwhash_str_verify, 2) DO + SALTY_INPUT_BIN(0, str, crypto_pwhash_argon2id_PASSWD_MIN); + SALTY_INPUT_BIN(1, password, crypto_pwhash_argon2id_PASSWD_MIN); + + SALTY_OUTPUT_BIN(hash, crypto_pwhash_argon2i_STRBYTES); + + SALTY_CALL(crypto_pwhash_str_verify( + (char *) str.data, (const char *) password.data, password.size), + hash); +END_OK_WITH(hash); + +SALTY_FUNC(pwhash_str_needs_rehash, 2) DO + SALTY_INPUT_UINT64(1, opslimit); + SALTY_INPUT_UINT64(2, memlimit); + + SALTY_OUTPUT_BIN(hash, crypto_pwhash_argon2i_STRBYTES); + + SALTY_CALL(crypto_pwhash_str_needs_rehash((char *) hash.data, opslimit, memlimit), hash); +END_OK_WITH(hash); + /** * GENERICHASH Blake2b */ @@ -2191,6 +2211,8 @@ salty_exports[] = { SALTY_EXPORT_FUNC(pwhash, 6), SALTY_EXPORT_FUNC(pwhash_str, 3), SALTY_EXPORT_FUNC(pwhash_str_alg, 4), + SALTY_EXPORT_FUNC(pwhash_str_verify, 2), + SALTY_EXPORT_FUNC(pwhash_str_needs_rehash, 2), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MIN, 0), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MAX, 0), From f5390b180d2083ea84cffd2dcea8dfd4c1cfaaef Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 23 Jan 2018 17:18:56 +0800 Subject: [PATCH 05/12] adding constant pwhash_ALG_ARGON2I13 --- lib/salty/nif.ex | 1 + src/salty_nif.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index 1b228dd..8adc876 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -125,6 +125,7 @@ defmodule Salty.Nif do def pwhash_argon2i_ALG_ARGON2I13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_ALG_ARGON2I13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str_alg(_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) diff --git a/src/salty_nif.c b/src/salty_nif.c index da4530d..d0e2b88 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1180,6 +1180,7 @@ END_OK_WITH(out); SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); SALTY_CONST_INT64(pwhash_argon2id_ALG_ARGON2ID13); SALTY_CONST_INT64(pwhash_ALG_ARGON2ID13); +SALTY_CONST_INT64(pwhash_ALG_ARGON2I13); // SALTY_CONST_INT64(pwhash_argon2id_BYTES_MIN); // SALTY_CONST_INT64(pwhash_argon2id_BYTES_MAX); SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MIN); @@ -2208,6 +2209,7 @@ salty_exports[] = { SALTY_EXPORT_CONS(pwhash_argon2i_ALG_ARGON2I13, 0), SALTY_EXPORT_CONS(pwhash_argon2id_ALG_ARGON2ID13, 0), SALTY_EXPORT_CONS(pwhash_ALG_ARGON2ID13, 0), + SALTY_EXPORT_CONS(pwhash_ALG_ARGON2I13, 0), SALTY_EXPORT_FUNC(pwhash, 6), SALTY_EXPORT_FUNC(pwhash_str, 3), SALTY_EXPORT_FUNC(pwhash_str_alg, 4), From 55c24df20f84f4cf34695415b6aaaf5079be5b00 Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 30 Jan 2018 15:04:09 +0800 Subject: [PATCH 06/12] primitives argon2i and argon2id compiling --- lib/salty/nif.ex | 10 ++++- lib/salty/pw_hash.ex | 55 +++++++++++++++++++++++++ lib/salty/pw_hash_argon2i.ex | 23 +++++++++++ lib/salty/pw_hash_argon2id.ex | 75 +++++++++++++++++++++++++++++++++++ src/salty_nif.c | 74 ++++++++++++++++++++++++---------- 5 files changed, 214 insertions(+), 23 deletions(-) create mode 100644 lib/salty/pw_hash.ex create mode 100644 lib/salty/pw_hash_argon2i.ex create mode 100644 lib/salty/pw_hash_argon2id.ex diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index 8adc876..7264b49 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -118,6 +118,9 @@ defmodule Salty.Nif do def core_hsalsa20(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_PASSWD_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_PASSWD_MAX, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_PASSWD_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_PASSWD_MAX, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) @@ -126,7 +129,12 @@ defmodule Salty.Nif do def pwhash_argon2id_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_ALG_ARGON2I13, do: :erlang.exit(:salty_nif_not_loaded) - def pwhash(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_BYTES_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_BYTES_MAX, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_BYTES_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_BYTES_MAX, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i(_,_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str(_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str_alg(_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded) def pwhash_str_verify(_,_), do: :erlang.exit(:salty_nif_not_loaded) diff --git a/lib/salty/pw_hash.ex b/lib/salty/pw_hash.ex new file mode 100644 index 0000000..e962ab9 --- /dev/null +++ b/lib/salty/pw_hash.ex @@ -0,0 +1,55 @@ +defmodule Salty.PwHash do + defmacro __using__(opts) do + quote do + @behaviour Salty.PwHash + alias Salty.Nif, as: C + end + end + + def primitive do + Salty.PwHash.Argon2id + end + + @callback alg() :: non_neg_integer() + + @callback bytes_min() :: non_neg_integer() + + @callback bytes_max() :: non_neg_integer() + + @callback passwd_min() :: non_neg_integer() + + @callback passwd_max() :: non_neg_integer() + + @callback saltbytes() :: non_neg_integer() + + @callback strbytes() :: non_neg_integer() + + @callback strprefix() :: binary() + + @callback opslimit_min() :: non_neg_integer() + + @callback opslimit_max() :: non_neg_integer() + + @callback memlimit_min() :: non_neg_integer() + + @callback memlimit_max() :: non_neg_integer() + + @callback opslimit_interactive() :: non_neg_integer() + + @callback memlimit_interactive() :: non_neg_integer() + + @callback opslimit_moderate() :: non_neg_integer() + + @callback memlimit_moderate() :: non_neg_integer() + + @callback opslimit_sensitive() :: non_neg_integer() + + @callback memlimit_sensitive() :: non_neg_integer() + + @callback pwhash(non_neg_integer(), binary(), binary(), non_neg_integer(), non_neg_integer(),non_neg_integer()) :: {:ok, binary()} | {:error, atom()} + + @callback pwhash_str() :: {:ok, binary()} | {:error, atom()} + + # @callback pwhash_str() + +end diff --git a/lib/salty/pw_hash_argon2i.ex b/lib/salty/pw_hash_argon2i.ex new file mode 100644 index 0000000..56fdedb --- /dev/null +++ b/lib/salty/pw_hash_argon2i.ex @@ -0,0 +1,23 @@ +defmodule Salty.PwHashArgon2i do + use Salty.PwHash + + def alg() do + C.pwhash_argon2i_ALG_ARGON2I13() + end + + def bytes_min() do + C.pwhash_argon2i_BYTES_MIN() + end + + def bytes_max() do + C.pwhash_argon2i_BYTES_MAX() + end + + def passwd_min() do + C.pwhash_argon2i_PASSWD_MIN() + end + + def passwd_max() do + C.pwhash_argon2i_PASSWD_MAX() + end +end diff --git a/lib/salty/pw_hash_argon2id.ex b/lib/salty/pw_hash_argon2id.ex new file mode 100644 index 0000000..9e38b5a --- /dev/null +++ b/lib/salty/pw_hash_argon2id.ex @@ -0,0 +1,75 @@ +defmodule Salty.PwHashArgon2id do + use Salty.PwHash + + def alg() do + C.pwhash_argon2id_ALG_ARGON2ID13() + end + + def bytes_min() do + C.pwhash_argon2id_BYTES_MIN() + end + + def bytes_max() do + C.pwhash_argon2id_BYTES_MAX() + end + + def passwd_min() do + C.pwhash_argon2id_PASSWD_MIN() + end + + def passwd_max() do + C.pwhash_argon2id_PASSWD_MAX() + end + + # def saltbytes() do + # C. + # end + + # def strbytes() do + # C. + # end + + # def strprefix() do + # C. + # end + + # def opslimit_min() do + # C. + # end + + # def opslimit_max() do + # C. + # end + + # def memlimit_min() do + # C. + # end + + # def memlimit_max() do + # C. + # end + + # def opslimit_interactive() do + # C. + # end + + # def memlimit_interactive() do + # C. + # end + + # def opslimit_moderate() do + # C. + # end + + # def memlimit_moderate() do + # C. + # end + + # def opslimit_sensitive() do + # C. + # end + + # def memlimit_sensitive() do + # C. + # end +end diff --git a/src/salty_nif.c b/src/salty_nif.c index d0e2b88..a723b36 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1175,16 +1175,18 @@ SALTY_FUNC(core_hsalsa20, 3) DO END_OK_WITH(out); /** - * PWHASH Argon2i + * PWHASH Argon2id */ -SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); SALTY_CONST_INT64(pwhash_argon2id_ALG_ARGON2ID13); SALTY_CONST_INT64(pwhash_ALG_ARGON2ID13); -SALTY_CONST_INT64(pwhash_ALG_ARGON2I13); -// SALTY_CONST_INT64(pwhash_argon2id_BYTES_MIN); -// SALTY_CONST_INT64(pwhash_argon2id_BYTES_MAX); +SALTY_CONST_INT64(pwhash_argon2id_BYTES_MIN); +SALTY_CONST_INT64(pwhash_argon2id_BYTES_MAX); +SALTY_CONST_INT64(pwhash_argon2i_BYTES_MIN); +SALTY_CONST_INT64(pwhash_argon2i_BYTES_MAX); SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MIN); -// SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MAX); +SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MAX); +SALTY_CONST_INT64(pwhash_argon2i_PASSWD_MIN); +SALTY_CONST_INT64(pwhash_argon2i_PASSWD_MAX); SALTY_CONST_INT64(pwhash_argon2id_SALTBYTES); SALTY_CONST_INT64(pwhash_argon2id_STRBYTES); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); @@ -1198,7 +1200,27 @@ SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_INTERACTIVE); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_SENSITIVE); // SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_SENSITIVE); -SALTY_FUNC(pwhash, 6) DO +SALTY_FUNC(pwhash_argon2id, 6) DO + SALTY_INPUT_UINT64(0, outlen); + SALTY_INPUT_BIN(1, password, crypto_pwhash_argon2id_PASSWD_MIN); + SALTY_INPUT_BIN(2, salt, crypto_pwhash_argon2id_SALTBYTES); + SALTY_INPUT_UINT64(3, opslimit); + SALTY_INPUT_UINT64(4, memlimit); + SALTY_INPUT_UINT64(5, alg); + + SALTY_OUTPUT_BIN(hash, outlen); + + SALTY_CALL(crypto_pwhash( + hash.data, outlen, (const char *) password.data, password.size, salt.data, opslimit, memlimit, alg), + hash); +END_OK_WITH(hash); + +/** + * PWHASH Argon2i + */ +SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); +SALTY_CONST_INT64(pwhash_ALG_ARGON2I13); +SALTY_FUNC(pwhash_argon2i, 6) DO SALTY_INPUT_UINT64(0, outlen); SALTY_INPUT_BIN(1, password, crypto_pwhash_argon2id_PASSWD_MIN); SALTY_INPUT_BIN(2, salt, crypto_pwhash_argon2id_SALTBYTES); @@ -2201,21 +2223,6 @@ salty_exports[] = { SALTY_EXPORT_FUNC(core_hchacha20, 3), SALTY_EXPORT_FUNC(core_hsalsa20, 3), - SALTY_EXPORT_CONS(pwhash_argon2id_PASSWD_MIN, 0), - SALTY_EXPORT_CONS(pwhash_argon2id_SALTBYTES, 0), - SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), - SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), - SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), - SALTY_EXPORT_CONS(pwhash_argon2i_ALG_ARGON2I13, 0), - SALTY_EXPORT_CONS(pwhash_argon2id_ALG_ARGON2ID13, 0), - SALTY_EXPORT_CONS(pwhash_ALG_ARGON2ID13, 0), - SALTY_EXPORT_CONS(pwhash_ALG_ARGON2I13, 0), - SALTY_EXPORT_FUNC(pwhash, 6), - SALTY_EXPORT_FUNC(pwhash_str, 3), - SALTY_EXPORT_FUNC(pwhash_str_alg, 4), - SALTY_EXPORT_FUNC(pwhash_str_verify, 2), - SALTY_EXPORT_FUNC(pwhash_str_needs_rehash, 2), - SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MIN, 0), SALTY_EXPORT_CONS(generichash_blake2b_BYTES_MAX, 0), SALTY_EXPORT_CONS(generichash_blake2b_BYTES, 0), @@ -2271,6 +2278,29 @@ salty_exports[] = { SALTY_EXPORT_FUNC(onetimeauth_poly1305_final, 1), SALTY_EXPORT_FUNC(onetimeauth_poly1305_final_verify, 2), + SALTY_EXPORT_CONS(pwhash_argon2id_PASSWD_MIN, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_PASSWD_MAX, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_PASSWD_MIN, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_PASSWD_MAX, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_SALTBYTES, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_ALG_ARGON2I13, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_ALG_ARGON2ID13, 0), + SALTY_EXPORT_CONS(pwhash_ALG_ARGON2ID13, 0), + SALTY_EXPORT_CONS(pwhash_ALG_ARGON2I13, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_BYTES_MIN, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_BYTES_MAX, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_BYTES_MIN, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_BYTES_MAX, 0), + SALTY_EXPORT_FUNC(pwhash_argon2id, 6), + SALTY_EXPORT_FUNC(pwhash_argon2i, 6), + SALTY_EXPORT_FUNC(pwhash_str, 3), + SALTY_EXPORT_FUNC(pwhash_str_alg, 4), + SALTY_EXPORT_FUNC(pwhash_str_verify, 2), + SALTY_EXPORT_FUNC(pwhash_str_needs_rehash, 2), + SALTY_EXPORT_CONS(scalarmult_curve25519_BYTES, 0), SALTY_EXPORT_CONS(scalarmult_curve25519_SCALARBYTES, 0), SALTY_EXPORT_FUNC(scalarmult_curve25519_base, 1), From 258960465f2ffafc70ef5e353bff86d29da7a1f5 Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 30 Jan 2018 15:22:04 +0800 Subject: [PATCH 07/12] ading saltbytes --- lib/salty/nif.ex | 1 + lib/salty/pw_hash_argon2i.ex | 4 ++++ lib/salty/pw_hash_argon2id.ex | 6 +++--- src/salty_nif.c | 6 ++++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index 7264b49..48759d1 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -122,6 +122,7 @@ defmodule Salty.Nif do def pwhash_argon2i_PASSWD_MIN, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_PASSWD_MAX, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/lib/salty/pw_hash_argon2i.ex b/lib/salty/pw_hash_argon2i.ex index 56fdedb..4f12e5c 100644 --- a/lib/salty/pw_hash_argon2i.ex +++ b/lib/salty/pw_hash_argon2i.ex @@ -20,4 +20,8 @@ defmodule Salty.PwHashArgon2i do def passwd_max() do C.pwhash_argon2i_PASSWD_MAX() end + + def saltbytes() do + C.pwhash_argon2i_SALTBYTES() + end end diff --git a/lib/salty/pw_hash_argon2id.ex b/lib/salty/pw_hash_argon2id.ex index 9e38b5a..51f1721 100644 --- a/lib/salty/pw_hash_argon2id.ex +++ b/lib/salty/pw_hash_argon2id.ex @@ -21,9 +21,9 @@ defmodule Salty.PwHashArgon2id do C.pwhash_argon2id_PASSWD_MAX() end - # def saltbytes() do - # C. - # end + def saltbytes() do + C.pwhash_argon2id_SALTBYTES() + end # def strbytes() do # C. diff --git a/src/salty_nif.c b/src/salty_nif.c index a723b36..657478d 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1178,7 +1178,9 @@ END_OK_WITH(out); * PWHASH Argon2id */ SALTY_CONST_INT64(pwhash_argon2id_ALG_ARGON2ID13); +SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); SALTY_CONST_INT64(pwhash_ALG_ARGON2ID13); +SALTY_CONST_INT64(pwhash_ALG_ARGON2I13); SALTY_CONST_INT64(pwhash_argon2id_BYTES_MIN); SALTY_CONST_INT64(pwhash_argon2id_BYTES_MAX); SALTY_CONST_INT64(pwhash_argon2i_BYTES_MIN); @@ -1188,6 +1190,7 @@ SALTY_CONST_INT64(pwhash_argon2id_PASSWD_MAX); SALTY_CONST_INT64(pwhash_argon2i_PASSWD_MIN); SALTY_CONST_INT64(pwhash_argon2i_PASSWD_MAX); SALTY_CONST_INT64(pwhash_argon2id_SALTBYTES); +SALTY_CONST_INT64(pwhash_argon2i_SALTBYTES); SALTY_CONST_INT64(pwhash_argon2id_STRBYTES); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MAX); @@ -1218,8 +1221,6 @@ END_OK_WITH(hash); /** * PWHASH Argon2i */ -SALTY_CONST_INT64(pwhash_argon2i_ALG_ARGON2I13); -SALTY_CONST_INT64(pwhash_ALG_ARGON2I13); SALTY_FUNC(pwhash_argon2i, 6) DO SALTY_INPUT_UINT64(0, outlen); SALTY_INPUT_BIN(1, password, crypto_pwhash_argon2id_PASSWD_MIN); @@ -2283,6 +2284,7 @@ salty_exports[] = { SALTY_EXPORT_CONS(pwhash_argon2i_PASSWD_MIN, 0), SALTY_EXPORT_CONS(pwhash_argon2i_PASSWD_MAX, 0), SALTY_EXPORT_CONS(pwhash_argon2id_SALTBYTES, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_SALTBYTES, 0), SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), From 3bf931c1819274ed740acaf334deb703855adbe8 Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 30 Jan 2018 15:27:41 +0800 Subject: [PATCH 08/12] adding strbytes --- lib/salty/nif.ex | 1 + lib/salty/pw_hash_argon2i.ex | 4 ++++ lib/salty/pw_hash_argon2id.ex | 6 +++--- src/salty_nif.c | 2 ++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index 48759d1..519891a 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -124,6 +124,7 @@ defmodule Salty.Nif do def pwhash_argon2id_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_ALG_ARGON2I13, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/lib/salty/pw_hash_argon2i.ex b/lib/salty/pw_hash_argon2i.ex index 4f12e5c..329b072 100644 --- a/lib/salty/pw_hash_argon2i.ex +++ b/lib/salty/pw_hash_argon2i.ex @@ -24,4 +24,8 @@ defmodule Salty.PwHashArgon2i do def saltbytes() do C.pwhash_argon2i_SALTBYTES() end + + def strbytes() do + C.pwhash_argon2i_STRBYTES() + end end diff --git a/lib/salty/pw_hash_argon2id.ex b/lib/salty/pw_hash_argon2id.ex index 51f1721..261fd32 100644 --- a/lib/salty/pw_hash_argon2id.ex +++ b/lib/salty/pw_hash_argon2id.ex @@ -25,9 +25,9 @@ defmodule Salty.PwHashArgon2id do C.pwhash_argon2id_SALTBYTES() end - # def strbytes() do - # C. - # end + def strbytes() do + C.pwhash_argon2id_STRBYTES() + end # def strprefix() do # C. diff --git a/src/salty_nif.c b/src/salty_nif.c index 657478d..dda5ddf 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1192,6 +1192,7 @@ SALTY_CONST_INT64(pwhash_argon2i_PASSWD_MAX); SALTY_CONST_INT64(pwhash_argon2id_SALTBYTES); SALTY_CONST_INT64(pwhash_argon2i_SALTBYTES); SALTY_CONST_INT64(pwhash_argon2id_STRBYTES); +SALTY_CONST_INT64(pwhash_argon2i_STRBYTES); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); // SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MAX); // SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MIN); @@ -2288,6 +2289,7 @@ salty_exports[] = { SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_STRBYTES, 0), SALTY_EXPORT_CONS(pwhash_argon2i_ALG_ARGON2I13, 0), SALTY_EXPORT_CONS(pwhash_argon2id_ALG_ARGON2ID13, 0), SALTY_EXPORT_CONS(pwhash_ALG_ARGON2ID13, 0), From f269550bf8ee982a6bc06a2aedcae2cd266dd539 Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 30 Jan 2018 16:01:19 +0800 Subject: [PATCH 09/12] adding ops limit min and max --- lib/salty/nif.ex | 4 ++++ lib/salty/pw_hash.ex | 2 -- lib/salty/pw_hash_argon2i.ex | 8 ++++++++ lib/salty/pw_hash_argon2id.ex | 16 ++++++---------- src/salty_nif.c | 10 ++++++++-- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index 519891a..3304a6d 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -125,6 +125,10 @@ defmodule Salty.Nif do def pwhash_argon2i_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_STRBYTES, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_OPSLIMIT_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_OPSLIMIT_MAX, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_OPSLIMIT_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_OPSLIMIT_MAX, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_ALG_ARGON2I13, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/lib/salty/pw_hash.ex b/lib/salty/pw_hash.ex index e962ab9..0a1025f 100644 --- a/lib/salty/pw_hash.ex +++ b/lib/salty/pw_hash.ex @@ -24,8 +24,6 @@ defmodule Salty.PwHash do @callback strbytes() :: non_neg_integer() - @callback strprefix() :: binary() - @callback opslimit_min() :: non_neg_integer() @callback opslimit_max() :: non_neg_integer() diff --git a/lib/salty/pw_hash_argon2i.ex b/lib/salty/pw_hash_argon2i.ex index 329b072..c217133 100644 --- a/lib/salty/pw_hash_argon2i.ex +++ b/lib/salty/pw_hash_argon2i.ex @@ -28,4 +28,12 @@ defmodule Salty.PwHashArgon2i do def strbytes() do C.pwhash_argon2i_STRBYTES() end + + def opslimit_min() do + C.pwhash_argon2i_OPSLIMIT_MIN() + end + + def opslimit_max() do + C.pwhash_argon2i_OPSLIMIT_MAX() + end end diff --git a/lib/salty/pw_hash_argon2id.ex b/lib/salty/pw_hash_argon2id.ex index 261fd32..dc16a1a 100644 --- a/lib/salty/pw_hash_argon2id.ex +++ b/lib/salty/pw_hash_argon2id.ex @@ -29,17 +29,13 @@ defmodule Salty.PwHashArgon2id do C.pwhash_argon2id_STRBYTES() end - # def strprefix() do - # C. - # end - - # def opslimit_min() do - # C. - # end + def opslimit_min() do + C.pwhash_argon2id_OPSLIMIT_MIN() + end - # def opslimit_max() do - # C. - # end + def opslimit_max() do + C.pwhash_argon2id_OPSLIMIT_MAX() + end # def memlimit_min() do # C. diff --git a/src/salty_nif.c b/src/salty_nif.c index dda5ddf..8b91da8 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1193,8 +1193,10 @@ SALTY_CONST_INT64(pwhash_argon2id_SALTBYTES); SALTY_CONST_INT64(pwhash_argon2i_SALTBYTES); SALTY_CONST_INT64(pwhash_argon2id_STRBYTES); SALTY_CONST_INT64(pwhash_argon2i_STRBYTES); -// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); -// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MAX); +SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); +SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MAX); +SALTY_CONST_INT64(pwhash_argon2i_OPSLIMIT_MIN); +SALTY_CONST_INT64(pwhash_argon2i_OPSLIMIT_MAX); // SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MIN); // SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MAX); SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_INTERACTIVE); @@ -2286,6 +2288,10 @@ salty_exports[] = { SALTY_EXPORT_CONS(pwhash_argon2i_PASSWD_MAX, 0), SALTY_EXPORT_CONS(pwhash_argon2id_SALTBYTES, 0), SALTY_EXPORT_CONS(pwhash_argon2i_SALTBYTES, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_MIN, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_MAX, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_OPSLIMIT_MIN, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_OPSLIMIT_MAX, 0), SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), From 443bf3b6338c6ccb72b1954a1eef5cd80e13d0fc Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 30 Jan 2018 16:19:09 +0800 Subject: [PATCH 10/12] adding mem and ops limits --- lib/salty/nif.ex | 14 ++++++++++ lib/salty/pw_hash_argon2i.ex | 32 +++++++++++++++++++++++ lib/salty/pw_hash_argon2id.ex | 48 +++++++++++++++++------------------ src/salty_nif.c | 26 ++++++++++++++----- 4 files changed, 90 insertions(+), 30 deletions(-) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index 3304a6d..315e9d6 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -129,8 +129,22 @@ defmodule Salty.Nif do def pwhash_argon2id_OPSLIMIT_MAX, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_OPSLIMIT_MIN, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_OPSLIMIT_MAX, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_MEMLIMIT_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_MEMLIMIT_MAX, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_MEMLIMIT_MIN, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_MEMLIMIT_MAX, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_OPSLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_MEMLIMIT_INTERACTIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_OPSLIMIT_MODERATE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_MEMLIMIT_MODERATE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_OPSLIMIT_MODERATE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_MEMLIMIT_MODERATE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_OPSLIMIT_SENSITIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2id_MEMLIMIT_SENSITIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_OPSLIMIT_SENSITIVE, do: :erlang.exit(:salty_nif_not_loaded) + def pwhash_argon2i_MEMLIMIT_SENSITIVE, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2i_ALG_ARGON2I13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_argon2id_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) def pwhash_ALG_ARGON2ID13, do: :erlang.exit(:salty_nif_not_loaded) diff --git a/lib/salty/pw_hash_argon2i.ex b/lib/salty/pw_hash_argon2i.ex index c217133..4c4425f 100644 --- a/lib/salty/pw_hash_argon2i.ex +++ b/lib/salty/pw_hash_argon2i.ex @@ -36,4 +36,36 @@ defmodule Salty.PwHashArgon2i do def opslimit_max() do C.pwhash_argon2i_OPSLIMIT_MAX() end + + def memlimit_min() do + C.pwhash_argon2i_MEMLIMIT_MIN() + end + + def memlimit_max() do + C.pwhash_argon2i_MEMLIMIT_MAX() + end + + def opslimit_interactive() do + C.pwhash_argon2i_OPSLIMIT_INTERACTIVE() + end + + def memlimit_interactive() do + C.pwhash_argon2i_MEMLIMIT_INTERACTIVE() + end + + def opslimit_moderate() do + C.pwhash_argon2i_OPSLIMIT_MODERATE() + end + + def memlimit_moderate() do + C.pwhash_argon2i_MEMLIMIT_MODERATE + end + + def opslimit_sensitive() do + C.pwhash_argon2i_OPSLIMIT_SENSITIVE() + end + + def memlimit_sensitive() do + C.pwhash_argon2i_MEMLIMIT_SENSITIVE() + end end diff --git a/lib/salty/pw_hash_argon2id.ex b/lib/salty/pw_hash_argon2id.ex index dc16a1a..a059420 100644 --- a/lib/salty/pw_hash_argon2id.ex +++ b/lib/salty/pw_hash_argon2id.ex @@ -37,35 +37,35 @@ defmodule Salty.PwHashArgon2id do C.pwhash_argon2id_OPSLIMIT_MAX() end - # def memlimit_min() do - # C. - # end + def memlimit_min() do + C.pwhash_argon2id_MEMLIMIT_MIN() + end - # def memlimit_max() do - # C. - # end + def memlimit_max() do + C.pwhash_argon2id_MEMLIMIT_MAX() + end - # def opslimit_interactive() do - # C. - # end + def opslimit_interactive() do + C.pwhash_argon2id_OPSLIMIT_INTERACTIVE() + end - # def memlimit_interactive() do - # C. - # end + def memlimit_interactive() do + C.pwhash_argon2id_MEMLIMIT_INTERACTIVE() + end - # def opslimit_moderate() do - # C. - # end + def opslimit_moderate() do + C.pwhash_argon2id_OPSLIMIT_MODERATE() + end - # def memlimit_moderate() do - # C. - # end + def memlimit_moderate() do + C.pwhash_argon2id_MEMLIMIT_MODERATE + end - # def opslimit_sensitive() do - # C. - # end + def opslimit_sensitive() do + C.pwhash_argon2id_OPSLIMIT_SENSITIVE() + end - # def memlimit_sensitive() do - # C. - # end + def memlimit_sensitive() do + C.pwhash_argon2id_MEMLIMIT_SENSITIVE() + end end diff --git a/src/salty_nif.c b/src/salty_nif.c index 8b91da8..306c839 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1197,14 +1197,18 @@ SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MIN); SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MAX); SALTY_CONST_INT64(pwhash_argon2i_OPSLIMIT_MIN); SALTY_CONST_INT64(pwhash_argon2i_OPSLIMIT_MAX); -// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MIN); -// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MAX); SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_INTERACTIVE); SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_INTERACTIVE); -// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MODERATE); -// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MODERATE); -// SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_SENSITIVE); -// SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_SENSITIVE); +SALTY_CONST_INT64(pwhash_argon2i_OPSLIMIT_INTERACTIVE); +SALTY_CONST_INT64(pwhash_argon2i_MEMLIMIT_INTERACTIVE); +SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_MODERATE); +SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_MODERATE); +SALTY_CONST_INT64(pwhash_argon2i_OPSLIMIT_MODERATE); +SALTY_CONST_INT64(pwhash_argon2i_MEMLIMIT_MODERATE); +SALTY_CONST_INT64(pwhash_argon2id_OPSLIMIT_SENSITIVE); +SALTY_CONST_INT64(pwhash_argon2id_MEMLIMIT_SENSITIVE); +SALTY_CONST_INT64(pwhash_argon2i_OPSLIMIT_SENSITIVE); +SALTY_CONST_INT64(pwhash_argon2i_MEMLIMIT_SENSITIVE); SALTY_FUNC(pwhash_argon2id, 6) DO SALTY_INPUT_UINT64(0, outlen); @@ -2294,6 +2298,16 @@ salty_exports[] = { SALTY_EXPORT_CONS(pwhash_argon2i_OPSLIMIT_MAX, 0), SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_INTERACTIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_OPSLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_MEMLIMIT_INTERACTIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_MODERATE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_MODERATE, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_OPSLIMIT_MODERATE, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_MEMLIMIT_MODERATE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_OPSLIMIT_SENSITIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2id_MEMLIMIT_SENSITIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_OPSLIMIT_SENSITIVE, 0), + SALTY_EXPORT_CONS(pwhash_argon2i_MEMLIMIT_SENSITIVE, 0), SALTY_EXPORT_CONS(pwhash_argon2id_STRBYTES, 0), SALTY_EXPORT_CONS(pwhash_argon2i_STRBYTES, 0), SALTY_EXPORT_CONS(pwhash_argon2i_ALG_ARGON2I13, 0), From 3d82ce8698a18e9436dd8554d7aed6ea37602f85 Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 30 Jan 2018 17:57:44 +0800 Subject: [PATCH 11/12] pwhash for argon2i and argon2id --- lib/salty/pw_hash.ex | 4 ++-- lib/salty/pw_hash_argon2i.ex | 6 +++++- lib/salty/pw_hash_argon2id.ex | 6 +++++- src/salty_nif.c | 28 ++++++++++++++++++++++++---- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/salty/pw_hash.ex b/lib/salty/pw_hash.ex index 0a1025f..8c117e4 100644 --- a/lib/salty/pw_hash.ex +++ b/lib/salty/pw_hash.ex @@ -44,9 +44,9 @@ defmodule Salty.PwHash do @callback memlimit_sensitive() :: non_neg_integer() - @callback pwhash(non_neg_integer(), binary(), binary(), non_neg_integer(), non_neg_integer(),non_neg_integer()) :: {:ok, binary()} | {:error, atom()} + @callback pwhash(non_neg_integer(), binary(), binary(), non_neg_integer(), non_neg_integer(), non_neg_integer()) :: {:ok, binary()} | {:error, atom()} - @callback pwhash_str() :: {:ok, binary()} | {:error, atom()} + # @callback pwhash_str() :: {:ok, binary()} | {:error, atom()} # @callback pwhash_str() diff --git a/lib/salty/pw_hash_argon2i.ex b/lib/salty/pw_hash_argon2i.ex index 4c4425f..aac118f 100644 --- a/lib/salty/pw_hash_argon2i.ex +++ b/lib/salty/pw_hash_argon2i.ex @@ -58,7 +58,7 @@ defmodule Salty.PwHashArgon2i do end def memlimit_moderate() do - C.pwhash_argon2i_MEMLIMIT_MODERATE + C.pwhash_argon2i_MEMLIMIT_MODERATE() end def opslimit_sensitive() do @@ -68,4 +68,8 @@ defmodule Salty.PwHashArgon2i do def memlimit_sensitive() do C.pwhash_argon2i_MEMLIMIT_SENSITIVE() end + + def pwhash(outlen, password, salt, opslimit, memlimit, alg) do + C.pwhash_argon2i(outlen, password, salt, opslimit, memlimit, alg()) + end end diff --git a/lib/salty/pw_hash_argon2id.ex b/lib/salty/pw_hash_argon2id.ex index a059420..6d3c886 100644 --- a/lib/salty/pw_hash_argon2id.ex +++ b/lib/salty/pw_hash_argon2id.ex @@ -58,7 +58,7 @@ defmodule Salty.PwHashArgon2id do end def memlimit_moderate() do - C.pwhash_argon2id_MEMLIMIT_MODERATE + C.pwhash_argon2id_MEMLIMIT_MODERATE() end def opslimit_sensitive() do @@ -68,4 +68,8 @@ defmodule Salty.PwHashArgon2id do def memlimit_sensitive() do C.pwhash_argon2id_MEMLIMIT_SENSITIVE() end + + def pwhash(outlen, password, salt, opslimit, memlimit, alg \\ alg()) do + C.pwhash_argon2id(outlen, password, salt, opslimit, memlimit, alg) + end end diff --git a/src/salty_nif.c b/src/salty_nif.c index 306c839..3fc6d53 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -379,6 +379,26 @@ crypto_onetimeauth_poly1305_final_verify(crypto_onetimeauth_poly1305_state *stat return crypto_verify_16(h, correct); } +int crypto_pwhash_argon2i(unsigned char * const out, + unsigned long long outlen, + const char * const passwd, + unsigned long long passwdlen, + const unsigned char * const salt, + unsigned long long opslimit, size_t memlimit, + int alg) { + return crypto_pwhash(out, outlen, passwd, passwdlen, salt, opslimit, memlimit, alg); +} + +int crypto_pwhash_argon2id(unsigned char * const out, + unsigned long long outlen, + const char * const passwd, + unsigned long long passwdlen, + const unsigned char * const salt, + unsigned long long opslimit, size_t memlimit, + int alg) { + return crypto_pwhash(out, outlen, passwd, passwdlen, salt, opslimit, memlimit, alg); +} + int crypto_secretbox_xsalsa20poly1305_easy(unsigned char *c, const unsigned char *m, @@ -1220,7 +1240,7 @@ SALTY_FUNC(pwhash_argon2id, 6) DO SALTY_OUTPUT_BIN(hash, outlen); - SALTY_CALL(crypto_pwhash( + SALTY_CALL(crypto_pwhash_argon2id( hash.data, outlen, (const char *) password.data, password.size, salt.data, opslimit, memlimit, alg), hash); END_OK_WITH(hash); @@ -1230,15 +1250,15 @@ END_OK_WITH(hash); */ SALTY_FUNC(pwhash_argon2i, 6) DO SALTY_INPUT_UINT64(0, outlen); - SALTY_INPUT_BIN(1, password, crypto_pwhash_argon2id_PASSWD_MIN); - SALTY_INPUT_BIN(2, salt, crypto_pwhash_argon2id_SALTBYTES); + SALTY_INPUT_BIN(1, password, crypto_pwhash_argon2i_PASSWD_MIN); + SALTY_INPUT_BIN(2, salt, crypto_pwhash_argon2i_SALTBYTES); SALTY_INPUT_UINT64(3, opslimit); SALTY_INPUT_UINT64(4, memlimit); SALTY_INPUT_UINT64(5, alg); SALTY_OUTPUT_BIN(hash, outlen); - SALTY_CALL(crypto_pwhash( + SALTY_CALL(crypto_pwhash_argon2i( hash.data, outlen, (const char *) password.data, password.size, salt.data, opslimit, memlimit, alg), hash); END_OK_WITH(hash); From 57c144e7d2ae8e8b82b43279375fc5e47650d66c Mon Sep 17 00:00:00 2001 From: daveed-al <33853903+daveed-al@users.noreply.github.com> Date: Tue, 30 Jan 2018 18:09:43 +0800 Subject: [PATCH 12/12] update module name --- lib/salty/pw_hash_argon2i.ex | 6 +++--- lib/salty/pw_hash_argon2id.ex | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/salty/pw_hash_argon2i.ex b/lib/salty/pw_hash_argon2i.ex index aac118f..c40784e 100644 --- a/lib/salty/pw_hash_argon2i.ex +++ b/lib/salty/pw_hash_argon2i.ex @@ -1,4 +1,4 @@ -defmodule Salty.PwHashArgon2i do +defmodule Salty.PwHash.Argon2i do use Salty.PwHash def alg() do @@ -69,7 +69,7 @@ defmodule Salty.PwHashArgon2i do C.pwhash_argon2i_MEMLIMIT_SENSITIVE() end - def pwhash(outlen, password, salt, opslimit, memlimit, alg) do - C.pwhash_argon2i(outlen, password, salt, opslimit, memlimit, alg()) + def pwhash(outlen, password, salt, opslimit, memlimit, alg \\ alg()) do + C.pwhash_argon2i(outlen, password, salt, opslimit, memlimit, alg) end end diff --git a/lib/salty/pw_hash_argon2id.ex b/lib/salty/pw_hash_argon2id.ex index 6d3c886..495894c 100644 --- a/lib/salty/pw_hash_argon2id.ex +++ b/lib/salty/pw_hash_argon2id.ex @@ -1,4 +1,4 @@ -defmodule Salty.PwHashArgon2id do +defmodule Salty.PwHash.Argon2id do use Salty.PwHash def alg() do