Contiki-NG
Loading...
Searching...
No Matches
tsch-security.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, SICS Swedish ICT.
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 * TSCH security
36 * \author
37 * Simon Duquennoy <simonduq@sics.se>
38 */
39
40/**
41 * \addtogroup tsch
42 * @{
43*/
44
45#include "contiki.h"
46#include "net/mac/tsch/tsch.h"
49#include "net/netstack.h"
50#include "net/packetbuf.h"
51#include "lib/ccm-star.h"
52#include "lib/aes-128.h"
53#include "sys/cc.h"
54#include <stdio.h>
55#include <string.h>
56
57#if LLSEC802154_ENABLED && !LLSEC802154_USES_EXPLICIT_KEYS
58#error LLSEC802154_ENABLED set but LLSEC802154_USES_EXPLICIT_KEYS unset
59#endif /* LLSEC802154_ENABLED */
60
61/* The two keys K1 and K2 from 6TiSCH minimal configuration
62 * K1: well-known, used for EBs
63 * K2: secret, used for data and ACK
64 * */
65static aes_key keys[] = {
66 TSCH_SECURITY_K1,
67 TSCH_SECURITY_K2
68};
69#define N_KEYS CC_ARRAY_LENGTH(keys)
70
71/*---------------------------------------------------------------------------*/
72static void
73tsch_security_init_nonce(uint8_t *nonce,
74 const linkaddr_t *sender, struct tsch_asn_t *asn)
75{
76 memcpy(nonce, sender, 8);
77 nonce[8] = asn->ms1b;
78 nonce[9] = (asn->ls4b >> 24) & 0xff;
79 nonce[10] = (asn->ls4b >> 16) & 0xff;
80 nonce[11] = (asn->ls4b >> 8) & 0xff;
81 nonce[12] = (asn->ls4b) & 0xff;
82}
83/*---------------------------------------------------------------------------*/
84static int
85tsch_security_check_level(const frame802154_t *frame)
86{
87 uint8_t required_security_level;
88 uint8_t required_key_index;
89
90 /* Sanity check */
91 if(frame == NULL) {
92 return 0;
93 }
94
95 /* Non-secured frame, ok iff we are not in a secured PAN
96 * (i.e. scanning or associated to a non-secured PAN) */
97 if(frame->fcf.security_enabled == 0) {
98 return !(tsch_is_associated == 1 && tsch_is_pan_secured == 1);
99 }
100
101 /* The frame is secured, that we are not in an unsecured PAN */
102 if(tsch_is_associated == 1 && tsch_is_pan_secured == 0) {
103 return 0;
104 }
105
106 /* The frame is secured, check its security level */
107 switch(frame->fcf.frame_type) {
108 case FRAME802154_BEACONFRAME:
109 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_EB;
110 required_key_index = TSCH_SECURITY_KEY_INDEX_EB;
111 break;
112 case FRAME802154_ACKFRAME:
113 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_ACK;
114 required_key_index = TSCH_SECURITY_KEY_INDEX_ACK;
115 break;
116 default:
117 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_OTHER;
118 required_key_index = TSCH_SECURITY_KEY_INDEX_OTHER;
119 break;
120 }
121 return ((frame->aux_hdr.security_control.security_level ==
122 required_security_level) &&
123 frame->aux_hdr.key_index == required_key_index);
124}
125/*---------------------------------------------------------------------------*/
126unsigned int
128{
129 if(frame != NULL && frame->fcf.security_enabled) {
130 return 2 << (frame->aux_hdr.security_control.security_level & 0x03);
131 } else {
132 return 0;
133 }
134}
135/*---------------------------------------------------------------------------*/
136unsigned int
137tsch_security_secure_frame(uint8_t *hdr, uint8_t *outbuf,
138 int hdrlen, int datalen, struct tsch_asn_t *asn)
139{
140 frame802154_t frame;
141 uint8_t key_index = 0;
142 uint8_t security_level = 0;
143 uint8_t with_encryption;
144 uint8_t mic_len;
145 uint8_t nonce[16];
146 struct ieee802154_ies ies;
147
148 uint8_t a_len;
149 uint8_t m_len;
150
151 if(hdr == NULL || outbuf == NULL || hdrlen < 0 || datalen < 0) {
152 return 0;
153 }
154
155 /* Parse the frame header to extract security settings */
156 if(frame802154_parse(hdr, hdrlen + datalen, &frame) < 3) {
157 return 0;
158 }
159
160 memset(&ies, 0, sizeof(ies));
161 if(frame802154e_parse_information_elements(hdr + hdrlen, datalen, &ies) > 0) {
162 /* put Header IEs into the header part which is not encrypted */
163 hdrlen += ies.ie_payload_ie_offset;
164 datalen -= ies.ie_payload_ie_offset;
165 }
166
167 if(!frame.fcf.security_enabled) {
168 /* Security is not enabled for this frame, we're done */
169 return 0;
170 }
171
172 /* Read security key index */
173 key_index = frame.aux_hdr.key_index;
174 security_level = frame.aux_hdr.security_control.security_level;
175 with_encryption = (security_level & 0x4) ? 1 : 0;
176 mic_len = tsch_security_mic_len(&frame);
177
178 if(key_index == 0 || key_index > N_KEYS) {
179 return 0;
180 }
181
182 tsch_security_init_nonce(nonce, &linkaddr_node_addr, asn);
183
184 if(with_encryption) {
185 a_len = hdrlen;
186 m_len = datalen;
187 } else {
188 a_len = hdrlen + datalen;
189 m_len = 0;
190 }
191
192 /* Copy source data to output */
193 if(hdr != outbuf) {
194 memcpy(outbuf, hdr, a_len + m_len);
195 }
196
197 CCM_STAR.set_key(keys[key_index - 1]);
198
199 CCM_STAR.aead(nonce,
200 outbuf + a_len, m_len,
201 outbuf, a_len,
202 outbuf + hdrlen + datalen, mic_len, 1);
203
204 return mic_len;
205}
206/*---------------------------------------------------------------------------*/
207unsigned int
208tsch_security_parse_frame(const uint8_t *hdr, int hdrlen, int datalen,
209 const frame802154_t *frame, const linkaddr_t *sender,
210 struct tsch_asn_t *asn)
211{
212 uint8_t generated_mic[16];
213 uint8_t key_index = 0;
214 uint8_t security_level = 0;
215 uint8_t with_encryption;
216 uint8_t mic_len;
217 uint8_t nonce[16];
218 uint8_t a_len;
219 uint8_t m_len;
220 struct ieee802154_ies ies;
221
222 if(frame == NULL || hdr == NULL || hdrlen < 0 || datalen < 0) {
223 return 0;
224 }
225
226 if(!tsch_security_check_level(frame)) {
227 /* Wrong security level */
228 return 0;
229 }
230
231 /* No security: nothing more to check */
232 if(!frame->fcf.security_enabled) {
233 return 1;
234 }
235
236 key_index = frame->aux_hdr.key_index;
237 security_level = frame->aux_hdr.security_control.security_level;
238 with_encryption = (security_level & 0x4) ? 1 : 0;
239 mic_len = tsch_security_mic_len(frame);
240
241 /* Check if key_index is in supported range */
242 if(key_index == 0 || key_index > N_KEYS) {
243 return 0;
244 }
245
246 memset(&ies, 0, sizeof(ies));
247 (void)frame802154e_parse_information_elements(hdr + hdrlen, datalen, &ies);
248 /* put Header IEs into the header part which is not encrypted */
249 hdrlen += ies.ie_payload_ie_offset;
250 datalen -= ies.ie_payload_ie_offset;
251
252 tsch_security_init_nonce(nonce, sender, asn);
253
254 if(with_encryption) {
255 a_len = hdrlen;
256 m_len = datalen;
257 } else {
258 a_len = hdrlen + datalen;
259 m_len = 0;
260 }
261
262 CCM_STAR.set_key(keys[key_index - 1]);
263
264 CCM_STAR.aead(nonce,
265 (uint8_t *)hdr + a_len, m_len,
266 (uint8_t *)hdr, a_len,
267 generated_mic, mic_len, 0);
268
269 if(mic_len > 0 && memcmp(generated_mic, hdr + hdrlen + datalen, mic_len) != 0) {
270 return 0;
271 } else {
272 return 1;
273 }
274}
275/*---------------------------------------------------------------------------*/
276void
278{
279#if LLSEC802154_ENABLED
280 if(tsch_is_pan_secured) {
281 /* Set security level, key id and index */
282 switch(frame_type) {
283 case FRAME802154_ACKFRAME:
284 /* For ACKs, we set attriburtes via tsch_packet_eackbuf_set_attr, as classic
285 * interrupts can not be used from interrupt context. */
286 tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, TSCH_SECURITY_KEY_SEC_LEVEL_ACK);
287 tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, FRAME802154_1_BYTE_KEY_ID_MODE);
288 tsch_packet_eackbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, TSCH_SECURITY_KEY_INDEX_ACK);
289 break;
290 case FRAME802154_BEACONFRAME:
291 packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, TSCH_SECURITY_KEY_SEC_LEVEL_EB);
292 packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, FRAME802154_1_BYTE_KEY_ID_MODE);
293 packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, TSCH_SECURITY_KEY_INDEX_EB);
294 break;
295 default:
296 packetbuf_set_attr(PACKETBUF_ATTR_SECURITY_LEVEL, TSCH_SECURITY_KEY_SEC_LEVEL_OTHER);
297 packetbuf_set_attr(PACKETBUF_ATTR_KEY_ID_MODE, FRAME802154_1_BYTE_KEY_ID_MODE);
298 packetbuf_set_attr(PACKETBUF_ATTR_KEY_INDEX, TSCH_SECURITY_KEY_INDEX_OTHER);
299 break;
300 }
301 }
302#endif /* LLSEC802154_ENABLED */
303}
304/** @} */
AES-128.
Default definitions of C compiler quirk work-arounds.
CCM* header file.
802.15.4 frame creation and parsing functions
A MAC framer for IEEE 802.15.4.
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition linkaddr.c:48
void tsch_security_set_packetbuf_attr(uint8_t frame_type)
Set packetbuf (or eackbuf) attributes depending on a given frame type.
unsigned int tsch_security_mic_len(const frame802154_t *frame)
Return MIC length.
unsigned int tsch_security_parse_frame(const uint8_t *hdr, int hdrlen, int datalen, const frame802154_t *frame, const linkaddr_t *sender, struct tsch_asn_t *asn)
Parse and check a frame protected with encryption and/or MIC.
unsigned int tsch_security_secure_frame(uint8_t *hdr, uint8_t *outbuf, int hdrlen, int datalen, struct tsch_asn_t *asn)
Protect a frame with encryption and/or MIC.
void tsch_packet_eackbuf_set_attr(uint8_t type, const packetbuf_attr_t val)
Set a packet attribute for the current eack.
Definition tsch-packet.c:78
Include file for the Contiki low-layer network stack (NETSTACK).
Header file for the Packet buffer (packetbuf) management.
frame802154_scf_t security_control
Security control bitfield.
uint8_t key_index
Key Index subfield.
uint8_t frame_type
3 bit.
uint8_t security_enabled
1 bit.
uint8_t security_level
3 bit.
Parameters used by the frame802154_create() function.
frame802154_aux_hdr_t aux_hdr
Aux security header.
frame802154_fcf_t fcf
Frame control field.
The ASN is an absolute slot number over 5 bytes.
Definition tsch-asn.h:48
Main API declarations for TSCH.