57#if LLSEC802154_ENABLED && !LLSEC802154_USES_EXPLICIT_KEYS
58#error LLSEC802154_ENABLED set but LLSEC802154_USES_EXPLICIT_KEYS unset
65static aes_key keys[] = {
69#define N_KEYS CC_ARRAY_LENGTH(keys)
73tsch_security_init_nonce(uint8_t *nonce,
74 const linkaddr_t *sender,
struct tsch_asn_t *asn)
76 memcpy(nonce, sender, 8);
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;
87 uint8_t required_security_level;
88 uint8_t required_key_index;
98 return !(tsch_is_associated == 1 && tsch_is_pan_secured == 1);
102 if(tsch_is_associated == 1 && tsch_is_pan_secured == 0) {
108 case FRAME802154_BEACONFRAME:
109 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_EB;
110 required_key_index = TSCH_SECURITY_KEY_INDEX_EB;
112 case FRAME802154_ACKFRAME:
113 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_ACK;
114 required_key_index = TSCH_SECURITY_KEY_INDEX_ACK;
117 required_security_level = TSCH_SECURITY_KEY_SEC_LEVEL_OTHER;
118 required_key_index = TSCH_SECURITY_KEY_INDEX_OTHER;
122 required_security_level) &&
138 int hdrlen,
int datalen,
struct tsch_asn_t *asn)
141 uint8_t key_index = 0;
142 uint8_t security_level = 0;
143 uint8_t with_encryption;
146 struct ieee802154_ies ies;
151 if(hdr == NULL || outbuf == NULL || hdrlen < 0 || datalen < 0) {
160 memset(&ies, 0,
sizeof(ies));
161 if(frame802154e_parse_information_elements(hdr + hdrlen, datalen, &ies) > 0) {
163 hdrlen += ies.ie_payload_ie_offset;
164 datalen -= ies.ie_payload_ie_offset;
175 with_encryption = (security_level & 0x4) ? 1 : 0;
178 if(key_index == 0 || key_index > N_KEYS) {
184 if(with_encryption) {
188 a_len = hdrlen + datalen;
194 memcpy(outbuf, hdr, a_len + m_len);
197 CCM_STAR.set_key(keys[key_index - 1]);
200 outbuf + a_len, m_len,
202 outbuf + hdrlen + datalen, mic_len, 1);
212 uint8_t generated_mic[16];
213 uint8_t key_index = 0;
214 uint8_t security_level = 0;
215 uint8_t with_encryption;
220 struct ieee802154_ies ies;
222 if(frame == NULL || hdr == NULL || hdrlen < 0 || datalen < 0) {
226 if(!tsch_security_check_level(frame)) {
238 with_encryption = (security_level & 0x4) ? 1 : 0;
242 if(key_index == 0 || key_index > N_KEYS) {
246 memset(&ies, 0,
sizeof(ies));
247 (void)frame802154e_parse_information_elements(hdr + hdrlen, datalen, &ies);
249 hdrlen += ies.ie_payload_ie_offset;
250 datalen -= ies.ie_payload_ie_offset;
252 tsch_security_init_nonce(nonce, sender, asn);
254 if(with_encryption) {
258 a_len = hdrlen + datalen;
262 CCM_STAR.set_key(keys[key_index - 1]);
265 (uint8_t *)hdr + a_len, m_len,
266 (uint8_t *)hdr, a_len,
267 generated_mic, mic_len, 0);
269 if(mic_len > 0 && memcmp(generated_mic, hdr + hdrlen + datalen, mic_len) != 0) {
279#if LLSEC802154_ENABLED
280 if(tsch_is_pan_secured) {
283 case FRAME802154_ACKFRAME:
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);
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);
Default definitions of C compiler quirk work-arounds.
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.
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.
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 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.
Main API declarations for TSCH.