xref: /btstack/test/crypto/mock.c (revision 77fc16ac1cd75d5c4040068fe473d4ee8878d298)
1 #include "btstack_util.h"
2 #include "hci.h"
3 #include "aes_cmac.h"
4 #include "hci_dump.h"
5 #include <stdio.h>
6 
7 static btstack_linked_list_t  event_packet_handlers;
8 static uint8_t packet_buffer[256];
9 static uint16_t packet_buffer_len;
10 
11 static uint8_t aes128_cyphertext[16];
hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler)12 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
13 	btstack_linked_list_add(&event_packet_handlers, (btstack_linked_item_t *) callback_handler);
14 }
15 
hci_can_send_command_packet_now(void)16 bool hci_can_send_command_packet_now(void){
17 	return true;
18 }
19 
hci_get_state(void)20 HCI_STATE hci_get_state(void){
21 	return HCI_STATE_WORKING;
22 }
23 
mock_simulate_hci_event(uint8_t * packet,uint16_t size)24 static void mock_simulate_hci_event(uint8_t * packet, uint16_t size){
25 	static int level = 0;
26 	// hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size);
27 	btstack_linked_list_iterator_t  it;
28 	btstack_linked_list_iterator_init(&it ,&event_packet_handlers);
29     while (btstack_linked_list_iterator_has_next(&it)){
30        	btstack_packet_callback_registration_t * item = (btstack_packet_callback_registration_t *) btstack_linked_list_iterator_next(&it);
31 		item->callback(HCI_EVENT_PACKET, 0, packet, size);
32     }
33 }
34 
aes128_report_result(void)35 static void aes128_report_result(void){
36 	uint8_t le_enc_result[22];
37 	uint8_t enc1_data[] = { 0x0e, 0x14, 0x01, 0x17, 0x20, 0x00 };
38 	memcpy (le_enc_result, enc1_data, 6);
39 	reverse_128(aes128_cyphertext, &le_enc_result[6]);
40 	mock_simulate_hci_event(&le_enc_result[0], sizeof(le_enc_result));
41 }
42 
hci_send_cmd(const hci_cmd_t * cmd,...)43 uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){
44     va_list argptr;
45     va_start(argptr, cmd);
46     uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr);
47     va_end(argptr);
48 	hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len);
49 	// dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len);
50 	packet_buffer_len = len;
51 	if (cmd->opcode ==  hci_le_encrypt.opcode){
52 	    uint8_t * key_flipped = &packet_buffer[3];
53 	    uint8_t key[16];
54 		reverse_128(key_flipped, key);
55 	    uint8_t * plaintext_flipped = &packet_buffer[19];
56 	    uint8_t plaintext[16];
57  		reverse_128(plaintext_flipped, plaintext);
58 	    aes128_calc_cyphertext(key, plaintext, aes128_cyphertext);
59 	    aes128_report_result();
60 	}
61 	return ERROR_CODE_SUCCESS;
62 }
63 
hci_halting_defer(void)64 void hci_halting_defer(void){
65 }
66