diff --git a/pkg/libhydrogen/Makefile b/pkg/libhydrogen/Makefile new file mode 100644 index 0000000000..12abf19121 --- /dev/null +++ b/pkg/libhydrogen/Makefile @@ -0,0 +1,15 @@ +PKG_NAME = libhydrogen +PKG_URL = https://github.com/jedisct1/libhydrogen +PKG_VERSION = 39eb529905ce118b674a7723c0d2b48074b9986d +PKG_LICENSE = ISC + +# This warning is triggered on non-32bit platforms +CFLAGS += -Wno-type-limits + +.PHONY: all + +all: git-download + "$(MAKE)" -C $(PKG_BUILDDIR) \ + -f $(RIOTPKG)/libhydrogen/Makefile.$(PKG_NAME) + +include $(RIOTBASE)/pkg/pkg.mk diff --git a/pkg/libhydrogen/Makefile.dep b/pkg/libhydrogen/Makefile.dep new file mode 100644 index 0000000000..3941d5a187 --- /dev/null +++ b/pkg/libhydrogen/Makefile.dep @@ -0,0 +1 @@ +USEMODULE += random diff --git a/pkg/libhydrogen/Makefile.include b/pkg/libhydrogen/Makefile.include new file mode 100644 index 0000000000..ff9e750134 --- /dev/null +++ b/pkg/libhydrogen/Makefile.include @@ -0,0 +1 @@ +INCLUDES += -I$(PKGDIRBASE)/libhydrogen diff --git a/pkg/libhydrogen/Makefile.libhydrogen b/pkg/libhydrogen/Makefile.libhydrogen new file mode 100644 index 0000000000..eee65bb350 --- /dev/null +++ b/pkg/libhydrogen/Makefile.libhydrogen @@ -0,0 +1,3 @@ +MODULE = libhydrogen + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/libhydrogen/doc.txt b/pkg/libhydrogen/doc.txt new file mode 100644 index 0000000000..2555e9eb16 --- /dev/null +++ b/pkg/libhydrogen/doc.txt @@ -0,0 +1,30 @@ +/** + * @defgroup pkg_libhydrogen LibHydrogen cryptographic library + * @ingroup pkg + * @brief A lightweight, secure, easy-to-use crypto library suitable for constrained environments. + * + * # LibHydrogen RIOT package + * + * The Hydrogen library is a small, easy-to-use, hard-to-misuse cryptographic + * library. It provides functions for random numbers, generic hashing, key + * derivation, secret-key encryption, public-key signatures, key exchange and + * password hashing. + * + * Full documentation can be found on the [LibHydrogen wiki](https://github.com/jedisct1/libhydrogen/wiki). + * + * ## Usage + * + * Add it as a package in your application's Makefile: + * + * ```makefile + * USEPKG += libhydrogen + * ``` + * + * Include the LibHydrogen header in your code: + * + * ```c + * #include "hydrogen.h" + * ``` + * + * @see https://github.com/jedisct1/libhydrogen + */ diff --git a/pkg/libhydrogen/patches/0001-Add-support-for-RIOT-OS.patch b/pkg/libhydrogen/patches/0001-Add-support-for-RIOT-OS.patch new file mode 100644 index 0000000000..2bc841db0f Binary files /dev/null and b/pkg/libhydrogen/patches/0001-Add-support-for-RIOT-OS.patch differ diff --git a/tests/pkg_libhydrogen/Makefile b/tests/pkg_libhydrogen/Makefile new file mode 100644 index 0000000000..4335d5144b --- /dev/null +++ b/tests/pkg_libhydrogen/Makefile @@ -0,0 +1,14 @@ +include ../Makefile.tests_common + +# AVR boards: require avr-gcc >= 7.0 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60040) +# MSP430 boards: invalid alignment of 'hydro_random_context' +BOARD_BLACKLIST := arduino-duemilanove arduino-mega2560 arduino-uno \ + jiminy-mega256rfr2 mega-xplained waspmote-pro \ + chronos msb-430 msb-430h telosb wsn430-v1_3b wsn430-v1_4 z1 + +TEST_ON_CI_WHITELIST += all + +USEPKG += libhydrogen +USEMODULE += embunit + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_libhydrogen/main.c b/tests/pkg_libhydrogen/main.c new file mode 100644 index 0000000000..486e0514b5 --- /dev/null +++ b/tests/pkg_libhydrogen/main.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2018 Silke Hofstra + * + * 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 tests + * @{ + * + * @file + * @brief Test the libhydrogen package + * + * @author Silke Hofstra + * + * @} + */ + +#include "embUnit.h" +#include "hydrogen.h" + +static char context[] = "examples"; +static char message[] = "0123456789abcdef"; + +/* This performs setup, but should never fail */ +static void test_hydro_init(void) +{ + TEST_ASSERT(hydro_init() == 0); +} + +/* Test public-key signatures */ +static void test_hydro_signverify(void) +{ + hydro_sign_keypair key_pair; + + hydro_sign_keygen(&key_pair); + + uint8_t signature[hydro_sign_BYTES]; + + hydro_sign_create(signature, message, sizeof message, context, key_pair.sk); + + int res = hydro_sign_verify(signature, message, sizeof message, context, key_pair.pk); + + TEST_ASSERT(res == 0); +} + +/* Test secret-key encryption */ +static void test_hydro_secretbox_encryptdecrypt(void) +{ + uint8_t key[hydro_secretbox_KEYBYTES]; + uint8_t ciphertext[hydro_secretbox_HEADERBYTES + sizeof message]; + + hydro_secretbox_keygen(key); + hydro_secretbox_encrypt(ciphertext, message, sizeof message, 0, context, key); + + char decrypted[sizeof message]; + int res = hydro_secretbox_decrypt( + decrypted, + ciphertext, + hydro_secretbox_HEADERBYTES + sizeof message, + 0, + context, + key + ); + + TEST_ASSERT(res == 0); +} + +Test *tests_libhydrogen(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_hydro_init), + new_TestFixture(test_hydro_signverify), + new_TestFixture(test_hydro_secretbox_encryptdecrypt), + }; + EMB_UNIT_TESTCALLER(libhydrogen_tests, NULL, NULL, fixtures); + return (Test *)&libhydrogen_tests; +} + +int main(void) +{ + TESTS_START(); + TESTS_RUN(tests_libhydrogen()); + TESTS_END(); + return 0; +} diff --git a/tests/pkg_libhydrogen/tests/01-run.py b/tests/pkg_libhydrogen/tests/01-run.py new file mode 100755 index 0000000000..a9078c0d97 --- /dev/null +++ b/tests/pkg_libhydrogen/tests/01-run.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2017 Freie Universität Berlin +# +# 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. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact('OK (3 tests)') + + +if __name__ == "__main__": + sys.exit(run(testfunc))