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"
56 #if TSCH_LOG_LEVEL >= 1
57 #define DEBUG DEBUG_PRINT
59 #define DEBUG DEBUG_NONE
67 static aes_key keys[] = {
71 #define N_KEYS (sizeof(keys) / sizeof(aes_key))
75 tsch_security_init_nonce(uint8_t *nonce,
76 const linkaddr_t *sender,
struct asn_t *asn)
78 memcpy(nonce, sender, 8);
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;
89 uint8_t required_security_level;
90 uint8_t required_key_index;
100 return !(tsch_is_associated == 1 && tsch_is_pan_secured == 1);
104 if(tsch_is_associated == 1 && tsch_is_pan_secured == 0) {
110 case FRAME802154_BEACONFRAME:
111 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_EB;
112 required_key_index = TSCH_SECURITY_KEY_INDEX_EB;
114 case FRAME802154_ACKFRAME:
115 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_ACK;
116 required_key_index = TSCH_SECURITY_KEY_INDEX_ACK;
119 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_OTHER;
120 required_key_index = TSCH_SECURITY_KEY_INDEX_OTHER;
138 tsch_security_secure_frame(uint8_t *hdr, uint8_t *outbuf,
139 int hdrlen,
int datalen,
struct asn_t *asn)
142 uint8_t key_index = 0;
143 uint8_t security_level = 0;
144 uint8_t with_encryption;
151 if(hdr ==
NULL || outbuf ==
NULL || hdrlen < 0 || datalen < 0) {
168 with_encryption = (security_level & 0x4) ? 1 : 0;
169 mic_len = tsch_security_mic_len(&frame);
171 if(key_index == 0 || key_index > N_KEYS) {
177 if(with_encryption) {
181 a_len = hdrlen + datalen;
187 memcpy(outbuf, hdr, a_len + m_len);
190 CCM_STAR.set_key(keys[key_index - 1]);
193 outbuf + a_len, m_len,
195 outbuf + hdrlen + datalen, mic_len, 1
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)
205 uint8_t generated_mic[16];
206 uint8_t key_index = 0;
207 uint8_t security_level = 0;
208 uint8_t with_encryption;
214 if(frame ==
NULL || hdr ==
NULL || hdrlen < 0 || datalen < 0) {
218 if(!tsch_security_check_level(frame)) {
230 with_encryption = (security_level & 0x4) ? 1 : 0;
231 mic_len = tsch_security_mic_len(frame);
234 if(key_index == 0 || key_index > N_KEYS) {
238 tsch_security_init_nonce(nonce, sender, asn);
240 if(with_encryption) {
244 a_len = hdrlen + datalen;
248 CCM_STAR.set_key(keys[key_index - 1]);
251 (uint8_t *)hdr + a_len, m_len,
252 (uint8_t *)hdr, a_len,
253 generated_mic, mic_len, 0
256 if(mic_len > 0 && memcmp(generated_mic, hdr + hdrlen + datalen, mic_len) != 0) {
uint8_t security_enabled
1 bit.
frame802154_scf_t security_control
Security control bitfield.
uint8_t key_index
Key Index subfield.
frame802154_fcf_t fcf
Frame control field.
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
#define NULL
The null pointer.
802.15.4 frame creation and parsing functions
int frame802154_parse(uint8_t *data, int len, frame802154_t *pf)
Parses an input frame.
uint8_t security_level
3 bit.
frame802154_aux_hdr_t aux_hdr
Aux security header.
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Parameters used by the frame802154_create() function.
Include file for the Contiki low-layer network stack (NETSTACK)
A MAC framer for IEEE 802.15.4