1 /*
2 * Copyright (C) 2018-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8
9 //#include <aos/aos.h>
10
11 #include <misc/printk.h>
12 #include <misc/byteorder.h>
13 #include <tinycrypt/sha256.h>
14 #include <tinycrypt/constants.h>
15 //#include <port/mesh_hal_sec.h>
16
17 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_MESH_DEBUG_MODEL)
18 #include "common/log.h"
19 #include "crc16.h"
20 #include "genie_service.h"
21
22 static genie_triple_t genie_triple;
23
genie_triple_write(uint32_t * p_pid,uint8_t * p_mac,uint8_t * p_key)24 genie_storage_status_e genie_triple_write(uint32_t *p_pid, uint8_t *p_mac, uint8_t *p_key)
25 {
26 #if 0
27 uint16_t triple_crc = 0;
28 uint16_t triple_crc_magic = GENIE_TRIPLE_CRC_MAGIC;
29 uint8_t data[GENIE_SIZE_TRI_TRUPLE];
30
31 memcpy(data, p_pid, GENIE_TRIPLE_PID_SIZE);
32 memcpy(data + GENIE_TRIPLE_PID_SIZE, p_key, GENIE_TRIPLE_KEY_SIZE);
33 memcpy(data + GENIE_TRIPLE_PID_SIZE + GENIE_TRIPLE_KEY_SIZE, p_mac, GENIE_TRIPLE_MAC_SIZE);
34
35 triple_crc = util_crc16_compute(data, GENIE_SIZE_TRI_TRUPLE - GENIE_TRIPLE_CRC_LEN, &triple_crc_magic);
36 memcpy(data + GENIE_TRIPLE_PID_SIZE + GENIE_TRIPLE_KEY_SIZE + GENIE_TRIPLE_MAC_SIZE, (unsigned char *)&triple_crc, GENIE_TRIPLE_CRC_LEN);
37
38 return genie_storage_write_reliable(GFI_MESH_TRITUPLE, data, GENIE_SIZE_TRI_TRUPLE);
39 #else
40 int32_t ret;
41
42 //uint32_t pid;
43 //uint8_t mac[GENIE_TRIPLE_MAC_SIZE];
44 //uint8_t device_secret[GENIE_TRIPLE_KEY_SIZE];
45
46 if ( p_pid == NULL || p_mac == NULL || p_key == NULL) {
47 GENIE_LOG_ERR("[%s]: input para error!\r\n", __func__);
48 return -1;
49 }
50
51 ret = aos_kv_set("pid", p_pid, GENIE_TRIPLE_PID_SIZE, 1);
52 if ( ret ){
53 GENIE_LOG_ERR("[%s]: set 'product_key' fail! ret = %d\r\n", __func__, ret);
54 return ret;
55 }
56
57 #if 0
58 printk("step 2, mac before save in kv : ");
59 for (int i = 0; i < 6; i++)
60 {
61 printk("%02x", p_mac[i]);
62 }
63 printk("\n");
64 #endif
65
66 ret = aos_kv_set("pro_mac", p_mac, GENIE_TRIPLE_MAC_SIZE + 1, 1);
67 if ( ret ) {
68 GENIE_LOG_ERR("[%s]: set 'device_name' fail! ret = %d\r\n", __func__, ret);
69 return ret;
70 }
71
72 ret = aos_kv_set("pro_secret", p_key, GENIE_TRIPLE_KEY_SIZE + 1, 1);
73 if ( ret ) {
74 GENIE_LOG_ERR("[%s]: set 'device_secret' fail! ret = %d\r\n", __func__, ret);
75 return ret;
76 }
77
78 return GENIE_STORAGE_SUCCESS;
79 #endif
80 }
81
genie_triple_read(uint32_t * p_pid,uint8_t * p_mac,uint8_t * p_key)82 genie_storage_status_e genie_triple_read(uint32_t *p_pid, uint8_t *p_mac, uint8_t *p_key)
83 {
84 #if 0
85 uint16_t triple_crc = 0;
86 uint16_t triple_read_crc = 0;
87 uint16_t triple_crc_magic = GENIE_TRIPLE_CRC_MAGIC;
88 unsigned char *p_data = NULL;
89 genie_storage_status_e ret;
90 uint8_t data[GENIE_SIZE_TRI_TRUPLE];
91
92 ret = genie_storage_read_reliable(GFI_MESH_TRITUPLE, data, GENIE_SIZE_TRI_TRUPLE);
93 if (GENIE_STORAGE_SUCCESS != ret)
94 {
95 GENIE_LOG_ERR("read triple fail:%d", ret);
96 return ret;
97 }
98
99 triple_crc = util_crc16_compute(data, GENIE_SIZE_TRI_TRUPLE - GENIE_TRIPLE_CRC_LEN, &triple_crc_magic);
100
101 p_data = data + GENIE_TRIPLE_PID_SIZE + GENIE_TRIPLE_KEY_SIZE + GENIE_TRIPLE_MAC_SIZE;
102
103 triple_read_crc = p_data[0] | (p_data[1] << 8);
104 if (triple_crc != triple_read_crc)
105 {
106 return GENIE_STORAGE_READ_FAIL;
107 }
108
109 memcpy(p_pid, data, GENIE_TRIPLE_PID_SIZE);
110 memcpy(p_key, data + GENIE_TRIPLE_PID_SIZE, GENIE_TRIPLE_KEY_SIZE);
111 memcpy(p_mac, data + GENIE_TRIPLE_PID_SIZE + GENIE_TRIPLE_KEY_SIZE, GENIE_TRIPLE_MAC_SIZE);
112
113 return GENIE_STORAGE_SUCCESS;
114 #else
115 int32_t ret;
116
117 uint32_t pid;
118 uint8_t mac[GENIE_TRIPLE_MAC_SIZE];
119 uint8_t device_secret[GENIE_TRIPLE_KEY_SIZE];
120
121 int pid_len = GENIE_TRIPLE_PID_SIZE;
122 int mac_len = GENIE_TRIPLE_MAC_SIZE + 1;
123 int secret_len = GENIE_TRIPLE_KEY_SIZE + 1;
124
125 ret = aos_kv_get("pid", &pid, &pid_len);
126 if (ret != 0) {
127 printf("pid in kv is valid \r\n");
128 }
129
130 ret = aos_kv_get("pro_mac", mac, &mac_len);
131 if ((ret != 0) || (mac_len != mac_len)) {
132 printf("mac in kv is valid ,ret = %d, mac_len = %d \r\n", ret, mac_len);
133 }
134
135 #if 0
136 printk("step 3, mac get from kv : ");
137 for (int i = 0; i < 6; i++)
138 {
139 printk("%02x", mac[i]);
140 }
141 printk("\n");
142 #endif
143
144 ret = aos_kv_get("pro_secret", device_secret, &secret_len);
145 if ((ret != 0) || (secret_len != secret_len)) {
146 printf("secret in kv is valid ,ret = %d, secret_len = %d\r\n", ret, secret_len);
147 }
148
149 memcpy(p_pid, &pid, GENIE_TRIPLE_PID_SIZE);
150 memcpy(p_key, device_secret, GENIE_TRIPLE_KEY_SIZE);
151 memcpy(p_mac, mac, GENIE_TRIPLE_MAC_SIZE);
152
153 #if 0
154 printk("step 4, in p_mac : ");
155 for (int i = 0; i < 6; i++)
156 {
157 printk("%02x", p_mac[i]);
158 }
159 printk("\n");
160 #endif
161
162 return GENIE_STORAGE_SUCCESS;
163 #endif
164 }
165
genie_triple_init(void)166 int8_t genie_triple_init(void)
167 {
168 genie_storage_status_e ret;
169
170 memset(&genie_triple, 0, sizeof(genie_triple_t));
171
172 ret = genie_triple_read(&genie_triple.pid, genie_triple.mac, genie_triple.key);
173 if (ret != GENIE_STORAGE_SUCCESS)
174 {
175 GENIE_LOG_ERR("No genie triples,please burn them");
176 return -1;
177 }
178
179 return 0;
180 }
181
genie_triple_get(void)182 genie_triple_t *genie_triple_get(void)
183 {
184 return &genie_triple;
185 }
186
genie_triple_get_mac(void)187 uint8_t *genie_triple_get_mac(void)
188 {
189 return genie_triple.mac;
190 }
191