From e6a887cdecd1bfa2319fcc05304462d278dbd903 Mon Sep 17 00:00:00 2001 From: Moritz Ulrich Date: Fri, 26 Apr 2019 22:27:51 +0200 Subject: [PATCH 1/5] Add .elixir_ls to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cea3aa9..8e70e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ erl_crash.dump *.sw* *.o /priv +/.elixir_ls \ No newline at end of file From 514ffcc9d5ece3200652880ec21e602d0898448f Mon Sep 17 00:00:00 2001 From: Moritz Ulrich Date: Fri, 26 Apr 2019 22:28:27 +0200 Subject: [PATCH 2/5] Add Salty.Sign.crypto_sign_ed25519_pk_to_curve25519 and Salty.Sign.crypto_sign_ed25519_sk_to_curve25519 --- lib/salty/nif.ex | 2 ++ lib/salty/sign.ex | 4 ++++ lib/salty/sign_ed25519.ex | 8 ++++++++ src/salty_nif.c | 20 ++++++++++++++++++++ test/salty_test.exs | 5 +++++ 5 files changed, 39 insertions(+) diff --git a/lib/salty/nif.ex b/lib/salty/nif.ex index a189fcd..4d04ceb 100644 --- a/lib/salty/nif.ex +++ b/lib/salty/nif.ex @@ -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) diff --git a/lib/salty/sign.ex b/lib/salty/sign.ex index f2db70e..e557313 100644 --- a/lib/salty/sign.ex +++ b/lib/salty/sign.ex @@ -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()} diff --git a/lib/salty/sign_ed25519.ex b/lib/salty/sign_ed25519.ex index 9d8ec88..9500a4e 100644 --- a/lib/salty/sign_ed25519.ex +++ b/lib/salty/sign_ed25519.ex @@ -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 diff --git a/src/salty_nif.c b/src/salty_nif.c index efe6164..a2cbb56 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1662,6 +1662,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); +\ + 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); +\ + 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); @@ -2194,6 +2212,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), diff --git a/test/salty_test.exs b/test/salty_test.exs index 89b661c..3a307ae 100644 --- a/test/salty_test.exs +++ b/test/salty_test.exs @@ -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 From 1c2c00b15ba5a0144e20077e9cb676d786e0436a Mon Sep 17 00:00:00 2001 From: Jan Winkler Date: Tue, 28 Aug 2018 17:01:59 +0200 Subject: [PATCH 3/5] #13: Do not attempt to 'cache' tuple_error_unknown in salty_onload. Tuples cannot be cached/reused in the same way atoms can. --- src/salty_nif.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/salty_nif.c b/src/salty_nif.c index a2cbb56..4a92b17 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -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) @@ -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 @@ -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; } From f1b760170fe8fe0acc5db4177cee0bd17be84459 Mon Sep 17 00:00:00 2001 From: Moritz Ulrich Date: Tue, 11 Jun 2019 20:55:55 +0200 Subject: [PATCH 4/5] Remove accidental backslash from src/salty_nif.c Co-Authored-By: Jan --- src/salty_nif.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/salty_nif.c b/src/salty_nif.c index 4a92b17..efcfe16 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1662,7 +1662,6 @@ 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); -\ SALTY_OUTPUT_BIN(pk_curve25519, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES); SALTY_CALL2(crypto_sign_ed25519_pk_to_curve25519( From a8d6bf41590cfd8e384a8662771869a1ce45f08b Mon Sep 17 00:00:00 2001 From: Moritz Ulrich Date: Tue, 11 Jun 2019 23:06:29 +0200 Subject: [PATCH 5/5] Remove another accidental backslash in salty_nif.c --- src/salty_nif.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/salty_nif.c b/src/salty_nif.c index efcfe16..53f861e 100644 --- a/src/salty_nif.c +++ b/src/salty_nif.c @@ -1670,7 +1670,6 @@ 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); -\ SALTY_OUTPUT_BIN(sk_curve25519, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES); SALTY_CALL2(crypto_sign_ed25519_sk_to_curve25519(