Skip to content

Commit

Permalink
pkg/libcose: add RIOT as crypto backend
Browse files Browse the repository at this point in the history
  • Loading branch information
fjmolinas committed May 17, 2022
1 parent 8b6ddca commit c7b9657
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/libcose/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/libcose/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion 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 @@ -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
Expand Down
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 <francois-xavier.molina@inria.fr>
*
* @}
*/

#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;
}
}
8 changes: 8 additions & 0 deletions pkg/libcose/include/cose/crypto/riot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit c7b9657

Please sign in to comment.