diff --git a/pkg/hacl/Makefile b/pkg/hacl/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..cf3f0d56348a04dfcf402286ba212cc12c8e999a --- /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 0000000000000000000000000000000000000000..8030144a9aacc54c6cba99fc905e02843373ae17 --- /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 0000000000000000000000000000000000000000..2268ccc1d53aff5eb428cfa00adf6c4506e9e841 --- /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 0000000000000000000000000000000000000000..1ed0ad0f63e3fd2630ab02822890be292f86de42 --- /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 0000000000000000000000000000000000000000..4e1be813586586e8d703fb7e216e5b918a3ab3c2 --- /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 <HACL.h> + * ``` + + * or for HACL*'s NaCl-compatible API: + * + * ```c + * #include <haclnacl.h> + * ``` +* + * @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 0000000000000000000000000000000000000000..e318dfa3b2e94a73a0cf4c3c502fdff9d0f02129 --- /dev/null +++ b/pkg/hacl/src/randombytes.c @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de> + * + * 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 <stdint.h> + +#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 2df160950e9bba0ac268d179f9a2f13711a04f0f..a338af3aa6d9688df2691e4864f6c306fd6708be 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 0000000000000000000000000000000000000000..48422e909a47d7cd428d10fa73825060ccc8d8c2 --- /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 0000000000000000000000000000000000000000..f2612ffbf49bd57581de74508359afa1621f0deb --- /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 0000000000000000000000000000000000000000..37c48a529c13e5e72c27acaf6e62214b1570a692 --- /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 <benjamin.beurdouche@inria.fr> + * @author Kaspar Schleiser <kaspar@schleiser.de> + * @author Martin Landsmann <Martin.Landsmann@HAW-Hamburg.de> + * + * @} + */ + +#include <string.h> +#include <haclnacl.h> +#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 0000000000000000000000000000000000000000..4020e476b306aed425f05063962b876f136fe04a --- /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 <benjamin.beurdouche@inria.fr> + */ +#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 */ +/** @} */