diff --git a/pkg/libb2/Makefile b/pkg/libb2/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..16ac11aae1fcdd675190ab72149140931f8d3104 --- /dev/null +++ b/pkg/libb2/Makefile @@ -0,0 +1,12 @@ +PKG_NAME = libb2 +PKG_URL = https://github.com/BLAKE2/libb2 +PKG_VERSION = 60ea749837362c226e8501718f505ab138e5c19d # v0.98 +PKG_LICENSE = CC0-1.0 + +.PHONY: all + +all: git-download + "$(MAKE)" -C $(PKG_BUILDDIR)/src \ + -f $(RIOTPKG)/libb2/Makefile.$(PKG_NAME) + +include $(RIOTBASE)/pkg/pkg.mk diff --git a/pkg/libb2/Makefile.include b/pkg/libb2/Makefile.include new file mode 100644 index 0000000000000000000000000000000000000000..46c0687c0b919d88e6ad97e4dd7590953f89ba82 --- /dev/null +++ b/pkg/libb2/Makefile.include @@ -0,0 +1,2 @@ +INCLUDES += -I$(PKGDIRBASE)/libb2/src +INCLUDES += -I$(RIOTPKG)/libb2/include diff --git a/pkg/libb2/Makefile.libb2 b/pkg/libb2/Makefile.libb2 new file mode 100644 index 0000000000000000000000000000000000000000..ec993728f8e345203915eaae0f6ed894bdf8e24c --- /dev/null +++ b/pkg/libb2/Makefile.libb2 @@ -0,0 +1,11 @@ +MODULE = libb2 + +# Compiling for native triggers this warning +CFLAGS += -Wno-unused-function + +SRC := blake2s-ref.c \ + blake2b-ref.c \ + blake2sp.c \ + blake2bp.c + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/libb2/doc.txt b/pkg/libb2/doc.txt new file mode 100644 index 0000000000000000000000000000000000000000..7298bc9dfdc772ce5ec67205815993bb4311af01 --- /dev/null +++ b/pkg/libb2/doc.txt @@ -0,0 +1,37 @@ +/** + * @defgroup pkg_libb2 BLAKE2 hashing library + * @ingroup pkg + * @brief C library providing BLAKE2b, BLAKE2s, BLAKE2bp, BLAKE2sp + + * + * # BLAKE2 RIOT package + * + * BLAKE2 is a cryptographic hash function specified in RFC 7693. + * It claims to be faster than MD5, SHA-1, SHA-2, and SHA-3, yet at least as + * secure as the latest standard SHA-3. + * + * BLAKE2 comes in two flavors: + * + * - BLAKE2b (or just BLAKE2) is optimized for 64-bit platforms—including NEON-enabled ARMs—and + * produces digests of any size between 1 and 64 bytes + * - BLAKE2s is optimized for 8- to 32-bit platforms and produces digests of + * any size between 1 and 32 bytes + * + * For more information, see the [BLAKE2 website](https://blake2.net/). + * + * ## Usage + * + * Add it as a package in your application's Makefile: + * + * ```makefile + * USEPKG += libb2 + * ``` + * + * Include the BLAKE2 header in your code: + * + * ```c + * #include "blake2.h" + * ``` + * + * @see https://github.com/BLAKE2/libb2 + */ diff --git a/pkg/libb2/include/config.h b/pkg/libb2/include/config.h new file mode 100644 index 0000000000000000000000000000000000000000..55cc6a0e8e5b33e3855e612fc35b8e734831342b --- /dev/null +++ b/pkg/libb2/include/config.h @@ -0,0 +1,19 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Add no suffix to the functions */ +#define SUFFIX + +/* Test for a little-endian machine */ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define NATIVE_LITTLE_ENDIAN +#endif + +#ifdef __cplusplus +} +#endif +#endif /* CONFIG_H */ diff --git a/tests/pkg_libb2/Makefile b/tests/pkg_libb2/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..c7bd172537fd70cf9df5e881e0a56169c7985023 --- /dev/null +++ b/tests/pkg_libb2/Makefile @@ -0,0 +1,14 @@ +include ../Makefile.tests_common + +# BLAKE2s + BLAKE2 is too big for these boards +BOARD_INSUFFICIENT_MEMORY := arduino-duemilanove arduino-uno chronos \ + mega-xplained msb-430 msb-430h nucleo-f031k6 \ + nucleo-f042k6 nucleo-l031k6 telosb waspmote-pro \ + wsn430-v1_3b wsn430-v1_4 z1 + +TEST_ON_CI_WHITELIST += all + +USEPKG += libb2 +USEMODULE += embunit + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_libb2/main.c b/tests/pkg_libb2/main.c new file mode 100644 index 0000000000000000000000000000000000000000..1ac0d2c432c73c678ada7eaf8fee0dd77466f2ea --- /dev/null +++ b/tests/pkg_libb2/main.c @@ -0,0 +1,86 @@ +/* + * 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 libb2 package + * + * @author Silke Hofstra <silke@slxh.eu> + * + * @} + */ + +#include <string.h> +#include "embUnit.h" +#include "blake2.h" + +const uint8_t msg[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c }; +const uint8_t key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f }; +const uint8_t b2s[] = { 0xf5, 0xc4, 0xbe, 0xc6, 0xd6, 0x2f, 0xc6, 0x08, + 0xbf, 0x41, 0xcc, 0x11, 0x5f, 0x16, 0xd6, 0x1c, + 0x7e, 0xfd, 0x3f, 0xf6, 0xc6, 0x56, 0x92, 0xbb, + 0xe0, 0xaf, 0xff, 0xb1, 0xfe, 0xde, 0x74, 0x75 }; +const uint8_t b2b[] = { 0x43, 0xd4, 0x4b, 0xfa, 0x18, 0x76, 0x8c, 0x59, + 0x89, 0x6b, 0xf7, 0xed, 0x17, 0x65, 0xcb, 0x2d, + 0x14, 0xaf, 0x8c, 0x26, 0x02, 0x66, 0x03, 0x90, + 0x99, 0xb2, 0x5a, 0x60, 0x3e, 0x4d, 0xdc, 0x50, + 0x39, 0xd6, 0xef, 0x3a, 0x91, 0x84, 0x7d, 0x10, + 0x88, 0xd4, 0x01, 0xc0, 0xc7, 0xe8, 0x47, 0x78, + 0x1a, 0x8a, 0x59, 0x0d, 0x33, 0xa3, 0xc6, 0xcb, + 0x4d, 0xf0, 0xfa, 0xb1, 0xc2, 0xf2, 0x23, 0x55 }; + +/* Test BLAKE2s */ +static void test_blake2s(void) +{ + uint8_t hash[BLAKE2S_OUTBYTES]; + int res = blake2s(hash, msg, key, + sizeof hash, sizeof msg, BLAKE2S_KEYBYTES); + + TEST_ASSERT(res == 0); + TEST_ASSERT(memcmp(b2s, hash, sizeof hash) == 0); +} + +/* Test BLAKE2b */ +static void test_blake2b(void) +{ + uint8_t hash[BLAKE2B_OUTBYTES]; + int res = blake2b(hash, msg, key, + sizeof hash, sizeof msg, BLAKE2B_KEYBYTES); + + TEST_ASSERT(res == 0); + TEST_ASSERT(memcmp(b2b, hash, sizeof hash) == 0); +} + +Test *tests_blake2(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_blake2s), + new_TestFixture(test_blake2b), + }; + EMB_UNIT_TESTCALLER(blake2_tests, NULL, NULL, fixtures); + return (Test *)&blake2_tests; +} + +int main(void) +{ + TESTS_START(); + TESTS_RUN(tests_blake2()); + TESTS_END(); + return 0; +} diff --git a/tests/pkg_libb2/tests/01-run.py b/tests/pkg_libb2/tests/01-run.py new file mode 100755 index 0000000000000000000000000000000000000000..36a1bc15fc8d23e16c94a994d0724acd75d43dc8 --- /dev/null +++ b/tests/pkg_libb2/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 (2 tests)') + + +if __name__ == "__main__": + sys.exit(run(testfunc))