2014-02-04 00:25:30 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* 802.15.4 security implementation, which uses a network-wide key
|
|
|
|
* \author
|
|
|
|
* Konrad Krentz <konrad.krentz@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 01:15:42 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup noncoresec
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2014-02-04 00:25:30 +01:00
|
|
|
#include "net/llsec/noncoresec/noncoresec.h"
|
|
|
|
#include "net/llsec/anti-replay.h"
|
|
|
|
#include "net/llsec/llsec802154.h"
|
2015-06-08 20:06:40 +02:00
|
|
|
#include "net/llsec/ccm-star-packetbuf.h"
|
2014-02-04 00:25:30 +01:00
|
|
|
#include "net/mac/frame802154.h"
|
|
|
|
#include "net/netstack.h"
|
|
|
|
#include "net/packetbuf.h"
|
|
|
|
#include "net/nbr-table.h"
|
|
|
|
#include "net/linkaddr.h"
|
2015-06-08 20:06:40 +02:00
|
|
|
#include "lib/ccm-star.h"
|
2014-02-04 00:25:30 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-12-17 14:55:13 +01:00
|
|
|
#ifdef NONCORESEC_CONF_DECORATED_FRAMER
|
|
|
|
#define DECORATED_FRAMER NONCORESEC_CONF_DECORATED_FRAMER
|
|
|
|
#else /* NONCORESEC_CONF_DECORATED_FRAMER */
|
|
|
|
#define DECORATED_FRAMER framer_802154
|
|
|
|
#endif /* NONCORESEC_CONF_DECORATED_FRAMER */
|
|
|
|
|
|
|
|
extern const struct framer DECORATED_FRAMER;
|
|
|
|
|
2015-12-17 12:13:32 +01:00
|
|
|
#ifdef NONCORESEC_CONF_SEC_LVL
|
|
|
|
#define SEC_LVL NONCORESEC_CONF_SEC_LVL
|
|
|
|
#else /* NONCORESEC_CONF_SEC_LVL */
|
|
|
|
#define SEC_LVL 2
|
|
|
|
#endif /* NONCORESEC_CONF_SEC_LVL */
|
|
|
|
|
|
|
|
#define WITH_ENCRYPTION (SEC_LVL & (1 << 2))
|
|
|
|
#define MIC_LEN LLSEC802154_MIC_LEN(SEC_LVL)
|
2014-02-04 00:25:30 +01:00
|
|
|
|
|
|
|
#ifdef NONCORESEC_CONF_KEY
|
|
|
|
#define NONCORESEC_KEY NONCORESEC_CONF_KEY
|
|
|
|
#else /* NONCORESEC_CONF_KEY */
|
|
|
|
#define NONCORESEC_KEY { 0x00 , 0x01 , 0x02 , 0x03 , \
|
|
|
|
0x04 , 0x05 , 0x06 , 0x07 , \
|
|
|
|
0x08 , 0x09 , 0x0A , 0x0B , \
|
|
|
|
0x0C , 0x0D , 0x0E , 0x0F }
|
|
|
|
#endif /* NONCORESEC_CONF_KEY */
|
|
|
|
|
2014-02-09 15:02:55 +01:00
|
|
|
#define SECURITY_HEADER_LENGTH 5
|
|
|
|
|
2014-02-04 00:25:30 +01:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else /* DEBUG */
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2015-12-17 12:13:32 +01:00
|
|
|
#if LLSEC802154_USES_AUX_HEADER && SEC_LVL && LLSEC802154_USES_FRAME_COUNTER
|
2015-12-17 10:42:00 +01:00
|
|
|
|
2014-02-04 00:25:30 +01:00
|
|
|
/* network-wide CCM* key */
|
|
|
|
static uint8_t key[16] = NONCORESEC_KEY;
|
|
|
|
NBR_TABLE(struct anti_replay_info, anti_replay_table);
|
|
|
|
|
2015-07-15 15:11:42 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2015-07-17 17:26:35 +02:00
|
|
|
aead(uint8_t hdrlen, int forward)
|
2015-07-15 15:11:42 +02:00
|
|
|
{
|
2015-07-17 17:26:35 +02:00
|
|
|
uint8_t totlen;
|
2015-07-15 15:11:42 +02:00
|
|
|
uint8_t nonce[CCM_STAR_NONCE_LENGTH];
|
|
|
|
uint8_t *m;
|
|
|
|
uint8_t m_len;
|
|
|
|
uint8_t *a;
|
|
|
|
uint8_t a_len;
|
|
|
|
uint8_t *result;
|
2015-12-17 12:13:32 +01:00
|
|
|
uint8_t generated_mic[MIC_LEN];
|
2015-07-15 15:11:42 +02:00
|
|
|
uint8_t *mic;
|
|
|
|
|
|
|
|
ccm_star_packetbuf_set_nonce(nonce, forward);
|
2015-07-17 17:26:35 +02:00
|
|
|
totlen = packetbuf_totlen();
|
2015-07-15 15:11:42 +02:00
|
|
|
a = packetbuf_hdrptr();
|
|
|
|
#if WITH_ENCRYPTION
|
2015-07-17 17:26:35 +02:00
|
|
|
a_len = hdrlen;
|
|
|
|
m = a + a_len;
|
|
|
|
m_len = totlen - hdrlen;
|
2015-07-15 15:11:42 +02:00
|
|
|
#else /* WITH_ENCRYPTION */
|
2015-07-17 17:26:35 +02:00
|
|
|
a_len = totlen;
|
|
|
|
m = NULL;
|
2015-07-15 15:11:42 +02:00
|
|
|
m_len = 0;
|
|
|
|
#endif /* WITH_ENCRYPTION */
|
2015-07-17 17:26:35 +02:00
|
|
|
|
|
|
|
mic = a + totlen;
|
2015-07-15 15:11:42 +02:00
|
|
|
result = forward ? mic : generated_mic;
|
|
|
|
|
|
|
|
CCM_STAR.aead(nonce,
|
|
|
|
m, m_len,
|
|
|
|
a, a_len,
|
2015-12-17 12:13:32 +01:00
|
|
|
result, MIC_LEN,
|
2015-07-15 15:11:42 +02:00
|
|
|
forward);
|
|
|
|
|
|
|
|
if(forward) {
|
2015-12-17 12:13:32 +01:00
|
|
|
packetbuf_set_datalen(packetbuf_datalen() + MIC_LEN);
|
2015-07-15 15:11:42 +02:00
|
|
|
return 1;
|
|
|
|
} else {
|
2015-12-17 12:13:32 +01:00
|
|
|
return (memcmp(generated_mic, mic, MIC_LEN) == 0);
|
2015-07-15 15:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 00:25:30 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2015-07-17 17:26:35 +02:00
|
|
|
add_security_header(void)
|
|
|
|
{
|
|
|
|
if(!packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL)) {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
|
2015-12-17 12:13:32 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, SEC_LVL);
|
2015-07-17 17:26:35 +02:00
|
|
|
anti_replay_set_counter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2014-02-04 00:25:30 +01:00
|
|
|
send(mac_callback_t sent, void *ptr)
|
|
|
|
{
|
|
|
|
NETSTACK_MAC.send(sent, ptr);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2015-06-27 18:21:08 +02:00
|
|
|
create(void)
|
2014-02-04 00:25:30 +01:00
|
|
|
{
|
2015-06-27 18:21:08 +02:00
|
|
|
int result;
|
|
|
|
|
2015-07-17 17:26:35 +02:00
|
|
|
add_security_header();
|
2015-12-17 14:55:13 +01:00
|
|
|
result = DECORATED_FRAMER.create();
|
2015-06-27 18:21:08 +02:00
|
|
|
if(result == FRAMER_FAILED) {
|
|
|
|
return result;
|
|
|
|
}
|
2015-07-01 16:00:14 +02:00
|
|
|
|
2015-07-17 17:26:35 +02:00
|
|
|
aead(result, 1);
|
2014-02-04 00:25:30 +01:00
|
|
|
|
2015-06-27 18:21:08 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
parse(void)
|
|
|
|
{
|
2015-07-17 17:26:35 +02:00
|
|
|
int result;
|
2014-02-04 00:25:30 +01:00
|
|
|
const linkaddr_t *sender;
|
|
|
|
struct anti_replay_info* info;
|
|
|
|
|
2015-12-17 14:55:13 +01:00
|
|
|
result = DECORATED_FRAMER.parse();
|
2015-07-17 17:26:35 +02:00
|
|
|
if(result == FRAMER_FAILED) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-12-17 12:13:32 +01:00
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) != SEC_LVL) {
|
2014-02-04 00:25:30 +01:00
|
|
|
PRINTF("noncoresec: received frame with wrong security level\n");
|
2015-07-17 17:26:35 +02:00
|
|
|
return FRAMER_FAILED;
|
2014-02-04 00:25:30 +01:00
|
|
|
}
|
|
|
|
sender = packetbuf_addr(PACKETBUF_ADDR_SENDER);
|
|
|
|
if(linkaddr_cmp(sender, &linkaddr_node_addr)) {
|
|
|
|
PRINTF("noncoresec: frame from ourselves\n");
|
2015-07-17 17:26:35 +02:00
|
|
|
return FRAMER_FAILED;
|
2014-02-04 00:25:30 +01:00
|
|
|
}
|
|
|
|
|
2015-12-17 12:13:32 +01:00
|
|
|
packetbuf_set_datalen(packetbuf_datalen() - MIC_LEN);
|
2014-02-04 00:25:30 +01:00
|
|
|
|
2015-07-17 17:26:35 +02:00
|
|
|
if(!aead(result, 0)) {
|
2015-07-15 15:11:42 +02:00
|
|
|
PRINTF("noncoresec: received unauthentic frame %"PRIu32"\n",
|
2014-02-04 00:25:30 +01:00
|
|
|
anti_replay_get_counter());
|
2015-07-17 17:26:35 +02:00
|
|
|
return FRAMER_FAILED;
|
2014-02-04 00:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
info = nbr_table_get_from_lladdr(anti_replay_table, sender);
|
|
|
|
if(!info) {
|
2015-09-20 21:02:30 +02:00
|
|
|
info = nbr_table_add_lladdr(anti_replay_table, sender, NBR_TABLE_REASON_LLSEC, NULL);
|
2014-02-04 00:25:30 +01:00
|
|
|
if(!info) {
|
|
|
|
PRINTF("noncoresec: could not get nbr_table_item\n");
|
2015-07-17 17:26:35 +02:00
|
|
|
return FRAMER_FAILED;
|
2014-02-04 00:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Locking avoids replay attacks due to removed neighbor table items.
|
|
|
|
* Unfortunately, an attacker can mount a memory-based DoS attack
|
|
|
|
* on this by replaying broadcast frames from other network parts.
|
|
|
|
* However, this is not an issue as long as the network size does not
|
|
|
|
* exceed NBR_TABLE_MAX_NEIGHBORS.
|
|
|
|
*
|
|
|
|
* To avoid locking, we could swap anti-replay information
|
|
|
|
* to external flash. Locking is also unnecessary when using
|
|
|
|
* pairwise session keys, as done in coresec.
|
|
|
|
*/
|
|
|
|
if(!nbr_table_lock(anti_replay_table, info)) {
|
|
|
|
nbr_table_remove(anti_replay_table, info);
|
|
|
|
PRINTF("noncoresec: could not lock\n");
|
2015-07-17 17:26:35 +02:00
|
|
|
return FRAMER_FAILED;
|
2014-02-04 00:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
anti_replay_init_info(info);
|
|
|
|
} else {
|
|
|
|
if(anti_replay_was_replayed(info)) {
|
|
|
|
PRINTF("noncoresec: received replayed frame %"PRIu32"\n",
|
|
|
|
anti_replay_get_counter());
|
2015-07-17 17:26:35 +02:00
|
|
|
return FRAMER_FAILED;
|
2014-02-04 00:25:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 17:26:35 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
input(void)
|
|
|
|
{
|
2014-02-04 00:25:30 +01:00
|
|
|
NETSTACK_NETWORK.input();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-06-27 18:21:08 +02:00
|
|
|
static int
|
|
|
|
length(void)
|
2014-02-09 15:02:55 +01:00
|
|
|
{
|
2015-07-17 17:26:35 +02:00
|
|
|
add_security_header();
|
2015-12-17 14:55:13 +01:00
|
|
|
return DECORATED_FRAMER.length() + MIC_LEN;
|
2014-02-09 15:02:55 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-02-04 00:25:30 +01:00
|
|
|
static void
|
2015-06-27 21:22:53 +02:00
|
|
|
init(void)
|
2014-02-04 00:25:30 +01:00
|
|
|
{
|
2015-06-08 20:06:40 +02:00
|
|
|
CCM_STAR.set_key(key);
|
2014-02-04 00:25:30 +01:00
|
|
|
nbr_table_register(anti_replay_table, NULL);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const struct llsec_driver noncoresec_driver = {
|
|
|
|
"noncoresec",
|
2015-06-27 21:22:53 +02:00
|
|
|
init,
|
2014-02-04 00:25:30 +01:00
|
|
|
send,
|
2015-06-27 18:21:08 +02:00
|
|
|
input
|
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const struct framer noncoresec_framer = {
|
|
|
|
length,
|
|
|
|
create,
|
|
|
|
parse
|
2014-02-04 00:25:30 +01:00
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-12-17 12:13:32 +01:00
|
|
|
#endif /* LLSEC802154_USES_AUX_HEADER && SEC_LVL && LLSEC802154_USES_FRAME_COUNTER */
|
2014-02-04 00:25:30 +01:00
|
|
|
|
|
|
|
/** @} */
|