xref: /btstack/src/mesh/mesh_keys.h (revision a5a7b6da93a734de273f68a4770fffd1d7ae3888)
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 MATTHIAS
24  * RINGWALD 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 typedef struct {
53     btstack_linked_item_t item;
54 
55     // internal index [0..MAX_NR_MESH_NETWORK_KEYS-1]
56     uint16_t internal_index;
57 
58     // index into shared global key list
59     uint16_t netkey_index;
60 
61     // internal version - allows for newer-than relation between keys with same netkey_index
62     uint8_t version;
63 
64     // net_key from provisioner or Config Model Client
65     uint8_t net_key[16];
66 
67     // derived data
68 
69     // k1
70     uint8_t identity_key[16];
71     uint8_t beacon_key[16];
72 
73     // k3
74     uint8_t network_id[8];
75 
76     // k2
77     uint8_t nid;
78     uint8_t encryption_key[16];
79     uint8_t privacy_key[16];
80 
81 } mesh_network_key_t;
82 
83 typedef struct {
84     btstack_linked_list_iterator_t it;
85     mesh_network_key_t * key;
86     uint8_t nid;
87 } mesh_network_key_iterator_t;
88 
89 typedef struct {
90     btstack_linked_item_t item;
91 
92     // internal index [0..MAX_NR_MESH_TRANSPORT_KEYS-1]
93     uint16_t internal_index;
94 
95     // netkey_index of subnet this app key is used with
96     uint16_t netkey_index;
97 
98     // index into shared global app key list
99     uint16_t appkey_index;
100 
101     // app_key
102     uint8_t key[16];
103 
104     // internal version - allows for newer-than relation between keys with same appkey_index
105     uint8_t version;
106 
107     // old key - mark key as 'older' in app key update or startup
108     uint8_t old_key;
109 
110     // application key flag, 0 for device key
111     uint8_t akf;
112 
113     // application key hash id
114     uint8_t aid;
115 
116     // key refresth
117     uint8_t key_refresh;
118 
119 } mesh_transport_key_t;
120 
121 typedef struct {
122     btstack_linked_list_iterator_t it;
123     mesh_transport_key_t * key;
124     uint16_t netkey_index;
125     uint8_t  akf;
126     uint8_t  aid;
127 } mesh_transport_key_iterator_t;
128 
129 /**
130  * @brief Init network key storage
131  */
132 void mesh_network_key_init(void);
133 
134 /**
135  * @brief Get internal index of free network key storage entry
136  * @note index 0 is reserved for primary network key
137  * @returns index or 0u if none found
138  */
139 uint16_t mesh_network_key_get_free_index(void);
140 
141 /**
142  * @brief Add network key to list
143  * @param network_key
144  * @note derivative data k1-k3 need to be already calculated
145  */
146 void mesh_network_key_add(mesh_network_key_t * network_key);
147 
148 /**
149  * @brief Remove network key from list
150  * @param network_key
151  * @return 0 if removed
152  * @note key is only removed from list, memory is not released
153  */
154 int mesh_network_key_remove(mesh_network_key_t * network_key);
155 
156 /**
157  * @brief Get network_key for netkey_index
158  * @param netkey_index
159  * @returns mesh_network_key_t or NULL
160  */
161 mesh_network_key_t * mesh_network_key_list_get(uint16_t netkey_index);
162 
163 /**
164  * @brief Get number of stored network_keys
165  * @returns count
166  */
167 int mesh_network_key_list_count(void);
168 
169 /**
170  * @brief Iterate over all network keys
171  * @param it
172  */
173 void mesh_network_key_iterator_init(mesh_network_key_iterator_t *it);
174 
175 /**
176  * @brief Check if another network_key is available
177  * @param it
178  * @return
179  */
180 int mesh_network_key_iterator_has_more(mesh_network_key_iterator_t *it);
181 
182 /**
183  * @brief Get net network_key
184  * @param it
185  * @return
186  */
187 mesh_network_key_t * mesh_network_key_iterator_get_next(mesh_network_key_iterator_t *it);
188 
189 /**
190  * @brief Iterate over all network keys with a given NID
191  * @param it
192  * @param nid
193  */
194 void mesh_network_key_nid_iterator_init(mesh_network_key_iterator_t *it, uint8_t nid);
195 
196 /**
197  * @brief Check if another network_key with given NID is available
198  * @param it
199  * @return
200  */
201 int mesh_network_key_nid_iterator_has_more(mesh_network_key_iterator_t *it);
202 
203 /**
204  * @brief Get next network_key with given NID
205  * @param it
206  * @return
207  */
208 mesh_network_key_t * mesh_network_key_nid_iterator_get_next(mesh_network_key_iterator_t *it);
209 
210 /**
211  * Transport Keys = Application Keys + Device Key
212  */
213 
214 /**
215  * @brief Set device key
216  * @param device_key
217  */
218 void mesh_transport_set_device_key(const uint8_t * device_key);
219 
220 /**
221  * @brief Get internal index of free transport key storage entry
222  * @note index 0 is reserved for device key
223  * @returns index or 0u if none found
224  */
225 uint16_t mesh_transport_key_get_free_index(void);
226 
227 /**
228  * @brief Add application key to list
229  * @param application key
230  * @note AID needs to be set
231  */
232 void mesh_transport_key_add(mesh_transport_key_t * transport_key);
233 
234 /**
235  * @brief Remove application key from list
236  * @param application key
237  * @return 0 if removed
238  * @note key is only removed from list, memory is not released
239  */
240 int mesh_transport_key_remove(mesh_transport_key_t * transport_key);
241 
242 /**
243  * Get transport key for appkey_index
244  * @param appkey_index
245  * @return
246  */
247 mesh_transport_key_t * mesh_transport_key_get(uint16_t appkey_index);
248 
249 /**
250  * @brief Iterate over all transport keys (AppKeys) for a given netkey index
251  * @param it
252  * @param netkey_index
253  */
254 void mesh_transport_key_iterator_init(mesh_transport_key_iterator_t *it, uint16_t netkey_index);
255 
256 /**
257  * @brief Check if another transport key (AppKey) is available
258  * @param it
259  * @return
260  */
261 int mesh_transport_key_iterator_has_more(mesh_transport_key_iterator_t *it);
262 
263 /**
264  * @brief Get next transport key (AppKey)
265  * @param it
266  * @return
267  */
268 mesh_transport_key_t * mesh_transport_key_iterator_get_next(mesh_transport_key_iterator_t *it);
269 
270 /**
271  * @brief Transport Key Iterator by AID - init
272  * @param it
273  * @param netkey_index
274  * @param akf
275  * @param aid
276  */
277 void mesh_transport_key_aid_iterator_init(mesh_transport_key_iterator_t *it, uint16_t netkey_index, uint8_t akf,
278                                           uint8_t aid);
279 
280 /**
281  * @brief Transport Key Iterator by AID - has more?
282  * @param it
283  * @return
284  */
285 int mesh_transport_key_aid_iterator_has_more(mesh_transport_key_iterator_t *it);
286 
287 /**
288  * @brief Transport Key Iterator by AID - get next
289  * @param it
290  * @return transport key
291  */
292 mesh_transport_key_t * mesh_transport_key_aid_iterator_get_next(mesh_transport_key_iterator_t *it);
293 
294 #ifdef __cplusplus
295 } /* end of extern "C" */
296 #endif
297 
298 #endif
299