smp_keys.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-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. *
  20. * This file contains security manager protocol utility functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #if (BLE_INCLUDED == TRUE && SMP_INCLUDED == TRUE)
  25. #if SMP_DEBUG == TRUE
  26. #include <stdio.h>
  27. #endif
  28. #include <string.h>
  29. //#include "bt_utils.h"
  30. #include "stack/btm_ble_api.h"
  31. #include "smp_int.h"
  32. #include "btm_int.h"
  33. #include "btm_ble_int.h"
  34. #include "stack/hcimsgs.h"
  35. #include "aes.h"
  36. #include "p_256_ecc_pp.h"
  37. #include "device/controller.h"
  38. #ifndef SMP_MAX_ENC_REPEAT
  39. #define SMP_MAX_ENC_REPEAT 3
  40. #endif
  41. static void smp_rand_back(tBTM_RAND_ENC *p);
  42. static void smp_generate_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
  43. static void smp_generate_ltk_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data);
  44. static void smp_generate_y(tSMP_CB *p_cb, tSMP_INT_DATA *p);
  45. static void smp_generate_rand_vector (tSMP_CB *p_cb, tSMP_INT_DATA *p);
  46. static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p);
  47. static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p);
  48. static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p);
  49. static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p);
  50. static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p);
  51. static BOOLEAN smp_calculate_legacy_short_term_key(tSMP_CB *p_cb, tSMP_ENC *output);
  52. static void smp_continue_private_key_creation(tSMP_CB *p_cb, tBTM_RAND_ENC *p);
  53. static void smp_process_private_key(tSMP_CB *p_cb);
  54. static void smp_finish_nonce_generation(tSMP_CB *p_cb);
  55. static void smp_process_new_nonce(tSMP_CB *p_cb);
  56. static const tSMP_ACT smp_encrypt_action[] = {
  57. smp_generate_compare, /* SMP_GEN_COMPARE */
  58. smp_generate_confirm, /* SMP_GEN_CONFIRM*/
  59. smp_generate_stk, /* SMP_GEN_STK*/
  60. smp_generate_ltk_cont, /* SMP_GEN_LTK */
  61. smp_generate_ltk, /* SMP_GEN_DIV_LTK */
  62. smp_generate_rand_vector, /* SMP_GEN_RAND_V */
  63. smp_generate_y, /* SMP_GEN_EDIV */
  64. smp_generate_passkey, /* SMP_GEN_TK */
  65. smp_generate_srand_mrand_confirm, /* SMP_GEN_SRAND_MRAND */
  66. smp_generate_rand_cont /* SMP_GEN_SRAND_MRAND_CONT */
  67. };
  68. /* If there is data saved here, then use its info instead
  69. * This needs to be cleared on a successful pairing using the oob data
  70. */
  71. static tSMP_LOC_OOB_DATA saved_local_oob_data = {};
  72. void smp_save_local_oob_data(tSMP_CB *p_cb)
  73. {
  74. memcpy(&saved_local_oob_data, &p_cb->sc_oob_data.loc_oob_data, sizeof(tSMP_LOC_OOB_DATA));
  75. }
  76. void smp_clear_local_oob_data(void)
  77. {
  78. memset(&saved_local_oob_data, 0, sizeof(tSMP_LOC_OOB_DATA));
  79. }
  80. static BOOLEAN oob_data_is_empty(tSMP_LOC_OOB_DATA *data)
  81. {
  82. tSMP_LOC_OOB_DATA empty_data = {0};
  83. return (memcmp(data, &empty_data, sizeof(tSMP_LOC_OOB_DATA)) == 0);
  84. }
  85. tSMP_LOC_OOB_DATA *smp_get_local_oob_data(void)
  86. {
  87. return &saved_local_oob_data;
  88. }
  89. void smp_debug_print_nbyte_little_endian(UINT8 *p, const UINT8 *key_name, UINT8 len)
  90. {
  91. #if SMP_DEBUG == TRUE
  92. int ind, x;
  93. int col_count = 32;
  94. int row_count;
  95. UINT8 p_buf[512];
  96. SMP_TRACE_WARNING("%s(LSB ~ MSB):\n", key_name);
  97. memset(p_buf, 0, sizeof(p_buf));
  98. row_count = len % col_count ? len / col_count + 1 : len / col_count;
  99. ind = 0;
  100. for (int row = 0; row < row_count; row++) {
  101. for (int column = 0, x = 0; (ind < len) && (column < col_count); column++, ind++) {
  102. x += sprintf((char *)&p_buf[x], "%02x ", p[ind]);
  103. }
  104. SMP_TRACE_WARNING(" [%03d]: %s", row * col_count, p_buf);
  105. }
  106. #endif
  107. }
  108. #if 0 //Unused
  109. void smp_debug_print_nbyte_big_endian (UINT8 *p, const UINT8 *key_name, UINT8 len)
  110. {
  111. #if SMP_DEBUG == TRUE
  112. UINT8 p_buf[512];
  113. SMP_TRACE_WARNING("%s(MSB ~ LSB):", key_name);
  114. memset(p_buf, 0, sizeof(p_buf));
  115. nrows = len % ncols ? len / ncols + 1 : len / ncols;
  116. int ind = 0;
  117. int ncols = 32; /* num entries in one line */
  118. int nrows; /* num lines */
  119. int x;
  120. for (int row = 0; row < nrows; row++) {
  121. for (int col = 0, x = 0; (ind < len) && (col < ncols); col++, ind++) {
  122. x += sprintf ((char *)&p_buf[len - x - 1], "%02x ", p[ind]);
  123. }
  124. SMP_TRACE_WARNING("[%03d]: %s", row * ncols, p_buf);
  125. }
  126. #endif
  127. }
  128. #endif
  129. /*******************************************************************************
  130. **
  131. ** Function smp_encrypt_data
  132. **
  133. ** Description This function is called to encrypt data.
  134. ** It uses AES-128 encryption algorithm.
  135. ** Plain_text is encrypted using key, the result is at p_out.
  136. **
  137. ** Returns void
  138. **
  139. *******************************************************************************/
  140. BOOLEAN smp_encrypt_data (UINT8 *key, UINT8 key_len,
  141. UINT8 *plain_text, UINT8 pt_len,
  142. tSMP_ENC *p_out)
  143. {
  144. aes_context ctx;
  145. UINT8 *p_start = NULL;
  146. UINT8 *p = NULL;
  147. UINT8 *p_rev_data = NULL; /* input data in big endilan format */
  148. UINT8 *p_rev_key = NULL; /* input key in big endilan format */
  149. UINT8 *p_rev_output = NULL; /* encrypted output in big endilan format */
  150. SMP_TRACE_DEBUG ("%s\n", __func__);
  151. if ( (p_out == NULL ) || (key_len != SMP_ENCRYT_KEY_SIZE) ) {
  152. SMP_TRACE_ERROR ("%s failed\n", __func__);
  153. return FALSE;
  154. }
  155. if ((p_start = (UINT8 *)osi_malloc((SMP_ENCRYT_DATA_SIZE * 4))) == NULL) {
  156. SMP_TRACE_ERROR ("%s failed unable to allocate buffer\n", __func__);
  157. return FALSE;
  158. }
  159. if (pt_len > SMP_ENCRYT_DATA_SIZE) {
  160. pt_len = SMP_ENCRYT_DATA_SIZE;
  161. }
  162. memset(p_start, 0, SMP_ENCRYT_DATA_SIZE * 4);
  163. p = p_start;
  164. ARRAY_TO_STREAM (p, plain_text, pt_len); /* byte 0 to byte 15 */
  165. p_rev_data = p = p_start + SMP_ENCRYT_DATA_SIZE; /* start at byte 16 */
  166. REVERSE_ARRAY_TO_STREAM (p, p_start, SMP_ENCRYT_DATA_SIZE); /* byte 16 to byte 31 */
  167. p_rev_key = p; /* start at byte 32 */
  168. REVERSE_ARRAY_TO_STREAM (p, key, SMP_ENCRYT_KEY_SIZE); /* byte 32 to byte 47 */
  169. #if SMP_DEBUG == TRUE && SMP_DEBUG_VERBOSE == TRUE
  170. smp_debug_print_nbyte_little_endian(key, (const UINT8 *)"Key", SMP_ENCRYT_KEY_SIZE);
  171. smp_debug_print_nbyte_little_endian(p_start, (const UINT8 *)"Plain text", SMP_ENCRYT_DATA_SIZE);
  172. #endif
  173. p_rev_output = p;
  174. aes_set_key(p_rev_key, SMP_ENCRYT_KEY_SIZE, &ctx);
  175. bluedroid_aes_encrypt(p_rev_data, p, &ctx); /* outputs in byte 48 to byte 63 */
  176. p = p_out->param_buf;
  177. REVERSE_ARRAY_TO_STREAM (p, p_rev_output, SMP_ENCRYT_DATA_SIZE);
  178. #if SMP_DEBUG == TRUE && SMP_DEBUG_VERBOSE == TRUE
  179. smp_debug_print_nbyte_little_endian(p_out->param_buf, (const UINT8 *)"Encrypted text", SMP_ENCRYT_KEY_SIZE);
  180. #endif
  181. p_out->param_len = SMP_ENCRYT_KEY_SIZE;
  182. p_out->status = HCI_SUCCESS;
  183. p_out->opcode = HCI_BLE_ENCRYPT;
  184. osi_free(p_start);
  185. return TRUE;
  186. }
  187. void smp_use_static_passkey(void)
  188. {
  189. tSMP_CB *p_cb = &smp_cb;
  190. UINT8 *tt = p_cb->tk;
  191. tSMP_KEY key;
  192. UINT32 passkey = p_cb->static_passkey;
  193. /* save the TK */
  194. memset(p_cb->tk, 0, BT_OCTET16_LEN);
  195. UINT32_TO_STREAM(tt, passkey);
  196. key.key_type = SMP_KEY_TYPE_TK;
  197. key.p_data = p_cb->tk;
  198. if (p_cb->p_callback) {
  199. (*p_cb->p_callback)(SMP_PASSKEY_NOTIF_EVT, p_cb->pairing_bda, (tSMP_EVT_DATA *)&passkey);
  200. }
  201. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_PASSKEY_DISP) {
  202. smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &passkey);
  203. } else {
  204. smp_sm_event(p_cb, SMP_KEY_READY_EVT, (tSMP_INT_DATA *)&key);
  205. }
  206. }
  207. /*******************************************************************************
  208. **
  209. ** Function smp_generate_passkey
  210. **
  211. ** Description This function is called to generate passkey.
  212. **
  213. ** Returns void
  214. **
  215. *******************************************************************************/
  216. void smp_generate_passkey(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  217. {
  218. UNUSED(p_data);
  219. if(p_cb->use_static_passkey) {
  220. SMP_TRACE_DEBUG ("%s use static passkey %6d", __func__, p_cb->static_passkey);
  221. smp_use_static_passkey();
  222. return;
  223. }
  224. SMP_TRACE_DEBUG ("%s generate rand passkey", __func__);
  225. p_cb->rand_enc_proc_state = SMP_GEN_TK;
  226. /* generate MRand or SRand */
  227. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  228. smp_rand_back(NULL);
  229. }
  230. }
  231. /*******************************************************************************
  232. **
  233. ** Function smp_proc_passkey
  234. **
  235. ** Description This function is called to process a passkey.
  236. **
  237. ** Returns void
  238. **
  239. *******************************************************************************/
  240. void smp_proc_passkey(tSMP_CB *p_cb , tBTM_RAND_ENC *p)
  241. {
  242. UINT8 *tt = p_cb->tk;
  243. tSMP_KEY key;
  244. UINT32 passkey; /* 19655 test number; */
  245. UINT8 *pp = p->param_buf;
  246. SMP_TRACE_DEBUG ("%s", __func__);
  247. STREAM_TO_UINT32(passkey, pp);
  248. passkey &= ~SMP_PASSKEY_MASK;
  249. /* truncate by maximum value */
  250. while (passkey > BTM_MAX_PASSKEY_VAL) {
  251. passkey >>= 1;
  252. }
  253. /* save the TK */
  254. memset(p_cb->tk, 0, BT_OCTET16_LEN);
  255. UINT32_TO_STREAM(tt, passkey);
  256. key.key_type = SMP_KEY_TYPE_TK;
  257. key.p_data = p_cb->tk;
  258. if (p_cb->p_callback) {
  259. (*p_cb->p_callback)(SMP_PASSKEY_NOTIF_EVT, p_cb->pairing_bda, (tSMP_EVT_DATA *)&passkey);
  260. }
  261. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_PASSKEY_DISP) {
  262. smp_sm_event(&smp_cb, SMP_KEY_READY_EVT, &passkey);
  263. } else {
  264. smp_sm_event(p_cb, SMP_KEY_READY_EVT, (tSMP_INT_DATA *)&key);
  265. }
  266. }
  267. /*******************************************************************************
  268. **
  269. ** Function smp_generate_stk
  270. **
  271. ** Description This function is called to generate STK calculated by running
  272. ** AES with the TK value as key and a concatenation of the random
  273. ** values.
  274. **
  275. ** Returns void
  276. **
  277. *******************************************************************************/
  278. void smp_generate_stk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  279. {
  280. UNUSED(p_data);
  281. tSMP_ENC output;
  282. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  283. SMP_TRACE_DEBUG ("%s\n", __func__);
  284. if (p_cb->le_secure_connections_mode_is_used) {
  285. SMP_TRACE_WARNING ("FOR LE SC LTK IS USED INSTEAD OF STK");
  286. output.param_len = SMP_ENCRYT_KEY_SIZE;
  287. output.status = HCI_SUCCESS;
  288. output.opcode = HCI_BLE_ENCRYPT;
  289. memcpy(output.param_buf, p_cb->ltk, SMP_ENCRYT_DATA_SIZE);
  290. } else if (!smp_calculate_legacy_short_term_key(p_cb, &output)) {
  291. SMP_TRACE_ERROR("%s failed", __func__);
  292. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  293. return;
  294. }
  295. smp_process_stk(p_cb, &output);
  296. }
  297. /*******************************************************************************
  298. **
  299. ** Function smp_generate_srand_mrand_confirm
  300. **
  301. ** Description This function is called to start the second pairing phase by
  302. ** start generating random number.
  303. **
  304. **
  305. ** Returns void
  306. **
  307. *******************************************************************************/
  308. void smp_generate_srand_mrand_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  309. {
  310. UNUSED(p_data);
  311. SMP_TRACE_DEBUG ("%s\n", __func__);
  312. p_cb->rand_enc_proc_state = SMP_GEN_SRAND_MRAND;
  313. /* generate MRand or SRand */
  314. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  315. smp_rand_back(NULL);
  316. }
  317. }
  318. /*******************************************************************************
  319. **
  320. ** Function smp_generate_rand_cont
  321. **
  322. ** Description This function is called to generate another 64 bits random for
  323. ** MRand or Srand.
  324. **
  325. ** Returns void
  326. **
  327. *******************************************************************************/
  328. void smp_generate_rand_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  329. {
  330. UNUSED(p_data);
  331. SMP_TRACE_DEBUG ("%s\n", __func__);
  332. p_cb->rand_enc_proc_state = SMP_GEN_SRAND_MRAND_CONT;
  333. /* generate 64 MSB of MRand or SRand */
  334. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  335. smp_rand_back(NULL);
  336. }
  337. }
  338. /*******************************************************************************
  339. **
  340. ** Function smp_generate_ltk
  341. **
  342. ** Description This function is called:
  343. ** - in legacy pairing - to calculate LTK, starting with DIV
  344. ** generation;
  345. ** - in LE Secure Connections pairing over LE transport - to process LTK
  346. ** already generated to encrypt LE link;
  347. ** - in LE Secure Connections pairing over BR/EDR transport - to start
  348. ** BR/EDR Link Key processing.
  349. **
  350. ** Returns void
  351. **
  352. *******************************************************************************/
  353. void smp_generate_ltk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  354. {
  355. UNUSED(p_data);
  356. BOOLEAN div_status;
  357. SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
  358. #if (CLASSIC_BT_INCLUDED == TRUE)
  359. if (smp_get_br_state() == SMP_BR_STATE_BOND_PENDING) {
  360. smp_br_process_link_key(p_cb, NULL);
  361. return;
  362. }
  363. #endif ///CLASSIC_BT_INCLUDED == TRUE
  364. if (p_cb->le_secure_connections_mode_is_used) {
  365. smp_process_secure_connection_long_term_key();
  366. return;
  367. }
  368. div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
  369. if (div_status) {
  370. smp_generate_ltk_cont(p_cb, NULL);
  371. } else {
  372. SMP_TRACE_DEBUG ("Generate DIV for LTK\n");
  373. p_cb->rand_enc_proc_state = SMP_GEN_DIV_LTK;
  374. /* generate MRand or SRand */
  375. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  376. smp_rand_back(NULL);
  377. }
  378. }
  379. }
  380. /*******************************************************************************
  381. **
  382. ** Function smp_compute_csrk
  383. **
  384. ** Description This function is called to calculate CSRK
  385. **
  386. **
  387. ** Returns void
  388. **
  389. *******************************************************************************/
  390. void smp_compute_csrk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  391. {
  392. UNUSED(p_data);
  393. BT_OCTET16 er;
  394. UINT8 buffer[4]; /* for (r || DIV) r=1*/
  395. UINT16 r = 1;
  396. UINT8 *p = buffer;
  397. tSMP_ENC output;
  398. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  399. SMP_TRACE_DEBUG ("smp_compute_csrk div=%x\n", p_cb->div);
  400. BTM_GetDeviceEncRoot(er);
  401. /* CSRK = d1(ER, DIV, 1) */
  402. UINT16_TO_STREAM(p, p_cb->div);
  403. UINT16_TO_STREAM(p, r);
  404. if (!SMP_Encrypt(er, BT_OCTET16_LEN, buffer, 4, &output)) {
  405. SMP_TRACE_ERROR("smp_generate_csrk failed\n");
  406. if (p_cb->smp_over_br) {
  407. #if (CLASSIC_BT_INCLUDED == TRUE)
  408. smp_br_state_machine_event(p_cb, SMP_BR_AUTH_CMPL_EVT, &status);
  409. #endif ///CLASSIC_BT_INCLUDED == TRUE
  410. } else {
  411. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  412. }
  413. } else {
  414. memcpy((void *)p_cb->csrk, output.param_buf, BT_OCTET16_LEN);
  415. smp_send_csrk_info(p_cb, NULL);
  416. }
  417. }
  418. /*******************************************************************************
  419. **
  420. ** Function smp_generate_csrk
  421. **
  422. ** Description This function is called to calculate CSRK, starting with DIV
  423. ** generation.
  424. **
  425. **
  426. ** Returns void
  427. **
  428. *******************************************************************************/
  429. void smp_generate_csrk(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  430. {
  431. UNUSED(p_data);
  432. BOOLEAN div_status;
  433. SMP_TRACE_DEBUG ("smp_generate_csrk");
  434. div_status = btm_get_local_div(p_cb->pairing_bda, &p_cb->div);
  435. if (div_status) {
  436. smp_compute_csrk(p_cb, NULL);
  437. } else {
  438. SMP_TRACE_DEBUG ("Generate DIV for CSRK");
  439. p_cb->rand_enc_proc_state = SMP_GEN_DIV_CSRK;
  440. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  441. smp_rand_back(NULL);
  442. }
  443. }
  444. }
  445. /*******************************************************************************
  446. ** Function smp_concatenate_peer
  447. ** add pairing command sent from local device into p1.
  448. *******************************************************************************/
  449. void smp_concatenate_local( tSMP_CB *p_cb, UINT8 **p_data, UINT8 op_code)
  450. {
  451. UINT8 *p = *p_data;
  452. SMP_TRACE_DEBUG ("%s\n", __func__);
  453. UINT8_TO_STREAM(p, op_code);
  454. UINT8_TO_STREAM(p, p_cb->local_io_capability);
  455. UINT8_TO_STREAM(p, p_cb->loc_oob_flag);
  456. UINT8_TO_STREAM(p, p_cb->loc_auth_req);
  457. UINT8_TO_STREAM(p, p_cb->loc_enc_size);
  458. UINT8_TO_STREAM(p, p_cb->local_i_key);
  459. UINT8_TO_STREAM(p, p_cb->local_r_key);
  460. *p_data = p;
  461. }
  462. /*******************************************************************************
  463. ** Function smp_concatenate_peer
  464. ** add pairing command received from peer device into p1.
  465. *******************************************************************************/
  466. void smp_concatenate_peer( tSMP_CB *p_cb, UINT8 **p_data, UINT8 op_code)
  467. {
  468. UINT8 *p = *p_data;
  469. SMP_TRACE_DEBUG ("smp_concatenate_peer \n");
  470. UINT8_TO_STREAM(p, op_code);
  471. UINT8_TO_STREAM(p, p_cb->peer_io_caps);
  472. UINT8_TO_STREAM(p, p_cb->peer_oob_flag);
  473. UINT8_TO_STREAM(p, p_cb->peer_auth_req);
  474. UINT8_TO_STREAM(p, p_cb->peer_enc_size);
  475. UINT8_TO_STREAM(p, p_cb->peer_i_key);
  476. UINT8_TO_STREAM(p, p_cb->peer_r_key);
  477. *p_data = p;
  478. }
  479. /*******************************************************************************
  480. **
  481. ** Function smp_gen_p1_4_confirm
  482. **
  483. ** Description Generate Confirm/Compare Step1:
  484. ** p1 = pres || preq || rat' || iat'
  485. **
  486. ** Returns void
  487. **
  488. *******************************************************************************/
  489. void smp_gen_p1_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p1)
  490. {
  491. UINT8 *p = (UINT8 *)p1;
  492. tBLE_ADDR_TYPE addr_type = 0;
  493. BD_ADDR remote_bda;
  494. SMP_TRACE_DEBUG ("smp_gen_p1_4_confirm\n");
  495. if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, remote_bda, &addr_type)) {
  496. SMP_TRACE_ERROR("can not generate confirm for unknown device\n");
  497. return;
  498. }
  499. BTM_ReadConnectionAddr( p_cb->pairing_bda, p_cb->local_bda, &p_cb->addr_type);
  500. if (p_cb->role == HCI_ROLE_MASTER) {
  501. /* LSB : rat': initiator's(local) address type */
  502. UINT8_TO_STREAM(p, p_cb->addr_type);
  503. /* LSB : iat': responder's address type */
  504. UINT8_TO_STREAM(p, addr_type);
  505. /* concatinate preq */
  506. smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
  507. /* concatinate pres */
  508. smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
  509. } else {
  510. /* LSB : iat': initiator's address type */
  511. UINT8_TO_STREAM(p, addr_type);
  512. /* LSB : rat': responder's(local) address type */
  513. UINT8_TO_STREAM(p, p_cb->addr_type);
  514. /* concatinate preq */
  515. smp_concatenate_peer(p_cb, &p, SMP_OPCODE_PAIRING_REQ);
  516. /* concatinate pres */
  517. smp_concatenate_local(p_cb, &p, SMP_OPCODE_PAIRING_RSP);
  518. }
  519. #if SMP_DEBUG == TRUE
  520. SMP_TRACE_DEBUG("p1 = pres || preq || rat' || iat'\n");
  521. smp_debug_print_nbyte_little_endian ((UINT8 *)p1, (const UINT8 *)"P1", 16);
  522. #endif
  523. }
  524. /*******************************************************************************
  525. **
  526. ** Function smp_gen_p2_4_confirm
  527. **
  528. ** Description Generate Confirm/Compare Step2:
  529. ** p2 = padding || ia || ra
  530. **
  531. ** Returns void
  532. **
  533. *******************************************************************************/
  534. void smp_gen_p2_4_confirm( tSMP_CB *p_cb, BT_OCTET16 p2)
  535. {
  536. UINT8 *p = (UINT8 *)p2;
  537. BD_ADDR remote_bda;
  538. tBLE_ADDR_TYPE addr_type = 0;
  539. SMP_TRACE_DEBUG ("smp_gen_p2_4_confirm\n");
  540. if (!BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, remote_bda, &addr_type)) {
  541. SMP_TRACE_ERROR("can not generate confirm p2 for unknown device\n");
  542. return;
  543. }
  544. SMP_TRACE_DEBUG ("smp_gen_p2_4_confirm\n");
  545. memset(p, 0, sizeof(BT_OCTET16));
  546. if (p_cb->role == HCI_ROLE_MASTER) {
  547. /* LSB ra */
  548. BDADDR_TO_STREAM(p, remote_bda);
  549. /* ia */
  550. BDADDR_TO_STREAM(p, p_cb->local_bda);
  551. } else {
  552. /* LSB ra */
  553. BDADDR_TO_STREAM(p, p_cb->local_bda);
  554. /* ia */
  555. BDADDR_TO_STREAM(p, remote_bda);
  556. }
  557. #if SMP_DEBUG == TRUE
  558. SMP_TRACE_DEBUG("p2 = padding || ia || ra");
  559. smp_debug_print_nbyte_little_endian(p2, (const UINT8 *)"p2", 16);
  560. #endif
  561. }
  562. /*******************************************************************************
  563. **
  564. ** Function smp_calculate_comfirm
  565. **
  566. ** Description This function is called to calculate Confirm value.
  567. **
  568. ** Returns void
  569. **
  570. *******************************************************************************/
  571. void smp_calculate_comfirm (tSMP_CB *p_cb, BT_OCTET16 rand, BD_ADDR bda)
  572. {
  573. UNUSED(bda);
  574. BT_OCTET16 p1;
  575. tSMP_ENC output;
  576. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  577. SMP_TRACE_DEBUG ("smp_calculate_comfirm \n");
  578. /* generate p1 = pres || preq || rat' || iat' */
  579. smp_gen_p1_4_confirm(p_cb, p1);
  580. /* p1 = rand XOR p1 */
  581. smp_xor_128(p1, rand);
  582. smp_debug_print_nbyte_little_endian ((UINT8 *)p1, (const UINT8 *)"P1' = r XOR p1", 16);
  583. /* calculate e(k, r XOR p1), where k = TK */
  584. if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p1, BT_OCTET16_LEN, &output)) {
  585. SMP_TRACE_ERROR("smp_generate_csrk failed");
  586. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  587. } else {
  588. smp_calculate_comfirm_cont(p_cb, &output);
  589. }
  590. }
  591. /*******************************************************************************
  592. **
  593. ** Function smp_calculate_comfirm_cont
  594. **
  595. ** Description This function is called when SConfirm/MConfirm is generated
  596. ** proceed to send the Confirm request/response to peer device.
  597. **
  598. ** Returns void
  599. **
  600. *******************************************************************************/
  601. static void smp_calculate_comfirm_cont(tSMP_CB *p_cb, tSMP_ENC *p)
  602. {
  603. BT_OCTET16 p2;
  604. tSMP_ENC output;
  605. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  606. SMP_TRACE_DEBUG ("smp_calculate_comfirm_cont \n");
  607. #if SMP_DEBUG == TRUE
  608. SMP_TRACE_DEBUG("Confirm step 1 p1' = e(k, r XOR p1) Generated\n");
  609. smp_debug_print_nbyte_little_endian (p->param_buf, (const UINT8 *)"C1", 16);
  610. #endif
  611. smp_gen_p2_4_confirm(p_cb, p2);
  612. /* calculate p2 = (p1' XOR p2) */
  613. smp_xor_128(p2, p->param_buf);
  614. smp_debug_print_nbyte_little_endian ((UINT8 *)p2, (const UINT8 *)"p2' = C1 xor p2", 16);
  615. /* calculate: Confirm = E(k, p1' XOR p2) */
  616. if (!SMP_Encrypt(p_cb->tk, BT_OCTET16_LEN, p2, BT_OCTET16_LEN, &output)) {
  617. SMP_TRACE_ERROR("smp_calculate_comfirm_cont failed\n");
  618. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  619. } else {
  620. SMP_TRACE_DEBUG("p_cb->rand_enc_proc_state=%d\n", p_cb->rand_enc_proc_state);
  621. switch (p_cb->rand_enc_proc_state) {
  622. case SMP_GEN_CONFIRM:
  623. smp_process_confirm(p_cb, &output);
  624. break;
  625. case SMP_GEN_COMPARE:
  626. smp_process_compare(p_cb, &output);
  627. break;
  628. }
  629. }
  630. }
  631. /*******************************************************************************
  632. **
  633. ** Function smp_generate_confirm
  634. **
  635. ** Description This function is called when a 48 bits random number is generated
  636. ** as SRand or MRand, continue to calculate Sconfirm or MConfirm.
  637. **
  638. ** Returns void
  639. **
  640. *******************************************************************************/
  641. static void smp_generate_confirm(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  642. {
  643. UNUSED(p_data);
  644. SMP_TRACE_DEBUG ("%s\n", __func__);
  645. p_cb->rand_enc_proc_state = SMP_GEN_CONFIRM;
  646. smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->rand, (const UINT8 *)"local rand", 16);
  647. smp_calculate_comfirm(p_cb, p_cb->rand, p_cb->pairing_bda);
  648. }
  649. /*******************************************************************************
  650. **
  651. ** Function smp_generate_compare
  652. **
  653. ** Description This function is called to generate SConfirm for Slave device,
  654. ** or MSlave for Master device. This function can be also used for
  655. ** generating Compare number for confirm value check.
  656. **
  657. ** Returns void
  658. **
  659. *******************************************************************************/
  660. void smp_generate_compare (tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  661. {
  662. UNUSED(p_data);
  663. SMP_TRACE_DEBUG ("smp_generate_compare \n");
  664. p_cb->rand_enc_proc_state = SMP_GEN_COMPARE;
  665. smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->rrand, (const UINT8 *)"peer rand", 16);
  666. smp_calculate_comfirm(p_cb, p_cb->rrand, p_cb->local_bda);
  667. }
  668. /*******************************************************************************
  669. **
  670. ** Function smp_process_confirm
  671. **
  672. ** Description This function is called when SConfirm/MConfirm is generated
  673. ** proceed to send the Confirm request/response to peer device.
  674. **
  675. ** Returns void
  676. **
  677. *******************************************************************************/
  678. static void smp_process_confirm(tSMP_CB *p_cb, tSMP_ENC *p)
  679. {
  680. tSMP_KEY key;
  681. SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
  682. memcpy(p_cb->confirm, p->param_buf, BT_OCTET16_LEN);
  683. #if (SMP_DEBUG == TRUE)
  684. SMP_TRACE_DEBUG("Confirm Generated");
  685. smp_debug_print_nbyte_little_endian ((UINT8 *)p_cb->confirm, (const UINT8 *)"Confirm", 16);
  686. #endif
  687. key.key_type = SMP_KEY_TYPE_CFM;
  688. key.p_data = p->param_buf;
  689. smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
  690. }
  691. /*******************************************************************************
  692. **
  693. ** Function smp_process_compare
  694. **
  695. ** Description This function is called when Compare is generated using the
  696. ** RRand and local BDA, TK information.
  697. **
  698. ** Returns void
  699. **
  700. *******************************************************************************/
  701. static void smp_process_compare(tSMP_CB *p_cb, tSMP_ENC *p)
  702. {
  703. tSMP_KEY key;
  704. SMP_TRACE_DEBUG ("smp_process_compare \n");
  705. #if (SMP_DEBUG == TRUE)
  706. SMP_TRACE_DEBUG("Compare Generated\n");
  707. smp_debug_print_nbyte_little_endian (p->param_buf, (const UINT8 *)"Compare", 16);
  708. #endif
  709. key.key_type = SMP_KEY_TYPE_CMP;
  710. key.p_data = p->param_buf;
  711. //smp_set_state(SMP_STATE_CONFIRM);
  712. smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
  713. }
  714. /*******************************************************************************
  715. **
  716. ** Function smp_process_stk
  717. **
  718. ** Description This function is called when STK is generated
  719. ** proceed to send the encrypt the link using STK.
  720. **
  721. ** Returns void
  722. **
  723. *******************************************************************************/
  724. static void smp_process_stk(tSMP_CB *p_cb, tSMP_ENC *p)
  725. {
  726. tSMP_KEY key;
  727. SMP_TRACE_DEBUG ("smp_process_stk ");
  728. #if (SMP_DEBUG == TRUE)
  729. SMP_TRACE_ERROR("STK Generated");
  730. #endif
  731. smp_mask_enc_key(p_cb->loc_enc_size, p->param_buf);
  732. key.key_type = SMP_KEY_TYPE_STK;
  733. key.p_data = p->param_buf;
  734. smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
  735. }
  736. /*******************************************************************************
  737. **
  738. ** Function smp_generate_ltk_cont
  739. **
  740. ** Description This function is to calculate LTK = d1(ER, DIV, 0)= e(ER, DIV)
  741. **
  742. ** Returns void
  743. **
  744. *******************************************************************************/
  745. static void smp_generate_ltk_cont(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  746. {
  747. UNUSED(p_data);
  748. BT_OCTET16 er;
  749. tSMP_ENC output;
  750. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  751. SMP_TRACE_DEBUG ("%s\n", __func__);
  752. BTM_GetDeviceEncRoot(er);
  753. /* LTK = d1(ER, DIV, 0)= e(ER, DIV)*/
  754. if (!SMP_Encrypt(er, BT_OCTET16_LEN, (UINT8 *)&p_cb->div,
  755. sizeof(UINT16), &output)) {
  756. SMP_TRACE_ERROR("%s failed\n", __func__);
  757. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  758. } else {
  759. /* mask the LTK */
  760. smp_mask_enc_key(p_cb->loc_enc_size, output.param_buf);
  761. memcpy((void *)p_cb->ltk, output.param_buf, BT_OCTET16_LEN);
  762. smp_generate_rand_vector(p_cb, NULL);
  763. }
  764. }
  765. /*******************************************************************************
  766. **
  767. ** Function smp_generate_y
  768. **
  769. ** Description This function is to proceed generate Y = E(DHK, Rand)
  770. **
  771. ** Returns void
  772. **
  773. *******************************************************************************/
  774. static void smp_generate_y(tSMP_CB *p_cb, tSMP_INT_DATA *p)
  775. {
  776. UNUSED(p);
  777. BT_OCTET16 dhk;
  778. tSMP_ENC output;
  779. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  780. SMP_TRACE_DEBUG ("smp_generate_y \n");
  781. BTM_GetDeviceDHK(dhk);
  782. if (!SMP_Encrypt(dhk, BT_OCTET16_LEN, p_cb->enc_rand,
  783. BT_OCTET8_LEN, &output)) {
  784. SMP_TRACE_ERROR("smp_generate_y failed");
  785. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  786. } else {
  787. smp_process_ediv(p_cb, &output);
  788. }
  789. }
  790. /*******************************************************************************
  791. **
  792. ** Function smp_generate_rand_vector
  793. **
  794. ** Description This function is called when LTK is generated, send state machine
  795. ** event to SMP.
  796. **
  797. ** Returns void
  798. **
  799. *******************************************************************************/
  800. static void smp_generate_rand_vector (tSMP_CB *p_cb, tSMP_INT_DATA *p)
  801. {
  802. UNUSED(p);
  803. /* generate EDIV and rand now */
  804. /* generate random vector */
  805. SMP_TRACE_DEBUG ("smp_generate_rand_vector\n");
  806. p_cb->rand_enc_proc_state = SMP_GEN_RAND_V;
  807. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  808. smp_rand_back(NULL);
  809. }
  810. }
  811. /*******************************************************************************
  812. **
  813. ** Function smp_process_ediv
  814. **
  815. ** Description This function is to calculate EDIV = Y xor DIV
  816. **
  817. ** Returns void
  818. **
  819. *******************************************************************************/
  820. static void smp_process_ediv(tSMP_CB *p_cb, tSMP_ENC *p)
  821. {
  822. tSMP_KEY key;
  823. UINT8 *pp = p->param_buf;
  824. UINT16 y;
  825. SMP_TRACE_DEBUG ("smp_process_ediv ");
  826. STREAM_TO_UINT16(y, pp);
  827. /* EDIV = Y xor DIV */
  828. p_cb->ediv = p_cb->div ^ y;
  829. /* send LTK ready */
  830. SMP_TRACE_DEBUG("LTK ready");
  831. key.key_type = SMP_KEY_TYPE_LTK;
  832. key.p_data = p->param_buf;
  833. smp_sm_event(p_cb, SMP_KEY_READY_EVT, &key);
  834. }
  835. /*******************************************************************************
  836. **
  837. ** Function smp_calculate_legacy_short_term_key
  838. **
  839. ** Description The function calculates legacy STK.
  840. **
  841. ** Returns FALSE if out of resources, TRUE in other cases.
  842. **
  843. *******************************************************************************/
  844. BOOLEAN smp_calculate_legacy_short_term_key(tSMP_CB *p_cb, tSMP_ENC *output)
  845. {
  846. BT_OCTET16 ptext;
  847. UINT8 *p = ptext;
  848. SMP_TRACE_DEBUG ("%s\n", __func__);
  849. memset(p, 0, BT_OCTET16_LEN);
  850. if (p_cb->role == HCI_ROLE_MASTER) {
  851. memcpy(p, p_cb->rand, BT_OCTET8_LEN);
  852. memcpy(&p[BT_OCTET8_LEN], p_cb->rrand, BT_OCTET8_LEN);
  853. } else {
  854. memcpy(p, p_cb->rrand, BT_OCTET8_LEN);
  855. memcpy(&p[BT_OCTET8_LEN], p_cb->rand, BT_OCTET8_LEN);
  856. }
  857. BOOLEAN encrypted;
  858. /* generate STK = Etk(rand|rrand)*/
  859. encrypted = SMP_Encrypt( p_cb->tk, BT_OCTET16_LEN, ptext, BT_OCTET16_LEN, output);
  860. if (!encrypted) {
  861. SMP_TRACE_ERROR("%s failed\n", __func__);
  862. }
  863. return encrypted;
  864. }
  865. /*******************************************************************************
  866. **
  867. ** Function smp_create_private_key
  868. **
  869. ** Description This function is called to create private key used to
  870. ** calculate public key and DHKey.
  871. ** The function starts private key creation requesting controller
  872. ** to generate [0-7] octets of private key.
  873. **
  874. ** Returns void
  875. **
  876. *******************************************************************************/
  877. void smp_create_private_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  878. {
  879. SMP_TRACE_DEBUG("%s", __func__);
  880. if (p_cb->selected_association_model == SMP_MODEL_SEC_CONN_OOB) {
  881. SMP_TRACE_EVENT("OOB Association Model");
  882. if (!oob_data_is_empty(&saved_local_oob_data)) {
  883. SMP_TRACE_EVENT("Found OOB data, loading keys");
  884. memcpy(&p_cb->sc_oob_data.loc_oob_data, &saved_local_oob_data, sizeof(tSMP_LOC_OOB_DATA));
  885. smp_process_private_key(p_cb);
  886. return;
  887. }
  888. SMP_TRACE_EVENT("OOB Association Model with no saved data");
  889. }
  890. p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_0_7;
  891. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  892. smp_rand_back(NULL);
  893. }
  894. }
  895. /*******************************************************************************
  896. **
  897. ** Function smp_use_oob_private_key
  898. **
  899. ** Description This function is called
  900. ** - to save the secret key used to calculate the public key used
  901. ** in calculations of commitment sent OOB to a peer
  902. ** - to use this secret key to recalculate the public key and
  903. ** start the process of sending this public key to the peer
  904. ** if secret/public keys have to be reused.
  905. ** If the keys aren't supposed to be reused, continue from the
  906. ** point from which request for OOB data was issued.
  907. **
  908. ** Returns void
  909. **
  910. *******************************************************************************/
  911. void smp_use_oob_private_key(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  912. {
  913. SMP_TRACE_DEBUG ("%s req_oob_type: %d, role: %d\n",
  914. __func__, p_cb->req_oob_type, p_cb->role);
  915. switch (p_cb->req_oob_type) {
  916. case SMP_OOB_BOTH:
  917. case SMP_OOB_LOCAL:
  918. SMP_TRACE_DEBUG("%s restore secret key\n", __func__);
  919. // copy private key in smp_process_private_key
  920. smp_process_private_key(p_cb);
  921. break;
  922. default:
  923. SMP_TRACE_DEBUG("%s create secret key anew\n", __func__);
  924. smp_set_state(SMP_STATE_PAIR_REQ_RSP);
  925. smp_decide_association_model(p_cb, NULL);
  926. break;
  927. }
  928. }
  929. /*******************************************************************************
  930. **
  931. ** Function smp_continue_private_key_creation
  932. **
  933. ** Description This function is used to continue private key creation.
  934. **
  935. ** Returns void
  936. **
  937. *******************************************************************************/
  938. void smp_continue_private_key_creation (tSMP_CB *p_cb, tBTM_RAND_ENC *p)
  939. {
  940. UINT8 state = p_cb->rand_enc_proc_state & ~0x80;
  941. SMP_TRACE_DEBUG ("%s state=0x%x\n", __func__, state);
  942. switch (state) {
  943. case SMP_GENERATE_PRIVATE_KEY_0_7:
  944. memcpy((void *)p_cb->private_key, p->param_buf, p->param_len);
  945. p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_8_15;
  946. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  947. smp_rand_back(NULL);
  948. }
  949. break;
  950. case SMP_GENERATE_PRIVATE_KEY_8_15:
  951. memcpy((void *)&p_cb->private_key[8], p->param_buf, p->param_len);
  952. p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_16_23;
  953. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  954. smp_rand_back(NULL);
  955. }
  956. break;
  957. case SMP_GENERATE_PRIVATE_KEY_16_23:
  958. memcpy((void *)&p_cb->private_key[16], p->param_buf, p->param_len);
  959. p_cb->rand_enc_proc_state = SMP_GENERATE_PRIVATE_KEY_24_31;
  960. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  961. smp_rand_back(NULL);
  962. }
  963. break;
  964. case SMP_GENERATE_PRIVATE_KEY_24_31:
  965. memcpy((void *)&p_cb->private_key[24], p->param_buf, p->param_len);
  966. smp_process_private_key (p_cb);
  967. break;
  968. default:
  969. break;
  970. }
  971. return;
  972. }
  973. /*******************************************************************************
  974. **
  975. ** Function smp_process_private_key
  976. **
  977. ** Description This function processes private key.
  978. ** It calculates public key and notifies SM that private key /
  979. ** public key pair is created.
  980. **
  981. ** Returns void
  982. **
  983. *******************************************************************************/
  984. void smp_process_private_key(tSMP_CB *p_cb)
  985. {
  986. Point public_key;
  987. BT_OCTET32 private_key;
  988. tSMP_LOC_OOB_DATA *p_loc_oob = &p_cb->sc_oob_data.loc_oob_data;
  989. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  990. /* if local oob data present, then restore oob private and public key */
  991. if (p_loc_oob->present) {
  992. memcpy(p_cb->private_key, p_loc_oob->private_key_used, BT_OCTET32_LEN);
  993. memcpy(p_cb->loc_publ_key.x, p_loc_oob->publ_key_used.x, BT_OCTET32_LEN);
  994. memcpy(p_cb->loc_publ_key.y, p_loc_oob->publ_key_used.y, BT_OCTET32_LEN);
  995. memcpy(p_cb->local_random, p_loc_oob->randomizer, BT_OCTET16_LEN);
  996. } else {
  997. memcpy(private_key, p_cb->private_key, BT_OCTET32_LEN);
  998. ECC_PointMult(&public_key, &(curve_p256.G), (DWORD *) private_key, KEY_LENGTH_DWORDS_P256);
  999. memcpy(p_cb->loc_publ_key.x, public_key.x, BT_OCTET32_LEN);
  1000. memcpy(p_cb->loc_publ_key.y, public_key.y, BT_OCTET32_LEN);
  1001. }
  1002. smp_debug_print_nbyte_little_endian (p_cb->private_key, (const UINT8 *)"private",
  1003. BT_OCTET32_LEN);
  1004. smp_debug_print_nbyte_little_endian (p_cb->loc_publ_key.x, (const UINT8 *)"local public(x)",
  1005. BT_OCTET32_LEN);
  1006. smp_debug_print_nbyte_little_endian (p_cb->loc_publ_key.y, (const UINT8 *)"local public(y)",
  1007. BT_OCTET32_LEN);
  1008. p_cb->flags |= SMP_PAIR_FLAG_HAVE_LOCAL_PUBL_KEY;
  1009. smp_sm_event(p_cb, SMP_LOC_PUBL_KEY_CRTD_EVT, NULL);
  1010. }
  1011. /*******************************************************************************
  1012. **
  1013. ** Function smp_compute_dhkey
  1014. **
  1015. ** Description The function:
  1016. ** - calculates a new public key using as input local private
  1017. ** key and peer public key;
  1018. ** - saves the new public key x-coordinate as DHKey.
  1019. **
  1020. ** Returns void
  1021. **
  1022. *******************************************************************************/
  1023. void smp_compute_dhkey (tSMP_CB *p_cb)
  1024. {
  1025. Point peer_publ_key, new_publ_key;
  1026. BT_OCTET32 private_key;
  1027. SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
  1028. memcpy(private_key, p_cb->private_key, BT_OCTET32_LEN);
  1029. memcpy(peer_publ_key.x, p_cb->peer_publ_key.x, BT_OCTET32_LEN);
  1030. memcpy(peer_publ_key.y, p_cb->peer_publ_key.y, BT_OCTET32_LEN);
  1031. ECC_PointMult(&new_publ_key, &peer_publ_key, (DWORD *) private_key, KEY_LENGTH_DWORDS_P256);
  1032. memcpy(p_cb->dhkey, new_publ_key.x, BT_OCTET32_LEN);
  1033. smp_debug_print_nbyte_little_endian (p_cb->dhkey, (const UINT8 *)"Old DHKey",
  1034. BT_OCTET32_LEN);
  1035. smp_debug_print_nbyte_little_endian (p_cb->private_key, (const UINT8 *)"private",
  1036. BT_OCTET32_LEN);
  1037. smp_debug_print_nbyte_little_endian (p_cb->peer_publ_key.x, (const UINT8 *)"rem public(x)",
  1038. BT_OCTET32_LEN);
  1039. smp_debug_print_nbyte_little_endian (p_cb->peer_publ_key.y, (const UINT8 *)"rem public(y)",
  1040. BT_OCTET32_LEN);
  1041. smp_debug_print_nbyte_little_endian (p_cb->dhkey, (const UINT8 *)"Reverted DHKey",
  1042. BT_OCTET32_LEN);
  1043. }
  1044. /*******************************************************************************
  1045. **
  1046. ** Function smp_calculate_local_commitment
  1047. **
  1048. ** Description The function calculates and saves local commmitment in CB.
  1049. **
  1050. ** Returns void
  1051. **
  1052. *******************************************************************************/
  1053. void smp_calculate_local_commitment(tSMP_CB *p_cb)
  1054. {
  1055. UINT8 random_input;
  1056. SMP_TRACE_DEBUG("%s\n", __FUNCTION__);
  1057. switch (p_cb->selected_association_model) {
  1058. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1059. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1060. if (p_cb->role == HCI_ROLE_MASTER) {
  1061. SMP_TRACE_WARNING ("local commitment calc on master is not expected \
  1062. for Just Works/Numeric Comparison models\n");
  1063. }
  1064. smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand, 0,
  1065. p_cb->commitment);
  1066. break;
  1067. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1068. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1069. random_input = smp_calculate_random_input(p_cb->local_random, p_cb->round);
  1070. smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand,
  1071. random_input, p_cb->commitment);
  1072. break;
  1073. case SMP_MODEL_SEC_CONN_OOB:
  1074. SMP_TRACE_WARNING ("local commitment calc is expected for OOB model BEFORE pairing\n");
  1075. smp_calculate_f4(p_cb->loc_publ_key.x, p_cb->loc_publ_key.x, p_cb->local_random, 0,
  1076. p_cb->commitment);
  1077. break;
  1078. default:
  1079. SMP_TRACE_ERROR("Association Model = %d is not used in LE SC\n",
  1080. p_cb->selected_association_model);
  1081. return;
  1082. }
  1083. SMP_TRACE_EVENT ("local commitment calculation is completed");
  1084. }
  1085. /*******************************************************************************
  1086. **
  1087. ** Function smp_calculate_peer_commitment
  1088. **
  1089. ** Description The function calculates and saves peer commmitment at the
  1090. ** provided output buffer.
  1091. **
  1092. ** Returns void
  1093. **
  1094. *******************************************************************************/
  1095. void smp_calculate_peer_commitment(tSMP_CB *p_cb, BT_OCTET16 output_buf)
  1096. {
  1097. UINT8 ri;
  1098. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1099. switch (p_cb->selected_association_model) {
  1100. case SMP_MODEL_SEC_CONN_JUSTWORKS:
  1101. case SMP_MODEL_SEC_CONN_NUM_COMP:
  1102. if (p_cb->role == HCI_ROLE_SLAVE) {
  1103. SMP_TRACE_WARNING ("peer commitment calc on slave is not expected \
  1104. for Just Works/Numeric Comparison models\n");
  1105. }
  1106. smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand, 0,
  1107. output_buf);
  1108. break;
  1109. case SMP_MODEL_SEC_CONN_PASSKEY_ENT:
  1110. case SMP_MODEL_SEC_CONN_PASSKEY_DISP:
  1111. ri = smp_calculate_random_input(p_cb->peer_random, p_cb->round);
  1112. smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand, ri,
  1113. output_buf);
  1114. break;
  1115. case SMP_MODEL_SEC_CONN_OOB:
  1116. smp_calculate_f4(p_cb->peer_publ_key.x, p_cb->peer_publ_key.x, p_cb->peer_random, 0,
  1117. output_buf);
  1118. break;
  1119. default:
  1120. SMP_TRACE_ERROR("Association Model = %d is not used in LE SC\n",
  1121. p_cb->selected_association_model);
  1122. return;
  1123. }
  1124. SMP_TRACE_EVENT ("peer commitment calculation is completed\n");
  1125. }
  1126. /*******************************************************************************
  1127. **
  1128. ** Function smp_calculate_f4
  1129. **
  1130. ** Description The function calculates
  1131. ** C = f4(U, V, X, Z) = AES-CMAC (U||V||Z)
  1132. ** X
  1133. ** where
  1134. ** input: U is 256 bit,
  1135. ** V is 256 bit,
  1136. ** X is 128 bit,
  1137. ** Z is 8 bit,
  1138. ** output: C is 128 bit.
  1139. **
  1140. ** Returns void
  1141. **
  1142. ** Note The LSB is the first octet, the MSB is the last octet of
  1143. ** the AES-CMAC input/output stream.
  1144. **
  1145. *******************************************************************************/
  1146. void smp_calculate_f4(UINT8 *u, UINT8 *v, UINT8 *x, UINT8 z, UINT8 *c)
  1147. {
  1148. UINT8 msg_len = BT_OCTET32_LEN /* U size */ + BT_OCTET32_LEN /* V size */ + 1 /* Z size */;
  1149. UINT8 msg[BT_OCTET32_LEN + BT_OCTET32_LEN + 1];
  1150. UINT8 key[BT_OCTET16_LEN];
  1151. UINT8 cmac[BT_OCTET16_LEN];
  1152. UINT8 *p = NULL;
  1153. #if SMP_DEBUG == TRUE
  1154. UINT8 *p_prnt = NULL;
  1155. #endif
  1156. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1157. #if SMP_DEBUG == TRUE
  1158. p_prnt = u;
  1159. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"U", BT_OCTET32_LEN);
  1160. p_prnt = v;
  1161. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"V", BT_OCTET32_LEN);
  1162. p_prnt = x;
  1163. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"X", BT_OCTET16_LEN);
  1164. p_prnt = &z;
  1165. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Z", 1);
  1166. #endif
  1167. p = msg;
  1168. UINT8_TO_STREAM(p, z);
  1169. ARRAY_TO_STREAM(p, v, BT_OCTET32_LEN);
  1170. ARRAY_TO_STREAM(p, u, BT_OCTET32_LEN);
  1171. #if SMP_DEBUG == TRUE
  1172. p_prnt = msg;
  1173. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"M", msg_len);
  1174. #endif
  1175. p = key;
  1176. ARRAY_TO_STREAM(p, x, BT_OCTET16_LEN);
  1177. #if SMP_DEBUG == TRUE
  1178. p_prnt = key;
  1179. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);
  1180. #endif
  1181. aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac);
  1182. #if SMP_DEBUG == TRUE
  1183. p_prnt = cmac;
  1184. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES_CMAC", BT_OCTET16_LEN);
  1185. #endif
  1186. p = c;
  1187. ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
  1188. }
  1189. /*******************************************************************************
  1190. **
  1191. ** Function smp_calculate_numeric_comparison_display_number
  1192. **
  1193. ** Description The function calculates and saves number to display in numeric
  1194. ** comparison association mode.
  1195. **
  1196. ** Returns void
  1197. **
  1198. *******************************************************************************/
  1199. void smp_calculate_numeric_comparison_display_number(tSMP_CB *p_cb,
  1200. tSMP_INT_DATA *p_data)
  1201. {
  1202. SMP_TRACE_DEBUG ("%s", __func__);
  1203. if (p_cb->role == HCI_ROLE_MASTER) {
  1204. p_cb->number_to_display =
  1205. smp_calculate_g2(p_cb->loc_publ_key.x, p_cb->peer_publ_key.x, p_cb->rand,
  1206. p_cb->rrand);
  1207. } else {
  1208. p_cb->number_to_display =
  1209. smp_calculate_g2(p_cb->peer_publ_key.x, p_cb->loc_publ_key.x, p_cb->rrand,
  1210. p_cb->rand);
  1211. }
  1212. if (p_cb->number_to_display >= (BTM_MAX_PASSKEY_VAL + 1)) {
  1213. UINT8 reason;
  1214. reason = p_cb->failure = SMP_PAIR_FAIL_UNKNOWN;
  1215. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &reason);
  1216. return;
  1217. }
  1218. SMP_TRACE_EVENT("Number to display in numeric comparison = %d", p_cb->number_to_display);
  1219. p_cb->cb_evt = SMP_NC_REQ_EVT;
  1220. smp_sm_event(p_cb, SMP_SC_DSPL_NC_EVT, &p_cb->number_to_display);
  1221. return;
  1222. }
  1223. /*******************************************************************************
  1224. **
  1225. ** Function smp_calculate_g2
  1226. **
  1227. ** Description The function calculates
  1228. ** g2(U, V, X, Y) = AES-CMAC (U||V||Y) mod 2**32 mod 10**6
  1229. ** X
  1230. ** and
  1231. ** Vres = g2(U, V, X, Y) mod 10**6
  1232. ** where
  1233. ** input: U is 256 bit,
  1234. ** V is 256 bit,
  1235. ** X is 128 bit,
  1236. ** Y is 128 bit,
  1237. **
  1238. ** Returns Vres.
  1239. ** Expected value has to be in the range [0 - 999999] i.e. [0 - 0xF423F].
  1240. ** Vres = 1000000 means that the calculation fails.
  1241. **
  1242. ** Note The LSB is the first octet, the MSB is the last octet of
  1243. ** the AES-CMAC input/output stream.
  1244. **
  1245. *******************************************************************************/
  1246. UINT32 smp_calculate_g2(UINT8 *u, UINT8 *v, UINT8 *x, UINT8 *y)
  1247. {
  1248. UINT8 msg_len = BT_OCTET32_LEN /* U size */ + BT_OCTET32_LEN /* V size */
  1249. + BT_OCTET16_LEN /* Y size */;
  1250. UINT8 msg[BT_OCTET32_LEN + BT_OCTET32_LEN + BT_OCTET16_LEN];
  1251. UINT8 key[BT_OCTET16_LEN];
  1252. UINT8 cmac[BT_OCTET16_LEN];
  1253. UINT8 *p = NULL;
  1254. UINT32 vres;
  1255. #if SMP_DEBUG == TRUE
  1256. UINT8 *p_prnt = NULL;
  1257. #endif
  1258. SMP_TRACE_DEBUG ("%s\n", __FUNCTION__);
  1259. p = msg;
  1260. ARRAY_TO_STREAM(p, y, BT_OCTET16_LEN);
  1261. ARRAY_TO_STREAM(p, v, BT_OCTET32_LEN);
  1262. ARRAY_TO_STREAM(p, u, BT_OCTET32_LEN);
  1263. #if SMP_DEBUG == TRUE
  1264. p_prnt = u;
  1265. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"U", BT_OCTET32_LEN);
  1266. p_prnt = v;
  1267. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"V", BT_OCTET32_LEN);
  1268. p_prnt = x;
  1269. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"X", BT_OCTET16_LEN);
  1270. p_prnt = y;
  1271. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Y", BT_OCTET16_LEN);
  1272. #endif
  1273. p = key;
  1274. ARRAY_TO_STREAM(p, x, BT_OCTET16_LEN);
  1275. #if SMP_DEBUG == TRUE
  1276. p_prnt = key;
  1277. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);
  1278. #endif
  1279. if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
  1280. SMP_TRACE_ERROR("%s failed", __FUNCTION__);
  1281. return (BTM_MAX_PASSKEY_VAL + 1);
  1282. }
  1283. #if SMP_DEBUG == TRUE
  1284. p_prnt = cmac;
  1285. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);
  1286. #endif
  1287. /* vres = cmac mod 2**32 mod 10**6 */
  1288. p = &cmac[0];
  1289. STREAM_TO_UINT32(vres, p);
  1290. #if SMP_DEBUG == TRUE
  1291. p_prnt = (UINT8 *) &vres;
  1292. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"cmac mod 2**32", 4);
  1293. #endif
  1294. while (vres > BTM_MAX_PASSKEY_VAL) {
  1295. vres -= (BTM_MAX_PASSKEY_VAL + 1);
  1296. }
  1297. #if SMP_DEBUG == TRUE
  1298. p_prnt = (UINT8 *) &vres;
  1299. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"cmac mod 2**32 mod 10**6", 4);
  1300. #endif
  1301. SMP_TRACE_ERROR("Value for numeric comparison = %d", vres);
  1302. return vres;
  1303. }
  1304. /*******************************************************************************
  1305. **
  1306. ** Function smp_calculate_f5
  1307. **
  1308. ** Description The function provides two AES-CMAC that are supposed to be used as
  1309. ** - MacKey (MacKey is used in pairing DHKey check calculation);
  1310. ** - LTK (LTK is used to ecrypt the link after completion of Phase 2
  1311. ** and on reconnection, to derive BR/EDR LK).
  1312. ** The function inputs are W, N1, N2, A1, A2.
  1313. ** F5 rules:
  1314. ** - the value used as key in MacKey/LTK (T) is calculated
  1315. ** (function smp_calculate_f5_key(...));
  1316. ** The formula is:
  1317. ** T = AES-CMAC (W)
  1318. ** salt
  1319. ** where salt is internal parameter of smp_calculate_f5_key(...).
  1320. ** - MacKey and LTK are calculated as AES-MAC values received with the
  1321. ** key T calculated in the previous step and the plaintext message
  1322. ** built from the external parameters N1, N2, A1, A2 and the internal
  1323. ** parameters counter, keyID, length.
  1324. ** The function smp_calculate_f5_mackey_or_long_term_key(...) is used in the
  1325. ** calculations.
  1326. ** The same formula is used in calculation of MacKey and LTK and the
  1327. ** same parameter values except the value of the internal parameter
  1328. ** counter:
  1329. ** - in MacKey calculations the value is 0;
  1330. ** - in LTK calculations the value is 1.
  1331. ** MacKey = AES-CMAC (Counter=0||keyID||N1||N2||A1||A2||Length=256)
  1332. ** T
  1333. ** LTK = AES-CMAC (Counter=1||keyID||N1||N2||A1||A2||Length=256)
  1334. ** T
  1335. ** The parameters are
  1336. ** input:
  1337. ** W is 256 bits,
  1338. ** N1 is 128 bits,
  1339. ** N2 is 128 bits,
  1340. ** A1 is 56 bit,
  1341. ** A2 is 56 bit.
  1342. ** internal:
  1343. ** Counter is 8 bits, its value is 0 for MacKey,
  1344. ** 1 for LTK;
  1345. ** KeyId is 32 bits, its value is
  1346. ** 0x62746c65 (MSB~LSB);
  1347. ** Length is 16 bits, its value is 0x0100
  1348. ** (MSB~LSB).
  1349. ** output:
  1350. ** MacKey is 128 bits;
  1351. ** LTK is 128 bits
  1352. **
  1353. ** Returns FALSE if out of resources, TRUE in other cases.
  1354. **
  1355. ** Note The LSB is the first octet, the MSB is the last octet of
  1356. ** the AES-CMAC input/output stream.
  1357. **
  1358. *******************************************************************************/
  1359. BOOLEAN smp_calculate_f5(UINT8 *w, UINT8 *n1, UINT8 *n2, UINT8 *a1, UINT8 *a2,
  1360. UINT8 *mac_key, UINT8 *ltk)
  1361. {
  1362. BT_OCTET16 t; /* AES-CMAC output in smp_calculate_f5_key(...), key in */
  1363. /* smp_calculate_f5_mackey_or_long_term_key(...) */
  1364. #if SMP_DEBUG == TRUE
  1365. UINT8 *p_prnt = NULL;
  1366. #endif
  1367. /* internal parameters: */
  1368. /*
  1369. counter is 0 for MacKey,
  1370. is 1 for LTK
  1371. */
  1372. UINT8 counter_mac_key[1] = {0};
  1373. UINT8 counter_ltk[1] = {1};
  1374. /*
  1375. keyID 62746c65
  1376. */
  1377. UINT8 key_id[4] = {0x65, 0x6c, 0x74, 0x62};
  1378. /*
  1379. length 0100
  1380. */
  1381. UINT8 length[2] = {0x00, 0x01};
  1382. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1383. #if SMP_DEBUG == TRUE
  1384. p_prnt = w;
  1385. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"W", BT_OCTET32_LEN);
  1386. p_prnt = n1;
  1387. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N1", BT_OCTET16_LEN);
  1388. p_prnt = n2;
  1389. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N2", BT_OCTET16_LEN);
  1390. p_prnt = a1;
  1391. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"A1", 7);
  1392. p_prnt = a2;
  1393. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *) "A2", 7);
  1394. #endif
  1395. if (!smp_calculate_f5_key(w, t)) {
  1396. SMP_TRACE_ERROR("%s failed to calc T", __FUNCTION__);
  1397. return FALSE;
  1398. }
  1399. #if SMP_DEBUG == TRUE
  1400. p_prnt = t;
  1401. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"T", BT_OCTET16_LEN);
  1402. #endif
  1403. if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_mac_key, key_id, n1, n2, a1, a2,
  1404. length, mac_key)) {
  1405. SMP_TRACE_ERROR("%s failed to calc MacKey", __FUNCTION__);
  1406. return FALSE;
  1407. }
  1408. #if SMP_DEBUG == TRUE
  1409. p_prnt = mac_key;
  1410. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"MacKey", BT_OCTET16_LEN);
  1411. #endif
  1412. if (!smp_calculate_f5_mackey_or_long_term_key(t, counter_ltk, key_id, n1, n2, a1, a2,
  1413. length, ltk)) {
  1414. SMP_TRACE_ERROR("%s failed to calc LTK", __FUNCTION__);
  1415. return FALSE;
  1416. }
  1417. #if SMP_DEBUG == TRUE
  1418. p_prnt = ltk;
  1419. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"LTK", BT_OCTET16_LEN);
  1420. #endif
  1421. return TRUE;
  1422. }
  1423. /*******************************************************************************
  1424. **
  1425. ** Function smp_calculate_f5_mackey_or_long_term_key
  1426. **
  1427. ** Description The function calculates the value of MacKey or LTK by the rules
  1428. ** defined for f5 function.
  1429. ** At the moment exactly the same formula is used to calculate
  1430. ** LTK and MacKey.
  1431. ** The difference is the value of input parameter Counter:
  1432. ** - in MacKey calculations the value is 0;
  1433. ** - in LTK calculations the value is 1.
  1434. ** The formula:
  1435. ** mac = AES-CMAC (Counter||keyID||N1||N2||A1||A2||Length)
  1436. ** T
  1437. ** where
  1438. ** input: T is 256 bits;
  1439. ** Counter is 8 bits, its value is 0 for MacKey,
  1440. ** 1 for LTK;
  1441. ** keyID is 32 bits, its value is 0x62746c65;
  1442. ** N1 is 128 bits;
  1443. ** N2 is 128 bits;
  1444. ** A1 is 56 bits;
  1445. ** A2 is 56 bits;
  1446. ** Length is 16 bits, its value is 0x0100
  1447. ** output: LTK is 128 bit.
  1448. **
  1449. ** Returns FALSE if out of resources, TRUE in other cases.
  1450. **
  1451. ** Note The LSB is the first octet, the MSB is the last octet of
  1452. ** the AES-CMAC input/output stream.
  1453. **
  1454. *******************************************************************************/
  1455. BOOLEAN smp_calculate_f5_mackey_or_long_term_key(UINT8 *t, UINT8 *counter,
  1456. UINT8 *key_id, UINT8 *n1, UINT8 *n2, UINT8 *a1, UINT8 *a2,
  1457. UINT8 *length, UINT8 *mac)
  1458. {
  1459. UINT8 *p = NULL;
  1460. UINT8 cmac[BT_OCTET16_LEN];
  1461. UINT8 key[BT_OCTET16_LEN];
  1462. UINT8 msg_len = 1 /* Counter size */ + 4 /* keyID size */ +
  1463. BT_OCTET16_LEN /* N1 size */ + BT_OCTET16_LEN /* N2 size */ +
  1464. 7 /* A1 size*/ + 7 /* A2 size*/ + 2 /* Length size */;
  1465. UINT8 msg[1 + 4 + BT_OCTET16_LEN + BT_OCTET16_LEN + 7 + 7 + 2];
  1466. BOOLEAN ret = TRUE;
  1467. #if SMP_DEBUG == TRUE
  1468. UINT8 *p_prnt = NULL;
  1469. #endif
  1470. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1471. #if SMP_DEBUG == TRUE
  1472. p_prnt = t;
  1473. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"T", BT_OCTET16_LEN);
  1474. p_prnt = counter;
  1475. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Counter", 1);
  1476. p_prnt = key_id;
  1477. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"KeyID", 4);
  1478. p_prnt = n1;
  1479. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N1", BT_OCTET16_LEN);
  1480. p_prnt = n2;
  1481. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"N2", BT_OCTET16_LEN);
  1482. p_prnt = a1;
  1483. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"A1", 7);
  1484. p_prnt = a2;
  1485. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"A2", 7);
  1486. p_prnt = length;
  1487. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"Length", 2);
  1488. #endif
  1489. p = key;
  1490. ARRAY_TO_STREAM(p, t, BT_OCTET16_LEN);
  1491. #if SMP_DEBUG == TRUE
  1492. p_prnt = key;
  1493. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);
  1494. #endif
  1495. p = msg;
  1496. ARRAY_TO_STREAM(p, length, 2);
  1497. ARRAY_TO_STREAM(p, a2, 7);
  1498. ARRAY_TO_STREAM(p, a1, 7);
  1499. ARRAY_TO_STREAM(p, n2, BT_OCTET16_LEN);
  1500. ARRAY_TO_STREAM(p, n1, BT_OCTET16_LEN);
  1501. ARRAY_TO_STREAM(p, key_id, 4);
  1502. ARRAY_TO_STREAM(p, counter, 1);
  1503. #if SMP_DEBUG == TRUE
  1504. p_prnt = msg;
  1505. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"M", msg_len);
  1506. #endif
  1507. if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
  1508. SMP_TRACE_ERROR("%s failed", __FUNCTION__);
  1509. ret = FALSE;
  1510. }
  1511. #if SMP_DEBUG == TRUE
  1512. p_prnt = cmac;
  1513. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);
  1514. #endif
  1515. p = mac;
  1516. ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
  1517. return ret;
  1518. }
  1519. /*******************************************************************************
  1520. **
  1521. ** Function smp_calculate_f5_key
  1522. **
  1523. ** Description The function calculates key T used in calculation of
  1524. ** MacKey and LTK (f5 output is defined as MacKey || LTK).
  1525. ** T = AES-CMAC (W)
  1526. ** salt
  1527. ** where
  1528. ** Internal: salt is 128 bit.
  1529. ** input: W is 256 bit.
  1530. ** Output: T is 128 bit.
  1531. **
  1532. ** Returns FALSE if out of resources, TRUE in other cases.
  1533. **
  1534. ** Note The LSB is the first octet, the MSB is the last octet of
  1535. ** the AES-CMAC input/output stream.
  1536. **
  1537. *******************************************************************************/
  1538. BOOLEAN smp_calculate_f5_key(UINT8 *w, UINT8 *t)
  1539. {
  1540. UINT8 *p = NULL;
  1541. /* Please see 2.2.7 LE Secure Connections Key Generation Function f5 */
  1542. /*
  1543. salt: 6C88 8391 AAF5 A538 6037 0BDB 5A60 83BE
  1544. */
  1545. BT_OCTET16 salt = {
  1546. 0xBE, 0x83, 0x60, 0x5A, 0xDB, 0x0B, 0x37, 0x60,
  1547. 0x38, 0xA5, 0xF5, 0xAA, 0x91, 0x83, 0x88, 0x6C
  1548. };
  1549. #if SMP_DEBUG == TRUE
  1550. UINT8 *p_prnt = NULL;
  1551. #endif
  1552. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1553. #if SMP_DEBUG == TRUE
  1554. p_prnt = salt;
  1555. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"salt", BT_OCTET16_LEN);
  1556. p_prnt = w;
  1557. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"W", BT_OCTET32_LEN);
  1558. #endif
  1559. BT_OCTET16 key;
  1560. BT_OCTET32 msg;
  1561. p = key;
  1562. ARRAY_TO_STREAM(p, salt, BT_OCTET16_LEN);
  1563. p = msg;
  1564. ARRAY_TO_STREAM(p, w, BT_OCTET32_LEN);
  1565. #if SMP_DEBUG == TRUE
  1566. p_prnt = key;
  1567. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"K", BT_OCTET16_LEN);
  1568. p_prnt = msg;
  1569. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"M", BT_OCTET32_LEN);
  1570. #endif
  1571. BT_OCTET16 cmac;
  1572. BOOLEAN ret = TRUE;
  1573. if (!aes_cipher_msg_auth_code(key, msg, BT_OCTET32_LEN, BT_OCTET16_LEN, cmac)) {
  1574. SMP_TRACE_ERROR("%s failed", __FUNCTION__);
  1575. ret = FALSE;
  1576. }
  1577. #if SMP_DEBUG == TRUE
  1578. p_prnt = cmac;
  1579. smp_debug_print_nbyte_little_endian (p_prnt, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);
  1580. #endif
  1581. p = t;
  1582. ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
  1583. return ret;
  1584. }
  1585. /*******************************************************************************
  1586. **
  1587. ** Function smp_calculate_local_dhkey_check
  1588. **
  1589. ** Description The function calculates and saves local device DHKey check
  1590. ** value in CB.
  1591. ** Before doing this it calls smp_calculate_f5_mackey_and_long_term_key(...).
  1592. ** to calculate MacKey and LTK.
  1593. ** MacKey is used in dhkey calculation.
  1594. **
  1595. ** Returns void
  1596. **
  1597. *******************************************************************************/
  1598. void smp_calculate_local_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1599. {
  1600. UINT8 iocap[3], a[7], b[7];
  1601. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1602. smp_calculate_f5_mackey_and_long_term_key(p_cb);
  1603. smp_collect_local_io_capabilities(iocap, p_cb);
  1604. smp_collect_local_ble_address(a, p_cb);
  1605. smp_collect_peer_ble_address(b, p_cb);
  1606. smp_calculate_f6(p_cb->mac_key, p_cb->rand, p_cb->rrand, p_cb->peer_random, iocap, a, b,
  1607. p_cb->dhkey_check);
  1608. SMP_TRACE_EVENT ("local DHKey check calculation is completed");
  1609. }
  1610. /*******************************************************************************
  1611. **
  1612. ** Function smp_calculate_peer_dhkey_check
  1613. **
  1614. ** Description The function calculates peer device DHKey check value.
  1615. **
  1616. ** Returns void
  1617. **
  1618. *******************************************************************************/
  1619. void smp_calculate_peer_dhkey_check(tSMP_CB *p_cb, tSMP_INT_DATA *p_data)
  1620. {
  1621. UINT8 iocap[3], a[7], b[7];
  1622. BT_OCTET16 param_buf;
  1623. BOOLEAN ret;
  1624. tSMP_KEY key;
  1625. tSMP_STATUS status = SMP_PAIR_FAIL_UNKNOWN;
  1626. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1627. smp_collect_peer_io_capabilities(iocap, p_cb);
  1628. smp_collect_local_ble_address(a, p_cb);
  1629. smp_collect_peer_ble_address(b, p_cb);
  1630. ret = smp_calculate_f6(p_cb->mac_key, p_cb->rrand, p_cb->rand, p_cb->local_random, iocap,
  1631. b, a, param_buf);
  1632. if (ret) {
  1633. SMP_TRACE_EVENT ("peer DHKey check calculation is completed");
  1634. #if (SMP_DEBUG == TRUE)
  1635. smp_debug_print_nbyte_little_endian (param_buf, (const UINT8 *)"peer DHKey check",
  1636. BT_OCTET16_LEN);
  1637. #endif
  1638. key.key_type = SMP_KEY_TYPE_PEER_DHK_CHCK;
  1639. key.p_data = param_buf;
  1640. smp_sm_event(p_cb, SMP_SC_KEY_READY_EVT, &key);
  1641. } else {
  1642. SMP_TRACE_EVENT ("peer DHKey check calculation failed");
  1643. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &status);
  1644. }
  1645. }
  1646. /*******************************************************************************
  1647. **
  1648. ** Function smp_calculate_f6
  1649. **
  1650. ** Description The function calculates
  1651. ** C = f6(W, N1, N2, R, IOcap, A1, A2) = AES-CMAC (N1||N2||R||IOcap||A1||A2)
  1652. ** W
  1653. ** where
  1654. ** input: W is 128 bit,
  1655. ** N1 is 128 bit,
  1656. ** N2 is 128 bit,
  1657. ** R is 128 bit,
  1658. ** IOcap is 24 bit,
  1659. ** A1 is 56 bit,
  1660. ** A2 is 56 bit,
  1661. ** output: C is 128 bit.
  1662. **
  1663. ** Returns FALSE if out of resources, TRUE in other cases.
  1664. **
  1665. ** Note The LSB is the first octet, the MSB is the last octet of
  1666. ** the AES-CMAC input/output stream.
  1667. **
  1668. *******************************************************************************/
  1669. BOOLEAN smp_calculate_f6(UINT8 *w, UINT8 *n1, UINT8 *n2, UINT8 *r, UINT8 *iocap, UINT8 *a1,
  1670. UINT8 *a2, UINT8 *c)
  1671. {
  1672. UINT8 *p = NULL;
  1673. UINT8 msg_len = BT_OCTET16_LEN /* N1 size */ + BT_OCTET16_LEN /* N2 size */ +
  1674. BT_OCTET16_LEN /* R size */ + 3 /* IOcap size */ + 7 /* A1 size*/
  1675. + 7 /* A2 size*/;
  1676. UINT8 msg[BT_OCTET16_LEN + BT_OCTET16_LEN + BT_OCTET16_LEN + 3 + 7 + 7];
  1677. #if SMP_DEBUG == TRUE
  1678. UINT8 *p_print = NULL;
  1679. #endif
  1680. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1681. #if SMP_DEBUG == TRUE
  1682. p_print = w;
  1683. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"W", BT_OCTET16_LEN);
  1684. p_print = n1;
  1685. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"N1", BT_OCTET16_LEN);
  1686. p_print = n2;
  1687. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"N2", BT_OCTET16_LEN);
  1688. p_print = r;
  1689. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"R", BT_OCTET16_LEN);
  1690. p_print = iocap;
  1691. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"IOcap", 3);
  1692. p_print = a1;
  1693. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"A1", 7);
  1694. p_print = a2;
  1695. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"A2", 7);
  1696. #endif
  1697. UINT8 cmac[BT_OCTET16_LEN];
  1698. UINT8 key[BT_OCTET16_LEN];
  1699. p = key;
  1700. ARRAY_TO_STREAM(p, w, BT_OCTET16_LEN);
  1701. #if SMP_DEBUG == TRUE
  1702. p_print = key;
  1703. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"K", BT_OCTET16_LEN);
  1704. #endif
  1705. p = msg;
  1706. ARRAY_TO_STREAM(p, a2, 7);
  1707. ARRAY_TO_STREAM(p, a1, 7);
  1708. ARRAY_TO_STREAM(p, iocap, 3);
  1709. ARRAY_TO_STREAM(p, r, BT_OCTET16_LEN);
  1710. ARRAY_TO_STREAM(p, n2, BT_OCTET16_LEN);
  1711. ARRAY_TO_STREAM(p, n1, BT_OCTET16_LEN);
  1712. #if SMP_DEBUG == TRUE
  1713. p_print = msg;
  1714. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"M", msg_len);
  1715. #endif
  1716. BOOLEAN ret = TRUE;
  1717. if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
  1718. SMP_TRACE_ERROR("%s failed", __FUNCTION__);
  1719. ret = FALSE;
  1720. }
  1721. #if SMP_DEBUG == TRUE
  1722. p_print = cmac;
  1723. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);
  1724. #endif
  1725. p = c;
  1726. ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
  1727. return ret;
  1728. }
  1729. /*******************************************************************************
  1730. **
  1731. ** Function smp_calculate_link_key_from_long_term_key
  1732. **
  1733. ** Description The function calculates and saves BR/EDR link key derived from
  1734. ** LE SC LTK.
  1735. **
  1736. ** Returns FALSE if out of resources, TRUE in other cases.
  1737. **
  1738. *******************************************************************************/
  1739. BOOLEAN smp_calculate_link_key_from_long_term_key(tSMP_CB *p_cb)
  1740. {
  1741. tBTM_SEC_DEV_REC *p_dev_rec;
  1742. BD_ADDR bda_for_lk;
  1743. tBLE_ADDR_TYPE conn_addr_type;
  1744. SMP_TRACE_DEBUG ("%s", __func__);
  1745. if (p_cb->id_addr_rcvd && p_cb->id_addr_type == BLE_ADDR_PUBLIC) {
  1746. SMP_TRACE_DEBUG ("Use rcvd identity address as BD_ADDR of LK rcvd identity address");
  1747. memcpy(bda_for_lk, p_cb->id_addr, BD_ADDR_LEN);
  1748. } else if ((BTM_ReadRemoteConnectionAddr(p_cb->pairing_bda, bda_for_lk, &conn_addr_type)) &&
  1749. conn_addr_type == BLE_ADDR_PUBLIC) {
  1750. SMP_TRACE_DEBUG ("Use rcvd connection address as BD_ADDR of LK");
  1751. } else {
  1752. SMP_TRACE_WARNING ("Don't have peer public address to associate with LK");
  1753. return FALSE;
  1754. }
  1755. if ((p_dev_rec = btm_find_dev (p_cb->pairing_bda)) == NULL) {
  1756. SMP_TRACE_ERROR("%s failed to find Security Record", __func__);
  1757. return FALSE;
  1758. }
  1759. BT_OCTET16 intermediate_link_key;
  1760. BOOLEAN ret = TRUE;
  1761. ret = smp_calculate_h6(p_cb->ltk, (UINT8 *)"1pmt" /* reversed "tmp1" */, intermediate_link_key);
  1762. if (!ret) {
  1763. SMP_TRACE_ERROR("%s failed to derive intermediate_link_key", __func__);
  1764. return ret;
  1765. }
  1766. BT_OCTET16 link_key;
  1767. ret = smp_calculate_h6(intermediate_link_key, (UINT8 *) "rbel" /* reversed "lebr" */, link_key);
  1768. if (!ret) {
  1769. SMP_TRACE_ERROR("%s failed", __func__);
  1770. } else {
  1771. UINT8 link_key_type;
  1772. if (btm_cb.security_mode == BTM_SEC_MODE_SC) {
  1773. /* Secure Connections Only Mode */
  1774. link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
  1775. } else if (controller_get_interface()->supports_secure_connections()) {
  1776. /* both transports are SC capable */
  1777. if (p_cb->sec_level == SMP_SEC_AUTHENTICATED) {
  1778. link_key_type = BTM_LKEY_TYPE_AUTH_COMB_P_256;
  1779. } else {
  1780. link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB_P_256;
  1781. }
  1782. } else if (btm_cb.security_mode == BTM_SEC_MODE_SP) {
  1783. /* BR/EDR transport is SSP capable */
  1784. if (p_cb->sec_level == SMP_SEC_AUTHENTICATED) {
  1785. link_key_type = BTM_LKEY_TYPE_AUTH_COMB;
  1786. } else {
  1787. link_key_type = BTM_LKEY_TYPE_UNAUTH_COMB;
  1788. }
  1789. } else {
  1790. SMP_TRACE_ERROR ("%s failed to update link_key. Sec Mode = %d, sm4 = 0x%02x",
  1791. __func__, btm_cb.security_mode, p_dev_rec->sm4);
  1792. return FALSE;
  1793. }
  1794. link_key_type += BTM_LTK_DERIVED_LKEY_OFFSET;
  1795. UINT8 *p;
  1796. BT_OCTET16 notif_link_key;
  1797. p = notif_link_key;
  1798. ARRAY16_TO_STREAM(p, link_key);
  1799. btm_sec_link_key_notification (bda_for_lk, notif_link_key, link_key_type);
  1800. SMP_TRACE_EVENT ("%s is completed", __func__);
  1801. }
  1802. return ret;
  1803. }
  1804. /*******************************************************************************
  1805. **
  1806. ** Function smp_calculate_long_term_key_from_link_key
  1807. **
  1808. ** Description The function calculates and saves SC LTK derived from BR/EDR
  1809. ** link key.
  1810. **
  1811. ** Returns FALSE if out of resources, TRUE in other cases.
  1812. **
  1813. *******************************************************************************/
  1814. BOOLEAN smp_calculate_long_term_key_from_link_key(tSMP_CB *p_cb)
  1815. {
  1816. BOOLEAN ret = TRUE;
  1817. tBTM_SEC_DEV_REC *p_dev_rec;
  1818. UINT8 rev_link_key[16];
  1819. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1820. if ((p_dev_rec = btm_find_dev (p_cb->pairing_bda)) == NULL) {
  1821. SMP_TRACE_ERROR("%s failed to find Security Record", __FUNCTION__);
  1822. return FALSE;
  1823. }
  1824. UINT8 br_link_key_type;
  1825. if ((br_link_key_type = BTM_SecGetDeviceLinkKeyType (p_cb->pairing_bda))
  1826. == BTM_LKEY_TYPE_IGNORE) {
  1827. SMP_TRACE_ERROR("%s failed to retrieve BR link type", __FUNCTION__);
  1828. return FALSE;
  1829. }
  1830. if ((br_link_key_type != BTM_LKEY_TYPE_AUTH_COMB_P_256) &&
  1831. (br_link_key_type != BTM_LKEY_TYPE_UNAUTH_COMB_P_256)) {
  1832. SMP_TRACE_ERROR("%s LE SC LTK can't be derived from LK %d",
  1833. __FUNCTION__, br_link_key_type);
  1834. return FALSE;
  1835. }
  1836. UINT8 *p1;
  1837. UINT8 *p2;
  1838. p1 = rev_link_key;
  1839. p2 = p_dev_rec->link_key;
  1840. REVERSE_ARRAY_TO_STREAM(p1, p2, 16);
  1841. BT_OCTET16 intermediate_long_term_key;
  1842. /* "tmp2" obtained from the spec */
  1843. ret = smp_calculate_h6(rev_link_key, (UINT8 *) "2pmt" /* reversed "tmp2" */,
  1844. intermediate_long_term_key);
  1845. if (!ret) {
  1846. SMP_TRACE_ERROR("%s failed to derive intermediate_long_term_key", __FUNCTION__);
  1847. return ret;
  1848. }
  1849. /* "brle" obtained from the spec */
  1850. ret = smp_calculate_h6(intermediate_long_term_key, (UINT8 *) "elrb" /* reversed "brle" */,
  1851. p_cb->ltk);
  1852. if (!ret) {
  1853. SMP_TRACE_ERROR("%s failed", __FUNCTION__);
  1854. } else {
  1855. p_cb->sec_level = (br_link_key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256)
  1856. ? SMP_SEC_AUTHENTICATED : SMP_SEC_UNAUTHENTICATE;
  1857. SMP_TRACE_EVENT ("%s is completed", __FUNCTION__);
  1858. }
  1859. return ret;
  1860. }
  1861. /*******************************************************************************
  1862. **
  1863. ** Function smp_calculate_h6
  1864. **
  1865. ** Description The function calculates
  1866. ** C = h6(W, KeyID) = AES-CMAC (KeyID)
  1867. ** W
  1868. ** where
  1869. ** input: W is 128 bit,
  1870. ** KeyId is 32 bit,
  1871. ** output: C is 128 bit.
  1872. **
  1873. ** Returns FALSE if out of resources, TRUE in other cases.
  1874. **
  1875. ** Note The LSB is the first octet, the MSB is the last octet of
  1876. ** the AES-CMAC input/output stream.
  1877. **
  1878. *******************************************************************************/
  1879. BOOLEAN smp_calculate_h6(UINT8 *w, UINT8 *keyid, UINT8 *c)
  1880. {
  1881. #if SMP_DEBUG == TRUE
  1882. UINT8 *p_print = NULL;
  1883. #endif
  1884. SMP_TRACE_DEBUG ("%s", __FUNCTION__);
  1885. #if SMP_DEBUG == TRUE
  1886. p_print = w;
  1887. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"W", BT_OCTET16_LEN);
  1888. p_print = keyid;
  1889. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"keyID", 4);
  1890. #endif
  1891. UINT8 *p = NULL;
  1892. UINT8 key[BT_OCTET16_LEN];
  1893. p = key;
  1894. ARRAY_TO_STREAM(p, w, BT_OCTET16_LEN);
  1895. #if SMP_DEBUG == TRUE
  1896. p_print = key;
  1897. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"K", BT_OCTET16_LEN);
  1898. #endif
  1899. UINT8 msg_len = 4 /* KeyID size */;
  1900. UINT8 msg[4];
  1901. p = msg;
  1902. ARRAY_TO_STREAM(p, keyid, 4);
  1903. #if SMP_DEBUG == TRUE
  1904. p_print = msg;
  1905. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *) "M", msg_len);
  1906. #endif
  1907. BOOLEAN ret = TRUE;
  1908. UINT8 cmac[BT_OCTET16_LEN];
  1909. if (!aes_cipher_msg_auth_code(key, msg, msg_len, BT_OCTET16_LEN, cmac)) {
  1910. SMP_TRACE_ERROR("%s failed", __FUNCTION__);
  1911. ret = FALSE;
  1912. }
  1913. #if SMP_DEBUG == TRUE
  1914. p_print = cmac;
  1915. smp_debug_print_nbyte_little_endian (p_print, (const UINT8 *)"AES-CMAC", BT_OCTET16_LEN);
  1916. #endif
  1917. p = c;
  1918. ARRAY_TO_STREAM(p, cmac, BT_OCTET16_LEN);
  1919. return ret;
  1920. }
  1921. /*******************************************************************************
  1922. **
  1923. ** Function smp_start_nonce_generation
  1924. **
  1925. ** Description This function starts nonce generation.
  1926. **
  1927. ** Returns void
  1928. **
  1929. *******************************************************************************/
  1930. void smp_start_nonce_generation(tSMP_CB *p_cb)
  1931. {
  1932. SMP_TRACE_DEBUG("%s", __FUNCTION__);
  1933. p_cb->rand_enc_proc_state = SMP_GEN_NONCE_0_7;
  1934. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  1935. smp_rand_back(NULL);
  1936. }
  1937. }
  1938. /*******************************************************************************
  1939. **
  1940. ** Function smp_finish_nonce_generation
  1941. **
  1942. ** Description This function finishes nonce generation.
  1943. **
  1944. ** Returns void
  1945. **
  1946. *******************************************************************************/
  1947. void smp_finish_nonce_generation(tSMP_CB *p_cb)
  1948. {
  1949. SMP_TRACE_DEBUG("%s", __FUNCTION__);
  1950. p_cb->rand_enc_proc_state = SMP_GEN_NONCE_8_15;
  1951. if (!btsnd_hcic_ble_rand((void *)smp_rand_back)) {
  1952. smp_rand_back(NULL);
  1953. }
  1954. }
  1955. /*******************************************************************************
  1956. **
  1957. ** Function smp_process_new_nonce
  1958. **
  1959. ** Description This function notifies SM that it has new nonce.
  1960. **
  1961. ** Returns void
  1962. **
  1963. *******************************************************************************/
  1964. void smp_process_new_nonce(tSMP_CB *p_cb)
  1965. {
  1966. SMP_TRACE_DEBUG ("%s round %d", __FUNCTION__, p_cb->round);
  1967. smp_sm_event(p_cb, SMP_HAVE_LOC_NONCE_EVT, NULL);
  1968. }
  1969. /*******************************************************************************
  1970. **
  1971. ** Function smp_rand_back
  1972. **
  1973. ** Description This function is to process the rand command finished,
  1974. ** process the random/encrypted number for further action.
  1975. **
  1976. ** Returns void
  1977. **
  1978. *******************************************************************************/
  1979. static void smp_rand_back(tBTM_RAND_ENC *p)
  1980. {
  1981. tSMP_CB *p_cb = &smp_cb;
  1982. UINT8 *pp = p->param_buf;
  1983. UINT8 failure = SMP_PAIR_FAIL_UNKNOWN;
  1984. UINT8 state = p_cb->rand_enc_proc_state & ~0x80;
  1985. SMP_TRACE_DEBUG ("%s state=0x%x", __FUNCTION__, state);
  1986. if (p && p->status == HCI_SUCCESS) {
  1987. switch (state) {
  1988. case SMP_GEN_SRAND_MRAND:
  1989. memcpy((void *)p_cb->rand, p->param_buf, p->param_len);
  1990. smp_generate_rand_cont(p_cb, NULL);
  1991. break;
  1992. case SMP_GEN_SRAND_MRAND_CONT:
  1993. memcpy((void *)&p_cb->rand[8], p->param_buf, p->param_len);
  1994. smp_generate_confirm(p_cb, NULL);
  1995. break;
  1996. case SMP_GEN_DIV_LTK:
  1997. STREAM_TO_UINT16(p_cb->div, pp);
  1998. smp_generate_ltk_cont(p_cb, NULL);
  1999. break;
  2000. case SMP_GEN_DIV_CSRK:
  2001. STREAM_TO_UINT16(p_cb->div, pp);
  2002. smp_compute_csrk(p_cb, NULL);
  2003. break;
  2004. case SMP_GEN_TK:
  2005. smp_proc_passkey(p_cb, p);
  2006. break;
  2007. case SMP_GEN_RAND_V:
  2008. memcpy(p_cb->enc_rand, p->param_buf, BT_OCTET8_LEN);
  2009. smp_generate_y(p_cb, NULL);
  2010. break;
  2011. case SMP_GENERATE_PRIVATE_KEY_0_7:
  2012. case SMP_GENERATE_PRIVATE_KEY_8_15:
  2013. case SMP_GENERATE_PRIVATE_KEY_16_23:
  2014. case SMP_GENERATE_PRIVATE_KEY_24_31:
  2015. smp_continue_private_key_creation(p_cb, p);
  2016. break;
  2017. case SMP_GEN_NONCE_0_7:
  2018. memcpy((void *)p_cb->rand, p->param_buf, p->param_len);
  2019. smp_finish_nonce_generation(p_cb);
  2020. break;
  2021. case SMP_GEN_NONCE_8_15:
  2022. memcpy((void *)&p_cb->rand[8], p->param_buf, p->param_len);
  2023. smp_process_new_nonce(p_cb);
  2024. break;
  2025. }
  2026. return;
  2027. }
  2028. SMP_TRACE_ERROR("%s key generation failed: (%d)", __FUNCTION__, p_cb->rand_enc_proc_state);
  2029. smp_sm_event(p_cb, SMP_AUTH_CMPL_EVT, &failure);
  2030. }
  2031. #endif