Add AES-128 driver for CC26x0/CC13x0

This commit is contained in:
Atis Elsts 2019-01-09 16:53:26 +02:00
parent 4928a5e1e0
commit 0b31b090aa
5 changed files with 221 additions and 0 deletions

View File

@ -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 += 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 += random.c soc-trng.c int-master.c
CONTIKI_CPU_SOURCEFILES += spi-arch.c CONTIKI_CPU_SOURCEFILES += spi-arch.c
CONTIKI_CPU_SOURCEFILES += cc26xx-aes.c
CONTIKI_SOURCEFILES += $(CONTIKI_CPU_SOURCEFILES) CONTIKI_SOURCEFILES += $(CONTIKI_CPU_SOURCEFILES)

View File

@ -102,6 +102,11 @@
#define NETSTACK_RADIO_MAX_PAYLOAD_LEN 125 #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 */
/** @} */ /** @} */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/** /**

View File

@ -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 <atis.elsts@gmail.com>
*/
#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
};
/** @} */

View File

@ -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 <atis.elsts@gmail.com>
*/
#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_ */
/**
* @}
* @}
*/

View File

@ -572,6 +572,14 @@
#define ti_lib_watchdog_stall_enable(...) WatchdogStallEnable(__VA_ARGS__) #define ti_lib_watchdog_stall_enable(...) WatchdogStallEnable(__VA_ARGS__)
#define ti_lib_watchdog_stall_disable(...) WatchdogStallDisable(__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_ */ #endif /* TI_LIB_H_ */
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/** /**