CC2420: Turned SPI macros to functions and removed redundant AES API

This commit is contained in:
kkrentz 2014-02-09 14:16:48 -08:00
parent 6486c51692
commit c6a98f2968
6 changed files with 102 additions and 325 deletions

View file

@ -1,104 +0,0 @@
/*
* Copyright (c) 2008, Swedish Institute of Computer Science.
* 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.
*
*/
/**
* \file
* AES encryption functions.
* \author
* Adam Dunkels <adam@sics.se>
*/
#include "contiki.h"
#include "cc2420.h"
#include "cc2420-aes.h"
#include "dev/spi.h"
#define KEYLEN 16
#define MAX_DATALEN 16
#define MIN(a,b) ((a) < (b)? (a): (b))
/*---------------------------------------------------------------------------*/
void
cc2420_aes_set_key(const uint8_t *key, int index)
{
switch(index) {
case 0:
CC2420_WRITE_RAM(key, CC2420RAM_KEY0, KEYLEN, CC2420_WRITE_RAM_REVERSE);
break;
case 1:
CC2420_WRITE_RAM(key, CC2420RAM_KEY1, KEYLEN, CC2420_WRITE_RAM_REVERSE);
break;
}
}
/*---------------------------------------------------------------------------*/
/* Encrypt at most 16 bytes of data. */
static void
cipher16(uint8_t *data, int len)
{
uint8_t status;
len = MIN(len, MAX_DATALEN);
CC2420_WRITE_RAM(data, CC2420RAM_SABUF, len, CC2420_WRITE_RAM_IN_ORDER);
CC2420_STROBE(CC2420_SAES);
/* Wait for the encryption to finish */
do {
CC2420_GET_STATUS(status);
} while(status & BV(CC2420_ENC_BUSY));
CC2420_READ_RAM(data, CC2420RAM_SABUF, len);
}
/*---------------------------------------------------------------------------*/
void
cc2420_aes_cipher(uint8_t *data, int len, int key_index)
{
int i;
uint16_t secctrl0;
CC2420_READ_REG(CC2420_SECCTRL0, secctrl0);
secctrl0 &= ~(CC2420_SECCTRL0_SAKEYSEL0 | CC2420_SECCTRL0_SAKEYSEL1);
switch(key_index) {
case 0:
secctrl0 |= CC2420_SECCTRL0_SAKEYSEL0;
break;
case 1:
secctrl0 |= CC2420_SECCTRL0_SAKEYSEL1;
break;
}
CC2420_WRITE_REG(CC2420_SECCTRL0, secctrl0);
for(i = 0; i < len; i = i + MAX_DATALEN) {
cipher16(data + i, MIN(len - i, MAX_DATALEN));
}
}
/*---------------------------------------------------------------------------*/

View file

@ -1,75 +0,0 @@
/*
* Copyright (c) 2008, Swedish Institute of Computer Science.
* 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.
*
*/
/**
* \file
* Interface to the CC2420 AES encryption/decryption functions
* \author
* Adam Dunkels <adam@sics.se>
*/
#ifndef CC2420_AES_H_
#define CC2420_AES_H_
/**
* \brief Setup an AES key
* \param key A pointer to a 16-byte AES key
* \param index The key index: either 0 or 1.
*
* This function sets up an AES key with the CC2420
* chip. The AES key can later be used with the
* cc2420_aes_cipher() function to encrypt or decrypt
* data.
*
* The CC2420 can store two separate keys in its
* memory. The keys are indexed as 0 or 1 and the key
* index is given by the 'index' parameter.
*
*/
void cc2420_aes_set_key(const uint8_t *key, int index);
/**
* \brief Encrypt/decrypt data with AES
* \param data A pointer to the data to be encrypted/decrypted
* \param len The length of the data to be encrypted/decrypted
* \param key_index The key to use. The key must have previously been set up with cc2420_aes_set_key().
*
* This function encrypts/decrypts data with AES. A
* pointer to the data is passed as a parameter, and the
* function overwrites the data with the encrypted data.
*
*/
void cc2420_aes_cipher(uint8_t *data, int len, int key_index);
#endif /* CC2420_AES_H_ */

View file

@ -72,6 +72,13 @@
/* The RSSI_OFFSET is approximate -45 (see CC2420 specification) */ /* The RSSI_OFFSET is approximate -45 (see CC2420 specification) */
#define RSSI_OFFSET -45 #define RSSI_OFFSET -45
enum write_ram_order {
/* Begin with writing the first given byte */
WRITE_RAM_IN_ORDER,
/* Begin with writing the last given byte */
WRITE_RAM_REVERSE
};
#define DEBUG 0 #define DEBUG 0
#if DEBUG #if DEBUG
#include <stdio.h> #include <stdio.h>
@ -266,59 +273,133 @@ const struct radio_driver cc2420_driver =
}; };
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Sends a strobe */
static void static void
strobe(enum cc2420_register regname) strobe(enum cc2420_register regname)
{ {
CC2420_STROBE(regname); CC2420_SPI_ENABLE();
SPI_WRITE(regname);
CC2420_SPI_DISABLE();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Reads a register */
static uint16_t static uint16_t
getreg(enum cc2420_register regname) getreg(enum cc2420_register regname)
{ {
uint16_t reg; uint16_t value;
CC2420_READ_REG(regname, reg);
return reg; CC2420_SPI_ENABLE();
SPI_WRITE(regname | 0x40);
value = (uint8_t)SPI_RXBUF;
SPI_TXBUF = 0;
SPI_WAITFOREORx();
value = SPI_RXBUF << 8;
SPI_TXBUF = 0;
SPI_WAITFOREORx();
value |= SPI_RXBUF;
CC2420_SPI_DISABLE();
return value;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/**
* Writes to a register.
* Note: the SPI_WRITE(0) seems to be needed for getting the
* write reg working on the Z1 / MSP430X platform
*/
static void static void
setreg(enum cc2420_register regname, uint16_t value) setreg(enum cc2420_register regname, uint16_t value)
{ {
CC2420_WRITE_REG(regname, value); CC2420_SPI_ENABLE();
SPI_WRITE_FAST(regname);
SPI_WRITE_FAST((uint8_t) (value >> 8));
SPI_WRITE_FAST((uint8_t) (value & 0xff));
SPI_WAITFORTx_ENDED();
SPI_WRITE(0);
CC2420_SPI_DISABLE();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
read_ram(uint8_t *buffer, uint16_t adr, uint16_t count) read_ram(uint8_t *buffer, uint16_t adr, uint16_t count)
{ {
CC2420_READ_RAM(buffer, adr, count); uint8_t i;
CC2420_SPI_ENABLE();
SPI_WRITE(0x80 | ((adr) & 0x7f));
SPI_WRITE((((adr) >> 1) & 0xc0) | 0x20);
SPI_RXBUF;
for(i = 0; i < count; i++) {
SPI_READ(((uint8_t*) buffer)[i]);
}
CC2420_SPI_DISABLE();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Write to RAM in the CC2420 */
static void static void
write_ram(const uint8_t *buffer, write_ram(const uint8_t *buffer,
uint16_t adr, uint16_t adr,
uint16_t count, uint16_t count,
enum cc2420_write_ram_order order) enum write_ram_order order)
{ {
CC2420_WRITE_RAM(buffer, adr, count, order); uint8_t i;
CC2420_SPI_ENABLE();
SPI_WRITE_FAST(0x80 | (adr & 0x7f));
SPI_WRITE_FAST((adr >> 1) & 0xc0);
if(order == WRITE_RAM_IN_ORDER) {
for(i = 0; i < count; i++) {
SPI_WRITE_FAST((buffer)[i]);
}
} else {
for(i = count; i > 0; i--) {
SPI_WRITE_FAST((buffer)[i - 1]);
}
}
SPI_WAITFORTx_ENDED();
CC2420_SPI_DISABLE();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
write_fifo_buf(const uint8_t *buffer, uint16_t count) write_fifo_buf(const uint8_t *buffer, uint16_t count)
{ {
CC2420_WRITE_FIFO_BUF(buffer, count); uint8_t i;
CC2420_SPI_ENABLE();
SPI_WRITE_FAST(CC2420_TXFIFO);
for(i = 0; i < count; i++) {
SPI_WRITE_FAST((buffer)[i]);
}
SPI_WAITFORTx_ENDED();
CC2420_SPI_DISABLE();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
/* Returns the current status */
static uint8_t static uint8_t
get_status(void) get_status(void)
{ {
uint8_t status; uint8_t status;
CC2420_GET_STATUS(status);
CC2420_SPI_ENABLE();
SPI_WRITE(CC2420_SNOP);
status = SPI_RXBUF;
CC2420_SPI_DISABLE();
return status; return status;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
getrxdata(void *buf, int len) getrxdata(uint8_t *buffer, int count)
{ {
CC2420_READ_FIFO_BUF(buf, len); uint8_t i;
CC2420_SPI_ENABLE();
SPI_WRITE(CC2420_RXFIFO | 0x40);
(void) SPI_RXBUF;
for(i = 0; i < count; i++) {
SPI_READ(buffer[i]);
}
clock_delay(1);
CC2420_SPI_DISABLE();
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
@ -363,7 +444,7 @@ init_security(void)
static void static void
set_key(uint8_t *key) set_key(uint8_t *key)
{ {
write_ram(key, CC2420RAM_KEY0, 16, CC2420_WRITE_RAM_REVERSE); write_ram(key, CC2420RAM_KEY0, 16, WRITE_RAM_REVERSE);
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static void
@ -372,7 +453,7 @@ encrypt(uint8_t *plaintext_and_result)
write_ram(plaintext_and_result, write_ram(plaintext_and_result,
CC2420RAM_SABUF, CC2420RAM_SABUF,
16, 16,
CC2420_WRITE_RAM_IN_ORDER); WRITE_RAM_IN_ORDER);
strobe(CC2420_SAES); strobe(CC2420_SAES);
while(get_status() & BV(CC2420_ENC_BUSY)); while(get_status() & BV(CC2420_ENC_BUSY));
@ -561,7 +642,7 @@ cc2420_transmit(unsigned short payload_len)
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP) { PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP) {
/* Write timestamp to last two bytes of packet in TXFIFO. */ /* Write timestamp to last two bytes of packet in TXFIFO. */
write_ram((uint8_t *) &sfd_timestamp, CC2420RAM_TXFIFO + payload_len - 1, 2, CC2420_WRITE_RAM_IN_ORDER); write_ram((uint8_t *) &sfd_timestamp, CC2420RAM_TXFIFO + payload_len - 1, 2, WRITE_RAM_IN_ORDER);
} }
} }
@ -737,11 +818,11 @@ cc2420_set_pan_addr(unsigned pan,
{ {
GET_LOCK(); GET_LOCK();
write_ram((uint8_t *) &pan, CC2420RAM_PANID, 2, CC2420_WRITE_RAM_IN_ORDER); write_ram((uint8_t *) &pan, CC2420RAM_PANID, 2, WRITE_RAM_IN_ORDER);
write_ram((uint8_t *) &addr, CC2420RAM_SHORTADDR, 2, CC2420_WRITE_RAM_IN_ORDER); write_ram((uint8_t *) &addr, CC2420RAM_SHORTADDR, 2, WRITE_RAM_IN_ORDER);
if(ieee_addr != NULL) { if(ieee_addr != NULL) {
write_ram(ieee_addr, CC2420RAM_IEEEADDR, 8, CC2420_WRITE_RAM_REVERSE); write_ram(ieee_addr, CC2420RAM_IEEEADDR, 8, WRITE_RAM_REVERSE);
} }
RELEASE_LOCK(); RELEASE_LOCK();
} }
@ -805,7 +886,7 @@ cc2420_read(void *buf, unsigned short bufsize)
} else if(len - FOOTER_LEN > bufsize) { } else if(len - FOOTER_LEN > bufsize) {
RIMESTATS_ADD(toolong); RIMESTATS_ADD(toolong);
} else { } else {
getrxdata(buf, len - FOOTER_LEN); getrxdata((uint8_t *) buf, len - FOOTER_LEN);
getrxdata(footer, FOOTER_LEN); getrxdata(footer, FOOTER_LEN);
if(footer[1] & FOOTER1_CRC_OK) { if(footer[1] & FOOTER1_CRC_OK) {

View file

@ -92,129 +92,4 @@ void cc2420_set_cca_threshold(int value);
extern const struct aes_128_driver cc2420_aes_128_driver; extern const struct aes_128_driver cc2420_aes_128_driver;
/************************************************************************/
/* Additional SPI Macros for the CC2420 */
/************************************************************************/
/* Send a strobe to the CC2420 */
#define CC2420_STROBE(s) \
do { \
CC2420_SPI_ENABLE(); \
SPI_WRITE(s); \
CC2420_SPI_DISABLE(); \
} while (0)
/* Write to a register in the CC2420 */
/* Note: the SPI_WRITE(0) seems to be needed for getting the */
/* write reg working on the Z1 / MSP430X platform */
#define CC2420_WRITE_REG(adr,data) \
do { \
CC2420_SPI_ENABLE(); \
SPI_WRITE_FAST(adr); \
SPI_WRITE_FAST((uint8_t)((data) >> 8)); \
SPI_WRITE_FAST((uint8_t)(data & 0xff)); \
SPI_WAITFORTx_ENDED(); \
SPI_WRITE(0); \
CC2420_SPI_DISABLE(); \
} while(0)
/* Read a register in the CC2420 */
#define CC2420_READ_REG(adr,data) \
do { \
CC2420_SPI_ENABLE(); \
SPI_WRITE(adr | 0x40); \
data = (uint8_t)SPI_RXBUF; \
SPI_TXBUF = 0; \
SPI_WAITFOREORx(); \
data = SPI_RXBUF << 8; \
SPI_TXBUF = 0; \
SPI_WAITFOREORx(); \
data |= SPI_RXBUF; \
CC2420_SPI_DISABLE(); \
} while(0)
#define CC2420_READ_FIFO_BYTE(data) \
do { \
CC2420_SPI_ENABLE(); \
SPI_WRITE(CC2420_RXFIFO | 0x40); \
(void)SPI_RXBUF; \
SPI_READ(*data); \
clock_delay(1); \
CC2420_SPI_DISABLE(); \
} while(0)
#define CC2420_READ_FIFO_BUF(buffer,count) \
do { \
uint8_t i; \
CC2420_SPI_ENABLE(); \
SPI_WRITE(CC2420_RXFIFO | 0x40); \
(void)SPI_RXBUF; \
for(i = 0; i < (count); i++) { \
SPI_READ(((uint8_t *)(buffer))[i]); \
} \
clock_delay(1); \
CC2420_SPI_DISABLE(); \
} while(0)
#define CC2420_WRITE_FIFO_BUF(buffer,count) \
do { \
uint8_t i; \
CC2420_SPI_ENABLE(); \
SPI_WRITE_FAST(CC2420_TXFIFO); \
for(i = 0; i < (count); i++) { \
SPI_WRITE_FAST(((uint8_t *)(buffer))[i]); \
} \
SPI_WAITFORTx_ENDED(); \
CC2420_SPI_DISABLE(); \
} while(0)
enum cc2420_write_ram_order {
/* Begin with writing the first given byte */
CC2420_WRITE_RAM_IN_ORDER,
/* Begin with writing the last given byte */
CC2420_WRITE_RAM_REVERSE
};
/* Write to RAM in the CC2420 */
#define CC2420_WRITE_RAM(buffer,adr,count,order) \
do { \
uint8_t i; \
CC2420_SPI_ENABLE(); \
SPI_WRITE_FAST(0x80 | ((adr) & 0x7f)); \
SPI_WRITE_FAST(((adr) >> 1) & 0xc0); \
if(order == CC2420_WRITE_RAM_IN_ORDER) { \
for(i = 0; i < (count); i++) { \
SPI_WRITE_FAST(((uint8_t*)(buffer))[i]); \
} \
} else { \
for(i = (count); i > 0; i--) { \
SPI_WRITE_FAST(((uint8_t*)(buffer))[i - 1]); \
} \
} \
SPI_WAITFORTx_ENDED(); \
CC2420_SPI_DISABLE(); \
} while(0)
/* Read from RAM in the CC2420 */
#define CC2420_READ_RAM(buffer,adr,count) \
do { \
uint8_t i; \
CC2420_SPI_ENABLE(); \
SPI_WRITE(0x80 | ((adr) & 0x7f)); \
SPI_WRITE((((adr) >> 1) & 0xc0) | 0x20); \
SPI_RXBUF; \
for(i = 0; i < (count); i++) { \
SPI_READ(((uint8_t*)(buffer))[i]); \
} \
CC2420_SPI_DISABLE(); \
} while(0)
/* Read status of the CC2420 */
#define CC2420_GET_STATUS(s) \
do { \
CC2420_SPI_ENABLE(); \
SPI_WRITE(CC2420_SNOP); \
s = SPI_RXBUF; \
CC2420_SPI_DISABLE(); \
} while (0)
#endif /* CC2420_H_ */ #endif /* CC2420_H_ */

View file

@ -1,7 +1,7 @@
# $Id: Makefile.common,v 1.3 2010/08/24 16:24:11 joxe Exp $ # $Id: Makefile.common,v 1.3 2010/08/24 16:24:11 joxe Exp $
ARCH=spi.c ds2411.c xmem.c i2c.c node-id.c sensors.c cfs-coffee.c \ ARCH=spi.c ds2411.c xmem.c i2c.c node-id.c sensors.c cfs-coffee.c \
cc2420.c cc2420-aes.c cc2420-arch.c cc2420-arch-sfd.c \ cc2420.c cc2420-arch.c cc2420-arch-sfd.c \
sky-sensors.c uip-ipchksum.c \ sky-sensors.c uip-ipchksum.c \
uart1.c slip_uart1.c uart1-putchar.c uart1.c slip_uart1.c uart1-putchar.c

View file

@ -13,7 +13,7 @@ endif
CLEAN += symbols.c symbols.h CLEAN += symbols.c symbols.h
ARCH=msp430.c leds.c watchdog.c xmem.c \ ARCH=msp430.c leds.c watchdog.c xmem.c \
spi.c cc2420.c cc2420-aes.c cc2420-arch.c cc2420-arch-sfd.c\ spi.c cc2420.c cc2420-arch.c cc2420-arch-sfd.c\
node-id.c sensors.c button-sensor.c cfs-coffee.c \ node-id.c sensors.c button-sensor.c cfs-coffee.c \
radio-sensor.c uart0.c uart0-putchar.c uip-ipchksum.c \ radio-sensor.c uart0.c uart0-putchar.c uip-ipchksum.c \
slip.c slip_uart0.c \ slip.c slip_uart0.c \