esp-sdr-zephyr
Raw I/Q receive and transmit on the ESP32-S3/C6 Wi-Fi radio, for Zephyr
Loading...
Searching...
No Matches
zb_crypto.h
1/*
2 * SPDX-License-Identifier: GPL-3.0-or-later
3 *
4 * The cryptography Zigbee needs: AES-128, CCM* with a 4 byte MIC (security level 5) and the
5 * Matyas-Meyer-Oseas hash with its keyed form for deriving link keys. Portable C. The AES is a
6 * plain table implementation: it does not run in constant time, which is no issue for keys that
7 * only ever cross a radio link at this duty cycle.
8 */
9#ifndef ZIGBEE_ZB_CRYPTO_H_
10#define ZIGBEE_ZB_CRYPTO_H_
11
12#include <stddef.h>
13#include <stdint.h>
14
15#define ZB_KEY_LEN 16
16#define ZB_MIC_LEN 4
17#define ZB_NONCE_LEN 13
18
19/* One block, ECB. */
20void zb_aes128_encrypt(const uint8_t key[ZB_KEY_LEN], const uint8_t in[16], uint8_t out[16]);
21
22/*
23 * CCM* with M = 4, L = 2: encrypt @p data in place and write the MIC over @p aad and the plain
24 * text. @p aad may be empty.
25 */
26void zb_ccm_encrypt(const uint8_t key[ZB_KEY_LEN], const uint8_t nonce[ZB_NONCE_LEN],
27 const uint8_t *aad, size_t aad_len, uint8_t *data, size_t len,
28 uint8_t mic[ZB_MIC_LEN]);
29
30/* Decrypt in place and check the MIC; 0, or -EBADMSG (the data is then garbage). */
31int zb_ccm_decrypt(const uint8_t key[ZB_KEY_LEN], const uint8_t nonce[ZB_NONCE_LEN],
32 const uint8_t *aad, size_t aad_len, uint8_t *data, size_t len,
33 const uint8_t mic[ZB_MIC_LEN]);
34
35/* Matyas-Meyer-Oseas hash with AES-128 (Zigbee 4.5.? / B.6), 16 byte digest. */
36void zb_mmo_hash(const uint8_t *msg, size_t len, uint8_t out[16]);
37
38/* The keyed hash of the Zigbee spec (HMAC with the MMO hash), for a 16 byte key. */
39void zb_keyed_hash(const uint8_t key[ZB_KEY_LEN], const uint8_t *msg, size_t len, uint8_t out[16]);
40
41/* The key-transport key derived from a link key (keyed hash of the single byte 0x00). */
42void zb_key_transport_key(const uint8_t link_key[ZB_KEY_LEN], uint8_t out[ZB_KEY_LEN]);
43
44#endif /* ZIGBEE_ZB_CRYPTO_H_ */