xref: /btstack/src/mesh/provisioning_device.c (revision a5a7b6da93a734de273f68a4770fffd1d7ae3888)
1 /*
2  * Copyright (C) 2017 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 #define __BTSTACK_FILE__ "provisioning_device.c"
39 
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "btstack.h"
46 #include "btstack_memory.h"
47 
48 #include "mesh/mesh_crypto.h"
49 #include "mesh/pb_adv.h"
50 #include "mesh/pb_gatt.h"
51 #include "mesh/provisioning.h"
52 
53 static void provisioning_attention_timer_set(void);
54 static void prov_key_generated(void * arg);
55 
56 // remote ecc
57 static uint8_t remote_ec_q[64];
58 static uint8_t dhkey[32];
59 
60 static btstack_packet_handler_t prov_packet_handler;
61 
62 static uint8_t  prov_buffer_out[MESH_PROV_MAX_PROXY_PDU];
63 // ConfirmationInputs = ProvisioningInvitePDUValue || ProvisioningCapabilitiesPDUValue || ProvisioningStartPDUValue || PublicKeyProvisioner || PublicKeyDevice
64 static uint8_t  prov_confirmation_inputs[1 + 11 + 5 + 64 + 64];
65 static uint8_t  prov_authentication_method;
66 static uint8_t  prov_public_key_oob_used;
67 static uint8_t  prov_emit_public_key_oob_active;
68 static uint8_t  prov_emit_output_oob_active;
69 static uint8_t  prov_ec_q[64];
70 
71 static const uint8_t * prov_public_key_oob_q;
72 static const uint8_t * prov_public_key_oob_d;
73 
74 // num elements
75 static uint8_t  prov_num_elements = 1;
76 
77 // capabilites
78 static const uint8_t * prov_static_oob_data;
79 
80 static uint16_t  prov_static_oob_len;
81 static uint16_t  prov_output_oob_actions;
82 static uint16_t  prov_input_oob_actions;
83 static uint8_t   prov_public_key_oob_available;
84 static uint8_t   prov_static_oob_available;
85 static uint8_t   prov_output_oob_size;
86 static uint8_t   prov_input_oob_size;
87 static uint8_t   prov_error_code;
88 static uint8_t   prov_waiting_for_outgoing_complete;
89 
90 static uint8_t                      prov_attention_timer_timeout;
91 static btstack_timer_source_t       prov_attention_timer;
92 
93 static btstack_timer_source_t       prov_protocol_timer;
94 
95 static btstack_crypto_aes128_cmac_t prov_cmac_request;
96 static btstack_crypto_random_t      prov_random_request;
97 static btstack_crypto_ecc_p256_t    prov_ecc_p256_request;
98 static btstack_crypto_ccm_t         prov_ccm_request;
99 
100 // ConfirmationDevice
101 static uint8_t confirmation_device[16];
102 // ConfirmationSalt
103 static uint8_t confirmation_salt[16];
104 // ConfirmationKey
105 static uint8_t confirmation_key[16];
106 // RandomDevice
107 static uint8_t random_device[16];
108 // ProvisioningSalt
109 static uint8_t provisioning_salt[16];
110 // AuthValue
111 static uint8_t auth_value[16];
112 // SessionKey
113 static uint8_t session_key[16];
114 // SessionNonce
115 static uint8_t session_nonce[16];
116 // EncProvisioningData
117 static uint8_t enc_provisioning_data[25];
118 // ProvisioningData
119 static uint8_t provisioning_data[25];
120 
121 // received network_key
122 static mesh_network_key_t * network_key;
123 
124 // DeviceKey
125 static uint8_t device_key[16];
126 
127 static uint8_t  flags;
128 
129 static uint32_t iv_index;
130 static uint16_t unicast_address;
131 
132 typedef enum {
133     DEVICE_W4_INVITE,
134     DEVICE_SEND_CAPABILITIES,
135     DEVICE_W4_START,
136     DEVICE_W4_INPUT_OOK,
137     DEVICE_SEND_INPUT_COMPLETE,
138     DEVICE_W4_PUB_KEY,
139     DEVICE_SEND_PUB_KEY,
140     DEVICE_W4_CONFIRM,
141     DEVICE_SEND_CONFIRM,
142     DEVICE_W4_RANDOM,
143     DEVICE_SEND_RANDOM,
144     DEVICE_W4_DATA,
145     DEVICE_SEND_COMPLETE,
146     DEVICE_SEND_ERROR,
147 } device_state_t;
148 
149 static device_state_t device_state;
150 static uint16_t pb_transport_cid;
151 static pb_type_t pb_type;
152 
153 static void pb_send_pdu(uint16_t transport_cid, const uint8_t * buffer, uint16_t buffer_size){
154     switch (pb_type){
155         case PB_TYPE_ADV:
156             pb_adv_send_pdu(transport_cid, buffer, buffer_size);
157             break;
158         case PB_TYPE_GATT:
159             pb_gatt_send_pdu(transport_cid, buffer, buffer_size);
160             break;
161     }
162 }
163 
164 static void pb_close_link(uint16_t transport_cid, uint8_t reason){
165     switch (pb_type){
166         case PB_TYPE_ADV:
167             pb_adv_close_link(transport_cid, reason);
168             break;
169         case PB_TYPE_GATT:
170             pb_gatt_close_link(transport_cid, reason);
171             break;
172     }
173 }
174 
175 static void provisioning_emit_event(uint16_t pb_adv_cid, uint8_t mesh_subevent){
176     if (!prov_packet_handler) return;
177     uint8_t event[5] = { HCI_EVENT_MESH_META, 3, mesh_subevent};
178     little_endian_store_16(event, 3, pb_adv_cid);
179     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
180 }
181 
182 static void provisioning_emit_output_oob_event(uint16_t pb_adv_cid, uint32_t number){
183     if (!prov_packet_handler) return;
184     uint8_t event[9] = { HCI_EVENT_MESH_META, 7, MESH_SUBEVENT_PB_PROV_START_EMIT_OUTPUT_OOB};
185     little_endian_store_16(event, 3, pb_adv_cid);
186     little_endian_store_16(event, 5, number);
187     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
188 }
189 
190 static void provisioning_emit_attention_timer_event(uint16_t pb_adv_cid, uint8_t timer_s){
191     if (!prov_packet_handler) return;
192     uint8_t event[4] = { HCI_EVENT_MESH_META, 7, MESH_SUBEVENT_PB_PROV_ATTENTION_TIMER};
193     event[3] = timer_s;
194     prov_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
195 }
196 
197 static void provisiong_timer_handler(btstack_timer_source_t * ts){
198     UNUSED(ts);
199     printf("Provisioning Protocol Timeout -> Close Link!\n");
200     pb_close_link(1, 1);
201 }
202 
203 // The provisioning protocol shall have a minimum timeout of 60 seconds that is reset
204 // each time a provisioning protocol PDU is sent or received
205 static void provisioning_timer_start(void){
206     btstack_run_loop_remove_timer(&prov_protocol_timer);
207     btstack_run_loop_set_timer_handler(&prov_protocol_timer, &provisiong_timer_handler);
208     btstack_run_loop_set_timer(&prov_protocol_timer, PROVISIONING_PROTOCOL_TIMEOUT_MS);
209     btstack_run_loop_add_timer(&prov_protocol_timer);
210 }
211 
212 static void provisioning_timer_stop(void){
213     btstack_run_loop_remove_timer(&prov_protocol_timer);
214 }
215 
216 static void provisioning_attention_timer_timeout(btstack_timer_source_t * ts){
217     UNUSED(ts);
218     if (prov_attention_timer_timeout == 0) return;
219     prov_attention_timer_timeout--;
220     provisioning_attention_timer_set();
221 }
222 
223 static void provisioning_attention_timer_set(void){
224     provisioning_emit_attention_timer_event(1, prov_attention_timer_timeout);
225     if (prov_attention_timer_timeout){
226         btstack_run_loop_set_timer_handler(&prov_attention_timer, &provisioning_attention_timer_timeout);
227         btstack_run_loop_set_timer(&prov_attention_timer, 1000);
228         btstack_run_loop_add_timer(&prov_attention_timer);
229     }
230 }
231 
232 // Outgoing Provisioning PDUs
233 static void provisioning_send_provisioning_error(void){
234     // setup response
235     prov_buffer_out[0] = MESH_PROV_FAILED;
236     prov_buffer_out[1] = prov_error_code;
237     pb_send_pdu(pb_transport_cid, prov_buffer_out, 2);
238 }
239 
240 static void provisioning_send_capabilites(void){
241     // setup response
242     prov_buffer_out[0] = MESH_PROV_CAPABILITIES;
243 
244     /* Number of Elements supported */
245     prov_buffer_out[1] = prov_num_elements;
246 
247     /* Supported algorithms - FIPS P-256 Eliptic Curve */
248     big_endian_store_16(prov_buffer_out, 2, 1);
249 
250     /* Public Key Type - Public Key OOB information available */
251     prov_buffer_out[4] = prov_public_key_oob_available;
252 
253     /* Static OOB Type - Static OOB information available */
254     prov_buffer_out[5] = prov_static_oob_available;
255 
256     /* Output OOB Size - max of 8 */
257     prov_buffer_out[6] = prov_output_oob_size;
258 
259     /* Output OOB Action */
260     big_endian_store_16(prov_buffer_out, 7, prov_output_oob_actions);
261 
262     /* Input OOB Size - max of 8*/
263     prov_buffer_out[9] = prov_input_oob_size;
264 
265     /* Input OOB Action */
266     big_endian_store_16(prov_buffer_out, 10, prov_input_oob_actions);
267 
268     // store for confirmation inputs: len 11
269     memcpy(&prov_confirmation_inputs[1], &prov_buffer_out[1], 11);
270 
271     // send
272 
273     pb_send_pdu(pb_transport_cid, prov_buffer_out, 12);
274 }
275 
276 static void provisioning_send_public_key(void){
277     // setup response
278     prov_buffer_out[0] = MESH_PROV_PUB_KEY;
279     memcpy(&prov_buffer_out[1], prov_ec_q, 64);
280 
281     // store for confirmation inputs: len 64
282     memcpy(&prov_confirmation_inputs[81], &prov_buffer_out[1], 64);
283 
284     // send
285     pb_send_pdu(pb_transport_cid, prov_buffer_out, 65);
286 }
287 
288 static void provisioning_send_input_complete(void){
289     // setup response
290     prov_buffer_out[0] = MESH_PROV_INPUT_COMPLETE;
291 
292     // send
293     pb_send_pdu(pb_transport_cid, prov_buffer_out, 17);
294 }
295 static void provisioning_send_confirm(void){
296     // setup response
297     prov_buffer_out[0] = MESH_PROV_CONFIRM;
298     memcpy(&prov_buffer_out[1], confirmation_device, 16);
299 
300     // send
301     pb_send_pdu(pb_transport_cid, prov_buffer_out, 17);
302 }
303 
304 static void provisioning_send_random(void){
305     // setup response
306     prov_buffer_out[0] = MESH_PROV_RANDOM;
307     memcpy(&prov_buffer_out[1],  random_device, 16);
308 
309     // send pdu
310     pb_send_pdu(pb_transport_cid, prov_buffer_out, 17);
311 }
312 
313 static void provisioning_send_complete(void){
314     // setup response
315     prov_buffer_out[0] = MESH_PROV_COMPLETE;
316 
317     // send pdu
318     pb_send_pdu(pb_transport_cid, prov_buffer_out, 1);
319 }
320 
321 static void provisioning_done(void){
322     if (prov_emit_public_key_oob_active){
323         prov_emit_public_key_oob_active = 0;
324         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB);
325     }
326     if (prov_emit_output_oob_active){
327         prov_emit_output_oob_active = 0;
328         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB);
329     }
330     if (prov_attention_timer_timeout){
331         prov_attention_timer_timeout = 0;
332         provisioning_emit_attention_timer_event(1, 0);
333     }
334     device_state = DEVICE_W4_INVITE;
335 
336     // generate new public key
337     printf("Generate new public key\n");
338     btstack_crypto_ecc_p256_generate_key(&prov_ecc_p256_request, prov_ec_q, &prov_key_generated, NULL);
339 }
340 
341 static void provisioning_handle_auth_value_output_oob(void * arg){
342     // limit auth value to single digit
343     auth_value[15] = auth_value[15] % 9 + 1;
344 
345     printf("Output OOB: %u\n", auth_value[15]);
346 
347     // emit output oob value
348     provisioning_emit_output_oob_event(1, auth_value[15]);
349     prov_emit_output_oob_active = 1;
350 }
351 
352 static void provisioning_public_key_exchange_complete(void){
353 
354     // reset auth_value
355     memset(auth_value, 0, sizeof(auth_value));
356 
357     // handle authentication method
358     switch (prov_authentication_method){
359         case 0x00:
360             device_state = DEVICE_W4_CONFIRM;
361             break;
362         case 0x01:
363             memcpy(&auth_value[16-prov_static_oob_len], prov_static_oob_data, prov_static_oob_len);
364             device_state = DEVICE_W4_CONFIRM;
365             break;
366         case 0x02:
367             device_state = DEVICE_W4_CONFIRM;
368             printf("Generate random for auth_value\n");
369             // generate single byte of random data to use for authentication
370             btstack_crypto_random_generate(&prov_random_request, &auth_value[15], 1, &provisioning_handle_auth_value_output_oob, NULL);
371             break;
372         case 0x03:
373             // Input OOB
374             printf("Input OOB requested\n");
375             provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_INPUT_OOB_REQUEST);
376             device_state = DEVICE_W4_INPUT_OOK;
377             break;
378         default:
379             break;
380     }
381 }
382 
383 static void provisioning_run(void){
384     printf("provisioning_run: state %x, wait for outgoing complete %u\n", device_state, prov_waiting_for_outgoing_complete);
385     if (prov_waiting_for_outgoing_complete) return;
386     int start_timer = 1;
387     switch (device_state){
388         case DEVICE_SEND_ERROR:
389             start_timer = 0;    // game over
390             prov_waiting_for_outgoing_complete = 1;
391             provisioning_send_provisioning_error();
392             provisioning_done();
393             break;
394         case DEVICE_SEND_CAPABILITIES:
395             device_state = DEVICE_W4_START;
396             prov_waiting_for_outgoing_complete = 1;
397             provisioning_send_capabilites();
398             break;
399         case DEVICE_SEND_INPUT_COMPLETE:
400             device_state = DEVICE_W4_CONFIRM;
401             prov_waiting_for_outgoing_complete = 1;
402             provisioning_send_input_complete();
403             break;
404         case DEVICE_SEND_PUB_KEY:
405             prov_waiting_for_outgoing_complete = 1;
406             provisioning_send_public_key();
407             provisioning_public_key_exchange_complete();
408             break;
409         case DEVICE_SEND_CONFIRM:
410             device_state = DEVICE_W4_RANDOM;
411             prov_waiting_for_outgoing_complete = 1;
412             provisioning_send_confirm();
413             break;
414         case DEVICE_SEND_RANDOM:
415             device_state = DEVICE_W4_DATA;
416             prov_waiting_for_outgoing_complete = 1;
417             provisioning_send_random();
418             break;
419         case DEVICE_SEND_COMPLETE:
420             start_timer = 0;    // last message
421             prov_waiting_for_outgoing_complete = 1;
422             provisioning_send_complete();
423             provisioning_done();
424             break;
425         default:
426             return;
427     }
428     if (start_timer){
429         provisioning_timer_start();
430     }
431 }
432 
433 static void provisioning_handle_provisioning_error(uint8_t error_code){
434     printf("PROVISIONING ERROR\n");
435     provisioning_timer_stop();
436     prov_error_code = error_code;
437     device_state = DEVICE_SEND_ERROR;
438     provisioning_run();
439 }
440 
441 static void provisioning_handle_invite(uint8_t *packet, uint16_t size){
442 
443     if (size != 1) return;
444 
445     // store for confirmation inputs: len 1
446     memcpy(&prov_confirmation_inputs[0], packet, 1);
447 
448     // handle invite message
449     prov_attention_timer_timeout = packet[0];
450     provisioning_attention_timer_set();
451 
452     device_state = DEVICE_SEND_CAPABILITIES;
453     provisioning_run();
454 }
455 
456 static void provisioning_handle_start(uint8_t * packet, uint16_t size){
457 
458     if (size != 5) return;
459 
460     // validate Algorithm
461     int ok = 1;
462     if (packet[0] > 0x00){
463         ok = 0;
464     }
465     // validate Publik Key
466     if (packet[1] > 0x01){
467         ok = 0;
468     }
469     // validate Authentication Method
470     switch (packet[2]){
471         case 0:
472         case 1:
473             if (packet[3] != 0 || packet[4] != 0){
474                 ok = 0;
475                 break;
476             }
477             break;
478         case 2:
479             if (packet[3] > 0x04 || packet[4] == 0 || packet[4] > 0x08){
480                 ok = 0;
481                 break;
482             }
483             break;
484         case 3:
485             if (packet[3] > 0x03 || packet[4] == 0 || packet[4] > 0x08){
486                 ok = 0;
487                 break;
488             }
489             break;
490     }
491     if (!ok){
492         printf("PROV_START arguments incorrect\n");
493         provisioning_handle_provisioning_error(0x02);
494         return;
495     }
496 
497     // store for confirmation inputs: len 5
498     memcpy(&prov_confirmation_inputs[12], packet, 5);
499 
500     // public key oob
501     prov_public_key_oob_used = packet[1];
502 
503     // authentication method
504     prov_authentication_method = packet[2];
505 
506     // start emit public OOK if specified
507     if (prov_public_key_oob_available && prov_public_key_oob_used){
508         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_START_EMIT_PUBLIC_KEY_OOB);
509     }
510 
511     printf("PublicKey:  %02x\n", prov_public_key_oob_used);
512     printf("AuthMethod: %02x\n", prov_authentication_method);
513 
514     device_state = DEVICE_W4_PUB_KEY;
515     provisioning_run();
516 }
517 
518 static void provisioning_handle_public_key_dhkey(void * arg){
519     UNUSED(arg);
520 
521     printf("DHKEY: ");
522     printf_hexdump(dhkey, sizeof(dhkey));
523 
524     // skip sending own public key when public key oob is used
525     if (prov_public_key_oob_available && prov_public_key_oob_used){
526         // just copy key for confirmation inputs
527         memcpy(&prov_confirmation_inputs[81], prov_ec_q, 64);
528         provisioning_public_key_exchange_complete();
529     } else {
530         // queue public key pdu
531         printf("DEVICE_SEND_PUB_KEY\n");
532         device_state = DEVICE_SEND_PUB_KEY;
533     }
534     provisioning_run();
535 }
536 
537 static void provisioning_handle_public_key(uint8_t *packet, uint16_t size){
538 
539     // validate public key
540     if (size != sizeof(remote_ec_q) || btstack_crypto_ecc_p256_validate_public_key(packet) != 0){
541         printf("Public Key invalid, abort provisioning\n");
542         provisioning_handle_provisioning_error(0x07);   // Unexpected Error
543         return;
544     }
545 
546     // stop emit public OOK if specified and send to crypto module
547     if (prov_public_key_oob_available && prov_public_key_oob_used){
548         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_PUBLIC_KEY_OOB);
549 
550         printf("Replace generated ECC with Public Key OOB:");
551         memcpy(prov_ec_q, prov_public_key_oob_q, 64);
552         printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
553         btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
554     }
555 
556     // store for confirmation inputs: len 64
557     memcpy(&prov_confirmation_inputs[17], packet, 64);
558 
559     // store remote q
560     memcpy(remote_ec_q, packet, sizeof(remote_ec_q));
561 
562     // calculate DHKey
563     btstack_crypto_ecc_p256_calculate_dhkey(&prov_ecc_p256_request, remote_ec_q, dhkey, provisioning_handle_public_key_dhkey, NULL);
564 }
565 
566 static void provisioning_handle_confirmation_device_calculated(void * arg){
567 
568     UNUSED(arg);
569 
570     printf("ConfirmationDevice: ");
571     printf_hexdump(confirmation_device, sizeof(confirmation_device));
572 
573     device_state = DEVICE_SEND_CONFIRM;
574     provisioning_run();
575 }
576 
577 static void provisioning_handle_confirmation_random_device(void * arg){
578     // re-use prov_confirmation_inputs buffer
579     memcpy(&prov_confirmation_inputs[0],  random_device, 16);
580     memcpy(&prov_confirmation_inputs[16], auth_value, 16);
581 
582     // calc confirmation device
583     btstack_crypto_aes128_cmac_message(&prov_cmac_request, confirmation_key, 32, prov_confirmation_inputs, confirmation_device, &provisioning_handle_confirmation_device_calculated, NULL);
584 }
585 
586 static void provisioning_handle_confirmation_k1_calculated(void * arg){
587     printf("ConfirmationKey:   ");
588     printf_hexdump(confirmation_key, sizeof(confirmation_key));
589 
590     printf("AuthValue: ");
591     printf_hexdump(auth_value, 16);
592 
593     // generate random_device
594     btstack_crypto_random_generate(&prov_random_request,random_device, 16, &provisioning_handle_confirmation_random_device, NULL);
595 }
596 
597 static void provisioning_handle_confirmation_s1_calculated(void * arg){
598     UNUSED(arg);
599 
600     // ConfirmationSalt
601     printf("ConfirmationSalt:   ");
602     printf_hexdump(confirmation_salt, sizeof(confirmation_salt));
603 
604     // ConfirmationKey
605     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), confirmation_salt, (const uint8_t*) "prck", 4, confirmation_key, &provisioning_handle_confirmation_k1_calculated, NULL);
606 }
607 
608 static void provisioning_handle_confirmation(uint8_t *packet, uint16_t size){
609 
610     UNUSED(size);
611     UNUSED(packet);
612 
613     //
614     if (prov_emit_output_oob_active){
615         prov_emit_output_oob_active = 0;
616         provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_STOP_EMIT_OUTPUT_OOB);
617     }
618 
619     // CalculationInputs
620     printf("ConfirmationInputs: ");
621     printf_hexdump(prov_confirmation_inputs, sizeof(prov_confirmation_inputs));
622 
623     // calculate s1
624     btstack_crypto_aes128_cmac_zero(&prov_cmac_request, sizeof(prov_confirmation_inputs), prov_confirmation_inputs, confirmation_salt, &provisioning_handle_confirmation_s1_calculated, NULL);
625 }
626 
627 // PROV_RANDOM
628 static void provisioning_handle_random_session_nonce_calculated(void * arg){
629     UNUSED(arg);
630 
631     // The nonce shall be the 13 least significant octets == zero most significant octets
632     uint8_t temp[13];
633     memcpy(temp, &session_nonce[3], 13);
634     memcpy(session_nonce, temp, 13);
635 
636     // SessionNonce
637     printf("SessionNonce:   ");
638     printf_hexdump(session_nonce, 13);
639 
640     device_state = DEVICE_SEND_RANDOM;
641     provisioning_run();
642 }
643 
644 static void provisioning_handle_random_session_key_calculated(void * arg){
645     UNUSED(arg);
646 
647     // SessionKey
648     printf("SessionKey:   ");
649     printf_hexdump(session_key, sizeof(session_key));
650 
651     // SessionNonce
652     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsn", 4, session_nonce, &provisioning_handle_random_session_nonce_calculated, NULL);
653 }
654 
655 static void provisioning_handle_random_s1_calculated(void * arg){
656 
657     UNUSED(arg);
658 
659     // ProvisioningSalt
660     printf("ProvisioningSalt:   ");
661     printf_hexdump(provisioning_salt, sizeof(provisioning_salt));
662 
663     // SessionKey
664     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prsk", 4, session_key, &provisioning_handle_random_session_key_calculated, NULL);
665 }
666 
667 static void provisioning_handle_random(uint8_t *packet, uint16_t size){
668 
669     UNUSED(size);
670     UNUSED(packet);
671 
672     // TODO: validate Confirmation
673 
674     // calc ProvisioningSalt = s1(ConfirmationSalt || RandomProvisioner || RandomDevice)
675     memcpy(&prov_confirmation_inputs[0], confirmation_salt, 16);
676     memcpy(&prov_confirmation_inputs[16], packet, 16);
677     memcpy(&prov_confirmation_inputs[32], random_device, 16);
678     btstack_crypto_aes128_cmac_zero(&prov_cmac_request, 48, prov_confirmation_inputs, provisioning_salt, &provisioning_handle_random_s1_calculated, NULL);
679 }
680 
681 // PROV_DATA
682 static void provisioning_handle_network_dervived(void * arg){
683     provisioning_timer_stop();
684 
685     // notify client
686     provisioning_emit_event(1, MESH_SUBEVENT_PB_PROV_COMPLETE);
687 
688     device_state = DEVICE_SEND_COMPLETE;
689     provisioning_run();
690 
691 }
692 
693 static void provisioning_handle_data_device_key(void * arg){
694     // derive full network key
695     mesh_network_key_derive(&prov_cmac_request, network_key, &provisioning_handle_network_dervived, NULL);
696 }
697 
698 static void provisioning_handle_data_ccm(void * arg){
699 
700     UNUSED(arg);
701 
702     // TODO: validate MIC?
703     uint8_t mic[8];
704     btstack_crypto_ccm_get_authentication_value(&prov_ccm_request, mic);
705     printf("MIC: ");
706     printf_hexdump(mic, 8);
707 
708     // allocate network key
709     network_key = btstack_memory_mesh_network_key_get();
710 
711     // sort provisoning data
712     memcpy(network_key->net_key, provisioning_data, 16);
713     network_key->netkey_index = big_endian_read_16(provisioning_data, 16);
714     network_key->internal_index = 0;
715     flags = provisioning_data[18];
716     iv_index = big_endian_read_32(provisioning_data, 19);
717     unicast_address = big_endian_read_16(provisioning_data, 23);
718 
719     // DeviceKey
720     mesh_k1(&prov_cmac_request, dhkey, sizeof(dhkey), provisioning_salt, (const uint8_t*) "prdk", 4, device_key, &provisioning_handle_data_device_key, NULL);
721 }
722 
723 static void provisioning_handle_data(uint8_t *packet, uint16_t size){
724 
725     UNUSED(size);
726 
727     memcpy(enc_provisioning_data, packet, 25);
728 
729     // decode response
730     btstack_crypto_ccm_init(&prov_ccm_request, session_key, session_nonce, 25, 0, 8);
731     btstack_crypto_ccm_decrypt_block(&prov_ccm_request, 25, enc_provisioning_data, provisioning_data, &provisioning_handle_data_ccm, NULL);
732 }
733 
734 static void provisioning_handle_unexpected_pdu(uint8_t *packet, uint16_t size){
735     printf("Unexpected PDU #%u in state #%u\n", packet[0], (int) device_state);
736     provisioning_handle_provisioning_error(0x03);
737 }
738 
739 static void provisioning_handle_pdu(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
740 
741     if (size < 1) return;
742 
743     switch (packet_type){
744         case HCI_EVENT_PACKET:
745             if (packet[0] != HCI_EVENT_MESH_META)  break;
746             switch (packet[2]){
747                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_OPEN:
748                     pb_transport_cid = mesh_subevent_pb_transport_link_open_get_pb_transport_cid(packet);
749                     pb_type = mesh_subevent_pb_transport_link_open_get_pb_type(packet);
750                     printf("Link opened, reset state, transport cid 0x%02x, PB type %d\n", pb_transport_cid, pb_type);
751                     provisioning_done();
752                     break;
753                 case MESH_SUBEVENT_PB_TRANSPORT_PDU_SENT:
754                     printf("Outgoing packet acked\n");
755                     prov_waiting_for_outgoing_complete = 0;
756                     break;
757                 case MESH_SUBEVENT_PB_TRANSPORT_LINK_CLOSED:
758                     printf("Link close, reset state\n");
759                     pb_transport_cid = MESH_PB_TRANSPORT_INVALID_CID;
760                     provisioning_done();
761                     break;
762             }
763             break;
764         case PROVISIONING_DATA_PACKET:
765             // check state
766             switch (device_state){
767                 case DEVICE_W4_INVITE:
768                     if (packet[0] != MESH_PROV_INVITE) provisioning_handle_unexpected_pdu(packet, size);
769                     printf("MESH_PROV_INVITE: ");
770                     printf_hexdump(&packet[1], size-1);
771                     provisioning_handle_invite(&packet[1], size-1);
772                     break;
773                 case DEVICE_W4_START:
774                     if (packet[0] != MESH_PROV_START) provisioning_handle_unexpected_pdu(packet, size);
775                     printf("MESH_PROV_START:  ");
776                     printf_hexdump(&packet[1], size-1);
777                     provisioning_handle_start(&packet[1], size-1);
778                     break;
779                 case DEVICE_W4_PUB_KEY:
780                     if (packet[0] != MESH_PROV_PUB_KEY) provisioning_handle_unexpected_pdu(packet, size);
781                     printf("MESH_PROV_PUB_KEY: ");
782                     printf_hexdump(&packet[1], size-1);
783                     provisioning_handle_public_key(&packet[1], size-1);
784                     break;
785                 case DEVICE_W4_CONFIRM:
786                     if (packet[0] != MESH_PROV_CONFIRM) provisioning_handle_unexpected_pdu(packet, size);
787                     printf("MESH_PROV_CONFIRM: ");
788                     printf_hexdump(&packet[1], size-1);
789                     provisioning_handle_confirmation(&packet[1], size-1);
790                     break;
791                 case DEVICE_W4_RANDOM:
792                     if (packet[0] != MESH_PROV_RANDOM) provisioning_handle_unexpected_pdu(packet, size);
793                     printf("MESH_PROV_RANDOM:  ");
794                     printf_hexdump(&packet[1], size-1);
795                     provisioning_handle_random(&packet[1], size-1);
796                     break;
797                 case DEVICE_W4_DATA:
798                     if (packet[0] != MESH_PROV_DATA) provisioning_handle_unexpected_pdu(packet, size);
799                     printf("MESH_PROV_DATA:  ");
800                     provisioning_handle_data(&packet[1], size-1);
801                     break;
802                 default:
803                     break;
804             }
805             break;
806         default:
807             break;
808     }
809     provisioning_run();
810 }
811 
812 static void prov_key_generated(void * arg){
813     UNUSED(arg);
814     printf("ECC-P256: ");
815     printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
816     // allow override
817     if (prov_public_key_oob_available){
818         printf("Replace generated ECC with Public Key OOB:");
819         memcpy(prov_ec_q, prov_public_key_oob_q, 64);
820         printf_hexdump(prov_ec_q, sizeof(prov_ec_q));
821         btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
822     }
823 }
824 
825 void provisioning_device_init(void){
826     // setup PB ADV
827     pb_adv_init();
828     pb_adv_register_packet_handler(&provisioning_handle_pdu);
829     // setup PB GATT
830     pb_gatt_init();
831     pb_gatt_register_packet_handler(&provisioning_handle_pdu);
832 
833     pb_transport_cid = MESH_PB_TRANSPORT_INVALID_CID;
834 
835     // init provisioning state
836     provisioning_done();
837 
838     // generate public key
839     btstack_crypto_ecc_p256_generate_key(&prov_ecc_p256_request, prov_ec_q, &prov_key_generated, NULL);
840 }
841 
842 void provisioning_device_register_packet_handler(btstack_packet_handler_t packet_handler){
843     prov_packet_handler = packet_handler;
844 }
845 
846 void provisioning_device_set_public_key_oob(const uint8_t * public_key, const uint8_t * private_key){
847     prov_public_key_oob_q = public_key;
848     prov_public_key_oob_d = private_key;
849     prov_public_key_oob_available = 1;
850     btstack_crypto_ecc_p256_set_key(prov_public_key_oob_q, prov_public_key_oob_d);
851 }
852 
853 void provisioning_device_set_static_oob(uint16_t static_oob_len, const uint8_t * static_oob_data){
854     prov_static_oob_available = 1;
855     prov_static_oob_data = static_oob_data;
856     prov_static_oob_len  = btstack_min(static_oob_len, 16);
857 }
858 
859 void provisioning_device_set_output_oob_actions(uint16_t supported_output_oob_action_types, uint8_t max_oob_output_size){
860     prov_output_oob_actions = supported_output_oob_action_types;
861     prov_output_oob_size    = max_oob_output_size;
862 }
863 
864 void provisioning_device_set_input_oob_actions(uint16_t supported_input_oob_action_types, uint8_t max_oob_input_size){
865     prov_input_oob_actions = supported_input_oob_action_types;
866     prov_input_oob_size    = max_oob_input_size;
867 }
868 
869 void provisioning_device_input_oob_complete_numeric(uint16_t pb_adv_cid, uint32_t input_oob){
870     UNUSED(pb_adv_cid);
871     if (device_state != DEVICE_W4_INPUT_OOK) return;
872 
873     // store input_oob as auth value
874     big_endian_store_32(auth_value, 12, input_oob);
875     device_state = DEVICE_SEND_INPUT_COMPLETE;
876     provisioning_run();
877 }
878 
879 void provisioning_device_input_oob_complete_alphanumeric(uint16_t pb_adv_cid, const uint8_t * input_oob_data, uint16_t input_oob_len){
880     UNUSED(pb_adv_cid);
881     if (device_state != DEVICE_W4_INPUT_OOK) return;
882 
883     // store input_oob and fillup with zeros
884     input_oob_len = btstack_min(input_oob_len, 16);
885     memset(auth_value, 0, 16);
886     memcpy(auth_value, input_oob_data, input_oob_len);
887     device_state = DEVICE_SEND_INPUT_COMPLETE;
888     provisioning_run();
889 }
890 
891 
892 uint8_t provisioning_device_data_get_flags(void){
893     return flags;
894 }
895 uint16_t provisioning_device_data_get_unicast_address(void){
896     return unicast_address;
897 }
898 const uint8_t * provisioning_device_data_get_device_key(void){
899     return device_key;
900 }
901 uint32_t provisioning_device_data_get_iv_index(void){
902     return iv_index;
903 }
904 mesh_network_key_t * provisioning_device_data_get_network_key(void){
905     return network_key;
906 }
907 
908 void provisioning_device_data_get(mesh_provisioning_data_t * provisioning_data){
909     provisioning_data->unicast_address = unicast_address;
910     provisioning_data->iv_index = iv_index;
911     provisioning_data->flags = flags;
912     memcpy(provisioning_data->device_key, device_key, 16);
913     provisioning_data->network_key = network_key;
914 }
915