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