CC2538: Add AES-128 driver

This commit is contained in:
kkrentz 2015-10-21 01:18:04 -07:00 committed by Benoît Thébaudeau
parent 81765746ad
commit 24cb05059a
10 changed files with 240 additions and 2 deletions

View File

@ -54,6 +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-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

View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 2015, Hasso-Plattner-Institut.
* 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.
*
* This file is part of the Contiki operating system.
*/
/**
* \addtogroup cc2538-aes-128
* @{
*
* \file
* Implementation of the AES-128 driver for the CC2538 SoC
* \author
* Konrad Krentz <konrad.krentz@gmail.com>
*/
#include "contiki.h"
#include "dev/ecb.h"
#include "dev/cc2538-aes-128.h"
#include <stdint.h>
#include <stdio.h>
/*---------------------------------------------------------------------------*/
#define MODULE_NAME "cc2538-aes-128"
#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)
{
uint8_t crypto_enabled, ret;
crypto_enabled = enable_crypto();
ret = aes_load_keys(key, AES_KEY_STORE_SIZE_KEY_SIZE_128, 1,
CC2538_AES_128_KEY_AREA);
if(ret != CRYPTO_SUCCESS) {
PRINTF("%s: aes_load_keys() error %u\n", MODULE_NAME, ret);
}
restore_crypto(crypto_enabled);
}
/*---------------------------------------------------------------------------*/
static void
encrypt(uint8_t *plaintext_and_result)
{
uint8_t crypto_enabled, ret;
int8_t res;
crypto_enabled = enable_crypto();
ret = ecb_crypt_start(true, CC2538_AES_128_KEY_AREA, plaintext_and_result,
plaintext_and_result, AES_128_BLOCK_SIZE, NULL);
if(ret != CRYPTO_SUCCESS) {
PRINTF("%s: ecb_crypt_start() error %u\n", MODULE_NAME, ret);
restore_crypto(crypto_enabled);
return;
}
while((res = ecb_crypt_check_status()) == CRYPTO_PENDING);
if(res != CRYPTO_SUCCESS) {
PRINTF("%s: ecb_crypt_check_status() error %d\n", MODULE_NAME, res);
}
restore_crypto(crypto_enabled);
}
/*---------------------------------------------------------------------------*/
const struct aes_128_driver cc2538_aes_128_driver = {
set_key,
encrypt
};
/** @} */

View File

@ -0,0 +1,63 @@
/*
* Copyright (c) 2015, Hasso-Plattner-Institut.
* 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.
*
* This file is part of the Contiki operating system.
*/
/**
* \addtogroup cc2538-aes
* @{
*
* \defgroup cc2538-aes-128 CC2538 AES-128
*
* AES-128 driver for the CC2538 SoC
* @{
*
* \file
* Header file of the AES-128 driver for the CC2538 SoC
* \author
* Konrad Krentz <konrad.krentz@gmail.com>
*/
#ifndef CC2538_AES_128_H_
#define CC2538_AES_128_H_
#include "lib/aes-128.h"
/*---------------------------------------------------------------------------*/
#ifdef CC2538_AES_128_CONF_KEY_AREA
#define CC2538_AES_128_KEY_AREA CC2538_AES_128_CONF_KEY_AREA
#else
#define CC2538_AES_128_KEY_AREA 0
#endif
/*---------------------------------------------------------------------------*/
extern const struct aes_128_driver cc2538_aes_128_driver;
#endif /* CC2538_AES_128_H_ */
/**
* @}
* @}
*/

View File

@ -44,6 +44,8 @@
#define CRYPTO_H_
#include "contiki.h"
#include "dev/sys-ctrl.h"
#include "reg.h"
/*---------------------------------------------------------------------------*/
/** \name Crypto drivers return codes
* @{
@ -54,6 +56,18 @@
#define CRYPTO_NULL_ERROR 2
#define CRYPTO_RESOURCE_IN_USE 3
#define CRYPTO_DMA_BUS_ERROR 4
/** @} */
/*---------------------------------------------------------------------------*/
/** \name Crypto macros
* @{
*/
/** \brief Indicates whether the AES/SHA cryptoprocessor is enabled
* \return Boolean value indicating whether the AES/SHA cryptoprocessor is
* enabled
*/
#define CRYPTO_IS_ENABLED() (!!(REG(SYS_CTRL_RCGCSEC) & SYS_CTRL_RCGCSEC_AES))
/** @} */
/*---------------------------------------------------------------------------*/
/** \name Crypto functions

View File

@ -26,7 +26,7 @@ include $(CONTIKI_CPU)/Makefile.cc2538
MODULES += core/net core/net/mac \
core/net/mac/contikimac \
core/net/llsec
core/net/llsec core/net/llsec/noncoresec
PYTHON = python
BSL_FLAGS += -e -w -v

View File

@ -500,6 +500,20 @@ typedef uint32_t rtimer_clock_t;
#endif /* NETSTACK_CONF_WITH_IPV6 */
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Security
*
* @{
*/
#ifndef CRYPTO_CONF_INIT
#define CRYPTO_CONF_INIT 1 /**< Whether to init cryptoprocessor */
#endif
#ifndef AES_128_CONF
#define AES_128_CONF cc2538_aes_128_driver /**< AES-128 driver */
#endif
/** @} */
/*---------------------------------------------------------------------------*/
#endif /* CONTIKI_CONF_H_ */

View File

@ -56,6 +56,7 @@
#include "dev/slip.h"
#include "dev/cc2538-rf.h"
#include "dev/udma.h"
#include "dev/crypto.h"
#include "usb/usb-serial.h"
#include "lib/random.h"
#include "net/netstack.h"
@ -201,6 +202,12 @@ main(void)
ctimer_init();
set_rf_params();
#if CRYPTO_CONF_INIT
crypto_init();
crypto_disable();
#endif
netstack_init();
#if NETSTACK_CONF_WITH_IPV6

View File

@ -45,7 +45,7 @@ include $(CONTIKI_CPU)/Makefile.cc2538
MODULES += core/net core/net/mac \
core/net/mac/contikimac \
core/net/llsec dev/cc1200
core/net/llsec core/net/llsec/noncoresec dev/cc1200
BSL = $(CONTIKI)/tools/cc2538-bsl/cc2538-bsl.py

View File

@ -568,6 +568,20 @@ typedef uint32_t rtimer_clock_t;
#endif /* NETSTACK_CONF_WITH_IPV6 */
/** @} */
/*---------------------------------------------------------------------------*/
/**
* \name Security
*
* @{
*/
#ifndef CRYPTO_CONF_INIT
#define CRYPTO_CONF_INIT 1 /**< Whether to init cryptoprocessor */
#endif
#ifndef AES_128_CONF
#define AES_128_CONF cc2538_aes_128_driver /**< AES-128 driver */
#endif
/** @} */
/*---------------------------------------------------------------------------*/
#endif /* CONTIKI_CONF_H_ */

View File

@ -57,6 +57,7 @@
#include "dev/slip.h"
#include "dev/cc2538-rf.h"
#include "dev/udma.h"
#include "dev/crypto.h"
#include "usb/usb-serial.h"
#include "lib/random.h"
#include "net/netstack.h"
@ -195,6 +196,11 @@ main(void)
board_init();
#if CRYPTO_CONF_INIT
crypto_init();
crypto_disable();
#endif
netstack_init();
set_rf_params();