Skip to content

Commit

Permalink
Merge pull request #9980 from silkeh/libhydrogen
Browse files Browse the repository at this point in the history
pkg: add libhydrogen
  • Loading branch information
kaspar030 committed Sep 21, 2018
2 parents c4016fc + 398d1d4 commit 91f7110
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 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
1 change: 1 addition & 0 deletions pkg/libhydrogen/Makefile.dep
@@ -0,0 +1 @@
USEMODULE += random
1 change: 1 addition & 0 deletions pkg/libhydrogen/Makefile.include
@@ -0,0 +1 @@
INCLUDES += -I$(PKGDIRBASE)/libhydrogen
3 changes: 3 additions & 0 deletions pkg/libhydrogen/Makefile.libhydrogen
@@ -0,0 +1,3 @@
MODULE = libhydrogen

include $(RIOTBASE)/Makefile.base
30 changes: 30 additions & 0 deletions 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
*/
35 changes: 35 additions & 0 deletions pkg/libhydrogen/patches/0001-Add-support-for-RIOT-OS.patch
@@ -0,0 +1,35 @@
From 59ba85698386d2d55c86a00557169f3f5efad502 Mon Sep 17 00:00:00 2001
From: Silke Hofstra <silke@slxh.eu>
Date: Thu, 20 Sep 2018 16:32:40 +0200
Subject: [PATCH] Add support for RIOT OS

---
impl/random.h | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/impl/random.h b/impl/random.h
index f2ca1a4..2c09e02 100644
--- a/impl/random.h
+++ b/impl/random.h
@@ -99,6 +99,18 @@ hydro_random_init(void)
return 0;
}

+#elif defined(RIOT_VERSION) && !defined(__unix__)
+
+#include <random.h>
+
+static int
+hydro_random_init(void)
+{
+ random_bytes(hydro_random_context.state, sizeof hydro_random_context.state);
+ hydro_random_context.counter = ~LOAD64_LE(hydro_random_context.state);
+ return 0;
+}
+
#elif defined(_WIN32)

#include <windows.h>
--
2.18.0

14 changes: 14 additions & 0 deletions 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
88 changes: 88 additions & 0 deletions 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;
}
18 changes: 18 additions & 0 deletions 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))

0 comments on commit 91f7110

Please sign in to comment.