Move the high-level API to enacl_ext

This commit is contained in:
Alexander Færøy 2015-02-22 14:29:44 +01:00
parent d61d363426
commit e408278d50
No known key found for this signature in database
GPG Key ID: E15081D5D3C3DB53
2 changed files with 46 additions and 33 deletions

View File

@ -60,6 +60,11 @@
onetime_auth_verify/3 onetime_auth_verify/3
]). ]).
%% Curve 25519.
-export([
curve25519_scalarmult/2
]).
%% Low-level functions %% Low-level functions
-export([ -export([
hash/1, hash/1,
@ -67,13 +72,6 @@
verify_32/2 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 %% Libsodium specific functions (which are also part of the "undocumented" interface to NaCl
-export([ -export([
randombytes/1 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(X, Y) when is_binary(X), is_binary(Y) -> enacl_nif:crypto_verify_32(X, Y);
verify_32(_, _) -> error(badarg). 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 = 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 %% Public Key Crypto
%% --------------------- %% ---------------------
%% @doc box_keypair/0 creates a new Public/Secret keypair. %% @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(). -spec onetime_auth_key_size() -> pos_integer().
onetime_auth_key_size() -> enacl_nif:crypto_onetimeauth_KEYBYTES(). 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 %% Obtaining random bytes
%% @doc randombytes/1 produces a stream of random bytes of the given size %% @doc randombytes/1 produces a stream of random bytes of the given size

View File

@ -9,6 +9,13 @@
scramble_block_16/2 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 %% @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 %% 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 %% 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(). -spec scramble_block_16(binary(), binary()) -> binary().
scramble_block_16(Block, Key) -> scramble_block_16(Block, Key) ->
enacl_nif: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() ->
<<B0:8/integer, B1:30/binary, B2:8/integer>> = 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).