Contiki-NG
Loading...
Searching...
No Matches
csma-security.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, RISE SICS
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \file
35 * CSMA security
36 * \author
37 * Joakim Eriksson <joakim.eriksson@ri.se>
38 */
39
40/**
41 * \addtogroup csma
42 * @{
43*/
44
45#include "contiki.h"
46#include "net/mac/csma/csma.h"
47#include "net/mac/anti-replay.h"
51#include "net/mac/llsec802154.h"
52#include "net/netstack.h"
53#include "net/packetbuf.h"
54#include "lib/ccm-star.h"
55#include "lib/aes-128.h"
56#include <stdio.h>
57#include <string.h>
58#include "net/mac/ccm-star-packetbuf.h"
59/* Log configuration */
60#include "sys/log.h"
61#define LOG_MODULE "CSMA"
62#define LOG_LEVEL LOG_LEVEL_MAC
63
64#if LOG_LEVEL == LOG_LEVEL_DBG
65static const char * HEX = "0123456789ABCDEF";
66#endif
67
68#if LLSEC802154_USES_AUX_HEADER && LLSEC802154_USES_FRAME_COUNTER
69
70#if LLSEC802154_USES_EXPLICIT_KEYS
71#define LLSEC_KEY_INDEX (FRAME802154_IMPLICIT_KEY == packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE) \
72 ? 0 \
73 : packetbuf_attr(PACKETBUF_ATTR_KEY_INDEX))
74#define LLSEC_KEY_MODE (packetbuf_attr(PACKETBUF_ATTR_KEY_ID_MODE))
75#else
76#define LLSEC_KEY_INDEX (0)
77#define LLSEC_KEY_MODE (FRAME802154_IMPLICIT_KEY)
78#endif /* LLSEC802154_USES_EXPLICIT_KEYS */
79
80/**
81 * The keys for LLSEC for CSMA
82 */
83typedef struct {
84 uint8_t u8[16];
85} aes_key_t;
86static aes_key_t keys[CSMA_LLSEC_MAXKEYS];
87
88/* assumed to be 16 bytes */
89int
90csma_security_set_key(uint8_t index, const uint8_t *key)
91{
92 if(key != NULL && index < CSMA_LLSEC_MAXKEYS) {
93 memcpy(keys[index].u8, key, 16);
94 return 1;
95 }
96 return 0;
97}
98
99/*---------------------------------------------------------------------------*/
100static int
101aead(uint8_t hdrlen, int forward)
102{
103 uint8_t totlen;
104 uint8_t nonce[CCM_STAR_NONCE_LENGTH];
105 uint8_t *m;
106 uint8_t m_len;
107 uint8_t *a;
108 uint8_t a_len;
109 uint8_t *result;
110 /* Allocate for MAX level */
111 uint8_t generated_mic[LLSEC802154_MIC_LEN(
112 FRAME802154_SECURITY_LEVEL_ENC_MIC_128)];
113 uint8_t *mic;
114 uint8_t key_index;
115 aes_key_t *key;
116 uint8_t with_encryption;
117
118 key_index = LLSEC_KEY_INDEX;
119 if(key_index >= CSMA_LLSEC_MAXKEYS) {
120 LOG_ERR("Key not available: %u\n", key_index);
121 return 0;
122 }
123
124 key = &keys[key_index];
125
126 ccm_star_packetbuf_set_nonce(nonce, forward);
127 totlen = packetbuf_totlen();
128 a = packetbuf_hdrptr();
129
130 with_encryption =
131 (packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x4) ? 1 : 0;
132
133 if(with_encryption) {
134 a_len = hdrlen;
135 m = a + a_len;
136 m_len = totlen - hdrlen;
137 } else {
138 a_len = totlen;
139 m = NULL;
140 m_len = 0;
141 }
142
143 mic = a + totlen;
144 result = forward ? mic : generated_mic;
145
146 CCM_STAR.set_key(key->u8);
147 CCM_STAR.aead(nonce,
148 m, m_len,
149 a, a_len,
150 result, LLSEC802154_PACKETBUF_MIC_LEN(),
151 forward);
152
153 if(forward) {
155 + LLSEC802154_PACKETBUF_MIC_LEN());
156 return 1;
157 } else {
158 return memcmp(generated_mic, mic, LLSEC802154_PACKETBUF_MIC_LEN()) == 0;
159 }
160}
161
162/*---------------------------------------------------------------------------*/
163static int
164create(void)
165{
166 int hdr_len;
167
168 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
169 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0 &&
170 LLSEC_KEY_INDEX != 0xffff) {
172 }
173
174 hdr_len = framer_802154.create();
175 if(hdr_len < 0) {
176 return hdr_len;
177 }
178
179 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0) {
180#if LOG_LEVEL == LOG_LEVEL_DBG
181 int i = 0;
182 uint8_t *p;
183 LOG_DBG(" Payload before (%d):", packetbuf_totlen());
184 p = packetbuf_hdrptr();
185 for(i = 0; i < packetbuf_totlen(); i++) {
186 LOG_DBG_("%c%c", HEX[(p[i] >> 4) & 0x0f], HEX[p[i] & 0x0f]);
187 }
188 LOG_DBG("\n");
189#endif
190
191 if(!aead(hdr_len, 1)) {
192 LOG_ERR("failed to encrypt packet to ");
193 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
194 LOG_ERR_("\n");
195 return FRAMER_FAILED;
196 }
197 LOG_INFO("LLSEC-OUT:");
198 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
199 LOG_INFO_(" ");
200 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
201 LOG_INFO_(" %u (%u) LV:%d, KEY:0x%02x\n", packetbuf_datalen(), packetbuf_totlen(),
202 packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL), LLSEC_KEY_INDEX);
203
204#if LOG_LEVEL == LOG_LEVEL_DBG
205 LOG_DBG(" Payload after: (%d)", packetbuf_totlen());
206 p = packetbuf_hdrptr();
207 for(i = 0; i < packetbuf_totlen(); i++) {
208 LOG_DBG_("%c%c", HEX[(p[i] >> 4) & 0x0f], HEX[p[i] & 0x0f]);
209 }
210 LOG_DBG_("\n");
211#endif
212
213 }
214 return hdr_len;
215}
216
217/*---------------------------------------------------------------------------*/
218static int
219length(void)
220{
221 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0 &&
222 LLSEC_KEY_INDEX != 0xffff) {
223 return framer_802154.length() + LLSEC802154_PACKETBUF_MIC_LEN();
224 }
225 return framer_802154.length();
226}
227/*---------------------------------------------------------------------------*/
228static int
229parse(void)
230{
231 int hdr_len;
232
233 hdr_len = framer_802154.parse();
234 if(hdr_len < 0) {
235 return hdr_len;
236 }
237
238 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) == 0) {
239 /* From https://github.com/contiki-ng/contiki-ng/issues/1610 */
240 /* This should reject unencrypted packets */
241 #ifdef LLSEC802154_REJECT_INSECURE
242 return FRAMER_FAILED;
243 #else
244 /* No security - no more processing required */
245 return hdr_len;
246 #endif
247 }
248
249 LOG_INFO("LLSEC-IN: ");
250 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
251 LOG_INFO_(" ");
252 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
253 LOG_INFO_(" %d %u (%u) LV:%d KM:%d KEY:0x%02x\n", hdr_len, packetbuf_datalen(),
254 packetbuf_totlen(), packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL),
255 LLSEC_KEY_MODE,
256 LLSEC_KEY_INDEX);
257
258 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) != CSMA_LLSEC_SECURITY_LEVEL) {
259 LOG_INFO("received frame with wrong security level (%u) from ",
260 packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL));
261 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
262 LOG_INFO_("\n");
263 return FRAMER_FAILED;
264 }
265
266 if(LLSEC_KEY_MODE != CSMA_LLSEC_KEY_ID_MODE) {
267 LOG_INFO("received frame with wrong key id mode (%u) from ", LLSEC_KEY_MODE);
268 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
269 LOG_INFO("\n");
270 return FRAMER_FAILED;
271 }
272
273 if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER), &linkaddr_node_addr)) {
274 LOG_INFO("frame from ourselves\n");
275 return FRAMER_FAILED;
276 }
277
278 if(packetbuf_datalen() <= LLSEC802154_PACKETBUF_MIC_LEN()) {
279 LOG_ERR("MIC error - too little data in frame!\n");
280 return FRAMER_FAILED;
281 }
282
283 packetbuf_set_datalen(packetbuf_datalen() - LLSEC802154_PACKETBUF_MIC_LEN());
284 if(!aead(hdr_len, 0)) {
285 LOG_INFO("received unauthentic frame %u from ",
286 (unsigned int) anti_replay_get_counter());
287 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
288 LOG_INFO_("\n");
289 return FRAMER_FAILED;
290 }
291
292 /* TODO anti-reply protection */
293 return hdr_len;
294}
295/*---------------------------------------------------------------------------*/
296const struct framer csma_security_framer = {
297 length,
298 create,
299 parse,
300};
301#endif /* LLSEC802154_USES_AUX_HEADER && LLSEC802154_USES_FRAME_COUNTER */
302
303/** @} */
AES-128.
Interface to anti-replay mechanisms.
CCM* header file.
LLSEC802154 Security related configuration.
The 802.15.4 standard CSMA protocol (nonbeacon-enabled).
802.15.4 frame creation and parsing functions
A MAC framer for IEEE 802.15.4.
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition linkaddr.c:48
bool linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition linkaddr.c:69
uint32_t anti_replay_get_counter(void)
Gets the frame counter from packetbuf.
void anti_replay_set_counter(void)
Sets the frame counter packetbuf attributes.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition packetbuf.c:136
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition packetbuf.c:167
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition packetbuf.c:155
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition packetbuf.c:149
Common functionality of 802.15.4-compliant llsec_drivers.
Header file for the logging system.
Include file for the Contiki low-layer network stack (NETSTACK).
Header file for the Packet buffer (packetbuf) management.