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#define N_KEYS (sizeof(keys) / sizeof(aes_key))
100/*---------------------------------------------------------------------------*/
101static int
102aead(uint8_t hdrlen, int forward)
103{
104 uint8_t totlen;
105 uint8_t nonce[CCM_STAR_NONCE_LENGTH];
106 uint8_t *m;
107 uint8_t m_len;
108 uint8_t *a;
109 uint8_t a_len;
110 uint8_t *result;
111 /* Allocate for MAX level */
112 uint8_t generated_mic[LLSEC802154_MIC_LEN(
113 FRAME802154_SECURITY_LEVEL_ENC_MIC_128)];
114 uint8_t *mic;
115 uint8_t key_index;
116 aes_key_t *key;
117 uint8_t with_encryption;
118
119 key_index = LLSEC_KEY_INDEX;
120 if(key_index >= CSMA_LLSEC_MAXKEYS) {
121 LOG_ERR("Key not available: %u\n", key_index);
122 return 0;
123 }
124
125 key = &keys[key_index];
126
127 ccm_star_packetbuf_set_nonce(nonce, forward);
128 totlen = packetbuf_totlen();
129 a = packetbuf_hdrptr();
130
131 with_encryption =
132 (packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & 0x4) ? 1 : 0;
133
134 if(with_encryption) {
135 a_len = hdrlen;
136 m = a + a_len;
137 m_len = totlen - hdrlen;
138 } else {
139 a_len = totlen;
140 m = NULL;
141 m_len = 0;
142 }
143
144 mic = a + totlen;
145 result = forward ? mic : generated_mic;
146
147 CCM_STAR.set_key(key->u8);
148 CCM_STAR.aead(nonce,
149 m, m_len,
150 a, a_len,
151 result, LLSEC802154_PACKETBUF_MIC_LEN(),
152 forward);
153
154 if(forward) {
156 + LLSEC802154_PACKETBUF_MIC_LEN());
157 return 1;
158 } else {
159 return memcmp(generated_mic, mic, LLSEC802154_PACKETBUF_MIC_LEN()) == 0;
160 }
161}
162
163/*---------------------------------------------------------------------------*/
164static int
165create(void)
166{
167 int hdr_len;
168
169 packetbuf_set_attr(PACKETBUF_ATTR_FRAME_TYPE, FRAME802154_DATAFRAME);
170 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0 &&
171 LLSEC_KEY_INDEX != 0xffff) {
173 }
174
175 hdr_len = framer_802154.create();
176 if(hdr_len < 0) {
177 return hdr_len;
178 }
179
180 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0) {
181#if LOG_LEVEL == LOG_LEVEL_DBG
182 int i = 0;
183 uint8_t *p;
184 LOG_DBG(" Payload before (%d):", packetbuf_totlen());
185 p = packetbuf_hdrptr();
186 for(i = 0; i < packetbuf_totlen(); i++) {
187 LOG_DBG_("%c%c", HEX[(p[i] >> 4) & 0x0f], HEX[p[i] & 0x0f]);
188 }
189 LOG_DBG("\n");
190#endif
191
192 if(!aead(hdr_len, 1)) {
193 LOG_ERR("failed to encrypt packet to ");
194 LOG_ERR_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
195 LOG_ERR_("\n");
196 return FRAMER_FAILED;
197 }
198 LOG_INFO("LLSEC-OUT:");
199 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
200 LOG_INFO_(" ");
201 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
202 LOG_INFO_(" %u (%u) LV:%d, KEY:0x%02x\n", packetbuf_datalen(), packetbuf_totlen(),
203 packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL), LLSEC_KEY_INDEX);
204
205#if LOG_LEVEL == LOG_LEVEL_DBG
206 LOG_DBG(" Payload after: (%d)", packetbuf_totlen());
207 p = packetbuf_hdrptr();
208 for(i = 0; i < packetbuf_totlen(); i++) {
209 LOG_DBG_("%c%c", HEX[(p[i] >> 4) & 0x0f], HEX[p[i] & 0x0f]);
210 }
211 LOG_DBG_("\n");
212#endif
213
214 }
215 return hdr_len;
216}
217
218/*---------------------------------------------------------------------------*/
219static int
220length(void)
221{
222 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) > 0 &&
223 LLSEC_KEY_INDEX != 0xffff) {
224 return framer_802154.length() + LLSEC802154_PACKETBUF_MIC_LEN();
225 }
226 return framer_802154.length();
227}
228/*---------------------------------------------------------------------------*/
229static int
230parse(void)
231{
232 int hdr_len;
233
234 hdr_len = framer_802154.parse();
235 if(hdr_len < 0) {
236 return hdr_len;
237 }
238
239 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) == 0) {
240 /* From https://github.com/contiki-ng/contiki-ng/issues/1610 */
241 /* This should reject unencrypted packets */
242 #ifdef LLSEC802154_REJECT_INSECURE
243 return FRAMER_FAILED;
244 #else
245 /* No security - no more processing required */
246 return hdr_len;
247 #endif
248 }
249
250 LOG_INFO("LLSEC-IN: ");
251 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
252 LOG_INFO_(" ");
253 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
254 LOG_INFO_(" %d %u (%u) LV:%d KM:%d KEY:0x%02x\n", hdr_len, packetbuf_datalen(),
255 packetbuf_totlen(), packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL),
256 LLSEC_KEY_MODE,
257 LLSEC_KEY_INDEX);
258
259 if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) != CSMA_LLSEC_SECURITY_LEVEL) {
260 LOG_INFO("received frame with wrong security level (%u) from ",
261 packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL));
262 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
263 LOG_INFO_("\n");
264 return FRAMER_FAILED;
265 }
266
267 if(LLSEC_KEY_MODE != CSMA_LLSEC_KEY_ID_MODE) {
268 LOG_INFO("received frame with wrong key id mode (%u) from ", LLSEC_KEY_MODE);
269 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
270 LOG_INFO("\n");
271 return FRAMER_FAILED;
272 }
273
274 if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER), &linkaddr_node_addr)) {
275 LOG_INFO("frame from ourselves\n");
276 return FRAMER_FAILED;
277 }
278
279 if(packetbuf_datalen() <= LLSEC802154_PACKETBUF_MIC_LEN()) {
280 LOG_ERR("MIC error - too little data in frame!\n");
281 return FRAMER_FAILED;
282 }
283
284 packetbuf_set_datalen(packetbuf_datalen() - LLSEC802154_PACKETBUF_MIC_LEN());
285 if(!aead(hdr_len, 0)) {
286 LOG_INFO("received unauthentic frame %u from ",
287 (unsigned int) anti_replay_get_counter());
288 LOG_INFO_LLADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
289 LOG_INFO_("\n");
290 return FRAMER_FAILED;
291 }
292
293 /* TODO anti-reply protection */
294 return hdr_len;
295}
296/*---------------------------------------------------------------------------*/
297const struct framer csma_security_framer = {
298 length,
299 create,
300 parse,
301};
302#endif /* LLSEC802154_USES_AUX_HEADER && LLSEC802154_USES_FRAME_COUNTER */
303
304/** @} */
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.