smp.c 83 KB

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