• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2003-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "bt_target.h"
20 
21 #include <log/log.h>
22 #include <string.h>
23 #include "smp_int.h"
24 
25 const char* const smp_state_name[] = {
26     "SMP_STATE_IDLE",
27     "SMP_STATE_WAIT_APP_RSP",
28     "SMP_STATE_SEC_REQ_PENDING",
29     "SMP_STATE_PAIR_REQ_RSP",
30     "SMP_STATE_WAIT_CONFIRM",
31     "SMP_STATE_CONFIRM",
32     "SMP_STATE_RAND",
33     "SMP_STATE_PUBLIC_KEY_EXCH",
34     "SMP_STATE_SEC_CONN_PHS1_START",
35     "SMP_STATE_WAIT_COMMITMENT",
36     "SMP_STATE_WAIT_NONCE",
37     "SMP_STATE_SEC_CONN_PHS2_START",
38     "SMP_STATE_WAIT_DHK_CHECK",
39     "SMP_STATE_DHK_CHECK",
40     "SMP_STATE_ENCRYPTION_PENDING",
41     "SMP_STATE_BOND_PENDING",
42     "SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA",
43     "SMP_STATE_MAX"};
44 
45 const char* const smp_event_name[] = {"PAIRING_REQ_EVT",
46                                       "PAIRING_RSP_EVT",
47                                       "CONFIRM_EVT",
48                                       "RAND_EVT",
49                                       "PAIRING_FAILED_EVT",
50                                       "ENC_INFO_EVT",
51                                       "MASTER_ID_EVT",
52                                       "ID_INFO_EVT",
53                                       "ID_ADDR_EVT",
54                                       "SIGN_INFO_EVT",
55                                       "SECURITY_REQ_EVT",
56                                       "PAIR_PUBLIC_KEY_EVT",
57                                       "PAIR_DHKEY_CHECK_EVT",
58                                       "PAIR_KEYPRESS_NOTIFICATION_EVT",
59                                       "PAIR_COMMITMENT_EVT",
60                                       "KEY_READY_EVT",
61                                       "ENCRYPTED_EVT",
62                                       "L2CAP_CONN_EVT",
63                                       "L2CAP_DISCONN_EVT",
64                                       "API_IO_RSP_EVT",
65                                       "API_SEC_GRANT_EVT",
66                                       "TK_REQ_EVT",
67                                       "AUTH_CMPL_EVT",
68                                       "ENC_REQ_EVT",
69                                       "BOND_REQ_EVT",
70                                       "DISCARD_SEC_REQ_EVT",
71                                       "PUBLIC_KEY_EXCHANGE_REQ_EVT",
72                                       "LOCAL_PUBLIC_KEY_CRTD_EVT",
73                                       "BOTH_PUBLIC_KEYS_RCVD_EVT",
74                                       "SEC_CONN_DHKEY_COMPLETE_EVT",
75                                       "HAVE_LOCAL_NONCE_EVT",
76                                       "SEC_CONN_PHASE1_CMPLT_EVT",
77                                       "SEC_CONN_CALC_NC_EVT",
78                                       "SEC_CONN_DISPLAY_NC_EVT",
79                                       "SEC_CONN_OK_EVT",
80                                       "SEC_CONN_2_DHCK_CHECKS_PRESENT_EVT",
81                                       "SEC_CONN_KEY_READY_EVT",
82                                       "KEYPRESS_NOTIFICATION_EVT",
83                                       "SEC_CONN_OOB_DATA_EVT",
84                                       "CREATE_LOCAL_SEC_CONN_OOB_DATA_EVT",
85                                       "OUT_OF_RANGE_EVT"};
86 
87 const char* smp_get_event_name(tSMP_EVENT event);
88 const char* smp_get_state_name(tSMP_STATE state);
89 
90 #define SMP_SM_IGNORE 0
91 #define SMP_NUM_ACTIONS 2
92 #define SMP_SME_NEXT_STATE 2
93 #define SMP_SM_NUM_COLS 3
94 
95 typedef const uint8_t (*tSMP_SM_TBL)[SMP_SM_NUM_COLS];
96 
97 enum {
98   SMP_PROC_SEC_REQ,
99   SMP_SEND_PAIR_REQ,
100   SMP_SEND_PAIR_RSP,
101   SMP_SEND_CONFIRM,
102   SMP_SEND_PAIR_FAIL,
103   SMP_SEND_RAND,
104   SMP_SEND_ENC_INFO,
105   SMP_SEND_ID_INFO,
106   SMP_SEND_LTK_REPLY,
107   SMP_PROC_PAIR_CMD,
108   SMP_PROC_PAIR_FAIL,
109   SMP_PROC_CONFIRM,
110   SMP_PROC_RAND,
111   SMP_PROC_ENC_INFO,
112   SMP_PROC_MASTER_ID,
113   SMP_PROC_ID_INFO,
114   SMP_PROC_ID_ADDR,
115   SMP_PROC_SRK_INFO,
116   SMP_PROC_SEC_GRANT,
117   SMP_PROC_SL_KEY,
118   SMP_PROC_COMPARE,
119   SMP_PROC_IO_RSP,
120   SMP_GENERATE_COMPARE,
121   SMP_GENERATE_CONFIRM,
122   SMP_GENERATE_STK,
123   SMP_KEY_DISTRIBUTE,
124   SMP_START_ENC,
125   SMP_PAIRING_CMPL,
126   SMP_DECIDE_ASSO_MODEL,
127   SMP_SEND_APP_CBACK,
128   SMP_CHECK_AUTH_REQ,
129   SMP_PAIR_TERMINATE,
130   SMP_ENC_CMPL,
131   SMP_PROC_DISCARD,
132   SMP_CREATE_PRIVATE_KEY,
133   SMP_USE_OOB_PRIVATE_KEY,
134   SMP_SEND_PAIR_PUBLIC_KEY,
135   SMP_PROCESS_PAIR_PUBLIC_KEY,
136   SMP_HAVE_BOTH_PUBLIC_KEYS,
137   SMP_START_SEC_CONN_PHASE1,
138   SMP_PROCESS_LOCAL_NONCE,
139   SMP_SEND_COMMITMENT,
140   SMP_PROCESS_PAIRING_COMMITMENT,
141   SMP_PROCESS_PEER_NONCE,
142   SMP_CALCULATE_LOCAL_DHKEY_CHECK,
143   SMP_SEND_DHKEY_CHECK,
144   SMP_PROCESS_DHKEY_CHECK,
145   SMP_CALCULATE_PEER_DHKEY_CHECK,
146   SMP_MATCH_DHKEY_CHECKS,
147   SMP_CALCULATE_NUMERIC_COMPARISON_DISPLAY_NUMBER,
148   SMP_MOVE_TO_SEC_CONN_PHASE2,
149   SMP_PH2_DHKEY_CHECKS_ARE_PRESENT,
150   SMP_WAIT_FOR_BOTH_PUBLIC_KEYS,
151   SMP_START_PASSKEY_VERIFICATION,
152   SMP_SEND_KEYPRESS_NOTIFICATION,
153   SMP_PROCESS_KEYPRESS_NOTIFICATION,
154   SMP_PROCESS_SECURE_CONNECTION_OOB_DATA,
155   SMP_SET_LOCAL_OOB_KEYS,
156   SMP_SET_LOCAL_OOB_RAND_COMMITMENT,
157   SMP_IDLE_TERMINATE,
158   SMP_SM_NO_ACTION
159 };
160 
161 static const tSMP_ACT smp_sm_action[] = {
162     smp_proc_sec_req,
163     smp_send_pair_req,
164     smp_send_pair_rsp,
165     smp_send_confirm,
166     smp_send_pair_fail,
167     smp_send_rand,
168     smp_send_enc_info,
169     smp_send_id_info,
170     smp_send_ltk_reply,
171     smp_proc_pair_cmd,
172     smp_proc_pair_fail,
173     smp_proc_confirm,
174     smp_proc_rand,
175     smp_proc_enc_info,
176     smp_proc_master_id,
177     smp_proc_id_info,
178     smp_proc_id_addr,
179     smp_proc_srk_info,
180     smp_proc_sec_grant,
181     smp_proc_sl_key,
182     smp_proc_compare,
183     smp_process_io_response,
184     smp_generate_compare,
185     smp_generate_srand_mrand_confirm,
186     smp_generate_stk,
187     smp_key_distribution,
188     smp_start_enc,
189     smp_pairing_cmpl,
190     smp_decide_association_model,
191     smp_send_app_cback,
192     smp_check_auth_req,
193     smp_pair_terminate,
194     smp_enc_cmpl,
195     smp_proc_discard,
196     smp_create_private_key,
197     smp_use_oob_private_key,
198     smp_send_pair_public_key,
199     smp_process_pairing_public_key,
200     smp_both_have_public_keys,
201     smp_start_secure_connection_phase1,
202     smp_process_local_nonce,
203     smp_send_commitment,
204     smp_process_pairing_commitment,
205     smp_process_peer_nonce,
206     smp_calculate_local_dhkey_check,
207     smp_send_dhkey_check,
208     smp_process_dhkey_check,
209     smp_calculate_peer_dhkey_check,
210     smp_match_dhkey_checks,
211     smp_calculate_numeric_comparison_display_number,
212     smp_move_to_secure_connections_phase2,
213     smp_phase_2_dhkey_checks_are_present,
214     smp_wait_for_both_public_keys,
215     smp_start_passkey_verification,
216     smp_send_keypress_notification,
217     smp_process_keypress_notification,
218     smp_process_secure_connection_oob_data,
219     smp_set_local_oob_keys,
220     smp_set_local_oob_random_commitment,
221     smp_idle_terminate};
222 
223 /************ SMP Master FSM State/Event Indirection Table **************/
224 static const uint8_t smp_master_entry_map[][SMP_STATE_MAX] = {
225     /* state name: */
226     /* Idle, WaitApp Rsp, SecReq Pend, Pair ReqRsp, Wait Cfm, Confirm, Rand,
227        PublKey Exch, SCPhs1 Strt, Wait Cmtm, Wait Nonce, SCPhs2 Strt, Wait
228        DHKChk, DHKChk, Enc Pend, Bond Pend, CrLocSc OobData */
229     /* PAIR_REQ */
230     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
231     /* PAIR_RSP */
232     {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
233     /* CONFIRM */
234     {0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
235     /* RAND */
236     {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
237     /* PAIR_FAIL */
238     {0, 0x81, 0, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
239      0x81, 0, 0x81, 0},
240     /* ENC_INFO */
241     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
242     /* MASTER_ID */
243     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0},
244     /* ID_INFO */
245     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
246     /* ID_ADDR */
247     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0},
248     /* SIGN_INFO */
249     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0},
250     /* SEC_REQ */
251     {2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
252     /* PAIR_PUBLIC_KEY */
253     {0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
254     /* PAIR_DHKEY_CHCK */
255     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
256     /* PAIR_KEYPR_NOTIF */
257     {0, 8, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0},
258     /* PAIR_COMMITM */
259     {0, 0, 0, 0, 0, 0, 0, 0, 6, 1, 0, 0, 0, 0, 0, 0, 0},
260     /* KEY_READY */
261     {0, 3, 0, 3, 1, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 6, 0},
262     /* ENC_CMPL */
263     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
264     /* L2C_CONN */
265     {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
266     /* L2C_DISC */
267     {3, 0x83, 0, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
268      0x83, 0x83, 0x83, 0},
269     /* IO_RSP */
270     {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
271     /* SEC_GRANT */
272     {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
273     /* TK_REQ */
274     {0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0},
275     /* AUTH_CMPL */
276     {4, 0x82, 0, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
277      0x82, 0x82, 0x82, 0},
278     /* ENC_REQ */
279     {0, 4, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0},
280     /* BOND_REQ */
281     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0},
282     /* DISCARD_SEC_REQ */
283     {0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0},
284     /* PUBL_KEY_EXCH_REQ */
285     {0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
286     /* LOC_PUBL_KEY_CRTD */
287     {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
288     /* BOTH_PUBL_KEYS_RCVD */
289     {0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0},
290     /* SC_DHKEY_CMPLT */
291     {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
292     /* HAVE_LOC_NONCE */
293     {0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2},
294     /* SC_PHASE1_CMPLT */
295     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
296     /* SC_CALC_NC */
297     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0},
298     /* SC_DSPL_NC */
299     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
300     /* SC_NC_OK */
301     {0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
302     /* SC_2_DHCK_CHKS_PRES */
303     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
304     /* SC_KEY_READY */
305     {0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
306     /* KEYPR_NOTIF */
307     {0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
308     /* SC_OOB_DATA */
309     {0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
310     /* CR_LOC_SC_OOB_DATA */
311     {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
312 };
313 
314 static const uint8_t smp_all_table[][SMP_SM_NUM_COLS] = {
315     /* Event                  Action             Next State */
316     /* PAIR_FAIL */
317     {SMP_PROC_PAIR_FAIL, SMP_PAIRING_CMPL, SMP_STATE_IDLE},
318     /* AUTH_CMPL */
319     {SMP_SEND_PAIR_FAIL, SMP_PAIRING_CMPL, SMP_STATE_IDLE},
320     /* L2C_DISC */
321     {SMP_PAIR_TERMINATE, SMP_SM_NO_ACTION, SMP_STATE_IDLE}};
322 
323 static const uint8_t smp_master_idle_table[][SMP_SM_NUM_COLS] = {
324     /* Event                  Action               Next State */
325     /* L2C_CONN */
326     {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
327     /* SEC_REQ */
328     {SMP_PROC_SEC_REQ, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
329     /* L2C_DISC */
330     {SMP_IDLE_TERMINATE, SMP_SM_NO_ACTION, SMP_STATE_IDLE},
331     /* AUTH_CMPL */
332     {SMP_PAIRING_CMPL, SMP_SM_NO_ACTION, SMP_STATE_IDLE},
333     /* CR_LOC_SC_OOB_DATA */
334     {SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION,
335      SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA}
336 
337 };
338 
339 static const uint8_t smp_master_wait_for_app_response_table[][SMP_SM_NUM_COLS] =
340     {
341         /* Event                Action               Next State */
342         /* SEC_GRANT */
343         {SMP_PROC_SEC_GRANT, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
344         /* IO_RSP */
345         {SMP_SEND_PAIR_REQ, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
346 
347         /* TK ready */
348         /* KEY_READY */
349         {SMP_GENERATE_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_WAIT_CONFIRM},
350 
351         /* start enc mode setup */
352         /* ENC_REQ */
353         {SMP_START_ENC, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
354         /* DISCARD_SEC_REQ */
355         {SMP_PROC_DISCARD, SMP_SM_NO_ACTION, SMP_STATE_IDLE}
356         /* user confirms NC 'OK', i.e. phase 1 is completed */
357         /* SC_NC_OK */,
358         {SMP_MOVE_TO_SEC_CONN_PHASE2, SMP_SM_NO_ACTION,
359          SMP_STATE_SEC_CONN_PHS2_START},
360         /* user-provided passkey is rcvd */
361         /* SC_KEY_READY */
362         {SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION,
363          SMP_STATE_SEC_CONN_PHS1_START},
364         /* PAIR_KEYPR_NOTIF */
365         {SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK,
366          SMP_STATE_WAIT_APP_RSP},
367         /* KEYPR_NOTIF */
368         {SMP_SEND_KEYPRESS_NOTIFICATION, SMP_SM_NO_ACTION,
369          SMP_STATE_WAIT_APP_RSP},
370         /* SC_OOB_DATA */
371         {SMP_USE_OOB_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH}};
372 
373 static const uint8_t smp_master_pair_request_response_table[][SMP_SM_NUM_COLS] =
374     {
375         /* Event                  Action            Next State */
376         /* PAIR_RSP */
377         {SMP_PROC_PAIR_CMD, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
378         /* TK_REQ */
379         {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
380 
381         /* TK ready */
382         /* KEY_READY */
383         {SMP_GENERATE_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_WAIT_CONFIRM}
384         /* PUBL_KEY_EXCH_REQ */,
385         {SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH}};
386 
387 static const uint8_t smp_master_wait_for_confirm_table[][SMP_SM_NUM_COLS] = {
388     /* Event                Action            Next State */
389     /* KEY_READY*/
390     /* CONFIRM ready */
391     {SMP_SEND_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM}};
392 
393 static const uint8_t smp_master_confirm_table[][SMP_SM_NUM_COLS] = {
394     /* Event            Action         Next State */
395     /* CONFIRM */
396     {SMP_PROC_CONFIRM, SMP_SEND_RAND, SMP_STATE_RAND}};
397 
398 static const uint8_t smp_master_rand_table[][SMP_SM_NUM_COLS] = {
399     /*               Event                  Action Next State */
400     /* RAND */
401     {SMP_PROC_RAND, SMP_GENERATE_COMPARE, SMP_STATE_RAND},
402     /* KEY_READY */
403     {SMP_PROC_COMPARE, SMP_SM_NO_ACTION, SMP_STATE_RAND}, /* Compare ready */
404     /* ENC_REQ */
405     {SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING}};
406 
407 static const uint8_t smp_master_public_key_exchange_table[][SMP_SM_NUM_COLS] = {
408     /* Event                        Action              Next State */
409     /* LOC_PUBL_KEY_CRTD */
410     {SMP_SEND_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
411     /* PAIR_PUBLIC_KEY */
412     {SMP_PROCESS_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
413     /* BOTH_PUBL_KEYS_RCVD */
414     {SMP_HAVE_BOTH_PUBLIC_KEYS, SMP_SM_NO_ACTION,
415      SMP_STATE_SEC_CONN_PHS1_START},
416 };
417 
418 static const uint8_t smp_master_sec_conn_phs1_start_table[][SMP_SM_NUM_COLS] = {
419     /* Event                  Action                Next State */
420     /* SC_DHKEY_CMPLT */
421     {SMP_START_SEC_CONN_PHASE1, SMP_SM_NO_ACTION,
422      SMP_STATE_SEC_CONN_PHS1_START},
423     /* HAVE_LOC_NONCE */
424     {SMP_PROCESS_LOCAL_NONCE, SMP_SM_NO_ACTION, SMP_STATE_WAIT_COMMITMENT},
425     /* TK_REQ */
426     {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
427     /* SMP_MODEL_SEC_CONN_PASSKEY_DISP model, passkey is sent up to display,*/
428     /* It's time to start commitment calculation */
429     /* KEY_READY */
430     {SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION,
431      SMP_STATE_SEC_CONN_PHS1_START},
432     /* PAIR_KEYPR_NOTIF */
433     {SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK,
434      SMP_STATE_SEC_CONN_PHS1_START},
435     /* PAIR_COMMITM */
436     {SMP_PROCESS_PAIRING_COMMITMENT, SMP_SM_NO_ACTION,
437      SMP_STATE_SEC_CONN_PHS1_START},
438 };
439 
440 static const uint8_t smp_master_wait_commitment_table[][SMP_SM_NUM_COLS] = {
441     /* Event                  Action                 Next State */
442     /* PAIR_COMMITM */
443     {SMP_PROCESS_PAIRING_COMMITMENT, SMP_SEND_RAND, SMP_STATE_WAIT_NONCE},
444     /* PAIR_KEYPR_NOTIF */
445     {SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK,
446      SMP_STATE_WAIT_COMMITMENT},
447 };
448 
449 static const uint8_t smp_master_wait_nonce_table[][SMP_SM_NUM_COLS] = {
450     /* Event                  Action                 Next State */
451     /* peer nonce is received */
452     /* RAND */
453     {SMP_PROC_RAND, SMP_PROCESS_PEER_NONCE, SMP_STATE_SEC_CONN_PHS2_START},
454     /* NC model, time to calculate number for NC */
455     /* SC_CALC_NC */
456     {SMP_CALCULATE_NUMERIC_COMPARISON_DISPLAY_NUMBER, SMP_SM_NO_ACTION,
457      SMP_STATE_WAIT_NONCE},
458     /* NC model, time to display calculated number for NC to the user */
459     /* SC_DSPL_NC */
460     {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
461 };
462 
463 static const uint8_t smp_master_sec_conn_phs2_start_table[][SMP_SM_NUM_COLS] = {
464     /* Event                           Action                 Next State */
465     /* SC_PHASE1_CMPLT */
466     {SMP_CALCULATE_LOCAL_DHKEY_CHECK, SMP_SEND_DHKEY_CHECK,
467      SMP_STATE_WAIT_DHK_CHECK},
468 };
469 
470 static const uint8_t smp_master_wait_dhk_check_table[][SMP_SM_NUM_COLS] = {
471     /* Event                  Action                          Next State */
472     /* PAIR_DHKEY_CHCK */
473     {SMP_PROCESS_DHKEY_CHECK, SMP_CALCULATE_PEER_DHKEY_CHECK,
474      SMP_STATE_DHK_CHECK},
475 };
476 
477 static const uint8_t smp_master_dhk_check_table[][SMP_SM_NUM_COLS] = {
478     /* Event                  Action                 Next State */
479     /* locally calculated peer dhkey check is ready -> compare it withs DHKey
480      * Check
481      * actually received from peer */
482     /* SC_KEY_READY */
483     {SMP_MATCH_DHKEY_CHECKS, SMP_SM_NO_ACTION, SMP_STATE_DHK_CHECK},
484     /* locally calculated peer dhkey check is ready -> calculate STK, go to
485      * sending
486      */
487     /* HCI LE Start Encryption command */
488     /* ENC_REQ */
489     {SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
490 };
491 
492 static const uint8_t smp_master_enc_pending_table[][SMP_SM_NUM_COLS] = {
493     /* Event                  Action                 Next State */
494     /* STK ready */
495     /* KEY_READY */
496     {SMP_START_ENC, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
497     /* ENCRYPTED */
498     {SMP_CHECK_AUTH_REQ, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
499     /* BOND_REQ */
500     {SMP_KEY_DISTRIBUTE, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING}};
501 static const uint8_t smp_master_bond_pending_table[][SMP_SM_NUM_COLS] = {
502     /* Event                  Action                 Next State */
503     /* ENC_INFO */
504     {SMP_PROC_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
505     /* ID_INFO */
506     {SMP_PROC_ID_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
507     /* SIGN_INFO */
508     {SMP_PROC_SRK_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
509     /* MASTER_ID */
510     {SMP_PROC_MASTER_ID, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
511     /* ID_ADDR */
512     {SMP_PROC_ID_ADDR, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
513     /* KEY_READY */
514     /* LTK ready */
515     {SMP_SEND_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING}};
516 
517 static const uint8_t
518     smp_master_create_local_sec_conn_oob_data[][SMP_SM_NUM_COLS] = {
519         /* Event                   Action            Next State */
520         /* LOC_PUBL_KEY_CRTD */
521         {SMP_SET_LOCAL_OOB_KEYS, SMP_SM_NO_ACTION,
522          SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA},
523         /* HAVE_LOC_NONCE */
524         {SMP_SET_LOCAL_OOB_RAND_COMMITMENT, SMP_SM_NO_ACTION, SMP_STATE_IDLE}};
525 
526 /************ SMP Slave FSM State/Event Indirection Table **************/
527 static const uint8_t smp_slave_entry_map[][SMP_STATE_MAX] = {
528     /* state name: */
529     /* Idle, WaitApp Rsp, SecReq Pend, Pair ReqRsp, Wait Cfm, Confirm, Rand,
530        PublKey Exch, SCPhs1 Strt, Wait Cmtm, Wait Nonce, SCPhs2 Strt, Wait
531        DHKChk, DHKChk, Enc Pend, Bond Pend, CrLocSc OobData */
532     /* PAIR_REQ */
533     {2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
534     /* PAIR_RSP */
535     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
536     /* CONFIRM */
537     {0, 4, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
538     /* RAND */
539     {0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
540     /* PAIR_FAIL */
541     {0, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
542      0x81, 0x81, 0, 0},
543     /* ENC_INFO */
544     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0},
545     /* MASTER_ID */
546     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0},
547     /* ID_INFO */
548     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0},
549     /* ID_ADDR */
550     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0},
551     /* SIGN_INFO */
552     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0},
553     /* SEC_REQ */
554     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
555     /* PAIR_PUBLIC_KEY */
556     {0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
557     /* PAIR_DHKEY_CHCK */
558     {0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0},
559     /* PAIR_KEYPR_NOTIF */
560     {0, 9, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0},
561     /* PAIR_COMMITM */
562     {0, 8, 0, 0, 0, 0, 0, 0, 6, 1, 0, 0, 0, 0, 0, 0, 0},
563     /* KEY_READY */
564     {0, 3, 0, 3, 2, 2, 1, 0, 4, 0, 0, 0, 0, 0, 2, 1, 0},
565     /* ENC_CMPL */
566     {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0},
567     /* L2C_CONN */
568     {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
569     /* L2C_DISC */
570     {0, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83,
571      0x83, 0x83, 0x83, 0},
572     /* IO_RSP */
573     {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
574     /* SEC_GRANT */
575     {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
576     /* TK_REQ */
577     {0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0},
578     /* AUTH_CMPL */
579     {0, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
580      0x82, 0x82, 0x82, 0},
581     /* ENC_REQ */
582     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
583     /* BOND_REQ */
584     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0},
585     /* DISCARD_SEC_REQ */
586     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
587     /* PUBL_KEY_EXCH_REQ */
588     {0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
589     /* LOC_PUBL_KEY_CRTD */
590     {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
591     /* BOTH_PUBL_KEYS_RCVD */
592     {0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0},
593     /* SC_DHKEY_CMPLT */
594     {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
595     /* HAVE_LOC_NONCE */
596     {0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2},
597     /* SC_PHASE1_CMPLT */
598     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
599     /* SC_CALC_NC */
600     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0},
601     /* SC_DSPL_NC */
602     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
603     /* SC_NC_OK */
604     {0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
605     /* SC_2_DHCK_CHKS_PRES */
606     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0},
607     /* SC_KEY_READY */
608     {0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
609     /* KEYPR_NOTIF */
610     {0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
611     /* SC_OOB_DATA */
612     {0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
613     /* CR_LOC_SC_OOB_DATA */
614     {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
615 };
616 
617 static const uint8_t smp_slave_idle_table[][SMP_SM_NUM_COLS] = {
618     /* Event                 Action                Next State */
619     /* L2C_CONN */
620     {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
621     /* PAIR_REQ */
622     {SMP_PROC_PAIR_CMD, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
623     /* CR_LOC_SC_OOB_DATA */
624     {SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION,
625      SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA}};
626 
627 static const uint8_t smp_slave_wait_for_app_response_table[][SMP_SM_NUM_COLS] =
628     {
629         /* Event                   Action                 Next State */
630         /* IO_RSP */
631         {SMP_PROC_IO_RSP, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
632         /* SEC_GRANT */
633         {SMP_PROC_SEC_GRANT, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
634 
635         /* TK ready */
636         /* KEY_READY */
637         {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
638         /* CONFIRM */
639         {SMP_PROC_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM},
640         /* DHKey Check from master is received before phase 1 is completed -
641            race */
642         /* PAIR_DHKEY_CHCK */
643         {SMP_PROCESS_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
644         /* user confirms NC 'OK', i.e. phase 1 is completed */
645         /* SC_NC_OK */
646         {SMP_MOVE_TO_SEC_CONN_PHASE2, SMP_SM_NO_ACTION,
647          SMP_STATE_SEC_CONN_PHS2_START},
648         /* user-provided passkey is rcvd */
649         /* SC_KEY_READY */
650         {SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION,
651          SMP_STATE_SEC_CONN_PHS1_START},
652         /* PAIR_COMMITM */
653         {SMP_PROCESS_PAIRING_COMMITMENT, SMP_SM_NO_ACTION,
654          SMP_STATE_WAIT_APP_RSP},
655         /* PAIR_KEYPR_NOTIF */
656         {SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK,
657          SMP_STATE_WAIT_APP_RSP},
658         /* KEYPR_NOTIF */
659         {SMP_SEND_KEYPRESS_NOTIFICATION, SMP_SM_NO_ACTION,
660          SMP_STATE_WAIT_APP_RSP},
661         /* SC_OOB_DATA */
662         {SMP_SEND_PAIR_RSP, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
663 };
664 
665 static const uint8_t smp_slave_sec_request_table[][SMP_SM_NUM_COLS] = {
666     /* Event                  Action                 Next State */
667     /* PAIR_REQ */
668     {SMP_PROC_PAIR_CMD, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
669     /* ENCRYPTED*/
670     {SMP_ENC_CMPL, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
671 };
672 
673 static const uint8_t smp_slave_pair_request_response_table[][SMP_SM_NUM_COLS] =
674     {
675         /* Event                  Action                 Next State */
676         /* CONFIRM */
677         {SMP_PROC_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM},
678         /* TK_REQ */
679         {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
680 
681         /* TK/Confirm ready */
682         /* KEY_READY */
683         {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
684         /* PUBL_KEY_EXCH_REQ */
685         {SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
686         /* PAIR_PUBLIC_KEY */
687         {SMP_PROCESS_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
688 };
689 
690 static const uint8_t smp_slave_wait_confirm_table[][SMP_SM_NUM_COLS] = {
691     /* Event                  Action                 Next State */
692     /* CONFIRM */
693     {SMP_PROC_CONFIRM, SMP_SEND_CONFIRM, SMP_STATE_CONFIRM},
694     /* KEY_READY*/
695     {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_WAIT_CONFIRM}};
696 
697 static const uint8_t smp_slave_confirm_table[][SMP_SM_NUM_COLS] = {
698     /* Event                  Action                 Next State */
699     /* RAND */
700     {SMP_PROC_RAND, SMP_GENERATE_COMPARE, SMP_STATE_RAND},
701 
702     /* TK/Confirm ready */
703     /* KEY_READY*/
704     {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM}};
705 
706 static const uint8_t smp_slave_rand_table[][SMP_SM_NUM_COLS] = {
707     /* Event                  Action                 Next State */
708     /* KEY_READY */
709     {SMP_PROC_COMPARE, SMP_SM_NO_ACTION, SMP_STATE_RAND}, /* compare match */
710     /* RAND */
711     {SMP_SEND_RAND, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING}};
712 
713 static const uint8_t smp_slave_public_key_exch_table[][SMP_SM_NUM_COLS] = {
714     /* Event                  Action                 Next State */
715     /* LOC_PUBL_KEY_CRTD */
716     {SMP_WAIT_FOR_BOTH_PUBLIC_KEYS, SMP_SM_NO_ACTION,
717      SMP_STATE_PUBLIC_KEY_EXCH},
718     /* PAIR_PUBLIC_KEY */
719     {SMP_PROCESS_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
720     /* BOTH_PUBL_KEYS_RCVD */
721     {SMP_HAVE_BOTH_PUBLIC_KEYS, SMP_SM_NO_ACTION,
722      SMP_STATE_SEC_CONN_PHS1_START},
723 };
724 
725 static const uint8_t smp_slave_sec_conn_phs1_start_table[][SMP_SM_NUM_COLS] = {
726     /* Event                  Action                 Next State */
727     /* SC_DHKEY_CMPLT */
728     {SMP_START_SEC_CONN_PHASE1, SMP_SM_NO_ACTION,
729      SMP_STATE_SEC_CONN_PHS1_START},
730     /* HAVE_LOC_NONCE */
731     {SMP_PROCESS_LOCAL_NONCE, SMP_SM_NO_ACTION, SMP_STATE_WAIT_COMMITMENT},
732     /* TK_REQ */
733     {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
734     /* SMP_MODEL_SEC_CONN_PASSKEY_DISP model, passkey is sent up to display,
735      * it's
736      * time to start */
737     /* commitment calculation */
738     /* KEY_READY */
739     {SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION,
740      SMP_STATE_SEC_CONN_PHS1_START},
741     /* PAIR_KEYPR_NOTIF */
742     {SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK,
743      SMP_STATE_SEC_CONN_PHS1_START},
744     /*COMMIT*/
745     {SMP_PROCESS_PAIRING_COMMITMENT, SMP_SM_NO_ACTION,
746      SMP_STATE_SEC_CONN_PHS1_START},
747 };
748 
749 static const uint8_t smp_slave_wait_commitment_table[][SMP_SM_NUM_COLS] = {
750     /* Event                  Action                 Next State */
751     /* PAIR_COMMITM */
752     {SMP_PROCESS_PAIRING_COMMITMENT, SMP_SEND_COMMITMENT, SMP_STATE_WAIT_NONCE},
753     /* PAIR_KEYPR_NOTIF */
754     {SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK,
755      SMP_STATE_WAIT_COMMITMENT},
756 };
757 
758 static const uint8_t smp_slave_wait_nonce_table[][SMP_SM_NUM_COLS] = {
759     /* Event                  Action                 Next State */
760     /* peer nonce is received */
761     /* RAND */
762     {SMP_PROC_RAND, SMP_PROCESS_PEER_NONCE, SMP_STATE_SEC_CONN_PHS2_START},
763     /* NC model, time to calculate number for NC */
764     /* SC_CALC_NC */
765     {SMP_CALCULATE_NUMERIC_COMPARISON_DISPLAY_NUMBER, SMP_SM_NO_ACTION,
766      SMP_STATE_WAIT_NONCE},
767     /* NC model, time to display calculated number for NC to the user */
768     /* SC_DSPL_NC */
769     {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
770 };
771 
772 static const uint8_t smp_slave_sec_conn_phs2_start_table[][SMP_SM_NUM_COLS] = {
773     /* Event                  Action                 Next State */
774     /* SC_PHASE1_CMPLT */
775     {SMP_CALCULATE_LOCAL_DHKEY_CHECK, SMP_PH2_DHKEY_CHECKS_ARE_PRESENT,
776      SMP_STATE_WAIT_DHK_CHECK},
777     /* DHKey Check from master is received before slave DHKey calculation is
778      * completed - race */
779     /* PAIR_DHKEY_CHCK */
780     {SMP_PROCESS_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS2_START},
781 };
782 
783 static const uint8_t smp_slave_wait_dhk_check_table[][SMP_SM_NUM_COLS] = {
784     /* Event                  Action                 Next State */
785     /* PAIR_DHKEY_CHCK */
786     {SMP_PROCESS_DHKEY_CHECK, SMP_CALCULATE_PEER_DHKEY_CHECK,
787      SMP_STATE_DHK_CHECK},
788     /* DHKey Check from master was received before slave came to this state */
789     /* SC_2_DHCK_CHKS_PRES */
790     {SMP_CALCULATE_PEER_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_DHK_CHECK},
791 };
792 
793 static const uint8_t smp_slave_dhk_check_table[][SMP_SM_NUM_COLS] = {
794     /* Event                  Action                 Next State */
795 
796     /* locally calculated peer dhkey check is ready -> compare it withs DHKey
797      * Check
798      */
799     /* actually received from peer */
800     /* SC_KEY_READY */
801     {SMP_MATCH_DHKEY_CHECKS, SMP_SM_NO_ACTION, SMP_STATE_DHK_CHECK},
802 
803     /* dhkey checks match -> send local dhkey check to master, go to wait for
804      * HCI LE
805      */
806     /* Long Term Key Request Event */
807     /* PAIR_DHKEY_CHCK */
808     {SMP_SEND_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
809 };
810 
811 static const uint8_t smp_slave_enc_pending_table[][SMP_SM_NUM_COLS] = {
812     /* Event                  Action                 Next State */
813     /* ENC_REQ */
814     {SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
815 
816     /* STK ready */
817     /* KEY_READY */
818     {SMP_SEND_LTK_REPLY, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
819     /* ENCRYPTED */
820     {SMP_CHECK_AUTH_REQ, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
821     /* BOND_REQ */
822     {SMP_KEY_DISTRIBUTE, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING}};
823 static const uint8_t smp_slave_bond_pending_table[][SMP_SM_NUM_COLS] = {
824     /* Event                  Action                 Next State */
825 
826     /* LTK ready */
827     /* KEY_READY */
828     {SMP_SEND_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
829 
830     /* rev SRK */
831     /* SIGN_INFO */
832     {SMP_PROC_SRK_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
833     /* ENC_INFO */
834     {SMP_PROC_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
835     /* ID_INFO */
836     {SMP_PROC_ID_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
837     /* MASTER_ID*/
838     {SMP_PROC_MASTER_ID, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
839     /* ID_ADDR */
840     {SMP_PROC_ID_ADDR, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING}
841 
842 };
843 
844 static const uint8_t
845     smp_slave_create_local_sec_conn_oob_data[][SMP_SM_NUM_COLS] = {
846         /* Event                  Action                 Next State */
847         /* LOC_PUBL_KEY_CRTD */
848         {SMP_SET_LOCAL_OOB_KEYS, SMP_SM_NO_ACTION,
849          SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA},
850         /* HAVE_LOC_NONCE */
851         {SMP_SET_LOCAL_OOB_RAND_COMMITMENT, SMP_SM_NO_ACTION, SMP_STATE_IDLE}};
852 
853 static const tSMP_SM_TBL smp_state_table[][2] = {
854     /* SMP_STATE_IDLE */
855     {smp_master_idle_table, smp_slave_idle_table},
856 
857     /* SMP_STATE_WAIT_APP_RSP */
858     {smp_master_wait_for_app_response_table,
859      smp_slave_wait_for_app_response_table},
860 
861     /* SMP_STATE_SEC_REQ_PENDING */
862     {NULL, smp_slave_sec_request_table},
863 
864     /* SMP_STATE_PAIR_REQ_RSP */
865     {smp_master_pair_request_response_table,
866      smp_slave_pair_request_response_table},
867 
868     /* SMP_STATE_WAIT_CONFIRM */
869     {smp_master_wait_for_confirm_table, smp_slave_wait_confirm_table},
870 
871     /* SMP_STATE_CONFIRM */
872     {smp_master_confirm_table, smp_slave_confirm_table},
873 
874     /* SMP_STATE_RAND */
875     {smp_master_rand_table, smp_slave_rand_table},
876 
877     /* SMP_STATE_PUBLIC_KEY_EXCH */
878     {smp_master_public_key_exchange_table, smp_slave_public_key_exch_table},
879 
880     /* SMP_STATE_SEC_CONN_PHS1_START */
881     {smp_master_sec_conn_phs1_start_table, smp_slave_sec_conn_phs1_start_table},
882 
883     /* SMP_STATE_WAIT_COMMITMENT */
884     {smp_master_wait_commitment_table, smp_slave_wait_commitment_table},
885 
886     /* SMP_STATE_WAIT_NONCE */
887     {smp_master_wait_nonce_table, smp_slave_wait_nonce_table},
888 
889     /* SMP_STATE_SEC_CONN_PHS2_START */
890     {smp_master_sec_conn_phs2_start_table, smp_slave_sec_conn_phs2_start_table},
891 
892     /* SMP_STATE_WAIT_DHK_CHECK */
893     {smp_master_wait_dhk_check_table, smp_slave_wait_dhk_check_table},
894 
895     /* SMP_STATE_DHK_CHECK */
896     {smp_master_dhk_check_table, smp_slave_dhk_check_table},
897 
898     /* SMP_STATE_ENCRYPTION_PENDING */
899     {smp_master_enc_pending_table, smp_slave_enc_pending_table},
900 
901     /* SMP_STATE_BOND_PENDING */
902     {smp_master_bond_pending_table, smp_slave_bond_pending_table},
903 
904     /* SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA */
905     {smp_master_create_local_sec_conn_oob_data,
906      smp_slave_create_local_sec_conn_oob_data}};
907 
908 typedef const uint8_t (*tSMP_ENTRY_TBL)[SMP_STATE_MAX];
909 static const tSMP_ENTRY_TBL smp_entry_table[] = {smp_master_entry_map,
910                                                  smp_slave_entry_map};
911 
912 tSMP_CB smp_cb;
913 
914 #define SMP_ALL_TBL_MASK 0x80
915 
916 /*******************************************************************************
917  * Function     smp_set_state
918  * Returns      None
919  ******************************************************************************/
smp_set_state(tSMP_STATE state)920 void smp_set_state(tSMP_STATE state) {
921   if (state < SMP_STATE_MAX) {
922     SMP_TRACE_DEBUG("State change: %s(%d) ==> %s(%d)",
923                     smp_get_state_name(smp_cb.state), smp_cb.state,
924                     smp_get_state_name(state), state);
925     smp_cb.state = state;
926   } else {
927     SMP_TRACE_DEBUG("smp_set_state invalid state =%d", state);
928   }
929 }
930 
931 /*******************************************************************************
932  * Function     smp_get_state
933  * Returns      The smp state
934  ******************************************************************************/
smp_get_state(void)935 tSMP_STATE smp_get_state(void) { return smp_cb.state; }
936 
937 /*******************************************************************************
938  *
939  * Function     smp_sm_event
940  *
941  * Description  Handle events to the state machine. It looks up the entry
942  *              in the smp_entry_table array.
943  *              If it is a valid entry, it gets the state table. Set the next
944  *              state, if not NULL state. Execute the action function according
945  *              to the state table. If the state returned by action function is
946  *              not NULL state, adjust the new state to the returned state. If
947  *              (api_evt != MAX), call callback function.
948  *
949  * Returns      void.
950  *
951  ******************************************************************************/
smp_sm_event(tSMP_CB * p_cb,tSMP_EVENT event,tSMP_INT_DATA * p_data)952 void smp_sm_event(tSMP_CB* p_cb, tSMP_EVENT event, tSMP_INT_DATA* p_data) {
953   uint8_t curr_state = p_cb->state;
954   tSMP_SM_TBL state_table;
955   uint8_t action, entry, i;
956 
957   if (p_cb->role >= 2) {
958     SMP_TRACE_DEBUG("Invalid role: %d", p_cb->role);
959     android_errorWriteLog(0x534e4554, "74121126");
960     return;
961   }
962 
963   tSMP_ENTRY_TBL entry_table = smp_entry_table[p_cb->role];
964 
965   SMP_TRACE_EVENT("main smp_sm_event");
966   if (curr_state >= SMP_STATE_MAX) {
967     SMP_TRACE_DEBUG("Invalid state: %d", curr_state);
968     return;
969   }
970 
971   SMP_TRACE_DEBUG("SMP Role: %s State: [%s (%d)], Event: [%s (%d)]",
972                   (p_cb->role == 0x01) ? "Slave" : "Master",
973                   smp_get_state_name(p_cb->state), p_cb->state,
974                   smp_get_event_name(event), event);
975 
976   /* look up the state table for the current state */
977   /* lookup entry /w event & curr_state */
978   /* If entry is ignore, return.
979    * Otherwise, get state table (according to curr_state or all_state) */
980   if ((event <= SMP_MAX_EVT) &&
981       ((entry = entry_table[event - 1][curr_state]) != SMP_SM_IGNORE)) {
982     if (entry & SMP_ALL_TBL_MASK) {
983       entry &= ~SMP_ALL_TBL_MASK;
984       state_table = smp_all_table;
985     } else
986       state_table = smp_state_table[curr_state][p_cb->role];
987   } else {
988     SMP_TRACE_DEBUG("Ignore event [%s (%d)] in state [%s (%d)]",
989                     smp_get_event_name(event), event,
990                     smp_get_state_name(curr_state), curr_state);
991     return;
992   }
993 
994   /* Get possible next state from state table. */
995 
996   smp_set_state(state_table[entry - 1][SMP_SME_NEXT_STATE]);
997 
998   /* If action is not ignore, clear param, exec action and get next state.
999    * The action function may set the Param for cback.
1000    * Depending on param, call cback or free buffer. */
1001   /* execute action */
1002   /* execute action functions */
1003   for (i = 0; i < SMP_NUM_ACTIONS; i++) {
1004     action = state_table[entry - 1][i];
1005     if (action != SMP_SM_NO_ACTION) {
1006       (*smp_sm_action[action])(p_cb, p_data);
1007     } else {
1008       break;
1009     }
1010   }
1011   SMP_TRACE_DEBUG("result state = %s", smp_get_state_name(p_cb->state));
1012 }
1013 
1014 /*******************************************************************************
1015  * Function     smp_get_state_name
1016  * Returns      The smp state name.
1017  ******************************************************************************/
smp_get_state_name(tSMP_STATE state)1018 const char* smp_get_state_name(tSMP_STATE state) {
1019   const char* p_str = smp_state_name[SMP_STATE_MAX];
1020 
1021   if (state < SMP_STATE_MAX) {
1022     p_str = smp_state_name[state];
1023   }
1024   return p_str;
1025 }
1026 
1027 /*******************************************************************************
1028  * Function     smp_get_event_name
1029  * Returns      The smp event name.
1030  ******************************************************************************/
smp_get_event_name(tSMP_EVENT event)1031 const char* smp_get_event_name(tSMP_EVENT event) {
1032   const char* p_str = smp_event_name[SMP_MAX_EVT];
1033 
1034   if (event <= SMP_MAX_EVT) {
1035     p_str = smp_event_name[event - 1];
1036   }
1037   return p_str;
1038 }
1039