Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/libcose: add RIOT as crypto backend #17701

Merged
merged 3 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions makefiles/default_modules.deps.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pkg/libcose/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ 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

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
Expand Down
2 changes: 1 addition & 1 deletion pkg/libcose/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions pkg/libcose/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ endif
ifneq (,$(filter libcose_crypt_tinycrypt,$(USEMODULE)))
USEPKG += tinycrypt
endif
ifneq (,$(filter libcose_crypt_monocypher,$(USEMODULE)))
USEPKG += monocypher
endif
ifneq (,$(filter libcose_crypt_riot,$(USEMODULE)))
USEMODULE += crypto
endif

DEFAULT_MODULE += libcose_crypt_init
DEFAULT_MODULE += auto_init_libcose_crypt
21 changes: 19 additions & 2 deletions pkg/libcose/Makefile.include
Original file line number Diff line number Diff line change
@@ -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)))
Expand All @@ -10,6 +13,20 @@ 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_riot,$(USEMODULE)))
CFLAGS += -DCRYPTO_RIOT
DIRS += $(RIOTBASE)/pkg/libcose/contrib
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
2 changes: 2 additions & 0 deletions pkg/libcose/Makefile.libcose_crypt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
MODULE := libcose_crypt
SUBMODULES = 1

SRC += keygen_symm.c

include $(RIOTBASE)/Makefile.base
3 changes: 3 additions & 0 deletions pkg/libcose/contrib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE = libcose_crypt_riot

include $(RIOTBASE)/Makefile.base
54 changes: 54 additions & 0 deletions pkg/libcose/contrib/libcose_riot_crypto.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* @}
*/

#include <stdint.h>
#include <stdlib.h>

#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;
}
}
56 changes: 56 additions & 0 deletions pkg/libcose/include/cose/crypto/riot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 <[email protected]>
*/

#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

/**
* @name list of provided algorithms
*
* @{
*/
#define HAVE_ALGO_CHACHA20POLY1305
/** @} */

/**
* @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 */

/** @} */
3 changes: 3 additions & 0 deletions pkg/libcose/init/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MODULE = libcose_crypt_init

include $(RIOTBASE)/Makefile.base
48 changes: 48 additions & 0 deletions pkg/libcose/init/init.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* @}
*/


#include <stdint.h>
#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

This file was deleted.

Loading