summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-11-03 11:25:13 +0100
committerpukkamustard <pukkamustard@posteo.net>2020-11-03 11:25:13 +0100
commit45a59cc056e30946287bee34c0013e5450cce63b (patch)
treec75882404782b8e1ec453ca7bfd556cfcdf25247
parent8e8d66d22f0f890f6235ed69205738861e2b7d1a (diff)
Add bindings to crypto_ietf_chacha20 and crypt_ietf_chacha20_ctr.
-rw-r--r--README.md4
-rw-r--r--c_src/monocypher_nif.c80
-rw-r--r--src/monocypher.erl12
3 files changed, 93 insertions, 3 deletions
diff --git a/README.md b/README.md
index 8291ff5..be799a6 100644
--- a/README.md
+++ b/README.md
@@ -25,8 +25,8 @@ See also the [Monocypher documentation](https://monocypher.org/manual/).
#### IETF ChaCha20
-- [ ] `crypto_ietf_chacha20`
-- [ ] `crypto_ietf_chacha20_ctr`
+- [X] `crypto_ietf_chacha20`
+- [X] `crypto_ietf_chacha20_ctr`
#### Ed25519
diff --git a/c_src/monocypher_nif.c b/c_src/monocypher_nif.c
index cafd66a..54d9d77 100644
--- a/c_src/monocypher_nif.c
+++ b/c_src/monocypher_nif.c
@@ -51,10 +51,88 @@ static ERL_NIF_TERM crypto_blake2b_general_nif(ErlNifEnv* env, int argc, const E
}
+/* IETF ChaCha20 */
+
+static ERL_NIF_TERM crypto_ietf_chacha20_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+
+ ErlNifBinary plain_text;
+ if(!enif_inspect_binary(env, argv[0], &plain_text)) {
+ return enif_make_badarg(env);
+ }
+
+ ErlNifBinary key;
+ if(!enif_inspect_binary(env, argv[1], &key)) {
+ return enif_make_badarg(env);
+ }
+
+ if(key.size != 32) {
+ return enif_make_badarg(env);
+ }
+
+ ErlNifBinary nonce;
+ if(!enif_inspect_binary(env, argv[2], &nonce)) {
+ return enif_make_badarg(env);
+ }
+
+ if(nonce.size != 12) {
+ return enif_make_badarg(env);
+ }
+
+ ERL_NIF_TERM cipher_text_term;
+ unsigned char* cipher_text = enif_make_new_binary(env, plain_text.size, &cipher_text_term);
+
+ crypto_ietf_chacha20(cipher_text, plain_text.data, plain_text.size, key.data, nonce.data);
+
+ return cipher_text_term;
+}
+
+static ERL_NIF_TERM crypto_ietf_chacha20_ctr_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+
+ ErlNifBinary plain_text;
+ if(!enif_inspect_binary(env, argv[0], &plain_text)) {
+ return enif_make_badarg(env);
+ }
+
+ ErlNifBinary key;
+ if(!enif_inspect_binary(env, argv[1], &key)) {
+ return enif_make_badarg(env);
+ }
+
+ if(key.size != 32) {
+ return enif_make_badarg(env);
+ }
+
+ ErlNifBinary nonce;
+ if(!enif_inspect_binary(env, argv[2], &nonce)) {
+ return enif_make_badarg(env);
+ }
+
+ if(nonce.size != 12) {
+ return enif_make_badarg(env);
+ }
+
+ uint32_t ctr;
+ if (!enif_get_uint(env, argv[3], &ctr)) {
+ return enif_make_badarg(env);
+ }
+
+ ERL_NIF_TERM cipher_text_term;
+ unsigned char* cipher_text = enif_make_new_binary(env, plain_text.size, &cipher_text_term);
+
+ crypto_ietf_chacha20_ctr(cipher_text, plain_text.data, plain_text.size, key.data, nonce.data, ctr);
+
+ return cipher_text_term;
+}
+
+
static ErlNifFunc nif_funs[] =
{
{"crypto_blake2b", 1, crypto_blake2b_nif},
- {"crypto_blake2b_general", 3, crypto_blake2b_general_nif}
+ {"crypto_blake2b_general", 3, crypto_blake2b_general_nif},
+ {"crypto_ietf_chacha20", 3, crypto_ietf_chacha20_nif},
+ {"crypto_ietf_chacha20_ctr", 4, crypto_ietf_chacha20_ctr_nif}
/* {"crypto_lock", 6, crypto_lock_nif}, */
/* {"crypto_unlock", 6, crypto_unlock_nif} */
};
diff --git a/src/monocypher.erl b/src/monocypher.erl
index d61bb79..a3067dc 100644
--- a/src/monocypher.erl
+++ b/src/monocypher.erl
@@ -1,17 +1,29 @@
-module(monocypher).
-export([crypto_blake2b/1]).
-export([crypto_blake2b_general/3]).
+-export([crypto_ietf_chacha20/3]).
+-export([crypto_ietf_chacha20_ctr/4]).
-on_load(init/0).
-define(APPNAME, monocypher).
-define(LIBNAME, monocypher).
+-spec crypto_blake2b(binary()) -> binary().
crypto_blake2b(_) ->
not_loaded(?LINE).
+-spec crypto_blake2b_general(integer(), binary(), binary()) -> binary().
crypto_blake2b_general(_,_,_) ->
not_loaded(?LINE).
+-spec crypto_ietf_chacha20(binary(), binary(), binary()) -> binary().
+crypto_ietf_chacha20(_,_,_) ->
+ not_loaded(?LINE).
+
+-spec crypto_ietf_chacha20_ctr(binary(), binary(), binary(), pos_integer()) -> binary().
+crypto_ietf_chacha20_ctr(_,_,_,_) ->
+ not_loaded(?LINE).
+
init() ->
SoName = case code:priv_dir(?APPNAME) of
{error, bad_name} ->