From 69eacbddbe7fa5fc24e8400df44aaf538b1a6635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Th=C3=A9baudeau?= Date: Sat, 26 Dec 2015 00:42:39 +0100 Subject: [PATCH] cc2538: Add AES-CCM* driver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Benoît Thébaudeau --- cpu/cc2538/Makefile.cc2538 | 2 +- cpu/cc2538/dev/cc2538-ccm-star.c | 130 +++++++++++++++++++++++++++++++ cpu/cc2538/dev/cc2538-ccm-star.h | 55 +++++++++++++ cpu/cc2538/dev/ccm.c | 4 +- platform/cc2538dk/contiki-conf.h | 4 + platform/zoul/contiki-conf.h | 4 + 6 files changed, 196 insertions(+), 3 deletions(-) create mode 100644 cpu/cc2538/dev/cc2538-ccm-star.c create mode 100644 cpu/cc2538/dev/cc2538-ccm-star.h diff --git a/cpu/cc2538/Makefile.cc2538 b/cpu/cc2538/Makefile.cc2538 index fe9f3a1de..fe6c06263 100644 --- a/cpu/cc2538/Makefile.cc2538 +++ b/cpu/cc2538/Makefile.cc2538 @@ -54,7 +54,7 @@ CONTIKI_CPU_DIRS += ../cc253x/usb/common ../cc253x/usb/common/cdc-acm CONTIKI_CPU_SOURCEFILES += clock.c rtimer-arch.c uart.c watchdog.c CONTIKI_CPU_SOURCEFILES += nvic.c cpu.c sys-ctrl.c gpio.c ioc.c spi.c adc.c CONTIKI_CPU_SOURCEFILES += crypto.c aes.c ecb.c ccm.c sha256.c -CONTIKI_CPU_SOURCEFILES += cc2538-aes-128.c +CONTIKI_CPU_SOURCEFILES += cc2538-aes-128.c cc2538-ccm-star.c CONTIKI_CPU_SOURCEFILES += cc2538-rf.c udma.c lpm.c CONTIKI_CPU_SOURCEFILES += pka.c bignum-driver.c ecc-driver.c ecc-algorithm.c CONTIKI_CPU_SOURCEFILES += ecc-curve.c diff --git a/cpu/cc2538/dev/cc2538-ccm-star.c b/cpu/cc2538/dev/cc2538-ccm-star.c new file mode 100644 index 000000000..754b0548e --- /dev/null +++ b/cpu/cc2538/dev/cc2538-ccm-star.c @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2015, Benoît Thébaudeau + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup cc2538-ccm-star + * @{ + * + * \file + * Implementation of the AES-CCM* driver for the CC2538 SoC + */ +#include "contiki.h" +#include "dev/ccm.h" +#include "dev/cc2538-aes-128.h" +#include "dev/cc2538-ccm-star.h" + +#include +#include +/*---------------------------------------------------------------------------*/ +#define MODULE_NAME "cc2538-ccm-star" + +#define CCM_STAR_LEN_LEN (CCM_NONCE_LEN_LEN - CCM_STAR_NONCE_LENGTH) + +#define DEBUG 0 +#if DEBUG +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif +/*---------------------------------------------------------------------------*/ +static uint8_t +enable_crypto(void) +{ + uint8_t enabled = CRYPTO_IS_ENABLED(); + if(!enabled) { + crypto_enable(); + } + return enabled; +} +/*---------------------------------------------------------------------------*/ +static void +restore_crypto(uint8_t enabled) +{ + if(!enabled) { + crypto_disable(); + } +} +/*---------------------------------------------------------------------------*/ +static void +set_key(const uint8_t *key) +{ + cc2538_aes_128_driver.set_key(key); +} +/*---------------------------------------------------------------------------*/ +static void +aead(const uint8_t *nonce, uint8_t *m, uint8_t m_len, const uint8_t *a, + uint8_t a_len, uint8_t *result, uint8_t mic_len, int forward) +{ + uint16_t cdata_len; + uint8_t crypto_enabled, ret; + + crypto_enabled = enable_crypto(); + + if(forward) { + ret = ccm_auth_encrypt_start(CCM_STAR_LEN_LEN, CC2538_AES_128_KEY_AREA, + nonce, a, a_len, m, m_len, m, mic_len, NULL); + if(ret != CRYPTO_SUCCESS) { + PRINTF("%s: ccm_auth_encrypt_start() error %u\n", MODULE_NAME, ret); + restore_crypto(crypto_enabled); + return; + } + + while(!ccm_auth_encrypt_check_status()); + ret = ccm_auth_encrypt_get_result(result, mic_len); + if(ret != CRYPTO_SUCCESS) { + PRINTF("%s: ccm_auth_encrypt_get_result() error %u\n", MODULE_NAME, ret); + } + } else { + cdata_len = m_len + mic_len; + ret = ccm_auth_decrypt_start(CCM_STAR_LEN_LEN, CC2538_AES_128_KEY_AREA, + nonce, a, a_len, m, cdata_len, m, mic_len, + NULL); + if(ret != CRYPTO_SUCCESS) { + PRINTF("%s: ccm_auth_decrypt_start() error %u\n", MODULE_NAME, ret); + restore_crypto(crypto_enabled); + return; + } + + while(!ccm_auth_decrypt_check_status()); + ret = ccm_auth_decrypt_get_result(m, cdata_len, result, mic_len); + if(ret != CRYPTO_SUCCESS) { + PRINTF("%s: ccm_auth_decrypt_get_result() error %u\n", MODULE_NAME, ret); + } + } + + restore_crypto(crypto_enabled); +} +/*---------------------------------------------------------------------------*/ +const struct ccm_star_driver cc2538_ccm_star_driver = { + set_key, + aead +}; + +/** @} */ diff --git a/cpu/cc2538/dev/cc2538-ccm-star.h b/cpu/cc2538/dev/cc2538-ccm-star.h new file mode 100644 index 000000000..a608444ce --- /dev/null +++ b/cpu/cc2538/dev/cc2538-ccm-star.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2015, Benoît Thébaudeau + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** + * \addtogroup cc2538-aes + * @{ + * + * \defgroup cc2538-ccm-star CC2538 AES-CCM* + * + * AES-CCM* driver for the CC2538 SoC + * @{ + * + * \file + * Header file of the AES-CCM* driver for the CC2538 SoC + */ +#ifndef CC2538_CCM_STAR_H_ +#define CC2538_CCM_STAR_H_ + +#include "lib/ccm-star.h" +/*---------------------------------------------------------------------------*/ +extern const struct ccm_star_driver cc2538_ccm_star_driver; + +#endif /* CC2538_CCM_STAR_H_ */ + +/** + * @} + * @} + */ diff --git a/cpu/cc2538/dev/ccm.c b/cpu/cc2538/dev/ccm.c index c0f02ddc1..7559fbe76 100644 --- a/cpu/cc2538/dev/ccm.c +++ b/cpu/cc2538/dev/ccm.c @@ -96,14 +96,14 @@ ccm_auth_crypt_get_result(const void *cdata, uint16_t cdata_len, /* Check MIC */ data_len = cdata_len - mic_len; if(rom_util_memcmp(tag, &((const uint8_t *)cdata)[data_len], mic_len)) { - return AES_AUTHENTICATION_FAILED; + ret = AES_AUTHENTICATION_FAILED; } } /* Copy tag to MIC */ rom_util_memcpy(mic, tag, mic_len); - return CRYPTO_SUCCESS; + return ret; } /*---------------------------------------------------------------------------*/ uint8_t diff --git a/platform/cc2538dk/contiki-conf.h b/platform/cc2538dk/contiki-conf.h index 1158f0922..34a7f1673 100644 --- a/platform/cc2538dk/contiki-conf.h +++ b/platform/cc2538dk/contiki-conf.h @@ -512,6 +512,10 @@ typedef uint32_t rtimer_clock_t; #ifndef AES_128_CONF #define AES_128_CONF cc2538_aes_128_driver /**< AES-128 driver */ #endif + +#ifndef CCM_STAR_CONF +#define CCM_STAR_CONF cc2538_ccm_star_driver /**< AES-CCM* driver */ +#endif /** @} */ /*---------------------------------------------------------------------------*/ diff --git a/platform/zoul/contiki-conf.h b/platform/zoul/contiki-conf.h index 034c67157..cef99108c 100644 --- a/platform/zoul/contiki-conf.h +++ b/platform/zoul/contiki-conf.h @@ -580,6 +580,10 @@ typedef uint32_t rtimer_clock_t; #ifndef AES_128_CONF #define AES_128_CONF cc2538_aes_128_driver /**< AES-128 driver */ #endif + +#ifndef CCM_STAR_CONF +#define CCM_STAR_CONF cc2538_ccm_star_driver /**< AES-CCM* driver */ +#endif /** @} */ /*---------------------------------------------------------------------------*/