Skip to content
This repository has been archived by the owner on Apr 4, 2020. It is now read-only.

Pwhash implementation #7

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
43 changes: 43 additions & 0 deletions lib/salty/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,49 @@ 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_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_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_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)
def pwhash_ALG_ARGON2I13, 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)
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)
def generichash_blake2b_BYTES, do: :erlang.exit(:salty_nif_not_loaded)
Expand Down
53 changes: 53 additions & 0 deletions lib/salty/pw_hash.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 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
75 changes: 75 additions & 0 deletions lib/salty/pw_hash_argon2i.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule Salty.PwHash.Argon2i 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

def saltbytes() do
C.pwhash_argon2i_SALTBYTES()
end

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

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

def pwhash(outlen, password, salt, opslimit, memlimit, alg \\ alg()) do
C.pwhash_argon2i(outlen, password, salt, opslimit, memlimit, alg)
end
end
75 changes: 75 additions & 0 deletions lib/salty/pw_hash_argon2id.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule Salty.PwHash.Argon2id 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.pwhash_argon2id_SALTBYTES()
end

def strbytes() do
C.pwhash_argon2id_STRBYTES()
end

def opslimit_min() do
C.pwhash_argon2id_OPSLIMIT_MIN()
end

def opslimit_max() do
C.pwhash_argon2id_OPSLIMIT_MAX()
end

def memlimit_min() do
C.pwhash_argon2id_MEMLIMIT_MIN()
end

def memlimit_max() do
C.pwhash_argon2id_MEMLIMIT_MAX()
end

def opslimit_interactive() do
C.pwhash_argon2id_OPSLIMIT_INTERACTIVE()
end

def memlimit_interactive() do
C.pwhash_argon2id_MEMLIMIT_INTERACTIVE()
end

def opslimit_moderate() do
C.pwhash_argon2id_OPSLIMIT_MODERATE()
end

def memlimit_moderate() do
C.pwhash_argon2id_MEMLIMIT_MODERATE()
end

def opslimit_sensitive() do
C.pwhash_argon2id_OPSLIMIT_SENSITIVE()
end

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
Loading