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