ieee802154_security.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2020 Otto-von-Guericke-Universität Magdeburg
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
29 #include <stdint.h>
30 #include "ieee802154.h"
31 #include "crypto/ciphers.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
41 
61  const uint8_t *key,
62  uint8_t key_size);
72  void (*cbc)(const ieee802154_sec_dev_t *dev,
73  uint8_t *cipher,
74  uint8_t *iv,
75  const uint8_t *plain,
76  uint8_t nblocks);
85  void (*ecb)(const ieee802154_sec_dev_t *dev,
86  uint8_t *cipher,
87  const uint8_t *plain,
88  uint8_t nblocks);
90 
102  void *ctx;
103 };
104 
105 #if !defined(CONFIG_IEEE802154_SEC_DEFAULT_KEY) || defined(DOXYGEN)
112 #define CONFIG_IEEE802154_SEC_DEFAULT_KEY "pizza_margherita"
113 #endif
114 
118 #define IEEE802154_SEC_KEY_LENGTH (16U)
119 
123 #define IEEE802154_SEC_BLOCK_SIZE (16U)
124 
128 #define IEEE802154_SEC_MAX_AUX_HDR_LEN (14U)
129 
133 #define IEEE802154_SEC_MAX_MAC_SIZE (16U)
134 
138 #define IEEE802154_SEC_SCF_SECLEVEL_MASK (0x07)
139 
143 #define IEEE802154_SEC_SCF_SECLEVEL_SHIFT (0)
144 
148 #define IEEE802154_SEC_SCF_KEYMODE_MASK (0x18)
149 
153 #define IEEE802154_SEC_SCF_KEYMODE_SHIFT (3)
154 
173 typedef enum {
183 
213 typedef enum {
219 
223 typedef enum {
230 
234 typedef struct ieee802154_sec_context {
242  uint8_t security_level;
246  uint8_t key_id_mode;
250  uint8_t key_index;
260  uint32_t frame_counter;
266 
270 typedef struct __attribute__((packed)) {
288  uint8_t scf;
292  uint32_t fc;
296  uint8_t key_id[];
298 
302 typedef struct __attribute__((packed)) {
306  uint8_t key_index;
308 
312 typedef struct __attribute__((packed)) {
316  uint8_t key_source[4];
320  uint8_t key_index;
322 
326 typedef struct __attribute__((packed)) {
330  uint8_t key_source[IEEE802154_LONG_ADDRESS_LEN];
334  uint8_t key_index;
336 
340 typedef struct __attribute__((packed)) {
344  uint8_t src_addr[IEEE802154_LONG_ADDRESS_LEN];
348  uint32_t frame_counter;
352  uint8_t security_level;
354 
358 typedef struct __attribute__((packed)) {
362  uint8_t flags;
371  uint16_t counter;
373 
380 
399  const uint8_t *header, uint8_t *header_size,
400  uint8_t *payload, uint16_t payload_size,
401  uint8_t *mic, uint8_t *mic_size,
402  const uint8_t *src_address);
403 
423  uint16_t frame_size,
424  uint8_t *header, uint8_t *header_size,
425  uint8_t **payload, uint16_t *payload_size,
426  uint8_t **mic, uint8_t *mic_size,
427  const uint8_t *src_address);
428 
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 
Headers for the packet encryption class.
#define IEEE802154_LONG_ADDRESS_LEN
long address (EUI-64)
Definition: ieee802154.h:42
int ieee802154_sec_encrypt_frame(ieee802154_sec_context_t *ctx, const uint8_t *header, uint8_t *header_size, uint8_t *payload, uint16_t payload_size, uint8_t *mic, uint8_t *mic_size, const uint8_t *src_address)
Encrypt IEEE 802.15.4 frame according to ctx.
ieee802154_sec_scf_seclevel_t
Security levels.
ieee802154_sec_error_t
IEEE 802.15.4 security error codes.
ieee802154_sec_scf_keymode_t
Key identifier modes.
struct ieee802154_radio_cipher_ops ieee802154_radio_cipher_ops_t
Struct of security operations.
const ieee802154_radio_cipher_ops_t ieee802154_radio_cipher_ops
Default descriptor that will fallback to default implementations.
void ieee802154_sec_init(ieee802154_sec_context_t *ctx)
Initialize IEEE 802.15.4 security context with default values.
int ieee802154_sec_decrypt_frame(ieee802154_sec_context_t *ctx, uint16_t frame_size, uint8_t *header, uint8_t *header_size, uint8_t **payload, uint16_t *payload_size, uint8_t **mic, uint8_t *mic_size, const uint8_t *src_address)
Decrypt IEEE 802.15.4 frame according to ctx.
struct ieee802154_sec_context ieee802154_sec_context_t
Struct to hold IEEE 802.15.4 security information.
@ IEEE802154_SEC_SCF_SECLEVEL_MIC32
32 bit MIC
@ IEEE802154_SEC_SCF_SECLEVEL_ENC_MIC32
enc.
@ IEEE802154_SEC_SCF_SECLEVEL_MIC128
128 bit MIC
@ IEEE802154_SEC_SCF_SECLEVEL_MIC64
64 bit MIC
@ IEEE802154_SEC_SCF_SECLEVEL_ENC_MIC64
enc.
@ IEEE802154_SEC_SCF_SECLEVEL_ENC_MIC128
enc.
@ IEEE802154_SEC_SCF_SECLEVEL_NONE
no security
@ IEEE802154_SEC_SCF_SECLEVEL_ENC
encryption
@ IEEE802154_SEC_OK
Everything went fine.
@ IEEE802154_SEC_UNSUPORTED
Unsupported operation.
@ IEEE802154_SEC_MAC_CHECK_FAILURE
The computet MAC did not match.
@ IEEE802154_SEC_FRAME_COUNTER_OVERFLOW
The requested operation would let the frame counter overflow.
@ IEEE802154_SEC_NO_KEY
Could not find the key to perform a requested cipher operation.
@ IEEE802154_SEC_SCF_KEYMODE_IMPLICIT
Key is determined implicitly.
@ IEEE802154_SEC_SCF_KEYMODE_INDEX
Key is determined from key index.
@ IEEE802154_SEC_SCF_KEYMODE_SHORT_INDEX
Key is determined from 4 byte key source and key index.
@ IEEE802154_SEC_SCF_KEYMODE_HW_INDEX
Key is determined from 8 byte key source and key index.
basic struct for using block ciphers contains the cipher interface and the context
Definition: ciphers.h:115
Struct of security operations.
void(* ecb)(const ieee802154_sec_dev_t *dev, uint8_t *cipher, const uint8_t *plain, uint8_t nblocks)
Function type to perform ECB encryption.
void(* cbc)(const ieee802154_sec_dev_t *dev, uint8_t *cipher, uint8_t *iv, const uint8_t *plain, uint8_t nblocks)
Function type to compute CBC-MAC.
void(* set_key)(ieee802154_sec_dev_t *dev, const uint8_t *key, uint8_t key_size)
Function to set the encryption key for the next cipher operation.
Content of key_source if key mode is IEEE802154_SEC_SCF_KEYMODE_INDEX.
uint8_t key_index
Key index of key from originator, defined by key source.
Content of key_source if key mode is IEEE802154_SEC_SCF_KEYMODE_SHORT_INDEX.
uint8_t key_index
Key index of key from originator, defined by key source.
Content of key_source if key mode is IEEE802154_SEC_SCF_KEYMODE_HW_INDEX.
uint8_t key_index
Key index of key from originator, defined by key source.
IEEE 802.15.4 auxiliary security header.
uint8_t scf
Security Control field (SCF)
uint32_t fc
frame counter
Format of 16 byte input block of CCM.
ieee802154_sec_ccm_nonce_t nonce
Nonce (Number that is only used once)
uint16_t counter
Either the length of the actual message (for CBC-MAC) or a block counter (for CTR)
Format of 13 byte nonce.
uint32_t frame_counter
Frame counter.
uint8_t security_level
One of IEEE802154_SEC_SCF_SECLEVEL_*.
Struct to hold IEEE 802.15.4 security information.
uint8_t key_index
Key index.
ieee802154_sec_dev_t dev
802.15.4 security dev
uint8_t key_id_mode
Key mode IEEE802154_SEC_SCF_KEYMODE_*.
uint32_t frame_counter
Own frame counter.
cipher_t cipher
Cipher context with AES128 interface and key storage.
uint8_t key_source[IEEE802154_LONG_ADDRESS_LEN]
Key source.
uint8_t security_level
Security level IEEE802154_SEC_SCF_SECLEVEL_*.
IEEE 802.15.4 security device descriptor.
const struct ieee802154_radio_cipher_ops * cipher_ops
Pointer to the operations of the device.
void * ctx
Pointer to the context of the device.
IEEE 802.15.4 header definitions.