xref: /btstack/test/security_manager/mock.c (revision 077fecbb6ed539507f37505ebd8a5b00e01c55e9)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "ble/att_db.h"
7 #include "hci.h"
8 #include "hci_dump.h"
9 #include "l2cap.h"
10 #include "rijndael.h"
11 #include "btstack_linked_list.h"
12 #include "btstack_run_loop_embedded.h"
13 
14 static btstack_packet_handler_t le_data_handler;
15 
16 static uint8_t packet_buffer[256];
17 static uint16_t packet_buffer_len = 0;
18 
19 static uint8_t aes128_cyphertext[16];
20 
21 static hci_connection_t  the_connection;
22 static btstack_linked_list_t     connections;
23 static btstack_linked_list_t     event_packet_handlers;
24 
25 void mock_init(void){
26 	the_connection.item.next = NULL;
27 	connections = (btstack_linked_item_t*) &the_connection;
28 }
29 
30 uint8_t * mock_packet_buffer(void){
31 	return packet_buffer;
32 }
33 
34 void mock_clear_packet_buffer(void){
35 	packet_buffer_len = 0;
36 	memset(packet_buffer, 0, sizeof(packet_buffer));
37 }
38 
39 static void dump_packet(int packet_type, uint8_t * buffer, uint16_t size){
40 #if 0
41 	static int packet_counter = 1;
42 	char var_name[80];
43 	sprintf(var_name, "test_%s_packet_%02u", packet_type == HCI_COMMAND_DATA_PACKET ? "command" : "acl", packet_counter);
44 	printf("uint8_t %s[] = { ", var_name);
45 	for (int i = 0; i < size ; i++){
46 		if ((i % 16) == 0) printf("\n    ");
47 		printf ("0x%02x, ", buffer[i]);
48 	}
49 	printf("};\n");
50 	packet_counter++;
51 #endif
52 }
53 
54 void aes128_calc_cyphertext(uint8_t key[16], uint8_t plaintext[16], uint8_t cyphertext[16]){
55 	uint32_t rk[RKLENGTH(KEYBITS)];
56 	int nrounds = rijndaelSetupEncrypt(rk, &key[0], KEYBITS);
57 	rijndaelEncrypt(rk, nrounds, plaintext, cyphertext);
58 }
59 
60 void mock_simulate_hci_event(uint8_t * packet, uint16_t size){
61 	hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size);
62 	btstack_linked_list_iterator_t  it;
63 	btstack_linked_list_iterator_init(&it ,&event_packet_handlers);
64     while (btstack_linked_list_iterator_has_next(&it)){
65        	btstack_packet_callback_registration_t * item = (btstack_packet_callback_registration_t *) btstack_linked_list_iterator_next(&it);
66 		item->callback(HCI_EVENT_PACKET, 0, packet, size);
67     }
68 	if (le_data_handler){
69 		le_data_handler(HCI_EVENT_PACKET, 0, packet, size);
70 	}
71 }
72 
73 void aes128_report_result(void){
74 	uint8_t le_enc_result[22];
75 	uint8_t enc1_data[] = { 0x0e, 0x14, 0x01, 0x17, 0x20, 0x00 };
76 	memcpy (le_enc_result, enc1_data, 6);
77 	reverse_128(aes128_cyphertext, &le_enc_result[6]);
78 	mock_simulate_hci_event(&le_enc_result[0], sizeof(le_enc_result));
79 }
80 
81 void mock_simulate_sm_data_packet(uint8_t * packet, uint16_t len){
82 
83 	uint16_t handle = 0x40;
84 	uint16_t cid = 0x06;
85 
86 	uint8_t acl_buffer[len + 8];
87 
88 	// 0 - Connection handle : PB=10 : BC=00
89     little_endian_store_16(acl_buffer, 0, handle | (0 << 12) | (0 << 14));
90     // 2 - ACL length
91     little_endian_store_16(acl_buffer, 2,  len + 4);
92     // 4 - L2CAP packet length
93     little_endian_store_16(acl_buffer, 4,  len + 0);
94     // 6 - L2CAP channel DEST
95     little_endian_store_16(acl_buffer, 6, cid);
96 
97 	memcpy(&acl_buffer[8], packet, len);
98 	hci_dump_packet(HCI_ACL_DATA_PACKET, 1, &acl_buffer[0], len + 8);
99 
100 	le_data_handler(SM_DATA_PACKET, handle, packet, len);
101 
102 	// process queued callbacks, might become obsolete after queued callback integration in run loop
103 	btstack_run_loop_embedded_execute_once();
104 }
105 
106 void mock_simulate_command_complete(const hci_cmd_t *cmd){
107 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) cmd->opcode & 0xff, (uint8_t) cmd->opcode >> 8, 0};
108 	mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet));
109 }
110 
111 void mock_simulate_hci_state_working(void){
112 	uint8_t packet[] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
113 	mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet));
114 }
115 
116 void mock_simulate_connected(void){
117     uint8_t packet[] = { 0x3e, 0x13, 0x01, 0x00, 0x40, 0x00, 0x01, 0x01, 0x18, 0x12, 0x5e, 0x68, 0xc9, 0x73, 0x18, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05};
118 	mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet));
119 }
120 
121 void att_init_connection(att_connection_t * att_connection){
122     att_connection->mtu = 23;
123     att_connection->encryption_key_size = 0;
124     att_connection->authenticated = 0;
125 	att_connection->authorized = 0;
126 }
127 
128 void gap_local_bd_addr(bd_addr_t address_buffer){
129 	int i;
130 	for (i=0;i<6;i++) {
131 		address_buffer[i] = 0x11 * (i+1);
132 	}
133 }
134 
135 void hci_halting_defer(void){
136 }
137 
138 bool hci_can_send_command_packet_now(void){
139 	return true;
140 }
141 bool hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
142 	return true;
143 }
144 
145 hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){
146 	return &the_connection;
147 }
148 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
149 	return &the_connection;
150 }
151 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
152     btstack_linked_list_iterator_init(it, &connections);
153 }
154 
155 // get addr type and address used in different contexts
156 void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){
157     static uint8_t dummy[] = { 0x00, 0x1b, 0xdc, 0x07, 0x32, 0xef };
158     *addr_type = 0;
159     memcpy(addr, dummy, 6);
160 }
161 void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr){
162     gap_le_get_own_address(addr_type, addr);
163 }
164 void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){
165     gap_le_get_own_address(addr_type, addr);
166 }
167 
168 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type,
169     uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy) {
170  }
171 
172 uint16_t hci_get_manufacturer(void){
173  return 0xffff;
174 };
175 
176 void hci_le_set_own_address_type(uint8_t own_address){
177 }
178 
179 void hci_le_random_address_set(const bd_addr_t addr){
180 }
181 
182 void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t cid){
183 	if (packet_buffer_len) return;
184     uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 0, 0};
185     little_endian_store_16(event, 2, cid);
186     le_data_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
187 }
188 
189 bool l2cap_can_send_connectionless_packet_now(void){
190 	return packet_buffer_len == 0;
191 }
192 
193 bool l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
194 	return packet_buffer_len == 0;
195 }
196 
197 uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){
198     va_list argptr;
199     va_start(argptr, cmd);
200     uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr);
201     va_end(argptr);
202 	hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len);
203 	dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len);
204 	packet_buffer_len = len;
205 
206 	// track le encrypt and le rand
207 	if (cmd->opcode ==  hci_le_encrypt.opcode){
208 	    uint8_t * key_flipped = &packet_buffer[3];
209 	    uint8_t key[16];
210 		reverse_128(key_flipped, key);
211 	    // printf("le_encrypt key ");
212 	    // hexdump(key, 16);
213 	    uint8_t * plaintext_flipped = &packet_buffer[19];
214 	    uint8_t plaintext[16];
215  		reverse_128(plaintext_flipped, plaintext);
216 	    // printf("le_encrypt txt ");
217 	    // hexdump(plaintext, 16);
218 	    aes128_calc_cyphertext(key, plaintext, aes128_cyphertext);
219 	    // printf("le_encrypt res ");
220 	    // hexdump(aes128_cyphertext, 16);
221 	}
222 	return ERROR_CODE_SUCCESS;
223 }
224 
225 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
226 	le_data_handler = packet_handler;
227 }
228 
229 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
230 	btstack_linked_list_add(&event_packet_handlers, (btstack_linked_item_t *) callback_handler);
231 }
232 
233 bool l2cap_reserve_packet_buffer(void){
234 	printf("l2cap_reserve_packet_buffer\n");
235 	return true;
236 }
237 
238 uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
239 	printf("l2cap_send_prepared_connectionless\n");
240 	return 0;
241 }
242 
243 uint8_t l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t * buffer, uint16_t len){
244 	// printf("l2cap_send_connectionless\n");
245 
246     int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02;
247 
248 	// 0 - Connection handle : PB=pb : BC=00
249     little_endian_store_16(packet_buffer, 0, handle | (pb << 12) | (0 << 14));
250     // 2 - ACL length
251     little_endian_store_16(packet_buffer, 2,  len + 4);
252     // 4 - L2CAP packet length
253     little_endian_store_16(packet_buffer, 4,  len + 0);
254     // 6 - L2CAP channel DEST
255     little_endian_store_16(packet_buffer, 6, cid);
256 
257 	memcpy(&packet_buffer[8], buffer, len);
258 	hci_dump_packet(HCI_ACL_DATA_PACKET, 0, &packet_buffer[0], len + 8);
259 
260 	dump_packet(HCI_ACL_DATA_PACKET, packet_buffer, len + 8);
261 	packet_buffer_len = len + 8;
262 
263 	return ERROR_CODE_SUCCESS;
264 }
265 
266 void hci_disconnect_security_block(hci_con_handle_t con_handle){
267 	printf("hci_disconnect_security_block \n");
268 }
269 
270 bool hci_non_flushable_packet_boundary_flag_supported(void){
271 	return true;
272 }
273 
274 void l2cap_run(void){
275 }
276 
277 HCI_STATE hci_get_state(void){
278 	return HCI_STATE_WORKING;
279 }
280 
281 #include "hal_cpu.h"
282 void hal_cpu_disable_irqs(void){}
283 void hal_cpu_enable_irqs(void){}
284 void hal_cpu_enable_irqs_and_sleep(void){}
285 
286 #include "hal_time_ms.h"
287 static uint32_t time_ms;
288 uint32_t hal_time_ms(void){
289 	return time_ms++;
290 }
291