From 8b6ddca6e9087fa78132f9b5503f2812acef0c63 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Mon, 25 Apr 2022 16:07:08 +0200 Subject: [PATCH] pkg/libcose: bump, set random function on init --- makefiles/default_modules.deps.mk | 4 ++ pkg/libcose/Kconfig | 10 ++++ pkg/libcose/Makefile | 2 +- pkg/libcose/Makefile.dep | 6 +++ pkg/libcose/Makefile.include | 12 ++++- pkg/libcose/Makefile.libcose_crypt | 2 + pkg/libcose/contrib/Makefile | 3 ++ pkg/libcose/include/cose/crypto/riot.h | 48 ++++++++++++++++++ pkg/libcose/init/Makefile | 3 ++ pkg/libcose/init/init.c | 48 ++++++++++++++++++ ...ndom_bytes-function-instead-of-rando.patch | Bin 3611 -> 0 bytes ...-add-defines-for-RIOT-crypto-backend.patch | Bin 0 -> 1375 bytes 12 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 pkg/libcose/contrib/Makefile create mode 100644 pkg/libcose/include/cose/crypto/riot.h create mode 100644 pkg/libcose/init/Makefile create mode 100644 pkg/libcose/init/init.c delete mode 100644 pkg/libcose/patches/0001-RIOT-Use-RIOT-random_bytes-function-instead-of-rando.patch create mode 100644 pkg/libcose/patches/0001-cose-crypto-add-defines-for-RIOT-crypto-backend.patch diff --git a/makefiles/default_modules.deps.mk b/makefiles/default_modules.deps.mk index b128a73292..4a6f92069d 100644 --- a/makefiles/default_modules.deps.mk +++ b/makefiles/default_modules.deps.mk @@ -14,6 +14,10 @@ ifneq (,$(filter auto_init_saul,$(USEMODULE))) USEMODULE += saul_init_devs endif +ifneq (,$(filter auto_init_libcose_crypt,$(USEMODULE))) + USEMODULE += libcose_crypt_init +endif + ifneq (,$(filter xtimer,$(USEMODULE))) ifeq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) USEMODULE += div diff --git a/pkg/libcose/Kconfig b/pkg/libcose/Kconfig index 48e80253b0..944d64451d 100644 --- a/pkg/libcose/Kconfig +++ b/pkg/libcose/Kconfig @@ -31,6 +31,16 @@ config MODULE_LIBCOSE_CRYPT_MONOCYPHER depends on TEST_KCONFIG depends on PACKAGE_MONOCYPHER +config MODULE_LIBCOSE_CRYPT_INIT + bool "LibCose Crypt Initialization functions" + default y + +config MODULE_AUTO_INIT_LIBCOSE_CRYPT + bool "Auto initialize LibCose Crypt" + depends on MODULE_AUTO_INIT + select MODULE_LIBCOSE_CRYPT_INIT + default y + endif # PACKAGE_LIBCOSE config MODULE_LIBCOSE_CRYPT diff --git a/pkg/libcose/Makefile b/pkg/libcose/Makefile index 99d1d063e0..c5fe7caa14 100644 --- a/pkg/libcose/Makefile +++ b/pkg/libcose/Makefile @@ -1,6 +1,6 @@ PKG_NAME=libcose PKG_URL=https://github.com/bergzand/libcose -PKG_VERSION=2929fdce7affbd5bb9db201370d95d8f7cf680f9 +PKG_VERSION=ea1fed87d6ca9b478f8bed323af97e6b192c0a6d PKG_LICENSE=LGPL include $(RIOTBASE)/pkg/pkg.mk diff --git a/pkg/libcose/Makefile.dep b/pkg/libcose/Makefile.dep index ffd4e2c6ca..077f0f1fd7 100644 --- a/pkg/libcose/Makefile.dep +++ b/pkg/libcose/Makefile.dep @@ -13,3 +13,9 @@ endif ifneq (,$(filter libcose_crypt_tinycrypt,$(USEMODULE))) USEPKG += tinycrypt endif +ifneq (,$(filter libcose_crypt_monocypher,$(USEMODULE))) + USEPKG += monocypher +endif + +DEFAULT_MODULE += auto_init_libcose_crypt +DEFAULT_MODULE += libcose_crypt_init diff --git a/pkg/libcose/Makefile.include b/pkg/libcose/Makefile.include index 7342c0b07a..eee721d553 100644 --- a/pkg/libcose/Makefile.include +++ b/pkg/libcose/Makefile.include @@ -10,6 +10,16 @@ endif ifneq (,$(filter libcose_crypt_tinycrypt,$(USEMODULE))) CFLAGS += -DCRYPTO_TINYCRYPT endif +ifneq (,$(filter libcose_crypt_monocypher,$(USEMODULE))) + CFLAGS += -DCRYPTO_MONOCYPHER +endif +ifneq (,$(filter libcose_crypt_init,$(USEMODULE))) + DIRS += $(RIOTBASE)/pkg/libcose/init +endif # Declare pseudomodules here to be selfcontained -PSEUDOMODULES += libcose_crypt_% +PSEUDOMODULES += libcose_crypt_c25519 +PSEUDOMODULES += libcose_crypt_hacl +PSEUDOMODULES += libcose_crypt_tinycrypt +PSEUDOMODULES += libcose_crypt_monocypher +PSEUDOMODULES += auto_init_libcose_crypt diff --git a/pkg/libcose/Makefile.libcose_crypt b/pkg/libcose/Makefile.libcose_crypt index a381cb2df3..081fd78447 100644 --- a/pkg/libcose/Makefile.libcose_crypt +++ b/pkg/libcose/Makefile.libcose_crypt @@ -1,4 +1,6 @@ MODULE := libcose_crypt SUBMODULES = 1 +SRC += keygen_symm.c + include $(RIOTBASE)/Makefile.base diff --git a/pkg/libcose/contrib/Makefile b/pkg/libcose/contrib/Makefile new file mode 100644 index 0000000000..7738090630 --- /dev/null +++ b/pkg/libcose/contrib/Makefile @@ -0,0 +1,3 @@ +MODULE = libcose_crypt_riot + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/libcose/include/cose/crypto/riot.h b/pkg/libcose/include/cose/crypto/riot.h new file mode 100644 index 0000000000..cdab01926e --- /dev/null +++ b/pkg/libcose/include/cose/crypto/riot.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 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 pkg_libcose + * + * @{ + * + * @file + * @brief Crypto function api for glueing RIOT crypto libraries + * + * @author Francisco Molina + */ + +#ifndef COSE_CRYPTO_RIOT_H +#define COSE_CRYPTO_RIOT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef AUTO_INIT_PRIO_MOD_LIBCOSE +/** + * @brief libCOSE init priority + */ +#define AUTO_INIT_PRIO_MOD_LIBCOSE 1050 +#endif + +/** + * @brief Initialize libCOSE RIOT crypto backend + * + * @note Automatically called if 'auto_init_libcose_crypt_riot' is included + * + */ +void libcose_crypt_init(void); + +#ifdef __cplusplus +} +#endif + +#endif /* COSE_CRYPTO_RIOT_H */ + +/** @} */ diff --git a/pkg/libcose/init/Makefile b/pkg/libcose/init/Makefile new file mode 100644 index 0000000000..00674b2292 --- /dev/null +++ b/pkg/libcose/init/Makefile @@ -0,0 +1,3 @@ +MODULE = libcose_crypt_init + +include $(RIOTBASE)/Makefile.base diff --git a/pkg/libcose/init/init.c b/pkg/libcose/init/init.c new file mode 100644 index 0000000000..cd3e9fa9c9 --- /dev/null +++ b/pkg/libcose/init/init.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 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 pkg_libcose + * @{ + * + * @file + * @brief RIOT as a crypto backend common functions + * + * @author Francisco Molina + * + * @} + */ + + +#include +#include "random.h" +#include "kernel_defines.h" +#include "xfa.h" + +#include "cose/crypto.h" + +#if IS_USED(MODULE_AUTO_INIT) +#include "auto_init_utils.h" +#endif + +static int _riot_random_bytes(void* arg, unsigned char * buf, size_t len) +{ + (void)arg; + random_bytes((uint8_t*) buf, len); + return 1; +} + +void libcose_crypt_init(void) +{ + cose_crypt_set_rng(_riot_random_bytes, NULL); +} + +#if IS_USED(MODULE_AUTO_INIT_LIBCOSE_CRYPT) +/* initialize just after random module */ +AUTO_INIT(libcose_crypt_init, AUTO_INIT_PRIO_MOD_LIBCOSE); +#endif diff --git a/pkg/libcose/patches/0001-RIOT-Use-RIOT-random_bytes-function-instead-of-rando.patch b/pkg/libcose/patches/0001-RIOT-Use-RIOT-random_bytes-function-instead-of-rando.patch deleted file mode 100644 index 831e6e8af022be87e944ff14cf791694ecb98152..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3611 zcmcInZByGw4F0~qLeqXpa2))ax8oSNU}s;Wik#c?MhlnkNj|+#1EjTisAP>)iis)>luotnI1KIT3vci=jX zV9FMtc0o}F;tQIhsN@{Y4B(@aE}R6WbRM!GWeFVJ3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Cw-M{3;$&xT3b`w3L*ahepONw diff --git a/pkg/libcose/patches/0001-cose-crypto-add-defines-for-RIOT-crypto-backend.patch b/pkg/libcose/patches/0001-cose-crypto-add-defines-for-RIOT-crypto-backend.patch new file mode 100644 index 0000000000000000000000000000000000000000..977b4f9894360d192f47248c5c9da6c163b78c0d GIT binary patch literal 1375 zcma)6VQ-r-6#VXA@w9zuN+8A|grI3kMw6ye+Ej&2sJUQ2zIT3i_sF8iAAybeM1*4+v4}gA4yh2M5$8-=)+A(%n-QB(IT3K3XW+{xFvmct zTi?JaHOT^LTv!xg#*>og2uzbKgb%TNtly?PZt(OYxdpcwWi@FP@*O+=WmG5He0OSwfr_aCfy1pxJ~dP8)gqu^kIzW zl(MLjJS)$%9xB83QrZ00dxVH2jzQNSk_y5>cTZ9GI!Q7ie}Qz&STJrFwuo(ET7V|m zmZe0VVkFotoMN3$LARVfYQpCPrc(m%llWk&IP>oAZ-ezFxVpKoUwdl1u@c@Lc{!+( z?724H*uFyy8lS{OLn@314lF7iwboT%BzdJuwL>YKI_M}1Jm^hBTiJD*aEv&ZJQ|Of zJq!(l^AR6A_M0ZRI8NH#Le<9Gwefb}X^d}1`Ngj5PxFgl2Ek6U?fVJZK5X49-_W_ zq?&p73nzLLGuDB(_EB3iXa0OO_k#7Ezga3S{KSrjaXI@k-^^B*>&