blob: e68c715f8d371d5b14e272f6b8de1b8f6cf28664 [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
Marcel Holtmann300acfde2014-12-31 14:43:16 -080023#include <linux/debugfs.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030024#include <linux/scatterlist.h>
Andy Lutomirskia4770e12016-06-26 14:55:23 -070025#include <linux/crypto.h>
Jason A. Donenfeld329d8232017-06-10 04:59:11 +020026#include <crypto/algapi.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030027#include <crypto/b128ops.h>
Herbert Xu71af2f62016-01-24 21:18:30 +080028#include <crypto/hash.h>
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +030029#include <crypto/kpp.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030030
Anderson Brigliaeb492e02011-06-09 18:50:40 -030031#include <net/bluetooth/bluetooth.h>
32#include <net/bluetooth/hci_core.h>
33#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080034#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010036#include "ecdh_helper.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070037#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030038
Johan Hedberg2fd36552015-06-11 13:52:26 +030039#define SMP_DEV(hdev) \
40 ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data)
41
Johan Hedbergc7a3d572014-12-01 22:03:16 +020042/* Low-level debug macros to be used for stuff that we don't want
43 * accidentially in dmesg, i.e. the values of the various crypto keys
44 * and the inputs & outputs of crypto functions.
45 */
46#ifdef DEBUG
47#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
48 ##__VA_ARGS__)
49#else
50#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
51 ##__VA_ARGS__)
52#endif
53
Johan Hedbergb28b4942014-09-05 22:19:55 +030054#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030055
Johan Hedberg3b191462014-06-06 10:50:15 +030056/* Keys which are not distributed with Secure Connections */
57#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
58
Marcel Holtmann17b02e62012-03-01 14:32:37 -080059#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030060
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070061#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberga62da6f2016-12-08 08:32:54 +020062 0x3f : 0x07)
Johan Hedberg0edb14d2014-05-26 13:29:28 +030063#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020064
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030065/* Maximum message length that can be passed to aes_cmac */
66#define CMAC_MSG_MAX 80
67
Johan Hedberg533e35d2014-06-16 19:25:18 +030068enum {
69 SMP_FLAG_TK_VALID,
70 SMP_FLAG_CFM_PENDING,
71 SMP_FLAG_MITM_AUTH,
72 SMP_FLAG_COMPLETE,
73 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030074 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030075 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030076 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030077 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030078 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg1a8bab42015-03-16 11:45:44 +020079 SMP_FLAG_REMOTE_OOB,
80 SMP_FLAG_LOCAL_OOB,
Johan Hedberga62da6f2016-12-08 08:32:54 +020081 SMP_FLAG_CT2,
Johan Hedberg533e35d2014-06-16 19:25:18 +030082};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030083
Marcel Holtmann88a479d2015-03-16 01:10:19 -070084struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070085 /* Secure Connections OOB data */
Johan Hedberg94f14e42018-09-11 14:10:12 +030086 bool local_oob;
Marcel Holtmann60a27d62015-03-16 01:10:22 -070087 u8 local_pk[64];
Marcel Holtmannfb334fe2015-03-16 12:34:57 -070088 u8 local_rand[16];
Marcel Holtmann60a27d62015-03-16 01:10:22 -070089 bool debug_key;
90
Andy Lutomirskia4770e12016-06-26 14:55:23 -070091 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +080092 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +030093 struct crypto_kpp *tfm_ecdh;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070094};
95
Johan Hedberg4bc58f52014-05-20 09:45:47 +030096struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030097 struct l2cap_conn *conn;
98 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030099 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +0300100
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300101 u8 preq[7]; /* SMP Pairing Request */
102 u8 prsp[7]; /* SMP Pairing Response */
103 u8 prnd[16]; /* SMP Pairing Random (local) */
104 u8 rrnd[16]; /* SMP Pairing Random (remote) */
105 u8 pcnf[16]; /* SMP Pairing Confirm */
106 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +0200107 u8 rr[16]; /* Remote OOB ra/rb value */
108 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300109 u8 enc_key_size;
110 u8 remote_key_dist;
111 bdaddr_t id_addr;
112 u8 id_addr_type;
113 u8 irk[16];
114 struct smp_csrk *csrk;
115 struct smp_csrk *slave_csrk;
116 struct smp_ltk *ltk;
117 struct smp_ltk *slave_ltk;
118 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300119 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300120 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300121 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300122 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300123
Johan Hedberg3b191462014-06-06 10:50:15 +0300124 /* Secure Connections variables */
125 u8 local_pk[64];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300126 u8 remote_pk[64];
127 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300128 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300129
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700130 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +0800131 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +0300132 struct crypto_kpp *tfm_ecdh;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300133};
134
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300135/* These debug key values are defined in the SMP section of the core
136 * specification. debug_pk is the public debug key and debug_sk the
137 * private debug key.
138 */
139static const u8 debug_pk[64] = {
140 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
141 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
142 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
143 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
144
145 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
146 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
147 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
148 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
149};
150
151static const u8 debug_sk[32] = {
152 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
153 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
154 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
155 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
156};
157
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300158static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300159{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300160 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300161
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300162 for (i = 0; i < len; i++)
163 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300164}
165
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200166/* The following functions map to the LE SC SMP crypto functions
167 * AES-CMAC, f4, f5, f6, g2 and h6.
168 */
169
Herbert Xu71af2f62016-01-24 21:18:30 +0800170static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m,
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300171 size_t len, u8 mac[16])
172{
173 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
Herbert Xu71af2f62016-01-24 21:18:30 +0800174 SHASH_DESC_ON_STACK(desc, tfm);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300175 int err;
176
177 if (len > CMAC_MSG_MAX)
178 return -EFBIG;
179
180 if (!tfm) {
181 BT_ERR("tfm %p", tfm);
182 return -EINVAL;
183 }
184
Herbert Xu71af2f62016-01-24 21:18:30 +0800185 desc->tfm = tfm;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300186
187 /* Swap key and message from LSB to MSB */
188 swap_buf(k, tmp, 16);
189 swap_buf(m, msg_msb, len);
190
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200191 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
192 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300193
Herbert Xu71af2f62016-01-24 21:18:30 +0800194 err = crypto_shash_setkey(tfm, tmp, 16);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300195 if (err) {
196 BT_ERR("cipher setkey failed: %d", err);
197 return err;
198 }
199
Herbert Xu71af2f62016-01-24 21:18:30 +0800200 err = crypto_shash_digest(desc, msg_msb, len, mac_msb);
201 shash_desc_zero(desc);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300202 if (err) {
Herbert Xu71af2f62016-01-24 21:18:30 +0800203 BT_ERR("Hash computation error %d", err);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300204 return err;
205 }
206
207 swap_buf(mac_msb, mac, 16);
208
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200209 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300210
211 return 0;
212}
213
Herbert Xu71af2f62016-01-24 21:18:30 +0800214static int smp_f4(struct crypto_shash *tfm_cmac, const u8 u[32],
215 const u8 v[32], const u8 x[16], u8 z, u8 res[16])
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300216{
217 u8 m[65];
218 int err;
219
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200220 SMP_DBG("u %32phN", u);
221 SMP_DBG("v %32phN", v);
222 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300223
224 m[0] = z;
225 memcpy(m + 1, v, 32);
226 memcpy(m + 33, u, 32);
227
228 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
229 if (err)
230 return err;
231
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200232 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300233
234 return err;
235}
236
Herbert Xu71af2f62016-01-24 21:18:30 +0800237static int smp_f5(struct crypto_shash *tfm_cmac, const u8 w[32],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200238 const u8 n1[16], const u8 n2[16], const u8 a1[7],
239 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300240{
241 /* The btle, salt and length "magic" values are as defined in
242 * the SMP section of the Bluetooth core specification. In ASCII
243 * the btle value ends up being 'btle'. The salt is just a
244 * random number whereas length is the value 256 in little
245 * endian format.
246 */
247 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
248 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
249 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
250 const u8 length[2] = { 0x00, 0x01 };
251 u8 m[53], t[16];
252 int err;
253
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200254 SMP_DBG("w %32phN", w);
255 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
256 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300257
258 err = aes_cmac(tfm_cmac, salt, w, 32, t);
259 if (err)
260 return err;
261
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200262 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300263
264 memcpy(m, length, 2);
265 memcpy(m + 2, a2, 7);
266 memcpy(m + 9, a1, 7);
267 memcpy(m + 16, n2, 16);
268 memcpy(m + 32, n1, 16);
269 memcpy(m + 48, btle, 4);
270
271 m[52] = 0; /* Counter */
272
273 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
274 if (err)
275 return err;
276
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200277 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300278
279 m[52] = 1; /* Counter */
280
281 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
282 if (err)
283 return err;
284
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200285 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300286
287 return 0;
288}
289
Herbert Xu71af2f62016-01-24 21:18:30 +0800290static int smp_f6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200291 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300292 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
293 u8 res[16])
294{
295 u8 m[65];
296 int err;
297
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200298 SMP_DBG("w %16phN", w);
299 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
300 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300301
302 memcpy(m, a2, 7);
303 memcpy(m + 7, a1, 7);
304 memcpy(m + 14, io_cap, 3);
305 memcpy(m + 17, r, 16);
306 memcpy(m + 33, n2, 16);
307 memcpy(m + 49, n1, 16);
308
309 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
310 if (err)
311 return err;
312
Marcel Holtmann203de212014-12-31 20:01:22 -0800313 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300314
315 return err;
316}
317
Herbert Xu71af2f62016-01-24 21:18:30 +0800318static int smp_g2(struct crypto_shash *tfm_cmac, const u8 u[32], const u8 v[32],
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300319 const u8 x[16], const u8 y[16], u32 *val)
320{
321 u8 m[80], tmp[16];
322 int err;
323
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200324 SMP_DBG("u %32phN", u);
325 SMP_DBG("v %32phN", v);
326 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300327
328 memcpy(m, y, 16);
329 memcpy(m + 16, v, 32);
330 memcpy(m + 48, u, 32);
331
332 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
333 if (err)
334 return err;
335
336 *val = get_unaligned_le32(tmp);
337 *val %= 1000000;
338
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200339 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300340
341 return 0;
342}
343
Herbert Xu71af2f62016-01-24 21:18:30 +0800344static int smp_h6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200345 const u8 key_id[4], u8 res[16])
346{
347 int err;
348
349 SMP_DBG("w %16phN key_id %4phN", w, key_id);
350
351 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
352 if (err)
353 return err;
354
355 SMP_DBG("res %16phN", res);
356
357 return err;
358}
359
Johan Hedberga62da6f2016-12-08 08:32:54 +0200360static int smp_h7(struct crypto_shash *tfm_cmac, const u8 w[16],
361 const u8 salt[16], u8 res[16])
362{
363 int err;
364
365 SMP_DBG("w %16phN salt %16phN", w, salt);
366
367 err = aes_cmac(tfm_cmac, salt, w, 16, res);
368 if (err)
369 return err;
370
371 SMP_DBG("res %16phN", res);
372
373 return err;
374}
375
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200376/* The following functions map to the legacy SMP crypto functions e, c1,
377 * s1 and ah.
378 */
379
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700380static int smp_e(struct crypto_cipher *tfm, const u8 *k, u8 *r)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300381{
Johan Hedberg943a7322014-03-18 12:58:24 +0200382 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200383 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300384
Johan Hedberg011c3912015-05-19 21:06:04 +0300385 SMP_DBG("k %16phN r %16phN", k, r);
386
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200387 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300388 BT_ERR("tfm %p", tfm);
389 return -EINVAL;
390 }
391
Johan Hedberg943a7322014-03-18 12:58:24 +0200392 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300393 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200394
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700395 err = crypto_cipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300396 if (err) {
397 BT_ERR("cipher setkey failed: %d", err);
398 return err;
399 }
400
Johan Hedberg943a7322014-03-18 12:58:24 +0200401 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300402 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200403
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700404 crypto_cipher_encrypt_one(tfm, data, data);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300405
Johan Hedberg943a7322014-03-18 12:58:24 +0200406 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300407 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200408
Johan Hedberg011c3912015-05-19 21:06:04 +0300409 SMP_DBG("r %16phN", r);
410
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300411 return err;
412}
413
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700414static int smp_c1(struct crypto_cipher *tfm_aes, const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200415 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
416 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
417{
418 u8 p1[16], p2[16];
419 int err;
420
Johan Hedberg011c3912015-05-19 21:06:04 +0300421 SMP_DBG("k %16phN r %16phN", k, r);
422 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
423 SMP_DBG("preq %7phN pres %7phN", preq, pres);
424
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200425 memset(p1, 0, 16);
426
427 /* p1 = pres || preq || _rat || _iat */
428 p1[0] = _iat;
429 p1[1] = _rat;
430 memcpy(p1 + 2, preq, 7);
431 memcpy(p1 + 9, pres, 7);
432
Johan Hedberg011c3912015-05-19 21:06:04 +0300433 SMP_DBG("p1 %16phN", p1);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200434
435 /* res = r XOR p1 */
436 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
437
438 /* res = e(k, res) */
439 err = smp_e(tfm_aes, k, res);
440 if (err) {
441 BT_ERR("Encrypt data error");
442 return err;
443 }
444
Johan Hedberg011c3912015-05-19 21:06:04 +0300445 /* p2 = padding || ia || ra */
446 memcpy(p2, ra, 6);
447 memcpy(p2 + 6, ia, 6);
448 memset(p2 + 12, 0, 4);
449
450 SMP_DBG("p2 %16phN", p2);
451
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200452 /* res = res XOR p2 */
453 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
454
455 /* res = e(k, res) */
456 err = smp_e(tfm_aes, k, res);
457 if (err)
458 BT_ERR("Encrypt data error");
459
460 return err;
461}
462
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700463static int smp_s1(struct crypto_cipher *tfm_aes, const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200464 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300465{
466 int err;
467
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200468 /* Just least significant octets from r1 and r2 are considered */
469 memcpy(_r, r2, 8);
470 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300471
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200472 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300473 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200474 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300475
476 return err;
477}
478
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700479static int smp_ah(struct crypto_cipher *tfm, const u8 irk[16],
Johan Hedbergcd082792014-12-02 13:37:41 +0200480 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200481{
Johan Hedberg943a7322014-03-18 12:58:24 +0200482 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200483 int err;
484
485 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200486 memcpy(_res, r, 3);
487 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200488
Johan Hedberg943a7322014-03-18 12:58:24 +0200489 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200490 if (err) {
491 BT_ERR("Encrypt error");
492 return err;
493 }
494
495 /* The output of the random address function ah is:
Marcel Holtmannc5080d42015-09-04 17:08:18 +0200496 * ah(k, r) = e(k, r') mod 2^24
Johan Hedberg60478052014-02-18 10:19:31 +0200497 * The output of the security function e is then truncated to 24 bits
498 * by taking the least significant 24 bits of the output of e as the
499 * result of ah.
500 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200501 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200502
503 return 0;
504}
505
Johan Hedbergcd082792014-12-02 13:37:41 +0200506bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
507 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200508{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300509 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700510 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200511 u8 hash[3];
512 int err;
513
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300514 if (!chan || !chan->data)
515 return false;
516
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700517 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300518
Johan Hedberg60478052014-02-18 10:19:31 +0200519 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
520
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700521 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200522 if (err)
523 return false;
524
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200525 return !crypto_memneq(bdaddr->b, hash, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200526}
527
Johan Hedbergcd082792014-12-02 13:37:41 +0200528int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200529{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300530 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700531 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200532 int err;
533
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300534 if (!chan || !chan->data)
535 return -EOPNOTSUPP;
536
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700537 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300538
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200539 get_random_bytes(&rpa->b[3], 3);
540
541 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
542 rpa->b[5] |= 0x40; /* Set second most significant bit */
543
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700544 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200545 if (err < 0)
546 return err;
547
548 BT_DBG("RPA %pMR", rpa);
549
550 return 0;
551}
552
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700553int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
554{
555 struct l2cap_chan *chan = hdev->smp_data;
556 struct smp_dev *smp;
557 int err;
558
559 if (!chan || !chan->data)
560 return -EOPNOTSUPP;
561
562 smp = chan->data;
563
564 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
565 BT_DBG("Using debug keys");
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300566 err = set_ecdh_privkey(smp->tfm_ecdh, debug_sk);
567 if (err)
568 return err;
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700569 memcpy(smp->local_pk, debug_pk, 64);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700570 smp->debug_key = true;
571 } else {
572 while (true) {
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300573 /* Generate key pair for Secure Connections */
574 err = generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk);
Tudor Ambarusa2976412017-09-28 17:14:52 +0300575 if (err)
576 return err;
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700577
578 /* This is unlikely, but we need to check that
579 * we didn't accidentially generate a debug key.
580 */
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300581 if (crypto_memneq(smp->local_pk, debug_pk, 64))
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700582 break;
583 }
584 smp->debug_key = false;
585 }
586
587 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
588 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700589
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700590 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700591
592 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700593 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700594 if (err < 0)
595 return err;
596
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700597 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700598
Johan Hedberg94f14e42018-09-11 14:10:12 +0300599 smp->local_oob = true;
600
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700601 return 0;
602}
603
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300604static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
605{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300606 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300607 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300608 struct kvec iv[2];
609 struct msghdr msg;
610
611 if (!chan)
612 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300613
614 BT_DBG("code 0x%2.2x", code);
615
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300616 iv[0].iov_base = &code;
617 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300618
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300619 iv[1].iov_base = data;
620 iv[1].iov_len = len;
621
622 memset(&msg, 0, sizeof(msg));
623
David Howellsaa563d72018-10-20 00:57:56 +0100624 iov_iter_kvec(&msg.msg_iter, WRITE, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300625
626 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300627
Johan Hedbergb68fda62014-08-11 22:06:40 +0300628 if (!chan->data)
629 return;
630
631 smp = chan->data;
632
633 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300634 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300635}
636
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300637static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800638{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300639 if (authreq & SMP_AUTH_MITM) {
640 if (authreq & SMP_AUTH_SC)
641 return BT_SECURITY_FIPS;
642 else
643 return BT_SECURITY_HIGH;
644 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800645 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300646 }
Brian Gix2b64d152011-12-21 16:12:12 -0800647}
648
649static __u8 seclevel_to_authreq(__u8 sec_level)
650{
651 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300652 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800653 case BT_SECURITY_HIGH:
654 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
655 case BT_SECURITY_MEDIUM:
656 return SMP_AUTH_BONDING;
657 default:
658 return SMP_AUTH_NONE;
659 }
660}
661
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300662static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700663 struct smp_cmd_pairing *req,
664 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300665{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300666 struct l2cap_chan *chan = conn->smp;
667 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200668 struct hci_conn *hcon = conn->hcon;
669 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100670 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300671
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700672 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700673 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
674 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300675 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800676 } else {
677 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300678 }
679
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700680 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200681 remote_dist |= SMP_DIST_ID_KEY;
682
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700683 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200684 local_dist |= SMP_DIST_ID_KEY;
685
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700686 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100687 (authreq & SMP_AUTH_SC)) {
688 struct oob_data *oob_data;
689 u8 bdaddr_type;
690
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700691 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300692 local_dist |= SMP_DIST_LINK_KEY;
693 remote_dist |= SMP_DIST_LINK_KEY;
694 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100695
696 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
697 bdaddr_type = BDADDR_LE_PUBLIC;
698 else
699 bdaddr_type = BDADDR_LE_RANDOM;
700
701 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
702 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800703 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200704 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100705 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100706 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100707 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700708 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
709 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100710 }
711
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300712 } else {
713 authreq &= ~SMP_AUTH_SC;
714 }
715
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300716 if (rsp == NULL) {
717 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100718 req->oob_flag = oob_flag;
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300719 req->max_key_size = hdev->le_max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200720 req->init_key_dist = local_dist;
721 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300722 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200723
724 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300725 return;
726 }
727
728 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100729 rsp->oob_flag = oob_flag;
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300730 rsp->max_key_size = hdev->le_max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200731 rsp->init_key_dist = req->init_key_dist & remote_dist;
732 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300733 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200734
735 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300736}
737
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300738static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
739{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300740 struct l2cap_chan *chan = conn->smp;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300741 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300742 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300743
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300744 if (max_key_size > hdev->le_max_key_size ||
Johan Hedberg2fd36552015-06-11 13:52:26 +0300745 max_key_size < SMP_MIN_ENC_KEY_SIZE)
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300746 return SMP_ENC_KEY_SIZE;
747
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300748 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300749
750 return 0;
751}
752
Johan Hedberg6f48e262014-08-11 22:06:44 +0300753static void smp_chan_destroy(struct l2cap_conn *conn)
754{
755 struct l2cap_chan *chan = conn->smp;
756 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200757 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300758 bool complete;
759
760 BUG_ON(!smp);
761
762 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300763
Johan Hedberg6f48e262014-08-11 22:06:44 +0300764 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200765 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300766
Marcel Holtmann276812e2015-03-16 01:10:18 -0700767 kzfree(smp->csrk);
768 kzfree(smp->slave_csrk);
769 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300770
Andy Lutomirskia4770e12016-06-26 14:55:23 -0700771 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +0800772 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +0300773 crypto_free_kpp(smp->tfm_ecdh);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300774
Johan Hedberg923e2412014-12-03 12:43:39 +0200775 /* Ensure that we don't leave any debug key around if debug key
776 * support hasn't been explicitly enabled.
777 */
778 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700779 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200780 list_del_rcu(&smp->ltk->list);
781 kfree_rcu(smp->ltk, rcu);
782 smp->ltk = NULL;
783 }
784
Johan Hedberg6f48e262014-08-11 22:06:44 +0300785 /* If pairing failed clean up any keys we might have */
786 if (!complete) {
787 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200788 list_del_rcu(&smp->ltk->list);
789 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300790 }
791
792 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200793 list_del_rcu(&smp->slave_ltk->list);
794 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300795 }
796
797 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200798 list_del_rcu(&smp->remote_irk->list);
799 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300800 }
801 }
802
803 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700804 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200805 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300806}
807
Johan Hedberg84794e12013-11-06 11:24:57 +0200808static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800809{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200810 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300811 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200812
Johan Hedberg84794e12013-11-06 11:24:57 +0200813 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800814 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700815 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800816
Johan Hedberge1e930f2014-09-08 17:09:49 -0700817 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300818
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300819 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300820 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800821}
822
Brian Gix2b64d152011-12-21 16:12:12 -0800823#define JUST_WORKS 0x00
824#define JUST_CFM 0x01
825#define REQ_PASSKEY 0x02
826#define CFM_PASSKEY 0x03
827#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300828#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800829#define OVERLAP 0xFF
830
831static const u8 gen_method[5][5] = {
832 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
833 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
834 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
835 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
836 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
837};
838
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300839static const u8 sc_method[5][5] = {
840 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
841 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
842 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
843 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
844 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
845};
846
Johan Hedberg581370c2014-06-17 13:07:38 +0300847static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
848{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300849 /* If either side has unknown io_caps, use JUST_CFM (which gets
850 * converted later to JUST_WORKS if we're initiators.
851 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300852 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
853 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300854 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300855
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300856 if (test_bit(SMP_FLAG_SC, &smp->flags))
857 return sc_method[remote_io][local_io];
858
Johan Hedberg581370c2014-06-17 13:07:38 +0300859 return gen_method[remote_io][local_io];
860}
861
Brian Gix2b64d152011-12-21 16:12:12 -0800862static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
863 u8 local_io, u8 remote_io)
864{
865 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300866 struct l2cap_chan *chan = conn->smp;
867 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800868 u32 passkey = 0;
869 int ret = 0;
870
871 /* Initialize key for JUST WORKS */
872 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300873 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800874
875 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
876
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300877 /* If neither side wants MITM, either "just" confirm an incoming
878 * request or use just-works for outgoing ones. The JUST_CFM
879 * will be converted to JUST_WORKS if necessary later in this
880 * function. If either side has MITM look up the method from the
881 * table.
882 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300883 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300884 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800885 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300886 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800887
Johan Hedberga82505c2014-03-24 14:39:07 +0200888 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300889 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
890 &smp->flags))
891 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200892
Johan Hedberg02f3e252014-07-16 15:09:13 +0300893 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300894 if (smp->method == JUST_CFM &&
895 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
896 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300897
Brian Gix2b64d152011-12-21 16:12:12 -0800898 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300899 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300900 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800901 return 0;
902 }
903
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200904 /* If this function is used for SC -> legacy fallback we
905 * can only recover the just-works case.
906 */
907 if (test_bit(SMP_FLAG_SC, &smp->flags))
908 return -EINVAL;
909
Brian Gix2b64d152011-12-21 16:12:12 -0800910 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300911 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300912 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300913 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
914 hcon->pending_sec_level = BT_SECURITY_HIGH;
915 }
Brian Gix2b64d152011-12-21 16:12:12 -0800916
917 /* If both devices have Keyoard-Display I/O, the master
918 * Confirms and the slave Enters the passkey.
919 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300920 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300921 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300922 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800923 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300924 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800925 }
926
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200927 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300928 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200929 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800930 get_random_bytes(&passkey, sizeof(passkey));
931 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200932 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800933 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300934 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800935 }
936
Johan Hedberg783e0572014-05-31 18:48:26 +0300937 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700938 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200939 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300940 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200941 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
942 hcon->type, hcon->dst_type,
943 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800944 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200945 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200946 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200947 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800948
Brian Gix2b64d152011-12-21 16:12:12 -0800949 return ret;
950}
951
Johan Hedberg1cc61142014-05-20 09:45:52 +0300952static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300953{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300954 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300955 struct smp_cmd_pairing_confirm cp;
956 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300957
958 BT_DBG("conn %p", conn);
959
Johan Hedberge491eaf2014-10-25 21:15:37 +0200960 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200961 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200962 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
963 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300964 if (ret)
965 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300966
Johan Hedberg4a74d652014-05-20 09:45:50 +0300967 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800968
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300969 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
970
Johan Hedbergb28b4942014-09-05 22:19:55 +0300971 if (conn->hcon->out)
972 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
973 else
974 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
975
Johan Hedberg1cc61142014-05-20 09:45:52 +0300976 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300977}
978
Johan Hedberg861580a2014-05-20 09:45:51 +0300979static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300980{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300981 struct l2cap_conn *conn = smp->conn;
982 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300983 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300984 int ret;
985
Johan Hedbergec70f362014-06-27 14:23:04 +0300986 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300987 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300988
989 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
990
Johan Hedberge491eaf2014-10-25 21:15:37 +0200991 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200992 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200993 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300994 if (ret)
995 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300996
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200997 if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100998 bt_dev_err(hcon->hdev, "pairing failed "
999 "(confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +03001000 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001001 }
1002
1003 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001004 u8 stk[16];
1005 __le64 rand = 0;
1006 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001007
Johan Hedberge491eaf2014-10-25 21:15:37 +02001008 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001009
Johan Hedberg861580a2014-05-20 09:45:51 +03001010 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1011 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001012
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001013 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001014 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001015 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001016 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001017 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001018 __le64 rand = 0;
1019 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001020
Johan Hedberg943a7322014-03-18 12:58:24 +02001021 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1022 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001023
Johan Hedberge491eaf2014-10-25 21:15:37 +02001024 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001025
Johan Hedbergfff34902014-06-10 15:19:50 +03001026 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1027 auth = 1;
1028 else
1029 auth = 0;
1030
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001031 /* Even though there's no _SLAVE suffix this is the
1032 * slave STK we're adding for later lookup (the master
1033 * STK never needs to be stored).
1034 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001035 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001036 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001037 }
1038
Johan Hedberg861580a2014-05-20 09:45:51 +03001039 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001040}
1041
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001042static void smp_notify_keys(struct l2cap_conn *conn)
1043{
1044 struct l2cap_chan *chan = conn->smp;
1045 struct smp_chan *smp = chan->data;
1046 struct hci_conn *hcon = conn->hcon;
1047 struct hci_dev *hdev = hcon->hdev;
1048 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1049 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1050 bool persistent;
1051
Johan Hedbergcad20c22015-10-12 13:36:19 +02001052 if (hcon->type == ACL_LINK) {
1053 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1054 persistent = false;
1055 else
1056 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1057 &hcon->flags);
1058 } else {
1059 /* The LTKs, IRKs and CSRKs should be persistent only if
1060 * both sides had the bonding bit set in their
1061 * authentication requests.
1062 */
1063 persistent = !!((req->auth_req & rsp->auth_req) &
1064 SMP_AUTH_BONDING);
1065 }
1066
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001067 if (smp->remote_irk) {
Johan Hedbergcad20c22015-10-12 13:36:19 +02001068 mgmt_new_irk(hdev, smp->remote_irk, persistent);
1069
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001070 /* Now that user space can be considered to know the
1071 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001072 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001073 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001074 if (hcon->type == LE_LINK) {
1075 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1076 hcon->dst_type = smp->remote_irk->addr_type;
1077 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1078 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001079 }
1080
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001081 if (smp->csrk) {
1082 smp->csrk->bdaddr_type = hcon->dst_type;
1083 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1084 mgmt_new_csrk(hdev, smp->csrk, persistent);
1085 }
1086
1087 if (smp->slave_csrk) {
1088 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1089 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1090 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1091 }
1092
1093 if (smp->ltk) {
1094 smp->ltk->bdaddr_type = hcon->dst_type;
1095 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1096 mgmt_new_ltk(hdev, smp->ltk, persistent);
1097 }
1098
1099 if (smp->slave_ltk) {
1100 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1101 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1102 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1103 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001104
1105 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001106 struct link_key *key;
1107 u8 type;
1108
1109 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1110 type = HCI_LK_DEBUG_COMBINATION;
1111 else if (hcon->sec_level == BT_SECURITY_FIPS)
1112 type = HCI_LK_AUTH_COMBINATION_P256;
1113 else
1114 type = HCI_LK_UNAUTH_COMBINATION_P256;
1115
1116 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1117 smp->link_key, type, 0, &persistent);
1118 if (key) {
1119 mgmt_new_link_key(hdev, key, persistent);
1120
1121 /* Don't keep debug keys around if the relevant
1122 * flag is not set.
1123 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001124 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001125 key->type == HCI_LK_DEBUG_COMBINATION) {
1126 list_del_rcu(&key->list);
1127 kfree_rcu(key, rcu);
1128 }
1129 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001130 }
1131}
1132
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001133static void sc_add_ltk(struct smp_chan *smp)
1134{
1135 struct hci_conn *hcon = smp->conn->hcon;
1136 u8 key_type, auth;
1137
1138 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1139 key_type = SMP_LTK_P256_DEBUG;
1140 else
1141 key_type = SMP_LTK_P256;
1142
1143 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1144 auth = 1;
1145 else
1146 auth = 0;
1147
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001148 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1149 key_type, auth, smp->tk, smp->enc_key_size,
1150 0, 0);
1151}
1152
Johan Hedberg6a770832014-06-06 11:54:04 +03001153static void sc_generate_link_key(struct smp_chan *smp)
1154{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001155 /* From core spec. Spells out in ASCII as 'lebr'. */
Johan Hedberg6a770832014-06-06 11:54:04 +03001156 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1157
1158 smp->link_key = kzalloc(16, GFP_KERNEL);
1159 if (!smp->link_key)
1160 return;
1161
Johan Hedberga62da6f2016-12-08 08:32:54 +02001162 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1163 /* SALT = 0x00000000000000000000000000000000746D7031 */
1164 const u8 salt[16] = { 0x31, 0x70, 0x6d, 0x74 };
1165
1166 if (smp_h7(smp->tfm_cmac, smp->tk, salt, smp->link_key)) {
1167 kzfree(smp->link_key);
1168 smp->link_key = NULL;
1169 return;
1170 }
1171 } else {
1172 /* From core spec. Spells out in ASCII as 'tmp1'. */
1173 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1174
1175 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1176 kzfree(smp->link_key);
1177 smp->link_key = NULL;
1178 return;
1179 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001180 }
1181
1182 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001183 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001184 smp->link_key = NULL;
1185 return;
1186 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001187}
1188
Johan Hedbergb28b4942014-09-05 22:19:55 +03001189static void smp_allow_key_dist(struct smp_chan *smp)
1190{
1191 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1192 * will be allowed in each PDU handler to ensure we receive
1193 * them in the correct order.
1194 */
1195 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1196 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1197 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1198 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1199 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1200 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1201}
1202
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001203static void sc_generate_ltk(struct smp_chan *smp)
1204{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001205 /* From core spec. Spells out in ASCII as 'brle'. */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001206 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1207 struct hci_conn *hcon = smp->conn->hcon;
1208 struct hci_dev *hdev = hcon->hdev;
1209 struct link_key *key;
1210
1211 key = hci_find_link_key(hdev, &hcon->dst);
1212 if (!key) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001213 bt_dev_err(hdev, "no Link Key found to generate LTK");
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001214 return;
1215 }
1216
1217 if (key->type == HCI_LK_DEBUG_COMBINATION)
1218 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1219
Johan Hedberga62da6f2016-12-08 08:32:54 +02001220 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1221 /* SALT = 0x00000000000000000000000000000000746D7032 */
1222 const u8 salt[16] = { 0x32, 0x70, 0x6d, 0x74 };
1223
1224 if (smp_h7(smp->tfm_cmac, key->val, salt, smp->tk))
1225 return;
1226 } else {
1227 /* From core spec. Spells out in ASCII as 'tmp2'. */
1228 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1229
1230 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1231 return;
1232 }
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001233
1234 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1235 return;
1236
1237 sc_add_ltk(smp);
1238}
1239
Johan Hedbergd6268e82014-09-05 22:19:51 +03001240static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001241{
1242 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001243 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001244 struct hci_conn *hcon = conn->hcon;
1245 struct hci_dev *hdev = hcon->hdev;
1246 __u8 *keydist;
1247
1248 BT_DBG("conn %p", conn);
1249
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001250 rsp = (void *) &smp->prsp[1];
1251
1252 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001253 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1254 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001255 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001256 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001257
1258 req = (void *) &smp->preq[1];
1259
1260 if (hcon->out) {
1261 keydist = &rsp->init_key_dist;
1262 *keydist &= req->init_key_dist;
1263 } else {
1264 keydist = &rsp->resp_key_dist;
1265 *keydist &= req->resp_key_dist;
1266 }
1267
Johan Hedberg6a770832014-06-06 11:54:04 +03001268 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001269 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001270 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001271 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1272 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001273
1274 /* Clear the keys which are generated but not distributed */
1275 *keydist &= ~SMP_SC_NO_DIST;
1276 }
1277
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001278 BT_DBG("keydist 0x%x", *keydist);
1279
1280 if (*keydist & SMP_DIST_ENC_KEY) {
1281 struct smp_cmd_encrypt_info enc;
1282 struct smp_cmd_master_ident ident;
1283 struct smp_ltk *ltk;
1284 u8 authenticated;
1285 __le16 ediv;
1286 __le64 rand;
1287
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001288 /* Make sure we generate only the significant amount of
1289 * bytes based on the encryption key size, and set the rest
1290 * of the value to zeroes.
1291 */
1292 get_random_bytes(enc.ltk, smp->enc_key_size);
1293 memset(enc.ltk + smp->enc_key_size, 0,
1294 sizeof(enc.ltk) - smp->enc_key_size);
1295
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001296 get_random_bytes(&ediv, sizeof(ediv));
1297 get_random_bytes(&rand, sizeof(rand));
1298
1299 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1300
1301 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1302 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1303 SMP_LTK_SLAVE, authenticated, enc.ltk,
1304 smp->enc_key_size, ediv, rand);
1305 smp->slave_ltk = ltk;
1306
1307 ident.ediv = ediv;
1308 ident.rand = rand;
1309
1310 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1311
1312 *keydist &= ~SMP_DIST_ENC_KEY;
1313 }
1314
1315 if (*keydist & SMP_DIST_ID_KEY) {
1316 struct smp_cmd_ident_addr_info addrinfo;
1317 struct smp_cmd_ident_info idinfo;
1318
1319 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1320
1321 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1322
1323 /* The hci_conn contains the local identity address
1324 * after the connection has been established.
1325 *
1326 * This is true even when the connection has been
1327 * established using a resolvable random address.
1328 */
1329 bacpy(&addrinfo.bdaddr, &hcon->src);
1330 addrinfo.addr_type = hcon->src_type;
1331
1332 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1333 &addrinfo);
1334
1335 *keydist &= ~SMP_DIST_ID_KEY;
1336 }
1337
1338 if (*keydist & SMP_DIST_SIGN) {
1339 struct smp_cmd_sign_info sign;
1340 struct smp_csrk *csrk;
1341
1342 /* Generate a new random key */
1343 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1344
1345 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1346 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001347 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1348 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1349 else
1350 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001351 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1352 }
1353 smp->slave_csrk = csrk;
1354
1355 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1356
1357 *keydist &= ~SMP_DIST_SIGN;
1358 }
1359
1360 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001361 if (smp->remote_key_dist & KEY_DIST_MASK) {
1362 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001363 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001364 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001365
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001366 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1367 smp_notify_keys(conn);
1368
1369 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001370}
1371
Johan Hedbergb68fda62014-08-11 22:06:40 +03001372static void smp_timeout(struct work_struct *work)
1373{
1374 struct smp_chan *smp = container_of(work, struct smp_chan,
1375 security_timer.work);
1376 struct l2cap_conn *conn = smp->conn;
1377
1378 BT_DBG("conn %p", conn);
1379
Johan Hedberg1e91c292014-08-18 20:33:29 +03001380 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001381}
1382
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001383static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1384{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001385 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001386 struct smp_chan *smp;
1387
Marcel Holtmannf1560462013-10-13 05:43:25 -07001388 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001389 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001390 return NULL;
1391
Eric Biggers1ad0f162018-11-14 12:19:39 -08001392 smp->tfm_aes = crypto_alloc_cipher("aes", 0, 0);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001393 if (IS_ERR(smp->tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07001394 BT_ERR("Unable to create AES crypto context");
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001395 goto zfree_smp;
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001396 }
1397
Herbert Xu71af2f62016-01-24 21:18:30 +08001398 smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg407cecf62014-05-02 14:19:47 +03001399 if (IS_ERR(smp->tfm_cmac)) {
1400 BT_ERR("Unable to create CMAC crypto context");
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001401 goto free_cipher;
1402 }
1403
1404 smp->tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
1405 if (IS_ERR(smp->tfm_ecdh)) {
1406 BT_ERR("Unable to create ECDH crypto context");
1407 goto free_shash;
Johan Hedberg407cecf62014-05-02 14:19:47 +03001408 }
1409
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001410 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001411 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001412
Johan Hedbergb28b4942014-09-05 22:19:55 +03001413 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1414
Johan Hedbergb68fda62014-08-11 22:06:40 +03001415 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1416
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001417 hci_conn_hold(conn->hcon);
1418
1419 return smp;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001420
1421free_shash:
1422 crypto_free_shash(smp->tfm_cmac);
1423free_cipher:
1424 crypto_free_cipher(smp->tfm_aes);
1425zfree_smp:
1426 kzfree(smp);
1427 return NULL;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001428}
1429
Johan Hedberg760b0182014-06-06 11:44:05 +03001430static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1431{
1432 struct hci_conn *hcon = smp->conn->hcon;
1433 u8 *na, *nb, a[7], b[7];
1434
1435 if (hcon->out) {
1436 na = smp->prnd;
1437 nb = smp->rrnd;
1438 } else {
1439 na = smp->rrnd;
1440 nb = smp->prnd;
1441 }
1442
1443 memcpy(a, &hcon->init_addr, 6);
1444 memcpy(b, &hcon->resp_addr, 6);
1445 a[6] = hcon->init_addr_type;
1446 b[6] = hcon->resp_addr_type;
1447
1448 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1449}
1450
Johan Hedberg38606f12014-06-25 11:10:28 +03001451static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001452{
1453 struct hci_conn *hcon = smp->conn->hcon;
1454 struct smp_cmd_dhkey_check check;
1455 u8 a[7], b[7], *local_addr, *remote_addr;
1456 u8 io_cap[3], r[16];
1457
Johan Hedberg760b0182014-06-06 11:44:05 +03001458 memcpy(a, &hcon->init_addr, 6);
1459 memcpy(b, &hcon->resp_addr, 6);
1460 a[6] = hcon->init_addr_type;
1461 b[6] = hcon->resp_addr_type;
1462
1463 if (hcon->out) {
1464 local_addr = a;
1465 remote_addr = b;
1466 memcpy(io_cap, &smp->preq[1], 3);
1467 } else {
1468 local_addr = b;
1469 remote_addr = a;
1470 memcpy(io_cap, &smp->prsp[1], 3);
1471 }
1472
Johan Hedbergdddd3052014-06-01 15:38:09 +03001473 memset(r, 0, sizeof(r));
1474
1475 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001476 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001477
Johan Hedberga29b0732014-10-28 15:17:05 +01001478 if (smp->method == REQ_OOB)
1479 memcpy(r, smp->rr, 16);
1480
Johan Hedberg760b0182014-06-06 11:44:05 +03001481 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1482 local_addr, remote_addr, check.e);
1483
1484 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001485}
1486
Johan Hedberg38606f12014-06-25 11:10:28 +03001487static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1488{
1489 struct l2cap_conn *conn = smp->conn;
1490 struct hci_conn *hcon = conn->hcon;
1491 struct smp_cmd_pairing_confirm cfm;
1492 u8 r;
1493
1494 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1495 r |= 0x80;
1496
1497 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1498
1499 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1500 cfm.confirm_val))
1501 return SMP_UNSPECIFIED;
1502
1503 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1504
1505 return 0;
1506}
1507
1508static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1509{
1510 struct l2cap_conn *conn = smp->conn;
1511 struct hci_conn *hcon = conn->hcon;
1512 struct hci_dev *hdev = hcon->hdev;
1513 u8 cfm[16], r;
1514
1515 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1516 if (smp->passkey_round >= 20)
1517 return 0;
1518
1519 switch (smp_op) {
1520 case SMP_CMD_PAIRING_RANDOM:
1521 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1522 r |= 0x80;
1523
1524 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1525 smp->rrnd, r, cfm))
1526 return SMP_UNSPECIFIED;
1527
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02001528 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg38606f12014-06-25 11:10:28 +03001529 return SMP_CONFIRM_FAILED;
1530
1531 smp->passkey_round++;
1532
1533 if (smp->passkey_round == 20) {
1534 /* Generate MacKey and LTK */
1535 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1536 return SMP_UNSPECIFIED;
1537 }
1538
1539 /* The round is only complete when the initiator
1540 * receives pairing random.
1541 */
1542 if (!hcon->out) {
1543 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1544 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001545 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001546 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001547 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001548 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001549 return 0;
1550 }
1551
1552 /* Start the next round */
1553 if (smp->passkey_round != 20)
1554 return sc_passkey_round(smp, 0);
1555
1556 /* Passkey rounds are complete - start DHKey Check */
1557 sc_dhkey_check(smp);
1558 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1559
1560 break;
1561
1562 case SMP_CMD_PAIRING_CONFIRM:
1563 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1564 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1565 return 0;
1566 }
1567
1568 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1569
1570 if (hcon->out) {
1571 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1572 sizeof(smp->prnd), smp->prnd);
1573 return 0;
1574 }
1575
1576 return sc_passkey_send_confirm(smp);
1577
1578 case SMP_CMD_PUBLIC_KEY:
1579 default:
1580 /* Initiating device starts the round */
1581 if (!hcon->out)
1582 return 0;
1583
1584 BT_DBG("%s Starting passkey round %u", hdev->name,
1585 smp->passkey_round + 1);
1586
1587 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1588
1589 return sc_passkey_send_confirm(smp);
1590 }
1591
1592 return 0;
1593}
1594
Johan Hedbergdddd3052014-06-01 15:38:09 +03001595static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1596{
Johan Hedberg38606f12014-06-25 11:10:28 +03001597 struct l2cap_conn *conn = smp->conn;
1598 struct hci_conn *hcon = conn->hcon;
1599 u8 smp_op;
1600
1601 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1602
Johan Hedbergdddd3052014-06-01 15:38:09 +03001603 switch (mgmt_op) {
1604 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1605 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1606 return 0;
1607 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1608 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1609 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001610 case MGMT_OP_USER_PASSKEY_REPLY:
1611 hcon->passkey_notify = le32_to_cpu(passkey);
1612 smp->passkey_round = 0;
1613
1614 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1615 smp_op = SMP_CMD_PAIRING_CONFIRM;
1616 else
1617 smp_op = 0;
1618
1619 if (sc_passkey_round(smp, smp_op))
1620 return -EIO;
1621
1622 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001623 }
1624
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001625 /* Initiator sends DHKey check first */
1626 if (hcon->out) {
1627 sc_dhkey_check(smp);
1628 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1629 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1630 sc_dhkey_check(smp);
1631 sc_add_ltk(smp);
1632 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001633
1634 return 0;
1635}
1636
Brian Gix2b64d152011-12-21 16:12:12 -08001637int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1638{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001639 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001640 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001641 struct smp_chan *smp;
1642 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001643 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001644
1645 BT_DBG("");
1646
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001647 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001648 return -ENOTCONN;
1649
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001650 chan = conn->smp;
1651 if (!chan)
1652 return -ENOTCONN;
1653
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001654 l2cap_chan_lock(chan);
1655 if (!chan->data) {
1656 err = -ENOTCONN;
1657 goto unlock;
1658 }
1659
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001660 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001661
Johan Hedberg760b0182014-06-06 11:44:05 +03001662 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1663 err = sc_user_reply(smp, mgmt_op, passkey);
1664 goto unlock;
1665 }
1666
Brian Gix2b64d152011-12-21 16:12:12 -08001667 switch (mgmt_op) {
1668 case MGMT_OP_USER_PASSKEY_REPLY:
1669 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001670 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001671 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001672 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001673 /* Fall Through */
1674 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001675 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001676 break;
1677 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1678 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001679 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001680 err = 0;
1681 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001682 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001683 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001684 err = -EOPNOTSUPP;
1685 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001686 }
1687
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001688 err = 0;
1689
Brian Gix2b64d152011-12-21 16:12:12 -08001690 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001691 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1692 u8 rsp = smp_confirm(smp);
1693 if (rsp)
1694 smp_failure(conn, rsp);
1695 }
Brian Gix2b64d152011-12-21 16:12:12 -08001696
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001697unlock:
1698 l2cap_chan_unlock(chan);
1699 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001700}
1701
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001702static void build_bredr_pairing_cmd(struct smp_chan *smp,
1703 struct smp_cmd_pairing *req,
1704 struct smp_cmd_pairing *rsp)
1705{
1706 struct l2cap_conn *conn = smp->conn;
1707 struct hci_dev *hdev = conn->hcon->hdev;
1708 u8 local_dist = 0, remote_dist = 0;
1709
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001710 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001711 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1712 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1713 }
1714
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001715 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001716 remote_dist |= SMP_DIST_ID_KEY;
1717
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001718 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001719 local_dist |= SMP_DIST_ID_KEY;
1720
1721 if (!rsp) {
1722 memset(req, 0, sizeof(*req));
1723
Johan Hedberga62da6f2016-12-08 08:32:54 +02001724 req->auth_req = SMP_AUTH_CT2;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001725 req->init_key_dist = local_dist;
1726 req->resp_key_dist = remote_dist;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001727 req->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001728
1729 smp->remote_key_dist = remote_dist;
1730
1731 return;
1732 }
1733
1734 memset(rsp, 0, sizeof(*rsp));
1735
Johan Hedberga62da6f2016-12-08 08:32:54 +02001736 rsp->auth_req = SMP_AUTH_CT2;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001737 rsp->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001738 rsp->init_key_dist = req->init_key_dist & remote_dist;
1739 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1740
1741 smp->remote_key_dist = rsp->init_key_dist;
1742}
1743
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001744static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001745{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001746 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001747 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001748 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001749 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001750 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001751 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001752
1753 BT_DBG("conn %p", conn);
1754
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001755 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001756 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001757
Johan Hedberg40bef302014-07-16 11:42:27 +03001758 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001759 return SMP_CMD_NOTSUPP;
1760
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001761 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001762 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001763 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001764 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001765
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001766 if (!smp)
1767 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001768
Johan Hedbergc05b9332014-09-10 17:37:42 -07001769 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001770 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001771
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001772 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001773 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001774 return SMP_PAIRING_NOTSUPP;
1775
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001776 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001777 return SMP_AUTH_REQUIREMENTS;
1778
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001779 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1780 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001781 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001782
Johan Hedbergcb06d362015-03-16 21:12:34 +02001783 /* If the remote side's OOB flag is set it means it has
1784 * successfully received our local OOB data - therefore set the
1785 * flag to indicate that local OOB is in use.
1786 */
Johan Hedberg94f14e42018-09-11 14:10:12 +03001787 if (req->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob)
Johan Hedberg58428562015-03-16 11:45:45 +02001788 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1789
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001790 /* SMP over BR/EDR requires special treatment */
1791 if (conn->hcon->type == ACL_LINK) {
1792 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001793 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001794 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001795 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1796
1797 set_bit(SMP_FLAG_SC, &smp->flags);
1798
1799 build_bredr_pairing_cmd(smp, req, &rsp);
1800
Johan Hedberga62da6f2016-12-08 08:32:54 +02001801 if (req->auth_req & SMP_AUTH_CT2)
1802 set_bit(SMP_FLAG_CT2, &smp->flags);
1803
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001804 key_size = min(req->max_key_size, rsp.max_key_size);
1805 if (check_enc_key_size(conn, key_size))
1806 return SMP_ENC_KEY_SIZE;
1807
1808 /* Clear bits which are generated but not distributed */
1809 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1810
1811 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1812 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1813 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1814
1815 smp_distribute_keys(smp);
1816 return 0;
1817 }
1818
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001819 build_pairing_cmd(conn, req, &rsp, auth);
1820
Johan Hedberga62da6f2016-12-08 08:32:54 +02001821 if (rsp.auth_req & SMP_AUTH_SC) {
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001822 set_bit(SMP_FLAG_SC, &smp->flags);
1823
Johan Hedberga62da6f2016-12-08 08:32:54 +02001824 if (rsp.auth_req & SMP_AUTH_CT2)
1825 set_bit(SMP_FLAG_CT2, &smp->flags);
1826 }
1827
Johan Hedberg5be5e272014-09-10 17:58:54 -07001828 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001829 sec_level = BT_SECURITY_MEDIUM;
1830 else
1831 sec_level = authreq_to_seclevel(auth);
1832
Johan Hedbergc7262e72014-06-17 13:07:37 +03001833 if (sec_level > conn->hcon->pending_sec_level)
1834 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001835
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001836 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001837 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1838 u8 method;
1839
1840 method = get_auth_method(smp, conn->hcon->io_capability,
1841 req->io_capability);
1842 if (method == JUST_WORKS || method == JUST_CFM)
1843 return SMP_AUTH_REQUIREMENTS;
1844 }
1845
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001846 key_size = min(req->max_key_size, rsp.max_key_size);
1847 if (check_enc_key_size(conn, key_size))
1848 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001849
Johan Hedberge84a6b12013-12-02 10:49:03 +02001850 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001851
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001852 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1853 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001854
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001855 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001856
1857 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1858
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001859 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1860 * SC case, however some implementations incorrectly copy RFU auth
1861 * req bits from our security request, which may create a false
1862 * positive SC enablement.
1863 */
1864 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1865
Johan Hedberg3b191462014-06-06 10:50:15 +03001866 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1867 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1868 /* Clear bits which are generated but not distributed */
1869 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1870 /* Wait for Public Key from Initiating Device */
1871 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001872 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001873
Brian Gix2b64d152011-12-21 16:12:12 -08001874 /* Request setup of TK */
1875 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1876 if (ret)
1877 return SMP_UNSPECIFIED;
1878
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001879 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001880}
1881
Johan Hedberg3b191462014-06-06 10:50:15 +03001882static u8 sc_send_public_key(struct smp_chan *smp)
1883{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001884 struct hci_dev *hdev = smp->conn->hcon->hdev;
1885
Johan Hedberg3b191462014-06-06 10:50:15 +03001886 BT_DBG("");
1887
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001888 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001889 struct l2cap_chan *chan = hdev->smp_data;
1890 struct smp_dev *smp_dev;
1891
1892 if (!chan || !chan->data)
1893 return SMP_UNSPECIFIED;
1894
1895 smp_dev = chan->data;
1896
1897 memcpy(smp->local_pk, smp_dev->local_pk, 64);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001898 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001899
1900 if (smp_dev->debug_key)
1901 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1902
1903 goto done;
1904 }
1905
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001906 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001907 BT_DBG("Using debug keys");
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001908 if (set_ecdh_privkey(smp->tfm_ecdh, debug_sk))
1909 return SMP_UNSPECIFIED;
Johan Hedberg70157ef2014-06-24 15:22:59 +03001910 memcpy(smp->local_pk, debug_pk, 64);
Johan Hedberg70157ef2014-06-24 15:22:59 +03001911 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1912 } else {
1913 while (true) {
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001914 /* Generate key pair for Secure Connections */
1915 if (generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001916 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001917
Johan Hedberg70157ef2014-06-24 15:22:59 +03001918 /* This is unlikely, but we need to check that
1919 * we didn't accidentially generate a debug key.
1920 */
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001921 if (crypto_memneq(smp->local_pk, debug_pk, 64))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001922 break;
1923 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001924 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001925
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001926done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001927 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001928 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedberg3b191462014-06-06 10:50:15 +03001929
1930 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1931
1932 return 0;
1933}
1934
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001935static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001936{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001937 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001938 struct l2cap_chan *chan = conn->smp;
1939 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001940 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001941 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001942 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001943
1944 BT_DBG("conn %p", conn);
1945
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001946 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001947 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001948
Johan Hedberg40bef302014-07-16 11:42:27 +03001949 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001950 return SMP_CMD_NOTSUPP;
1951
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001952 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001953
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001954 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001955
1956 key_size = min(req->max_key_size, rsp->max_key_size);
1957 if (check_enc_key_size(conn, key_size))
1958 return SMP_ENC_KEY_SIZE;
1959
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001960 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001961
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001962 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001963 return SMP_AUTH_REQUIREMENTS;
1964
Johan Hedbergcb06d362015-03-16 21:12:34 +02001965 /* If the remote side's OOB flag is set it means it has
1966 * successfully received our local OOB data - therefore set the
1967 * flag to indicate that local OOB is in use.
1968 */
Johan Hedberg94f14e42018-09-11 14:10:12 +03001969 if (rsp->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob)
Johan Hedberg58428562015-03-16 11:45:45 +02001970 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1971
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001972 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1973 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1974
1975 /* Update remote key distribution in case the remote cleared
1976 * some bits that we had enabled in our request.
1977 */
1978 smp->remote_key_dist &= rsp->resp_key_dist;
1979
Johan Hedberga62da6f2016-12-08 08:32:54 +02001980 if ((req->auth_req & SMP_AUTH_CT2) && (auth & SMP_AUTH_CT2))
1981 set_bit(SMP_FLAG_CT2, &smp->flags);
1982
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001983 /* For BR/EDR this means we're done and can start phase 3 */
1984 if (conn->hcon->type == ACL_LINK) {
1985 /* Clear bits which are generated but not distributed */
1986 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1987 smp_distribute_keys(smp);
1988 return 0;
1989 }
1990
Johan Hedberg65668772014-05-16 11:03:34 +03001991 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1992 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001993 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1994 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001995
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001996 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001997 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1998 u8 method;
1999
2000 method = get_auth_method(smp, req->io_capability,
2001 rsp->io_capability);
2002 if (method == JUST_WORKS || method == JUST_CFM)
2003 return SMP_AUTH_REQUIREMENTS;
2004 }
2005
Johan Hedberge84a6b12013-12-02 10:49:03 +02002006 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002007
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02002008 /* Update remote key distribution in case the remote cleared
2009 * some bits that we had enabled in our request.
2010 */
2011 smp->remote_key_dist &= rsp->resp_key_dist;
2012
Johan Hedberg3b191462014-06-06 10:50:15 +03002013 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2014 /* Clear bits which are generated but not distributed */
2015 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
2016 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
2017 return sc_send_public_key(smp);
2018 }
2019
Johan Hedbergc05b9332014-09-10 17:37:42 -07002020 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08002021
Johan Hedberg476585e2012-06-06 18:54:15 +08002022 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08002023 if (ret)
2024 return SMP_UNSPECIFIED;
2025
Johan Hedberg4a74d652014-05-20 09:45:50 +03002026 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08002027
2028 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03002029 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002030 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002031
2032 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002033}
2034
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002035static u8 sc_check_confirm(struct smp_chan *smp)
2036{
2037 struct l2cap_conn *conn = smp->conn;
2038
2039 BT_DBG("");
2040
Johan Hedberg38606f12014-06-25 11:10:28 +03002041 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2042 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2043
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002044 if (conn->hcon->out) {
2045 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2046 smp->prnd);
2047 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2048 }
2049
2050 return 0;
2051}
2052
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002053/* Work-around for some implementations that incorrectly copy RFU bits
2054 * from our security request and thereby create the impression that
2055 * we're doing SC when in fact the remote doesn't support it.
2056 */
2057static int fixup_sc_false_positive(struct smp_chan *smp)
2058{
2059 struct l2cap_conn *conn = smp->conn;
2060 struct hci_conn *hcon = conn->hcon;
2061 struct hci_dev *hdev = hcon->hdev;
2062 struct smp_cmd_pairing *req, *rsp;
2063 u8 auth;
2064
2065 /* The issue is only observed when we're in slave role */
2066 if (hcon->out)
2067 return SMP_UNSPECIFIED;
2068
2069 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002070 bt_dev_err(hdev, "refusing legacy fallback in SC-only mode");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002071 return SMP_UNSPECIFIED;
2072 }
2073
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002074 bt_dev_err(hdev, "trying to fall back to legacy SMP");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002075
2076 req = (void *) &smp->preq[1];
2077 rsp = (void *) &smp->prsp[1];
2078
2079 /* Rebuild key dist flags which may have been cleared for SC */
2080 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2081
2082 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2083
2084 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002085 bt_dev_err(hdev, "failed to fall back to legacy SMP");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002086 return SMP_UNSPECIFIED;
2087 }
2088
2089 clear_bit(SMP_FLAG_SC, &smp->flags);
2090
2091 return 0;
2092}
2093
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002094static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002095{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002096 struct l2cap_chan *chan = conn->smp;
2097 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002098
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002099 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2100
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002101 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002102 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002103
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002104 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2105 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002106
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002107 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2108 int ret;
2109
2110 /* Public Key exchange must happen before any other steps */
2111 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2112 return sc_check_confirm(smp);
2113
2114 BT_ERR("Unexpected SMP Pairing Confirm");
2115
2116 ret = fixup_sc_false_positive(smp);
2117 if (ret)
2118 return ret;
2119 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002120
Johan Hedbergb28b4942014-09-05 22:19:55 +03002121 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002122 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2123 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002124 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2125 return 0;
2126 }
2127
2128 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002129 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002130
2131 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002132
2133 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002134}
2135
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002136static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002137{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002138 struct l2cap_chan *chan = conn->smp;
2139 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002140 struct hci_conn *hcon = conn->hcon;
2141 u8 *pkax, *pkbx, *na, *nb;
2142 u32 passkey;
2143 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002144
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002145 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002146
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002147 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002148 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002149
Johan Hedberg943a7322014-03-18 12:58:24 +02002150 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002151 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002152
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002153 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2154 return smp_random(smp);
2155
Johan Hedberg580039e2014-12-03 16:26:37 +02002156 if (hcon->out) {
2157 pkax = smp->local_pk;
2158 pkbx = smp->remote_pk;
2159 na = smp->prnd;
2160 nb = smp->rrnd;
2161 } else {
2162 pkax = smp->remote_pk;
2163 pkbx = smp->local_pk;
2164 na = smp->rrnd;
2165 nb = smp->prnd;
2166 }
2167
Johan Hedberga29b0732014-10-28 15:17:05 +01002168 if (smp->method == REQ_OOB) {
2169 if (!hcon->out)
2170 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2171 sizeof(smp->prnd), smp->prnd);
2172 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2173 goto mackey_and_ltk;
2174 }
2175
Johan Hedberg38606f12014-06-25 11:10:28 +03002176 /* Passkey entry has special treatment */
2177 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2178 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2179
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002180 if (hcon->out) {
2181 u8 cfm[16];
2182
2183 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2184 smp->rrnd, 0, cfm);
2185 if (err)
2186 return SMP_UNSPECIFIED;
2187
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002188 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002189 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002190 } else {
2191 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2192 smp->prnd);
2193 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002194 }
2195
Johan Hedberga29b0732014-10-28 15:17:05 +01002196mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002197 /* Generate MacKey and LTK */
2198 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2199 if (err)
2200 return SMP_UNSPECIFIED;
2201
Johan Hedberga29b0732014-10-28 15:17:05 +01002202 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002203 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002204 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002205 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2206 }
2207 return 0;
2208 }
2209
Johan Hedberg38606f12014-06-25 11:10:28 +03002210 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002211 if (err)
2212 return SMP_UNSPECIFIED;
2213
Johan Hedberg38606f12014-06-25 11:10:28 +03002214 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2215 hcon->dst_type, passkey, 0);
2216 if (err)
2217 return SMP_UNSPECIFIED;
2218
2219 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2220
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002221 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002222}
2223
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002224static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002225{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002226 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002227 struct hci_conn *hcon = conn->hcon;
2228
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002229 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002230 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002231 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002232
Johan Hedberga6f78332014-09-10 17:37:45 -07002233 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002234 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002235
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002236 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002237 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002238
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002239 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002240 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002241
Johan Hedbergfe59a052014-07-01 19:14:12 +03002242 /* We never store STKs for master role, so clear this flag */
2243 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2244
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002245 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002246}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002247
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002248bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2249 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002250{
2251 if (sec_level == BT_SECURITY_LOW)
2252 return true;
2253
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002254 /* If we're encrypted with an STK but the caller prefers using
2255 * LTK claim insufficient security. This way we allow the
2256 * connection to be re-encrypted with an LTK, even if the LTK
2257 * provides the same level of security. Only exception is if we
2258 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002259 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002260 if (key_pref == SMP_USE_LTK &&
2261 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002262 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002263 return false;
2264
Johan Hedberg854f4722014-07-01 18:40:20 +03002265 if (hcon->sec_level >= sec_level)
2266 return true;
2267
2268 return false;
2269}
2270
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002271static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002272{
2273 struct smp_cmd_security_req *rp = (void *) skb->data;
2274 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002275 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002276 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002277 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002278 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002279
2280 BT_DBG("conn %p", conn);
2281
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002282 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002283 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002284
Johan Hedberg40bef302014-07-16 11:42:27 +03002285 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002286 return SMP_CMD_NOTSUPP;
2287
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002288 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002289
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002290 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002291 return SMP_AUTH_REQUIREMENTS;
2292
Johan Hedberg5be5e272014-09-10 17:58:54 -07002293 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002294 sec_level = BT_SECURITY_MEDIUM;
2295 else
2296 sec_level = authreq_to_seclevel(auth);
2297
Szymon Janc64e759f2018-02-26 15:41:53 +01002298 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) {
2299 /* If link is already encrypted with sufficient security we
2300 * still need refresh encryption as per Core Spec 5.0 Vol 3,
2301 * Part H 2.4.6
2302 */
2303 smp_ltk_encrypt(conn, hcon->sec_level);
Johan Hedberg854f4722014-07-01 18:40:20 +03002304 return 0;
Szymon Janc64e759f2018-02-26 15:41:53 +01002305 }
Johan Hedberg854f4722014-07-01 18:40:20 +03002306
Johan Hedbergc7262e72014-06-17 13:07:37 +03002307 if (sec_level > hcon->pending_sec_level)
2308 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002309
Johan Hedberg4dab7862012-06-07 14:58:37 +08002310 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002311 return 0;
2312
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002313 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002314 if (!smp)
2315 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002316
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002317 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002318 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002319 return SMP_PAIRING_NOTSUPP;
2320
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002321 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002322
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002323 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002324 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002325
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002326 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2327 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002328
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002329 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002330 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002331
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002332 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002333}
2334
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002335int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002336{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002337 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002338 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002339 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002340 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002341 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002342
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002343 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2344
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002345 /* This may be NULL if there's an unexpected disconnection */
2346 if (!conn)
2347 return 1;
2348
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002349 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002350 return 1;
2351
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002352 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002353 return 1;
2354
Johan Hedbergc7262e72014-06-17 13:07:37 +03002355 if (sec_level > hcon->pending_sec_level)
2356 hcon->pending_sec_level = sec_level;
2357
Johan Hedberg40bef302014-07-16 11:42:27 +03002358 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002359 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2360 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002361
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002362 chan = conn->smp;
2363 if (!chan) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002364 bt_dev_err(hcon->hdev, "security requested but not available");
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002365 return 1;
2366 }
2367
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002368 l2cap_chan_lock(chan);
2369
2370 /* If SMP is already in progress ignore this request */
2371 if (chan->data) {
2372 ret = 0;
2373 goto unlock;
2374 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002375
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002376 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002377 if (!smp) {
2378 ret = 1;
2379 goto unlock;
2380 }
Brian Gix2b64d152011-12-21 16:12:12 -08002381
2382 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002383
Johan Hedberga62da6f2016-12-08 08:32:54 +02002384 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED)) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002385 authreq |= SMP_AUTH_SC;
Johan Hedberga62da6f2016-12-08 08:32:54 +02002386 if (hci_dev_test_flag(hcon->hdev, HCI_SSP_ENABLED))
2387 authreq |= SMP_AUTH_CT2;
2388 }
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002389
Johan Hedberg79897d22014-06-01 09:45:24 +03002390 /* Require MITM if IO Capability allows or the security level
2391 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002392 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002393 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002394 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002395 authreq |= SMP_AUTH_MITM;
2396
Johan Hedberg40bef302014-07-16 11:42:27 +03002397 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002398 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002399
Brian Gix2b64d152011-12-21 16:12:12 -08002400 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002401 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2402 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002403
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002404 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002405 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002406 } else {
2407 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002408 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002409 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002410 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002411 }
2412
Johan Hedberg4a74d652014-05-20 09:45:50 +03002413 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002414 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002415
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002416unlock:
2417 l2cap_chan_unlock(chan);
2418 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002419}
2420
Matias Karhumaacb28c302018-09-26 09:13:46 +03002421int smp_cancel_and_remove_pairing(struct hci_dev *hdev, bdaddr_t *bdaddr,
2422 u8 addr_type)
Johan Hedbergc81d5552015-10-22 09:38:35 +03002423{
Matias Karhumaacb28c302018-09-26 09:13:46 +03002424 struct hci_conn *hcon;
2425 struct l2cap_conn *conn;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002426 struct l2cap_chan *chan;
2427 struct smp_chan *smp;
Matias Karhumaacb28c302018-09-26 09:13:46 +03002428 int err;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002429
Matias Karhumaacb28c302018-09-26 09:13:46 +03002430 err = hci_remove_ltk(hdev, bdaddr, addr_type);
2431 hci_remove_irk(hdev, bdaddr, addr_type);
2432
2433 hcon = hci_conn_hash_lookup_le(hdev, bdaddr, addr_type);
2434 if (!hcon)
2435 goto done;
2436
2437 conn = hcon->l2cap_data;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002438 if (!conn)
Matias Karhumaacb28c302018-09-26 09:13:46 +03002439 goto done;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002440
2441 chan = conn->smp;
2442 if (!chan)
Matias Karhumaacb28c302018-09-26 09:13:46 +03002443 goto done;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002444
2445 l2cap_chan_lock(chan);
2446
2447 smp = chan->data;
2448 if (smp) {
Matias Karhumaacb28c302018-09-26 09:13:46 +03002449 /* Set keys to NULL to make sure smp_failure() does not try to
2450 * remove and free already invalidated rcu list entries. */
2451 smp->ltk = NULL;
2452 smp->slave_ltk = NULL;
2453 smp->remote_irk = NULL;
2454
Johan Hedbergc81d5552015-10-22 09:38:35 +03002455 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags))
2456 smp_failure(conn, 0);
2457 else
2458 smp_failure(conn, SMP_UNSPECIFIED);
Matias Karhumaacb28c302018-09-26 09:13:46 +03002459 err = 0;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002460 }
2461
2462 l2cap_chan_unlock(chan);
Matias Karhumaacb28c302018-09-26 09:13:46 +03002463
2464done:
2465 return err;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002466}
2467
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002468static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2469{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002470 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002471 struct l2cap_chan *chan = conn->smp;
2472 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002473
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002474 BT_DBG("conn %p", conn);
2475
2476 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002477 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002478
Johan Hedbergb28b4942014-09-05 22:19:55 +03002479 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002480
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002481 skb_pull(skb, sizeof(*rp));
2482
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002483 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002484
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002485 return 0;
2486}
2487
2488static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2489{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002490 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002491 struct l2cap_chan *chan = conn->smp;
2492 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002493 struct hci_dev *hdev = conn->hcon->hdev;
2494 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002495 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002496 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002497
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002498 BT_DBG("conn %p", conn);
2499
2500 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002501 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002502
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002503 /* Mark the information as received */
2504 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2505
Johan Hedbergb28b4942014-09-05 22:19:55 +03002506 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2507 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002508 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2509 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002510
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002511 skb_pull(skb, sizeof(*rp));
2512
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002513 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002514 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002515 authenticated, smp->tk, smp->enc_key_size,
2516 rp->ediv, rp->rand);
2517 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002518 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002519 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002520
2521 return 0;
2522}
2523
Johan Hedbergfd349c02014-02-18 10:19:36 +02002524static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2525{
2526 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002527 struct l2cap_chan *chan = conn->smp;
2528 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002529
2530 BT_DBG("");
2531
2532 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002533 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002534
Johan Hedbergb28b4942014-09-05 22:19:55 +03002535 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002536
Johan Hedbergfd349c02014-02-18 10:19:36 +02002537 skb_pull(skb, sizeof(*info));
2538
2539 memcpy(smp->irk, info->irk, 16);
2540
2541 return 0;
2542}
2543
2544static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2545 struct sk_buff *skb)
2546{
2547 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002548 struct l2cap_chan *chan = conn->smp;
2549 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002550 struct hci_conn *hcon = conn->hcon;
2551 bdaddr_t rpa;
2552
2553 BT_DBG("");
2554
2555 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002556 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002557
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002558 /* Mark the information as received */
2559 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2560
Johan Hedbergb28b4942014-09-05 22:19:55 +03002561 if (smp->remote_key_dist & SMP_DIST_SIGN)
2562 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2563
Johan Hedbergfd349c02014-02-18 10:19:36 +02002564 skb_pull(skb, sizeof(*info));
2565
Johan Hedberga9a58f82014-02-25 22:24:37 +02002566 /* Strictly speaking the Core Specification (4.1) allows sending
2567 * an empty address which would force us to rely on just the IRK
2568 * as "identity information". However, since such
2569 * implementations are not known of and in order to not over
2570 * complicate our implementation, simply pretend that we never
2571 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002572 *
2573 * The Identity Address must also be a Static Random or Public
2574 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002575 */
Johan Hedberge12af482015-01-14 20:51:37 +02002576 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2577 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002578 bt_dev_err(hcon->hdev, "ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002579 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002580 }
2581
Johan Hedbergfd349c02014-02-18 10:19:36 +02002582 bacpy(&smp->id_addr, &info->bdaddr);
2583 smp->id_addr_type = info->addr_type;
2584
2585 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2586 bacpy(&rpa, &hcon->dst);
2587 else
2588 bacpy(&rpa, BDADDR_ANY);
2589
Johan Hedberg23d0e122014-02-19 14:57:46 +02002590 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2591 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002592
Johan Hedberg31dd6242014-06-27 14:23:02 +03002593distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002594 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2595 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002596
2597 return 0;
2598}
2599
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002600static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2601{
2602 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002603 struct l2cap_chan *chan = conn->smp;
2604 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002605 struct smp_csrk *csrk;
2606
2607 BT_DBG("conn %p", conn);
2608
2609 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002610 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002611
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002612 /* Mark the information as received */
2613 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2614
2615 skb_pull(skb, sizeof(*rp));
2616
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002617 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2618 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002619 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2620 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2621 else
2622 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002623 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2624 }
2625 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002626 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002627
2628 return 0;
2629}
2630
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002631static u8 sc_select_method(struct smp_chan *smp)
2632{
2633 struct l2cap_conn *conn = smp->conn;
2634 struct hci_conn *hcon = conn->hcon;
2635 struct smp_cmd_pairing *local, *remote;
2636 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2637
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002638 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2639 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002640 return REQ_OOB;
2641
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002642 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2643 * which are needed as inputs to some crypto functions. To get
2644 * the "struct smp_cmd_pairing" from them we need to skip the
2645 * first byte which contains the opcode.
2646 */
2647 if (hcon->out) {
2648 local = (void *) &smp->preq[1];
2649 remote = (void *) &smp->prsp[1];
2650 } else {
2651 local = (void *) &smp->prsp[1];
2652 remote = (void *) &smp->preq[1];
2653 }
2654
2655 local_io = local->io_capability;
2656 remote_io = remote->io_capability;
2657
2658 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2659 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2660
2661 /* If either side wants MITM, look up the method from the table,
2662 * otherwise use JUST WORKS.
2663 */
2664 if (local_mitm || remote_mitm)
2665 method = get_auth_method(smp, local_io, remote_io);
2666 else
2667 method = JUST_WORKS;
2668
2669 /* Don't confirm locally initiated pairing attempts */
2670 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2671 method = JUST_WORKS;
2672
2673 return method;
2674}
2675
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002676static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2677{
2678 struct smp_cmd_public_key *key = (void *) skb->data;
2679 struct hci_conn *hcon = conn->hcon;
2680 struct l2cap_chan *chan = conn->smp;
2681 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002682 struct hci_dev *hdev = hcon->hdev;
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002683 struct crypto_kpp *tfm_ecdh;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002684 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002685 int err;
2686
2687 BT_DBG("conn %p", conn);
2688
2689 if (skb->len < sizeof(*key))
2690 return SMP_INVALID_PARAMS;
2691
2692 memcpy(smp->remote_pk, key, 64);
2693
Johan Hedberga8ca6172015-03-16 18:12:57 +02002694 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2695 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2696 smp->rr, 0, cfm.confirm_val);
2697 if (err)
2698 return SMP_UNSPECIFIED;
2699
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002700 if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16))
Johan Hedberga8ca6172015-03-16 18:12:57 +02002701 return SMP_CONFIRM_FAILED;
2702 }
2703
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002704 /* Non-initiating device sends its public key after receiving
2705 * the key from the initiating device.
2706 */
2707 if (!hcon->out) {
2708 err = sc_send_public_key(smp);
2709 if (err)
2710 return err;
2711 }
2712
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002713 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002714 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002715
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002716 /* Compute the shared secret on the same crypto tfm on which the private
2717 * key was set/generated.
2718 */
2719 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Matias Karhumaa4ba51752018-09-11 14:10:13 +03002720 struct l2cap_chan *hchan = hdev->smp_data;
2721 struct smp_dev *smp_dev;
2722
2723 if (!hchan || !hchan->data)
2724 return SMP_UNSPECIFIED;
2725
2726 smp_dev = hchan->data;
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002727
2728 tfm_ecdh = smp_dev->tfm_ecdh;
2729 } else {
2730 tfm_ecdh = smp->tfm_ecdh;
2731 }
2732
2733 if (compute_ecdh_secret(tfm_ecdh, smp->remote_pk, smp->dhkey))
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002734 return SMP_UNSPECIFIED;
2735
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002736 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002737
2738 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2739
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002740 smp->method = sc_select_method(smp);
2741
2742 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2743
2744 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2745 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2746 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2747 else
2748 hcon->pending_sec_level = BT_SECURITY_FIPS;
2749
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002750 if (!crypto_memneq(debug_pk, smp->remote_pk, 64))
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002751 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2752
Johan Hedberg38606f12014-06-25 11:10:28 +03002753 if (smp->method == DSP_PASSKEY) {
2754 get_random_bytes(&hcon->passkey_notify,
2755 sizeof(hcon->passkey_notify));
2756 hcon->passkey_notify %= 1000000;
2757 hcon->passkey_entered = 0;
2758 smp->passkey_round = 0;
2759 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2760 hcon->dst_type,
2761 hcon->passkey_notify,
2762 hcon->passkey_entered))
2763 return SMP_UNSPECIFIED;
2764 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2765 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2766 }
2767
Johan Hedberg94ea7252015-03-16 11:45:46 +02002768 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002769 if (hcon->out)
2770 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2771 sizeof(smp->prnd), smp->prnd);
2772
2773 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2774
2775 return 0;
2776 }
2777
Johan Hedberg38606f12014-06-25 11:10:28 +03002778 if (hcon->out)
2779 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2780
2781 if (smp->method == REQ_PASSKEY) {
2782 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2783 hcon->dst_type))
2784 return SMP_UNSPECIFIED;
2785 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2786 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2787 return 0;
2788 }
2789
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002790 /* The Initiating device waits for the non-initiating device to
2791 * send the confirm value.
2792 */
2793 if (conn->hcon->out)
2794 return 0;
2795
2796 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2797 0, cfm.confirm_val);
2798 if (err)
2799 return SMP_UNSPECIFIED;
2800
2801 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2802 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2803
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002804 return 0;
2805}
2806
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002807static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2808{
2809 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2810 struct l2cap_chan *chan = conn->smp;
2811 struct hci_conn *hcon = conn->hcon;
2812 struct smp_chan *smp = chan->data;
2813 u8 a[7], b[7], *local_addr, *remote_addr;
2814 u8 io_cap[3], r[16], e[16];
2815 int err;
2816
2817 BT_DBG("conn %p", conn);
2818
2819 if (skb->len < sizeof(*check))
2820 return SMP_INVALID_PARAMS;
2821
2822 memcpy(a, &hcon->init_addr, 6);
2823 memcpy(b, &hcon->resp_addr, 6);
2824 a[6] = hcon->init_addr_type;
2825 b[6] = hcon->resp_addr_type;
2826
2827 if (hcon->out) {
2828 local_addr = a;
2829 remote_addr = b;
2830 memcpy(io_cap, &smp->prsp[1], 3);
2831 } else {
2832 local_addr = b;
2833 remote_addr = a;
2834 memcpy(io_cap, &smp->preq[1], 3);
2835 }
2836
2837 memset(r, 0, sizeof(r));
2838
Johan Hedberg38606f12014-06-25 11:10:28 +03002839 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2840 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002841 else if (smp->method == REQ_OOB)
2842 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002843
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002844 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2845 io_cap, remote_addr, local_addr, e);
2846 if (err)
2847 return SMP_UNSPECIFIED;
2848
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002849 if (crypto_memneq(check->e, e, 16))
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002850 return SMP_DHKEY_CHECK_FAILED;
2851
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002852 if (!hcon->out) {
2853 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2854 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2855 return 0;
2856 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002857
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002858 /* Slave sends DHKey check as response to master */
2859 sc_dhkey_check(smp);
2860 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002861
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002862 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002863
2864 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002865 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002866 hcon->enc_key_size = smp->enc_key_size;
2867 }
2868
2869 return 0;
2870}
2871
Johan Hedberg1408bb62014-06-04 22:45:57 +03002872static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2873 struct sk_buff *skb)
2874{
2875 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2876
2877 BT_DBG("value 0x%02x", kp->value);
2878
2879 return 0;
2880}
2881
Johan Hedberg4befb862014-08-11 22:06:38 +03002882static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002883{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002884 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002885 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002886 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002887 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002888 int err = 0;
2889
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002890 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002891 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002892
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002893 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002894 reason = SMP_PAIRING_NOTSUPP;
2895 goto done;
2896 }
2897
Marcel Holtmann92381f52013-10-03 01:23:08 -07002898 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002899 skb_pull(skb, sizeof(code));
2900
Johan Hedbergb28b4942014-09-05 22:19:55 +03002901 smp = chan->data;
2902
2903 if (code > SMP_CMD_MAX)
2904 goto drop;
2905
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002906 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002907 goto drop;
2908
2909 /* If we don't have a context the only allowed commands are
2910 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002911 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002912 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2913 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002914
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002915 switch (code) {
2916 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002917 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002918 break;
2919
2920 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002921 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002922 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002923 break;
2924
2925 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002926 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002927 break;
2928
2929 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002930 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002931 break;
2932
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002933 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002934 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002935 break;
2936
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002937 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002938 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002939 break;
2940
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002941 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002942 reason = smp_cmd_encrypt_info(conn, skb);
2943 break;
2944
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002945 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002946 reason = smp_cmd_master_ident(conn, skb);
2947 break;
2948
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002949 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002950 reason = smp_cmd_ident_info(conn, skb);
2951 break;
2952
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002953 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002954 reason = smp_cmd_ident_addr_info(conn, skb);
2955 break;
2956
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002957 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002958 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002959 break;
2960
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002961 case SMP_CMD_PUBLIC_KEY:
2962 reason = smp_cmd_public_key(conn, skb);
2963 break;
2964
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002965 case SMP_CMD_DHKEY_CHECK:
2966 reason = smp_cmd_dhkey_check(conn, skb);
2967 break;
2968
Johan Hedberg1408bb62014-06-04 22:45:57 +03002969 case SMP_CMD_KEYPRESS_NOTIFY:
2970 reason = smp_cmd_keypress_notify(conn, skb);
2971 break;
2972
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002973 default:
2974 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002975 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002976 goto done;
2977 }
2978
2979done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002980 if (!err) {
2981 if (reason)
2982 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002983 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002984 }
2985
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002986 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002987
2988drop:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002989 bt_dev_err(hcon->hdev, "unexpected SMP command 0x%02x from %pMR",
2990 code, &hcon->dst);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002991 kfree_skb(skb);
2992 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002993}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002994
Johan Hedberg70db83c2014-08-08 09:37:16 +03002995static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2996{
2997 struct l2cap_conn *conn = chan->conn;
2998
2999 BT_DBG("chan %p", chan);
3000
Johan Hedbergfc75cc82014-09-05 22:19:52 +03003001 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003002 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003003
Johan Hedberg70db83c2014-08-08 09:37:16 +03003004 conn->smp = NULL;
3005 l2cap_chan_put(chan);
3006}
3007
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003008static void bredr_pairing(struct l2cap_chan *chan)
3009{
3010 struct l2cap_conn *conn = chan->conn;
3011 struct hci_conn *hcon = conn->hcon;
3012 struct hci_dev *hdev = hcon->hdev;
3013 struct smp_cmd_pairing req;
3014 struct smp_chan *smp;
3015
3016 BT_DBG("chan %p", chan);
3017
3018 /* Only new pairings are interesting */
3019 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
3020 return;
3021
3022 /* Don't bother if we're not encrypted */
3023 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3024 return;
3025
3026 /* Only master may initiate SMP over BR/EDR */
3027 if (hcon->role != HCI_ROLE_MASTER)
3028 return;
3029
3030 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003031 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003032 return;
3033
3034 /* BR/EDR must use Secure Connections for SMP */
3035 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003036 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003037 return;
3038
3039 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003040 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003041 return;
3042
3043 /* Don't bother if remote LE support is not enabled */
3044 if (!lmp_host_le_capable(hcon))
3045 return;
3046
3047 /* Remote must support SMP fixed chan for BR/EDR */
3048 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
3049 return;
3050
3051 /* Don't bother if SMP is already ongoing */
3052 if (chan->data)
3053 return;
3054
3055 smp = smp_chan_create(conn);
3056 if (!smp) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003057 bt_dev_err(hdev, "unable to create SMP context for BR/EDR");
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003058 return;
3059 }
3060
3061 set_bit(SMP_FLAG_SC, &smp->flags);
3062
3063 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
3064
3065 /* Prepare and send the BR/EDR SMP Pairing Request */
3066 build_bredr_pairing_cmd(smp, &req, NULL);
3067
3068 smp->preq[0] = SMP_CMD_PAIRING_REQ;
3069 memcpy(&smp->preq[1], &req, sizeof(req));
3070
3071 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
3072 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
3073}
3074
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003075static void smp_resume_cb(struct l2cap_chan *chan)
3076{
Johan Hedbergb68fda62014-08-11 22:06:40 +03003077 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003078 struct l2cap_conn *conn = chan->conn;
3079 struct hci_conn *hcon = conn->hcon;
3080
3081 BT_DBG("chan %p", chan);
3082
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003083 if (hcon->type == ACL_LINK) {
3084 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003085 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003086 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003087
Johan Hedberg86d14072014-08-11 22:06:43 +03003088 if (!smp)
3089 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03003090
Johan Hedberg84bc0db2014-09-05 22:19:49 +03003091 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3092 return;
3093
Johan Hedberg86d14072014-08-11 22:06:43 +03003094 cancel_delayed_work(&smp->security_timer);
3095
Johan Hedbergd6268e82014-09-05 22:19:51 +03003096 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003097}
3098
Johan Hedberg70db83c2014-08-08 09:37:16 +03003099static void smp_ready_cb(struct l2cap_chan *chan)
3100{
3101 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003102 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003103
3104 BT_DBG("chan %p", chan);
3105
Johan Hedberg78837462015-11-11 21:47:12 +02003106 /* No need to call l2cap_chan_hold() here since we already own
3107 * the reference taken in smp_new_conn_cb(). This is just the
3108 * first time that we tie it to a specific pointer. The code in
3109 * l2cap_core.c ensures that there's no risk this function wont
3110 * get called if smp_new_conn_cb was previously called.
3111 */
Johan Hedberg70db83c2014-08-08 09:37:16 +03003112 conn->smp = chan;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003113
3114 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3115 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003116}
3117
Johan Hedberg4befb862014-08-11 22:06:38 +03003118static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3119{
3120 int err;
3121
3122 BT_DBG("chan %p", chan);
3123
3124 err = smp_sig_channel(chan, skb);
3125 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003126 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003127
Johan Hedbergb68fda62014-08-11 22:06:40 +03003128 if (smp)
3129 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003130
Johan Hedberg1e91c292014-08-18 20:33:29 +03003131 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003132 }
3133
3134 return err;
3135}
3136
Johan Hedberg70db83c2014-08-08 09:37:16 +03003137static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3138 unsigned long hdr_len,
3139 unsigned long len, int nb)
3140{
3141 struct sk_buff *skb;
3142
3143 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3144 if (!skb)
3145 return ERR_PTR(-ENOMEM);
3146
3147 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003148 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003149
3150 return skb;
3151}
3152
3153static const struct l2cap_ops smp_chan_ops = {
3154 .name = "Security Manager",
3155 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003156 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003157 .alloc_skb = smp_alloc_skb_cb,
3158 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003159 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003160
3161 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003162 .state_change = l2cap_chan_no_state_change,
3163 .close = l2cap_chan_no_close,
3164 .defer = l2cap_chan_no_defer,
3165 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003166 .set_shutdown = l2cap_chan_no_set_shutdown,
3167 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003168};
3169
3170static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3171{
3172 struct l2cap_chan *chan;
3173
3174 BT_DBG("pchan %p", pchan);
3175
3176 chan = l2cap_chan_create();
3177 if (!chan)
3178 return NULL;
3179
3180 chan->chan_type = pchan->chan_type;
3181 chan->ops = &smp_chan_ops;
3182 chan->scid = pchan->scid;
3183 chan->dcid = chan->scid;
3184 chan->imtu = pchan->imtu;
3185 chan->omtu = pchan->omtu;
3186 chan->mode = pchan->mode;
3187
Johan Hedbergabe84902014-11-12 22:22:21 +02003188 /* Other L2CAP channels may request SMP routines in order to
3189 * change the security level. This means that the SMP channel
3190 * lock must be considered in its own category to avoid lockdep
3191 * warnings.
3192 */
3193 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3194
Johan Hedberg70db83c2014-08-08 09:37:16 +03003195 BT_DBG("created chan %p", chan);
3196
3197 return chan;
3198}
3199
3200static const struct l2cap_ops smp_root_chan_ops = {
3201 .name = "Security Manager Root",
3202 .new_connection = smp_new_conn_cb,
3203
3204 /* None of these are implemented for the root channel */
3205 .close = l2cap_chan_no_close,
3206 .alloc_skb = l2cap_chan_no_alloc_skb,
3207 .recv = l2cap_chan_no_recv,
3208 .state_change = l2cap_chan_no_state_change,
3209 .teardown = l2cap_chan_no_teardown,
3210 .ready = l2cap_chan_no_ready,
3211 .defer = l2cap_chan_no_defer,
3212 .suspend = l2cap_chan_no_suspend,
3213 .resume = l2cap_chan_no_resume,
3214 .set_shutdown = l2cap_chan_no_set_shutdown,
3215 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003216};
3217
Johan Hedbergef8efe42014-08-13 15:12:32 +03003218static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003219{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003220 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003221 struct smp_dev *smp;
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003222 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +08003223 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003224 struct crypto_kpp *tfm_ecdh;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003225
Johan Hedbergef8efe42014-08-13 15:12:32 +03003226 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003227 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003228 goto create_chan;
3229 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003230
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003231 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3232 if (!smp)
3233 return ERR_PTR(-ENOMEM);
3234
Eric Biggers1ad0f162018-11-14 12:19:39 -08003235 tfm_aes = crypto_alloc_cipher("aes", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003236 if (IS_ERR(tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003237 BT_ERR("Unable to create AES crypto context");
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003238 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003239 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003240 }
3241
Herbert Xu71af2f62016-01-24 21:18:30 +08003242 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003243 if (IS_ERR(tfm_cmac)) {
3244 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003245 crypto_free_cipher(tfm_aes);
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003246 kzfree(smp);
3247 return ERR_CAST(tfm_cmac);
3248 }
3249
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003250 tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
3251 if (IS_ERR(tfm_ecdh)) {
3252 BT_ERR("Unable to create ECDH crypto context");
3253 crypto_free_shash(tfm_cmac);
3254 crypto_free_cipher(tfm_aes);
3255 kzfree(smp);
3256 return ERR_CAST(tfm_ecdh);
3257 }
3258
Johan Hedberg94f14e42018-09-11 14:10:12 +03003259 smp->local_oob = false;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003260 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003261 smp->tfm_cmac = tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003262 smp->tfm_ecdh = tfm_ecdh;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003263
Johan Hedbergef8efe42014-08-13 15:12:32 +03003264create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003265 chan = l2cap_chan_create();
3266 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003267 if (smp) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003268 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +08003269 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003270 crypto_free_kpp(smp->tfm_ecdh);
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003271 kzfree(smp);
3272 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003273 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003274 }
3275
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003276 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003277
Johan Hedbergef8efe42014-08-13 15:12:32 +03003278 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003279
3280 l2cap_chan_set_defaults(chan);
3281
Marcel Holtmann157029b2015-01-14 15:43:09 -08003282 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003283 u8 bdaddr_type;
3284
3285 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3286
3287 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003288 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003289 else
3290 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003291 } else {
3292 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003293 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003294 }
3295
Johan Hedberg70db83c2014-08-08 09:37:16 +03003296 chan->state = BT_LISTEN;
3297 chan->mode = L2CAP_MODE_BASIC;
3298 chan->imtu = L2CAP_DEFAULT_MTU;
3299 chan->ops = &smp_root_chan_ops;
3300
Johan Hedbergabe84902014-11-12 22:22:21 +02003301 /* Set correct nesting level for a parent/listening channel */
3302 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3303
Johan Hedbergef8efe42014-08-13 15:12:32 +03003304 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003305}
3306
Johan Hedbergef8efe42014-08-13 15:12:32 +03003307static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003308{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003309 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003310
Johan Hedbergef8efe42014-08-13 15:12:32 +03003311 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003312
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003313 smp = chan->data;
3314 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003315 chan->data = NULL;
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003316 crypto_free_cipher(smp->tfm_aes);
Herbert Xu71af2f62016-01-24 21:18:30 +08003317 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003318 crypto_free_kpp(smp->tfm_ecdh);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003319 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003320 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003321
Johan Hedberg70db83c2014-08-08 09:37:16 +03003322 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003323}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003324
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003325static ssize_t force_bredr_smp_read(struct file *file,
3326 char __user *user_buf,
3327 size_t count, loff_t *ppos)
3328{
3329 struct hci_dev *hdev = file->private_data;
3330 char buf[3];
3331
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003332 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003333 buf[1] = '\n';
3334 buf[2] = '\0';
3335 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3336}
3337
3338static ssize_t force_bredr_smp_write(struct file *file,
3339 const char __user *user_buf,
3340 size_t count, loff_t *ppos)
3341{
3342 struct hci_dev *hdev = file->private_data;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003343 bool enable;
Andy Shevchenko3bf5e972018-05-29 16:33:48 +03003344 int err;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003345
Andy Shevchenko3bf5e972018-05-29 16:33:48 +03003346 err = kstrtobool_from_user(user_buf, count, &enable);
3347 if (err)
3348 return err;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003349
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003350 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003351 return -EALREADY;
3352
3353 if (enable) {
3354 struct l2cap_chan *chan;
3355
3356 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3357 if (IS_ERR(chan))
3358 return PTR_ERR(chan);
3359
3360 hdev->smp_bredr_data = chan;
3361 } else {
3362 struct l2cap_chan *chan;
3363
3364 chan = hdev->smp_bredr_data;
3365 hdev->smp_bredr_data = NULL;
3366 smp_del_chan(chan);
3367 }
3368
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003369 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003370
3371 return count;
3372}
3373
3374static const struct file_operations force_bredr_smp_fops = {
3375 .open = simple_open,
3376 .read = force_bredr_smp_read,
3377 .write = force_bredr_smp_write,
3378 .llseek = default_llseek,
3379};
3380
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003381static ssize_t le_min_key_size_read(struct file *file,
3382 char __user *user_buf,
3383 size_t count, loff_t *ppos)
3384{
3385 struct hci_dev *hdev = file->private_data;
3386 char buf[4];
3387
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003388 snprintf(buf, sizeof(buf), "%2u\n", hdev->le_min_key_size);
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003389
3390 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3391}
3392
3393static ssize_t le_min_key_size_write(struct file *file,
3394 const char __user *user_buf,
3395 size_t count, loff_t *ppos)
3396{
3397 struct hci_dev *hdev = file->private_data;
3398 char buf[32];
3399 size_t buf_size = min(count, (sizeof(buf) - 1));
3400 u8 key_size;
3401
3402 if (copy_from_user(buf, user_buf, buf_size))
3403 return -EFAULT;
3404
3405 buf[buf_size] = '\0';
3406
3407 sscanf(buf, "%hhu", &key_size);
3408
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003409 if (key_size > hdev->le_max_key_size ||
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003410 key_size < SMP_MIN_ENC_KEY_SIZE)
3411 return -EINVAL;
3412
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003413 hdev->le_min_key_size = key_size;
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003414
3415 return count;
3416}
3417
3418static const struct file_operations le_min_key_size_fops = {
3419 .open = simple_open,
3420 .read = le_min_key_size_read,
3421 .write = le_min_key_size_write,
3422 .llseek = default_llseek,
3423};
3424
Johan Hedberg2fd36552015-06-11 13:52:26 +03003425static ssize_t le_max_key_size_read(struct file *file,
3426 char __user *user_buf,
3427 size_t count, loff_t *ppos)
3428{
3429 struct hci_dev *hdev = file->private_data;
3430 char buf[4];
3431
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003432 snprintf(buf, sizeof(buf), "%2u\n", hdev->le_max_key_size);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003433
3434 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3435}
3436
3437static ssize_t le_max_key_size_write(struct file *file,
3438 const char __user *user_buf,
3439 size_t count, loff_t *ppos)
3440{
3441 struct hci_dev *hdev = file->private_data;
3442 char buf[32];
3443 size_t buf_size = min(count, (sizeof(buf) - 1));
3444 u8 key_size;
3445
3446 if (copy_from_user(buf, user_buf, buf_size))
3447 return -EFAULT;
3448
3449 buf[buf_size] = '\0';
3450
3451 sscanf(buf, "%hhu", &key_size);
3452
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003453 if (key_size > SMP_MAX_ENC_KEY_SIZE ||
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003454 key_size < hdev->le_min_key_size)
Johan Hedberg2fd36552015-06-11 13:52:26 +03003455 return -EINVAL;
3456
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003457 hdev->le_max_key_size = key_size;
Johan Hedberg2fd36552015-06-11 13:52:26 +03003458
3459 return count;
3460}
3461
3462static const struct file_operations le_max_key_size_fops = {
3463 .open = simple_open,
3464 .read = le_max_key_size_read,
3465 .write = le_max_key_size_write,
3466 .llseek = default_llseek,
3467};
3468
Johan Hedbergef8efe42014-08-13 15:12:32 +03003469int smp_register(struct hci_dev *hdev)
3470{
3471 struct l2cap_chan *chan;
3472
3473 BT_DBG("%s", hdev->name);
3474
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003475 /* If the controller does not support Low Energy operation, then
3476 * there is also no need to register any SMP channel.
3477 */
3478 if (!lmp_le_capable(hdev))
3479 return 0;
3480
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003481 if (WARN_ON(hdev->smp_data)) {
3482 chan = hdev->smp_data;
3483 hdev->smp_data = NULL;
3484 smp_del_chan(chan);
3485 }
3486
Johan Hedbergef8efe42014-08-13 15:12:32 +03003487 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3488 if (IS_ERR(chan))
3489 return PTR_ERR(chan);
3490
3491 hdev->smp_data = chan;
3492
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003493 debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
3494 &le_min_key_size_fops);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003495 debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3496 &le_max_key_size_fops);
3497
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003498 /* If the controller does not support BR/EDR Secure Connections
3499 * feature, then the BR/EDR SMP channel shall not be present.
3500 *
3501 * To test this with Bluetooth 4.0 controllers, create a debugfs
3502 * switch that allows forcing BR/EDR SMP support and accepting
3503 * cross-transport pairing on non-AES encrypted connections.
3504 */
3505 if (!lmp_sc_capable(hdev)) {
3506 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3507 hdev, &force_bredr_smp_fops);
Szymon Janc83ebb9e2016-09-09 20:24:40 +02003508
3509 /* Flag can be already set here (due to power toggle) */
3510 if (!hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
3511 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003512 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003513
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003514 if (WARN_ON(hdev->smp_bredr_data)) {
3515 chan = hdev->smp_bredr_data;
3516 hdev->smp_bredr_data = NULL;
3517 smp_del_chan(chan);
3518 }
3519
Johan Hedbergef8efe42014-08-13 15:12:32 +03003520 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3521 if (IS_ERR(chan)) {
3522 int err = PTR_ERR(chan);
3523 chan = hdev->smp_data;
3524 hdev->smp_data = NULL;
3525 smp_del_chan(chan);
3526 return err;
3527 }
3528
3529 hdev->smp_bredr_data = chan;
3530
3531 return 0;
3532}
3533
3534void smp_unregister(struct hci_dev *hdev)
3535{
3536 struct l2cap_chan *chan;
3537
3538 if (hdev->smp_bredr_data) {
3539 chan = hdev->smp_bredr_data;
3540 hdev->smp_bredr_data = NULL;
3541 smp_del_chan(chan);
3542 }
3543
3544 if (hdev->smp_data) {
3545 chan = hdev->smp_data;
3546 hdev->smp_data = NULL;
3547 smp_del_chan(chan);
3548 }
3549}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003550
3551#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3552
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003553static int __init test_debug_key(struct crypto_kpp *tfm_ecdh)
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003554{
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003555 u8 pk[64];
Tudor Ambarusa2976412017-09-28 17:14:52 +03003556 int err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003557
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003558 err = set_ecdh_privkey(tfm_ecdh, debug_sk);
Tudor Ambarusa2976412017-09-28 17:14:52 +03003559 if (err)
3560 return err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003561
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003562 err = generate_ecdh_public_key(tfm_ecdh, pk);
3563 if (err)
3564 return err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003565
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003566 if (crypto_memneq(pk, debug_pk, 64))
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003567 return -EINVAL;
3568
3569 return 0;
3570}
3571
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003572static int __init test_ah(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003573{
3574 const u8 irk[16] = {
3575 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3576 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3577 const u8 r[3] = { 0x94, 0x81, 0x70 };
3578 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3579 u8 res[3];
3580 int err;
3581
3582 err = smp_ah(tfm_aes, irk, r, res);
3583 if (err)
3584 return err;
3585
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003586 if (crypto_memneq(res, exp, 3))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003587 return -EINVAL;
3588
3589 return 0;
3590}
3591
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003592static int __init test_c1(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003593{
3594 const u8 k[16] = {
3595 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3596 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3597 const u8 r[16] = {
3598 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3599 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3600 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3601 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3602 const u8 _iat = 0x01;
3603 const u8 _rat = 0x00;
3604 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3605 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3606 const u8 exp[16] = {
3607 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3608 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3609 u8 res[16];
3610 int err;
3611
3612 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3613 if (err)
3614 return err;
3615
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003616 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003617 return -EINVAL;
3618
3619 return 0;
3620}
3621
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003622static int __init test_s1(struct crypto_cipher *tfm_aes)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003623{
3624 const u8 k[16] = {
3625 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3626 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3627 const u8 r1[16] = {
3628 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3629 const u8 r2[16] = {
3630 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3631 const u8 exp[16] = {
3632 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3633 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3634 u8 res[16];
3635 int err;
3636
3637 err = smp_s1(tfm_aes, k, r1, r2, res);
3638 if (err)
3639 return err;
3640
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003641 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003642 return -EINVAL;
3643
3644 return 0;
3645}
3646
Herbert Xu71af2f62016-01-24 21:18:30 +08003647static int __init test_f4(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003648{
3649 const u8 u[32] = {
3650 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3651 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3652 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3653 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3654 const u8 v[32] = {
3655 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3656 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3657 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3658 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3659 const u8 x[16] = {
3660 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3661 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3662 const u8 z = 0x00;
3663 const u8 exp[16] = {
3664 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3665 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3666 u8 res[16];
3667 int err;
3668
3669 err = smp_f4(tfm_cmac, u, v, x, z, res);
3670 if (err)
3671 return err;
3672
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003673 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003674 return -EINVAL;
3675
3676 return 0;
3677}
3678
Herbert Xu71af2f62016-01-24 21:18:30 +08003679static int __init test_f5(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003680{
3681 const u8 w[32] = {
3682 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3683 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3684 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3685 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3686 const u8 n1[16] = {
3687 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3688 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3689 const u8 n2[16] = {
3690 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3691 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3692 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3693 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3694 const u8 exp_ltk[16] = {
3695 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3696 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3697 const u8 exp_mackey[16] = {
3698 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3699 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3700 u8 mackey[16], ltk[16];
3701 int err;
3702
3703 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3704 if (err)
3705 return err;
3706
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003707 if (crypto_memneq(mackey, exp_mackey, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003708 return -EINVAL;
3709
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003710 if (crypto_memneq(ltk, exp_ltk, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003711 return -EINVAL;
3712
3713 return 0;
3714}
3715
Herbert Xu71af2f62016-01-24 21:18:30 +08003716static int __init test_f6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003717{
3718 const u8 w[16] = {
3719 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3720 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3721 const u8 n1[16] = {
3722 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3723 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3724 const u8 n2[16] = {
3725 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3726 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3727 const u8 r[16] = {
3728 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3729 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3730 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3731 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3732 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3733 const u8 exp[16] = {
3734 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3735 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3736 u8 res[16];
3737 int err;
3738
3739 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3740 if (err)
3741 return err;
3742
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003743 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003744 return -EINVAL;
3745
3746 return 0;
3747}
3748
Herbert Xu71af2f62016-01-24 21:18:30 +08003749static int __init test_g2(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003750{
3751 const u8 u[32] = {
3752 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3753 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3754 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3755 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3756 const u8 v[32] = {
3757 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3758 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3759 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3760 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3761 const u8 x[16] = {
3762 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3763 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3764 const u8 y[16] = {
3765 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3766 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3767 const u32 exp_val = 0x2f9ed5ba % 1000000;
3768 u32 val;
3769 int err;
3770
3771 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3772 if (err)
3773 return err;
3774
3775 if (val != exp_val)
3776 return -EINVAL;
3777
3778 return 0;
3779}
3780
Herbert Xu71af2f62016-01-24 21:18:30 +08003781static int __init test_h6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003782{
3783 const u8 w[16] = {
3784 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3785 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3786 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3787 const u8 exp[16] = {
3788 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3789 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3790 u8 res[16];
3791 int err;
3792
3793 err = smp_h6(tfm_cmac, w, key_id, res);
3794 if (err)
3795 return err;
3796
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003797 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003798 return -EINVAL;
3799
3800 return 0;
3801}
3802
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003803static char test_smp_buffer[32];
3804
3805static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3806 size_t count, loff_t *ppos)
3807{
3808 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3809 strlen(test_smp_buffer));
3810}
3811
3812static const struct file_operations test_smp_fops = {
3813 .open = simple_open,
3814 .read = test_smp_read,
3815 .llseek = default_llseek,
3816};
3817
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003818static int __init run_selftests(struct crypto_cipher *tfm_aes,
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003819 struct crypto_shash *tfm_cmac,
3820 struct crypto_kpp *tfm_ecdh)
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003821{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003822 ktime_t calltime, delta, rettime;
3823 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003824 int err;
3825
Marcel Holtmann255047b2014-12-30 00:11:20 -08003826 calltime = ktime_get();
3827
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003828 err = test_debug_key(tfm_ecdh);
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003829 if (err) {
3830 BT_ERR("debug_key test failed");
3831 goto done;
3832 }
3833
Johan Hedbergcfc41982014-12-30 09:50:40 +02003834 err = test_ah(tfm_aes);
3835 if (err) {
3836 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003837 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003838 }
3839
3840 err = test_c1(tfm_aes);
3841 if (err) {
3842 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003843 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003844 }
3845
3846 err = test_s1(tfm_aes);
3847 if (err) {
3848 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003849 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003850 }
3851
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003852 err = test_f4(tfm_cmac);
3853 if (err) {
3854 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003855 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003856 }
3857
3858 err = test_f5(tfm_cmac);
3859 if (err) {
3860 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003861 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003862 }
3863
3864 err = test_f6(tfm_cmac);
3865 if (err) {
3866 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003867 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003868 }
3869
3870 err = test_g2(tfm_cmac);
3871 if (err) {
3872 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003873 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003874 }
3875
3876 err = test_h6(tfm_cmac);
3877 if (err) {
3878 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003879 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003880 }
3881
Marcel Holtmann255047b2014-12-30 00:11:20 -08003882 rettime = ktime_get();
3883 delta = ktime_sub(rettime, calltime);
3884 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3885
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003886 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003887
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003888done:
3889 if (!err)
3890 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3891 "PASS (%llu usecs)\n", duration);
3892 else
3893 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3894
3895 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3896 &test_smp_fops);
3897
3898 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003899}
3900
3901int __init bt_selftest_smp(void)
3902{
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003903 struct crypto_cipher *tfm_aes;
Herbert Xu71af2f62016-01-24 21:18:30 +08003904 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003905 struct crypto_kpp *tfm_ecdh;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003906 int err;
3907
Eric Biggers1ad0f162018-11-14 12:19:39 -08003908 tfm_aes = crypto_alloc_cipher("aes", 0, 0);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003909 if (IS_ERR(tfm_aes)) {
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003910 BT_ERR("Unable to create AES crypto context");
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003911 return PTR_ERR(tfm_aes);
3912 }
3913
Eric Biggers3d234b32018-11-14 12:21:11 -08003914 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003915 if (IS_ERR(tfm_cmac)) {
3916 BT_ERR("Unable to create CMAC crypto context");
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003917 crypto_free_cipher(tfm_aes);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003918 return PTR_ERR(tfm_cmac);
3919 }
3920
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003921 tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
3922 if (IS_ERR(tfm_ecdh)) {
3923 BT_ERR("Unable to create ECDH crypto context");
3924 crypto_free_shash(tfm_cmac);
3925 crypto_free_cipher(tfm_aes);
3926 return PTR_ERR(tfm_ecdh);
3927 }
3928
3929 err = run_selftests(tfm_aes, tfm_cmac, tfm_ecdh);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003930
Herbert Xu71af2f62016-01-24 21:18:30 +08003931 crypto_free_shash(tfm_cmac);
Andy Lutomirskia4770e12016-06-26 14:55:23 -07003932 crypto_free_cipher(tfm_aes);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003933 crypto_free_kpp(tfm_ecdh);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003934
3935 return err;
3936}
3937
3938#endif