1 /* Bluetooth Mesh */
2
3 /*
4 * Copyright (c) 2017 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 struct bt_mesh_sg {
10 const void *data;
11 size_t len;
12 };
13
14 int bt_mesh_aes_cmac(const u8_t key[16], struct bt_mesh_sg *sg,
15 size_t sg_len, u8_t mac[16]);
16
bt_mesh_aes_cmac_one(const u8_t key[16],const void * m,size_t len,u8_t mac[16])17 static inline int bt_mesh_aes_cmac_one(const u8_t key[16], const void *m,
18 size_t len, u8_t mac[16])
19 {
20 struct bt_mesh_sg sg = { m, len };
21
22 return bt_mesh_aes_cmac(key, &sg, 1, mac);
23 }
24
bt_mesh_s1(const char * m,u8_t salt[16])25 static inline bool bt_mesh_s1(const char *m, u8_t salt[16])
26 {
27 const u8_t zero[16] = { 0 };
28
29 return bt_mesh_aes_cmac_one(zero, m, strlen(m), salt);
30 }
31
32 int bt_mesh_k1(const u8_t *ikm, size_t ikm_len, const u8_t salt[16],
33 const char *info, u8_t okm[16]);
34
35 #define bt_mesh_k1_str(ikm, ikm_len, salt_str, info, okm) \
36 ({ \
37 const u8_t salt[16] = salt_str; \
38 bt_mesh_k1(ikm, ikm_len, salt, info, okm); \
39 })
40
41 int bt_mesh_k2(const u8_t n[16], const u8_t *p, size_t p_len,
42 u8_t net_id[1], u8_t enc_key[16], u8_t priv_key[16]);
43
44 int bt_mesh_k3(const u8_t n[16], u8_t out[8]);
45
46 int bt_mesh_k4(const u8_t n[16], u8_t out[1]);
47
48 int bt_mesh_id128(const u8_t n[16], const char *s, u8_t out[16]);
49
bt_mesh_id_resolving_key(const u8_t net_key[16],u8_t resolving_key[16])50 static inline int bt_mesh_id_resolving_key(const u8_t net_key[16],
51 u8_t resolving_key[16])
52 {
53 return bt_mesh_k1_str(net_key, 16, "smbt", "smbi", resolving_key);
54 }
55
bt_mesh_identity_key(const u8_t net_key[16],u8_t identity_key[16])56 static inline int bt_mesh_identity_key(const u8_t net_key[16],
57 u8_t identity_key[16])
58 {
59 return bt_mesh_id128(net_key, "nkik", identity_key);
60 }
61
bt_mesh_beacon_key(const u8_t net_key[16],u8_t beacon_key[16])62 static inline int bt_mesh_beacon_key(const u8_t net_key[16],
63 u8_t beacon_key[16])
64 {
65 return bt_mesh_id128(net_key, "nkbk", beacon_key);
66 }
67
68 int bt_mesh_beacon_auth(const u8_t beacon_key[16], u8_t flags,
69 const u8_t net_id[16], bt_u32_t iv_index,
70 u8_t auth[8]);
71
bt_mesh_app_id(const u8_t app_key[16],u8_t app_id[1])72 static inline int bt_mesh_app_id(const u8_t app_key[16], u8_t app_id[1])
73 {
74 return bt_mesh_k4(app_key, app_id);
75 }
76
bt_mesh_session_key(const u8_t dhkey[32],const u8_t prov_salt[16],u8_t session_key[16])77 static inline int bt_mesh_session_key(const u8_t dhkey[32],
78 const u8_t prov_salt[16],
79 u8_t session_key[16])
80 {
81 return bt_mesh_k1(dhkey, 32, prov_salt, "prsk", session_key);
82 }
83
bt_mesh_prov_nonce(const u8_t dhkey[32],const u8_t prov_salt[16],u8_t nonce[13])84 static inline int bt_mesh_prov_nonce(const u8_t dhkey[32],
85 const u8_t prov_salt[16],
86 u8_t nonce[13])
87 {
88 u8_t tmp[16];
89 int err;
90
91 err = bt_mesh_k1(dhkey, 32, prov_salt, "prsn", tmp);
92 if (!err) {
93 memcpy(nonce, tmp + 3, 13);
94 }
95
96 return err;
97 }
98
bt_mesh_dev_key(const u8_t dhkey[32],const u8_t prov_salt[16],u8_t dev_key[16])99 static inline int bt_mesh_dev_key(const u8_t dhkey[32],
100 const u8_t prov_salt[16],
101 u8_t dev_key[16])
102 {
103 return bt_mesh_k1(dhkey, 32, prov_salt, "prdk", dev_key);
104 }
105
bt_mesh_prov_salt(const u8_t conf_salt[16],const u8_t prov_rand[16],const u8_t dev_rand[16],u8_t prov_salt[16])106 static inline int bt_mesh_prov_salt(const u8_t conf_salt[16],
107 const u8_t prov_rand[16],
108 const u8_t dev_rand[16],
109 u8_t prov_salt[16])
110 {
111 const u8_t prov_salt_key[16] = { 0 };
112 struct bt_mesh_sg sg[] = {
113 { conf_salt, 16 },
114 { prov_rand, 16 },
115 { dev_rand, 16 },
116 };
117
118 return bt_mesh_aes_cmac(prov_salt_key, sg, ARRAY_SIZE(sg), prov_salt);
119 }
120
121 int bt_mesh_net_obfuscate(u8_t *pdu, bt_u32_t iv_index,
122 const u8_t privacy_key[16]);
123
124 int bt_mesh_net_encrypt(const u8_t key[16], struct net_buf_simple *buf,
125 bt_u32_t iv_index, bool proxy);
126
127 int bt_mesh_net_decrypt(const u8_t key[16], struct net_buf_simple *buf,
128 bt_u32_t iv_index, bool proxy);
129
130 int bt_mesh_app_encrypt(const u8_t key[16], bool dev_key, u8_t aszmic,
131 struct net_buf_simple *buf, const u8_t *ad,
132 u16_t src, u16_t dst, bt_u32_t seq_num, bt_u32_t iv_index);
133
134 int bt_mesh_app_decrypt(const u8_t key[16], bool dev_key, u8_t aszmic,
135 struct net_buf_simple *buf, struct net_buf_simple *out,
136 const u8_t *ad, u16_t src, u16_t dst, bt_u32_t seq_num,
137 bt_u32_t iv_index);
138
139 u8_t bt_mesh_fcs_calc(const u8_t *data, u8_t data_len);
140
141 bool bt_mesh_fcs_check(struct net_buf_simple *buf, u8_t received_fcs);
142
143 int bt_mesh_virtual_addr(const u8_t virtual_label[16], u16_t *addr);
144
145 int bt_mesh_prov_conf_salt(const u8_t conf_inputs[145], u8_t salt[16]);
146
147 int bt_mesh_prov_conf_key(const u8_t dhkey[32], const u8_t conf_salt[16],
148 u8_t conf_key[16]);
149
150 int bt_mesh_prov_conf(const u8_t conf_key[16], const u8_t rand[16],
151 const u8_t auth[16], u8_t conf[16]);
152
153 int bt_mesh_prov_decrypt(const u8_t key[16], u8_t nonce[13],
154 const u8_t data[25 + 8], u8_t out[25]);
155
156 int bt_mesh_prov_encrypt(const u8_t key[16], u8_t nonce[13],
157 const u8_t data[25], u8_t out[33]);
158