diff --git a/src/enacl.erl b/src/enacl.erl index f6066cd..a6415cc 100644 --- a/src/enacl.erl +++ b/src/enacl.erl @@ -11,6 +11,8 @@ %%%

Warning: The cryptographic strength of your implementation is no stronger than %%% plaintext cryptography unless you take care in using these primitives correctly. Hence, %%% implementors should use these primitives with that in mind.

+%%%

Note:All functions will fail with a `badarg' error if given incorrect +%%% parameters.

%%% @end. -module(enacl). @@ -73,24 +75,58 @@ verify_32(X, Y) -> enacl_nif:crypto_verify_32(X, Y). %% Public Key Crypto %% --------------------- +%% @doc box_keypair/0 creates a new Public/Secret keypair. +%% Generates and returns a new key pair for the Box encryption scheme. +%% @end. +-spec box_keypair() -> {PublicKey, SecretKey} + when PublicKey :: binary(), + SecretKey :: binary(). box_keypair() -> enacl_nif:crypto_box_keypair(). +%% @doc box/4 encrypts+authenticates a message to another party. +%% Encrypt a `Msg` to the party identified by public key `PK` using your own secret key `SK` to +%% authenticate yourself. Requires a `Nonce` in addition. Returns the ciphered message. +%% @end +-spec box(Msg, Nonce, PK, SK) -> CipherText + when Msg :: binary(), + Nonce :: binary(), + PK :: binary(), + SK :: binary(), + CipherText :: binary(). box(Msg, Nonce, PK, SK) -> enacl_nif:crypto_box([p_zerobytes(), Msg], Nonce, PK, SK). +%% @doc box_open/4 decrypts+verifies a message from another party. +%% Decrypt a `CipherText` into a `Msg` given the other partys public key `PK` and your secret +%% key `SK`. Also requires the same nonce as was used by the other party. Returns the plaintext +%% message. +-spec box_open(CipherText, Nonce, PK, SK) -> Msg + when CipherText :: binary(), + Nonce :: binary(), + PK :: binary(), + SK :: binary(), + Msg :: binary(). box_open(CipherText, Nonce, PK, SK) -> case enacl_nif:crypto_box_open([p_box_zerobytes(), CipherText], Nonce, PK, SK) of {error, Err} -> {error, Err}; Bin when is_binary(Bin) -> {ok, Bin} end. +%% @doc box_nonce_size/0 return the byte-size of the nonce +%% Used to obtain the size of the nonce. +%% @end. +-spec box_nonce_size() -> pos_integer(). box_nonce_size() -> enacl_nif:crypto_box_NONCEBYTES(). +%% @private +-spec box_public_key_bytes() -> pos_integer(). box_public_key_bytes() -> enacl_nif:crypto_box_PUBLICKEYBYTES(). +%% @private +-spec box_secret_key_bytes() -> pos_integer(). box_secret_key_bytes() -> enacl_nif:crypto_box_SECRETKEYBYTES().