Add curve25519_keypair/0 and curve25519_shared/2.

This commit is contained in:
Alexander Færøy
2015-02-21 22:47:34 +01:00
parent 6de936899c
commit 4e66fc3b94
3 changed files with 64 additions and 1 deletions
+26
View File
@@ -67,6 +67,12 @@
verify_32/2
]).
%% Curve25519
-export([
curve25519_keypair/0,
curve25519_shared/2
]).
%% Libsodium specific functions (which are also part of the "undocumented" interface to NaCl
-export([
randombytes/1
@@ -168,6 +174,26 @@ verify_16(_, _) -> error(badarg).
verify_32(X, Y) when is_binary(X), is_binary(Y) -> enacl_nif:crypto_verify_32(X, Y);
verify_32(_, _) -> error(badarg).
%% Curve 25519 Crypto
%% ------------------
%% @doc curve25519_keypair/0 creates a new Public/Secret keypair.
%%
%% Generates and returns a new key pair for the Curve 25519 encryption scheme. The return value is a
%% map in order to avoid using the public key as a secret key and vice versa.
%% @end.
-spec curve25519_keypair() -> #{ atom() => binary() }.
curve25519_keypair() ->
<<B0:8/integer, B1:30/binary, B2:8/integer>> = randombytes(32),
SK = <<(B0 band 248), B1/binary, (64 bor (B2 band 127))>>,
PK = enacl_nif:crypto_curve25519_scalarmult(SK, <<9, 0:248>>),
#{ public => PK, secret => SK }.
%% @doc curve25519_shared/2 creates a new shared secret from a given SecretKey and PublicKey.
%% @end.
-spec curve25519_shared(SecretKey :: binary(), PublicKey :: binary()) -> binary().
curve25519_shared(SecretKey, PublicKey) ->
enacl_nif:crypto_curve25519_scalarmult(SecretKey, PublicKey).
%% Public Key Crypto
%% ---------------------
%% @doc box_keypair/0 creates a new Public/Secret keypair.
+7
View File
@@ -71,6 +71,11 @@
crypto_onetimeauth_verify_b/3
]).
%% Curve25519
-export([
crypto_curve25519_scalarmult/2
]).
%% Miscellaneous helper functions
-export([
crypto_hash/1,
@@ -161,6 +166,8 @@ crypto_onetimeauth_b(_Msg, _Key) -> erlang:nif_error(nif_not_loaded).
crypto_onetimeauth_verify(_Authenticator, _Msg, _Key) -> erlang:nif_error(nif_not_loaded).
crypto_onetimeauth_verify_b(_Authenticator, _Msg, _Key) -> erlang:nif_error(nif_not_loaded).
crypto_curve25519_scalarmult(_Secret, _BasePoint) -> erlang:nif_error(nif_not_loaded).
crypto_hash(Input) when is_binary(Input) -> erlang:nif_error(nif_not_loaded).
crypto_hash_b(Input) when is_binary(Input) -> erlang:nif_error(nif_not_loaded).
crypto_verify_16(_X, _Y) -> erlang:nif_error(nif_not_loaded).