11#include "../include/mesh/mesh.h"
18int bt_mesh_aes_cmac(
const uint8_t key[16],
struct bt_mesh_sg *sg,
19 size_t sg_len, uint8_t mac[16]);
21static inline int bt_mesh_aes_cmac_one(
const uint8_t key[16],
const void *m,
22 size_t len, uint8_t mac[16])
24 struct bt_mesh_sg sg = { m, len };
26 return bt_mesh_aes_cmac(key, &sg, 1, mac);
29static inline bool bt_mesh_s1(
const char *m, uint8_t salt[16])
31 const uint8_t zero[16] = { 0 };
33 return bt_mesh_aes_cmac_one(zero, m, strlen(m), salt);
36int bt_mesh_k1(
const uint8_t *ikm,
size_t ikm_len,
const uint8_t salt[16],
37 const char *info, uint8_t okm[16]);
39#define bt_mesh_k1_str(ikm, ikm_len, salt_str, info, okm) \
41 const uint8_t salt[16] = salt_str; \
42 bt_mesh_k1(ikm, ikm_len, salt, info, okm); \
45int bt_mesh_k2(
const uint8_t n[16],
const uint8_t *p,
size_t p_len,
46 uint8_t net_id[1], uint8_t enc_key[16], uint8_t priv_key[16]);
48int bt_mesh_k3(
const uint8_t n[16], uint8_t out[8]);
50int bt_mesh_k4(
const uint8_t n[16], uint8_t out[1]);
52int bt_mesh_id128(
const uint8_t n[16],
const char *s, uint8_t out[16]);
54static inline int bt_mesh_id_resolving_key(
const uint8_t net_key[16],
55 uint8_t resolving_key[16])
57 return bt_mesh_k1_str(net_key, 16,
"smbt",
"smbi", resolving_key);
60static inline int bt_mesh_identity_key(
const uint8_t net_key[16],
61 uint8_t identity_key[16])
63 return bt_mesh_id128(net_key,
"nkik", identity_key);
66static inline int bt_mesh_beacon_key(
const uint8_t net_key[16],
67 uint8_t beacon_key[16])
69 return bt_mesh_id128(net_key,
"nkbk", beacon_key);
72int bt_mesh_beacon_auth(
const uint8_t beacon_key[16], uint8_t flags,
73 const uint8_t net_id[8], uint32_t iv_index,
76static inline int bt_mesh_app_id(
const uint8_t app_key[16], uint8_t app_id[1])
78 return bt_mesh_k4(app_key, app_id);
81static inline int bt_mesh_session_key(
const uint8_t dhkey[32],
82 const uint8_t prov_salt[16],
83 uint8_t session_key[16])
85 return bt_mesh_k1(dhkey, 32, prov_salt,
"prsk", session_key);
88static inline int bt_mesh_prov_nonce(
const uint8_t dhkey[32],
89 const uint8_t prov_salt[16],
95 err = bt_mesh_k1(dhkey, 32, prov_salt,
"prsn", tmp);
97 memcpy(nonce, tmp + 3, 13);
103static inline int bt_mesh_dev_key(
const uint8_t dhkey[32],
104 const uint8_t prov_salt[16],
107 return bt_mesh_k1(dhkey, 32, prov_salt,
"prdk", dev_key);
110static inline int bt_mesh_prov_salt(
const uint8_t conf_salt[16],
111 const uint8_t prov_rand[16],
112 const uint8_t dev_rand[16],
113 uint8_t prov_salt[16])
115 const uint8_t prov_salt_key[16] = { 0 };
116 struct bt_mesh_sg sg[] = {
122 return bt_mesh_aes_cmac(prov_salt_key, sg, ARRAY_SIZE(sg), prov_salt);
125int bt_mesh_net_obfuscate(uint8_t *pdu, uint32_t iv_index,
126 const uint8_t privacy_key[16]);
128int bt_mesh_net_encrypt(
const uint8_t key[16],
struct os_mbuf *buf,
129 uint32_t iv_index,
bool proxy);
131int bt_mesh_net_decrypt(
const uint8_t key[16],
struct os_mbuf *buf,
132 uint32_t iv_index,
bool proxy);
134struct bt_mesh_app_crypto_ctx {
144int bt_mesh_app_encrypt(
const uint8_t key[16],
145 const struct bt_mesh_app_crypto_ctx *ctx,
148int bt_mesh_app_decrypt(
const uint8_t key[16],
149 const struct bt_mesh_app_crypto_ctx *ctx,
152uint8_t bt_mesh_fcs_calc(
const uint8_t *data, uint8_t data_len);
154bool bt_mesh_fcs_check(
struct os_mbuf *buf, uint8_t received_fcs);
156int bt_mesh_virtual_addr(
const uint8_t virtual_label[16], uint16_t *addr);
158int bt_mesh_prov_conf_salt(
const uint8_t conf_inputs[145], uint8_t salt[16]);
160int bt_mesh_prov_conf_key(
const uint8_t dhkey[32],
const uint8_t conf_salt[16],
161 uint8_t conf_key[16]);
163int bt_mesh_prov_conf(
const uint8_t conf_key[16],
const uint8_t rand[16],
164 const uint8_t auth[16], uint8_t conf[16]);
166int bt_mesh_prov_decrypt(
const uint8_t key[16], uint8_t nonce[13],
167 const uint8_t data[25 + 8], uint8_t out[25]);
169int bt_mesh_prov_encrypt(
const uint8_t key[16], uint8_t nonce[13],
170 const uint8_t data[25], uint8_t out[25 + 8]);