1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SMP_DEF_H
17 #define SMP_DEF_H
18 
19 #include "btstack.h"
20 #include "hci/hci.h"
21 #include "packet.h"
22 #include "platform/include/alarm.h"
23 #include "platform/include/event.h"
24 #include "smp.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define SMP_CODE_PAIRING_REQ 0x01
31 #define SMP_CODE_PAIRING_RSP 0x02
32 #define SMP_CODE_PAIRING_CFM 0x03
33 #define SMP_CODE_PAIRING_RAND 0x04
34 #define SMP_CODE_PAIRING_FAIL 0x05
35 #define SMP_CODE_ENCRYPTION_INFO 0x06
36 #define SMP_CODE_MASTER_IDENTITY 0x07
37 #define SMP_CODE_IDENTITY_INFO 0x08
38 #define SMP_CODE_IDENTITY_ADDR_INFO 0x09
39 #define SMP_CODE_SIGNING_INFO 0x0A
40 #define SMP_CODE_SECURITY_REQ 0x0B
41 #define SMP_CODE_PAIRING_PUBLIC_KEY 0x0C
42 #define SMP_CODE_PAIRING_DHKEY_CHECK 0x0D
43 #define SMP_CODE_PAIRING_KEYPRESS_NTY 0x0E
44 
45 #define SMP_CMD_PAIR_REQ_DATA_LEN 7
46 #define SMP_CMD_PAIR_RSP_DATA_LEN 7
47 #define SMP_CMD_PAIR_CFM_DATA_LEN 17
48 #define SMP_CMD_PAIR_RAND_DATA_LEN 17
49 #define SMP_CMD_PAIR_FAIL_DATA_LEN 2
50 #define SMP_CMD_PAIR_PUBLIC_KEY_DATA_LEN 65
51 #define SMP_CMD_PAIR_DHK_CHECK_DATA_LEN 17
52 #define SMP_CMD_KEYPRESS_DATA_LEN 2
53 #define SMP_CMD_ENCRYPTION_INFO_DATA_LEN 17
54 #define SMP_CMD_MASTER_IDENTITY_DATA_LEN 11
55 #define SMP_CMD_IDENTITY_INFO_DATA_LEN 17
56 #define SMP_CMD_IDENTITY_ADDR_INFO_DATA_LEN 8
57 #define SMP_CMD_SIGNING_INFO_DATA_LEN 17
58 #define SMP_CMD_SECURITY_REQ_DATA_LEN 2
59 
60 #define SMP_PUBLICKEY_LEN 0x40
61 #define SMP_PUBLICKEY_X_LEN 0x20
62 #define SMP_PUBLICKEY_Y_LEN 0x20
63 #define SMP_DHKEY_LEN 0x20
64 #define SMP_TK_LEN 0x10
65 #define SMP_CONFIRM_DATA_LEN 0x10
66 #define SMP_RANDOM_DATA_LEN 0x10
67 #define SMP_STK_LEN 0x10
68 #define SMP_MACKEY_LEN 0x10
69 #define SMP_DHKEY_CHECK_LEN 0x10
70 #define SMP_SC_OOB_LEN 0x26
71 #define SMP_LEGACY_OOB_LEN 0x10
72 #define SMP_MASTER_RAND_LEN 0x08
73 #define SMP_SIGNCOUNTER_LEN 0x04
74 #define SMP_SIGNATURE_LEN 0x0C
75 #define SMP_SIGN_DATA_MAX_LEN 0x0200
76 #define SMP_AES_CMAC_OUTPUT_LEN 0x10
77 #define SMP_ENCRYPT_KEY_LEN 0x10
78 #define SMP_ENCRYPT_PLAINTEXTDATA_LEN 0x10
79 
80 #define SMP_ROLE_MASTER 0x00
81 #define SMP_ROLE_SLAVE 0x01
82 
83 #define SMP_STATE_IDLE 0x00
84 #define SMP_STATE_PAIRING 0x01
85 #define SMP_STATE_SC_OOB_DATA_GENERATING 0x02
86 #define SMP_STATE_SC_OOB_DATA_GENERATED 0x03
87 
88 #define SMP_GENERATE_SIGNATURE_STEP_1 0x0141
89 #define SMP_GENERATE_SIGNATURE_STEP_2 0x0142
90 #define SMP_GENERATE_SIGNATURE_STEP_3 0x0143
91 
92 #define SMP_GENERATE_RPA_STEP_1 0x0151
93 #define SMP_RPA_HIGH_BIT_LEN 0x03
94 
95 #define SMP_RESOLVE_RPA_STEP_1 0x0161
96 
97 #define SMP_GENERATE_SC_OOB_DATA_STEP_1 0x0171
98 #define SMP_GENERATE_SC_OOB_DATA_STEP_2 0x0172
99 #define SMP_GENERATE_SC_OOB_DATA_STEP_3 0x0173
100 #define SMP_GENERATE_SC_OOB_DATA_STEP_4 0x0174
101 
102 #define SMP_PAIR_WAIT_TIME 30000
103 #define SMP_RESO_RPA_WAIT_TIME 30000
104 #define SMP_GEN_SC_OOB_DATA_WAIT_TIME 30000
105 
106 #define SMP_KEY_DIST_CMD_FLAG_BIT_ENC_INFO 0x01
107 #define SMP_KEY_DIST_CMD_FLAG_BIT_MASTER_IDENT 0x02
108 #define SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_INFO 0x04
109 #define SMP_KEY_DIST_CMD_FLAG_BIT_IDENT_ADDR 0x08
110 #define SMP_KEY_DIST_CMD_FLAG_BIT_SIGN_INFO 0x10
111 
112 #define SMP_ENC_KEY_SIZE_MIN 0x07
113 #define SMP_ENC_KEY_SIZE_MAX 0x10
114 
115 #define SMP_SLAVE_CONFIRM_RECV_FLAG_NO 0x00
116 #define SMP_SLAVE_CONFIRM_RECV_FLAG_YES 0x01
117 
118 #define SMP_SLAVE_PUBLICKEY_RECV_FLAG_NO 0x00
119 #define SMP_SLAVE_PUBLICKEY_RECV_FLAG_YES 0x01
120 
121 #define SMP_SLAVE_DHKEYCHECK_RECV_FLAG_NO 0x00
122 #define SMP_SLAVE_DHKEYCHECK_RECV_FLAG_YES 0x01
123 
124 #define SMP_MASTER_ENCRYPTED_FLAG_NO 0x00
125 #define SMP_MASTER_ENCRYPTED_FLAG_YES 0x01
126 
127 #define SMP_MAX_PASSKEY_VALUE 0x000F423F  // 999999
128 #define SMP_MAX_NUMERIC_VALUE 0x000F423F  // 999999
129 #define SMP_PASSKEY_LEN 0x04
130 
131 #define SMP_ENTRY_VALUE_LEN 0x26
132 
133 #define SMP_INIT_WAIT_TIME (-1)
134 
135 typedef struct {
136     BtAddr addr;
137     SMP_PairParam pairParam;
138     uint8_t pairMethod;
139     uint8_t confirm[SMP_CONFIRM_DATA_LEN];
140     uint8_t random[SMP_RANDOM_DATA_LEN];
141     uint8_t keyDist;
142     uint8_t keyDistCmdFlag;
143     uint8_t LTK[SMP_LTK_LEN];
144     uint16_t masterIdEdiv;
145     uint8_t masterIdRand[SMP_MASTER_RAND_LEN];
146     uint8_t IRK[SMP_IRK_LEN];
147     BtAddr identityAddr;
148     uint8_t CSRK[SMP_CSRK_LEN];
149     uint8_t publicKey[SMP_PUBLICKEY_LEN];
150     uint8_t DHKeyCheck[SMP_DHKEY_CHECK_LEN];
151     uint8_t oobRand[SMP_RANDOM_DATA_LEN];
152 } SMP_PairProcessData;
153 
154 typedef struct {
155     uint16_t handle;
156     uint8_t role;
157     uint8_t state;
158     uint16_t step;
159     Alarm *alarm;
160     uint8_t encKeySize;
161     uint8_t TK[SMP_TK_LEN];
162     uint8_t STK[SMP_STK_LEN];
163     uint8_t pairType;
164     uint8_t slaveConfirmRecvFlag;
165     uint8_t slavePubKeyRecvFlag;
166     uint8_t slaveDHKeyCheckRecvFlag;
167     uint8_t masterEncryptedFlag;
168     uint8_t DHKey[SMP_DHKEY_LEN];
169     uint8_t macKey[SMP_MACKEY_LEN];
170     uint8_t scConfirmCheckCounter;
171     SMP_PairProcessData local;
172     SMP_PairProcessData peer;
173 } SMP_PairMng;
174 
175 typedef struct {
176     uint16_t step;
177     Alarm *timeoutTimer;
178     uint8_t key[16];
179     uint8_t X[16];
180     uint8_t Y[16];
181     uint8_t *M;
182     uint16_t length;
183     uint8_t M_last[16];
184     int n;
185     int i;
186     uint16_t signDataLen;
187     uint32_t signCounter;
188     uint8_t address[6];
189 } SMP_EncCmd;
190 
191 typedef struct {
192     SMP_EncCmd *encCmd;
193 } SMP_EncTimeoutParam;
194 
195 typedef struct {
196     void *data;
197 } SMP_StepParam;
198 
199 typedef struct {
200     const HciLeEncryptReturnParam *encRetParam;
201     SMP_EncCmd *encCmd;
202 } SMP_EncData;
203 
204 typedef struct {
205     uint8_t state;
206     uint16_t step;
207     Alarm *alarm;
208     uint8_t confirm[SMP_CONFIRM_DATA_LEN];
209     uint8_t random[SMP_RANDOM_DATA_LEN];
210     uint8_t publicKey[SMP_PUBLICKEY_LEN];
211 } SMP_ScOobMng;
212 
213 typedef struct {
214     uint8_t irk[SMP_IRK_LEN];
215 } SMP_SetIrkTask_t;
216 
217 typedef struct {
218     BtAddr addr;
219 } SMP_SetIdentAddrTask_t;
220 
221 typedef struct {
222     uint8_t addr[BT_ADDRESS_SIZE];
223     uint8_t irk[SMP_IRK_LEN];
224 } SMP_ResoRpaTask_t;
225 
226 typedef struct {
227     uint8_t addr[BT_ADDRESS_SIZE];
228     uint8_t irk[SMP_IRK_LEN];
229 } SMP_AsyncResoRpaTask_t;
230 
231 typedef struct {
232     uint8_t irk[SMP_IRK_LEN];
233 } SMP_GenRpaTask_t;
234 
235 typedef struct {
236     bool mode;
237 } SMP_SetSecConnOnlyModeTask_t;
238 
239 typedef struct {
240     uint16_t handle;
241     uint8_t authReq;
242 } SMP_SendSecReqToRemoteTask_t;
243 
244 typedef struct {
245     uint8_t csrk[SMP_CSRK_LEN];
246     uint32_t counter;
247     uint8_t *data;
248     uint16_t dataLen;
249 } SMP_GenSignTask_t;
250 
251 typedef struct {
252     uint16_t handle;
253     uint8_t random[SMP_MASTER_RAND_LEN];
254     uint16_t ediv;
255     uint8_t key[SMP_LTK_LEN];
256 } SMP_StartEncTask_t;
257 
258 typedef struct {
259     uint16_t handle;
260     BtAddr localAddr;
261     BtAddr peerAddr;
262     SMP_PairParam param;
263 } SMP_StartPairTask_t;
264 
265 typedef struct {
266     uint16_t handle;
267     bool accept;
268     uint8_t rejectReason;
269     uint8_t pairMethod;
270     uint8_t entryValue[SMP_ENTRY_VALUE_LEN];
271 } SMP_AuthReqReplyTask_t;
272 
273 typedef struct {
274     uint16_t handle;
275     bool accept;
276     uint8_t rejectReason;
277     BtAddr localAddr;
278     BtAddr peerAddr;
279     SMP_PairParam param;
280 } SMP_RemotePairReqReplyTask_t;
281 
282 typedef struct {
283     uint16_t handle;
284     bool accept;
285     uint8_t rejectReason;
286 } SMP_RemotePairRspReplyTask_t;
287 
288 typedef struct {
289     uint16_t handle;
290     bool accept;
291     uint8_t rejectReason;
292 } SMP_RemoteSecReqReplyTask_t;
293 
294 typedef struct {
295     uint16_t handle;
296     bool accept;
297     uint8_t key[SMP_LTK_LEN];
298 } SMP_LongTermKeyReqReplyTask_t;
299 
300 typedef struct {
301     uint16_t handle;
302 } SMP_CancelPairTask_t;
303 
304 typedef struct {
305     SMP_Callback_t cb;
306 } SMP_RegCbTask_t;
307 
308 typedef struct {
309     uint16_t handle;
310     Packet *pkt;
311 } SMP_RecvDataTask_t;
312 
313 typedef struct {
314     uint16_t handle;
315 } SMP_DisconnectedTask_t;
316 
317 typedef struct {
318     uint16_t aclHandle;
319     int result;
320 } SMP_SendL2capDataCbTask_t;
321 
322 typedef void (*SMP_StepFunc)(const SMP_StepParam *param);
323 
324 #ifdef __cplusplus
325 }
326 #endif
327 
328 #endif