1 /*
2  * Copyright (C) 2018-2020 Alibaba Group Holding Limited
3  */
4 
5 #include <stdlib.h>
6 //#include <aos/aos.h>
7 #include <aos/kv.h>
8 #include "aos/hal/flash.h"
9 #include <hal/hal.h>
10 #include "crc16.h"
11 #include "bt_crypto.h"
12 
13 #include "genie_service.h"
14 
15 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_FLASH)
16 #include "common/log.h"
17 
18 static uint8_t g_enc_key[16] = {
19     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
21 
_genie_storage_encrypt(uint8_t * p_buff,uint16_t size)22 static genie_storage_status_e _genie_storage_encrypt(uint8_t *p_buff, uint16_t size)
23 {
24     uint8_t data_temp[16];
25     uint8_t enc_temp[16];
26     uint16_t i = 0;
27 
28     while (i < (size >> 4))
29     {
30         memcpy(data_temp, p_buff + (i << 4), 16);
31         BT_DBG("data: %s", bt_hex(data_temp, 16));
32         bt_mesh_aes_encrypt(g_enc_key, data_temp, enc_temp);
33         memcpy(p_buff + (i << 4), enc_temp, 16);
34         i++;
35     }
36 
37     return 0;
38 }
39 
_genie_storage_decrypt(uint8_t * p_buff,uint16_t size)40 static genie_storage_status_e _genie_storage_decrypt(uint8_t *p_buff, uint16_t size)
41 {
42     uint8_t data_temp[16];
43     uint8_t dec_temp[16];
44     uint16_t i = 0;
45 
46     while (i < (size >> 4))
47     {
48         memcpy(data_temp, p_buff + (i << 4), 16);
49         bt_mesh_aes_decrypt(g_enc_key, data_temp, dec_temp);
50         BT_DBG("dec: %s", bt_hex(dec_temp, 16));
51         memcpy(p_buff + (i << 4), dec_temp, 16);
52         i++;
53     }
54 
55     return 0;
56 }
57 
58 #ifdef PROJECT_SECRET_KEY
_ginie_flash_get_enc_key(uint8_t project_key[16])59 static void _ginie_flash_get_enc_key(uint8_t project_key[16])
60 {
61     BT_DBG("project key: %s", bt_hex(project_key, 16));
62     //TODO:need find suitable key
63     bt_mesh_aes_encrypt(project_key, project_key, g_enc_key);
64     BT_DBG("enc key: %s", bt_hex(g_enc_key, 16));
65 }
66 #endif
67 
genie_storage_init(void)68 genie_storage_status_e genie_storage_init(void)
69 {
70     static uint8_t flash_already_inited = 0;
71     genie_storage_status_e ret = GENIE_STORAGE_SUCCESS;
72 
73     if (flash_already_inited == 1)
74     {
75         return GENIE_STORAGE_SUCCESS;
76     }
77 
78     flash_already_inited = 1;
79 
80 #ifdef PROJECT_SECRET_KEY
81     char key_char[] = PROJECT_SECRET_KEY;
82     uint8_t prj_key[16];
83 
84     memset(prj_key, 0, 16);
85     stringtohex(key_char, prj_key, 16);
86 
87     _ginie_flash_get_enc_key(prj_key);
88 #endif
89 
90     return ret;
91 }
92 
genie_kv_read(int key_index,uint8_t * value,uint16_t value_size)93 static int genie_kv_read(int key_index, uint8_t *value, uint16_t value_size)
94 {
95     int read_len = 0;
96     char key[10] = {0};
97 
98     read_len = value_size;
99     snprintf(key, sizeof(key), "%s_%d", "key", key_index);
100 
101     return aos_kv_get(key, value, &read_len);
102 }
103 
_genie_storage_read(uint16_t key_index,uint8_t * p_buff,uint16_t size)104 static genie_storage_status_e _genie_storage_read(uint16_t key_index, uint8_t *p_buff, uint16_t size)
105 {
106     int retval = 0;
107 
108     retval = genie_kv_read(key_index, p_buff, size);
109     if (retval != 0)
110     {
111         //GENIE_LOG_ERR("func:%s index(%d) failed(%d)\n", __func__, key_index, retval);
112         return GENIE_STORAGE_READ_FAIL;
113     }
114     else
115     {
116         return GENIE_STORAGE_SUCCESS;
117     }
118 }
119 
genie_kv_write(int key_index,uint8_t * value,uint16_t value_size)120 static int genie_kv_write(int key_index, uint8_t *value, uint16_t value_size)
121 {
122     char key[10] = {0};
123 
124     snprintf(key, sizeof(key), "%s_%d", "key", key_index);
125 
126     return aos_kv_set(key, value, (int)value_size, 0);
127 }
128 
_genie_storage_write(uint16_t key_index,uint8_t * p_buff,uint16_t size)129 static genie_storage_status_e _genie_storage_write(uint16_t key_index, uint8_t *p_buff, uint16_t size)
130 {
131     int retval = 0;
132 
133     if (p_buff == NULL || 0 == size)
134     {
135         GENIE_LOG_ERR("p_buff is null!!!");
136         return GENIE_STORAGE_DATA_INVALID;
137     }
138 
139     retval = genie_kv_write(key_index, p_buff, size);
140     if (retval != 0)
141     {
142         //GENIE_LOG_ERR("func:%s index(%d) failed(%d)\n", __func__, key_index, retval);
143         return GENIE_STORAGE_WRITE_FAIL;
144     }
145 
146     return GENIE_STORAGE_SUCCESS;
147 }
148 
_genie_storage_get_reliable_size(uint16_t data_size)149 static uint16_t _genie_storage_get_reliable_size(uint16_t data_size)
150 {
151     if ((data_size & 0x0F) == 0)
152     {
153         return data_size;
154     }
155     else
156     {
157         return ((data_size >> 4) + 1) << 4;
158     }
159 }
160 
genie_storage_read_reliable(uint16_t index,uint8_t * p_data,uint16_t data_size)161 genie_storage_status_e genie_storage_read_reliable(uint16_t index, uint8_t *p_data, uint16_t data_size)
162 {
163     genie_storage_status_e ret = GENIE_STORAGE_SUCCESS;
164     uint16_t buff_size = _genie_storage_get_reliable_size(data_size);
165     uint8_t *p_buff = NULL;
166 
167     if (p_data == NULL)
168     {
169         GENIE_LOG_ERR("p_data is null!!!");
170         return GENIE_STORAGE_DATA_INVALID;
171     }
172 
173     p_buff = hal_malloc(buff_size);
174     if (p_buff == NULL)
175     {
176         GENIE_LOG_ERR("p_buff is null!!!");
177         return GENIE_STORAGE_DATA_INVALID;
178     }
179 
180     if (GFI_MESH_TRITUPLE == index)
181     {
182         if (0 != hal_flash_read_triples(p_buff, buff_size))
183         {
184             GENIE_LOG_ERR("read triples fail");
185             ret = GENIE_STORAGE_READ_FAIL;
186         }
187     }
188     else
189     {
190         ret = _genie_storage_read(index, p_buff, buff_size);
191     }
192 
193     if (ret == 0)
194     {
195         _genie_storage_decrypt(p_buff, buff_size);
196         memcpy(p_data, p_buff, data_size);
197     }
198     hal_free(p_buff);
199 
200     return ret;
201 }
202 
genie_storage_write_reliable(uint16_t index,uint8_t * p_data,uint16_t data_size)203 genie_storage_status_e genie_storage_write_reliable(uint16_t index, uint8_t *p_data, uint16_t data_size)
204 {
205     uint8_t *p_buff = NULL;
206     genie_storage_status_e ret = GENIE_STORAGE_SUCCESS;
207     uint16_t buff_size = _genie_storage_get_reliable_size(data_size);
208 
209     if (p_data == NULL)
210     {
211         GENIE_LOG_ERR("p_data is null!!!");
212         return GENIE_STORAGE_DATA_INVALID;
213     }
214 
215     p_buff = hal_malloc(buff_size);
216     if (p_buff == NULL)
217     {
218         GENIE_LOG_ERR("p_buff is null!!!");
219         return GENIE_STORAGE_DATA_INVALID;
220     }
221 
222     memset(p_buff, 0, buff_size);
223     memcpy(p_buff, p_data, data_size);
224     GENIE_LOG_DBG("data:%d buff:%d", data_size, buff_size);
225     _genie_storage_encrypt(p_buff, buff_size);
226 
227     if (GFI_MESH_TRITUPLE == index)
228     {
229         if (0 != hal_flash_write_triples(p_buff, buff_size))
230         {
231             GENIE_LOG_ERR("write triples fail");
232             ret = GENIE_STORAGE_WRITE_FAIL;
233         }
234     }
235     else
236     {
237         ret = _genie_storage_write(index, p_buff, buff_size);
238     }
239 
240     hal_free(p_buff);
241 
242     return ret;
243 }
244 
genie_storage_delete_reliable(uint16_t index)245 genie_storage_status_e genie_storage_delete_reliable(uint16_t index)
246 {
247     char key[10] = {0};
248 
249     snprintf(key, sizeof(key), "%s_%d", "key", index);
250 
251     return aos_kv_del(key);
252 }
253 
genie_storage_read_userdata(uint16_t index,uint8_t * p_buff,uint16_t size)254 genie_storage_status_e genie_storage_read_userdata(uint16_t index, uint8_t *p_buff, uint16_t size)
255 {
256     return _genie_storage_read(index, p_buff, size);
257 }
258 
genie_storage_write_userdata(uint16_t index,uint8_t * p_buff,uint16_t size)259 genie_storage_status_e genie_storage_write_userdata(uint16_t index, uint8_t *p_buff, uint16_t size)
260 {
261     return _genie_storage_write(index, p_buff, size);
262 }
263 
genie_storage_delete_userdata(uint16_t index)264 genie_storage_status_e genie_storage_delete_userdata(uint16_t index)
265 {
266     char key[10] = {0};
267 
268     snprintf(key, sizeof(key), "%s_%d", "key", index);
269 
270     return aos_kv_del(key);
271 }
272 
genie_storage_write_seq(uint32_t * p_seq,bool force_write)273 genie_storage_status_e genie_storage_write_seq(uint32_t *p_seq, bool force_write)
274 {
275     int ret = -1;
276     uint32_t seq_write = 0;
277 
278     /*Don't save seq number too frequently for save energy*/
279     /*Save seq number 1\2\3 for provision*/
280     if ((force_write == false) && (*p_seq > 0x03) && (*p_seq % GENIE_SEQ_SAVE_INTERVAL) != 0)
281     {
282         //GENIE_LOG_INFO("no save seq(%d)", *p_seq);
283         return GENIE_STORAGE_SUCCESS;
284     }
285 
286     seq_write = (*p_seq & 0x00FFFFFF) | (GENIE_SEQ_MAGIC_NUMBER << 24);
287     ret = aos_kv_set(GENIE_KV_SEQ_KEY, &seq_write, sizeof(uint32_t), 0);
288     if (ret != 0)
289     {
290         GENIE_LOG_ERR("func:%s (%d)failed\n", __func__, ret);
291         return GENIE_STORAGE_WRITE_FAIL;
292     }
293     else
294     {
295         return GENIE_STORAGE_SUCCESS;
296     }
297 }
298 
genie_storage_read_seq(uint32_t * p_seq)299 genie_storage_status_e genie_storage_read_seq(uint32_t *p_seq)
300 {
301     int ret = -1;
302     uint32_t seq_read;
303     int seq_len = sizeof(uint32_t);
304 
305     ret = aos_kv_get(GENIE_KV_SEQ_KEY, &seq_read, &seq_len);
306     if ((seq_read >> 24) != GENIE_SEQ_MAGIC_NUMBER)
307     {
308         return GENIE_STORAGE_DATA_INVALID;
309     }
310 
311     *p_seq = seq_read & 0x00FFFFFF;
312     *p_seq += GENIE_SEQ_SAVE_INTERVAL;
313 
314     genie_storage_write_seq(p_seq, true); //update seq in kv
315 
316     return ret;
317 }
318 
genie_storage_delete_seq(void)319 genie_storage_status_e genie_storage_delete_seq(void)
320 {
321     int ret = -1;
322 
323     ret = aos_kv_del(GENIE_KV_SEQ_KEY);
324     if (ret != 0)
325     {
326         GENIE_LOG_ERR("func:%s (%d)failed\n", __func__, ret);
327         return GENIE_STORAGE_DELETE_FAIL;
328     }
329     else
330     {
331         return GENIE_STORAGE_SUCCESS;
332     }
333 }
334 
genie_storage_write_sub(uint16_t * p_sub)335 genie_storage_status_e genie_storage_write_sub(uint16_t *p_sub)
336 {
337     uint16_t group_addr_crc = 0;
338     uint16_t crc_magic = 0xA5A6;
339     uint8_t data[CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2 + 2] = {0};
340 
341     memcpy(data, p_sub, CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2);
342     group_addr_crc = util_crc16_compute(data, CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2, &crc_magic);
343     memcpy(data + CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2, (unsigned char *)&group_addr_crc, 2);
344 
345     return hal_flash_write_group_addr(data, CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2 + 2);
346 }
347 
genie_storage_read_sub(uint16_t * p_sub)348 genie_storage_status_e genie_storage_read_sub(uint16_t *p_sub)
349 {
350     uint16_t group_addr_crc = 0;
351     uint16_t group_addr_read_crc = 0;
352     uint16_t crc_magic = 0xA5A6;
353     uint8_t *p_crc = NULL;
354     uint8_t data[CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2 + 2] = {0};
355 
356     if (0 != hal_flash_read_group_addr(data, CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2 + 2))
357     {
358         GENIE_LOG_ERR("read group addr fail");
359         return GENIE_STORAGE_READ_FAIL;
360     }
361 
362     group_addr_crc = util_crc16_compute(data, CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2, &crc_magic);
363 
364     p_crc = data + CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2;
365     group_addr_read_crc = p_crc[0] | (p_crc[1] << 8);
366 
367     if (group_addr_read_crc != group_addr_crc)
368     {
369         return GENIE_STORAGE_READ_FAIL;
370     }
371 
372     memcpy(p_sub, data, CONFIG_BT_MESH_MODEL_GROUP_COUNT * 2);
373 
374     return GENIE_STORAGE_SUCCESS;
375 }
376 
genie_storage_write_para(struct bt_mesh_net * mesh_data)377 genie_storage_status_e genie_storage_write_para(struct bt_mesh_net *mesh_data)
378 {
379     return genie_storage_write_reliable(GFI_MESH_PARA, (uint8_t *)mesh_data, sizeof(struct bt_mesh_net));
380 }
381 
genie_storage_read_para(struct bt_mesh_net * mesh_data)382 genie_storage_status_e genie_storage_read_para(struct bt_mesh_net *mesh_data)
383 {
384     return genie_storage_read_reliable(GFI_MESH_PARA, (uint8_t *)mesh_data, sizeof(struct bt_mesh_net));
385 }
386 
genie_storage_write_devkey(uint8_t * p_devkey)387 genie_storage_status_e genie_storage_write_devkey(uint8_t *p_devkey)
388 {
389     return genie_storage_write_reliable(GFI_MESH_DEVKEY, p_devkey, 16);
390 }
391 
genie_storage_read_devkey(uint8_t * p_devkey)392 genie_storage_status_e genie_storage_read_devkey(uint8_t *p_devkey)
393 {
394     return genie_storage_read_reliable(GFI_MESH_DEVKEY, p_devkey, 16);
395 }
396 
genie_storage_write_netkey(mesh_netkey_para_t * p_netkey)397 genie_storage_status_e genie_storage_write_netkey(mesh_netkey_para_t *p_netkey)
398 {
399     return genie_storage_write_reliable(GFI_MESH_NETKEY, (uint8_t *)p_netkey, sizeof(mesh_netkey_para_t));
400 }
401 
genie_storage_read_netkey(mesh_netkey_para_t * p_netkey)402 genie_storage_status_e genie_storage_read_netkey(mesh_netkey_para_t *p_netkey)
403 {
404     return genie_storage_read_reliable(GFI_MESH_NETKEY, (uint8_t *)p_netkey, sizeof(mesh_netkey_para_t));
405 }
406 
genie_storage_write_appkey(mesh_appkey_para_t * p_appkey)407 genie_storage_status_e genie_storage_write_appkey(mesh_appkey_para_t *p_appkey)
408 {
409     return genie_storage_write_reliable(GFI_MESH_APPKEY, (uint8_t *)p_appkey, sizeof(mesh_appkey_para_t));
410 }
411 
genie_storage_read_appkey(mesh_appkey_para_t * p_appkey)412 genie_storage_status_e genie_storage_read_appkey(mesh_appkey_para_t *p_appkey)
413 {
414     return genie_storage_read_reliable(GFI_MESH_APPKEY, (uint8_t *)p_appkey, sizeof(mesh_appkey_para_t));
415 }
416 
genie_storage_write_addr(uint16_t * p_addr)417 genie_storage_status_e genie_storage_write_addr(uint16_t *p_addr)
418 {
419     BT_DBG("0x%04x", *p_addr);
420 
421     return genie_storage_write_reliable(GFI_MESH_SADDR, (uint8_t *)p_addr, 2);
422 }
423 
genie_storage_read_addr(uint16_t * p_addr)424 genie_storage_status_e genie_storage_read_addr(uint16_t *p_addr)
425 {
426     return genie_storage_read_reliable(GFI_MESH_SADDR, (uint8_t *)p_addr, 2);
427 }
428 
genie_storage_write_hb(mesh_hb_para_t * p_para)429 genie_storage_status_e genie_storage_write_hb(mesh_hb_para_t *p_para)
430 {
431     return genie_storage_write_reliable(GFI_MESH_HB, (uint8_t *)p_para, sizeof(mesh_hb_para_t));
432 }
433 
genie_storage_read_hb(mesh_hb_para_t * p_para)434 genie_storage_status_e genie_storage_read_hb(mesh_hb_para_t *p_para)
435 {
436     return genie_storage_read_reliable(GFI_MESH_HB, (uint8_t *)p_para, sizeof(mesh_hb_para_t));
437 }
438 
439 #ifdef CONFIG_BT_MESH_CTRL_RELAY
genie_storage_write_ctrl_relay(mesh_ctrl_relay_para_t * p_para)440 genie_storage_status_e genie_storage_write_ctrl_relay(mesh_ctrl_relay_para_t *p_para)
441 {
442     return genie_storage_write_reliable(GFI_MESH_CTRL_RELAY, (uint8_t *)p_para, sizeof(mesh_ctrl_relay_para_t));
443 }
444 
genie_storage_read_ctrl_relay(mesh_ctrl_relay_para_t * p_para)445 genie_storage_status_e genie_storage_read_ctrl_relay(mesh_ctrl_relay_para_t *p_para)
446 {
447     return genie_storage_read_reliable(GFI_MESH_CTRL_RELAY, (uint8_t *)p_para, sizeof(mesh_ctrl_relay_para_t));
448 }
449 #endif
450 
genie_storage_reset_system(void)451 genie_storage_status_e genie_storage_reset_system(void)
452 {
453     int ret = 0;
454 
455     ret = aos_kv_reset();
456     if (ret != 0)
457     {
458         GENIE_LOG_ERR("func:%s (%d)failed", __func__, ret);
459         return GENIE_STORAGE_EARSE_FAIL;
460     }
461 
462     return GENIE_STORAGE_SUCCESS;
463 }
464