xref: /btstack/src/mesh/mesh_keys.h (revision bc6a318f2177319997f3b7da7b6f161b4ec94fed)
1 /*
2  * Copyright (C) 2018 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #ifndef MESH_KEYS_H
39 #define MESH_KEYS_H
40 
41 #include <stdint.h>
42 
43 #include "btstack_linked_list.h"
44 
45 #include "mesh/adv_bearer.h"
46 
47 #ifdef __cplusplus
48 extern "C"
49 {
50 #endif
51 
52 #define MESH_KEYS_INVALID_INDEX 0xffff
53 
54 typedef struct {
55     btstack_linked_item_t item;
56 
57     // internal index [0..MAX_NR_MESH_NETWORK_KEYS-1]
58     uint16_t internal_index;
59 
60     // index into shared global key list
61     uint16_t netkey_index;
62 
63     // internal version - allows for newer-than relation between keys with same netkey_index
64     uint8_t version;
65 
66     // net_key from provisioner or Config Model Client
67     uint8_t net_key[16];
68 
69     // derived data
70 
71     // k1
72     uint8_t identity_key[16];
73     uint8_t beacon_key[16];
74 
75     // k3
76     uint8_t network_id[8];
77 
78     // k2
79     uint8_t nid;
80     uint8_t encryption_key[16];
81     uint8_t privacy_key[16];
82 
83 } mesh_network_key_t;
84 
85 typedef struct {
86     btstack_linked_list_iterator_t it;
87     mesh_network_key_t * key;
88     uint8_t nid;
89 } mesh_network_key_iterator_t;
90 
91 typedef struct {
92     btstack_linked_item_t item;
93 
94     // internal index [0..MAX_NR_MESH_TRANSPORT_KEYS-1]
95     uint16_t internal_index;
96 
97     // netkey_index of subnet this app key is used with
98     uint16_t netkey_index;
99 
100     // index into shared global app key list
101     uint16_t appkey_index;
102 
103     // app_key
104     uint8_t key[16];
105 
106     // internal version - allows for newer-than relation between keys with same appkey_index
107     uint8_t version;
108 
109     // old key - mark key as 'older' in app key update or startup
110     uint8_t old_key;
111 
112     // application key flag, 0 for device key
113     uint8_t akf;
114 
115     // application key hash id
116     uint8_t aid;
117 
118     // key refresth
119     uint8_t key_refresh;
120 
121 } mesh_transport_key_t;
122 
123 typedef struct {
124     btstack_linked_list_iterator_t it;
125     mesh_transport_key_t * key;
126     uint16_t netkey_index;
127     uint8_t  akf;
128     uint8_t  aid;
129 } mesh_transport_key_iterator_t;
130 
131 /**
132  * @brief Init network key storage
133  */
134 void mesh_network_key_init(void);
135 
136 /**
137  * @brief Get internal index of free network key storage entry
138  * @note index 0 is reserved for primary network key
139  * @return index or MESH_KEYS_INVALID_INDEX if none found
140  */
141 uint16_t mesh_network_key_get_free_index(void);
142 
143 /**
144  * @brief Add network key to list
145  * @param network_key
146  * @note derivative data k1-k3 need to be already calculated
147  */
148 void mesh_network_key_add(mesh_network_key_t * network_key);
149 
150 /**
151  * @brief Remove network key from list
152  * @param network_key
153  * @return true if removed
154  * @note key is only removed from list, memory is not released
155  */
156 bool mesh_network_key_remove(mesh_network_key_t * network_key);
157 
158 /**
159  * @brief Get network_key for netkey_index
160  * @param netkey_index
161  * @return mesh_network_key_t or NULL
162  */
163 mesh_network_key_t * mesh_network_key_list_get(uint16_t netkey_index);
164 
165 /**
166  * @brief Get number of stored network_keys
167  * @return count
168  */
169 int mesh_network_key_list_count(void);
170 
171 /**
172  * @brief Iterate over all network keys
173  * @param it
174  */
175 void mesh_network_key_iterator_init(mesh_network_key_iterator_t *it);
176 
177 /**
178  * @brief Check if another network_key is available
179  * @param it
180  * @return
181  */
182 int mesh_network_key_iterator_has_more(mesh_network_key_iterator_t *it);
183 
184 /**
185  * @brief Get net network_key
186  * @param it
187  * @return
188  */
189 mesh_network_key_t * mesh_network_key_iterator_get_next(mesh_network_key_iterator_t *it);
190 
191 /**
192  * @brief Iterate over all network keys with a given NID
193  * @param it
194  * @param nid
195  */
196 void mesh_network_key_nid_iterator_init(mesh_network_key_iterator_t *it, uint8_t nid);
197 
198 /**
199  * @brief Check if another network_key with given NID is available
200  * @param it
201  * @return
202  */
203 int mesh_network_key_nid_iterator_has_more(mesh_network_key_iterator_t *it);
204 
205 /**
206  * @brief Get next network_key with given NID
207  * @param it
208  * @return
209  */
210 mesh_network_key_t * mesh_network_key_nid_iterator_get_next(mesh_network_key_iterator_t *it);
211 
212 /**
213  * Transport Keys = Application Keys + Device Key
214  */
215 
216 /**
217  * @brief Set device key
218  * @param device_key
219  */
220 void mesh_transport_set_device_key(const uint8_t * device_key);
221 
222 /**
223  * @brief Get internal index of free transport key storage entry
224  * @note index 0 is reserved for device key
225  * @return index or 0u if none found
226  */
227 uint16_t mesh_transport_key_get_free_index(void);
228 
229 /**
230  * @brief Add application key to list
231  * @param application key
232  * @note AID needs to be set
233  */
234 void mesh_transport_key_add(mesh_transport_key_t * transport_key);
235 
236 /**
237  * @brief Remove application key from list
238  * @param application key
239  * @return 0 if removed
240  * @note key is only removed from list, memory is not released
241  */
242 bool mesh_transport_key_remove(mesh_transport_key_t * transport_key);
243 
244 /**
245  * Get transport key for appkey_index
246  * @param appkey_index
247  * @return
248  */
249 mesh_transport_key_t * mesh_transport_key_get(uint16_t appkey_index);
250 
251 /**
252  * @brief Iterate over all transport keys (AppKeys) for a given netkey index
253  * @param it
254  * @param netkey_index
255  */
256 void mesh_transport_key_iterator_init(mesh_transport_key_iterator_t *it, uint16_t netkey_index);
257 
258 /**
259  * @brief Check if another transport key (AppKey) is available
260  * @param it
261  * @return
262  */
263 int mesh_transport_key_iterator_has_more(mesh_transport_key_iterator_t *it);
264 
265 /**
266  * @brief Get next transport key (AppKey)
267  * @param it
268  * @return
269  */
270 mesh_transport_key_t * mesh_transport_key_iterator_get_next(mesh_transport_key_iterator_t *it);
271 
272 /**
273  * @brief Transport Key Iterator by AID - init
274  * @param it
275  * @param netkey_index
276  * @param akf
277  * @param aid
278  */
279 void mesh_transport_key_aid_iterator_init(mesh_transport_key_iterator_t *it, uint16_t netkey_index, uint8_t akf,
280                                           uint8_t aid);
281 
282 /**
283  * @brief Transport Key Iterator by AID - has more?
284  * @param it
285  * @return
286  */
287 int mesh_transport_key_aid_iterator_has_more(mesh_transport_key_iterator_t *it);
288 
289 /**
290  * @brief Transport Key Iterator by AID - get next
291  * @param it
292  * @return transport key
293  */
294 mesh_transport_key_t * mesh_transport_key_aid_iterator_get_next(mesh_transport_key_iterator_t *it);
295 
296 #ifdef __cplusplus
297 } /* end of extern "C" */
298 #endif
299 
300 #endif
301