smp.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2 as
  6. published by the Free Software Foundation;
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  10. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  11. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  16. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  17. SOFTWARE IS DISCLAIMED.
  18. */
  19. #include <linux/debugfs.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/crypto.h>
  22. #include <crypto/algapi.h>
  23. #include <crypto/b128ops.h>
  24. #include <crypto/hash.h>
  25. #include <net/bluetooth/bluetooth.h>
  26. #include <net/bluetooth/hci_core.h>
  27. #include <net/bluetooth/l2cap.h>
  28. #include <net/bluetooth/mgmt.h>
  29. #include "ecc.h"
  30. #include "smp.h"
  31. #define SMP_DEV(hdev) \
  32. ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data)
  33. /* Low-level debug macros to be used for stuff that we don't want
  34. * accidentially in dmesg, i.e. the values of the various crypto keys
  35. * and the inputs & outputs of crypto functions.
  36. */
  37. #ifdef DEBUG
  38. #define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
  39. ##__VA_ARGS__)
  40. #else
  41. #define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
  42. ##__VA_ARGS__)
  43. #endif
  44. #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
  45. /* Keys which are not distributed with Secure Connections */
  46. #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
  47. #define SMP_TIMEOUT msecs_to_jiffies(30000)
  48. #define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
  49. 0x1f : 0x07)
  50. #define KEY_DIST_MASK 0x07
  51. /* Maximum message length that can be passed to aes_cmac */
  52. #define CMAC_MSG_MAX 80
  53. enum {
  54. SMP_FLAG_TK_VALID,
  55. SMP_FLAG_CFM_PENDING,
  56. SMP_FLAG_MITM_AUTH,
  57. SMP_FLAG_COMPLETE,
  58. SMP_FLAG_INITIATOR,
  59. SMP_FLAG_SC,
  60. SMP_FLAG_REMOTE_PK,
  61. SMP_FLAG_DEBUG_KEY,
  62. SMP_FLAG_WAIT_USER,
  63. SMP_FLAG_DHKEY_PENDING,
  64. SMP_FLAG_REMOTE_OOB,
  65. SMP_FLAG_LOCAL_OOB,
  66. };
  67. struct smp_dev {
  68. /* Secure Connections OOB data */
  69. u8 local_pk[64];
  70. u8 local_sk[32];
  71. u8 local_rand[16];
  72. bool debug_key;
  73. u8 min_key_size;
  74. u8 max_key_size;
  75. struct crypto_cipher *tfm_aes;
  76. struct crypto_shash *tfm_cmac;
  77. };
  78. struct smp_chan {
  79. struct l2cap_conn *conn;
  80. struct delayed_work security_timer;
  81. unsigned long allow_cmd; /* Bitmask of allowed commands */
  82. u8 preq[7]; /* SMP Pairing Request */
  83. u8 prsp[7]; /* SMP Pairing Response */
  84. u8 prnd[16]; /* SMP Pairing Random (local) */
  85. u8 rrnd[16]; /* SMP Pairing Random (remote) */
  86. u8 pcnf[16]; /* SMP Pairing Confirm */
  87. u8 tk[16]; /* SMP Temporary Key */
  88. u8 rr[16]; /* Remote OOB ra/rb value */
  89. u8 lr[16]; /* Local OOB ra/rb value */
  90. u8 enc_key_size;
  91. u8 remote_key_dist;
  92. bdaddr_t id_addr;
  93. u8 id_addr_type;
  94. u8 irk[16];
  95. struct smp_csrk *csrk;
  96. struct smp_csrk *slave_csrk;
  97. struct smp_ltk *ltk;
  98. struct smp_ltk *slave_ltk;
  99. struct smp_irk *remote_irk;
  100. u8 *link_key;
  101. unsigned long flags;
  102. u8 method;
  103. u8 passkey_round;
  104. /* Secure Connections variables */
  105. u8 local_pk[64];
  106. u8 local_sk[32];
  107. u8 remote_pk[64];
  108. u8 dhkey[32];
  109. u8 mackey[16];
  110. struct crypto_cipher *tfm_aes;
  111. struct crypto_shash *tfm_cmac;
  112. };
  113. /* These debug key values are defined in the SMP section of the core
  114. * specification. debug_pk is the public debug key and debug_sk the
  115. * private debug key.
  116. */
  117. static const u8 debug_pk[64] = {
  118. 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
  119. 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
  120. 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
  121. 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
  122. 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
  123. 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
  124. 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
  125. 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
  126. };
  127. static const u8 debug_sk[32] = {
  128. 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
  129. 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
  130. 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
  131. 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
  132. };
  133. static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
  134. {
  135. size_t i;
  136. for (i = 0; i < len; i++)
  137. dst[len - 1 - i] = src[i];
  138. }
  139. /* The following functions map to the LE SC SMP crypto functions
  140. * AES-CMAC, f4, f5, f6, g2 and h6.
  141. */
  142. static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m,
  143. size_t len, u8 mac[16])
  144. {
  145. uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
  146. SHASH_DESC_ON_STACK(desc, tfm);
  147. int err;
  148. if (len > CMAC_MSG_MAX)
  149. return -EFBIG;
  150. if (!tfm) {
  151. BT_ERR("tfm %p", tfm);
  152. return -EINVAL;
  153. }
  154. desc->tfm = tfm;
  155. desc->flags = 0;
  156. /* Swap key and message from LSB to MSB */
  157. swap_buf(k, tmp, 16);
  158. swap_buf(m, msg_msb, len);
  159. SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
  160. SMP_DBG("key %16phN", k);
  161. err = crypto_shash_setkey(tfm, tmp, 16);
  162. if (err) {
  163. BT_ERR("cipher setkey failed: %d", err);
  164. return err;
  165. }
  166. err = crypto_shash_digest(desc, msg_msb, len, mac_msb);
  167. shash_desc_zero(desc);
  168. if (err) {
  169. BT_ERR("Hash computation error %d", err);
  170. return err;
  171. }
  172. swap_buf(mac_msb, mac, 16);
  173. SMP_DBG("mac %16phN", mac);
  174. return 0;
  175. }
  176. static int smp_f4(struct crypto_shash *tfm_cmac, const u8 u[32],
  177. const u8 v[32], const u8 x[16], u8 z, u8 res[16])
  178. {
  179. u8 m[65];
  180. int err;
  181. SMP_DBG("u %32phN", u);
  182. SMP_DBG("v %32phN", v);
  183. SMP_DBG("x %16phN z %02x", x, z);
  184. m[0] = z;
  185. memcpy(m + 1, v, 32);
  186. memcpy(m + 33, u, 32);
  187. err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
  188. if (err)
  189. return err;
  190. SMP_DBG("res %16phN", res);
  191. return err;
  192. }
  193. static int smp_f5(struct crypto_shash *tfm_cmac, const u8 w[32],
  194. const u8 n1[16], const u8 n2[16], const u8 a1[7],
  195. const u8 a2[7], u8 mackey[16], u8 ltk[16])
  196. {
  197. /* The btle, salt and length "magic" values are as defined in
  198. * the SMP section of the Bluetooth core specification. In ASCII
  199. * the btle value ends up being 'btle'. The salt is just a
  200. * random number whereas length is the value 256 in little
  201. * endian format.
  202. */
  203. const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
  204. const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
  205. 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
  206. const u8 length[2] = { 0x00, 0x01 };
  207. u8 m[53], t[16];
  208. int err;
  209. SMP_DBG("w %32phN", w);
  210. SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
  211. SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
  212. err = aes_cmac(tfm_cmac, salt, w, 32, t);
  213. if (err)
  214. return err;
  215. SMP_DBG("t %16phN", t);
  216. memcpy(m, length, 2);
  217. memcpy(m + 2, a2, 7);
  218. memcpy(m + 9, a1, 7);
  219. memcpy(m + 16, n2, 16);
  220. memcpy(m + 32, n1, 16);
  221. memcpy(m + 48, btle, 4);
  222. m[52] = 0; /* Counter */
  223. err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
  224. if (err)
  225. return err;
  226. SMP_DBG("mackey %16phN", mackey);
  227. m[52] = 1; /* Counter */
  228. err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
  229. if (err)
  230. return err;
  231. SMP_DBG("ltk %16phN", ltk);
  232. return 0;
  233. }
  234. static int smp_f6(struct crypto_shash *tfm_cmac, const u8 w[16],
  235. const u8 n1[16], const u8 n2[16], const u8 r[16],
  236. const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
  237. u8 res[16])
  238. {
  239. u8 m[65];
  240. int err;
  241. SMP_DBG("w %16phN", w);
  242. SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
  243. SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
  244. memcpy(m, a2, 7);
  245. memcpy(m + 7, a1, 7);
  246. memcpy(m + 14, io_cap, 3);
  247. memcpy(m + 17, r, 16);
  248. memcpy(m + 33, n2, 16);
  249. memcpy(m + 49, n1, 16);
  250. err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
  251. if (err)
  252. return err;
  253. SMP_DBG("res %16phN", res);
  254. return err;
  255. }
  256. static int smp_g2(struct crypto_shash *tfm_cmac, const u8 u[32], const u8 v[32],
  257. const u8 x[16], const u8 y[16], u32 *val)
  258. {
  259. u8 m[80], tmp[16];
  260. int err;
  261. SMP_DBG("u %32phN", u);
  262. SMP_DBG("v %32phN", v);
  263. SMP_DBG("x %16phN y %16phN", x, y);
  264. memcpy(m, y, 16);
  265. memcpy(m + 16, v, 32);
  266. memcpy(m + 48, u, 32);
  267. err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
  268. if (err)
  269. return err;
  270. *val = get_unaligned_le32(tmp);
  271. *val %= 1000000;
  272. SMP_DBG("val %06u", *val);
  273. return 0;
  274. }
  275. static int smp_h6(struct crypto_shash *tfm_cmac, const u8 w[16],
  276. const u8 key_id[4], u8 res[16])
  277. {
  278. int err;
  279. SMP_DBG("w %16phN key_id %4phN", w, key_id);
  280. err = aes_cmac(tfm_cmac, w, key_id, 4, res);
  281. if (err)
  282. return err;
  283. SMP_DBG("res %16phN", res);
  284. return err;
  285. }
  286. /* The following functions map to the legacy SMP crypto functions e, c1,
  287. * s1 and ah.
  288. */
  289. static int smp_e(struct crypto_cipher *tfm, const u8 *k, u8 *r)
  290. {
  291. uint8_t tmp[16], data[16];
  292. int err;
  293. SMP_DBG("k %16phN r %16phN", k, r);
  294. if (!tfm) {
  295. BT_ERR("tfm %p", tfm);
  296. return -EINVAL;
  297. }
  298. /* The most significant octet of key corresponds to k[0] */
  299. swap_buf(k, tmp, 16);
  300. err = crypto_cipher_setkey(tfm, tmp, 16);
  301. if (err) {
  302. BT_ERR("cipher setkey failed: %d", err);
  303. return err;
  304. }
  305. /* Most significant octet of plaintextData corresponds to data[0] */
  306. swap_buf(r, data, 16);
  307. crypto_cipher_encrypt_one(tfm, data, data);
  308. /* Most significant octet of encryptedData corresponds to data[0] */
  309. swap_buf(data, r, 16);
  310. SMP_DBG("r %16phN", r);
  311. return err;
  312. }
  313. static int smp_c1(struct crypto_cipher *tfm_aes, const u8 k[16],
  314. const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
  315. const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
  316. {
  317. u8 p1[16], p2[16];
  318. int err;
  319. SMP_DBG("k %16phN r %16phN", k, r);
  320. SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
  321. SMP_DBG("preq %7phN pres %7phN", preq, pres);
  322. memset(p1, 0, 16);
  323. /* p1 = pres || preq || _rat || _iat */
  324. p1[0] = _iat;
  325. p1[1] = _rat;
  326. memcpy(p1 + 2, preq, 7);
  327. memcpy(p1 + 9, pres, 7);
  328. SMP_DBG("p1 %16phN", p1);
  329. /* res = r XOR p1 */
  330. u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
  331. /* res = e(k, res) */
  332. err = smp_e(tfm_aes, k, res);
  333. if (err) {
  334. BT_ERR("Encrypt data error");
  335. return err;
  336. }
  337. /* p2 = padding || ia || ra */
  338. memcpy(p2, ra, 6);
  339. memcpy(p2 + 6, ia, 6);
  340. memset(p2 + 12, 0, 4);
  341. SMP_DBG("p2 %16phN", p2);
  342. /* res = res XOR p2 */
  343. u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
  344. /* res = e(k, res) */
  345. err = smp_e(tfm_aes, k, res);
  346. if (err)
  347. BT_ERR("Encrypt data error");
  348. return err;
  349. }
  350. static int smp_s1(struct crypto_cipher *tfm_aes, const u8 k[16],
  351. const u8 r1[16], const u8 r2[16], u8 _r[16])
  352. {
  353. int err;
  354. /* Just least significant octets from r1 and r2 are considered */
  355. memcpy(_r, r2, 8);
  356. memcpy(_r + 8, r1, 8);
  357. err = smp_e(tfm_aes, k, _r);
  358. if (err)
  359. BT_ERR("Encrypt data error");
  360. return err;
  361. }
  362. static int smp_ah(struct crypto_cipher *tfm, const u8 irk[16],
  363. const u8 r[3], u8 res[3])
  364. {
  365. u8 _res[16];
  366. int err;
  367. /* r' = padding || r */
  368. memcpy(_res, r, 3);
  369. memset(_res + 3, 0, 13);
  370. err = smp_e(tfm, irk, _res);
  371. if (err) {
  372. BT_ERR("Encrypt error");
  373. return err;
  374. }
  375. /* The output of the random address function ah is:
  376. * ah(k, r) = e(k, r') mod 2^24
  377. * The output of the security function e is then truncated to 24 bits
  378. * by taking the least significant 24 bits of the output of e as the
  379. * result of ah.
  380. */
  381. memcpy(res, _res, 3);
  382. return 0;
  383. }
  384. bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
  385. const bdaddr_t *bdaddr)
  386. {
  387. struct l2cap_chan *chan = hdev->smp_data;
  388. struct smp_dev *smp;
  389. u8 hash[3];
  390. int err;
  391. if (!chan || !chan->data)
  392. return false;
  393. smp = chan->data;
  394. BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
  395. err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
  396. if (err)
  397. return false;
  398. return !crypto_memneq(bdaddr->b, hash, 3);
  399. }
  400. int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
  401. {
  402. struct l2cap_chan *chan = hdev->smp_data;
  403. struct smp_dev *smp;
  404. int err;
  405. if (!chan || !chan->data)
  406. return -EOPNOTSUPP;
  407. smp = chan->data;
  408. get_random_bytes(&rpa->b[3], 3);
  409. rpa->b[5] &= 0x3f; /* Clear two most significant bits */
  410. rpa->b[5] |= 0x40; /* Set second most significant bit */
  411. err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
  412. if (err < 0)
  413. return err;
  414. BT_DBG("RPA %pMR", rpa);
  415. return 0;
  416. }
  417. int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
  418. {
  419. struct l2cap_chan *chan = hdev->smp_data;
  420. struct smp_dev *smp;
  421. int err;
  422. if (!chan || !chan->data)
  423. return -EOPNOTSUPP;
  424. smp = chan->data;
  425. if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
  426. BT_DBG("Using debug keys");
  427. memcpy(smp->local_pk, debug_pk, 64);
  428. memcpy(smp->local_sk, debug_sk, 32);
  429. smp->debug_key = true;
  430. } else {
  431. while (true) {
  432. /* Generate local key pair for Secure Connections */
  433. if (!ecc_make_key(smp->local_pk, smp->local_sk))
  434. return -EIO;
  435. /* This is unlikely, but we need to check that
  436. * we didn't accidentially generate a debug key.
  437. */
  438. if (crypto_memneq(smp->local_sk, debug_sk, 32))
  439. break;
  440. }
  441. smp->debug_key = false;
  442. }
  443. SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
  444. SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
  445. SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
  446. get_random_bytes(smp->local_rand, 16);
  447. err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
  448. smp->local_rand, 0, hash);
  449. if (err < 0)
  450. return err;
  451. memcpy(rand, smp->local_rand, 16);
  452. return 0;
  453. }
  454. static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
  455. {
  456. struct l2cap_chan *chan = conn->smp;
  457. struct smp_chan *smp;
  458. struct kvec iv[2];
  459. struct msghdr msg;
  460. if (!chan)
  461. return;
  462. BT_DBG("code 0x%2.2x", code);
  463. iv[0].iov_base = &code;
  464. iv[0].iov_len = 1;
  465. iv[1].iov_base = data;
  466. iv[1].iov_len = len;
  467. memset(&msg, 0, sizeof(msg));
  468. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
  469. l2cap_chan_send(chan, &msg, 1 + len);
  470. if (!chan->data)
  471. return;
  472. smp = chan->data;
  473. cancel_delayed_work_sync(&smp->security_timer);
  474. schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
  475. }
  476. static u8 authreq_to_seclevel(u8 authreq)
  477. {
  478. if (authreq & SMP_AUTH_MITM) {
  479. if (authreq & SMP_AUTH_SC)
  480. return BT_SECURITY_FIPS;
  481. else
  482. return BT_SECURITY_HIGH;
  483. } else {
  484. return BT_SECURITY_MEDIUM;
  485. }
  486. }
  487. static __u8 seclevel_to_authreq(__u8 sec_level)
  488. {
  489. switch (sec_level) {
  490. case BT_SECURITY_FIPS:
  491. case BT_SECURITY_HIGH:
  492. return SMP_AUTH_MITM | SMP_AUTH_BONDING;
  493. case BT_SECURITY_MEDIUM:
  494. return SMP_AUTH_BONDING;
  495. default:
  496. return SMP_AUTH_NONE;
  497. }
  498. }
  499. static void build_pairing_cmd(struct l2cap_conn *conn,
  500. struct smp_cmd_pairing *req,
  501. struct smp_cmd_pairing *rsp, __u8 authreq)
  502. {
  503. struct l2cap_chan *chan = conn->smp;
  504. struct smp_chan *smp = chan->data;
  505. struct hci_conn *hcon = conn->hcon;
  506. struct hci_dev *hdev = hcon->hdev;
  507. u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
  508. if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
  509. local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
  510. remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
  511. authreq |= SMP_AUTH_BONDING;
  512. } else {
  513. authreq &= ~SMP_AUTH_BONDING;
  514. }
  515. if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
  516. remote_dist |= SMP_DIST_ID_KEY;
  517. if (hci_dev_test_flag(hdev, HCI_PRIVACY))
  518. local_dist |= SMP_DIST_ID_KEY;
  519. if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
  520. (authreq & SMP_AUTH_SC)) {
  521. struct oob_data *oob_data;
  522. u8 bdaddr_type;
  523. if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
  524. local_dist |= SMP_DIST_LINK_KEY;
  525. remote_dist |= SMP_DIST_LINK_KEY;
  526. }
  527. if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
  528. bdaddr_type = BDADDR_LE_PUBLIC;
  529. else
  530. bdaddr_type = BDADDR_LE_RANDOM;
  531. oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
  532. bdaddr_type);
  533. if (oob_data && oob_data->present) {
  534. set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
  535. oob_flag = SMP_OOB_PRESENT;
  536. memcpy(smp->rr, oob_data->rand256, 16);
  537. memcpy(smp->pcnf, oob_data->hash256, 16);
  538. SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
  539. SMP_DBG("OOB Remote Random: %16phN", smp->rr);
  540. }
  541. } else {
  542. authreq &= ~SMP_AUTH_SC;
  543. }
  544. if (rsp == NULL) {
  545. req->io_capability = conn->hcon->io_capability;
  546. req->oob_flag = oob_flag;
  547. req->max_key_size = SMP_DEV(hdev)->max_key_size;
  548. req->init_key_dist = local_dist;
  549. req->resp_key_dist = remote_dist;
  550. req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
  551. smp->remote_key_dist = remote_dist;
  552. return;
  553. }
  554. rsp->io_capability = conn->hcon->io_capability;
  555. rsp->oob_flag = oob_flag;
  556. rsp->max_key_size = SMP_DEV(hdev)->max_key_size;
  557. rsp->init_key_dist = req->init_key_dist & remote_dist;
  558. rsp->resp_key_dist = req->resp_key_dist & local_dist;
  559. rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
  560. smp->remote_key_dist = rsp->init_key_dist;
  561. }
  562. static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
  563. {
  564. struct l2cap_chan *chan = conn->smp;
  565. struct hci_dev *hdev = conn->hcon->hdev;
  566. struct smp_chan *smp = chan->data;
  567. if (max_key_size > SMP_DEV(hdev)->max_key_size ||
  568. max_key_size < SMP_MIN_ENC_KEY_SIZE)
  569. return SMP_ENC_KEY_SIZE;
  570. smp->enc_key_size = max_key_size;
  571. return 0;
  572. }
  573. static void smp_chan_destroy(struct l2cap_conn *conn)
  574. {
  575. struct l2cap_chan *chan = conn->smp;
  576. struct smp_chan *smp = chan->data;
  577. struct hci_conn *hcon = conn->hcon;
  578. bool complete;
  579. BUG_ON(!smp);
  580. cancel_delayed_work_sync(&smp->security_timer);
  581. complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
  582. mgmt_smp_complete(hcon, complete);
  583. kzfree(smp->csrk);
  584. kzfree(smp->slave_csrk);
  585. kzfree(smp->link_key);
  586. crypto_free_cipher(smp->tfm_aes);
  587. crypto_free_shash(smp->tfm_cmac);
  588. /* Ensure that we don't leave any debug key around if debug key
  589. * support hasn't been explicitly enabled.
  590. */
  591. if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
  592. !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
  593. list_del_rcu(&smp->ltk->list);
  594. kfree_rcu(smp->ltk, rcu);
  595. smp->ltk = NULL;
  596. }
  597. /* If pairing failed clean up any keys we might have */
  598. if (!complete) {
  599. if (smp->ltk) {
  600. list_del_rcu(&smp->ltk->list);
  601. kfree_rcu(smp->ltk, rcu);
  602. }
  603. if (smp->slave_ltk) {
  604. list_del_rcu(&smp->slave_ltk->list);
  605. kfree_rcu(smp->slave_ltk, rcu);
  606. }
  607. if (smp->remote_irk) {
  608. list_del_rcu(&smp->remote_irk->list);
  609. kfree_rcu(smp->remote_irk, rcu);
  610. }
  611. }
  612. chan->data = NULL;
  613. kzfree(smp);
  614. hci_conn_drop(hcon);
  615. }
  616. static void smp_failure(struct l2cap_conn *conn, u8 reason)
  617. {
  618. struct hci_conn *hcon = conn->hcon;
  619. struct l2cap_chan *chan = conn->smp;
  620. if (reason)
  621. smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
  622. &reason);
  623. mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
  624. if (chan->data)
  625. smp_chan_destroy(conn);
  626. }
  627. #define JUST_WORKS 0x00
  628. #define JUST_CFM 0x01
  629. #define REQ_PASSKEY 0x02
  630. #define CFM_PASSKEY 0x03
  631. #define REQ_OOB 0x04
  632. #define DSP_PASSKEY 0x05
  633. #define OVERLAP 0xFF
  634. static const u8 gen_method[5][5] = {
  635. { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
  636. { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
  637. { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
  638. { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
  639. { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
  640. };
  641. static const u8 sc_method[5][5] = {
  642. { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
  643. { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
  644. { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
  645. { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
  646. { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
  647. };
  648. static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
  649. {
  650. /* If either side has unknown io_caps, use JUST_CFM (which gets
  651. * converted later to JUST_WORKS if we're initiators.
  652. */
  653. if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
  654. remote_io > SMP_IO_KEYBOARD_DISPLAY)
  655. return JUST_CFM;
  656. if (test_bit(SMP_FLAG_SC, &smp->flags))
  657. return sc_method[remote_io][local_io];
  658. return gen_method[remote_io][local_io];
  659. }
  660. static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
  661. u8 local_io, u8 remote_io)
  662. {
  663. struct hci_conn *hcon = conn->hcon;
  664. struct l2cap_chan *chan = conn->smp;
  665. struct smp_chan *smp = chan->data;
  666. u32 passkey = 0;
  667. int ret = 0;
  668. /* Initialize key for JUST WORKS */
  669. memset(smp->tk, 0, sizeof(smp->tk));
  670. clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
  671. BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
  672. /* If neither side wants MITM, either "just" confirm an incoming
  673. * request or use just-works for outgoing ones. The JUST_CFM
  674. * will be converted to JUST_WORKS if necessary later in this
  675. * function. If either side has MITM look up the method from the
  676. * table.
  677. */
  678. if (!(auth & SMP_AUTH_MITM))
  679. smp->method = JUST_CFM;
  680. else
  681. smp->method = get_auth_method(smp, local_io, remote_io);
  682. /* Don't confirm locally initiated pairing attempts */
  683. if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
  684. &smp->flags))
  685. smp->method = JUST_WORKS;
  686. /* Don't bother user space with no IO capabilities */
  687. if (smp->method == JUST_CFM &&
  688. hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
  689. smp->method = JUST_WORKS;
  690. /* If Just Works, Continue with Zero TK */
  691. if (smp->method == JUST_WORKS) {
  692. set_bit(SMP_FLAG_TK_VALID, &smp->flags);
  693. return 0;
  694. }
  695. /* If this function is used for SC -> legacy fallback we
  696. * can only recover the just-works case.
  697. */
  698. if (test_bit(SMP_FLAG_SC, &smp->flags))
  699. return -EINVAL;
  700. /* Not Just Works/Confirm results in MITM Authentication */
  701. if (smp->method != JUST_CFM) {
  702. set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
  703. if (hcon->pending_sec_level < BT_SECURITY_HIGH)
  704. hcon->pending_sec_level = BT_SECURITY_HIGH;
  705. }
  706. /* If both devices have Keyoard-Display I/O, the master
  707. * Confirms and the slave Enters the passkey.
  708. */
  709. if (smp->method == OVERLAP) {
  710. if (hcon->role == HCI_ROLE_MASTER)
  711. smp->method = CFM_PASSKEY;
  712. else
  713. smp->method = REQ_PASSKEY;
  714. }
  715. /* Generate random passkey. */
  716. if (smp->method == CFM_PASSKEY) {
  717. memset(smp->tk, 0, sizeof(smp->tk));
  718. get_random_bytes(&passkey, sizeof(passkey));
  719. passkey %= 1000000;
  720. put_unaligned_le32(passkey, smp->tk);
  721. BT_DBG("PassKey: %d", passkey);
  722. set_bit(SMP_FLAG_TK_VALID, &smp->flags);
  723. }
  724. if (smp->method == REQ_PASSKEY)
  725. ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
  726. hcon->type, hcon->dst_type);
  727. else if (smp->method == JUST_CFM)
  728. ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
  729. hcon->type, hcon->dst_type,
  730. passkey, 1);
  731. else
  732. ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
  733. hcon->type, hcon->dst_type,
  734. passkey, 0);
  735. return ret;
  736. }
  737. static u8 smp_confirm(struct smp_chan *smp)
  738. {
  739. struct l2cap_conn *conn = smp->conn;
  740. struct smp_cmd_pairing_confirm cp;
  741. int ret;
  742. BT_DBG("conn %p", conn);
  743. ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
  744. conn->hcon->init_addr_type, &conn->hcon->init_addr,
  745. conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
  746. cp.confirm_val);
  747. if (ret)
  748. return SMP_UNSPECIFIED;
  749. clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
  750. smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
  751. if (conn->hcon->out)
  752. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
  753. else
  754. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
  755. return 0;
  756. }
  757. static u8 smp_random(struct smp_chan *smp)
  758. {
  759. struct l2cap_conn *conn = smp->conn;
  760. struct hci_conn *hcon = conn->hcon;
  761. u8 confirm[16];
  762. int ret;
  763. if (IS_ERR_OR_NULL(smp->tfm_aes))
  764. return SMP_UNSPECIFIED;
  765. BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
  766. ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
  767. hcon->init_addr_type, &hcon->init_addr,
  768. hcon->resp_addr_type, &hcon->resp_addr, confirm);
  769. if (ret)
  770. return SMP_UNSPECIFIED;
  771. if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) {
  772. BT_ERR("Pairing failed (confirmation values mismatch)");
  773. return SMP_CONFIRM_FAILED;
  774. }
  775. if (hcon->out) {
  776. u8 stk[16];
  777. __le64 rand = 0;
  778. __le16 ediv = 0;
  779. smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
  780. if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
  781. return SMP_UNSPECIFIED;
  782. hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
  783. hcon->enc_key_size = smp->enc_key_size;
  784. set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
  785. } else {
  786. u8 stk[16], auth;
  787. __le64 rand = 0;
  788. __le16 ediv = 0;
  789. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
  790. smp->prnd);
  791. smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
  792. if (hcon->pending_sec_level == BT_SECURITY_HIGH)
  793. auth = 1;
  794. else
  795. auth = 0;
  796. /* Even though there's no _SLAVE suffix this is the
  797. * slave STK we're adding for later lookup (the master
  798. * STK never needs to be stored).
  799. */
  800. hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
  801. SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
  802. }
  803. return 0;
  804. }
  805. static void smp_notify_keys(struct l2cap_conn *conn)
  806. {
  807. struct l2cap_chan *chan = conn->smp;
  808. struct smp_chan *smp = chan->data;
  809. struct hci_conn *hcon = conn->hcon;
  810. struct hci_dev *hdev = hcon->hdev;
  811. struct smp_cmd_pairing *req = (void *) &smp->preq[1];
  812. struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
  813. bool persistent;
  814. if (hcon->type == ACL_LINK) {
  815. if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
  816. persistent = false;
  817. else
  818. persistent = !test_bit(HCI_CONN_FLUSH_KEY,
  819. &hcon->flags);
  820. } else {
  821. /* The LTKs, IRKs and CSRKs should be persistent only if
  822. * both sides had the bonding bit set in their
  823. * authentication requests.
  824. */
  825. persistent = !!((req->auth_req & rsp->auth_req) &
  826. SMP_AUTH_BONDING);
  827. }
  828. if (smp->remote_irk) {
  829. mgmt_new_irk(hdev, smp->remote_irk, persistent);
  830. /* Now that user space can be considered to know the
  831. * identity address track the connection based on it
  832. * from now on (assuming this is an LE link).
  833. */
  834. if (hcon->type == LE_LINK) {
  835. bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
  836. hcon->dst_type = smp->remote_irk->addr_type;
  837. queue_work(hdev->workqueue, &conn->id_addr_update_work);
  838. }
  839. }
  840. if (smp->csrk) {
  841. smp->csrk->bdaddr_type = hcon->dst_type;
  842. bacpy(&smp->csrk->bdaddr, &hcon->dst);
  843. mgmt_new_csrk(hdev, smp->csrk, persistent);
  844. }
  845. if (smp->slave_csrk) {
  846. smp->slave_csrk->bdaddr_type = hcon->dst_type;
  847. bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
  848. mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
  849. }
  850. if (smp->ltk) {
  851. smp->ltk->bdaddr_type = hcon->dst_type;
  852. bacpy(&smp->ltk->bdaddr, &hcon->dst);
  853. mgmt_new_ltk(hdev, smp->ltk, persistent);
  854. }
  855. if (smp->slave_ltk) {
  856. smp->slave_ltk->bdaddr_type = hcon->dst_type;
  857. bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
  858. mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
  859. }
  860. if (smp->link_key) {
  861. struct link_key *key;
  862. u8 type;
  863. if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
  864. type = HCI_LK_DEBUG_COMBINATION;
  865. else if (hcon->sec_level == BT_SECURITY_FIPS)
  866. type = HCI_LK_AUTH_COMBINATION_P256;
  867. else
  868. type = HCI_LK_UNAUTH_COMBINATION_P256;
  869. key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
  870. smp->link_key, type, 0, &persistent);
  871. if (key) {
  872. mgmt_new_link_key(hdev, key, persistent);
  873. /* Don't keep debug keys around if the relevant
  874. * flag is not set.
  875. */
  876. if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
  877. key->type == HCI_LK_DEBUG_COMBINATION) {
  878. list_del_rcu(&key->list);
  879. kfree_rcu(key, rcu);
  880. }
  881. }
  882. }
  883. }
  884. static void sc_add_ltk(struct smp_chan *smp)
  885. {
  886. struct hci_conn *hcon = smp->conn->hcon;
  887. u8 key_type, auth;
  888. if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
  889. key_type = SMP_LTK_P256_DEBUG;
  890. else
  891. key_type = SMP_LTK_P256;
  892. if (hcon->pending_sec_level == BT_SECURITY_FIPS)
  893. auth = 1;
  894. else
  895. auth = 0;
  896. smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
  897. key_type, auth, smp->tk, smp->enc_key_size,
  898. 0, 0);
  899. }
  900. static void sc_generate_link_key(struct smp_chan *smp)
  901. {
  902. /* These constants are as specified in the core specification.
  903. * In ASCII they spell out to 'tmp1' and 'lebr'.
  904. */
  905. const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
  906. const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
  907. smp->link_key = kzalloc(16, GFP_KERNEL);
  908. if (!smp->link_key)
  909. return;
  910. if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
  911. kzfree(smp->link_key);
  912. smp->link_key = NULL;
  913. return;
  914. }
  915. if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
  916. kzfree(smp->link_key);
  917. smp->link_key = NULL;
  918. return;
  919. }
  920. }
  921. static void smp_allow_key_dist(struct smp_chan *smp)
  922. {
  923. /* Allow the first expected phase 3 PDU. The rest of the PDUs
  924. * will be allowed in each PDU handler to ensure we receive
  925. * them in the correct order.
  926. */
  927. if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
  928. SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
  929. else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
  930. SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
  931. else if (smp->remote_key_dist & SMP_DIST_SIGN)
  932. SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
  933. }
  934. static void sc_generate_ltk(struct smp_chan *smp)
  935. {
  936. /* These constants are as specified in the core specification.
  937. * In ASCII they spell out to 'tmp2' and 'brle'.
  938. */
  939. const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
  940. const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
  941. struct hci_conn *hcon = smp->conn->hcon;
  942. struct hci_dev *hdev = hcon->hdev;
  943. struct link_key *key;
  944. key = hci_find_link_key(hdev, &hcon->dst);
  945. if (!key) {
  946. BT_ERR("%s No Link Key found to generate LTK", hdev->name);
  947. return;
  948. }
  949. if (key->type == HCI_LK_DEBUG_COMBINATION)
  950. set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
  951. if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
  952. return;
  953. if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
  954. return;
  955. sc_add_ltk(smp);
  956. }
  957. static void smp_distribute_keys(struct smp_chan *smp)
  958. {
  959. struct smp_cmd_pairing *req, *rsp;
  960. struct l2cap_conn *conn = smp->conn;
  961. struct hci_conn *hcon = conn->hcon;
  962. struct hci_dev *hdev = hcon->hdev;
  963. __u8 *keydist;
  964. BT_DBG("conn %p", conn);
  965. rsp = (void *) &smp->prsp[1];
  966. /* The responder sends its keys first */
  967. if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
  968. smp_allow_key_dist(smp);
  969. return;
  970. }
  971. req = (void *) &smp->preq[1];
  972. if (hcon->out) {
  973. keydist = &rsp->init_key_dist;
  974. *keydist &= req->init_key_dist;
  975. } else {
  976. keydist = &rsp->resp_key_dist;
  977. *keydist &= req->resp_key_dist;
  978. }
  979. if (test_bit(SMP_FLAG_SC, &smp->flags)) {
  980. if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
  981. sc_generate_link_key(smp);
  982. if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
  983. sc_generate_ltk(smp);
  984. /* Clear the keys which are generated but not distributed */
  985. *keydist &= ~SMP_SC_NO_DIST;
  986. }
  987. BT_DBG("keydist 0x%x", *keydist);
  988. if (*keydist & SMP_DIST_ENC_KEY) {
  989. struct smp_cmd_encrypt_info enc;
  990. struct smp_cmd_master_ident ident;
  991. struct smp_ltk *ltk;
  992. u8 authenticated;
  993. __le16 ediv;
  994. __le64 rand;
  995. /* Make sure we generate only the significant amount of
  996. * bytes based on the encryption key size, and set the rest
  997. * of the value to zeroes.
  998. */
  999. get_random_bytes(enc.ltk, smp->enc_key_size);
  1000. memset(enc.ltk + smp->enc_key_size, 0,
  1001. sizeof(enc.ltk) - smp->enc_key_size);
  1002. get_random_bytes(&ediv, sizeof(ediv));
  1003. get_random_bytes(&rand, sizeof(rand));
  1004. smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
  1005. authenticated = hcon->sec_level == BT_SECURITY_HIGH;
  1006. ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
  1007. SMP_LTK_SLAVE, authenticated, enc.ltk,
  1008. smp->enc_key_size, ediv, rand);
  1009. smp->slave_ltk = ltk;
  1010. ident.ediv = ediv;
  1011. ident.rand = rand;
  1012. smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
  1013. *keydist &= ~SMP_DIST_ENC_KEY;
  1014. }
  1015. if (*keydist & SMP_DIST_ID_KEY) {
  1016. struct smp_cmd_ident_addr_info addrinfo;
  1017. struct smp_cmd_ident_info idinfo;
  1018. memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
  1019. smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
  1020. /* The hci_conn contains the local identity address
  1021. * after the connection has been established.
  1022. *
  1023. * This is true even when the connection has been
  1024. * established using a resolvable random address.
  1025. */
  1026. bacpy(&addrinfo.bdaddr, &hcon->src);
  1027. addrinfo.addr_type = hcon->src_type;
  1028. smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
  1029. &addrinfo);
  1030. *keydist &= ~SMP_DIST_ID_KEY;
  1031. }
  1032. if (*keydist & SMP_DIST_SIGN) {
  1033. struct smp_cmd_sign_info sign;
  1034. struct smp_csrk *csrk;
  1035. /* Generate a new random key */
  1036. get_random_bytes(sign.csrk, sizeof(sign.csrk));
  1037. csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
  1038. if (csrk) {
  1039. if (hcon->sec_level > BT_SECURITY_MEDIUM)
  1040. csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
  1041. else
  1042. csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
  1043. memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
  1044. }
  1045. smp->slave_csrk = csrk;
  1046. smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
  1047. *keydist &= ~SMP_DIST_SIGN;
  1048. }
  1049. /* If there are still keys to be received wait for them */
  1050. if (smp->remote_key_dist & KEY_DIST_MASK) {
  1051. smp_allow_key_dist(smp);
  1052. return;
  1053. }
  1054. set_bit(SMP_FLAG_COMPLETE, &smp->flags);
  1055. smp_notify_keys(conn);
  1056. smp_chan_destroy(conn);
  1057. }
  1058. static void smp_timeout(struct work_struct *work)
  1059. {
  1060. struct smp_chan *smp = container_of(work, struct smp_chan,
  1061. security_timer.work);
  1062. struct l2cap_conn *conn = smp->conn;
  1063. BT_DBG("conn %p", conn);
  1064. hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
  1065. }
  1066. static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
  1067. {
  1068. struct l2cap_chan *chan = conn->smp;
  1069. struct smp_chan *smp;
  1070. smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
  1071. if (!smp)
  1072. return NULL;
  1073. smp->tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  1074. if (IS_ERR(smp->tfm_aes)) {
  1075. BT_ERR("Unable to create AES crypto context");
  1076. kzfree(smp);
  1077. return NULL;
  1078. }
  1079. smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
  1080. if (IS_ERR(smp->tfm_cmac)) {
  1081. BT_ERR("Unable to create CMAC crypto context");
  1082. crypto_free_cipher(smp->tfm_aes);
  1083. kzfree(smp);
  1084. return NULL;
  1085. }
  1086. smp->conn = conn;
  1087. chan->data = smp;
  1088. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
  1089. INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
  1090. hci_conn_hold(conn->hcon);
  1091. return smp;
  1092. }
  1093. static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
  1094. {
  1095. struct hci_conn *hcon = smp->conn->hcon;
  1096. u8 *na, *nb, a[7], b[7];
  1097. if (hcon->out) {
  1098. na = smp->prnd;
  1099. nb = smp->rrnd;
  1100. } else {
  1101. na = smp->rrnd;
  1102. nb = smp->prnd;
  1103. }
  1104. memcpy(a, &hcon->init_addr, 6);
  1105. memcpy(b, &hcon->resp_addr, 6);
  1106. a[6] = hcon->init_addr_type;
  1107. b[6] = hcon->resp_addr_type;
  1108. return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
  1109. }
  1110. static void sc_dhkey_check(struct smp_chan *smp)
  1111. {
  1112. struct hci_conn *hcon = smp->conn->hcon;
  1113. struct smp_cmd_dhkey_check check;
  1114. u8 a[7], b[7], *local_addr, *remote_addr;
  1115. u8 io_cap[3], r[16];
  1116. memcpy(a, &hcon->init_addr, 6);
  1117. memcpy(b, &hcon->resp_addr, 6);
  1118. a[6] = hcon->init_addr_type;
  1119. b[6] = hcon->resp_addr_type;
  1120. if (hcon->out) {
  1121. local_addr = a;
  1122. remote_addr = b;
  1123. memcpy(io_cap, &smp->preq[1], 3);
  1124. } else {
  1125. local_addr = b;
  1126. remote_addr = a;
  1127. memcpy(io_cap, &smp->prsp[1], 3);
  1128. }
  1129. memset(r, 0, sizeof(r));
  1130. if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
  1131. put_unaligned_le32(hcon->passkey_notify, r);
  1132. if (smp->method == REQ_OOB)
  1133. memcpy(r, smp->rr, 16);
  1134. smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
  1135. local_addr, remote_addr, check.e);
  1136. smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
  1137. }
  1138. static u8 sc_passkey_send_confirm(struct smp_chan *smp)
  1139. {
  1140. struct l2cap_conn *conn = smp->conn;
  1141. struct hci_conn *hcon = conn->hcon;
  1142. struct smp_cmd_pairing_confirm cfm;
  1143. u8 r;
  1144. r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
  1145. r |= 0x80;
  1146. get_random_bytes(smp->prnd, sizeof(smp->prnd));
  1147. if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
  1148. cfm.confirm_val))
  1149. return SMP_UNSPECIFIED;
  1150. smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
  1151. return 0;
  1152. }
  1153. static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
  1154. {
  1155. struct l2cap_conn *conn = smp->conn;
  1156. struct hci_conn *hcon = conn->hcon;
  1157. struct hci_dev *hdev = hcon->hdev;
  1158. u8 cfm[16], r;
  1159. /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
  1160. if (smp->passkey_round >= 20)
  1161. return 0;
  1162. switch (smp_op) {
  1163. case SMP_CMD_PAIRING_RANDOM:
  1164. r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
  1165. r |= 0x80;
  1166. if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
  1167. smp->rrnd, r, cfm))
  1168. return SMP_UNSPECIFIED;
  1169. if (crypto_memneq(smp->pcnf, cfm, 16))
  1170. return SMP_CONFIRM_FAILED;
  1171. smp->passkey_round++;
  1172. if (smp->passkey_round == 20) {
  1173. /* Generate MacKey and LTK */
  1174. if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
  1175. return SMP_UNSPECIFIED;
  1176. }
  1177. /* The round is only complete when the initiator
  1178. * receives pairing random.
  1179. */
  1180. if (!hcon->out) {
  1181. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
  1182. sizeof(smp->prnd), smp->prnd);
  1183. if (smp->passkey_round == 20)
  1184. SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
  1185. else
  1186. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
  1187. return 0;
  1188. }
  1189. /* Start the next round */
  1190. if (smp->passkey_round != 20)
  1191. return sc_passkey_round(smp, 0);
  1192. /* Passkey rounds are complete - start DHKey Check */
  1193. sc_dhkey_check(smp);
  1194. SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
  1195. break;
  1196. case SMP_CMD_PAIRING_CONFIRM:
  1197. if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
  1198. set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
  1199. return 0;
  1200. }
  1201. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
  1202. if (hcon->out) {
  1203. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
  1204. sizeof(smp->prnd), smp->prnd);
  1205. return 0;
  1206. }
  1207. return sc_passkey_send_confirm(smp);
  1208. case SMP_CMD_PUBLIC_KEY:
  1209. default:
  1210. /* Initiating device starts the round */
  1211. if (!hcon->out)
  1212. return 0;
  1213. BT_DBG("%s Starting passkey round %u", hdev->name,
  1214. smp->passkey_round + 1);
  1215. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
  1216. return sc_passkey_send_confirm(smp);
  1217. }
  1218. return 0;
  1219. }
  1220. static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
  1221. {
  1222. struct l2cap_conn *conn = smp->conn;
  1223. struct hci_conn *hcon = conn->hcon;
  1224. u8 smp_op;
  1225. clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
  1226. switch (mgmt_op) {
  1227. case MGMT_OP_USER_PASSKEY_NEG_REPLY:
  1228. smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
  1229. return 0;
  1230. case MGMT_OP_USER_CONFIRM_NEG_REPLY:
  1231. smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
  1232. return 0;
  1233. case MGMT_OP_USER_PASSKEY_REPLY:
  1234. hcon->passkey_notify = le32_to_cpu(passkey);
  1235. smp->passkey_round = 0;
  1236. if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
  1237. smp_op = SMP_CMD_PAIRING_CONFIRM;
  1238. else
  1239. smp_op = 0;
  1240. if (sc_passkey_round(smp, smp_op))
  1241. return -EIO;
  1242. return 0;
  1243. }
  1244. /* Initiator sends DHKey check first */
  1245. if (hcon->out) {
  1246. sc_dhkey_check(smp);
  1247. SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
  1248. } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
  1249. sc_dhkey_check(smp);
  1250. sc_add_ltk(smp);
  1251. }
  1252. return 0;
  1253. }
  1254. int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
  1255. {
  1256. struct l2cap_conn *conn = hcon->l2cap_data;
  1257. struct l2cap_chan *chan;
  1258. struct smp_chan *smp;
  1259. u32 value;
  1260. int err;
  1261. BT_DBG("");
  1262. if (!conn)
  1263. return -ENOTCONN;
  1264. chan = conn->smp;
  1265. if (!chan)
  1266. return -ENOTCONN;
  1267. l2cap_chan_lock(chan);
  1268. if (!chan->data) {
  1269. err = -ENOTCONN;
  1270. goto unlock;
  1271. }
  1272. smp = chan->data;
  1273. if (test_bit(SMP_FLAG_SC, &smp->flags)) {
  1274. err = sc_user_reply(smp, mgmt_op, passkey);
  1275. goto unlock;
  1276. }
  1277. switch (mgmt_op) {
  1278. case MGMT_OP_USER_PASSKEY_REPLY:
  1279. value = le32_to_cpu(passkey);
  1280. memset(smp->tk, 0, sizeof(smp->tk));
  1281. BT_DBG("PassKey: %d", value);
  1282. put_unaligned_le32(value, smp->tk);
  1283. /* Fall Through */
  1284. case MGMT_OP_USER_CONFIRM_REPLY:
  1285. set_bit(SMP_FLAG_TK_VALID, &smp->flags);
  1286. break;
  1287. case MGMT_OP_USER_PASSKEY_NEG_REPLY:
  1288. case MGMT_OP_USER_CONFIRM_NEG_REPLY:
  1289. smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
  1290. err = 0;
  1291. goto unlock;
  1292. default:
  1293. smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
  1294. err = -EOPNOTSUPP;
  1295. goto unlock;
  1296. }
  1297. err = 0;
  1298. /* If it is our turn to send Pairing Confirm, do so now */
  1299. if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
  1300. u8 rsp = smp_confirm(smp);
  1301. if (rsp)
  1302. smp_failure(conn, rsp);
  1303. }
  1304. unlock:
  1305. l2cap_chan_unlock(chan);
  1306. return err;
  1307. }
  1308. static void build_bredr_pairing_cmd(struct smp_chan *smp,
  1309. struct smp_cmd_pairing *req,
  1310. struct smp_cmd_pairing *rsp)
  1311. {
  1312. struct l2cap_conn *conn = smp->conn;
  1313. struct hci_dev *hdev = conn->hcon->hdev;
  1314. u8 local_dist = 0, remote_dist = 0;
  1315. if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
  1316. local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
  1317. remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
  1318. }
  1319. if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
  1320. remote_dist |= SMP_DIST_ID_KEY;
  1321. if (hci_dev_test_flag(hdev, HCI_PRIVACY))
  1322. local_dist |= SMP_DIST_ID_KEY;
  1323. if (!rsp) {
  1324. memset(req, 0, sizeof(*req));
  1325. req->init_key_dist = local_dist;
  1326. req->resp_key_dist = remote_dist;
  1327. req->max_key_size = conn->hcon->enc_key_size;
  1328. smp->remote_key_dist = remote_dist;
  1329. return;
  1330. }
  1331. memset(rsp, 0, sizeof(*rsp));
  1332. rsp->max_key_size = conn->hcon->enc_key_size;
  1333. rsp->init_key_dist = req->init_key_dist & remote_dist;
  1334. rsp->resp_key_dist = req->resp_key_dist & local_dist;
  1335. smp->remote_key_dist = rsp->init_key_dist;
  1336. }
  1337. static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
  1338. {
  1339. struct smp_cmd_pairing rsp, *req = (void *) skb->data;
  1340. struct l2cap_chan *chan = conn->smp;
  1341. struct hci_dev *hdev = conn->hcon->hdev;
  1342. struct smp_chan *smp;
  1343. u8 key_size, auth, sec_level;
  1344. int ret;
  1345. BT_DBG("conn %p", conn);
  1346. if (skb->len < sizeof(*req))
  1347. return SMP_INVALID_PARAMS;
  1348. if (conn->hcon->role != HCI_ROLE_SLAVE)
  1349. return SMP_CMD_NOTSUPP;
  1350. if (!chan->data)
  1351. smp = smp_chan_create(conn);
  1352. else
  1353. smp = chan->data;
  1354. if (!smp)
  1355. return SMP_UNSPECIFIED;
  1356. /* We didn't start the pairing, so match remote */
  1357. auth = req->auth_req & AUTH_REQ_MASK(hdev);
  1358. if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
  1359. (auth & SMP_AUTH_BONDING))
  1360. return SMP_PAIRING_NOTSUPP;
  1361. if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
  1362. return SMP_AUTH_REQUIREMENTS;
  1363. smp->preq[0] = SMP_CMD_PAIRING_REQ;
  1364. memcpy(&smp->preq[1], req, sizeof(*req));
  1365. skb_pull(skb, sizeof(*req));
  1366. /* If the remote side's OOB flag is set it means it has
  1367. * successfully received our local OOB data - therefore set the
  1368. * flag to indicate that local OOB is in use.
  1369. */
  1370. if (req->oob_flag == SMP_OOB_PRESENT)
  1371. set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
  1372. /* SMP over BR/EDR requires special treatment */
  1373. if (conn->hcon->type == ACL_LINK) {
  1374. /* We must have a BR/EDR SC link */
  1375. if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
  1376. !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
  1377. return SMP_CROSS_TRANSP_NOT_ALLOWED;
  1378. set_bit(SMP_FLAG_SC, &smp->flags);
  1379. build_bredr_pairing_cmd(smp, req, &rsp);
  1380. key_size = min(req->max_key_size, rsp.max_key_size);
  1381. if (check_enc_key_size(conn, key_size))
  1382. return SMP_ENC_KEY_SIZE;
  1383. /* Clear bits which are generated but not distributed */
  1384. smp->remote_key_dist &= ~SMP_SC_NO_DIST;
  1385. smp->prsp[0] = SMP_CMD_PAIRING_RSP;
  1386. memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
  1387. smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
  1388. smp_distribute_keys(smp);
  1389. return 0;
  1390. }
  1391. build_pairing_cmd(conn, req, &rsp, auth);
  1392. if (rsp.auth_req & SMP_AUTH_SC)
  1393. set_bit(SMP_FLAG_SC, &smp->flags);
  1394. if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
  1395. sec_level = BT_SECURITY_MEDIUM;
  1396. else
  1397. sec_level = authreq_to_seclevel(auth);
  1398. if (sec_level > conn->hcon->pending_sec_level)
  1399. conn->hcon->pending_sec_level = sec_level;
  1400. /* If we need MITM check that it can be achieved */
  1401. if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
  1402. u8 method;
  1403. method = get_auth_method(smp, conn->hcon->io_capability,
  1404. req->io_capability);
  1405. if (method == JUST_WORKS || method == JUST_CFM)
  1406. return SMP_AUTH_REQUIREMENTS;
  1407. }
  1408. key_size = min(req->max_key_size, rsp.max_key_size);
  1409. if (check_enc_key_size(conn, key_size))
  1410. return SMP_ENC_KEY_SIZE;
  1411. get_random_bytes(smp->prnd, sizeof(smp->prnd));
  1412. smp->prsp[0] = SMP_CMD_PAIRING_RSP;
  1413. memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
  1414. smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
  1415. clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
  1416. /* Strictly speaking we shouldn't allow Pairing Confirm for the
  1417. * SC case, however some implementations incorrectly copy RFU auth
  1418. * req bits from our security request, which may create a false
  1419. * positive SC enablement.
  1420. */
  1421. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
  1422. if (test_bit(SMP_FLAG_SC, &smp->flags)) {
  1423. SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
  1424. /* Clear bits which are generated but not distributed */
  1425. smp->remote_key_dist &= ~SMP_SC_NO_DIST;
  1426. /* Wait for Public Key from Initiating Device */
  1427. return 0;
  1428. }
  1429. /* Request setup of TK */
  1430. ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
  1431. if (ret)
  1432. return SMP_UNSPECIFIED;
  1433. return 0;
  1434. }
  1435. static u8 sc_send_public_key(struct smp_chan *smp)
  1436. {
  1437. struct hci_dev *hdev = smp->conn->hcon->hdev;
  1438. BT_DBG("");
  1439. if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
  1440. struct l2cap_chan *chan = hdev->smp_data;
  1441. struct smp_dev *smp_dev;
  1442. if (!chan || !chan->data)
  1443. return SMP_UNSPECIFIED;
  1444. smp_dev = chan->data;
  1445. memcpy(smp->local_pk, smp_dev->local_pk, 64);
  1446. memcpy(smp->local_sk, smp_dev->local_sk, 32);
  1447. memcpy(smp->lr, smp_dev->local_rand, 16);
  1448. if (smp_dev->debug_key)
  1449. set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
  1450. goto done;
  1451. }
  1452. if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
  1453. BT_DBG("Using debug keys");
  1454. memcpy(smp->local_pk, debug_pk, 64);
  1455. memcpy(smp->local_sk, debug_sk, 32);
  1456. set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
  1457. } else {
  1458. while (true) {
  1459. /* Generate local key pair for Secure Connections */
  1460. if (!ecc_make_key(smp->local_pk, smp->local_sk))
  1461. return SMP_UNSPECIFIED;
  1462. /* This is unlikely, but we need to check that
  1463. * we didn't accidentially generate a debug key.
  1464. */
  1465. if (crypto_memneq(smp->local_sk, debug_sk, 32))
  1466. break;
  1467. }
  1468. }
  1469. done:
  1470. SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
  1471. SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
  1472. SMP_DBG("Local Private Key: %32phN", smp->local_sk);
  1473. smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
  1474. return 0;
  1475. }
  1476. static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
  1477. {
  1478. struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
  1479. struct l2cap_chan *chan = conn->smp;
  1480. struct smp_chan *smp = chan->data;
  1481. struct hci_dev *hdev = conn->hcon->hdev;
  1482. u8 key_size, auth;
  1483. int ret;
  1484. BT_DBG("conn %p", conn);
  1485. if (skb->len < sizeof(*rsp))
  1486. return SMP_INVALID_PARAMS;
  1487. if (conn->hcon->role != HCI_ROLE_MASTER)
  1488. return SMP_CMD_NOTSUPP;
  1489. skb_pull(skb, sizeof(*rsp));
  1490. req = (void *) &smp->preq[1];
  1491. key_size = min(req->max_key_size, rsp->max_key_size);
  1492. if (check_enc_key_size(conn, key_size))
  1493. return SMP_ENC_KEY_SIZE;
  1494. auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
  1495. if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
  1496. return SMP_AUTH_REQUIREMENTS;
  1497. /* If the remote side's OOB flag is set it means it has
  1498. * successfully received our local OOB data - therefore set the
  1499. * flag to indicate that local OOB is in use.
  1500. */
  1501. if (rsp->oob_flag == SMP_OOB_PRESENT)
  1502. set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
  1503. smp->prsp[0] = SMP_CMD_PAIRING_RSP;
  1504. memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
  1505. /* Update remote key distribution in case the remote cleared
  1506. * some bits that we had enabled in our request.
  1507. */
  1508. smp->remote_key_dist &= rsp->resp_key_dist;
  1509. /* For BR/EDR this means we're done and can start phase 3 */
  1510. if (conn->hcon->type == ACL_LINK) {
  1511. /* Clear bits which are generated but not distributed */
  1512. smp->remote_key_dist &= ~SMP_SC_NO_DIST;
  1513. smp_distribute_keys(smp);
  1514. return 0;
  1515. }
  1516. if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
  1517. set_bit(SMP_FLAG_SC, &smp->flags);
  1518. else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
  1519. conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
  1520. /* If we need MITM check that it can be achieved */
  1521. if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
  1522. u8 method;
  1523. method = get_auth_method(smp, req->io_capability,
  1524. rsp->io_capability);
  1525. if (method == JUST_WORKS || method == JUST_CFM)
  1526. return SMP_AUTH_REQUIREMENTS;
  1527. }
  1528. get_random_bytes(smp->prnd, sizeof(smp->prnd));
  1529. /* Update remote key distribution in case the remote cleared
  1530. * some bits that we had enabled in our request.
  1531. */
  1532. smp->remote_key_dist &= rsp->resp_key_dist;
  1533. if (test_bit(SMP_FLAG_SC, &smp->flags)) {
  1534. /* Clear bits which are generated but not distributed */
  1535. smp->remote_key_dist &= ~SMP_SC_NO_DIST;
  1536. SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
  1537. return sc_send_public_key(smp);
  1538. }
  1539. auth |= req->auth_req;
  1540. ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
  1541. if (ret)
  1542. return SMP_UNSPECIFIED;
  1543. set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
  1544. /* Can't compose response until we have been confirmed */
  1545. if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
  1546. return smp_confirm(smp);
  1547. return 0;
  1548. }
  1549. static u8 sc_check_confirm(struct smp_chan *smp)
  1550. {
  1551. struct l2cap_conn *conn = smp->conn;
  1552. BT_DBG("");
  1553. if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
  1554. return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
  1555. if (conn->hcon->out) {
  1556. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
  1557. smp->prnd);
  1558. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
  1559. }
  1560. return 0;
  1561. }
  1562. /* Work-around for some implementations that incorrectly copy RFU bits
  1563. * from our security request and thereby create the impression that
  1564. * we're doing SC when in fact the remote doesn't support it.
  1565. */
  1566. static int fixup_sc_false_positive(struct smp_chan *smp)
  1567. {
  1568. struct l2cap_conn *conn = smp->conn;
  1569. struct hci_conn *hcon = conn->hcon;
  1570. struct hci_dev *hdev = hcon->hdev;
  1571. struct smp_cmd_pairing *req, *rsp;
  1572. u8 auth;
  1573. /* The issue is only observed when we're in slave role */
  1574. if (hcon->out)
  1575. return SMP_UNSPECIFIED;
  1576. if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
  1577. BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
  1578. return SMP_UNSPECIFIED;
  1579. }
  1580. BT_ERR("Trying to fall back to legacy SMP");
  1581. req = (void *) &smp->preq[1];
  1582. rsp = (void *) &smp->prsp[1];
  1583. /* Rebuild key dist flags which may have been cleared for SC */
  1584. smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
  1585. auth = req->auth_req & AUTH_REQ_MASK(hdev);
  1586. if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
  1587. BT_ERR("Failed to fall back to legacy SMP");
  1588. return SMP_UNSPECIFIED;
  1589. }
  1590. clear_bit(SMP_FLAG_SC, &smp->flags);
  1591. return 0;
  1592. }
  1593. static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
  1594. {
  1595. struct l2cap_chan *chan = conn->smp;
  1596. struct smp_chan *smp = chan->data;
  1597. BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
  1598. if (skb->len < sizeof(smp->pcnf))
  1599. return SMP_INVALID_PARAMS;
  1600. memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
  1601. skb_pull(skb, sizeof(smp->pcnf));
  1602. if (test_bit(SMP_FLAG_SC, &smp->flags)) {
  1603. int ret;
  1604. /* Public Key exchange must happen before any other steps */
  1605. if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
  1606. return sc_check_confirm(smp);
  1607. BT_ERR("Unexpected SMP Pairing Confirm");
  1608. ret = fixup_sc_false_positive(smp);
  1609. if (ret)
  1610. return ret;
  1611. }
  1612. if (conn->hcon->out) {
  1613. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
  1614. smp->prnd);
  1615. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
  1616. return 0;
  1617. }
  1618. if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
  1619. return smp_confirm(smp);
  1620. set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
  1621. return 0;
  1622. }
  1623. static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
  1624. {
  1625. struct l2cap_chan *chan = conn->smp;
  1626. struct smp_chan *smp = chan->data;
  1627. struct hci_conn *hcon = conn->hcon;
  1628. u8 *pkax, *pkbx, *na, *nb;
  1629. u32 passkey;
  1630. int err;
  1631. BT_DBG("conn %p", conn);
  1632. if (skb->len < sizeof(smp->rrnd))
  1633. return SMP_INVALID_PARAMS;
  1634. memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
  1635. skb_pull(skb, sizeof(smp->rrnd));
  1636. if (!test_bit(SMP_FLAG_SC, &smp->flags))
  1637. return smp_random(smp);
  1638. if (hcon->out) {
  1639. pkax = smp->local_pk;
  1640. pkbx = smp->remote_pk;
  1641. na = smp->prnd;
  1642. nb = smp->rrnd;
  1643. } else {
  1644. pkax = smp->remote_pk;
  1645. pkbx = smp->local_pk;
  1646. na = smp->rrnd;
  1647. nb = smp->prnd;
  1648. }
  1649. if (smp->method == REQ_OOB) {
  1650. if (!hcon->out)
  1651. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
  1652. sizeof(smp->prnd), smp->prnd);
  1653. SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
  1654. goto mackey_and_ltk;
  1655. }
  1656. /* Passkey entry has special treatment */
  1657. if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
  1658. return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
  1659. if (hcon->out) {
  1660. u8 cfm[16];
  1661. err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
  1662. smp->rrnd, 0, cfm);
  1663. if (err)
  1664. return SMP_UNSPECIFIED;
  1665. if (crypto_memneq(smp->pcnf, cfm, 16))
  1666. return SMP_CONFIRM_FAILED;
  1667. } else {
  1668. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
  1669. smp->prnd);
  1670. SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
  1671. }
  1672. mackey_and_ltk:
  1673. /* Generate MacKey and LTK */
  1674. err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
  1675. if (err)
  1676. return SMP_UNSPECIFIED;
  1677. if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
  1678. if (hcon->out) {
  1679. sc_dhkey_check(smp);
  1680. SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
  1681. }
  1682. return 0;
  1683. }
  1684. err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
  1685. if (err)
  1686. return SMP_UNSPECIFIED;
  1687. err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
  1688. hcon->dst_type, passkey, 0);
  1689. if (err)
  1690. return SMP_UNSPECIFIED;
  1691. set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
  1692. return 0;
  1693. }
  1694. static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
  1695. {
  1696. struct smp_ltk *key;
  1697. struct hci_conn *hcon = conn->hcon;
  1698. key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
  1699. if (!key)
  1700. return false;
  1701. if (smp_ltk_sec_level(key) < sec_level)
  1702. return false;
  1703. if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
  1704. return true;
  1705. hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
  1706. hcon->enc_key_size = key->enc_size;
  1707. /* We never store STKs for master role, so clear this flag */
  1708. clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
  1709. return true;
  1710. }
  1711. bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
  1712. enum smp_key_pref key_pref)
  1713. {
  1714. if (sec_level == BT_SECURITY_LOW)
  1715. return true;
  1716. /* If we're encrypted with an STK but the caller prefers using
  1717. * LTK claim insufficient security. This way we allow the
  1718. * connection to be re-encrypted with an LTK, even if the LTK
  1719. * provides the same level of security. Only exception is if we
  1720. * don't have an LTK (e.g. because of key distribution bits).
  1721. */
  1722. if (key_pref == SMP_USE_LTK &&
  1723. test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
  1724. hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
  1725. return false;
  1726. if (hcon->sec_level >= sec_level)
  1727. return true;
  1728. return false;
  1729. }
  1730. static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
  1731. {
  1732. struct smp_cmd_security_req *rp = (void *) skb->data;
  1733. struct smp_cmd_pairing cp;
  1734. struct hci_conn *hcon = conn->hcon;
  1735. struct hci_dev *hdev = hcon->hdev;
  1736. struct smp_chan *smp;
  1737. u8 sec_level, auth;
  1738. BT_DBG("conn %p", conn);
  1739. if (skb->len < sizeof(*rp))
  1740. return SMP_INVALID_PARAMS;
  1741. if (hcon->role != HCI_ROLE_MASTER)
  1742. return SMP_CMD_NOTSUPP;
  1743. auth = rp->auth_req & AUTH_REQ_MASK(hdev);
  1744. if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
  1745. return SMP_AUTH_REQUIREMENTS;
  1746. if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
  1747. sec_level = BT_SECURITY_MEDIUM;
  1748. else
  1749. sec_level = authreq_to_seclevel(auth);
  1750. if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) {
  1751. /* If link is already encrypted with sufficient security we
  1752. * still need refresh encryption as per Core Spec 5.0 Vol 3,
  1753. * Part H 2.4.6
  1754. */
  1755. smp_ltk_encrypt(conn, hcon->sec_level);
  1756. return 0;
  1757. }
  1758. if (sec_level > hcon->pending_sec_level)
  1759. hcon->pending_sec_level = sec_level;
  1760. if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
  1761. return 0;
  1762. smp = smp_chan_create(conn);
  1763. if (!smp)
  1764. return SMP_UNSPECIFIED;
  1765. if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
  1766. (auth & SMP_AUTH_BONDING))
  1767. return SMP_PAIRING_NOTSUPP;
  1768. skb_pull(skb, sizeof(*rp));
  1769. memset(&cp, 0, sizeof(cp));
  1770. build_pairing_cmd(conn, &cp, NULL, auth);
  1771. smp->preq[0] = SMP_CMD_PAIRING_REQ;
  1772. memcpy(&smp->preq[1], &cp, sizeof(cp));
  1773. smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
  1774. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
  1775. return 0;
  1776. }
  1777. int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
  1778. {
  1779. struct l2cap_conn *conn = hcon->l2cap_data;
  1780. struct l2cap_chan *chan;
  1781. struct smp_chan *smp;
  1782. __u8 authreq;
  1783. int ret;
  1784. BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
  1785. /* This may be NULL if there's an unexpected disconnection */
  1786. if (!conn)
  1787. return 1;
  1788. if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
  1789. return 1;
  1790. if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
  1791. return 1;
  1792. if (sec_level > hcon->pending_sec_level)
  1793. hcon->pending_sec_level = sec_level;
  1794. if (hcon->role == HCI_ROLE_MASTER)
  1795. if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
  1796. return 0;
  1797. chan = conn->smp;
  1798. if (!chan) {
  1799. BT_ERR("SMP security requested but not available");
  1800. return 1;
  1801. }
  1802. l2cap_chan_lock(chan);
  1803. /* If SMP is already in progress ignore this request */
  1804. if (chan->data) {
  1805. ret = 0;
  1806. goto unlock;
  1807. }
  1808. smp = smp_chan_create(conn);
  1809. if (!smp) {
  1810. ret = 1;
  1811. goto unlock;
  1812. }
  1813. authreq = seclevel_to_authreq(sec_level);
  1814. if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
  1815. authreq |= SMP_AUTH_SC;
  1816. /* Require MITM if IO Capability allows or the security level
  1817. * requires it.
  1818. */
  1819. if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
  1820. hcon->pending_sec_level > BT_SECURITY_MEDIUM)
  1821. authreq |= SMP_AUTH_MITM;
  1822. if (hcon->role == HCI_ROLE_MASTER) {
  1823. struct smp_cmd_pairing cp;
  1824. build_pairing_cmd(conn, &cp, NULL, authreq);
  1825. smp->preq[0] = SMP_CMD_PAIRING_REQ;
  1826. memcpy(&smp->preq[1], &cp, sizeof(cp));
  1827. smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
  1828. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
  1829. } else {
  1830. struct smp_cmd_security_req cp;
  1831. cp.auth_req = authreq;
  1832. smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
  1833. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
  1834. }
  1835. set_bit(SMP_FLAG_INITIATOR, &smp->flags);
  1836. ret = 0;
  1837. unlock:
  1838. l2cap_chan_unlock(chan);
  1839. return ret;
  1840. }
  1841. void smp_cancel_pairing(struct hci_conn *hcon)
  1842. {
  1843. struct l2cap_conn *conn = hcon->l2cap_data;
  1844. struct l2cap_chan *chan;
  1845. struct smp_chan *smp;
  1846. if (!conn)
  1847. return;
  1848. chan = conn->smp;
  1849. if (!chan)
  1850. return;
  1851. l2cap_chan_lock(chan);
  1852. smp = chan->data;
  1853. if (smp) {
  1854. if (test_bit(SMP_FLAG_COMPLETE, &smp->flags))
  1855. smp_failure(conn, 0);
  1856. else
  1857. smp_failure(conn, SMP_UNSPECIFIED);
  1858. }
  1859. l2cap_chan_unlock(chan);
  1860. }
  1861. static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
  1862. {
  1863. struct smp_cmd_encrypt_info *rp = (void *) skb->data;
  1864. struct l2cap_chan *chan = conn->smp;
  1865. struct smp_chan *smp = chan->data;
  1866. BT_DBG("conn %p", conn);
  1867. if (skb->len < sizeof(*rp))
  1868. return SMP_INVALID_PARAMS;
  1869. SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
  1870. skb_pull(skb, sizeof(*rp));
  1871. memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
  1872. return 0;
  1873. }
  1874. static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
  1875. {
  1876. struct smp_cmd_master_ident *rp = (void *) skb->data;
  1877. struct l2cap_chan *chan = conn->smp;
  1878. struct smp_chan *smp = chan->data;
  1879. struct hci_dev *hdev = conn->hcon->hdev;
  1880. struct hci_conn *hcon = conn->hcon;
  1881. struct smp_ltk *ltk;
  1882. u8 authenticated;
  1883. BT_DBG("conn %p", conn);
  1884. if (skb->len < sizeof(*rp))
  1885. return SMP_INVALID_PARAMS;
  1886. /* Mark the information as received */
  1887. smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
  1888. if (smp->remote_key_dist & SMP_DIST_ID_KEY)
  1889. SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
  1890. else if (smp->remote_key_dist & SMP_DIST_SIGN)
  1891. SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
  1892. skb_pull(skb, sizeof(*rp));
  1893. authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
  1894. ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
  1895. authenticated, smp->tk, smp->enc_key_size,
  1896. rp->ediv, rp->rand);
  1897. smp->ltk = ltk;
  1898. if (!(smp->remote_key_dist & KEY_DIST_MASK))
  1899. smp_distribute_keys(smp);
  1900. return 0;
  1901. }
  1902. static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
  1903. {
  1904. struct smp_cmd_ident_info *info = (void *) skb->data;
  1905. struct l2cap_chan *chan = conn->smp;
  1906. struct smp_chan *smp = chan->data;
  1907. BT_DBG("");
  1908. if (skb->len < sizeof(*info))
  1909. return SMP_INVALID_PARAMS;
  1910. SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
  1911. skb_pull(skb, sizeof(*info));
  1912. memcpy(smp->irk, info->irk, 16);
  1913. return 0;
  1914. }
  1915. static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
  1916. struct sk_buff *skb)
  1917. {
  1918. struct smp_cmd_ident_addr_info *info = (void *) skb->data;
  1919. struct l2cap_chan *chan = conn->smp;
  1920. struct smp_chan *smp = chan->data;
  1921. struct hci_conn *hcon = conn->hcon;
  1922. bdaddr_t rpa;
  1923. BT_DBG("");
  1924. if (skb->len < sizeof(*info))
  1925. return SMP_INVALID_PARAMS;
  1926. /* Mark the information as received */
  1927. smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
  1928. if (smp->remote_key_dist & SMP_DIST_SIGN)
  1929. SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
  1930. skb_pull(skb, sizeof(*info));
  1931. /* Strictly speaking the Core Specification (4.1) allows sending
  1932. * an empty address which would force us to rely on just the IRK
  1933. * as "identity information". However, since such
  1934. * implementations are not known of and in order to not over
  1935. * complicate our implementation, simply pretend that we never
  1936. * received an IRK for such a device.
  1937. *
  1938. * The Identity Address must also be a Static Random or Public
  1939. * Address, which hci_is_identity_address() checks for.
  1940. */
  1941. if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
  1942. !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
  1943. BT_ERR("Ignoring IRK with no identity address");
  1944. goto distribute;
  1945. }
  1946. bacpy(&smp->id_addr, &info->bdaddr);
  1947. smp->id_addr_type = info->addr_type;
  1948. if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
  1949. bacpy(&rpa, &hcon->dst);
  1950. else
  1951. bacpy(&rpa, BDADDR_ANY);
  1952. smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
  1953. smp->id_addr_type, smp->irk, &rpa);
  1954. distribute:
  1955. if (!(smp->remote_key_dist & KEY_DIST_MASK))
  1956. smp_distribute_keys(smp);
  1957. return 0;
  1958. }
  1959. static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
  1960. {
  1961. struct smp_cmd_sign_info *rp = (void *) skb->data;
  1962. struct l2cap_chan *chan = conn->smp;
  1963. struct smp_chan *smp = chan->data;
  1964. struct smp_csrk *csrk;
  1965. BT_DBG("conn %p", conn);
  1966. if (skb->len < sizeof(*rp))
  1967. return SMP_INVALID_PARAMS;
  1968. /* Mark the information as received */
  1969. smp->remote_key_dist &= ~SMP_DIST_SIGN;
  1970. skb_pull(skb, sizeof(*rp));
  1971. csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
  1972. if (csrk) {
  1973. if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
  1974. csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
  1975. else
  1976. csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
  1977. memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
  1978. }
  1979. smp->csrk = csrk;
  1980. smp_distribute_keys(smp);
  1981. return 0;
  1982. }
  1983. static u8 sc_select_method(struct smp_chan *smp)
  1984. {
  1985. struct l2cap_conn *conn = smp->conn;
  1986. struct hci_conn *hcon = conn->hcon;
  1987. struct smp_cmd_pairing *local, *remote;
  1988. u8 local_mitm, remote_mitm, local_io, remote_io, method;
  1989. if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
  1990. test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
  1991. return REQ_OOB;
  1992. /* The preq/prsp contain the raw Pairing Request/Response PDUs
  1993. * which are needed as inputs to some crypto functions. To get
  1994. * the "struct smp_cmd_pairing" from them we need to skip the
  1995. * first byte which contains the opcode.
  1996. */
  1997. if (hcon->out) {
  1998. local = (void *) &smp->preq[1];
  1999. remote = (void *) &smp->prsp[1];
  2000. } else {
  2001. local = (void *) &smp->prsp[1];
  2002. remote = (void *) &smp->preq[1];
  2003. }
  2004. local_io = local->io_capability;
  2005. remote_io = remote->io_capability;
  2006. local_mitm = (local->auth_req & SMP_AUTH_MITM);
  2007. remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
  2008. /* If either side wants MITM, look up the method from the table,
  2009. * otherwise use JUST WORKS.
  2010. */
  2011. if (local_mitm || remote_mitm)
  2012. method = get_auth_method(smp, local_io, remote_io);
  2013. else
  2014. method = JUST_WORKS;
  2015. /* Don't confirm locally initiated pairing attempts */
  2016. if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
  2017. method = JUST_WORKS;
  2018. return method;
  2019. }
  2020. static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
  2021. {
  2022. struct smp_cmd_public_key *key = (void *) skb->data;
  2023. struct hci_conn *hcon = conn->hcon;
  2024. struct l2cap_chan *chan = conn->smp;
  2025. struct smp_chan *smp = chan->data;
  2026. struct hci_dev *hdev = hcon->hdev;
  2027. struct smp_cmd_pairing_confirm cfm;
  2028. int err;
  2029. BT_DBG("conn %p", conn);
  2030. if (skb->len < sizeof(*key))
  2031. return SMP_INVALID_PARAMS;
  2032. memcpy(smp->remote_pk, key, 64);
  2033. if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
  2034. err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
  2035. smp->rr, 0, cfm.confirm_val);
  2036. if (err)
  2037. return SMP_UNSPECIFIED;
  2038. if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16))
  2039. return SMP_CONFIRM_FAILED;
  2040. }
  2041. /* Non-initiating device sends its public key after receiving
  2042. * the key from the initiating device.
  2043. */
  2044. if (!hcon->out) {
  2045. err = sc_send_public_key(smp);
  2046. if (err)
  2047. return err;
  2048. }
  2049. SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
  2050. SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
  2051. if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
  2052. return SMP_UNSPECIFIED;
  2053. SMP_DBG("DHKey %32phN", smp->dhkey);
  2054. set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
  2055. smp->method = sc_select_method(smp);
  2056. BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
  2057. /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
  2058. if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
  2059. hcon->pending_sec_level = BT_SECURITY_MEDIUM;
  2060. else
  2061. hcon->pending_sec_level = BT_SECURITY_FIPS;
  2062. if (!crypto_memneq(debug_pk, smp->remote_pk, 64))
  2063. set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
  2064. if (smp->method == DSP_PASSKEY) {
  2065. get_random_bytes(&hcon->passkey_notify,
  2066. sizeof(hcon->passkey_notify));
  2067. hcon->passkey_notify %= 1000000;
  2068. hcon->passkey_entered = 0;
  2069. smp->passkey_round = 0;
  2070. if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
  2071. hcon->dst_type,
  2072. hcon->passkey_notify,
  2073. hcon->passkey_entered))
  2074. return SMP_UNSPECIFIED;
  2075. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
  2076. return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
  2077. }
  2078. if (smp->method == REQ_OOB) {
  2079. if (hcon->out)
  2080. smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
  2081. sizeof(smp->prnd), smp->prnd);
  2082. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
  2083. return 0;
  2084. }
  2085. if (hcon->out)
  2086. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
  2087. if (smp->method == REQ_PASSKEY) {
  2088. if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
  2089. hcon->dst_type))
  2090. return SMP_UNSPECIFIED;
  2091. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
  2092. set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
  2093. return 0;
  2094. }
  2095. /* The Initiating device waits for the non-initiating device to
  2096. * send the confirm value.
  2097. */
  2098. if (conn->hcon->out)
  2099. return 0;
  2100. err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
  2101. 0, cfm.confirm_val);
  2102. if (err)
  2103. return SMP_UNSPECIFIED;
  2104. smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
  2105. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
  2106. return 0;
  2107. }
  2108. static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
  2109. {
  2110. struct smp_cmd_dhkey_check *check = (void *) skb->data;
  2111. struct l2cap_chan *chan = conn->smp;
  2112. struct hci_conn *hcon = conn->hcon;
  2113. struct smp_chan *smp = chan->data;
  2114. u8 a[7], b[7], *local_addr, *remote_addr;
  2115. u8 io_cap[3], r[16], e[16];
  2116. int err;
  2117. BT_DBG("conn %p", conn);
  2118. if (skb->len < sizeof(*check))
  2119. return SMP_INVALID_PARAMS;
  2120. memcpy(a, &hcon->init_addr, 6);
  2121. memcpy(b, &hcon->resp_addr, 6);
  2122. a[6] = hcon->init_addr_type;
  2123. b[6] = hcon->resp_addr_type;
  2124. if (hcon->out) {
  2125. local_addr = a;
  2126. remote_addr = b;
  2127. memcpy(io_cap, &smp->prsp[1], 3);
  2128. } else {
  2129. local_addr = b;
  2130. remote_addr = a;
  2131. memcpy(io_cap, &smp->preq[1], 3);
  2132. }
  2133. memset(r, 0, sizeof(r));
  2134. if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
  2135. put_unaligned_le32(hcon->passkey_notify, r);
  2136. else if (smp->method == REQ_OOB)
  2137. memcpy(r, smp->lr, 16);
  2138. err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
  2139. io_cap, remote_addr, local_addr, e);
  2140. if (err)
  2141. return SMP_UNSPECIFIED;
  2142. if (crypto_memneq(check->e, e, 16))
  2143. return SMP_DHKEY_CHECK_FAILED;
  2144. if (!hcon->out) {
  2145. if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
  2146. set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
  2147. return 0;
  2148. }
  2149. /* Slave sends DHKey check as response to master */
  2150. sc_dhkey_check(smp);
  2151. }
  2152. sc_add_ltk(smp);
  2153. if (hcon->out) {
  2154. hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
  2155. hcon->enc_key_size = smp->enc_key_size;
  2156. }
  2157. return 0;
  2158. }
  2159. static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
  2160. struct sk_buff *skb)
  2161. {
  2162. struct smp_cmd_keypress_notify *kp = (void *) skb->data;
  2163. BT_DBG("value 0x%02x", kp->value);
  2164. return 0;
  2165. }
  2166. static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
  2167. {
  2168. struct l2cap_conn *conn = chan->conn;
  2169. struct hci_conn *hcon = conn->hcon;
  2170. struct smp_chan *smp;
  2171. __u8 code, reason;
  2172. int err = 0;
  2173. if (skb->len < 1)
  2174. return -EILSEQ;
  2175. if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
  2176. reason = SMP_PAIRING_NOTSUPP;
  2177. goto done;
  2178. }
  2179. code = skb->data[0];
  2180. skb_pull(skb, sizeof(code));
  2181. smp = chan->data;
  2182. if (code > SMP_CMD_MAX)
  2183. goto drop;
  2184. if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
  2185. goto drop;
  2186. /* If we don't have a context the only allowed commands are
  2187. * pairing request and security request.
  2188. */
  2189. if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
  2190. goto drop;
  2191. switch (code) {
  2192. case SMP_CMD_PAIRING_REQ:
  2193. reason = smp_cmd_pairing_req(conn, skb);
  2194. break;
  2195. case SMP_CMD_PAIRING_FAIL:
  2196. smp_failure(conn, 0);
  2197. err = -EPERM;
  2198. break;
  2199. case SMP_CMD_PAIRING_RSP:
  2200. reason = smp_cmd_pairing_rsp(conn, skb);
  2201. break;
  2202. case SMP_CMD_SECURITY_REQ:
  2203. reason = smp_cmd_security_req(conn, skb);
  2204. break;
  2205. case SMP_CMD_PAIRING_CONFIRM:
  2206. reason = smp_cmd_pairing_confirm(conn, skb);
  2207. break;
  2208. case SMP_CMD_PAIRING_RANDOM:
  2209. reason = smp_cmd_pairing_random(conn, skb);
  2210. break;
  2211. case SMP_CMD_ENCRYPT_INFO:
  2212. reason = smp_cmd_encrypt_info(conn, skb);
  2213. break;
  2214. case SMP_CMD_MASTER_IDENT:
  2215. reason = smp_cmd_master_ident(conn, skb);
  2216. break;
  2217. case SMP_CMD_IDENT_INFO:
  2218. reason = smp_cmd_ident_info(conn, skb);
  2219. break;
  2220. case SMP_CMD_IDENT_ADDR_INFO:
  2221. reason = smp_cmd_ident_addr_info(conn, skb);
  2222. break;
  2223. case SMP_CMD_SIGN_INFO:
  2224. reason = smp_cmd_sign_info(conn, skb);
  2225. break;
  2226. case SMP_CMD_PUBLIC_KEY:
  2227. reason = smp_cmd_public_key(conn, skb);
  2228. break;
  2229. case SMP_CMD_DHKEY_CHECK:
  2230. reason = smp_cmd_dhkey_check(conn, skb);
  2231. break;
  2232. case SMP_CMD_KEYPRESS_NOTIFY:
  2233. reason = smp_cmd_keypress_notify(conn, skb);
  2234. break;
  2235. default:
  2236. BT_DBG("Unknown command code 0x%2.2x", code);
  2237. reason = SMP_CMD_NOTSUPP;
  2238. goto done;
  2239. }
  2240. done:
  2241. if (!err) {
  2242. if (reason)
  2243. smp_failure(conn, reason);
  2244. kfree_skb(skb);
  2245. }
  2246. return err;
  2247. drop:
  2248. BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
  2249. code, &hcon->dst);
  2250. kfree_skb(skb);
  2251. return 0;
  2252. }
  2253. static void smp_teardown_cb(struct l2cap_chan *chan, int err)
  2254. {
  2255. struct l2cap_conn *conn = chan->conn;
  2256. BT_DBG("chan %p", chan);
  2257. if (chan->data)
  2258. smp_chan_destroy(conn);
  2259. conn->smp = NULL;
  2260. l2cap_chan_put(chan);
  2261. }
  2262. static void bredr_pairing(struct l2cap_chan *chan)
  2263. {
  2264. struct l2cap_conn *conn = chan->conn;
  2265. struct hci_conn *hcon = conn->hcon;
  2266. struct hci_dev *hdev = hcon->hdev;
  2267. struct smp_cmd_pairing req;
  2268. struct smp_chan *smp;
  2269. BT_DBG("chan %p", chan);
  2270. /* Only new pairings are interesting */
  2271. if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
  2272. return;
  2273. /* Don't bother if we're not encrypted */
  2274. if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
  2275. return;
  2276. /* Only master may initiate SMP over BR/EDR */
  2277. if (hcon->role != HCI_ROLE_MASTER)
  2278. return;
  2279. /* Secure Connections support must be enabled */
  2280. if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
  2281. return;
  2282. /* BR/EDR must use Secure Connections for SMP */
  2283. if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
  2284. !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
  2285. return;
  2286. /* If our LE support is not enabled don't do anything */
  2287. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  2288. return;
  2289. /* Don't bother if remote LE support is not enabled */
  2290. if (!lmp_host_le_capable(hcon))
  2291. return;
  2292. /* Remote must support SMP fixed chan for BR/EDR */
  2293. if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
  2294. return;
  2295. /* Don't bother if SMP is already ongoing */
  2296. if (chan->data)
  2297. return;
  2298. smp = smp_chan_create(conn);
  2299. if (!smp) {
  2300. BT_ERR("%s unable to create SMP context for BR/EDR",
  2301. hdev->name);
  2302. return;
  2303. }
  2304. set_bit(SMP_FLAG_SC, &smp->flags);
  2305. BT_DBG("%s starting SMP over BR/EDR", hdev->name);
  2306. /* Prepare and send the BR/EDR SMP Pairing Request */
  2307. build_bredr_pairing_cmd(smp, &req, NULL);
  2308. smp->preq[0] = SMP_CMD_PAIRING_REQ;
  2309. memcpy(&smp->preq[1], &req, sizeof(req));
  2310. smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
  2311. SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
  2312. }
  2313. static void smp_resume_cb(struct l2cap_chan *chan)
  2314. {
  2315. struct smp_chan *smp = chan->data;
  2316. struct l2cap_conn *conn = chan->conn;
  2317. struct hci_conn *hcon = conn->hcon;
  2318. BT_DBG("chan %p", chan);
  2319. if (hcon->type == ACL_LINK) {
  2320. bredr_pairing(chan);
  2321. return;
  2322. }
  2323. if (!smp)
  2324. return;
  2325. if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
  2326. return;
  2327. cancel_delayed_work(&smp->security_timer);
  2328. smp_distribute_keys(smp);
  2329. }
  2330. static void smp_ready_cb(struct l2cap_chan *chan)
  2331. {
  2332. struct l2cap_conn *conn = chan->conn;
  2333. struct hci_conn *hcon = conn->hcon;
  2334. BT_DBG("chan %p", chan);
  2335. /* No need to call l2cap_chan_hold() here since we already own
  2336. * the reference taken in smp_new_conn_cb(). This is just the
  2337. * first time that we tie it to a specific pointer. The code in
  2338. * l2cap_core.c ensures that there's no risk this function wont
  2339. * get called if smp_new_conn_cb was previously called.
  2340. */
  2341. conn->smp = chan;
  2342. if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
  2343. bredr_pairing(chan);
  2344. }
  2345. static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
  2346. {
  2347. int err;
  2348. BT_DBG("chan %p", chan);
  2349. err = smp_sig_channel(chan, skb);
  2350. if (err) {
  2351. struct smp_chan *smp = chan->data;
  2352. if (smp)
  2353. cancel_delayed_work_sync(&smp->security_timer);
  2354. hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
  2355. }
  2356. return err;
  2357. }
  2358. static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
  2359. unsigned long hdr_len,
  2360. unsigned long len, int nb)
  2361. {
  2362. struct sk_buff *skb;
  2363. skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
  2364. if (!skb)
  2365. return ERR_PTR(-ENOMEM);
  2366. skb->priority = HCI_PRIO_MAX;
  2367. bt_cb(skb)->l2cap.chan = chan;
  2368. return skb;
  2369. }
  2370. static const struct l2cap_ops smp_chan_ops = {
  2371. .name = "Security Manager",
  2372. .ready = smp_ready_cb,
  2373. .recv = smp_recv_cb,
  2374. .alloc_skb = smp_alloc_skb_cb,
  2375. .teardown = smp_teardown_cb,
  2376. .resume = smp_resume_cb,
  2377. .new_connection = l2cap_chan_no_new_connection,
  2378. .state_change = l2cap_chan_no_state_change,
  2379. .close = l2cap_chan_no_close,
  2380. .defer = l2cap_chan_no_defer,
  2381. .suspend = l2cap_chan_no_suspend,
  2382. .set_shutdown = l2cap_chan_no_set_shutdown,
  2383. .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
  2384. };
  2385. static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
  2386. {
  2387. struct l2cap_chan *chan;
  2388. BT_DBG("pchan %p", pchan);
  2389. chan = l2cap_chan_create();
  2390. if (!chan)
  2391. return NULL;
  2392. chan->chan_type = pchan->chan_type;
  2393. chan->ops = &smp_chan_ops;
  2394. chan->scid = pchan->scid;
  2395. chan->dcid = chan->scid;
  2396. chan->imtu = pchan->imtu;
  2397. chan->omtu = pchan->omtu;
  2398. chan->mode = pchan->mode;
  2399. /* Other L2CAP channels may request SMP routines in order to
  2400. * change the security level. This means that the SMP channel
  2401. * lock must be considered in its own category to avoid lockdep
  2402. * warnings.
  2403. */
  2404. atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
  2405. BT_DBG("created chan %p", chan);
  2406. return chan;
  2407. }
  2408. static const struct l2cap_ops smp_root_chan_ops = {
  2409. .name = "Security Manager Root",
  2410. .new_connection = smp_new_conn_cb,
  2411. /* None of these are implemented for the root channel */
  2412. .close = l2cap_chan_no_close,
  2413. .alloc_skb = l2cap_chan_no_alloc_skb,
  2414. .recv = l2cap_chan_no_recv,
  2415. .state_change = l2cap_chan_no_state_change,
  2416. .teardown = l2cap_chan_no_teardown,
  2417. .ready = l2cap_chan_no_ready,
  2418. .defer = l2cap_chan_no_defer,
  2419. .suspend = l2cap_chan_no_suspend,
  2420. .resume = l2cap_chan_no_resume,
  2421. .set_shutdown = l2cap_chan_no_set_shutdown,
  2422. .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
  2423. };
  2424. static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
  2425. {
  2426. struct l2cap_chan *chan;
  2427. struct smp_dev *smp;
  2428. struct crypto_cipher *tfm_aes;
  2429. struct crypto_shash *tfm_cmac;
  2430. if (cid == L2CAP_CID_SMP_BREDR) {
  2431. smp = NULL;
  2432. goto create_chan;
  2433. }
  2434. smp = kzalloc(sizeof(*smp), GFP_KERNEL);
  2435. if (!smp)
  2436. return ERR_PTR(-ENOMEM);
  2437. tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  2438. if (IS_ERR(tfm_aes)) {
  2439. BT_ERR("Unable to create AES crypto context");
  2440. kzfree(smp);
  2441. return ERR_CAST(tfm_aes);
  2442. }
  2443. tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
  2444. if (IS_ERR(tfm_cmac)) {
  2445. BT_ERR("Unable to create CMAC crypto context");
  2446. crypto_free_cipher(tfm_aes);
  2447. kzfree(smp);
  2448. return ERR_CAST(tfm_cmac);
  2449. }
  2450. smp->tfm_aes = tfm_aes;
  2451. smp->tfm_cmac = tfm_cmac;
  2452. smp->min_key_size = SMP_MIN_ENC_KEY_SIZE;
  2453. smp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
  2454. create_chan:
  2455. chan = l2cap_chan_create();
  2456. if (!chan) {
  2457. if (smp) {
  2458. crypto_free_cipher(smp->tfm_aes);
  2459. crypto_free_shash(smp->tfm_cmac);
  2460. kzfree(smp);
  2461. }
  2462. return ERR_PTR(-ENOMEM);
  2463. }
  2464. chan->data = smp;
  2465. l2cap_add_scid(chan, cid);
  2466. l2cap_chan_set_defaults(chan);
  2467. if (cid == L2CAP_CID_SMP) {
  2468. u8 bdaddr_type;
  2469. hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
  2470. if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
  2471. chan->src_type = BDADDR_LE_PUBLIC;
  2472. else
  2473. chan->src_type = BDADDR_LE_RANDOM;
  2474. } else {
  2475. bacpy(&chan->src, &hdev->bdaddr);
  2476. chan->src_type = BDADDR_BREDR;
  2477. }
  2478. chan->state = BT_LISTEN;
  2479. chan->mode = L2CAP_MODE_BASIC;
  2480. chan->imtu = L2CAP_DEFAULT_MTU;
  2481. chan->ops = &smp_root_chan_ops;
  2482. /* Set correct nesting level for a parent/listening channel */
  2483. atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
  2484. return chan;
  2485. }
  2486. static void smp_del_chan(struct l2cap_chan *chan)
  2487. {
  2488. struct smp_dev *smp;
  2489. BT_DBG("chan %p", chan);
  2490. smp = chan->data;
  2491. if (smp) {
  2492. chan->data = NULL;
  2493. crypto_free_cipher(smp->tfm_aes);
  2494. crypto_free_shash(smp->tfm_cmac);
  2495. kzfree(smp);
  2496. }
  2497. l2cap_chan_put(chan);
  2498. }
  2499. static ssize_t force_bredr_smp_read(struct file *file,
  2500. char __user *user_buf,
  2501. size_t count, loff_t *ppos)
  2502. {
  2503. struct hci_dev *hdev = file->private_data;
  2504. char buf[3];
  2505. buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
  2506. buf[1] = '\n';
  2507. buf[2] = '\0';
  2508. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  2509. }
  2510. static ssize_t force_bredr_smp_write(struct file *file,
  2511. const char __user *user_buf,
  2512. size_t count, loff_t *ppos)
  2513. {
  2514. struct hci_dev *hdev = file->private_data;
  2515. char buf[32];
  2516. size_t buf_size = min(count, (sizeof(buf)-1));
  2517. bool enable;
  2518. if (copy_from_user(buf, user_buf, buf_size))
  2519. return -EFAULT;
  2520. buf[buf_size] = '\0';
  2521. if (strtobool(buf, &enable))
  2522. return -EINVAL;
  2523. if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
  2524. return -EALREADY;
  2525. if (enable) {
  2526. struct l2cap_chan *chan;
  2527. chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
  2528. if (IS_ERR(chan))
  2529. return PTR_ERR(chan);
  2530. hdev->smp_bredr_data = chan;
  2531. } else {
  2532. struct l2cap_chan *chan;
  2533. chan = hdev->smp_bredr_data;
  2534. hdev->smp_bredr_data = NULL;
  2535. smp_del_chan(chan);
  2536. }
  2537. hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
  2538. return count;
  2539. }
  2540. static const struct file_operations force_bredr_smp_fops = {
  2541. .open = simple_open,
  2542. .read = force_bredr_smp_read,
  2543. .write = force_bredr_smp_write,
  2544. .llseek = default_llseek,
  2545. };
  2546. static ssize_t le_min_key_size_read(struct file *file,
  2547. char __user *user_buf,
  2548. size_t count, loff_t *ppos)
  2549. {
  2550. struct hci_dev *hdev = file->private_data;
  2551. char buf[4];
  2552. snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->min_key_size);
  2553. return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
  2554. }
  2555. static ssize_t le_min_key_size_write(struct file *file,
  2556. const char __user *user_buf,
  2557. size_t count, loff_t *ppos)
  2558. {
  2559. struct hci_dev *hdev = file->private_data;
  2560. char buf[32];
  2561. size_t buf_size = min(count, (sizeof(buf) - 1));
  2562. u8 key_size;
  2563. if (copy_from_user(buf, user_buf, buf_size))
  2564. return -EFAULT;
  2565. buf[buf_size] = '\0';
  2566. sscanf(buf, "%hhu", &key_size);
  2567. if (key_size > SMP_DEV(hdev)->max_key_size ||
  2568. key_size < SMP_MIN_ENC_KEY_SIZE)
  2569. return -EINVAL;
  2570. SMP_DEV(hdev)->min_key_size = key_size;
  2571. return count;
  2572. }
  2573. static const struct file_operations le_min_key_size_fops = {
  2574. .open = simple_open,
  2575. .read = le_min_key_size_read,
  2576. .write = le_min_key_size_write,
  2577. .llseek = default_llseek,
  2578. };
  2579. static ssize_t le_max_key_size_read(struct file *file,
  2580. char __user *user_buf,
  2581. size_t count, loff_t *ppos)
  2582. {
  2583. struct hci_dev *hdev = file->private_data;
  2584. char buf[4];
  2585. snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->max_key_size);
  2586. return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
  2587. }
  2588. static ssize_t le_max_key_size_write(struct file *file,
  2589. const char __user *user_buf,
  2590. size_t count, loff_t *ppos)
  2591. {
  2592. struct hci_dev *hdev = file->private_data;
  2593. char buf[32];
  2594. size_t buf_size = min(count, (sizeof(buf) - 1));
  2595. u8 key_size;
  2596. if (copy_from_user(buf, user_buf, buf_size))
  2597. return -EFAULT;
  2598. buf[buf_size] = '\0';
  2599. sscanf(buf, "%hhu", &key_size);
  2600. if (key_size > SMP_MAX_ENC_KEY_SIZE ||
  2601. key_size < SMP_DEV(hdev)->min_key_size)
  2602. return -EINVAL;
  2603. SMP_DEV(hdev)->max_key_size = key_size;
  2604. return count;
  2605. }
  2606. static const struct file_operations le_max_key_size_fops = {
  2607. .open = simple_open,
  2608. .read = le_max_key_size_read,
  2609. .write = le_max_key_size_write,
  2610. .llseek = default_llseek,
  2611. };
  2612. int smp_register(struct hci_dev *hdev)
  2613. {
  2614. struct l2cap_chan *chan;
  2615. BT_DBG("%s", hdev->name);
  2616. /* If the controller does not support Low Energy operation, then
  2617. * there is also no need to register any SMP channel.
  2618. */
  2619. if (!lmp_le_capable(hdev))
  2620. return 0;
  2621. if (WARN_ON(hdev->smp_data)) {
  2622. chan = hdev->smp_data;
  2623. hdev->smp_data = NULL;
  2624. smp_del_chan(chan);
  2625. }
  2626. chan = smp_add_cid(hdev, L2CAP_CID_SMP);
  2627. if (IS_ERR(chan))
  2628. return PTR_ERR(chan);
  2629. hdev->smp_data = chan;
  2630. debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
  2631. &le_min_key_size_fops);
  2632. debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
  2633. &le_max_key_size_fops);
  2634. /* If the controller does not support BR/EDR Secure Connections
  2635. * feature, then the BR/EDR SMP channel shall not be present.
  2636. *
  2637. * To test this with Bluetooth 4.0 controllers, create a debugfs
  2638. * switch that allows forcing BR/EDR SMP support and accepting
  2639. * cross-transport pairing on non-AES encrypted connections.
  2640. */
  2641. if (!lmp_sc_capable(hdev)) {
  2642. debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
  2643. hdev, &force_bredr_smp_fops);
  2644. /* Flag can be already set here (due to power toggle) */
  2645. if (!hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
  2646. return 0;
  2647. }
  2648. if (WARN_ON(hdev->smp_bredr_data)) {
  2649. chan = hdev->smp_bredr_data;
  2650. hdev->smp_bredr_data = NULL;
  2651. smp_del_chan(chan);
  2652. }
  2653. chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
  2654. if (IS_ERR(chan)) {
  2655. int err = PTR_ERR(chan);
  2656. chan = hdev->smp_data;
  2657. hdev->smp_data = NULL;
  2658. smp_del_chan(chan);
  2659. return err;
  2660. }
  2661. hdev->smp_bredr_data = chan;
  2662. return 0;
  2663. }
  2664. void smp_unregister(struct hci_dev *hdev)
  2665. {
  2666. struct l2cap_chan *chan;
  2667. if (hdev->smp_bredr_data) {
  2668. chan = hdev->smp_bredr_data;
  2669. hdev->smp_bredr_data = NULL;
  2670. smp_del_chan(chan);
  2671. }
  2672. if (hdev->smp_data) {
  2673. chan = hdev->smp_data;
  2674. hdev->smp_data = NULL;
  2675. smp_del_chan(chan);
  2676. }
  2677. }
  2678. #if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
  2679. static int __init test_ah(struct crypto_cipher *tfm_aes)
  2680. {
  2681. const u8 irk[16] = {
  2682. 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
  2683. 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
  2684. const u8 r[3] = { 0x94, 0x81, 0x70 };
  2685. const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
  2686. u8 res[3];
  2687. int err;
  2688. err = smp_ah(tfm_aes, irk, r, res);
  2689. if (err)
  2690. return err;
  2691. if (crypto_memneq(res, exp, 3))
  2692. return -EINVAL;
  2693. return 0;
  2694. }
  2695. static int __init test_c1(struct crypto_cipher *tfm_aes)
  2696. {
  2697. const u8 k[16] = {
  2698. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2699. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  2700. const u8 r[16] = {
  2701. 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
  2702. 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
  2703. const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
  2704. const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
  2705. const u8 _iat = 0x01;
  2706. const u8 _rat = 0x00;
  2707. const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
  2708. const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
  2709. const u8 exp[16] = {
  2710. 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
  2711. 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
  2712. u8 res[16];
  2713. int err;
  2714. err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
  2715. if (err)
  2716. return err;
  2717. if (crypto_memneq(res, exp, 16))
  2718. return -EINVAL;
  2719. return 0;
  2720. }
  2721. static int __init test_s1(struct crypto_cipher *tfm_aes)
  2722. {
  2723. const u8 k[16] = {
  2724. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2725. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  2726. const u8 r1[16] = {
  2727. 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
  2728. const u8 r2[16] = {
  2729. 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
  2730. const u8 exp[16] = {
  2731. 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
  2732. 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
  2733. u8 res[16];
  2734. int err;
  2735. err = smp_s1(tfm_aes, k, r1, r2, res);
  2736. if (err)
  2737. return err;
  2738. if (crypto_memneq(res, exp, 16))
  2739. return -EINVAL;
  2740. return 0;
  2741. }
  2742. static int __init test_f4(struct crypto_shash *tfm_cmac)
  2743. {
  2744. const u8 u[32] = {
  2745. 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
  2746. 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
  2747. 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
  2748. 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
  2749. const u8 v[32] = {
  2750. 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
  2751. 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
  2752. 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
  2753. 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
  2754. const u8 x[16] = {
  2755. 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
  2756. 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
  2757. const u8 z = 0x00;
  2758. const u8 exp[16] = {
  2759. 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
  2760. 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
  2761. u8 res[16];
  2762. int err;
  2763. err = smp_f4(tfm_cmac, u, v, x, z, res);
  2764. if (err)
  2765. return err;
  2766. if (crypto_memneq(res, exp, 16))
  2767. return -EINVAL;
  2768. return 0;
  2769. }
  2770. static int __init test_f5(struct crypto_shash *tfm_cmac)
  2771. {
  2772. const u8 w[32] = {
  2773. 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
  2774. 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
  2775. 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
  2776. 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
  2777. const u8 n1[16] = {
  2778. 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
  2779. 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
  2780. const u8 n2[16] = {
  2781. 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
  2782. 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
  2783. const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
  2784. const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
  2785. const u8 exp_ltk[16] = {
  2786. 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
  2787. 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
  2788. const u8 exp_mackey[16] = {
  2789. 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
  2790. 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
  2791. u8 mackey[16], ltk[16];
  2792. int err;
  2793. err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
  2794. if (err)
  2795. return err;
  2796. if (crypto_memneq(mackey, exp_mackey, 16))
  2797. return -EINVAL;
  2798. if (crypto_memneq(ltk, exp_ltk, 16))
  2799. return -EINVAL;
  2800. return 0;
  2801. }
  2802. static int __init test_f6(struct crypto_shash *tfm_cmac)
  2803. {
  2804. const u8 w[16] = {
  2805. 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
  2806. 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
  2807. const u8 n1[16] = {
  2808. 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
  2809. 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
  2810. const u8 n2[16] = {
  2811. 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
  2812. 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
  2813. const u8 r[16] = {
  2814. 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
  2815. 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
  2816. const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
  2817. const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
  2818. const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
  2819. const u8 exp[16] = {
  2820. 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
  2821. 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
  2822. u8 res[16];
  2823. int err;
  2824. err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
  2825. if (err)
  2826. return err;
  2827. if (crypto_memneq(res, exp, 16))
  2828. return -EINVAL;
  2829. return 0;
  2830. }
  2831. static int __init test_g2(struct crypto_shash *tfm_cmac)
  2832. {
  2833. const u8 u[32] = {
  2834. 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
  2835. 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
  2836. 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
  2837. 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
  2838. const u8 v[32] = {
  2839. 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
  2840. 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
  2841. 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
  2842. 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
  2843. const u8 x[16] = {
  2844. 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
  2845. 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
  2846. const u8 y[16] = {
  2847. 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
  2848. 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
  2849. const u32 exp_val = 0x2f9ed5ba % 1000000;
  2850. u32 val;
  2851. int err;
  2852. err = smp_g2(tfm_cmac, u, v, x, y, &val);
  2853. if (err)
  2854. return err;
  2855. if (val != exp_val)
  2856. return -EINVAL;
  2857. return 0;
  2858. }
  2859. static int __init test_h6(struct crypto_shash *tfm_cmac)
  2860. {
  2861. const u8 w[16] = {
  2862. 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
  2863. 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
  2864. const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
  2865. const u8 exp[16] = {
  2866. 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
  2867. 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
  2868. u8 res[16];
  2869. int err;
  2870. err = smp_h6(tfm_cmac, w, key_id, res);
  2871. if (err)
  2872. return err;
  2873. if (crypto_memneq(res, exp, 16))
  2874. return -EINVAL;
  2875. return 0;
  2876. }
  2877. static char test_smp_buffer[32];
  2878. static ssize_t test_smp_read(struct file *file, char __user *user_buf,
  2879. size_t count, loff_t *ppos)
  2880. {
  2881. return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
  2882. strlen(test_smp_buffer));
  2883. }
  2884. static const struct file_operations test_smp_fops = {
  2885. .open = simple_open,
  2886. .read = test_smp_read,
  2887. .llseek = default_llseek,
  2888. };
  2889. static int __init run_selftests(struct crypto_cipher *tfm_aes,
  2890. struct crypto_shash *tfm_cmac)
  2891. {
  2892. ktime_t calltime, delta, rettime;
  2893. unsigned long long duration;
  2894. int err;
  2895. calltime = ktime_get();
  2896. err = test_ah(tfm_aes);
  2897. if (err) {
  2898. BT_ERR("smp_ah test failed");
  2899. goto done;
  2900. }
  2901. err = test_c1(tfm_aes);
  2902. if (err) {
  2903. BT_ERR("smp_c1 test failed");
  2904. goto done;
  2905. }
  2906. err = test_s1(tfm_aes);
  2907. if (err) {
  2908. BT_ERR("smp_s1 test failed");
  2909. goto done;
  2910. }
  2911. err = test_f4(tfm_cmac);
  2912. if (err) {
  2913. BT_ERR("smp_f4 test failed");
  2914. goto done;
  2915. }
  2916. err = test_f5(tfm_cmac);
  2917. if (err) {
  2918. BT_ERR("smp_f5 test failed");
  2919. goto done;
  2920. }
  2921. err = test_f6(tfm_cmac);
  2922. if (err) {
  2923. BT_ERR("smp_f6 test failed");
  2924. goto done;
  2925. }
  2926. err = test_g2(tfm_cmac);
  2927. if (err) {
  2928. BT_ERR("smp_g2 test failed");
  2929. goto done;
  2930. }
  2931. err = test_h6(tfm_cmac);
  2932. if (err) {
  2933. BT_ERR("smp_h6 test failed");
  2934. goto done;
  2935. }
  2936. rettime = ktime_get();
  2937. delta = ktime_sub(rettime, calltime);
  2938. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  2939. BT_INFO("SMP test passed in %llu usecs", duration);
  2940. done:
  2941. if (!err)
  2942. snprintf(test_smp_buffer, sizeof(test_smp_buffer),
  2943. "PASS (%llu usecs)\n", duration);
  2944. else
  2945. snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
  2946. debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
  2947. &test_smp_fops);
  2948. return err;
  2949. }
  2950. int __init bt_selftest_smp(void)
  2951. {
  2952. struct crypto_cipher *tfm_aes;
  2953. struct crypto_shash *tfm_cmac;
  2954. int err;
  2955. tfm_aes = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
  2956. if (IS_ERR(tfm_aes)) {
  2957. BT_ERR("Unable to create AES crypto context");
  2958. return PTR_ERR(tfm_aes);
  2959. }
  2960. tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
  2961. if (IS_ERR(tfm_cmac)) {
  2962. BT_ERR("Unable to create CMAC crypto context");
  2963. crypto_free_cipher(tfm_aes);
  2964. return PTR_ERR(tfm_cmac);
  2965. }
  2966. err = run_selftests(tfm_aes, tfm_cmac);
  2967. crypto_free_shash(tfm_cmac);
  2968. crypto_free_cipher(tfm_aes);
  2969. return err;
  2970. }
  2971. #endif