Contiki-NG
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 <stdio.h>
54 #include <string.h>
55 
56 /* The two keys K1 and K2 from 6TiSCH minimal configuration
57  * K1: well-known, used for EBs
58  * K2: secret, used for data and ACK
59  * */
60 static aes_key keys[] = {
61  TSCH_SECURITY_K1,
62  TSCH_SECURITY_K2
63 };
64 #define N_KEYS (sizeof(keys) / sizeof(aes_key))
65 
66 /*---------------------------------------------------------------------------*/
67 static void
68 tsch_security_init_nonce(uint8_t *nonce,
69  const linkaddr_t *sender, struct tsch_asn_t *asn)
70 {
71  memcpy(nonce, sender, 8);
72  nonce[8] = asn->ms1b;
73  nonce[9] = (asn->ls4b >> 24) & 0xff;
74  nonce[10] = (asn->ls4b >> 16) & 0xff;
75  nonce[11] = (asn->ls4b >> 8) & 0xff;
76  nonce[12] = (asn->ls4b) & 0xff;
77 }
78 /*---------------------------------------------------------------------------*/
79 static int
80 tsch_security_check_level(const frame802154_t *frame)
81 {
82  uint8_t required_security_level;
83  uint8_t required_key_index;
84 
85  /* Sanity check */
86  if(frame == NULL) {
87  return 0;
88  }
89 
90  /* Non-secured frame, ok iff we are not in a secured PAN
91  * (i.e. scanning or associated to a non-secured PAN) */
92  if(frame->fcf.security_enabled == 0) {
93  return !(tsch_is_associated == 1 && tsch_is_pan_secured == 1);
94  }
95 
96  /* The frame is secured, that we are not in an unsecured PAN */
97  if(tsch_is_associated == 1 && tsch_is_pan_secured == 0) {
98  return 0;
99  }
100 
101  /* The frame is secured, check its security level */
102  switch(frame->fcf.frame_type) {
103  case FRAME802154_BEACONFRAME:
104  required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_EB;
105  required_key_index = TSCH_SECURITY_KEY_INDEX_EB;
106  break;
107  case FRAME802154_ACKFRAME:
108  required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_ACK;
109  required_key_index = TSCH_SECURITY_KEY_INDEX_ACK;
110  break;
111  default:
112  required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_OTHER;
113  required_key_index = TSCH_SECURITY_KEY_INDEX_OTHER;
114  break;
115  }
116  return ((frame->aux_hdr.security_control.security_level ==
117  required_security_level) &&
118  frame->aux_hdr.key_index == required_key_index);
119 }
120 /*---------------------------------------------------------------------------*/
121 unsigned int
123 {
124  if(frame != NULL && frame->fcf.security_enabled) {
125  return 2 << (frame->aux_hdr.security_control.security_level & 0x03);
126  } else {
127  return 0;
128  }
129 }
130 /*---------------------------------------------------------------------------*/
131 unsigned int
132 tsch_security_secure_frame(uint8_t *hdr, uint8_t *outbuf,
133  int hdrlen, int datalen, struct tsch_asn_t *asn)
134 {
135  frame802154_t frame;
136  uint8_t key_index = 0;
137  uint8_t security_level = 0;
138  uint8_t with_encryption;
139  uint8_t mic_len;
140  uint8_t nonce[16];
141  struct ieee802154_ies ies;
142 
143  uint8_t a_len;
144  uint8_t m_len;
145 
146  if(hdr == NULL || outbuf == NULL || hdrlen < 0 || datalen < 0) {
147  return 0;
148  }
149 
150  /* Parse the frame header to extract security settings */
151  if(frame802154_parse(hdr, hdrlen + datalen, &frame) < 3) {
152  return 0;
153  }
154 
155  memset(&ies, 0, sizeof(ies));
156  if(frame802154e_parse_information_elements(hdr + hdrlen, datalen, &ies) > 0) {
157  /* put Header IEs into the header part which is not encrypted */
158  hdrlen += ies.ie_payload_ie_offset;
159  datalen -= ies.ie_payload_ie_offset;
160  }
161 
162  if(!frame.fcf.security_enabled) {
163  /* Security is not enabled for this frame, we're done */
164  return 0;
165  }
166 
167  /* Read security key index */
168  key_index = frame.aux_hdr.key_index;
169  security_level = frame.aux_hdr.security_control.security_level;
170  with_encryption = (security_level & 0x4) ? 1 : 0;
171  mic_len = tsch_security_mic_len(&frame);
172 
173  if(key_index == 0 || key_index > N_KEYS) {
174  return 0;
175  }
176 
177  tsch_security_init_nonce(nonce, &linkaddr_node_addr, asn);
178 
179  if(with_encryption) {
180  a_len = hdrlen;
181  m_len = datalen;
182  } else {
183  a_len = hdrlen + datalen;
184  m_len = 0;
185  }
186 
187  /* Copy source data to output */
188  if(hdr != outbuf) {
189  memcpy(outbuf, hdr, a_len + m_len);
190  }
191 
192  CCM_STAR.set_key(keys[key_index - 1]);
193 
194  CCM_STAR.aead(nonce,
195  outbuf + a_len, m_len,
196  outbuf, a_len,
197  outbuf + hdrlen + datalen, mic_len, 1);
198 
199  return mic_len;
200 }
201 /*---------------------------------------------------------------------------*/
202 unsigned int
203 tsch_security_parse_frame(const uint8_t *hdr, int hdrlen, int datalen,
204  const frame802154_t *frame, const linkaddr_t *sender,
205  struct tsch_asn_t *asn)
206 {
207  uint8_t generated_mic[16];
208  uint8_t key_index = 0;
209  uint8_t security_level = 0;
210  uint8_t with_encryption;
211  uint8_t mic_len;
212  uint8_t nonce[16];
213  uint8_t a_len;
214  uint8_t m_len;
215  struct ieee802154_ies ies;
216 
217  if(frame == NULL || hdr == NULL || hdrlen < 0 || datalen < 0) {
218  return 0;
219  }
220 
221  if(!tsch_security_check_level(frame)) {
222  /* Wrong security level */
223  return 0;
224  }
225 
226  /* No security: nothing more to check */
227  if(!frame->fcf.security_enabled) {
228  return 1;
229  }
230 
231  key_index = frame->aux_hdr.key_index;
232  security_level = frame->aux_hdr.security_control.security_level;
233  with_encryption = (security_level & 0x4) ? 1 : 0;
234  mic_len = tsch_security_mic_len(frame);
235 
236  /* Check if key_index is in supported range */
237  if(key_index == 0 || key_index > N_KEYS) {
238  return 0;
239  }
240 
241  memset(&ies, 0, sizeof(ies));
242  (void)frame802154e_parse_information_elements(hdr + hdrlen, datalen, &ies);
243  /* put Header IEs into the header part which is not encrypted */
244  hdrlen += ies.ie_payload_ie_offset;
245  datalen -= ies.ie_payload_ie_offset;
246 
247  tsch_security_init_nonce(nonce, sender, asn);
248 
249  if(with_encryption) {
250  a_len = hdrlen;
251  m_len = datalen;
252  } else {
253  a_len = hdrlen + datalen;
254  m_len = 0;
255  }
256 
257  CCM_STAR.set_key(keys[key_index - 1]);
258 
259  CCM_STAR.aead(nonce,
260  (uint8_t *)hdr + a_len, m_len,
261  (uint8_t *)hdr, a_len,
262  generated_mic, mic_len, 0);
263 
264  if(mic_len > 0 && memcmp(generated_mic, hdr + hdrlen + datalen, mic_len) != 0) {
265  return 0;
266  } else {
267  return 1;
268  }
269 }
270 /** @} */
frame802154_scf_t security_control
Security control bitfield.
Definition: frame802154.h:188
frame802154_fcf_t fcf
Frame control field.
Definition: frame802154.h:204
uint8_t security_level
3 bit.
Definition: frame802154.h:168
uint8_t security_enabled
1 bit.
Definition: frame802154.h:154
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
Definition: frame802154.c:500
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.
A MAC framer for IEEE 802.15.4
uint8_t key_index
Key Index subfield.
Definition: frame802154.h:191
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
unsigned int tsch_security_mic_len(const frame802154_t *frame)
Return MIC length.
CCM* header file.
Main API declarations for TSCH.
802.15.4 frame creation and parsing functions
uint8_t frame_type
3 bit.
Definition: frame802154.h:153
Parameters used by the frame802154_create() function.
Definition: frame802154.h:198
Header file for the Packet buffer (packetbuf) management
Include file for the Contiki low-layer network stack (NETSTACK)
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.
AES-128.
The ASN is an absolute slot number over 5 bytes.
Definition: tsch-asn.h:48
frame802154_aux_hdr_t aux_hdr
Aux security header.
Definition: frame802154.h:208