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

Add crypto_sign_ed25519_{sk,pk}_to_curve25519 #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ erl_crash.dump
*.sw*
*.o
/priv
/.elixir_ls
2 changes: 2 additions & 0 deletions lib/salty/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ defmodule Salty.Nif do
def sign_ed25519_SECRETKEYBYTES, do: :erlang.exit(:salty_nif_not_loaded)
def sign_ed25519_seed_keypair(_), do: :erlang.exit(:salty_nif_not_loaded)
def sign_ed25519_keypair(), do: :erlang.exit(:salty_nif_not_loaded)
def crypto_sign_ed25519_pk_to_curve25519(_), do: :erlang.exit(:salty_nif_not_loaded)
def crypto_sign_ed25519_sk_to_curve25519(_), do: :erlang.exit(:salty_nif_not_loaded)
def sign_ed25519(_,_), do: :erlang.exit(:salty_nif_not_loaded)
def sign_ed25519_detached(_,_), do: :erlang.exit(:salty_nif_not_loaded)
def sign_ed25519_verify_detached(_,_,_), do: :erlang.exit(:salty_nif_not_loaded)
Expand Down
4 changes: 4 additions & 0 deletions lib/salty/sign.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ defmodule Salty.Sign do

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

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

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

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

@callback sign_detached(binary(), binary()) :: {:ok, binary()} | {:error, atom()}
Expand Down
8 changes: 8 additions & 0 deletions lib/salty/sign_ed25519.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ defmodule Salty.Sign.Ed25519 do
C.sign_ed25519_keypair()
end

def crypto_sign_ed25519_pk_to_curve25519(data) do
C.crypto_sign_ed25519_pk_to_curve25519(data)
end

def crypto_sign_ed25519_sk_to_curve25519(data) do
C.crypto_sign_ed25519_sk_to_curve25519(data)
end

def sign(data, sk) do
C.sign_ed25519(data, sk)
end
Expand Down
24 changes: 21 additions & 3 deletions src/salty_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#define SALTY_BADARG enif_make_badarg(env)
#define SALTY_BADALLOC SALTY_BADARG /* TODO make this return an actual failed-to-alloc error */

#define SALTY_ERROR tuple_error_unknown
#define SALTY_ERROR enif_make_tuple2(env, atom_error, atom_error_unknown)
#define SALTY_ERROR_PAIR(err) enif_make_tuple2(env, atom_error, err)
#define SALTY_OK atom_ok
#define SALTY_OK_PAIR(a) enif_make_tuple2(env, atom_ok, a)
Expand Down Expand Up @@ -415,7 +415,6 @@ ERL_NIF_TERM atom_error_no_match;
ERL_NIF_TERM atom_error_not_available;
ERL_NIF_TERM atom_error_forged;
ERL_NIF_TERM atom_error_unknown;
ERL_NIF_TERM tuple_error_unknown;

/* erl_nif code */
static int
Expand All @@ -429,7 +428,6 @@ salty_onload(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) {
atom_error_not_available = enif_make_atom(env, "not_available");
atom_error_forged = enif_make_atom(env, "forged");
atom_error_unknown = enif_make_atom(env, "salty_error_unknown");
tuple_error_unknown = enif_make_tuple2(env, atom_error, atom_error_unknown);

return 0;
}
Expand Down Expand Up @@ -1662,6 +1660,24 @@ SALTY_FUNC(sign_ed25519_keypair, 0) DO
pk.data, sk.data), pk, sk);
END_OK_WITH2(pk, sk);

SALTY_FUNC(crypto_sign_ed25519_pk_to_curve25519, 1) DO
SALTY_INPUT_BIN(0, pk_ed25519, crypto_sign_ed25519_PUBLICKEYBYTES);
\
the-kenny marked this conversation as resolved.
Show resolved Hide resolved
SALTY_OUTPUT_BIN(pk_curve25519, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);

SALTY_CALL2(crypto_sign_ed25519_pk_to_curve25519(
pk_curve25519.data, pk_ed25519.data), pk_ed25519, pk_curve25519);
END_OK_WITH(pk_curve25519);

SALTY_FUNC(crypto_sign_ed25519_sk_to_curve25519, 1) DO
SALTY_INPUT_BIN(0, sk_ed25519, crypto_sign_ed25519_PUBLICKEYBYTES);
\
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this one here.

SALTY_OUTPUT_BIN(sk_curve25519, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);

SALTY_CALL2(crypto_sign_ed25519_sk_to_curve25519(
sk_curve25519.data, sk_ed25519.data), sk_ed25519, sk_curve25519);
END_OK_WITH(sk_curve25519);

SALTY_FUNC(sign_ed25519, 2) DO
SALTY_INPUT_BIN(0, data, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, sk, crypto_sign_ed25519_SECRETKEYBYTES);
Expand Down Expand Up @@ -2194,6 +2210,8 @@ salty_exports[] = {
SALTY_EXPORT_CONS(sign_ed25519_SECRETKEYBYTES, 0),
SALTY_EXPORT_FUNC(sign_ed25519_seed_keypair, 1),
SALTY_EXPORT_FUNC(sign_ed25519_keypair, 0),
SALTY_EXPORT_FUNC(crypto_sign_ed25519_pk_to_curve25519, 1),
SALTY_EXPORT_FUNC(crypto_sign_ed25519_sk_to_curve25519, 1),
SALTY_EXPORT_FUNC(sign_ed25519, 2),
SALTY_EXPORT_FUNC(sign_ed25519_detached, 2),
SALTY_EXPORT_FUNC(sign_ed25519_verify_detached, 3),
Expand Down
5 changes: 5 additions & 0 deletions test/salty_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@ defmodule SaltyTest do
assert Salty.Sign.Ed25519.sk_to_pk(sk) == pk
end

test "crypto_sign_ed25519_{pk,sk}_to_curve25519 works" do
{:ok, pk, sk} = Salty.Sign.Ed25519.keypair()
{:ok, _} = Salty.Sign.Ed25519.crypto_sign_ed25519_pk_to_curve25519(pk)
{:ok, _} = Salty.Sign.Ed25519.crypto_sign_ed25519_sk_to_curve25519(sk)
end
end