From 398d1d44d25b1c31fe882150115f332cdb57c629 Mon Sep 17 00:00:00 2001
From: Silke Hofstra <silke@slxh.eu>
Date: Thu, 20 Sep 2018 16:40:22 +0200
Subject: [PATCH] pkg: add libhydrogen

---
 pkg/libhydrogen/Makefile                      |  15 +++
 pkg/libhydrogen/Makefile.dep                  |   1 +
 pkg/libhydrogen/Makefile.include              |   1 +
 pkg/libhydrogen/Makefile.libhydrogen          |   3 +
 pkg/libhydrogen/doc.txt                       |  30 ++++++
 .../0001-Add-support-for-RIOT-OS.patch        | Bin 0 -> 788 bytes
 tests/pkg_libhydrogen/Makefile                |  14 +++
 tests/pkg_libhydrogen/main.c                  |  88 ++++++++++++++++++
 tests/pkg_libhydrogen/tests/01-run.py         |  18 ++++
 9 files changed, 170 insertions(+)
 create mode 100644 pkg/libhydrogen/Makefile
 create mode 100644 pkg/libhydrogen/Makefile.dep
 create mode 100644 pkg/libhydrogen/Makefile.include
 create mode 100644 pkg/libhydrogen/Makefile.libhydrogen
 create mode 100644 pkg/libhydrogen/doc.txt
 create mode 100644 pkg/libhydrogen/patches/0001-Add-support-for-RIOT-OS.patch
 create mode 100644 tests/pkg_libhydrogen/Makefile
 create mode 100644 tests/pkg_libhydrogen/main.c
 create mode 100755 tests/pkg_libhydrogen/tests/01-run.py

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 0000000000000000000000000000000000000000..2bc841db0f64e501125692e63aebc4e4ceef878e
GIT binary patch
literal 788
zcmZ=O%Fk6WwM<I1Fg3HZFt#vDF-kEtO|~#gG%zqVH8(V~Ofyb1O-)NoF*Pt!@XgOt
z2u>|fFf>;%Ft7q61tS9kLoSdpRtmwHIoYWS9{Fj-B}It}HpL*WLvc<;hF)r^9hXaD
zNvf4XNJgoSf{_8(7$XBi3k5?nD`O)o69WZp10w?iuHe$7tkmQZD~0F)#}H?aSOv$F
z6oul_f`a^_5{0z<B84DN{}2WLU@k6QU0p7P%-n(;{i4LYl>A)1422p6Ln8%kB*3L$
zsF0SKld6!Mk(igBnxX?zkeOGUT2zvmpI5A*t;xlel9`sKpsSmnS)!1rk7`~LDxWJe
zFD11?A<ZZ`(J;|OPtPdXz%tdqNWsv+z|6!1<REktxU{vk70^v_a8S^-wA3+E(6+SH
zF|<%{a8SsoOexBbhxj8tGcU75qbxr&MUzVb1d38iN{jLo46L~nYPl4+w3SnHGSd`N
zQqwZ?Qd2ZQ;SnF^8Wim5@29Dtrlz0>lZlTn&C9HakJseV=F(Qq%uCKGO-WU-f!Ly#
zVFwZ}E=erOOjgLuE8)_{<p}L+E^V+oz`-1!R9TW*tbw99IX|x?wW351WLm0@LUCqQ
zYJM83r%;q=T0>04YDjW^X<kWck%Fy4osYkxi<wEhk1HPQU}hmgNxPOyn@a&Tl;XoZ
k{fvz?xfHk*5Ft^XnU|7ZUJMEfU0nq(BRxY4Jp(Q-0B^bQ@Bjb+

literal 0
HcmV?d00001

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 <silke@slxh.eu>
+ *
+ * @}
+ */
+
+#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))
-- 
GitLab