From e408278d5008f7f42ecf6f2f21abe1b6d07a7623 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Sun, 22 Feb 2015 14:29:44 +0100 Subject: [PATCH] Move the high-level API to enacl_ext --- src/enacl.erl | 46 +++++++++++++--------------------------------- src/enacl_ext.erl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/src/enacl.erl b/src/enacl.erl index 4e3e1f8..f73eace 100644 --- a/src/enacl.erl +++ b/src/enacl.erl @@ -60,6 +60,11 @@ onetime_auth_verify/3 ]). +%% Curve 25519. +-export([ + curve25519_scalarmult/2 +]). + %% Low-level functions -export([ hash/1, @@ -67,13 +72,6 @@ verify_32/2 ]). -%% Curve25519 --export([ - curve25519_keypair/0, - curve25519_public_key/1, - curve25519_shared/2 -]). - %% Libsodium specific functions (which are also part of the "undocumented" interface to NaCl -export([ randombytes/1 @@ -175,32 +173,6 @@ 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() -> - <> = randombytes(32), - SK = <<(B0 band 248), B1/binary, (64 bor (B2 band 127))>>, - PK = curve25519_public_key(SK), - #{ public => PK, secret => SK }. - -%% @doc curve25519_public_key/1 creates a public key from a given SecretKey. -%% @end --spec curve25519_public_key(SecretKey :: binary()) -> binary(). -curve25519_public_key(SecretKey) -> - enacl_nif:crypto_curve25519_scalarmult(SecretKey, <<9, 0:248>>). - -%% @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. @@ -624,6 +596,14 @@ onetime_auth_size() -> enacl_nif:crypto_onetimeauth_BYTES(). -spec onetime_auth_key_size() -> pos_integer(). onetime_auth_key_size() -> enacl_nif:crypto_onetimeauth_KEYBYTES(). +%% Curve 25519 Crypto +%% ------------------ +%% @doc curve25519_scalarmult/2 does a scalar multiplication between the Secret and the BasePoint. +%% @end. +-spec curve25519_scalarmult(Secret :: binary(), BasePoint :: binary()) -> binary(). +curve25519_scalarmult(Secret, BasePoint) -> + enacl_nif:crypto_curve25519_scalarmult(Secret, BasePoint). + %% Obtaining random bytes %% @doc randombytes/1 produces a stream of random bytes of the given size diff --git a/src/enacl_ext.erl b/src/enacl_ext.erl index c18abe1..cc3a594 100644 --- a/src/enacl_ext.erl +++ b/src/enacl_ext.erl @@ -9,6 +9,13 @@ scramble_block_16/2 ]). +%% Curve25519 +-export([ + curve25519_keypair/0, + curve25519_public_key/1, + curve25519_shared/2 +]). + %% @doc scramble_block_16/2 scrambles (encrypt) a block under a given key %% The rules are that the block is 16 bytes and the key is 32 bytes. The block %% is scrambled by means of the (secret) key. This makes it impossible for an @@ -23,3 +30,29 @@ -spec scramble_block_16(binary(), binary()) -> binary(). scramble_block_16(Block, Key) -> enacl_nif:scramble_block_16(Block, Key). + +%% 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() -> + <> = enacl:randombytes(32), + SK = <<(B0 band 248), B1/binary, (64 bor (B2 band 127))>>, + PK = curve25519_public_key(SK), + #{ public => PK, secret => SK }. + +%% @doc curve25519_public_key/1 creates a public key from a given SecretKey. +%% @end +-spec curve25519_public_key(SecretKey :: binary()) -> binary(). +curve25519_public_key(SecretKey) -> + enacl:curve25519_scalarmult(SecretKey, <<9, 0:248>>). + +%% @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:curve25519_scalarmult(SecretKey, PublicKey).