smp_main.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. #include "common/bt_target.h"
  19. #if SMP_INCLUDED == TRUE
  20. #include <string.h>
  21. #include "smp_int.h"
  22. const char *const smp_state_name [] = {
  23. "SMP_STATE_IDLE",
  24. "SMP_STATE_WAIT_APP_RSP",
  25. "SMP_STATE_SEC_REQ_PENDING",
  26. "SMP_STATE_PAIR_REQ_RSP",
  27. "SMP_STATE_WAIT_CONFIRM",
  28. "SMP_STATE_CONFIRM",
  29. "SMP_STATE_RAND",
  30. "SMP_STATE_PUBLIC_KEY_EXCH",
  31. "SMP_STATE_SEC_CONN_PHS1_START",
  32. "SMP_STATE_WAIT_COMMITMENT",
  33. "SMP_STATE_WAIT_NONCE",
  34. "SMP_STATE_SEC_CONN_PHS2_START",
  35. "SMP_STATE_WAIT_DHK_CHECK",
  36. "SMP_STATE_DHK_CHECK",
  37. "SMP_STATE_ENCRYPTION_PENDING",
  38. "SMP_STATE_BOND_PENDING",
  39. "SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA",
  40. "SMP_STATE_MAX"
  41. };
  42. const char *const smp_event_name [] = {
  43. "PAIRING_REQ_EVT",
  44. "PAIRING_RSP_EVT",
  45. "CONFIRM_EVT",
  46. "RAND_EVT",
  47. "PAIRING_FAILED_EVT",
  48. "ENC_INFO_EVT",
  49. "MASTER_ID_EVT",
  50. "ID_INFO_EVT",
  51. "ID_ADDR_EVT",
  52. "SIGN_INFO_EVT",
  53. "SECURITY_REQ_EVT",
  54. "PAIR_PUBLIC_KEY_EVT",
  55. "PAIR_DHKEY_CHECK_EVT",
  56. "PAIR_KEYPRESS_NOTIFICATION_EVT",
  57. "PAIR_COMMITMENT_EVT",
  58. "KEY_READY_EVT",
  59. "ENCRYPTED_EVT",
  60. "L2CAP_CONN_EVT",
  61. "L2CAP_DISCONN_EVT",
  62. "API_IO_RSP_EVT",
  63. "API_SEC_GRANT_EVT",
  64. "TK_REQ_EVT",
  65. "AUTH_CMPL_EVT",
  66. "ENC_REQ_EVT",
  67. "BOND_REQ_EVT",
  68. "DISCARD_SEC_REQ_EVT",
  69. "PUBLIC_KEY_EXCHANGE_REQ_EVT",
  70. "LOCAL_PUBLIC_KEY_CRTD_EVT",
  71. "BOTH_PUBLIC_KEYS_RCVD_EVT",
  72. "SEC_CONN_DHKEY_COMPLETE_EVT",
  73. "HAVE_LOCAL_NONCE_EVT",
  74. "SEC_CONN_PHASE1_CMPLT_EVT",
  75. "SEC_CONN_CALC_NC_EVT",
  76. "SEC_CONN_DISPLAY_NC_EVT",
  77. "SEC_CONN_OK_EVT",
  78. "SEC_CONN_2_DHCK_CHECKS_PRESENT_EVT",
  79. "SEC_CONN_KEY_READY_EVT",
  80. "KEYPRESS_NOTIFICATION_EVT",
  81. "SEC_CONN_OOB_DATA_EVT",
  82. "CREATE_LOCAL_SEC_CONN_OOB_DATA_EVT",
  83. "OUT_OF_RANGE_EVT"
  84. };
  85. const char *smp_get_event_name(tSMP_EVENT event);
  86. const char *smp_get_state_name(tSMP_STATE state);
  87. #define SMP_SM_IGNORE 0
  88. #define SMP_NUM_ACTIONS 2
  89. #define SMP_SME_NEXT_STATE 2
  90. #define SMP_SM_NUM_COLS 3
  91. typedef const UINT8(*tSMP_SM_TBL)[SMP_SM_NUM_COLS];
  92. enum {
  93. SMP_PROC_SEC_REQ,
  94. SMP_SEND_PAIR_REQ,
  95. SMP_SEND_PAIR_RSP,
  96. SMP_SEND_CONFIRM,
  97. SMP_SEND_PAIR_FAIL,
  98. SMP_SEND_RAND,
  99. SMP_SEND_ENC_INFO,
  100. SMP_SEND_ID_INFO,
  101. SMP_SEND_LTK_REPLY,
  102. SMP_PROC_PAIR_CMD,
  103. SMP_PROC_PAIR_FAIL,
  104. SMP_PROC_CONFIRM,
  105. SMP_PROC_RAND,
  106. SMP_PROC_ENC_INFO,
  107. SMP_PROC_MASTER_ID,
  108. SMP_PROC_ID_INFO,
  109. SMP_PROC_ID_ADDR,
  110. SMP_PROC_SRK_INFO,
  111. SMP_PROC_SEC_GRANT,
  112. SMP_PROC_SL_KEY,
  113. SMP_PROC_COMPARE,
  114. SMP_PROC_IO_RSP,
  115. SMP_GENERATE_COMPARE,
  116. SMP_GENERATE_CONFIRM,
  117. SMP_GENERATE_STK,
  118. SMP_KEY_DISTRIBUTE,
  119. SMP_START_ENC,
  120. SMP_PAIRING_CMPL,
  121. SMP_DECIDE_ASSO_MODEL,
  122. SMP_SEND_APP_CBACK,
  123. SMP_CHECK_AUTH_REQ,
  124. SMP_PAIR_TERMINATE,
  125. SMP_ENC_CMPL,
  126. SMP_PROC_DISCARD,
  127. SMP_CREATE_PRIVATE_KEY,
  128. SMP_USE_OOB_PRIVATE_KEY,
  129. SMP_SEND_PAIR_PUBLIC_KEY,
  130. SMP_PROCESS_PAIR_PUBLIC_KEY,
  131. SMP_HAVE_BOTH_PUBLIC_KEYS,
  132. SMP_START_SEC_CONN_PHASE1,
  133. SMP_PROCESS_LOCAL_NONCE,
  134. SMP_SEND_COMMITMENT,
  135. SMP_PROCESS_PAIRING_COMMITMENT,
  136. SMP_PROCESS_PEER_NONCE,
  137. SMP_CALCULATE_LOCAL_DHKEY_CHECK,
  138. SMP_SEND_DHKEY_CHECK,
  139. SMP_PROCESS_DHKEY_CHECK,
  140. SMP_CALCULATE_PEER_DHKEY_CHECK,
  141. SMP_MATCH_DHKEY_CHECKS,
  142. SMP_CALCULATE_NUMERIC_COMPARISON_DISPLAY_NUMBER,
  143. SMP_MOVE_TO_SEC_CONN_PHASE2,
  144. SMP_PH2_DHKEY_CHECKS_ARE_PRESENT,
  145. SMP_WAIT_FOR_BOTH_PUBLIC_KEYS,
  146. SMP_START_PASSKEY_VERIFICATION,
  147. SMP_SEND_KEYPRESS_NOTIFICATION,
  148. SMP_PROCESS_KEYPRESS_NOTIFICATION,
  149. SMP_PROCESS_SECURE_CONNECTION_OOB_DATA,
  150. SMP_SET_LOCAL_OOB_KEYS,
  151. SMP_SET_LOCAL_OOB_RAND_COMMITMENT,
  152. SMP_IDLE_TERMINATE,
  153. SMP_FAST_CONN_PARAM,
  154. SMP_SM_NO_ACTION
  155. };
  156. #if (BLE_INCLUDED == TRUE)
  157. static const tSMP_ACT smp_sm_action[SMP_SM_NO_ACTION] = {
  158. smp_proc_sec_req,
  159. smp_send_pair_req,
  160. smp_send_pair_rsp,
  161. smp_send_confirm,
  162. smp_send_pair_fail,
  163. smp_send_rand,
  164. smp_send_enc_info,
  165. smp_send_id_info,
  166. smp_send_ltk_reply,
  167. smp_proc_pair_cmd,
  168. smp_proc_pair_fail,
  169. smp_proc_confirm,
  170. smp_proc_rand,
  171. smp_proc_enc_info,
  172. smp_proc_master_id,
  173. smp_proc_id_info,
  174. smp_proc_id_addr,
  175. smp_proc_srk_info,
  176. smp_proc_sec_grant,
  177. smp_proc_sl_key,
  178. smp_proc_compare,
  179. smp_process_io_response,
  180. smp_generate_compare,
  181. smp_generate_srand_mrand_confirm,
  182. smp_generate_stk,
  183. smp_key_distribution,
  184. smp_start_enc,
  185. smp_pairing_cmpl,
  186. smp_decide_association_model,
  187. smp_send_app_cback,
  188. smp_check_auth_req,
  189. smp_pair_terminate,
  190. smp_enc_cmpl,
  191. smp_proc_discard,
  192. smp_create_private_key,
  193. smp_use_oob_private_key,
  194. smp_send_pair_public_key,
  195. smp_process_pairing_public_key,
  196. smp_both_have_public_keys,
  197. smp_start_secure_connection_phase1,
  198. smp_process_local_nonce,
  199. smp_send_commitment,
  200. smp_process_pairing_commitment,
  201. smp_process_peer_nonce,
  202. smp_calculate_local_dhkey_check,
  203. smp_send_dhkey_check,
  204. smp_process_dhkey_check,
  205. smp_calculate_peer_dhkey_check,
  206. smp_match_dhkey_checks,
  207. smp_calculate_numeric_comparison_display_number,
  208. smp_move_to_secure_connections_phase2,
  209. smp_phase_2_dhkey_checks_are_present,
  210. smp_wait_for_both_public_keys,
  211. smp_start_passkey_verification,
  212. smp_send_keypress_notification,
  213. smp_process_keypress_notification,
  214. smp_process_secure_connection_oob_data,
  215. smp_set_local_oob_keys,
  216. smp_set_local_oob_random_commitment,
  217. smp_idle_terminate,
  218. smp_fast_conn_param
  219. };
  220. #else
  221. static const tSMP_ACT smp_sm_action[SMP_SM_NO_ACTION] = {NULL};
  222. #endif ///BLE_INCLUDED == TRUE
  223. /************ SMP Master FSM State/Event Indirection Table **************/
  224. static const UINT8 smp_master_entry_map[][SMP_STATE_MAX] = {
  225. /* state name: Idle WaitApp SecReq Pair Wait Confirm Rand PublKey SCPhs1 Wait Wait SCPhs2 Wait DHKChk Enc Bond CrLocSc
  226. Rsp Pend ReqRsp Cfm Exch Strt Cmtm Nonce Strt DHKChk Pend Pend OobData */
  227. /* PAIR_REQ */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  228. /* PAIR_RSP */{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  229. /* CONFIRM */{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  230. /* RAND */{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
  231. /* PAIR_FAIL */{ 0, 0x81, 0, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0, 0x81, 0 },
  232. /* ENC_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
  233. /* MASTER_ID */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0 },
  234. /* ID_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 },
  235. /* ID_ADDR */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0 },
  236. /* SIGN_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
  237. /* SEC_REQ */{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  238. /* PAIR_PUBLIC_KEY */{ 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  239. /* PAIR_DHKEY_CHCK */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
  240. /* PAIR_KEYPR_NOTIF */{ 0, 8, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0 },
  241. /* PAIR_COMMITM */{ 0, 0, 0, 0, 0, 0, 0, 0, 6, 1, 0, 0, 0, 0, 0, 0, 0 },
  242. /* KEY_READY */{ 0, 3, 0, 3, 1, 0, 2, 0, 4, 0, 0, 0, 0, 0, 1, 6, 0 },
  243. /* ENC_CMPL */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0 },
  244. /* L2C_CONN */{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  245. /* L2C_DISC */{ 3, 0x83, 0, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0 },
  246. /* IO_RSP */{ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  247. /* SEC_GRANT */{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  248. /* TK_REQ */{ 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 },
  249. /* AUTH_CMPL */{ 4, 0x82, 0, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0 },
  250. /* ENC_REQ */{ 0, 4, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0 },
  251. /* BOND_REQ */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0 },
  252. /* DISCARD_SEC_REQ */{ 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0 },
  253. /* PUBL_KEY_EXCH_REQ */{ 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  254. /* LOC_PUBL_KEY_CRTD */{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  255. /* BOTH_PUBL_KEYS_RCVD */{ 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  256. /* SC_DHKEY_CMPLT */{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
  257. /* HAVE_LOC_NONCE */{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2 },
  258. /* SC_PHASE1_CMPLT */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  259. /* SC_CALC_NC */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
  260. /* SC_DSPL_NC */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0 },
  261. /* SC_NC_OK */{ 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  262. /* SC_2_DHCK_CHKS_PRES */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  263. /* SC_KEY_READY */{ 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
  264. /* KEYPR_NOTIF */{ 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  265. /* SC_OOB_DATA */{ 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  266. /* CR_LOC_SC_OOB_DATA */{ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  267. };
  268. static const UINT8 smp_all_table[][SMP_SM_NUM_COLS] = {
  269. /* Event Action Next State */
  270. /* PAIR_FAIL */ {SMP_PROC_PAIR_FAIL, SMP_PAIRING_CMPL, SMP_STATE_IDLE},
  271. /* AUTH_CMPL */ {SMP_SEND_PAIR_FAIL, SMP_PAIRING_CMPL, SMP_STATE_IDLE},
  272. /* L2C_DISC */ {SMP_PAIR_TERMINATE, SMP_SM_NO_ACTION, SMP_STATE_IDLE}
  273. };
  274. static const UINT8 smp_master_idle_table[][SMP_SM_NUM_COLS] = {
  275. /* Event Action Next State */
  276. /* L2C_CONN */ {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  277. /* SEC_REQ */ {SMP_PROC_SEC_REQ, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
  278. /* L2C_DISC */ {SMP_IDLE_TERMINATE, SMP_SM_NO_ACTION, SMP_STATE_IDLE},
  279. /* AUTH_CMPL */ {SMP_PAIRING_CMPL, SMP_SM_NO_ACTION, SMP_STATE_IDLE}
  280. /* CR_LOC_SC_OOB_DATA */ , {SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA}
  281. };
  282. static const UINT8 smp_master_wait_for_app_response_table[][SMP_SM_NUM_COLS] = {
  283. /* Event Action Next State */
  284. /* SEC_GRANT */ {SMP_PROC_SEC_GRANT, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
  285. /* IO_RSP */ {SMP_SEND_PAIR_REQ, SMP_FAST_CONN_PARAM, SMP_STATE_PAIR_REQ_RSP},
  286. /* TK ready */
  287. /* KEY_READY */ {SMP_GENERATE_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_WAIT_CONFIRM},
  288. /* start enc mode setup */
  289. /* ENC_REQ */ { SMP_START_ENC, SMP_FAST_CONN_PARAM, SMP_STATE_ENCRYPTION_PENDING},
  290. /* DISCARD_SEC_REQ */ { SMP_PROC_DISCARD, SMP_SM_NO_ACTION, SMP_STATE_IDLE}
  291. /* user confirms NC 'OK', i.e. phase 1 is completed */
  292. /* SC_NC_OK */, { SMP_MOVE_TO_SEC_CONN_PHASE2, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS2_START},
  293. /* user-provided passkey is rcvd */
  294. /* SC_KEY_READY */ { SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  295. /* PAIR_KEYPR_NOTIF */ { SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
  296. /* KEYPR_NOTIF */ { SMP_SEND_KEYPRESS_NOTIFICATION, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  297. /* SC_OOB_DATA */ { SMP_USE_OOB_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH}
  298. };
  299. static const UINT8 smp_master_pair_request_response_table[][SMP_SM_NUM_COLS] = {
  300. /* Event Action Next State */
  301. /* PAIR_RSP */ { SMP_PROC_PAIR_CMD, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
  302. /* TK_REQ */ { SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  303. /* TK ready */
  304. /* KEY_READY */{ SMP_GENERATE_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_WAIT_CONFIRM}
  305. /* PUBL_KEY_EXCH_REQ */, { SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH}
  306. };
  307. static const UINT8 smp_master_wait_for_confirm_table[][SMP_SM_NUM_COLS] = {
  308. /* Event Action Next State */
  309. /* KEY_READY*/ {SMP_SEND_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM}/* CONFIRM ready */
  310. };
  311. static const UINT8 smp_master_confirm_table[][SMP_SM_NUM_COLS] = {
  312. /* Event Action Next State */
  313. /* CONFIRM */ { SMP_PROC_CONFIRM, SMP_SEND_RAND, SMP_STATE_RAND}
  314. };
  315. static const UINT8 smp_master_rand_table[][SMP_SM_NUM_COLS] = {
  316. /* Event Action Next State */
  317. /* RAND */ { SMP_PROC_RAND, SMP_GENERATE_COMPARE, SMP_STATE_RAND},
  318. /* KEY_READY*/ { SMP_PROC_COMPARE, SMP_SM_NO_ACTION, SMP_STATE_RAND}, /* Compare ready */
  319. /* ENC_REQ */ { SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING}
  320. };
  321. static const UINT8 smp_master_public_key_exchange_table[][SMP_SM_NUM_COLS] = {
  322. /* Event Action Next State */
  323. /* LOC_PUBL_KEY_CRTD */{ SMP_SEND_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
  324. /* PAIR_PUBLIC_KEY */{ SMP_PROCESS_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
  325. /* BOTH_PUBL_KEYS_RCVD */{ SMP_HAVE_BOTH_PUBLIC_KEYS, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  326. };
  327. static const UINT8 smp_master_sec_conn_phs1_start_table[][SMP_SM_NUM_COLS] = {
  328. /* Event Action Next State */
  329. /* SC_DHKEY_CMPLT */{ SMP_START_SEC_CONN_PHASE1, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  330. /* HAVE_LOC_NONCE */{ SMP_PROCESS_LOCAL_NONCE, SMP_SM_NO_ACTION, SMP_STATE_WAIT_COMMITMENT},
  331. /* TK_REQ */{ SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  332. /* SMP_MODEL_SEC_CONN_PASSKEY_DISP model, passkey is sent up to display,*/
  333. /* It's time to start commitment calculation */
  334. /* KEY_READY */{ SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  335. /* PAIR_KEYPR_NOTIF */{ SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK, SMP_STATE_SEC_CONN_PHS1_START},
  336. /* PAIR_COMMITM */{ SMP_PROCESS_PAIRING_COMMITMENT, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  337. };
  338. static const UINT8 smp_master_wait_commitment_table[][SMP_SM_NUM_COLS] = {
  339. /* Event Action Next State */
  340. /* PAIR_COMMITM */{ SMP_PROCESS_PAIRING_COMMITMENT, SMP_SEND_RAND, SMP_STATE_WAIT_NONCE},
  341. /* PAIR_KEYPR_NOTIF */{ SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_COMMITMENT},
  342. };
  343. static const UINT8 smp_master_wait_nonce_table[][SMP_SM_NUM_COLS] = {
  344. /* Event Action Next State */
  345. /* peer nonce is received */
  346. /* RAND */{SMP_PROC_RAND, SMP_PROCESS_PEER_NONCE, SMP_STATE_SEC_CONN_PHS2_START},
  347. /* NC model, time to calculate number for NC */
  348. /* SC_CALC_NC */{SMP_CALCULATE_NUMERIC_COMPARISON_DISPLAY_NUMBER, SMP_SM_NO_ACTION, SMP_STATE_WAIT_NONCE},
  349. /* NC model, time to display calculated number for NC to the user */
  350. /* SC_DSPL_NC */{SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  351. };
  352. static const UINT8 smp_master_sec_conn_phs2_start_table[][SMP_SM_NUM_COLS] = {
  353. /* Event Action Next State */
  354. /* SC_PHASE1_CMPLT */{SMP_CALCULATE_LOCAL_DHKEY_CHECK, SMP_SEND_DHKEY_CHECK, SMP_STATE_WAIT_DHK_CHECK},
  355. };
  356. static const UINT8 smp_master_wait_dhk_check_table[][SMP_SM_NUM_COLS] = {
  357. /* Event Action Next State */
  358. /* PAIR_DHKEY_CHCK */{SMP_PROCESS_DHKEY_CHECK, SMP_CALCULATE_PEER_DHKEY_CHECK, SMP_STATE_DHK_CHECK},
  359. };
  360. static const UINT8 smp_master_dhk_check_table[][SMP_SM_NUM_COLS] = {
  361. /* Event Action Next State */
  362. /* locally calculated peer dhkey check is ready -> compare it withs DHKey Check actually received from peer */
  363. /* SC_KEY_READY */{SMP_MATCH_DHKEY_CHECKS, SMP_SM_NO_ACTION, SMP_STATE_DHK_CHECK},
  364. /* locally calculated peer dhkey check is ready -> calculate STK, go to sending */
  365. /* HCI LE Start Encryption command */
  366. /* ENC_REQ */{SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
  367. };
  368. static const UINT8 smp_master_enc_pending_table[][SMP_SM_NUM_COLS] = {
  369. /* Event Action Next State */
  370. /* STK ready */
  371. /* KEY_READY */ { SMP_START_ENC, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
  372. /* ENCRYPTED */ { SMP_CHECK_AUTH_REQ, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
  373. /* BOND_REQ */ { SMP_KEY_DISTRIBUTE, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING}
  374. };
  375. static const UINT8 smp_master_bond_pending_table[][SMP_SM_NUM_COLS] = {
  376. /* Event Action Next State */
  377. /* ENC_INFO */ { SMP_PROC_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  378. /* ID_INFO */ { SMP_PROC_ID_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  379. /* SIGN_INFO*/ { SMP_PROC_SRK_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  380. /* MASTER_ID*/ { SMP_PROC_MASTER_ID, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  381. /* ID_ADDR */ { SMP_PROC_ID_ADDR, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  382. /* KEY_READY */{SMP_SEND_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING} /* LTK ready */
  383. };
  384. static const UINT8 smp_master_create_local_sec_conn_oob_data[][SMP_SM_NUM_COLS] = {
  385. /* Event Action Next State */
  386. /* LOC_PUBL_KEY_CRTD */ {SMP_SET_LOCAL_OOB_KEYS, SMP_SM_NO_ACTION, SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA},
  387. /* HAVE_LOC_NONCE */ {SMP_SET_LOCAL_OOB_RAND_COMMITMENT, SMP_SM_NO_ACTION, SMP_STATE_IDLE}
  388. };
  389. /************ SMP Slave FSM State/Event Indirection Table **************/
  390. static const UINT8 smp_slave_entry_map[][SMP_STATE_MAX] = {
  391. /* state name: Idle WaitApp SecReq Pair Wait Confirm Rand PublKey SCPhs1 Wait Wait SCPhs2 Wait DHKChk Enc Bond CrLocSc
  392. Rsp Pend ReqRsp Cfm Exch Strt Cmtm Nonce Strt DHKChk Pend Pend OobData */
  393. /* PAIR_REQ */{ 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  394. /* PAIR_RSP */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  395. /* CONFIRM */{ 0, 4, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  396. /* RAND */{ 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
  397. /* PAIR_FAIL */{ 0, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0, 0 },
  398. /* ENC_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
  399. /* MASTER_ID */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0 },
  400. /* ID_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0 },
  401. /* ID_ADDR */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 },
  402. /* SIGN_INFO */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0 },
  403. /* SEC_REQ */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  404. /* PAIR_PUBLIC_KEY */{ 0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  405. /* PAIR_DHKEY_CHCK */{ 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0 },
  406. /* PAIR_KEYPR_NOTIF */{ 0, 9, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 0, 0, 0, 0, 0 },
  407. /* PAIR_COMMITM */{ 0, 8, 0, 0, 0, 0, 0, 0, 6, 1, 0, 0, 0, 0, 0, 0, 0 },
  408. /* KEY_READY */{ 0, 3, 0, 3, 2, 2, 1, 0, 4, 0, 0, 0, 0, 0, 2, 1, 0 },
  409. /* ENC_CMPL */{ 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0 },
  410. /* L2C_CONN */{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  411. /* L2C_DISC */{ 0, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0x83, 0 },
  412. /* IO_RSP */{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  413. /* SEC_GRANT */{ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  414. /* TK_REQ */{ 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 },
  415. /* AUTH_CMPL */{ 0, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0 },
  416. /* ENC_REQ */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
  417. /* BOND_REQ */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0 },
  418. /* DISCARD_SEC_REQ */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  419. /* PUBL_KEY_EXCH_REQ */{ 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  420. /* LOC_PUBL_KEY_CRTD */{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
  421. /* BOTH_PUBL_KEYS_RCVD */{ 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  422. /* SC_DHKEY_CMPLT */{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
  423. /* HAVE_LOC_NONCE */{ 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2 },
  424. /* SC_PHASE1_CMPLT */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
  425. /* SC_CALC_NC */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0 },
  426. /* SC_DSPL_NC */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0 },
  427. /* SC_NC_OK */{ 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  428. /* SC_2_DHCK_CHKS_PRES */{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0 },
  429. /* SC_KEY_READY */{ 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
  430. /* KEYPR_NOTIF */{ 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  431. /* SC_OOB_DATA */{ 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  432. /* CR_LOC_SC_OOB_DATA */{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  433. };
  434. static const UINT8 smp_slave_idle_table[][SMP_SM_NUM_COLS] = {
  435. /* Event Action Next State */
  436. /* L2C_CONN */ {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  437. /* PAIR_REQ */ {SMP_PROC_PAIR_CMD, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP}
  438. /* CR_LOC_SC_OOB_DATA */ , {SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA}
  439. };
  440. static const UINT8 smp_slave_wait_for_app_response_table [][SMP_SM_NUM_COLS] = {
  441. /* Event Action Next State */
  442. /* IO_RSP */ {SMP_PROC_IO_RSP, SMP_FAST_CONN_PARAM, SMP_STATE_PAIR_REQ_RSP},
  443. /* SEC_GRANT */ {SMP_PROC_SEC_GRANT, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
  444. /* TK ready */
  445. /* KEY_READY */ {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  446. /* CONFIRM */ {SMP_PROC_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM}
  447. /* DHKey Check from master is received before phase 1 is completed - race */
  448. /* PAIR_DHKEY_CHCK */, {SMP_PROCESS_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  449. /* user confirms NC 'OK', i.e. phase 1 is completed */
  450. /* SC_NC_OK */ {SMP_MOVE_TO_SEC_CONN_PHASE2, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS2_START},
  451. /* user-provided passkey is rcvd */
  452. /* SC_KEY_READY */ {SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  453. /* PAIR_COMMITM */ {SMP_PROCESS_PAIRING_COMMITMENT, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  454. /* PAIR_KEYPR_NOTIF */ {SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_APP_RSP},
  455. /* KEYPR_NOTIF */ {SMP_SEND_KEYPRESS_NOTIFICATION, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  456. /* SC_OOB_DATA */ {SMP_SEND_PAIR_RSP, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
  457. };
  458. static const UINT8 smp_slave_sec_request_table[][SMP_SM_NUM_COLS] = {
  459. /* Event Action Next State */
  460. /* PAIR_REQ */{SMP_PROC_PAIR_CMD, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
  461. /* ENCRYPTED*/{SMP_ENC_CMPL, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
  462. };
  463. static const UINT8 smp_slave_pair_request_response_table[][SMP_SM_NUM_COLS] = {
  464. /* Event Action Next State */
  465. /* CONFIRM */ {SMP_PROC_CONFIRM, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM},
  466. /* TK_REQ */ {SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  467. /* TK/Confirm ready */
  468. /* KEY_READY */{SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP}
  469. /* PUBL_KEY_EXCH_REQ */, { SMP_CREATE_PRIVATE_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
  470. /* PAIR_PUBLIC_KEY */ { SMP_PROCESS_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PAIR_REQ_RSP},
  471. };
  472. static const UINT8 smp_slave_wait_confirm_table[][SMP_SM_NUM_COLS] = {
  473. /* Event Action Next State */
  474. /* CONFIRM */ {SMP_PROC_CONFIRM, SMP_SEND_CONFIRM, SMP_STATE_CONFIRM},
  475. /* KEY_READY*/ {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_WAIT_CONFIRM}
  476. };
  477. static const UINT8 smp_slave_confirm_table[][SMP_SM_NUM_COLS] = {
  478. /* Event Action Next State */
  479. /* RAND */ {SMP_PROC_RAND, SMP_GENERATE_COMPARE, SMP_STATE_RAND},
  480. /* TK/Confirm ready */
  481. /* KEY_READY*/ {SMP_PROC_SL_KEY, SMP_SM_NO_ACTION, SMP_STATE_CONFIRM}
  482. };
  483. static const UINT8 smp_slave_rand_table[][SMP_SM_NUM_COLS] = {
  484. /* Event Action Next State */
  485. /* KEY_READY */ {SMP_PROC_COMPARE, SMP_SM_NO_ACTION, SMP_STATE_RAND}, /* compare match */
  486. /* RAND */ {SMP_SEND_RAND, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING}
  487. };
  488. static const UINT8 smp_slave_public_key_exch_table[][SMP_SM_NUM_COLS] = {
  489. /* Event Action Next State */
  490. /* LOC_PUBL_KEY_CRTD */{ SMP_WAIT_FOR_BOTH_PUBLIC_KEYS, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
  491. /* PAIR_PUBLIC_KEY */{ SMP_PROCESS_PAIR_PUBLIC_KEY, SMP_SM_NO_ACTION, SMP_STATE_PUBLIC_KEY_EXCH},
  492. /* BOTH_PUBL_KEYS_RCVD */{ SMP_HAVE_BOTH_PUBLIC_KEYS, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  493. };
  494. static const UINT8 smp_slave_sec_conn_phs1_start_table[][SMP_SM_NUM_COLS] = {
  495. /* Event Action Next State */
  496. /* SC_DHKEY_CMPLT */{ SMP_START_SEC_CONN_PHASE1, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  497. /* HAVE_LOC_NONCE */{ SMP_PROCESS_LOCAL_NONCE, SMP_SM_NO_ACTION, SMP_STATE_WAIT_COMMITMENT},
  498. /* TK_REQ */{ SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  499. /* SMP_MODEL_SEC_CONN_PASSKEY_DISP model, passkey is sent up to display, it's time to start */
  500. /* commitment calculation */
  501. /* KEY_READY */{ SMP_START_PASSKEY_VERIFICATION, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  502. /* PAIR_KEYPR_NOTIF */{ SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK, SMP_STATE_SEC_CONN_PHS1_START},
  503. /*COMMIT*/{SMP_PROCESS_PAIRING_COMMITMENT, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS1_START},
  504. };
  505. static const UINT8 smp_slave_wait_commitment_table[][SMP_SM_NUM_COLS] = {
  506. /* Event Action Next State */
  507. /* PAIR_COMMITM */{SMP_PROCESS_PAIRING_COMMITMENT, SMP_SEND_COMMITMENT, SMP_STATE_WAIT_NONCE},
  508. /* PAIR_KEYPR_NOTIF */{SMP_PROCESS_KEYPRESS_NOTIFICATION, SMP_SEND_APP_CBACK, SMP_STATE_WAIT_COMMITMENT},
  509. };
  510. static const UINT8 smp_slave_wait_nonce_table[][SMP_SM_NUM_COLS] = {
  511. /* Event Action Next State */
  512. /* peer nonce is received */
  513. /* RAND */{SMP_PROC_RAND, SMP_PROCESS_PEER_NONCE, SMP_STATE_SEC_CONN_PHS2_START},
  514. /* NC model, time to calculate number for NC */
  515. /* SC_CALC_NC */{SMP_CALCULATE_NUMERIC_COMPARISON_DISPLAY_NUMBER, SMP_SM_NO_ACTION, SMP_STATE_WAIT_NONCE},
  516. /* NC model, time to display calculated number for NC to the user */
  517. /* SC_DSPL_NC */{SMP_SEND_APP_CBACK, SMP_SM_NO_ACTION, SMP_STATE_WAIT_APP_RSP},
  518. };
  519. static const UINT8 smp_slave_sec_conn_phs2_start_table[][SMP_SM_NUM_COLS] = {
  520. /* Event Action Next State */
  521. /* SC_PHASE1_CMPLT */{SMP_CALCULATE_LOCAL_DHKEY_CHECK, SMP_PH2_DHKEY_CHECKS_ARE_PRESENT, SMP_STATE_WAIT_DHK_CHECK},
  522. /* DHKey Check from master is received before slave DHKey calculation is completed - race */
  523. /* PAIR_DHKEY_CHCK */{SMP_PROCESS_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_SEC_CONN_PHS2_START},
  524. };
  525. static const UINT8 smp_slave_wait_dhk_check_table[][SMP_SM_NUM_COLS] = {
  526. /* Event Action Next State */
  527. /* PAIR_DHKEY_CHCK */{SMP_PROCESS_DHKEY_CHECK, SMP_CALCULATE_PEER_DHKEY_CHECK, SMP_STATE_DHK_CHECK},
  528. /* DHKey Check from master was received before slave came to this state */
  529. /* SC_2_DHCK_CHKS_PRES */{SMP_CALCULATE_PEER_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_DHK_CHECK},
  530. };
  531. static const UINT8 smp_slave_dhk_check_table[][SMP_SM_NUM_COLS] = {
  532. /* Event Action Next State */
  533. /* locally calculated peer dhkey check is ready -> compare it withs DHKey Check */
  534. /* actually received from peer */
  535. /* SC_KEY_READY */{SMP_MATCH_DHKEY_CHECKS, SMP_SM_NO_ACTION, SMP_STATE_DHK_CHECK},
  536. /* dhkey checks match -> send local dhkey check to master, go to wait for HCI LE */
  537. /* Long Term Key Request Event */
  538. /* PAIR_DHKEY_CHCK */{SMP_SEND_DHKEY_CHECK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
  539. };
  540. static const UINT8 smp_slave_enc_pending_table[][SMP_SM_NUM_COLS] = {
  541. /* Event Action Next State */
  542. /* ENC_REQ */ {SMP_GENERATE_STK, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
  543. /* STK ready */
  544. /* KEY_READY */ {SMP_SEND_LTK_REPLY, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
  545. /* ENCRYPTED */ {SMP_CHECK_AUTH_REQ, SMP_SM_NO_ACTION, SMP_STATE_ENCRYPTION_PENDING},
  546. /* BOND_REQ */ {SMP_KEY_DISTRIBUTE, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING}
  547. };
  548. static const UINT8 smp_slave_bond_pending_table[][SMP_SM_NUM_COLS] = {
  549. /* Event Action Next State */
  550. /* LTK ready */
  551. /* KEY_READY */{ SMP_SEND_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  552. /* rev SRK */
  553. /* SIGN_INFO */{ SMP_PROC_SRK_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  554. /* ENC_INFO */ { SMP_PROC_ENC_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  555. /* ID_INFO */ { SMP_PROC_ID_INFO, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  556. /* MASTER_ID*/ { SMP_PROC_MASTER_ID, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING},
  557. /* ID_ADDR */ { SMP_PROC_ID_ADDR, SMP_SM_NO_ACTION, SMP_STATE_BOND_PENDING}
  558. };
  559. static const UINT8 smp_slave_create_local_sec_conn_oob_data[][SMP_SM_NUM_COLS] = {
  560. /* Event Action Next State */
  561. /* LOC_PUBL_KEY_CRTD */ {SMP_SET_LOCAL_OOB_KEYS, SMP_SM_NO_ACTION, SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA},
  562. /* HAVE_LOC_NONCE */ {SMP_SET_LOCAL_OOB_RAND_COMMITMENT, SMP_SM_NO_ACTION, SMP_STATE_IDLE}
  563. };
  564. static const tSMP_SM_TBL smp_state_table[][2] = {
  565. /* SMP_STATE_IDLE */
  566. {smp_master_idle_table, smp_slave_idle_table},
  567. /* SMP_STATE_WAIT_APP_RSP */
  568. {smp_master_wait_for_app_response_table, smp_slave_wait_for_app_response_table},
  569. /* SMP_STATE_SEC_REQ_PENDING */
  570. {NULL, smp_slave_sec_request_table},
  571. /* SMP_STATE_PAIR_REQ_RSP */
  572. {smp_master_pair_request_response_table, smp_slave_pair_request_response_table},
  573. /* SMP_STATE_WAIT_CONFIRM */
  574. {smp_master_wait_for_confirm_table, smp_slave_wait_confirm_table},
  575. /* SMP_STATE_CONFIRM */
  576. {smp_master_confirm_table, smp_slave_confirm_table},
  577. /* SMP_STATE_RAND */
  578. {smp_master_rand_table, smp_slave_rand_table},
  579. /* SMP_STATE_PUBLIC_KEY_EXCH */
  580. {smp_master_public_key_exchange_table, smp_slave_public_key_exch_table},
  581. /* SMP_STATE_SEC_CONN_PHS1_START */
  582. {smp_master_sec_conn_phs1_start_table, smp_slave_sec_conn_phs1_start_table},
  583. /* SMP_STATE_WAIT_COMMITMENT */
  584. {smp_master_wait_commitment_table, smp_slave_wait_commitment_table},
  585. /* SMP_STATE_WAIT_NONCE */
  586. {smp_master_wait_nonce_table, smp_slave_wait_nonce_table},
  587. /* SMP_STATE_SEC_CONN_PHS2_START */
  588. {smp_master_sec_conn_phs2_start_table, smp_slave_sec_conn_phs2_start_table},
  589. /* SMP_STATE_WAIT_DHK_CHECK */
  590. {smp_master_wait_dhk_check_table, smp_slave_wait_dhk_check_table},
  591. /* SMP_STATE_DHK_CHECK */
  592. {smp_master_dhk_check_table, smp_slave_dhk_check_table},
  593. /* SMP_STATE_ENCRYPTION_PENDING */
  594. {smp_master_enc_pending_table, smp_slave_enc_pending_table},
  595. /* SMP_STATE_BOND_PENDING */
  596. {smp_master_bond_pending_table, smp_slave_bond_pending_table},
  597. /* SMP_STATE_CREATE_LOCAL_SEC_CONN_OOB_DATA */
  598. {smp_master_create_local_sec_conn_oob_data, smp_slave_create_local_sec_conn_oob_data}
  599. };
  600. typedef const UINT8 (*tSMP_ENTRY_TBL)[SMP_STATE_MAX];
  601. static const tSMP_ENTRY_TBL smp_entry_table[] = {
  602. smp_master_entry_map,
  603. smp_slave_entry_map
  604. };
  605. #if SMP_DYNAMIC_MEMORY == FALSE
  606. tSMP_CB smp_cb;
  607. #else
  608. tSMP_CB *smp_cb_ptr;
  609. #endif
  610. #define SMP_ALL_TBL_MASK 0x80
  611. /*******************************************************************************
  612. ** Function smp_set_state
  613. ** Returns None
  614. *******************************************************************************/
  615. void smp_set_state(tSMP_STATE state)
  616. {
  617. if (state < SMP_STATE_MAX) {
  618. SMP_TRACE_DEBUG( "State change: %s(%d) ==> %s(%d)",
  619. smp_get_state_name(smp_cb.state), smp_cb.state,
  620. smp_get_state_name(state), state );
  621. smp_cb.state = state;
  622. } else {
  623. SMP_TRACE_DEBUG("smp_set_state invalid state =%d", state );
  624. }
  625. }
  626. /*******************************************************************************
  627. ** Function smp_get_state
  628. ** Returns The smp state
  629. *******************************************************************************/
  630. tSMP_STATE smp_get_state(void)
  631. {
  632. return smp_cb.state;
  633. }
  634. /*******************************************************************************
  635. **
  636. ** Function smp_sm_event
  637. **
  638. ** Description Handle events to the state machine. It looks up the entry
  639. ** in the smp_entry_table array.
  640. ** If it is a valid entry, it gets the state table.Set the next state,
  641. ** if not NULL state.Execute the action function according to the
  642. ** state table. If the state returned by action function is not NULL
  643. ** state, adjust the new state to the returned state.If (api_evt != MAX),
  644. ** call callback function.
  645. **
  646. ** Returns void.
  647. **
  648. *******************************************************************************/
  649. void smp_sm_event(tSMP_CB *p_cb, tSMP_EVENT event, void *p_data)
  650. {
  651. UINT8 curr_state = p_cb->state;
  652. tSMP_SM_TBL state_table;
  653. UINT8 action, entry, i;
  654. tSMP_ENTRY_TBL entry_table = smp_entry_table[p_cb->role];
  655. SMP_TRACE_EVENT("main smp_sm_event\n");
  656. if (curr_state >= SMP_STATE_MAX) {
  657. SMP_TRACE_DEBUG( "Invalid state: %d\n", curr_state) ;
  658. return;
  659. }
  660. SMP_TRACE_DEBUG( "SMP Role: %s State: [%s (%d)], Event: [%s (%d)]", \
  661. (p_cb->role == 0x01) ? "Slave" : "Master\n", smp_get_state_name( p_cb->state),
  662. p_cb->state, smp_get_event_name(event), event) ;
  663. /* look up the state table for the current state */
  664. /* lookup entry /w event & curr_state */
  665. /* If entry is ignore, return.
  666. * Otherwise, get state table (according to curr_state or all_state) */
  667. if ((event <= SMP_MAX_EVT) && ( (entry = entry_table[event - 1][curr_state]) != SMP_SM_IGNORE )) {
  668. if (entry & SMP_ALL_TBL_MASK) {
  669. entry &= ~SMP_ALL_TBL_MASK;
  670. state_table = smp_all_table;
  671. } else {
  672. state_table = smp_state_table[curr_state][p_cb->role ? 1 : 0];
  673. }
  674. } else {
  675. SMP_TRACE_DEBUG( "Ignore event [%s (%d)] in state [%s (%d)]\n",
  676. smp_get_event_name(event), event, smp_get_state_name(curr_state),
  677. curr_state);
  678. return;
  679. }
  680. /* Get possible next state from state table. */
  681. smp_set_state(state_table[entry - 1][SMP_SME_NEXT_STATE]);
  682. /* If action is not ignore, clear param, exec action and get next state.
  683. * The action function may set the Param for cback.
  684. * Depending on param, call cback or free buffer. */
  685. /* execute action */
  686. /* execute action functions */
  687. for (i = 0; i < SMP_NUM_ACTIONS; i++) {
  688. if ((action = state_table[entry - 1][i]) != SMP_SM_NO_ACTION && smp_sm_action[action] != NULL) {
  689. (*smp_sm_action[action])(p_cb, (tSMP_INT_DATA *)p_data);
  690. } else {
  691. break;
  692. }
  693. }
  694. SMP_TRACE_DEBUG( "result state = %s\n", smp_get_state_name( p_cb->state ) ) ;
  695. }
  696. /*******************************************************************************
  697. ** Function smp_get_state_name
  698. ** Returns The smp state name.
  699. *******************************************************************************/
  700. const char *smp_get_state_name(tSMP_STATE state)
  701. {
  702. const char *p_str = smp_state_name[SMP_STATE_MAX];
  703. if (state < SMP_STATE_MAX) {
  704. p_str = smp_state_name[state];
  705. }
  706. return p_str;
  707. }
  708. /*******************************************************************************
  709. ** Function smp_get_event_name
  710. ** Returns The smp event name.
  711. *******************************************************************************/
  712. const char *smp_get_event_name(tSMP_EVENT event)
  713. {
  714. const char *p_str = smp_event_name[SMP_MAX_EVT];
  715. if (event <= SMP_MAX_EVT) {
  716. p_str = smp_event_name[event - 1];
  717. }
  718. return p_str;
  719. }
  720. #endif