esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
zb_frame.h
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 *
4 * Zigbee NWK and APS frames and the security header they share: parse, build, protect and open
5 * (CCM* with a 4 byte MIC). Portable C. The layouts are Zigbee PRO's (05-3474); the security
6 * control field carries security level 0 on air and the real level (5) in the nonce and in
7 * the authenticated header.
8 */
9#ifndef ZIGBEE_ZB_FRAME_H_
10#define ZIGBEE_ZB_FRAME_H_
11
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdint.h>
15
16#include <zigbee/zb_crypto.h>
17
18/* NWK frame types. */
19#define ZB_NWK_DATA 0
20#define ZB_NWK_COMMAND 1
21
22/* NWK command ids. */
23#define ZB_NWKC_ROUTE_REQUEST 0x01
24#define ZB_NWKC_ROUTE_REPLY 0x02
25#define ZB_NWKC_NETWORK_STATUS 0x03
26#define ZB_NWKC_LEAVE 0x04
27#define ZB_NWKC_REJOIN_REQ 0x06
28#define ZB_NWKC_REJOIN_RSP 0x07
29#define ZB_NWKC_LINK_STATUS 0x08
30
31/* NWK broadcast addresses. */
32#define ZB_BCAST_ALL 0xFFFF
33#define ZB_BCAST_RX_ON_IDLE 0xFFFD
34#define ZB_BCAST_ROUTERS 0xFFFC
35
36/* Radius for application data (twice the network depth, as Z-Stack does). */
37#define ZB_NWK_RADIUS 30
38
39struct zb_nwk_hdr {
40 uint8_t type;
41 uint8_t proto_ver;
42 uint8_t discover;
43 bool security;
44 bool multicast;
45 bool src_route;
46 bool has_ext_dst, has_ext_src;
47 uint16_t dst, src;
48 uint8_t radius, seq;
49 uint8_t ext_dst[8], ext_src[8];
50 /* Header length in bytes, up to the (security header or) payload. */
51 uint8_t len;
52};
53
54/* -EINVAL when truncated; source routes are skipped, not interpreted. */
55int zb_nwk_parse(const uint8_t *f, size_t n, struct zb_nwk_hdr *h);
56/* Returns the length (the optional fields are written when flagged). */
57size_t zb_nwk_build(const struct zb_nwk_hdr *h, uint8_t *out);
58
59/* APS frame types and delivery modes. */
60#define ZB_APS_DATA 0
61#define ZB_APS_COMMAND 1
62#define ZB_APS_ACK 2
63#define ZB_APS_UNICAST 0
64#define ZB_APS_BROADCAST 2
65#define ZB_APS_GROUP 3
66
67#define ZB_APSC_TRANSPORT_KEY 0x05
68#define ZB_APSC_REQUEST_KEY 0x08
69
70struct zb_aps_hdr {
71 uint8_t type;
72 uint8_t delivery;
73 bool ack_format, security, ack_req, ext_hdr;
74 uint8_t dst_ep, src_ep;
75 uint16_t group, cluster, profile;
76 uint8_t counter;
77 uint8_t len;
78};
79
80int zb_aps_parse(const uint8_t *f, size_t n, struct zb_aps_hdr *h);
81size_t zb_aps_build(const struct zb_aps_hdr *h, uint8_t *out);
82
83/*
84 * The auxiliary security header: control (key id, extended nonce), frame counter, the sender's
85 * IEEE address and, for the network key, its sequence number.
86 */
87#define ZB_KEYID_DATA 0
88#define ZB_KEYID_NETWORK 1
89#define ZB_KEYID_TRANSPORT 2
90#define ZB_KEYID_LOAD 3
91
92struct zb_sec_hdr {
93 uint8_t key_id;
94 uint32_t counter;
95 uint8_t src[8];
96 uint8_t key_seq;
97 uint8_t len;
98};
99
100#define ZB_SEC_MAX_LEN 14
101
102/*
103 * Protect a frame: @p buf holds @p hdr_len bytes of header (security flag set by the caller)
104 * followed by @p plen bytes of plain payload. The auxiliary header goes between them, the
105 * payload is encrypted and the MIC appended. Returns the new length, 0 if it does not fit
106 * @p cap.
107 */
108size_t zb_sec_protect(uint8_t *buf, size_t cap, size_t hdr_len, size_t plen,
109 const uint8_t key[ZB_KEY_LEN], uint8_t key_id, uint8_t key_seq,
110 uint32_t counter, const uint8_t src[8]);
111
112/*
113 * Parse the auxiliary header at @p hdr_len (without checking the MIC): -EINVAL if malformed.
114 */
115int zb_sec_parse(const uint8_t *f, size_t n, size_t hdr_len, struct zb_sec_hdr *s);
116
117/*
118 * Verify and decrypt in place with @p key. On success the plain payload is at
119 * buf + hdr_len + s->len, of *plen bytes. -EBADMSG when the MIC fails.
120 */
121int zb_sec_open(uint8_t *buf, size_t n, size_t hdr_len, const uint8_t key[ZB_KEY_LEN],
122 const struct zb_sec_hdr *s, size_t *plen);
123
124/* The APS Transport-Key command payload for a standard network key; returns its length (35). */
125size_t zb_aps_transport_key_payload(uint8_t *out, const uint8_t key[ZB_KEY_LEN], uint8_t key_seq,
126 const uint8_t dst[8], const uint8_t src[8]);
127
128#endif /* ZIGBEE_ZB_FRAME_H_ */
Definition zb_frame.h:70
Definition zb_frame.h:39
Definition zb_frame.h:92