Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
potatosalad committed Jan 3, 2016
0 parents commit 0e834dc
Show file tree
Hide file tree
Showing 27 changed files with 4,402 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
/.ed448
.erlang.cookie
.erlang.mk.packages.*
ebin
log
ct.coverdata
erl_crash.dump
.rebar
.rebar3
rebar3
_rel
_deps
_plugins
_tdeps
deps
doc
logs
test/ct.cover.spec
/_build
/cover
/priv/*.dll
/priv/*.dylib
/priv/*.so
/tmp
*.ez
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PROJECT = ed448

include erlang.mk
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Ed448 port driver

[ed448-goldilocks](https://github.com/coruus/ed448-goldilocks/tree/upstream) port driver for Erlang and Elixir.

*Work In Progress* - not yet ready for production.

## Usage

```erlang
%% Ed448 keypair generation
{PrivateKey, PublicKey} = ed448_goldilocks:keygen().
% {<<37,53,125,217,238,243,234,11,31,66,138,114,219,40,56,
% 22,77,155,197,252,17,123,11,62,0,138,227,160,113,174,
% 141,186,192,232,197,47,96,166,45,79,118,55,102,138,202,
% 76,116,32,66,111,108,42,108,57,181,4,124,231,153,136,
% 93,115,123,187,91,105,65,37,52,54,233,46,127,85,214,
% 231,137,139,204,231,100,246,146,180,133,165,205,184,
% 134,235,149,101,127,176,178,241,60,99,12,74,161,183,
% 159,233,162,156,101,57,13,59,206,72,176,184,97,187,211,
% 246,148,58,209,189,196,51,81,15,227,16,211,211,27,64,
% 79,218,230,10,3,5,205,62,36,162,191,198>>,
% <<124,231,153,136,93,115,123,187,91,105,65,37,52,54,233,
% 46,127,85,214,231,137,139,204,231,100,246,146,180,133,
% 165,205,184,134,235,149,101,127,176,178,241,60,99,12,
% 74,161,183,159,233,162,156,101,57,13,59,206,72>>}

%% Ed448 message signing
Message = <<"test">>,
Signature = ed448_goldilocks:sign(Message, PrivateKey).
% <<245,189,29,5,58,149,32,28,235,146,74,155,121,22,135,117,
% 152,150,179,93,233,97,131,228,204,127,227,161,116,168,
% 124,90,147,50,41,201,162,165,106,120,193,132,213,236,
% 168,182,97,218,35,122,135,37,155,123,242,149,185,179,
% 116,226,35,66,115,24,134,9,68,152,28,70,41,46,56,63,142,
% 207,16,237,207,128,55,112,134,71,81,32,26,2,21,120,21,
% 68,206,164,12,171,34,24,91,29,21,28,202,100,8,152,183,
% 19,116,228,153,35>>

%% Ed448 message verification
0 = ed448_goldilocks:verify(Signature, Message, PublicKey).
```
40 changes: 40 additions & 0 deletions build.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Do *not* comment or remove core modules
# unless you know what you are doing.
#
# Feel free to comment plugins out however.

# Core modules.
core/core
#index/*
core/index
core/deps

# Plugins that must run before Erlang code gets compiled.
#plugins/erlydtl
#plugins/protobuffs

# Core modules, continued.
core/erlc
core/docs
core/rel
core/test
core/compat

# Plugins.
#plugins/asciidoc
plugins/bootstrap
plugins/c_src
plugins/ci
plugins/ct
plugins/dialyzer
#plugins/edoc
#plugins/erlydtl
plugins/escript
#plugins/eunit
#plugins/relx
plugins/shell
plugins/triq
plugins/xref

# Plugins enhancing the functionality of other plugins.
plugins/cover
94 changes: 94 additions & 0 deletions c_src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Based on c_src.mk from erlang.mk by Loic Hoguin <[email protected]>

CURDIR := $(shell pwd)
BASEDIR := $(abspath $(CURDIR)/..)

PROJECT ?= $(notdir $(BASEDIR))
PROJECT := $(strip $(PROJECT))

ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).")
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).")
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).")

C_SRC_DIR = $(CURDIR)
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT)_drv.so

ED448_VSN ?= 9ce5cbf53ca27a11f18b07f80b8c23ec938f0336
ED448_DIR ?= $(BASEDIR)/.ed448
ED448_GIT ?= git://github.com/coruus/ed448-goldilocks.git
ED448_OUT ?= $(ED448_DIR)/build/goldilocks.so
ED448_OBJ ?= $(ED448_DIR)/build/goldilocks.o $(ED448_DIR)/build/barrett_field.o \
$(ED448_DIR)/build/crandom.o $(ED448_DIR)/build/p448.o $(ED448_DIR)/build/ec_point.o \
$(ED448_DIR)/build/scalarmul.o $(ED448_DIR)/build/sha512.o $(ED448_DIR)/build/magic.o \
$(ED448_DIR)/build/f_arithmetic.o $(ED448_DIR)/build/arithmetic.o

# System type and C compiler/flags.

UNAME_SYS := $(shell uname -s)
ifeq ($(UNAME_SYS), Darwin)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
else ifeq ($(UNAME_SYS), FreeBSD)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -finline-functions -Wall
else ifeq ($(UNAME_SYS), Linux)
CC ?= gcc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -finline-functions -Wall
endif

CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I $(ED448_DIR)/include
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR) -I $(ED448_DIR)/include

LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
LDFLAGS += -shared

# Verbosity.

c_verbose_0 = @echo " C " $(?F);
c_verbose = $(c_verbose_$(V))

cpp_verbose_0 = @echo " CPP " $(?F);
cpp_verbose = $(cpp_verbose_$(V))

link_verbose_0 = @echo " LD " $(@F);
link_verbose = $(link_verbose_$(V))

SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))

COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c

$(C_SRC_OUTPUT): $(ED448_OUT) $(OBJECTS)
@mkdir -p $(BASEDIR)/priv/
$(link_verbose) $(CC) $(ED448_OBJ) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)

%.o: %.c
$(COMPILE_C) $(OUTPUT_OPTION) $<

%.o: %.cc
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

%.o: %.C
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

%.o: %.cpp
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

get-deps:
@if [ ! -f $(ED448_DIR)/Makefile ]; then \
git clone -n $(ED448_GIT) $(ED448_DIR) && \
(cd $(ED448_DIR) && git checkout -q $(ED448_VSN)); \
else \
true; \
fi

$(ED448_OUT): get-deps
$(MAKE) -C $(ED448_DIR)

clean:
@rm -f $(C_SRC_OUTPUT) $(OBJECTS)
92 changes: 92 additions & 0 deletions c_src/ed448_api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// -*- mode: c; tab-width: 8; indent-tabs-mode: 1; st-rulers: [70] -*-
// vim: ts=8 sw=8 ft=c noet

#include "ed448_api.h"
#include "ed448_api_goldilocks.h"

#define ED448_NS(NAMESPACE) { #NAMESPACE, ed448_functions_ ## NAMESPACE }

static ed448_namespace_t ed448_namespaces[] = {
ED448_NS(goldilocks),
// LS_NS(crypto_aead_chacha20poly1305),
// LS_NS(crypto_auth),
// LS_NS(crypto_auth_hmacsha256),
// LS_NS(crypto_auth_hmacsha512),
// LS_NS(crypto_auth_hmacsha512256),
// LS_NS(crypto_core_hsalsa20),
// LS_NS(crypto_core_salsa20),
// LS_NS(crypto_core_salsa2012),
// LS_NS(crypto_core_salsa208),
// LS_NS(crypto_generichash),
// LS_NS(crypto_generichash_blake2b),
// LS_NS(crypto_hash),
// LS_NS(crypto_hash_sha256),
// LS_NS(crypto_hash_sha512),
// LS_NS(crypto_onetimeauth),
// LS_NS(crypto_onetimeauth_poly1305),
// LS_NS(crypto_scalarmult),
// LS_NS(crypto_scalarmult_curve25519),
// LS_NS(crypto_shorthash),
// LS_NS(crypto_shorthash_siphash24),
// LS_NS(crypto_sign),
// LS_NS(crypto_sign_ed25519),
// LS_NS(crypto_stream),
// LS_NS(crypto_stream_aes128ctr),
// LS_NS(crypto_stream_chacha20),
// LS_NS(crypto_stream_salsa20),
// LS_NS(crypto_stream_salsa2012),
// LS_NS(crypto_stream_salsa208),
// LS_NS(crypto_stream_xsalsa20),
// LS_NS(randombytes),
// LS_NS(runtime),
// LS_NS(utils),
// LS_NS(version),
{NULL}
};

void
init_ed448_api(void)
{
ed448_namespace_t *n;
ed448_function_t *f;

n = NULL;
f = NULL;

for (n = ed448_namespaces; n->namespace; n++) {
n->am_namespace = driver_mk_atom((char *)(n->namespace));
for (f = n->functions; f->function; f++) {
f->am_function = driver_mk_atom((char *)(f->function));
}
}
}

ed448_function_t *
get_ed448_api(const char *namespace, const char *function)
{
ed448_namespace_t *n;
ed448_function_t *f;
ErlDrvTermData am_namespace;
ErlDrvTermData am_function;

n = NULL;
f = NULL;

// (void) erl_drv_mutex_lock(ed448_mutex);
am_namespace = driver_mk_atom((char *)namespace);
am_function = driver_mk_atom((char *)function);
// (void) erl_drv_mutex_unlock(ed448_mutex);

for (n = ed448_namespaces; n->namespace; n++) {
if (n->am_namespace == am_namespace) {
for (f = n->functions; f->function; f++) {
if (f->am_function == am_function) {
return f;
}
}
return NULL;
}
}

return NULL;
}
66 changes: 66 additions & 0 deletions c_src/ed448_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// -*- mode: c; tab-width: 8; indent-tabs-mode: 1; st-rulers: [70] -*-
// vim: ts=8 sw=8 ft=c noet

#ifndef ED448_API_H
#define ED448_API_H

#include "ed448_drv_common.h"
#include "ed448_request.h"

typedef struct ed448_function {
const char *function;
int arity;
int (*init)(ed448_request_t *request, char *buffer, int *index);
void (*exec)(ed448_request_t *request);
ErlDrvTermData am_function;
} ed448_function_t;

typedef struct ed448_namespace {
const char *namespace;
ed448_function_t *functions;
ErlDrvTermData am_namespace;
} ed448_namespace_t;

extern void init_ed448_api(void);
extern ed448_function_t *get_ed448_api(const char *namespace, const char *function);

#define ED448_API_F_NS(NAMESPACE) ed448_api_ ## NAMESPACE
#define ED448_API_F_FN(FUNCTION) _ ## FUNCTION
#define ED448_API_F0(A, B) A ## B
#define ED448_API_F1(A, B) ED448_API_F0(A, B)
#define ED448_API_F2(NAMESPACE, FUNCTION) ED448_API_F1(ED448_API_F_NS(NAMESPACE), ED448_API_F_FN(FUNCTION))

#define ED448_API_F_EXEC(NAMESPACE, FUNCTION) ED448_API_F2(NAMESPACE, FUNCTION)
#define ED448_API_F_INIT(NAMESPACE, FUNCTION) ED448_API_F1(ED448_API_F_EXEC(NAMESPACE, FUNCTION), _init)
#define ED448_API_F_ARGV(NAMESPACE, FUNCTION) ED448_API_F1(ED448_API_F_EXEC(NAMESPACE, FUNCTION), _argv)
#define ED448_API_F_ARGV_T(NAMESPACE, FUNCTION) ED448_API_F1(ED448_API_F_ARGV(NAMESPACE, FUNCTION), _t)

#define ED448_API_EXEC(NAMESPACE, FUNCTION) ED448_API_F_EXEC(NAMESPACE, FUNCTION) (ed448_request_t *request)
#define ED448_API_INIT(NAMESPACE, FUNCTION) ED448_API_F_INIT(NAMESPACE, FUNCTION) (ed448_request_t *request, char *buffer, int *index)

#define ED448_API_R_ARG0(NAMESPACE, FUNCTION) { #FUNCTION, 0, NULL, ED448_API_F_EXEC(NAMESPACE, FUNCTION) }
#define ED448_API_R_ARGV(NAMESPACE, FUNCTION, ARITY) { #FUNCTION, ARITY, ED448_API_F_INIT(NAMESPACE, FUNCTION), ED448_API_F_EXEC(NAMESPACE, FUNCTION) }

#define ED448_API_INIT_ARGV(NAMESPACE, FUNCTION) \
do { \
argv = (ED448_API_F_ARGV_T(NAMESPACE, FUNCTION) *)(driver_alloc((ErlDrvSizeT)(sizeof (ED448_API_F_ARGV_T(NAMESPACE, FUNCTION))))); \
if (argv == NULL) { \
return -1; \
} \
} while (0)

#define ED448_API_READ_ARGV(NAMESPACE, FUNCTION) \
do { \
argv = (ED448_API_F_ARGV_T(NAMESPACE, FUNCTION) *)(request->argv); \
} while (0)

#define ED448_RES_TAG(REQUEST) ERL_DRV_EXT2TERM, (ErlDrvTermData)(REQUEST->tag.buff), REQUEST->tag.index

#define ED448_RESPOND(REQUEST, SPEC, FILE, LINE) \
do { \
if (erl_drv_send_term(REQUEST->port->term_port, REQUEST->caller, SPEC, sizeof(SPEC) / sizeof(SPEC[0])) < 0) { \
TRACE_F("error sending term\n", FILE, LINE); \
} \
} while (0)

#endif
Loading

0 comments on commit 0e834dc

Please sign in to comment.