Skip to content

Commit

Permalink
Make the key optional in crypto_generichash (blake2d) (#4)
Browse files Browse the repository at this point in the history
* Make key optional for Blake2d bindings

* Test

* Rename original has to hash_key

* Add missing export
  • Loading branch information
TheWongGuy authored Dec 3, 2021
1 parent 83543ec commit 49fa935
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/salty/generichash.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Salty.Generichash do
@callback bytes_min() :: non_neg_integer()

@callback bytes_max() :: non_neg_integer()

@callback bytes() :: non_neg_integer()

@callback keybytes_min() :: non_neg_integer()
Expand All @@ -23,6 +23,8 @@ defmodule Salty.Generichash do

@callback keybytes :: non_neg_integer()

@callback hash(binary(), binary()) :: {:ok, binary()} | {:error, atom()}

@callback hash(binary(), binary(), binary()) :: {:ok, binary()} | {:error, atom()}

@callback init(binary(), non_neg_integer()) :: {:ok, binary()} | {:error, atom()}
Expand Down
6 changes: 5 additions & 1 deletion lib/salty/generichash_blake2b.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ defmodule Salty.Generichash.Blake2b do
C.generichash_blake2b_PERSONALBYTES()
end

def hash(outlen, data) do
C.generichash_blake2b(outlen, data)
end

def hash(outlen, data, key) do
C.generichash_blake2b(outlen, data, key)
C.generichash_blake2b_key(outlen, data, key)
end

def hash(outlen, data, key, salt, personal) do
Expand Down
3 changes: 2 additions & 1 deletion lib/salty/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ defmodule Salty.Nif do
def generichash_blake2b_KEYBYTES, do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b_SALTBYTES, do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b_PERSONALBYTES, do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b(_,_,_), do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b(_,_), do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b_key(_,_,_), do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b_salt_personal(_,_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b_init(_,_), do: :erlang.exit(:salty_nif_not_loaded)
def generichash_blake2b_init_salt_personal(_,_,_,_), do: :erlang.exit(:salty_nif_not_loaded)
Expand Down
15 changes: 13 additions & 2 deletions src/salty_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,17 @@ SALTY_CONST_INT64(generichash_blake2b_KEYBYTES);
SALTY_CONST_INT64(generichash_blake2b_SALTBYTES);
SALTY_CONST_INT64(generichash_blake2b_PERSONALBYTES);

SALTY_FUNC(generichash_blake2b, 3) DO
SALTY_FUNC(generichash_blake2b, 2) DO
SALTY_INPUT_UINT64(0, outlen);
SALTY_INPUT_BIN(1, input, SALTY_BIN_NO_SIZE);

SALTY_OUTPUT_BIN(hash, outlen);

SALTY_CALL(crypto_generichash_blake2b(hash.data, outlen, input.data,
input.size, NULL, 0), hash);
END_OK_WITH(hash);

SALTY_FUNC(generichash_blake2b_key, 3) DO
SALTY_INPUT_UINT64(0, outlen);
SALTY_INPUT_BIN(1, input, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, key, crypto_generichash_blake2b_KEYBYTES_MIN);
Expand Down Expand Up @@ -2114,7 +2124,8 @@ salty_exports[] = {
SALTY_EXPORT_CONS(generichash_blake2b_KEYBYTES, 0),
SALTY_EXPORT_CONS(generichash_blake2b_SALTBYTES, 0),
SALTY_EXPORT_CONS(generichash_blake2b_PERSONALBYTES, 0),
SALTY_EXPORT_FUNC(generichash_blake2b, 3),
SALTY_EXPORT_FUNC(generichash_blake2b, 2),
SALTY_EXPORT_FUNC(generichash_blake2b_key, 3),
SALTY_EXPORT_FUNC(generichash_blake2b_salt_personal, 5),
SALTY_EXPORT_FUNC(generichash_blake2b_init, 2),
SALTY_EXPORT_FUNC(generichash_blake2b_init_salt_personal, 4),
Expand Down

0 comments on commit 49fa935

Please sign in to comment.