From c01d3d7d227845721d66b9ec157acf68a41bcf1b Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Tue, 18 Dec 2018 17:14:18 +0000 Subject: [PATCH 1/2] Do not allow to 'trick' the platform and board exclude checks --- Makefile.include | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile.include b/Makefile.include index 2e5a31d1a..930378c01 100644 --- a/Makefile.include +++ b/Makefile.include @@ -478,6 +478,8 @@ ifeq ($(PLATFORM_ACTION),skip) # Skip this target. $(CONTIKI_PROJECT): @echo "Skipping $@: not for the '$(TARGET)/$(BOARD)' platform!" +%.$(TARGET): + @echo "Skipping $@: not for the '$(TARGET)/$(BOARD)' platform!" else # Build this target. # Match-anything pattern rule to allow the project makefiles to From 0b31b090aa425c9b5d4967f0a3b1c8fbe32c35e2 Mon Sep 17 00:00:00 2001 From: Atis Elsts Date: Wed, 9 Jan 2019 16:53:26 +0200 Subject: [PATCH 2/2] Add AES-128 driver for CC26x0/CC13x0 --- arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 | 1 + arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h | 5 + arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c | 127 ++++++++++++++++++ arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h | 80 +++++++++++ arch/cpu/cc26x0-cc13x0/ti-lib.h | 8 ++ 5 files changed, 221 insertions(+) create mode 100755 arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c create mode 100755 arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h diff --git a/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 index c0fb8eeed..d3900b9fb 100644 --- a/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 +++ b/arch/cpu/cc26x0-cc13x0/Makefile.cc26x0-cc13x0 @@ -44,6 +44,7 @@ CONTIKI_CPU_SOURCEFILES += rf-core.c rf-ble.c ieee-mode.c CONTIKI_CPU_SOURCEFILES += ble-cc2650.c ble-hal-cc26xx.c ble-addr.c rf-ble-cmd.c CONTIKI_CPU_SOURCEFILES += random.c soc-trng.c int-master.c CONTIKI_CPU_SOURCEFILES += spi-arch.c +CONTIKI_CPU_SOURCEFILES += cc26xx-aes.c CONTIKI_SOURCEFILES += $(CONTIKI_CPU_SOURCEFILES) diff --git a/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h index 7400c5ad9..0b553fb90 100644 --- a/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h +++ b/arch/cpu/cc26x0-cc13x0/cc13xx-cc26xx-conf.h @@ -102,6 +102,11 @@ #define NETSTACK_RADIO_MAX_PAYLOAD_LEN 125 +/* Platform-specific (H/W) AES implementation */ +#ifndef AES_128_CONF +#define AES_128_CONF cc26xx_aes_128_driver +#endif /* AES_128_CONF */ + /** @} */ /*---------------------------------------------------------------------------*/ /** diff --git a/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c new file mode 100755 index 000000000..cacdfc61f --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * 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 Institute 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 INSTITUTE 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 INSTITUTE 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 cc26xx-aes + * @{ + * + * \file + * Implementation of the AES driver for the CC26x0/CC13x0 SoC + * \author + * Atis Elsts + */ +#include "contiki.h" +#include "dev/cc26xx-aes.h" +#include "ti-lib.h" +/*---------------------------------------------------------------------------*/ +#include "sys/log.h" +#define LOG_MODULE "cc26xx-aes" +#define LOG_LEVEL LOG_LEVEL_MAIN +/*---------------------------------------------------------------------------*/ +static uint32_t skey[AES_128_KEY_LENGTH / sizeof(uint32_t)]; + +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_set_key(const uint8_t *key) +{ + memcpy(skey, key, AES_128_KEY_LENGTH); +} +/*---------------------------------------------------------------------------*/ +static void +encrypt_decrypt(uint8_t *plaintext_and_result, bool do_encrypt) +{ + uint32_t result[AES_128_BLOCK_SIZE / sizeof(uint32_t)]; + unsigned status; + int i; + + /* First, make sure the PERIPH PD is on */ + ti_lib_prcm_power_domain_on(PRCM_DOMAIN_PERIPH); + while((ti_lib_prcm_power_domain_status(PRCM_DOMAIN_PERIPH) + != PRCM_DOMAIN_POWER_ON)); + + /* Enable CRYPTO peripheral */ + ti_lib_prcm_peripheral_run_enable(PRCM_PERIPH_CRYPTO); + ti_lib_prcm_load_set(); + while(!ti_lib_prcm_load_get()); + + status = ti_lib_crypto_aes_load_key(skey, CRYPTO_KEY_AREA_0); + if(status != AES_SUCCESS) { + LOG_WARN("load key failed: %u\n", status); + } else { + + status = ti_lib_crypto_aes_ecb((uint32_t *)plaintext_and_result, result, CRYPTO_KEY_AREA_0, do_encrypt, false); + if(status != AES_SUCCESS) { + LOG_WARN("ecb failed: %u\n", status); + } else { + + for(i = 0; i < 100; ++i) { + ti_lib_cpu_delay(10); + status = ti_lib_crypto_aes_ecb_status(); + if(status != AES_DMA_BSY) { + break; + } + } + + ti_lib_crypto_aes_ecb_finish(); + + if(status != AES_SUCCESS) { + LOG_WARN("ecb get result failed: %u\n", status); + } + } + } + + ti_lib_prcm_peripheral_run_disable(PRCM_PERIPH_CRYPTO); + ti_lib_prcm_load_set(); + while(!ti_lib_prcm_load_get()); + + if(status == AES_SUCCESS) { + memcpy(plaintext_and_result, result, AES_128_BLOCK_SIZE); + } else { + /* corrupt the result */ + plaintext_and_result[0] ^= 1; + } +} +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_encrypt(uint8_t *plaintext_and_result) +{ + encrypt_decrypt(plaintext_and_result, true); +} +/*---------------------------------------------------------------------------*/ +void +cc26xx_aes_decrypt(uint8_t *cyphertext_and_result) +{ + encrypt_decrypt(cyphertext_and_result, false); +} +/*---------------------------------------------------------------------------*/ +const struct aes_128_driver cc26xx_aes_128_driver = { + cc26xx_aes_set_key, + cc26xx_aes_encrypt +}; + +/** @} */ diff --git a/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h new file mode 100755 index 000000000..a979cc562 --- /dev/null +++ b/arch/cpu/cc26x0-cc13x0/dev/cc26xx-aes.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016, University of Bristol - http://www.bristol.ac.uk + * 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 Institute 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 INSTITUTE 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 INSTITUTE 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 cc26xx + * @{ + * + * \defgroup cc26xx-aes CC26x0/CC13x0 AES-128 + * + * AES-128 driver for the CC26x0/CC13x0 SoC + * @{ + * + * \file + * Header file of the AES-128 driver for the CC26xx SoC + * \author + * Atis Elsts + */ +#ifndef CC2538_AES_H_ +#define CC2538_AES_H_ + +#include "lib/aes-128.h" + +/** + * \brief Set a key to use in subsequent encryption & decryption operations. + * \param key The key to use + * + * The size of the key must be AES_128_KEY_LENGTH. + */ +void cc26xx_aes_set_key(const uint8_t *key); + +/** + * \brief Encrypt a message using the SoC AES-128 hardware implementation + * \param plaintext_and_result In: message to encrypt, out: the encrypted message. + * + * The size of the message must be AES_128_BLOCK_SIZE. + * The key to use in the encryption must be set before calling this function. + */ +void cc26xx_aes_encrypt(uint8_t *plaintext_and_result); + +/** + * \brief Decrypt a message using the SoC AES-128 hardware implementation + * \param cyphertext_and_result In: message to decrypt, out: the decrypted message. + * + * The size of the message must be AES_128_BLOCK_SIZE. + * The key to use in the decryption must be set before calling this function. + */ +void cc26xx_aes_decrypt(uint8_t *cyphertext_and_result); + +extern const struct aes_128_driver cc26xx_aes_128_driver; + +#endif /* CC2538_AES_H_ */ +/** + * @} + * @} + */ diff --git a/arch/cpu/cc26x0-cc13x0/ti-lib.h b/arch/cpu/cc26x0-cc13x0/ti-lib.h index d6c7363ee..0d8400813 100644 --- a/arch/cpu/cc26x0-cc13x0/ti-lib.h +++ b/arch/cpu/cc26x0-cc13x0/ti-lib.h @@ -572,6 +572,14 @@ #define ti_lib_watchdog_stall_enable(...) WatchdogStallEnable(__VA_ARGS__) #define ti_lib_watchdog_stall_disable(...) WatchdogStallDisable(__VA_ARGS__) /*---------------------------------------------------------------------------*/ +/* crypto.h */ +#include "driverlib/crypto.h" + +#define ti_lib_crypto_aes_load_key(...) CRYPTOAesLoadKey(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb(...) CRYPTOAesEcb(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb_status(...) CRYPTOAesEcbStatus(__VA_ARGS__) +#define ti_lib_crypto_aes_ecb_finish(...) CRYPTOAesEcbFinish(__VA_ARGS__) +/*---------------------------------------------------------------------------*/ #endif /* TI_LIB_H_ */ /*---------------------------------------------------------------------------*/ /**