summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-11-02 19:40:10 +0100
committerpukkamustard <pukkamustard@posteo.net>2020-11-02 19:40:10 +0100
commitd5b07f697fd1fc8d1040d3e0460e89658bc23c0f (patch)
tree503eaceae84359e514737c639ca6ba8dc6572556
parent675b83034cb67e970d675bb4f51eb77f9d398d00 (diff)
Add bindings for crypto_blake2b_general.
-rw-r--r--README.md28
-rw-r--r--c_src/monocypher_nif.c36
-rw-r--r--src/monocypher.erl4
3 files changed, 67 insertions, 1 deletions
diff --git a/README.md b/README.md
index ca993f6..8291ff5 100644
--- a/README.md
+++ b/README.md
@@ -5,3 +5,31 @@ Erlang (NIF) bindings to the [Monocypher](https://monocypher.org/) crypto librar
## Monocypher
This package includes the [Monocypher](https://monocypher.org/) cryptographic library version 3.1.1, which is dedicated to the public domain via CC0-1.0.
+
+### Functions
+
+Only a very limited set of functions from Monocypher are bound in the Erlang module.
+
+Contributions to bind more functions are very welcome.
+
+See also the [Monocypher documentation](https://monocypher.org/manual/).
+
+#### Hashing
+
+- [X] `crypto_blake2b`
+- [X] `crypto_blake2b_general`
+- [ ] `crypto_blake2b_init`
+- [ ] `crypto_blake2b_general_init`
+- [ ] `crypto_blake2b_update`
+- [ ] `crypto_blake2b_final`
+
+#### IETF ChaCha20
+
+- [ ] `crypto_ietf_chacha20`
+- [ ] `crypto_ietf_chacha20_ctr`
+
+#### Ed25519
+
+- [ ] `crypto_ed25519_public_key`
+- [ ] `crypto_ed25519_sign`
+- [ ] `crypto_ed25519_check`
diff --git a/c_src/monocypher_nif.c b/c_src/monocypher_nif.c
index 0568f0d..cafd66a 100644
--- a/c_src/monocypher_nif.c
+++ b/c_src/monocypher_nif.c
@@ -1,6 +1,8 @@
#include <erl_nif.h>
#include "monocypher.h"
+/* Hashing */
+
static ERL_NIF_TERM crypto_blake2b_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
@@ -17,10 +19,42 @@ static ERL_NIF_TERM crypto_blake2b_nif(ErlNifEnv* env, int argc, const ERL_NIF_T
return hash_term;
}
+static ERL_NIF_TERM crypto_blake2b_general_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+
+ size_t hash_size;
+ if (!enif_get_ulong(env, argv[0], &hash_size)) {
+ return enif_make_badarg(env);
+ }
+
+ // hash_size must be between 1 and 64.
+ if(hash_size < 1 || hash_size > 64) {
+ return enif_make_badarg(env);
+ }
+
+ ERL_NIF_TERM hash_term;
+ unsigned char* hash = enif_make_new_binary(env, hash_size, &hash_term);
+
+ ErlNifBinary key;
+ if(!enif_inspect_binary(env, argv[1], &key)) {
+ return enif_make_badarg(env);
+ }
+
+ ErlNifBinary message;
+ if(!enif_inspect_binary(env, argv[2], &message)) {
+ return enif_make_badarg(env);
+ }
+
+ crypto_blake2b_general(hash, hash_size, key.data, key.size, message.data, message.size);
+
+ return hash_term;
+}
+
static ErlNifFunc nif_funs[] =
{
- {"crypto_blake2b", 1, crypto_blake2b_nif}
+ {"crypto_blake2b", 1, crypto_blake2b_nif},
+ {"crypto_blake2b_general", 3, crypto_blake2b_general_nif}
/* {"crypto_lock", 6, crypto_lock_nif}, */
/* {"crypto_unlock", 6, crypto_unlock_nif} */
};
diff --git a/src/monocypher.erl b/src/monocypher.erl
index 1dfbb1f..dfceab9 100644
--- a/src/monocypher.erl
+++ b/src/monocypher.erl
@@ -1,5 +1,6 @@
-module(monocypher).
-export([crypto_blake2b/1]).
+-export([crypto_blake2b_general/3]).
-on_load(init/0).
init() ->
@@ -7,3 +8,6 @@ init() ->
crypto_blake2b(_X) ->
exit(nif_library_not_loaded).
+
+crypto_blake2b_general(_,_,_) ->
+ exit(nif_library_not_loaded).