smp_keys.c 76 KB

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