From c7b9657ff57f1fa2417e4ce14518ba912d287a83 Mon Sep 17 00:00:00 2001 From: Francisco Molina Date: Thu, 24 Feb 2022 13:19:42 +0100 Subject: [PATCH] pkg/libcose: add RIOT as crypto backend --- pkg/libcose/Kconfig | 5 +++ pkg/libcose/Makefile.dep | 5 ++- pkg/libcose/Makefile.include | 9 +++- pkg/libcose/contrib/libcose_riot_crypto.c | 54 +++++++++++++++++++++++ pkg/libcose/include/cose/crypto/riot.h | 8 ++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 pkg/libcose/contrib/libcose_riot_crypto.c diff --git a/pkg/libcose/Kconfig b/pkg/libcose/Kconfig index 944d64451d..734b448b4a 100644 --- a/pkg/libcose/Kconfig +++ b/pkg/libcose/Kconfig @@ -31,6 +31,11 @@ config MODULE_LIBCOSE_CRYPT_MONOCYPHER depends on TEST_KCONFIG depends on PACKAGE_MONOCYPHER +config MODULE_LIBCOSE_CRYPT_RIOT + bool "COSE use RIOT backend" + depends on TEST_KCONFIG + select MODULE_CRYPTO + config MODULE_LIBCOSE_CRYPT_INIT bool "LibCose Crypt Initialization functions" default y diff --git a/pkg/libcose/Makefile.dep b/pkg/libcose/Makefile.dep index 077f0f1fd7..a66b6d73b0 100644 --- a/pkg/libcose/Makefile.dep +++ b/pkg/libcose/Makefile.dep @@ -16,6 +16,9 @@ endif ifneq (,$(filter libcose_crypt_monocypher,$(USEMODULE))) USEPKG += monocypher endif +ifneq (,$(filter libcose_crypt_riot,$(USEMODULE))) + USEMODULE += crypto +endif -DEFAULT_MODULE += auto_init_libcose_crypt DEFAULT_MODULE += libcose_crypt_init +DEFAULT_MODULE += auto_init_libcose_crypt diff --git a/pkg/libcose/Makefile.include b/pkg/libcose/Makefile.include index eee721d553..ebe77d953c 100644 --- a/pkg/libcose/Makefile.include +++ b/pkg/libcose/Makefile.include @@ -1,4 +1,7 @@ -INCLUDES += -I$(PKGDIRBASE)/libcose/include +INCLUDES += -I$(PKGDIRBASE)/libcose/include \ + -I$(RIOTBASE)/pkg/libcose/include \ + # + CFLAGS += -DUSE_CBOR_CONTEXT ifneq (,$(filter libcose_crypt_hacl,$(USEMODULE))) @@ -13,6 +16,10 @@ endif ifneq (,$(filter libcose_crypt_monocypher,$(USEMODULE))) CFLAGS += -DCRYPTO_MONOCYPHER endif +ifneq (,$(filter libcose_crypt_riot,$(USEMODULE))) + CFLAGS += -DCRYPTO_RIOT + DIRS += $(RIOTBASE)/pkg/libcose/contrib +endif ifneq (,$(filter libcose_crypt_init,$(USEMODULE))) DIRS += $(RIOTBASE)/pkg/libcose/init endif diff --git a/pkg/libcose/contrib/libcose_riot_crypto.c b/pkg/libcose/contrib/libcose_riot_crypto.c new file mode 100644 index 0000000000..40a0c27624 --- /dev/null +++ b/pkg/libcose/contrib/libcose_riot_crypto.c @@ -0,0 +1,54 @@ +/* + * 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 for libcose implementation + * + * @author Francisco Molina + * + * @} + */ + +#include +#include + +#include "crypto/chacha20poly1305.h" + +#include "cose.h" +#include "cose/crypto.h" +#include "cose/crypto/selectors.h" + +int cose_crypto_aead_encrypt_chachapoly(uint8_t *c, size_t *clen, + const uint8_t *msg, size_t msglen, + const uint8_t *aad, size_t aadlen, + const uint8_t *npub, const uint8_t *k) +{ + if (*clen < msglen + CHACHA20POLY1305_TAG_BYTES) { + return COSE_ERR_INVALID_PARAM; + } + chacha20poly1305_encrypt(c, msg, msglen, aad, aadlen, k, npub); + *clen = msglen + CHACHA20POLY1305_TAG_BYTES; + return COSE_OK; +} + +int cose_crypto_aead_decrypt_chachapoly(uint8_t *msg, size_t *msglen, + const uint8_t *c, size_t clen, + const uint8_t *aad, size_t aadlen, + const uint8_t *npub, const uint8_t *k) +{ + if (chacha20poly1305_decrypt(c, clen, msg, msglen, aad, aadlen, k, npub) == 1) { + return COSE_OK; + } + else { + return COSE_ERR_CRYPTO; + } +} diff --git a/pkg/libcose/include/cose/crypto/riot.h b/pkg/libcose/include/cose/crypto/riot.h index cdab01926e..4c2bbc73d8 100644 --- a/pkg/libcose/include/cose/crypto/riot.h +++ b/pkg/libcose/include/cose/crypto/riot.h @@ -31,6 +31,14 @@ extern "C" { #define AUTO_INIT_PRIO_MOD_LIBCOSE 1050 #endif +/** + * @name list of provided algorithms + * + * @{ + */ +#define HAVE_ALGO_CHACHA20POLY1305 +/** @} */ + /** * @brief Initialize libCOSE RIOT crypto backend *