smp.c 93 KB

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