diff --git a/pkg/hacl/Makefile b/pkg/hacl/Makefile new file mode 100644 index 0000000000..cf3f0d5634 --- /dev/null +++ b/pkg/hacl/Makefile @@ -0,0 +1,12 @@ +PKG_NAME=hacl +PKG_URL=https://github.com/mitls/hacl-c +PKG_VERSION=aac05f5094fc92569169d5a2af54c12387160634 +PKG_LICENSE=MIT + +.PHONY: all + +all: git-download + @cp $(RIOTBASE)/pkg/hacl/src/* $(PKG_BUILDDIR) + "$(MAKE)" -C $(PKG_BUILDDIR) -f $(CURDIR)/Makefile.$(PKG_NAME) + +include $(RIOTBASE)/pkg/pkg.mk diff --git a/pkg/hacl/Makefile.dep b/pkg/hacl/Makefile.dep new file mode 100644 index 0000000000..8030144a9a --- /dev/null +++ b/pkg/hacl/Makefile.dep @@ -0,0 +1 @@ +USEMODULE+=random diff --git a/pkg/hacl/Makefile.hacl b/pkg/hacl/Makefile.hacl new file mode 100644 index 0000000000..2268ccc1d5 --- /dev/null +++ b/pkg/hacl/Makefile.hacl @@ -0,0 +1,5 @@ +MODULE=hacl + +include $(RIOTBASE)/Makefile.base + +CFLAGS += -DKRML_NOUINT128 -Wno-unused-parameter diff --git a/pkg/hacl/Makefile.include b/pkg/hacl/Makefile.include new file mode 100644 index 0000000000..1ed0ad0f63 --- /dev/null +++ b/pkg/hacl/Makefile.include @@ -0,0 +1 @@ +INCLUDES += -I$(PKGDIRBASE)/hacl/ diff --git a/pkg/hacl/doc.txt b/pkg/hacl/doc.txt new file mode 100644 index 0000000000..4e1be81358 --- /dev/null +++ b/pkg/hacl/doc.txt @@ -0,0 +1,30 @@ +/** + * @defgroup pkg_hacl HACL* High Assurance Cryptographic Library + * @ingroup pkg + * @ingroup sys_crypto + * @brief Support for HACL* (High Assurance Cryptographic Library) + * + * # HACL* RIOT package + * + * ## Usage + * + * Just add it as a package in your application: + * + * ```makefile + * USEPKG += hacl + * ``` + * + * And don't forget to include the header for the HACL* standard API: + * + * ```c + * #include + * ``` + + * or for HACL*'s NaCl-compatible API: + * + * ```c + * #include + * ``` +* + * @see https://github.com/mitls/hacl-c + */ diff --git a/pkg/hacl/src/randombytes.c b/pkg/hacl/src/randombytes.c new file mode 100644 index 0000000000..e318dfa3b2 --- /dev/null +++ b/pkg/hacl/src/randombytes.c @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2016 Kaspar Schleiser + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#include + +#include "random.h" + + +void randombytes(uint8_t *target, uint64_t n) +{ + /* HACL* (haclnacl.c) needs uint64_t as "n" parameter, random provides uint32 */ + random_bytes(target, n); +} diff --git a/tests/unittests/Makefile b/tests/unittests/Makefile index 2df160950e..a338af3aa6 100644 --- a/tests/unittests/Makefile +++ b/tests/unittests/Makefile @@ -214,6 +214,7 @@ ifneq (, $(filter $(AVR_BOARDS), $(BOARD))) LARGE_STACK_TESTS += tests-qDSA endif +LARGE_STACK_TESTS += tests-hacl LARGE_STACK_TESTS += tests-tweetnacl ifneq (,$(filter $(LARGE_STACK_TESTS), $(UNIT_TESTS))) CFLAGS += -DTHREAD_STACKSIZE_MAIN=\(4*THREAD_STACKSIZE_DEFAULT+THREAD_EXTRA_STACKSIZE_PRINTF\) diff --git a/tests/unittests/tests-hacl/Makefile b/tests/unittests/tests-hacl/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-hacl/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-hacl/Makefile.include b/tests/unittests/tests-hacl/Makefile.include new file mode 100644 index 0000000000..f2612ffbf4 --- /dev/null +++ b/tests/unittests/tests-hacl/Makefile.include @@ -0,0 +1,2 @@ +USEMODULE += random +USEPKG += hacl diff --git a/tests/unittests/tests-hacl/tests-hacl.c b/tests/unittests/tests-hacl/tests-hacl.c new file mode 100644 index 0000000000..37c48a529c --- /dev/null +++ b/tests/unittests/tests-hacl/tests-hacl.c @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2018 INRIA + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup unittests + * @{ + * + * @file + * @brief HACL* crypto library tests + * + * @author Benjamin Beurdouche + * @author Kaspar Schleiser + * @author Martin Landsmann + * + * @} + */ + +#include +#include +#include "embUnit.h" +#include "tests-hacl.h" + +static const char message[] = "0123456789abcdef"; +static char r[sizeof(message)]; + +#define MLEN (sizeof(message) + crypto_box_ZEROBYTES) + +static unsigned char alice_pk[crypto_box_PUBLICKEYBYTES]; +static unsigned char alice_sk[crypto_box_SECRETKEYBYTES]; +static unsigned char bob_pk[crypto_box_PUBLICKEYBYTES]; +static unsigned char bob_sk[crypto_box_SECRETKEYBYTES]; +static unsigned char m[MLEN]; +static unsigned char c[MLEN]; +static const unsigned char n[crypto_box_NONCEBYTES]; +static unsigned char result[MLEN]; + +static void setUp(void) +{ + /* Initialize */ + random_init(0); +} + +static void test_hacl_01(void) +{ + int res; + + /* Creating keypair ALICE... */ + crypto_box_keypair(alice_pk, alice_sk); + + /* Creating keypair BOB... */ + crypto_box_keypair(bob_pk, bob_sk); + + memset(m, 0, crypto_box_ZEROBYTES); + memcpy(m + crypto_box_ZEROBYTES, message, MLEN - crypto_box_ZEROBYTES); + + /* Encrypting using pk_bob... */ + crypto_box(c, m, MLEN, n, bob_pk, alice_sk); + + memset(result, '\0', sizeof(result)); + + /* Decrypting... */ + res = crypto_box_open(result, c, MLEN, n, alice_pk, bob_sk); + + TEST_ASSERT_EQUAL_INT(0, res); + + memset(r, 0, sizeof(r)); + memcpy(r, result + crypto_box_ZEROBYTES, MLEN - crypto_box_ZEROBYTES); + + TEST_ASSERT_EQUAL_STRING((const char*)message, (const char*)r); +} + +Test *tests_hacl_all(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_hacl_01) + }; + + EMB_UNIT_TESTCALLER(hacl_tests, setUp, NULL, fixtures); + return (Test*)&hacl_tests; +} + +void tests_hacl(void) +{ + TESTS_RUN(tests_hacl_all()); +} diff --git a/tests/unittests/tests-hacl/tests-hacl.h b/tests/unittests/tests-hacl/tests-hacl.h new file mode 100644 index 0000000000..4020e476b3 --- /dev/null +++ b/tests/unittests/tests-hacl/tests-hacl.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2018 INRIA + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @addtogroup unittests + * @{ + * + * @file + * @brief Unittests for the ``hacl`` package + * + * @author Benjamin Beurdouche + */ +#ifndef TESTS_HACL_H +#define TESTS_HACL_H + +#include "embUnit/embUnit.h" +#include "random.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* @brief The entry point of this test suite. +*/ +void tests_hacl(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_HACL_H */ +/** @} */