blob: d0220fb76dc02305b9c9b968289f9e19843a7ca8 [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/crypto.h>
25#include <linux/scatterlist.h>
26#include <crypto/b128ops.h>
27
Anderson Brigliaeb492e02011-06-09 18:50:40 -030028#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080031#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070032
Johan Hedberg3b191462014-06-06 10:50:15 +030033#include "ecc.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070034#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030035
Johan Hedberg2fd36552015-06-11 13:52:26 +030036#define SMP_DEV(hdev) \
37 ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data)
38
Johan Hedbergc7a3d572014-12-01 22:03:16 +020039/* Low-level debug macros to be used for stuff that we don't want
40 * accidentially in dmesg, i.e. the values of the various crypto keys
41 * and the inputs & outputs of crypto functions.
42 */
43#ifdef DEBUG
44#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
45 ##__VA_ARGS__)
46#else
47#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
48 ##__VA_ARGS__)
49#endif
50
Johan Hedbergb28b4942014-09-05 22:19:55 +030051#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030052
Johan Hedberg3b191462014-06-06 10:50:15 +030053/* Keys which are not distributed with Secure Connections */
54#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
55
Marcel Holtmann17b02e62012-03-01 14:32:37 -080056#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030057
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070058#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberg0edb14d2014-05-26 13:29:28 +030059 0x1f : 0x07)
60#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020061
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030062/* Maximum message length that can be passed to aes_cmac */
63#define CMAC_MSG_MAX 80
64
Johan Hedberg533e35d2014-06-16 19:25:18 +030065enum {
66 SMP_FLAG_TK_VALID,
67 SMP_FLAG_CFM_PENDING,
68 SMP_FLAG_MITM_AUTH,
69 SMP_FLAG_COMPLETE,
70 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030071 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030072 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030073 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030074 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030075 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg1a8bab42015-03-16 11:45:44 +020076 SMP_FLAG_REMOTE_OOB,
77 SMP_FLAG_LOCAL_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030078};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030079
Marcel Holtmann88a479d2015-03-16 01:10:19 -070080struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070081 /* Secure Connections OOB data */
82 u8 local_pk[64];
83 u8 local_sk[32];
Marcel Holtmannfb334fe2015-03-16 12:34:57 -070084 u8 local_rand[16];
Marcel Holtmann60a27d62015-03-16 01:10:22 -070085 bool debug_key;
86
Johan Hedberg2fd36552015-06-11 13:52:26 +030087 u8 max_key_size;
88
Marcel Holtmann88a479d2015-03-16 01:10:19 -070089 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -070090 struct crypto_hash *tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070091};
92
Johan Hedberg4bc58f52014-05-20 09:45:47 +030093struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030094 struct l2cap_conn *conn;
95 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030096 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030097
Johan Hedberg4bc58f52014-05-20 09:45:47 +030098 u8 preq[7]; /* SMP Pairing Request */
99 u8 prsp[7]; /* SMP Pairing Response */
100 u8 prnd[16]; /* SMP Pairing Random (local) */
101 u8 rrnd[16]; /* SMP Pairing Random (remote) */
102 u8 pcnf[16]; /* SMP Pairing Confirm */
103 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +0200104 u8 rr[16]; /* Remote OOB ra/rb value */
105 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300106 u8 enc_key_size;
107 u8 remote_key_dist;
108 bdaddr_t id_addr;
109 u8 id_addr_type;
110 u8 irk[16];
111 struct smp_csrk *csrk;
112 struct smp_csrk *slave_csrk;
113 struct smp_ltk *ltk;
114 struct smp_ltk *slave_ltk;
115 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300116 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300117 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300118 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300119 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300120
Johan Hedberg3b191462014-06-06 10:50:15 +0300121 /* Secure Connections variables */
122 u8 local_pk[64];
123 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300124 u8 remote_pk[64];
125 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300126 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300127
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300128 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300129 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300130};
131
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300132/* These debug key values are defined in the SMP section of the core
133 * specification. debug_pk is the public debug key and debug_sk the
134 * private debug key.
135 */
136static const u8 debug_pk[64] = {
137 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
138 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
139 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
140 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
141
142 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
143 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
144 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
145 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
146};
147
148static const u8 debug_sk[32] = {
149 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
150 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
151 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
152 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
153};
154
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300155static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300156{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300157 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300158
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300159 for (i = 0; i < len; i++)
160 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300161}
162
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200163/* The following functions map to the LE SC SMP crypto functions
164 * AES-CMAC, f4, f5, f6, g2 and h6.
165 */
166
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300167static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
168 size_t len, u8 mac[16])
169{
170 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
171 struct hash_desc desc;
172 struct scatterlist sg;
173 int err;
174
175 if (len > CMAC_MSG_MAX)
176 return -EFBIG;
177
178 if (!tfm) {
179 BT_ERR("tfm %p", tfm);
180 return -EINVAL;
181 }
182
183 desc.tfm = tfm;
184 desc.flags = 0;
185
186 crypto_hash_init(&desc);
187
188 /* Swap key and message from LSB to MSB */
189 swap_buf(k, tmp, 16);
190 swap_buf(m, msg_msb, len);
191
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200192 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
193 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300194
195 err = crypto_hash_setkey(tfm, tmp, 16);
196 if (err) {
197 BT_ERR("cipher setkey failed: %d", err);
198 return err;
199 }
200
201 sg_init_one(&sg, msg_msb, len);
202
203 err = crypto_hash_update(&desc, &sg, len);
204 if (err) {
205 BT_ERR("Hash update error %d", err);
206 return err;
207 }
208
209 err = crypto_hash_final(&desc, mac_msb);
210 if (err) {
211 BT_ERR("Hash final error %d", err);
212 return err;
213 }
214
215 swap_buf(mac_msb, mac, 16);
216
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200217 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300218
219 return 0;
220}
221
222static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
223 const u8 x[16], u8 z, u8 res[16])
224{
225 u8 m[65];
226 int err;
227
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200228 SMP_DBG("u %32phN", u);
229 SMP_DBG("v %32phN", v);
230 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300231
232 m[0] = z;
233 memcpy(m + 1, v, 32);
234 memcpy(m + 33, u, 32);
235
236 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
237 if (err)
238 return err;
239
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200240 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300241
242 return err;
243}
244
Johan Hedberg4da50de2014-12-29 12:04:10 +0200245static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32],
246 const u8 n1[16], const u8 n2[16], const u8 a1[7],
247 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300248{
249 /* The btle, salt and length "magic" values are as defined in
250 * the SMP section of the Bluetooth core specification. In ASCII
251 * the btle value ends up being 'btle'. The salt is just a
252 * random number whereas length is the value 256 in little
253 * endian format.
254 */
255 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
256 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
257 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
258 const u8 length[2] = { 0x00, 0x01 };
259 u8 m[53], t[16];
260 int err;
261
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200262 SMP_DBG("w %32phN", w);
263 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
264 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300265
266 err = aes_cmac(tfm_cmac, salt, w, 32, t);
267 if (err)
268 return err;
269
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200270 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300271
272 memcpy(m, length, 2);
273 memcpy(m + 2, a2, 7);
274 memcpy(m + 9, a1, 7);
275 memcpy(m + 16, n2, 16);
276 memcpy(m + 32, n1, 16);
277 memcpy(m + 48, btle, 4);
278
279 m[52] = 0; /* Counter */
280
281 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
282 if (err)
283 return err;
284
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200285 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300286
287 m[52] = 1; /* Counter */
288
289 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
290 if (err)
291 return err;
292
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200293 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300294
295 return 0;
296}
297
298static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200299 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300300 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
301 u8 res[16])
302{
303 u8 m[65];
304 int err;
305
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200306 SMP_DBG("w %16phN", w);
307 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
308 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300309
310 memcpy(m, a2, 7);
311 memcpy(m + 7, a1, 7);
312 memcpy(m + 14, io_cap, 3);
313 memcpy(m + 17, r, 16);
314 memcpy(m + 33, n2, 16);
315 memcpy(m + 49, n1, 16);
316
317 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
318 if (err)
319 return err;
320
Marcel Holtmann203de212014-12-31 20:01:22 -0800321 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300322
323 return err;
324}
325
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300326static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
327 const u8 x[16], const u8 y[16], u32 *val)
328{
329 u8 m[80], tmp[16];
330 int err;
331
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200332 SMP_DBG("u %32phN", u);
333 SMP_DBG("v %32phN", v);
334 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300335
336 memcpy(m, y, 16);
337 memcpy(m + 16, v, 32);
338 memcpy(m + 48, u, 32);
339
340 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
341 if (err)
342 return err;
343
344 *val = get_unaligned_le32(tmp);
345 *val %= 1000000;
346
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200347 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300348
349 return 0;
350}
351
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200352static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
353 const u8 key_id[4], u8 res[16])
354{
355 int err;
356
357 SMP_DBG("w %16phN key_id %4phN", w, key_id);
358
359 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
360 if (err)
361 return err;
362
363 SMP_DBG("res %16phN", res);
364
365 return err;
366}
367
368/* The following functions map to the legacy SMP crypto functions e, c1,
369 * s1 and ah.
370 */
371
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300372static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
373{
374 struct blkcipher_desc desc;
375 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200376 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200377 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300378
Johan Hedberg011c3912015-05-19 21:06:04 +0300379 SMP_DBG("k %16phN r %16phN", k, r);
380
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200381 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300382 BT_ERR("tfm %p", tfm);
383 return -EINVAL;
384 }
385
386 desc.tfm = tfm;
387 desc.flags = 0;
388
Johan Hedberg943a7322014-03-18 12:58:24 +0200389 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300390 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200391
392 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300393 if (err) {
394 BT_ERR("cipher setkey failed: %d", err);
395 return err;
396 }
397
Johan Hedberg943a7322014-03-18 12:58:24 +0200398 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300399 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200400
401 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300402
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300403 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
404 if (err)
405 BT_ERR("Encrypt data error %d", err);
406
Johan Hedberg943a7322014-03-18 12:58:24 +0200407 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300408 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200409
Johan Hedberg011c3912015-05-19 21:06:04 +0300410 SMP_DBG("r %16phN", r);
411
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300412 return err;
413}
414
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200415static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
416 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
417 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
418{
419 u8 p1[16], p2[16];
420 int err;
421
Johan Hedberg011c3912015-05-19 21:06:04 +0300422 SMP_DBG("k %16phN r %16phN", k, r);
423 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
424 SMP_DBG("preq %7phN pres %7phN", preq, pres);
425
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200426 memset(p1, 0, 16);
427
428 /* p1 = pres || preq || _rat || _iat */
429 p1[0] = _iat;
430 p1[1] = _rat;
431 memcpy(p1 + 2, preq, 7);
432 memcpy(p1 + 9, pres, 7);
433
Johan Hedberg011c3912015-05-19 21:06:04 +0300434 SMP_DBG("p1 %16phN", p1);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200435
436 /* res = r XOR p1 */
437 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
438
439 /* res = e(k, res) */
440 err = smp_e(tfm_aes, k, res);
441 if (err) {
442 BT_ERR("Encrypt data error");
443 return err;
444 }
445
Johan Hedberg011c3912015-05-19 21:06:04 +0300446 /* p2 = padding || ia || ra */
447 memcpy(p2, ra, 6);
448 memcpy(p2 + 6, ia, 6);
449 memset(p2 + 12, 0, 4);
450
451 SMP_DBG("p2 %16phN", p2);
452
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200453 /* res = res XOR p2 */
454 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
455
456 /* res = e(k, res) */
457 err = smp_e(tfm_aes, k, res);
458 if (err)
459 BT_ERR("Encrypt data error");
460
461 return err;
462}
463
464static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
465 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300466{
467 int err;
468
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200469 /* Just least significant octets from r1 and r2 are considered */
470 memcpy(_r, r2, 8);
471 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300472
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200473 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300474 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200475 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300476
477 return err;
478}
479
Johan Hedbergcd082792014-12-02 13:37:41 +0200480static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
481 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200482{
Johan Hedberg943a7322014-03-18 12:58:24 +0200483 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200484 int err;
485
486 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200487 memcpy(_res, r, 3);
488 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200489
Johan Hedberg943a7322014-03-18 12:58:24 +0200490 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200491 if (err) {
492 BT_ERR("Encrypt error");
493 return err;
494 }
495
496 /* The output of the random address function ah is:
497 * ah(h, r) = e(k, r') mod 2^24
498 * The output of the security function e is then truncated to 24 bits
499 * by taking the least significant 24 bits of the output of e as the
500 * result of ah.
501 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200502 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200503
504 return 0;
505}
506
Johan Hedbergcd082792014-12-02 13:37:41 +0200507bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
508 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200509{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300510 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700511 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200512 u8 hash[3];
513 int err;
514
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300515 if (!chan || !chan->data)
516 return false;
517
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700518 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300519
Johan Hedberg60478052014-02-18 10:19:31 +0200520 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
521
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700522 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200523 if (err)
524 return false;
525
526 return !memcmp(bdaddr->b, hash, 3);
527}
528
Johan Hedbergcd082792014-12-02 13:37:41 +0200529int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200530{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300531 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700532 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200533 int err;
534
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300535 if (!chan || !chan->data)
536 return -EOPNOTSUPP;
537
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700538 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300539
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200540 get_random_bytes(&rpa->b[3], 3);
541
542 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
543 rpa->b[5] |= 0x40; /* Set second most significant bit */
544
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700545 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200546 if (err < 0)
547 return err;
548
549 BT_DBG("RPA %pMR", rpa);
550
551 return 0;
552}
553
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700554int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
555{
556 struct l2cap_chan *chan = hdev->smp_data;
557 struct smp_dev *smp;
558 int err;
559
560 if (!chan || !chan->data)
561 return -EOPNOTSUPP;
562
563 smp = chan->data;
564
565 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
566 BT_DBG("Using debug keys");
567 memcpy(smp->local_pk, debug_pk, 64);
568 memcpy(smp->local_sk, debug_sk, 32);
569 smp->debug_key = true;
570 } else {
571 while (true) {
572 /* Generate local key pair for Secure Connections */
573 if (!ecc_make_key(smp->local_pk, smp->local_sk))
574 return -EIO;
575
576 /* This is unlikely, but we need to check that
577 * we didn't accidentially generate a debug key.
578 */
579 if (memcmp(smp->local_sk, debug_sk, 32))
580 break;
581 }
582 smp->debug_key = false;
583 }
584
585 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
586 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
587 SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
588
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700589 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700590
591 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700592 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700593 if (err < 0)
594 return err;
595
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700596 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700597
598 return 0;
599}
600
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300601static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
602{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300603 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300604 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300605 struct kvec iv[2];
606 struct msghdr msg;
607
608 if (!chan)
609 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300610
611 BT_DBG("code 0x%2.2x", code);
612
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300613 iv[0].iov_base = &code;
614 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300615
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300616 iv[1].iov_base = data;
617 iv[1].iov_len = len;
618
619 memset(&msg, 0, sizeof(msg));
620
Al Viro17836392014-11-24 17:07:38 -0500621 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300622
623 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300624
Johan Hedbergb68fda62014-08-11 22:06:40 +0300625 if (!chan->data)
626 return;
627
628 smp = chan->data;
629
630 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300631 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300632}
633
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300634static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800635{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300636 if (authreq & SMP_AUTH_MITM) {
637 if (authreq & SMP_AUTH_SC)
638 return BT_SECURITY_FIPS;
639 else
640 return BT_SECURITY_HIGH;
641 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800642 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300643 }
Brian Gix2b64d152011-12-21 16:12:12 -0800644}
645
646static __u8 seclevel_to_authreq(__u8 sec_level)
647{
648 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300649 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800650 case BT_SECURITY_HIGH:
651 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
652 case BT_SECURITY_MEDIUM:
653 return SMP_AUTH_BONDING;
654 default:
655 return SMP_AUTH_NONE;
656 }
657}
658
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300659static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700660 struct smp_cmd_pairing *req,
661 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300662{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300663 struct l2cap_chan *chan = conn->smp;
664 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200665 struct hci_conn *hcon = conn->hcon;
666 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100667 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300668
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700669 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700670 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
671 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300672 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800673 } else {
674 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300675 }
676
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700677 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200678 remote_dist |= SMP_DIST_ID_KEY;
679
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700680 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200681 local_dist |= SMP_DIST_ID_KEY;
682
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700683 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100684 (authreq & SMP_AUTH_SC)) {
685 struct oob_data *oob_data;
686 u8 bdaddr_type;
687
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700688 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300689 local_dist |= SMP_DIST_LINK_KEY;
690 remote_dist |= SMP_DIST_LINK_KEY;
691 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100692
693 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
694 bdaddr_type = BDADDR_LE_PUBLIC;
695 else
696 bdaddr_type = BDADDR_LE_RANDOM;
697
698 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
699 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800700 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200701 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100702 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100703 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100704 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700705 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
706 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100707 }
708
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300709 } else {
710 authreq &= ~SMP_AUTH_SC;
711 }
712
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300713 if (rsp == NULL) {
714 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100715 req->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300716 req->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200717 req->init_key_dist = local_dist;
718 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300719 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200720
721 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300722 return;
723 }
724
725 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100726 rsp->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300727 rsp->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200728 rsp->init_key_dist = req->init_key_dist & remote_dist;
729 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300730 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200731
732 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300733}
734
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300735static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
736{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300737 struct l2cap_chan *chan = conn->smp;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300738 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300739 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300740
Johan Hedberg2fd36552015-06-11 13:52:26 +0300741 if (max_key_size > SMP_DEV(hdev)->max_key_size ||
742 max_key_size < SMP_MIN_ENC_KEY_SIZE)
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300743 return SMP_ENC_KEY_SIZE;
744
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300745 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300746
747 return 0;
748}
749
Johan Hedberg6f48e262014-08-11 22:06:44 +0300750static void smp_chan_destroy(struct l2cap_conn *conn)
751{
752 struct l2cap_chan *chan = conn->smp;
753 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200754 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300755 bool complete;
756
757 BUG_ON(!smp);
758
759 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300760
Johan Hedberg6f48e262014-08-11 22:06:44 +0300761 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200762 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300763
Marcel Holtmann276812e2015-03-16 01:10:18 -0700764 kzfree(smp->csrk);
765 kzfree(smp->slave_csrk);
766 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300767
768 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300769 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300770
Johan Hedberg923e2412014-12-03 12:43:39 +0200771 /* Ensure that we don't leave any debug key around if debug key
772 * support hasn't been explicitly enabled.
773 */
774 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700775 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200776 list_del_rcu(&smp->ltk->list);
777 kfree_rcu(smp->ltk, rcu);
778 smp->ltk = NULL;
779 }
780
Johan Hedberg6f48e262014-08-11 22:06:44 +0300781 /* If pairing failed clean up any keys we might have */
782 if (!complete) {
783 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200784 list_del_rcu(&smp->ltk->list);
785 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300786 }
787
788 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200789 list_del_rcu(&smp->slave_ltk->list);
790 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300791 }
792
793 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200794 list_del_rcu(&smp->remote_irk->list);
795 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300796 }
797 }
798
799 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700800 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200801 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300802}
803
Johan Hedberg84794e12013-11-06 11:24:57 +0200804static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800805{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200806 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300807 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200808
Johan Hedberg84794e12013-11-06 11:24:57 +0200809 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800810 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700811 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800812
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700813 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700814 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300815
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300816 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300817 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800818}
819
Brian Gix2b64d152011-12-21 16:12:12 -0800820#define JUST_WORKS 0x00
821#define JUST_CFM 0x01
822#define REQ_PASSKEY 0x02
823#define CFM_PASSKEY 0x03
824#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300825#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800826#define OVERLAP 0xFF
827
828static const u8 gen_method[5][5] = {
829 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
830 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
831 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
832 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
833 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
834};
835
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300836static const u8 sc_method[5][5] = {
837 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
838 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
839 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
840 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
841 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
842};
843
Johan Hedberg581370c2014-06-17 13:07:38 +0300844static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
845{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300846 /* If either side has unknown io_caps, use JUST_CFM (which gets
847 * converted later to JUST_WORKS if we're initiators.
848 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300849 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
850 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300851 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300852
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300853 if (test_bit(SMP_FLAG_SC, &smp->flags))
854 return sc_method[remote_io][local_io];
855
Johan Hedberg581370c2014-06-17 13:07:38 +0300856 return gen_method[remote_io][local_io];
857}
858
Brian Gix2b64d152011-12-21 16:12:12 -0800859static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
860 u8 local_io, u8 remote_io)
861{
862 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300863 struct l2cap_chan *chan = conn->smp;
864 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800865 u32 passkey = 0;
866 int ret = 0;
867
868 /* Initialize key for JUST WORKS */
869 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300870 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800871
872 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
873
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300874 /* If neither side wants MITM, either "just" confirm an incoming
875 * request or use just-works for outgoing ones. The JUST_CFM
876 * will be converted to JUST_WORKS if necessary later in this
877 * function. If either side has MITM look up the method from the
878 * table.
879 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300880 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300881 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800882 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300883 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800884
Johan Hedberga82505c2014-03-24 14:39:07 +0200885 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300886 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
887 &smp->flags))
888 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200889
Johan Hedberg02f3e252014-07-16 15:09:13 +0300890 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300891 if (smp->method == JUST_CFM &&
892 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
893 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300894
Brian Gix2b64d152011-12-21 16:12:12 -0800895 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300896 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300897 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800898 return 0;
899 }
900
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200901 /* If this function is used for SC -> legacy fallback we
902 * can only recover the just-works case.
903 */
904 if (test_bit(SMP_FLAG_SC, &smp->flags))
905 return -EINVAL;
906
Brian Gix2b64d152011-12-21 16:12:12 -0800907 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300908 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300909 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300910 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
911 hcon->pending_sec_level = BT_SECURITY_HIGH;
912 }
Brian Gix2b64d152011-12-21 16:12:12 -0800913
914 /* If both devices have Keyoard-Display I/O, the master
915 * Confirms and the slave Enters the passkey.
916 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300917 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300918 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300919 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800920 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300921 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800922 }
923
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200924 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300925 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200926 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800927 get_random_bytes(&passkey, sizeof(passkey));
928 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200929 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800930 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300931 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800932 }
933
Johan Hedberg783e0572014-05-31 18:48:26 +0300934 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700935 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200936 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300937 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200938 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
939 hcon->type, hcon->dst_type,
940 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800941 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200942 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200943 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200944 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800945
Brian Gix2b64d152011-12-21 16:12:12 -0800946 return ret;
947}
948
Johan Hedberg1cc61142014-05-20 09:45:52 +0300949static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300950{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300951 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300952 struct smp_cmd_pairing_confirm cp;
953 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300954
955 BT_DBG("conn %p", conn);
956
Johan Hedberge491eaf2014-10-25 21:15:37 +0200957 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200958 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200959 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
960 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300961 if (ret)
962 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300963
Johan Hedberg4a74d652014-05-20 09:45:50 +0300964 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800965
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300966 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
967
Johan Hedbergb28b4942014-09-05 22:19:55 +0300968 if (conn->hcon->out)
969 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
970 else
971 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
972
Johan Hedberg1cc61142014-05-20 09:45:52 +0300973 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300974}
975
Johan Hedberg861580a2014-05-20 09:45:51 +0300976static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300977{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300978 struct l2cap_conn *conn = smp->conn;
979 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300980 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300981 int ret;
982
Johan Hedbergec70f362014-06-27 14:23:04 +0300983 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300984 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300985
986 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
987
Johan Hedberge491eaf2014-10-25 21:15:37 +0200988 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200989 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200990 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300991 if (ret)
992 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300993
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300994 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
995 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300996 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300997 }
998
999 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001000 u8 stk[16];
1001 __le64 rand = 0;
1002 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001003
Johan Hedberge491eaf2014-10-25 21:15:37 +02001004 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001005
Johan Hedberg861580a2014-05-20 09:45:51 +03001006 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1007 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001008
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001009 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001010 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001011 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001012 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001013 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001014 __le64 rand = 0;
1015 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001016
Johan Hedberg943a7322014-03-18 12:58:24 +02001017 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1018 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001019
Johan Hedberge491eaf2014-10-25 21:15:37 +02001020 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001021
Johan Hedbergfff34902014-06-10 15:19:50 +03001022 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1023 auth = 1;
1024 else
1025 auth = 0;
1026
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001027 /* Even though there's no _SLAVE suffix this is the
1028 * slave STK we're adding for later lookup (the master
1029 * STK never needs to be stored).
1030 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001031 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001032 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001033 }
1034
Johan Hedberg861580a2014-05-20 09:45:51 +03001035 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001036}
1037
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001038static void smp_notify_keys(struct l2cap_conn *conn)
1039{
1040 struct l2cap_chan *chan = conn->smp;
1041 struct smp_chan *smp = chan->data;
1042 struct hci_conn *hcon = conn->hcon;
1043 struct hci_dev *hdev = hcon->hdev;
1044 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1045 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1046 bool persistent;
1047
1048 if (smp->remote_irk) {
1049 mgmt_new_irk(hdev, smp->remote_irk);
1050 /* Now that user space can be considered to know the
1051 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001052 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001053 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001054 if (hcon->type == LE_LINK) {
1055 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1056 hcon->dst_type = smp->remote_irk->addr_type;
1057 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1058 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001059
1060 /* When receiving an indentity resolving key for
1061 * a remote device that does not use a resolvable
1062 * private address, just remove the key so that
1063 * it is possible to use the controller white
1064 * list for scanning.
1065 *
1066 * Userspace will have been told to not store
1067 * this key at this point. So it is safe to
1068 * just remove it.
1069 */
1070 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +02001071 list_del_rcu(&smp->remote_irk->list);
1072 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001073 smp->remote_irk = NULL;
1074 }
1075 }
1076
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001077 if (hcon->type == ACL_LINK) {
1078 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1079 persistent = false;
1080 else
1081 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1082 &hcon->flags);
1083 } else {
1084 /* The LTKs and CSRKs should be persistent only if both sides
1085 * had the bonding bit set in their authentication requests.
1086 */
1087 persistent = !!((req->auth_req & rsp->auth_req) &
1088 SMP_AUTH_BONDING);
1089 }
1090
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001091
1092 if (smp->csrk) {
1093 smp->csrk->bdaddr_type = hcon->dst_type;
1094 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1095 mgmt_new_csrk(hdev, smp->csrk, persistent);
1096 }
1097
1098 if (smp->slave_csrk) {
1099 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1100 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1101 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1102 }
1103
1104 if (smp->ltk) {
1105 smp->ltk->bdaddr_type = hcon->dst_type;
1106 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1107 mgmt_new_ltk(hdev, smp->ltk, persistent);
1108 }
1109
1110 if (smp->slave_ltk) {
1111 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1112 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1113 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1114 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001115
1116 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001117 struct link_key *key;
1118 u8 type;
1119
1120 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1121 type = HCI_LK_DEBUG_COMBINATION;
1122 else if (hcon->sec_level == BT_SECURITY_FIPS)
1123 type = HCI_LK_AUTH_COMBINATION_P256;
1124 else
1125 type = HCI_LK_UNAUTH_COMBINATION_P256;
1126
1127 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1128 smp->link_key, type, 0, &persistent);
1129 if (key) {
1130 mgmt_new_link_key(hdev, key, persistent);
1131
1132 /* Don't keep debug keys around if the relevant
1133 * flag is not set.
1134 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001135 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001136 key->type == HCI_LK_DEBUG_COMBINATION) {
1137 list_del_rcu(&key->list);
1138 kfree_rcu(key, rcu);
1139 }
1140 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001141 }
1142}
1143
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001144static void sc_add_ltk(struct smp_chan *smp)
1145{
1146 struct hci_conn *hcon = smp->conn->hcon;
1147 u8 key_type, auth;
1148
1149 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1150 key_type = SMP_LTK_P256_DEBUG;
1151 else
1152 key_type = SMP_LTK_P256;
1153
1154 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1155 auth = 1;
1156 else
1157 auth = 0;
1158
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001159 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1160 key_type, auth, smp->tk, smp->enc_key_size,
1161 0, 0);
1162}
1163
Johan Hedberg6a770832014-06-06 11:54:04 +03001164static void sc_generate_link_key(struct smp_chan *smp)
1165{
1166 /* These constants are as specified in the core specification.
1167 * In ASCII they spell out to 'tmp1' and 'lebr'.
1168 */
1169 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1170 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1171
1172 smp->link_key = kzalloc(16, GFP_KERNEL);
1173 if (!smp->link_key)
1174 return;
1175
1176 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001177 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001178 smp->link_key = NULL;
1179 return;
1180 }
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{
1205 /* These constants are as specified in the core specification.
1206 * In ASCII they spell out to 'tmp2' and 'brle'.
1207 */
1208 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1209 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1210 struct hci_conn *hcon = smp->conn->hcon;
1211 struct hci_dev *hdev = hcon->hdev;
1212 struct link_key *key;
1213
1214 key = hci_find_link_key(hdev, &hcon->dst);
1215 if (!key) {
1216 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1217 return;
1218 }
1219
1220 if (key->type == HCI_LK_DEBUG_COMBINATION)
1221 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1222
1223 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1224 return;
1225
1226 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1227 return;
1228
1229 sc_add_ltk(smp);
1230}
1231
Johan Hedbergd6268e82014-09-05 22:19:51 +03001232static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001233{
1234 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001235 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001236 struct hci_conn *hcon = conn->hcon;
1237 struct hci_dev *hdev = hcon->hdev;
1238 __u8 *keydist;
1239
1240 BT_DBG("conn %p", conn);
1241
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001242 rsp = (void *) &smp->prsp[1];
1243
1244 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001245 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1246 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001247 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001248 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001249
1250 req = (void *) &smp->preq[1];
1251
1252 if (hcon->out) {
1253 keydist = &rsp->init_key_dist;
1254 *keydist &= req->init_key_dist;
1255 } else {
1256 keydist = &rsp->resp_key_dist;
1257 *keydist &= req->resp_key_dist;
1258 }
1259
Johan Hedberg6a770832014-06-06 11:54:04 +03001260 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001261 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001262 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001263 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1264 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001265
1266 /* Clear the keys which are generated but not distributed */
1267 *keydist &= ~SMP_SC_NO_DIST;
1268 }
1269
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001270 BT_DBG("keydist 0x%x", *keydist);
1271
1272 if (*keydist & SMP_DIST_ENC_KEY) {
1273 struct smp_cmd_encrypt_info enc;
1274 struct smp_cmd_master_ident ident;
1275 struct smp_ltk *ltk;
1276 u8 authenticated;
1277 __le16 ediv;
1278 __le64 rand;
1279
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001280 /* Make sure we generate only the significant amount of
1281 * bytes based on the encryption key size, and set the rest
1282 * of the value to zeroes.
1283 */
1284 get_random_bytes(enc.ltk, smp->enc_key_size);
1285 memset(enc.ltk + smp->enc_key_size, 0,
1286 sizeof(enc.ltk) - smp->enc_key_size);
1287
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001288 get_random_bytes(&ediv, sizeof(ediv));
1289 get_random_bytes(&rand, sizeof(rand));
1290
1291 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1292
1293 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1294 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1295 SMP_LTK_SLAVE, authenticated, enc.ltk,
1296 smp->enc_key_size, ediv, rand);
1297 smp->slave_ltk = ltk;
1298
1299 ident.ediv = ediv;
1300 ident.rand = rand;
1301
1302 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1303
1304 *keydist &= ~SMP_DIST_ENC_KEY;
1305 }
1306
1307 if (*keydist & SMP_DIST_ID_KEY) {
1308 struct smp_cmd_ident_addr_info addrinfo;
1309 struct smp_cmd_ident_info idinfo;
1310
1311 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1312
1313 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1314
1315 /* The hci_conn contains the local identity address
1316 * after the connection has been established.
1317 *
1318 * This is true even when the connection has been
1319 * established using a resolvable random address.
1320 */
1321 bacpy(&addrinfo.bdaddr, &hcon->src);
1322 addrinfo.addr_type = hcon->src_type;
1323
1324 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1325 &addrinfo);
1326
1327 *keydist &= ~SMP_DIST_ID_KEY;
1328 }
1329
1330 if (*keydist & SMP_DIST_SIGN) {
1331 struct smp_cmd_sign_info sign;
1332 struct smp_csrk *csrk;
1333
1334 /* Generate a new random key */
1335 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1336
1337 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1338 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001339 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1340 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1341 else
1342 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001343 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1344 }
1345 smp->slave_csrk = csrk;
1346
1347 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1348
1349 *keydist &= ~SMP_DIST_SIGN;
1350 }
1351
1352 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001353 if (smp->remote_key_dist & KEY_DIST_MASK) {
1354 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001355 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001356 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001357
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001358 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1359 smp_notify_keys(conn);
1360
1361 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001362}
1363
Johan Hedbergb68fda62014-08-11 22:06:40 +03001364static void smp_timeout(struct work_struct *work)
1365{
1366 struct smp_chan *smp = container_of(work, struct smp_chan,
1367 security_timer.work);
1368 struct l2cap_conn *conn = smp->conn;
1369
1370 BT_DBG("conn %p", conn);
1371
Johan Hedberg1e91c292014-08-18 20:33:29 +03001372 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001373}
1374
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001375static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1376{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001377 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001378 struct smp_chan *smp;
1379
Marcel Holtmannf1560462013-10-13 05:43:25 -07001380 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001381 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001382 return NULL;
1383
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001384 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1385 if (IS_ERR(smp->tfm_aes)) {
1386 BT_ERR("Unable to create ECB crypto context");
Marcel Holtmann276812e2015-03-16 01:10:18 -07001387 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001388 return NULL;
1389 }
1390
Johan Hedberg407cecf2014-05-02 14:19:47 +03001391 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1392 if (IS_ERR(smp->tfm_cmac)) {
1393 BT_ERR("Unable to create CMAC crypto context");
1394 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann276812e2015-03-16 01:10:18 -07001395 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001396 return NULL;
1397 }
1398
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001399 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001400 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001401
Johan Hedbergb28b4942014-09-05 22:19:55 +03001402 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1403
Johan Hedbergb68fda62014-08-11 22:06:40 +03001404 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1405
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001406 hci_conn_hold(conn->hcon);
1407
1408 return smp;
1409}
1410
Johan Hedberg760b0182014-06-06 11:44:05 +03001411static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1412{
1413 struct hci_conn *hcon = smp->conn->hcon;
1414 u8 *na, *nb, a[7], b[7];
1415
1416 if (hcon->out) {
1417 na = smp->prnd;
1418 nb = smp->rrnd;
1419 } else {
1420 na = smp->rrnd;
1421 nb = smp->prnd;
1422 }
1423
1424 memcpy(a, &hcon->init_addr, 6);
1425 memcpy(b, &hcon->resp_addr, 6);
1426 a[6] = hcon->init_addr_type;
1427 b[6] = hcon->resp_addr_type;
1428
1429 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1430}
1431
Johan Hedberg38606f12014-06-25 11:10:28 +03001432static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001433{
1434 struct hci_conn *hcon = smp->conn->hcon;
1435 struct smp_cmd_dhkey_check check;
1436 u8 a[7], b[7], *local_addr, *remote_addr;
1437 u8 io_cap[3], r[16];
1438
Johan Hedberg760b0182014-06-06 11:44:05 +03001439 memcpy(a, &hcon->init_addr, 6);
1440 memcpy(b, &hcon->resp_addr, 6);
1441 a[6] = hcon->init_addr_type;
1442 b[6] = hcon->resp_addr_type;
1443
1444 if (hcon->out) {
1445 local_addr = a;
1446 remote_addr = b;
1447 memcpy(io_cap, &smp->preq[1], 3);
1448 } else {
1449 local_addr = b;
1450 remote_addr = a;
1451 memcpy(io_cap, &smp->prsp[1], 3);
1452 }
1453
Johan Hedbergdddd3052014-06-01 15:38:09 +03001454 memset(r, 0, sizeof(r));
1455
1456 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001457 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001458
Johan Hedberga29b0732014-10-28 15:17:05 +01001459 if (smp->method == REQ_OOB)
1460 memcpy(r, smp->rr, 16);
1461
Johan Hedberg760b0182014-06-06 11:44:05 +03001462 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1463 local_addr, remote_addr, check.e);
1464
1465 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001466}
1467
Johan Hedberg38606f12014-06-25 11:10:28 +03001468static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1469{
1470 struct l2cap_conn *conn = smp->conn;
1471 struct hci_conn *hcon = conn->hcon;
1472 struct smp_cmd_pairing_confirm cfm;
1473 u8 r;
1474
1475 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1476 r |= 0x80;
1477
1478 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1479
1480 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1481 cfm.confirm_val))
1482 return SMP_UNSPECIFIED;
1483
1484 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1485
1486 return 0;
1487}
1488
1489static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1490{
1491 struct l2cap_conn *conn = smp->conn;
1492 struct hci_conn *hcon = conn->hcon;
1493 struct hci_dev *hdev = hcon->hdev;
1494 u8 cfm[16], r;
1495
1496 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1497 if (smp->passkey_round >= 20)
1498 return 0;
1499
1500 switch (smp_op) {
1501 case SMP_CMD_PAIRING_RANDOM:
1502 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1503 r |= 0x80;
1504
1505 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1506 smp->rrnd, r, cfm))
1507 return SMP_UNSPECIFIED;
1508
1509 if (memcmp(smp->pcnf, cfm, 16))
1510 return SMP_CONFIRM_FAILED;
1511
1512 smp->passkey_round++;
1513
1514 if (smp->passkey_round == 20) {
1515 /* Generate MacKey and LTK */
1516 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1517 return SMP_UNSPECIFIED;
1518 }
1519
1520 /* The round is only complete when the initiator
1521 * receives pairing random.
1522 */
1523 if (!hcon->out) {
1524 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1525 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001526 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001527 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001528 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001529 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001530 return 0;
1531 }
1532
1533 /* Start the next round */
1534 if (smp->passkey_round != 20)
1535 return sc_passkey_round(smp, 0);
1536
1537 /* Passkey rounds are complete - start DHKey Check */
1538 sc_dhkey_check(smp);
1539 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1540
1541 break;
1542
1543 case SMP_CMD_PAIRING_CONFIRM:
1544 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1545 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1546 return 0;
1547 }
1548
1549 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1550
1551 if (hcon->out) {
1552 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1553 sizeof(smp->prnd), smp->prnd);
1554 return 0;
1555 }
1556
1557 return sc_passkey_send_confirm(smp);
1558
1559 case SMP_CMD_PUBLIC_KEY:
1560 default:
1561 /* Initiating device starts the round */
1562 if (!hcon->out)
1563 return 0;
1564
1565 BT_DBG("%s Starting passkey round %u", hdev->name,
1566 smp->passkey_round + 1);
1567
1568 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1569
1570 return sc_passkey_send_confirm(smp);
1571 }
1572
1573 return 0;
1574}
1575
Johan Hedbergdddd3052014-06-01 15:38:09 +03001576static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1577{
Johan Hedberg38606f12014-06-25 11:10:28 +03001578 struct l2cap_conn *conn = smp->conn;
1579 struct hci_conn *hcon = conn->hcon;
1580 u8 smp_op;
1581
1582 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1583
Johan Hedbergdddd3052014-06-01 15:38:09 +03001584 switch (mgmt_op) {
1585 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1586 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1587 return 0;
1588 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1589 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1590 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001591 case MGMT_OP_USER_PASSKEY_REPLY:
1592 hcon->passkey_notify = le32_to_cpu(passkey);
1593 smp->passkey_round = 0;
1594
1595 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1596 smp_op = SMP_CMD_PAIRING_CONFIRM;
1597 else
1598 smp_op = 0;
1599
1600 if (sc_passkey_round(smp, smp_op))
1601 return -EIO;
1602
1603 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001604 }
1605
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001606 /* Initiator sends DHKey check first */
1607 if (hcon->out) {
1608 sc_dhkey_check(smp);
1609 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1610 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1611 sc_dhkey_check(smp);
1612 sc_add_ltk(smp);
1613 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001614
1615 return 0;
1616}
1617
Brian Gix2b64d152011-12-21 16:12:12 -08001618int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1619{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001620 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001621 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001622 struct smp_chan *smp;
1623 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001624 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001625
1626 BT_DBG("");
1627
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001628 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001629 return -ENOTCONN;
1630
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001631 chan = conn->smp;
1632 if (!chan)
1633 return -ENOTCONN;
1634
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001635 l2cap_chan_lock(chan);
1636 if (!chan->data) {
1637 err = -ENOTCONN;
1638 goto unlock;
1639 }
1640
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001641 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001642
Johan Hedberg760b0182014-06-06 11:44:05 +03001643 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1644 err = sc_user_reply(smp, mgmt_op, passkey);
1645 goto unlock;
1646 }
1647
Brian Gix2b64d152011-12-21 16:12:12 -08001648 switch (mgmt_op) {
1649 case MGMT_OP_USER_PASSKEY_REPLY:
1650 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001651 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001652 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001653 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001654 /* Fall Through */
1655 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001656 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001657 break;
1658 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1659 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001660 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001661 err = 0;
1662 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001663 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001664 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001665 err = -EOPNOTSUPP;
1666 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001667 }
1668
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001669 err = 0;
1670
Brian Gix2b64d152011-12-21 16:12:12 -08001671 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001672 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1673 u8 rsp = smp_confirm(smp);
1674 if (rsp)
1675 smp_failure(conn, rsp);
1676 }
Brian Gix2b64d152011-12-21 16:12:12 -08001677
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001678unlock:
1679 l2cap_chan_unlock(chan);
1680 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001681}
1682
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001683static void build_bredr_pairing_cmd(struct smp_chan *smp,
1684 struct smp_cmd_pairing *req,
1685 struct smp_cmd_pairing *rsp)
1686{
1687 struct l2cap_conn *conn = smp->conn;
1688 struct hci_dev *hdev = conn->hcon->hdev;
1689 u8 local_dist = 0, remote_dist = 0;
1690
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001691 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001692 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1693 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1694 }
1695
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001696 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001697 remote_dist |= SMP_DIST_ID_KEY;
1698
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001699 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001700 local_dist |= SMP_DIST_ID_KEY;
1701
1702 if (!rsp) {
1703 memset(req, 0, sizeof(*req));
1704
1705 req->init_key_dist = local_dist;
1706 req->resp_key_dist = remote_dist;
1707 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1708
1709 smp->remote_key_dist = remote_dist;
1710
1711 return;
1712 }
1713
1714 memset(rsp, 0, sizeof(*rsp));
1715
1716 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1717 rsp->init_key_dist = req->init_key_dist & remote_dist;
1718 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1719
1720 smp->remote_key_dist = rsp->init_key_dist;
1721}
1722
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001723static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001724{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001725 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001726 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001727 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001728 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001729 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001730 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001731
1732 BT_DBG("conn %p", conn);
1733
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001734 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001735 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001736
Johan Hedberg40bef302014-07-16 11:42:27 +03001737 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001738 return SMP_CMD_NOTSUPP;
1739
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001740 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001741 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001742 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001743 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001744
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001745 if (!smp)
1746 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001747
Johan Hedbergc05b9332014-09-10 17:37:42 -07001748 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001749 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001750
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001751 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001752 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001753 return SMP_PAIRING_NOTSUPP;
1754
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001755 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001756 return SMP_AUTH_REQUIREMENTS;
1757
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001758 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1759 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001760 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001761
Johan Hedbergcb06d362015-03-16 21:12:34 +02001762 /* If the remote side's OOB flag is set it means it has
1763 * successfully received our local OOB data - therefore set the
1764 * flag to indicate that local OOB is in use.
1765 */
Johan Hedberg58428562015-03-16 11:45:45 +02001766 if (req->oob_flag == SMP_OOB_PRESENT)
1767 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1768
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001769 /* SMP over BR/EDR requires special treatment */
1770 if (conn->hcon->type == ACL_LINK) {
1771 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001772 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001773 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001774 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1775
1776 set_bit(SMP_FLAG_SC, &smp->flags);
1777
1778 build_bredr_pairing_cmd(smp, req, &rsp);
1779
1780 key_size = min(req->max_key_size, rsp.max_key_size);
1781 if (check_enc_key_size(conn, key_size))
1782 return SMP_ENC_KEY_SIZE;
1783
1784 /* Clear bits which are generated but not distributed */
1785 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1786
1787 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1788 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1789 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1790
1791 smp_distribute_keys(smp);
1792 return 0;
1793 }
1794
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001795 build_pairing_cmd(conn, req, &rsp, auth);
1796
1797 if (rsp.auth_req & SMP_AUTH_SC)
1798 set_bit(SMP_FLAG_SC, &smp->flags);
1799
Johan Hedberg5be5e272014-09-10 17:58:54 -07001800 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001801 sec_level = BT_SECURITY_MEDIUM;
1802 else
1803 sec_level = authreq_to_seclevel(auth);
1804
Johan Hedbergc7262e72014-06-17 13:07:37 +03001805 if (sec_level > conn->hcon->pending_sec_level)
1806 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001807
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001808 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001809 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1810 u8 method;
1811
1812 method = get_auth_method(smp, conn->hcon->io_capability,
1813 req->io_capability);
1814 if (method == JUST_WORKS || method == JUST_CFM)
1815 return SMP_AUTH_REQUIREMENTS;
1816 }
1817
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001818 key_size = min(req->max_key_size, rsp.max_key_size);
1819 if (check_enc_key_size(conn, key_size))
1820 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001821
Johan Hedberge84a6b12013-12-02 10:49:03 +02001822 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001823
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001824 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1825 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001826
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001827 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001828
1829 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1830
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001831 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1832 * SC case, however some implementations incorrectly copy RFU auth
1833 * req bits from our security request, which may create a false
1834 * positive SC enablement.
1835 */
1836 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1837
Johan Hedberg3b191462014-06-06 10:50:15 +03001838 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1839 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1840 /* Clear bits which are generated but not distributed */
1841 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1842 /* Wait for Public Key from Initiating Device */
1843 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001844 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001845
Brian Gix2b64d152011-12-21 16:12:12 -08001846 /* Request setup of TK */
1847 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1848 if (ret)
1849 return SMP_UNSPECIFIED;
1850
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001851 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001852}
1853
Johan Hedberg3b191462014-06-06 10:50:15 +03001854static u8 sc_send_public_key(struct smp_chan *smp)
1855{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001856 struct hci_dev *hdev = smp->conn->hcon->hdev;
1857
Johan Hedberg3b191462014-06-06 10:50:15 +03001858 BT_DBG("");
1859
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001860 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001861 struct l2cap_chan *chan = hdev->smp_data;
1862 struct smp_dev *smp_dev;
1863
1864 if (!chan || !chan->data)
1865 return SMP_UNSPECIFIED;
1866
1867 smp_dev = chan->data;
1868
1869 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1870 memcpy(smp->local_sk, smp_dev->local_sk, 32);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001871 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001872
1873 if (smp_dev->debug_key)
1874 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1875
1876 goto done;
1877 }
1878
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001879 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001880 BT_DBG("Using debug keys");
1881 memcpy(smp->local_pk, debug_pk, 64);
1882 memcpy(smp->local_sk, debug_sk, 32);
1883 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1884 } else {
1885 while (true) {
1886 /* Generate local key pair for Secure Connections */
1887 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1888 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001889
Johan Hedberg70157ef2014-06-24 15:22:59 +03001890 /* This is unlikely, but we need to check that
1891 * we didn't accidentially generate a debug key.
1892 */
1893 if (memcmp(smp->local_sk, debug_sk, 32))
1894 break;
1895 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001896 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001897
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001898done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001899 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001900 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001901 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001902
1903 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1904
1905 return 0;
1906}
1907
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001908static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001909{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001910 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001911 struct l2cap_chan *chan = conn->smp;
1912 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001913 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001914 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001915 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001916
1917 BT_DBG("conn %p", conn);
1918
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001919 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001920 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001921
Johan Hedberg40bef302014-07-16 11:42:27 +03001922 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001923 return SMP_CMD_NOTSUPP;
1924
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001925 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001926
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001927 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001928
1929 key_size = min(req->max_key_size, rsp->max_key_size);
1930 if (check_enc_key_size(conn, key_size))
1931 return SMP_ENC_KEY_SIZE;
1932
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001933 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001934
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001935 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001936 return SMP_AUTH_REQUIREMENTS;
1937
Johan Hedbergcb06d362015-03-16 21:12:34 +02001938 /* If the remote side's OOB flag is set it means it has
1939 * successfully received our local OOB data - therefore set the
1940 * flag to indicate that local OOB is in use.
1941 */
Johan Hedberg58428562015-03-16 11:45:45 +02001942 if (rsp->oob_flag == SMP_OOB_PRESENT)
1943 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1944
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001945 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1946 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1947
1948 /* Update remote key distribution in case the remote cleared
1949 * some bits that we had enabled in our request.
1950 */
1951 smp->remote_key_dist &= rsp->resp_key_dist;
1952
1953 /* For BR/EDR this means we're done and can start phase 3 */
1954 if (conn->hcon->type == ACL_LINK) {
1955 /* Clear bits which are generated but not distributed */
1956 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1957 smp_distribute_keys(smp);
1958 return 0;
1959 }
1960
Johan Hedberg65668772014-05-16 11:03:34 +03001961 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1962 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001963 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1964 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001965
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001966 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001967 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1968 u8 method;
1969
1970 method = get_auth_method(smp, req->io_capability,
1971 rsp->io_capability);
1972 if (method == JUST_WORKS || method == JUST_CFM)
1973 return SMP_AUTH_REQUIREMENTS;
1974 }
1975
Johan Hedberge84a6b12013-12-02 10:49:03 +02001976 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001977
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001978 /* Update remote key distribution in case the remote cleared
1979 * some bits that we had enabled in our request.
1980 */
1981 smp->remote_key_dist &= rsp->resp_key_dist;
1982
Johan Hedberg3b191462014-06-06 10:50:15 +03001983 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1984 /* Clear bits which are generated but not distributed */
1985 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1986 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1987 return sc_send_public_key(smp);
1988 }
1989
Johan Hedbergc05b9332014-09-10 17:37:42 -07001990 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001991
Johan Hedberg476585e2012-06-06 18:54:15 +08001992 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001993 if (ret)
1994 return SMP_UNSPECIFIED;
1995
Johan Hedberg4a74d652014-05-20 09:45:50 +03001996 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001997
1998 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001999 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002000 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002001
2002 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002003}
2004
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002005static u8 sc_check_confirm(struct smp_chan *smp)
2006{
2007 struct l2cap_conn *conn = smp->conn;
2008
2009 BT_DBG("");
2010
Johan Hedberg38606f12014-06-25 11:10:28 +03002011 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2012 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2013
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002014 if (conn->hcon->out) {
2015 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2016 smp->prnd);
2017 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2018 }
2019
2020 return 0;
2021}
2022
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002023/* Work-around for some implementations that incorrectly copy RFU bits
2024 * from our security request and thereby create the impression that
2025 * we're doing SC when in fact the remote doesn't support it.
2026 */
2027static int fixup_sc_false_positive(struct smp_chan *smp)
2028{
2029 struct l2cap_conn *conn = smp->conn;
2030 struct hci_conn *hcon = conn->hcon;
2031 struct hci_dev *hdev = hcon->hdev;
2032 struct smp_cmd_pairing *req, *rsp;
2033 u8 auth;
2034
2035 /* The issue is only observed when we're in slave role */
2036 if (hcon->out)
2037 return SMP_UNSPECIFIED;
2038
2039 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
2040 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2041 return SMP_UNSPECIFIED;
2042 }
2043
2044 BT_ERR("Trying to fall back to legacy SMP");
2045
2046 req = (void *) &smp->preq[1];
2047 rsp = (void *) &smp->prsp[1];
2048
2049 /* Rebuild key dist flags which may have been cleared for SC */
2050 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2051
2052 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2053
2054 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
2055 BT_ERR("Failed to fall back to legacy SMP");
2056 return SMP_UNSPECIFIED;
2057 }
2058
2059 clear_bit(SMP_FLAG_SC, &smp->flags);
2060
2061 return 0;
2062}
2063
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002064static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002065{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002066 struct l2cap_chan *chan = conn->smp;
2067 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002068
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002069 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2070
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002071 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002072 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002073
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002074 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2075 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002076
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002077 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2078 int ret;
2079
2080 /* Public Key exchange must happen before any other steps */
2081 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2082 return sc_check_confirm(smp);
2083
2084 BT_ERR("Unexpected SMP Pairing Confirm");
2085
2086 ret = fixup_sc_false_positive(smp);
2087 if (ret)
2088 return ret;
2089 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002090
Johan Hedbergb28b4942014-09-05 22:19:55 +03002091 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002092 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2093 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002094 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2095 return 0;
2096 }
2097
2098 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002099 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002100
2101 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002102
2103 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002104}
2105
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002106static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002107{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002108 struct l2cap_chan *chan = conn->smp;
2109 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002110 struct hci_conn *hcon = conn->hcon;
2111 u8 *pkax, *pkbx, *na, *nb;
2112 u32 passkey;
2113 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002114
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002115 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002116
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002117 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002118 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002119
Johan Hedberg943a7322014-03-18 12:58:24 +02002120 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002121 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002122
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002123 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2124 return smp_random(smp);
2125
Johan Hedberg580039e2014-12-03 16:26:37 +02002126 if (hcon->out) {
2127 pkax = smp->local_pk;
2128 pkbx = smp->remote_pk;
2129 na = smp->prnd;
2130 nb = smp->rrnd;
2131 } else {
2132 pkax = smp->remote_pk;
2133 pkbx = smp->local_pk;
2134 na = smp->rrnd;
2135 nb = smp->prnd;
2136 }
2137
Johan Hedberga29b0732014-10-28 15:17:05 +01002138 if (smp->method == REQ_OOB) {
2139 if (!hcon->out)
2140 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2141 sizeof(smp->prnd), smp->prnd);
2142 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2143 goto mackey_and_ltk;
2144 }
2145
Johan Hedberg38606f12014-06-25 11:10:28 +03002146 /* Passkey entry has special treatment */
2147 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2148 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2149
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002150 if (hcon->out) {
2151 u8 cfm[16];
2152
2153 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2154 smp->rrnd, 0, cfm);
2155 if (err)
2156 return SMP_UNSPECIFIED;
2157
2158 if (memcmp(smp->pcnf, cfm, 16))
2159 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002160 } else {
2161 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2162 smp->prnd);
2163 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002164 }
2165
Johan Hedberga29b0732014-10-28 15:17:05 +01002166mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002167 /* Generate MacKey and LTK */
2168 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2169 if (err)
2170 return SMP_UNSPECIFIED;
2171
Johan Hedberga29b0732014-10-28 15:17:05 +01002172 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002173 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002174 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002175 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2176 }
2177 return 0;
2178 }
2179
Johan Hedberg38606f12014-06-25 11:10:28 +03002180 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002181 if (err)
2182 return SMP_UNSPECIFIED;
2183
Johan Hedberg38606f12014-06-25 11:10:28 +03002184 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2185 hcon->dst_type, passkey, 0);
2186 if (err)
2187 return SMP_UNSPECIFIED;
2188
2189 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2190
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002191 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002192}
2193
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002194static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002195{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002196 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002197 struct hci_conn *hcon = conn->hcon;
2198
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002199 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002200 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002201 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002202
Johan Hedberga6f78332014-09-10 17:37:45 -07002203 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002204 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002205
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002206 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002207 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002208
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002209 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002210 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002211
Johan Hedbergfe59a052014-07-01 19:14:12 +03002212 /* We never store STKs for master role, so clear this flag */
2213 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2214
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002215 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002216}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002217
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002218bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2219 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002220{
2221 if (sec_level == BT_SECURITY_LOW)
2222 return true;
2223
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002224 /* If we're encrypted with an STK but the caller prefers using
2225 * LTK claim insufficient security. This way we allow the
2226 * connection to be re-encrypted with an LTK, even if the LTK
2227 * provides the same level of security. Only exception is if we
2228 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002229 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002230 if (key_pref == SMP_USE_LTK &&
2231 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002232 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002233 return false;
2234
Johan Hedberg854f4722014-07-01 18:40:20 +03002235 if (hcon->sec_level >= sec_level)
2236 return true;
2237
2238 return false;
2239}
2240
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002241static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002242{
2243 struct smp_cmd_security_req *rp = (void *) skb->data;
2244 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002245 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002246 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002247 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002248 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002249
2250 BT_DBG("conn %p", conn);
2251
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002252 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002253 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002254
Johan Hedberg40bef302014-07-16 11:42:27 +03002255 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002256 return SMP_CMD_NOTSUPP;
2257
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002258 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002259
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002260 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002261 return SMP_AUTH_REQUIREMENTS;
2262
Johan Hedberg5be5e272014-09-10 17:58:54 -07002263 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002264 sec_level = BT_SECURITY_MEDIUM;
2265 else
2266 sec_level = authreq_to_seclevel(auth);
2267
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002268 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002269 return 0;
2270
Johan Hedbergc7262e72014-06-17 13:07:37 +03002271 if (sec_level > hcon->pending_sec_level)
2272 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002273
Johan Hedberg4dab7862012-06-07 14:58:37 +08002274 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002275 return 0;
2276
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002277 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002278 if (!smp)
2279 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002280
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002281 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002282 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002283 return SMP_PAIRING_NOTSUPP;
2284
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002285 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002286
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002287 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002288 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002289
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002290 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2291 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002292
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002293 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002294 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002295
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002296 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002297}
2298
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002299int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002300{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002301 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002302 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002303 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002304 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002305 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002306
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002307 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2308
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002309 /* This may be NULL if there's an unexpected disconnection */
2310 if (!conn)
2311 return 1;
2312
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002313 chan = conn->smp;
2314
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002315 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002316 return 1;
2317
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002318 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002319 return 1;
2320
Johan Hedbergc7262e72014-06-17 13:07:37 +03002321 if (sec_level > hcon->pending_sec_level)
2322 hcon->pending_sec_level = sec_level;
2323
Johan Hedberg40bef302014-07-16 11:42:27 +03002324 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002325 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2326 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002327
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002328 l2cap_chan_lock(chan);
2329
2330 /* If SMP is already in progress ignore this request */
2331 if (chan->data) {
2332 ret = 0;
2333 goto unlock;
2334 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002335
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002336 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002337 if (!smp) {
2338 ret = 1;
2339 goto unlock;
2340 }
Brian Gix2b64d152011-12-21 16:12:12 -08002341
2342 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002343
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002344 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002345 authreq |= SMP_AUTH_SC;
2346
Johan Hedberg79897d22014-06-01 09:45:24 +03002347 /* Require MITM if IO Capability allows or the security level
2348 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002349 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002350 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002351 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002352 authreq |= SMP_AUTH_MITM;
2353
Johan Hedberg40bef302014-07-16 11:42:27 +03002354 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002355 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002356
Brian Gix2b64d152011-12-21 16:12:12 -08002357 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002358 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2359 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002360
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002361 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002362 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002363 } else {
2364 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002365 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002366 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002367 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002368 }
2369
Johan Hedberg4a74d652014-05-20 09:45:50 +03002370 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002371 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002372
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002373unlock:
2374 l2cap_chan_unlock(chan);
2375 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002376}
2377
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002378static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2379{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002380 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002381 struct l2cap_chan *chan = conn->smp;
2382 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002383
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002384 BT_DBG("conn %p", conn);
2385
2386 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002387 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002388
Johan Hedbergb28b4942014-09-05 22:19:55 +03002389 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002390
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002391 skb_pull(skb, sizeof(*rp));
2392
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002393 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002394
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002395 return 0;
2396}
2397
2398static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2399{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002400 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002401 struct l2cap_chan *chan = conn->smp;
2402 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002403 struct hci_dev *hdev = conn->hcon->hdev;
2404 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002405 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002406 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002407
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002408 BT_DBG("conn %p", conn);
2409
2410 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002411 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002412
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002413 /* Mark the information as received */
2414 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2415
Johan Hedbergb28b4942014-09-05 22:19:55 +03002416 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2417 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002418 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2419 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002420
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002421 skb_pull(skb, sizeof(*rp));
2422
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002423 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002424 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002425 authenticated, smp->tk, smp->enc_key_size,
2426 rp->ediv, rp->rand);
2427 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002428 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002429 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002430
2431 return 0;
2432}
2433
Johan Hedbergfd349c02014-02-18 10:19:36 +02002434static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2435{
2436 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002437 struct l2cap_chan *chan = conn->smp;
2438 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002439
2440 BT_DBG("");
2441
2442 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002443 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002444
Johan Hedbergb28b4942014-09-05 22:19:55 +03002445 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002446
Johan Hedbergfd349c02014-02-18 10:19:36 +02002447 skb_pull(skb, sizeof(*info));
2448
2449 memcpy(smp->irk, info->irk, 16);
2450
2451 return 0;
2452}
2453
2454static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2455 struct sk_buff *skb)
2456{
2457 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002458 struct l2cap_chan *chan = conn->smp;
2459 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002460 struct hci_conn *hcon = conn->hcon;
2461 bdaddr_t rpa;
2462
2463 BT_DBG("");
2464
2465 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002466 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002467
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002468 /* Mark the information as received */
2469 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2470
Johan Hedbergb28b4942014-09-05 22:19:55 +03002471 if (smp->remote_key_dist & SMP_DIST_SIGN)
2472 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2473
Johan Hedbergfd349c02014-02-18 10:19:36 +02002474 skb_pull(skb, sizeof(*info));
2475
Johan Hedberga9a58f82014-02-25 22:24:37 +02002476 /* Strictly speaking the Core Specification (4.1) allows sending
2477 * an empty address which would force us to rely on just the IRK
2478 * as "identity information". However, since such
2479 * implementations are not known of and in order to not over
2480 * complicate our implementation, simply pretend that we never
2481 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002482 *
2483 * The Identity Address must also be a Static Random or Public
2484 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002485 */
Johan Hedberge12af482015-01-14 20:51:37 +02002486 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2487 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002488 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002489 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002490 }
2491
Johan Hedbergfd349c02014-02-18 10:19:36 +02002492 bacpy(&smp->id_addr, &info->bdaddr);
2493 smp->id_addr_type = info->addr_type;
2494
2495 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2496 bacpy(&rpa, &hcon->dst);
2497 else
2498 bacpy(&rpa, BDADDR_ANY);
2499
Johan Hedberg23d0e122014-02-19 14:57:46 +02002500 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2501 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002502
Johan Hedberg31dd6242014-06-27 14:23:02 +03002503distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002504 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2505 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002506
2507 return 0;
2508}
2509
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002510static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2511{
2512 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002513 struct l2cap_chan *chan = conn->smp;
2514 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002515 struct smp_csrk *csrk;
2516
2517 BT_DBG("conn %p", conn);
2518
2519 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002520 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002521
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002522 /* Mark the information as received */
2523 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2524
2525 skb_pull(skb, sizeof(*rp));
2526
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002527 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2528 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002529 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2530 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2531 else
2532 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002533 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2534 }
2535 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002536 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002537
2538 return 0;
2539}
2540
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002541static u8 sc_select_method(struct smp_chan *smp)
2542{
2543 struct l2cap_conn *conn = smp->conn;
2544 struct hci_conn *hcon = conn->hcon;
2545 struct smp_cmd_pairing *local, *remote;
2546 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2547
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002548 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2549 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002550 return REQ_OOB;
2551
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002552 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2553 * which are needed as inputs to some crypto functions. To get
2554 * the "struct smp_cmd_pairing" from them we need to skip the
2555 * first byte which contains the opcode.
2556 */
2557 if (hcon->out) {
2558 local = (void *) &smp->preq[1];
2559 remote = (void *) &smp->prsp[1];
2560 } else {
2561 local = (void *) &smp->prsp[1];
2562 remote = (void *) &smp->preq[1];
2563 }
2564
2565 local_io = local->io_capability;
2566 remote_io = remote->io_capability;
2567
2568 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2569 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2570
2571 /* If either side wants MITM, look up the method from the table,
2572 * otherwise use JUST WORKS.
2573 */
2574 if (local_mitm || remote_mitm)
2575 method = get_auth_method(smp, local_io, remote_io);
2576 else
2577 method = JUST_WORKS;
2578
2579 /* Don't confirm locally initiated pairing attempts */
2580 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2581 method = JUST_WORKS;
2582
2583 return method;
2584}
2585
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002586static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2587{
2588 struct smp_cmd_public_key *key = (void *) skb->data;
2589 struct hci_conn *hcon = conn->hcon;
2590 struct l2cap_chan *chan = conn->smp;
2591 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002592 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002593 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002594 int err;
2595
2596 BT_DBG("conn %p", conn);
2597
2598 if (skb->len < sizeof(*key))
2599 return SMP_INVALID_PARAMS;
2600
2601 memcpy(smp->remote_pk, key, 64);
2602
Johan Hedberga8ca6172015-03-16 18:12:57 +02002603 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2604 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2605 smp->rr, 0, cfm.confirm_val);
2606 if (err)
2607 return SMP_UNSPECIFIED;
2608
2609 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2610 return SMP_CONFIRM_FAILED;
2611 }
2612
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002613 /* Non-initiating device sends its public key after receiving
2614 * the key from the initiating device.
2615 */
2616 if (!hcon->out) {
2617 err = sc_send_public_key(smp);
2618 if (err)
2619 return err;
2620 }
2621
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002622 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002623 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002624
2625 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2626 return SMP_UNSPECIFIED;
2627
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002628 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002629
2630 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2631
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002632 smp->method = sc_select_method(smp);
2633
2634 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2635
2636 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2637 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2638 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2639 else
2640 hcon->pending_sec_level = BT_SECURITY_FIPS;
2641
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002642 if (!memcmp(debug_pk, smp->remote_pk, 64))
2643 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2644
Johan Hedberg38606f12014-06-25 11:10:28 +03002645 if (smp->method == DSP_PASSKEY) {
2646 get_random_bytes(&hcon->passkey_notify,
2647 sizeof(hcon->passkey_notify));
2648 hcon->passkey_notify %= 1000000;
2649 hcon->passkey_entered = 0;
2650 smp->passkey_round = 0;
2651 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2652 hcon->dst_type,
2653 hcon->passkey_notify,
2654 hcon->passkey_entered))
2655 return SMP_UNSPECIFIED;
2656 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2657 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2658 }
2659
Johan Hedberg94ea7252015-03-16 11:45:46 +02002660 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002661 if (hcon->out)
2662 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2663 sizeof(smp->prnd), smp->prnd);
2664
2665 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2666
2667 return 0;
2668 }
2669
Johan Hedberg38606f12014-06-25 11:10:28 +03002670 if (hcon->out)
2671 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2672
2673 if (smp->method == REQ_PASSKEY) {
2674 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2675 hcon->dst_type))
2676 return SMP_UNSPECIFIED;
2677 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2678 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2679 return 0;
2680 }
2681
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002682 /* The Initiating device waits for the non-initiating device to
2683 * send the confirm value.
2684 */
2685 if (conn->hcon->out)
2686 return 0;
2687
2688 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2689 0, cfm.confirm_val);
2690 if (err)
2691 return SMP_UNSPECIFIED;
2692
2693 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2694 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2695
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002696 return 0;
2697}
2698
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002699static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2700{
2701 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2702 struct l2cap_chan *chan = conn->smp;
2703 struct hci_conn *hcon = conn->hcon;
2704 struct smp_chan *smp = chan->data;
2705 u8 a[7], b[7], *local_addr, *remote_addr;
2706 u8 io_cap[3], r[16], e[16];
2707 int err;
2708
2709 BT_DBG("conn %p", conn);
2710
2711 if (skb->len < sizeof(*check))
2712 return SMP_INVALID_PARAMS;
2713
2714 memcpy(a, &hcon->init_addr, 6);
2715 memcpy(b, &hcon->resp_addr, 6);
2716 a[6] = hcon->init_addr_type;
2717 b[6] = hcon->resp_addr_type;
2718
2719 if (hcon->out) {
2720 local_addr = a;
2721 remote_addr = b;
2722 memcpy(io_cap, &smp->prsp[1], 3);
2723 } else {
2724 local_addr = b;
2725 remote_addr = a;
2726 memcpy(io_cap, &smp->preq[1], 3);
2727 }
2728
2729 memset(r, 0, sizeof(r));
2730
Johan Hedberg38606f12014-06-25 11:10:28 +03002731 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2732 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002733 else if (smp->method == REQ_OOB)
2734 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002735
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002736 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2737 io_cap, remote_addr, local_addr, e);
2738 if (err)
2739 return SMP_UNSPECIFIED;
2740
2741 if (memcmp(check->e, e, 16))
2742 return SMP_DHKEY_CHECK_FAILED;
2743
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002744 if (!hcon->out) {
2745 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2746 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2747 return 0;
2748 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002749
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002750 /* Slave sends DHKey check as response to master */
2751 sc_dhkey_check(smp);
2752 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002753
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002754 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002755
2756 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002757 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002758 hcon->enc_key_size = smp->enc_key_size;
2759 }
2760
2761 return 0;
2762}
2763
Johan Hedberg1408bb62014-06-04 22:45:57 +03002764static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2765 struct sk_buff *skb)
2766{
2767 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2768
2769 BT_DBG("value 0x%02x", kp->value);
2770
2771 return 0;
2772}
2773
Johan Hedberg4befb862014-08-11 22:06:38 +03002774static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002775{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002776 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002777 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002778 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002779 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002780 int err = 0;
2781
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002782 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002783 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002784
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002785 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002786 reason = SMP_PAIRING_NOTSUPP;
2787 goto done;
2788 }
2789
Marcel Holtmann92381f52013-10-03 01:23:08 -07002790 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002791 skb_pull(skb, sizeof(code));
2792
Johan Hedbergb28b4942014-09-05 22:19:55 +03002793 smp = chan->data;
2794
2795 if (code > SMP_CMD_MAX)
2796 goto drop;
2797
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002798 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002799 goto drop;
2800
2801 /* If we don't have a context the only allowed commands are
2802 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002803 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002804 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2805 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002806
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002807 switch (code) {
2808 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002809 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002810 break;
2811
2812 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002813 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002814 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002815 break;
2816
2817 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002818 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002819 break;
2820
2821 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002822 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002823 break;
2824
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002825 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002826 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002827 break;
2828
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002829 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002830 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002831 break;
2832
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002833 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002834 reason = smp_cmd_encrypt_info(conn, skb);
2835 break;
2836
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002837 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002838 reason = smp_cmd_master_ident(conn, skb);
2839 break;
2840
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002841 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002842 reason = smp_cmd_ident_info(conn, skb);
2843 break;
2844
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002845 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002846 reason = smp_cmd_ident_addr_info(conn, skb);
2847 break;
2848
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002849 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002850 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002851 break;
2852
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002853 case SMP_CMD_PUBLIC_KEY:
2854 reason = smp_cmd_public_key(conn, skb);
2855 break;
2856
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002857 case SMP_CMD_DHKEY_CHECK:
2858 reason = smp_cmd_dhkey_check(conn, skb);
2859 break;
2860
Johan Hedberg1408bb62014-06-04 22:45:57 +03002861 case SMP_CMD_KEYPRESS_NOTIFY:
2862 reason = smp_cmd_keypress_notify(conn, skb);
2863 break;
2864
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002865 default:
2866 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002867 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002868 goto done;
2869 }
2870
2871done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002872 if (!err) {
2873 if (reason)
2874 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002875 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002876 }
2877
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002878 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002879
2880drop:
2881 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2882 code, &hcon->dst);
2883 kfree_skb(skb);
2884 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002885}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002886
Johan Hedberg70db83c2014-08-08 09:37:16 +03002887static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2888{
2889 struct l2cap_conn *conn = chan->conn;
2890
2891 BT_DBG("chan %p", chan);
2892
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002893 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002894 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002895
Johan Hedberg70db83c2014-08-08 09:37:16 +03002896 conn->smp = NULL;
2897 l2cap_chan_put(chan);
2898}
2899
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002900static void bredr_pairing(struct l2cap_chan *chan)
2901{
2902 struct l2cap_conn *conn = chan->conn;
2903 struct hci_conn *hcon = conn->hcon;
2904 struct hci_dev *hdev = hcon->hdev;
2905 struct smp_cmd_pairing req;
2906 struct smp_chan *smp;
2907
2908 BT_DBG("chan %p", chan);
2909
2910 /* Only new pairings are interesting */
2911 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2912 return;
2913
2914 /* Don't bother if we're not encrypted */
2915 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2916 return;
2917
2918 /* Only master may initiate SMP over BR/EDR */
2919 if (hcon->role != HCI_ROLE_MASTER)
2920 return;
2921
2922 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002923 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002924 return;
2925
2926 /* BR/EDR must use Secure Connections for SMP */
2927 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002928 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002929 return;
2930
2931 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002932 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002933 return;
2934
2935 /* Don't bother if remote LE support is not enabled */
2936 if (!lmp_host_le_capable(hcon))
2937 return;
2938
2939 /* Remote must support SMP fixed chan for BR/EDR */
2940 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2941 return;
2942
2943 /* Don't bother if SMP is already ongoing */
2944 if (chan->data)
2945 return;
2946
2947 smp = smp_chan_create(conn);
2948 if (!smp) {
2949 BT_ERR("%s unable to create SMP context for BR/EDR",
2950 hdev->name);
2951 return;
2952 }
2953
2954 set_bit(SMP_FLAG_SC, &smp->flags);
2955
2956 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2957
2958 /* Prepare and send the BR/EDR SMP Pairing Request */
2959 build_bredr_pairing_cmd(smp, &req, NULL);
2960
2961 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2962 memcpy(&smp->preq[1], &req, sizeof(req));
2963
2964 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2965 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2966}
2967
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002968static void smp_resume_cb(struct l2cap_chan *chan)
2969{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002970 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002971 struct l2cap_conn *conn = chan->conn;
2972 struct hci_conn *hcon = conn->hcon;
2973
2974 BT_DBG("chan %p", chan);
2975
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002976 if (hcon->type == ACL_LINK) {
2977 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002978 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002979 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002980
Johan Hedberg86d14072014-08-11 22:06:43 +03002981 if (!smp)
2982 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002983
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002984 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2985 return;
2986
Johan Hedberg86d14072014-08-11 22:06:43 +03002987 cancel_delayed_work(&smp->security_timer);
2988
Johan Hedbergd6268e82014-09-05 22:19:51 +03002989 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002990}
2991
Johan Hedberg70db83c2014-08-08 09:37:16 +03002992static void smp_ready_cb(struct l2cap_chan *chan)
2993{
2994 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002995 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002996
2997 BT_DBG("chan %p", chan);
2998
2999 conn->smp = chan;
3000 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003001
3002 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3003 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003004}
3005
Johan Hedberg4befb862014-08-11 22:06:38 +03003006static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3007{
3008 int err;
3009
3010 BT_DBG("chan %p", chan);
3011
3012 err = smp_sig_channel(chan, skb);
3013 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003014 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003015
Johan Hedbergb68fda62014-08-11 22:06:40 +03003016 if (smp)
3017 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003018
Johan Hedberg1e91c292014-08-18 20:33:29 +03003019 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003020 }
3021
3022 return err;
3023}
3024
Johan Hedberg70db83c2014-08-08 09:37:16 +03003025static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3026 unsigned long hdr_len,
3027 unsigned long len, int nb)
3028{
3029 struct sk_buff *skb;
3030
3031 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3032 if (!skb)
3033 return ERR_PTR(-ENOMEM);
3034
3035 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003036 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003037
3038 return skb;
3039}
3040
3041static const struct l2cap_ops smp_chan_ops = {
3042 .name = "Security Manager",
3043 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003044 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003045 .alloc_skb = smp_alloc_skb_cb,
3046 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003047 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003048
3049 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003050 .state_change = l2cap_chan_no_state_change,
3051 .close = l2cap_chan_no_close,
3052 .defer = l2cap_chan_no_defer,
3053 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003054 .set_shutdown = l2cap_chan_no_set_shutdown,
3055 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003056};
3057
3058static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3059{
3060 struct l2cap_chan *chan;
3061
3062 BT_DBG("pchan %p", pchan);
3063
3064 chan = l2cap_chan_create();
3065 if (!chan)
3066 return NULL;
3067
3068 chan->chan_type = pchan->chan_type;
3069 chan->ops = &smp_chan_ops;
3070 chan->scid = pchan->scid;
3071 chan->dcid = chan->scid;
3072 chan->imtu = pchan->imtu;
3073 chan->omtu = pchan->omtu;
3074 chan->mode = pchan->mode;
3075
Johan Hedbergabe84902014-11-12 22:22:21 +02003076 /* Other L2CAP channels may request SMP routines in order to
3077 * change the security level. This means that the SMP channel
3078 * lock must be considered in its own category to avoid lockdep
3079 * warnings.
3080 */
3081 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3082
Johan Hedberg70db83c2014-08-08 09:37:16 +03003083 BT_DBG("created chan %p", chan);
3084
3085 return chan;
3086}
3087
3088static const struct l2cap_ops smp_root_chan_ops = {
3089 .name = "Security Manager Root",
3090 .new_connection = smp_new_conn_cb,
3091
3092 /* None of these are implemented for the root channel */
3093 .close = l2cap_chan_no_close,
3094 .alloc_skb = l2cap_chan_no_alloc_skb,
3095 .recv = l2cap_chan_no_recv,
3096 .state_change = l2cap_chan_no_state_change,
3097 .teardown = l2cap_chan_no_teardown,
3098 .ready = l2cap_chan_no_ready,
3099 .defer = l2cap_chan_no_defer,
3100 .suspend = l2cap_chan_no_suspend,
3101 .resume = l2cap_chan_no_resume,
3102 .set_shutdown = l2cap_chan_no_set_shutdown,
3103 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003104};
3105
Johan Hedbergef8efe42014-08-13 15:12:32 +03003106static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003107{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003108 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003109 struct smp_dev *smp;
3110 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003111 struct crypto_hash *tfm_cmac;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003112
Johan Hedbergef8efe42014-08-13 15:12:32 +03003113 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003114 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003115 goto create_chan;
3116 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003117
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003118 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3119 if (!smp)
3120 return ERR_PTR(-ENOMEM);
3121
3122 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003123 if (IS_ERR(tfm_aes)) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003124 BT_ERR("Unable to create ECB crypto context");
3125 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003126 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003127 }
3128
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003129 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3130 if (IS_ERR(tfm_cmac)) {
3131 BT_ERR("Unable to create CMAC crypto context");
3132 crypto_free_blkcipher(tfm_aes);
3133 kzfree(smp);
3134 return ERR_CAST(tfm_cmac);
3135 }
3136
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003137 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003138 smp->tfm_cmac = tfm_cmac;
Johan Hedberg2fd36552015-06-11 13:52:26 +03003139 smp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003140
Johan Hedbergef8efe42014-08-13 15:12:32 +03003141create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003142 chan = l2cap_chan_create();
3143 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003144 if (smp) {
3145 crypto_free_blkcipher(smp->tfm_aes);
3146 crypto_free_hash(smp->tfm_cmac);
3147 kzfree(smp);
3148 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003149 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003150 }
3151
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003152 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003153
Johan Hedbergef8efe42014-08-13 15:12:32 +03003154 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003155
3156 l2cap_chan_set_defaults(chan);
3157
Marcel Holtmann157029b2015-01-14 15:43:09 -08003158 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003159 u8 bdaddr_type;
3160
3161 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3162
3163 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003164 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003165 else
3166 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003167 } else {
3168 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003169 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003170 }
3171
Johan Hedberg70db83c2014-08-08 09:37:16 +03003172 chan->state = BT_LISTEN;
3173 chan->mode = L2CAP_MODE_BASIC;
3174 chan->imtu = L2CAP_DEFAULT_MTU;
3175 chan->ops = &smp_root_chan_ops;
3176
Johan Hedbergabe84902014-11-12 22:22:21 +02003177 /* Set correct nesting level for a parent/listening channel */
3178 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3179
Johan Hedbergef8efe42014-08-13 15:12:32 +03003180 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003181}
3182
Johan Hedbergef8efe42014-08-13 15:12:32 +03003183static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003184{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003185 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003186
Johan Hedbergef8efe42014-08-13 15:12:32 +03003187 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003188
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003189 smp = chan->data;
3190 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003191 chan->data = NULL;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003192 if (smp->tfm_aes)
3193 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003194 if (smp->tfm_cmac)
3195 crypto_free_hash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003196 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003197 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003198
Johan Hedberg70db83c2014-08-08 09:37:16 +03003199 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003200}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003201
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003202static ssize_t force_bredr_smp_read(struct file *file,
3203 char __user *user_buf,
3204 size_t count, loff_t *ppos)
3205{
3206 struct hci_dev *hdev = file->private_data;
3207 char buf[3];
3208
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003209 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003210 buf[1] = '\n';
3211 buf[2] = '\0';
3212 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3213}
3214
3215static ssize_t force_bredr_smp_write(struct file *file,
3216 const char __user *user_buf,
3217 size_t count, loff_t *ppos)
3218{
3219 struct hci_dev *hdev = file->private_data;
3220 char buf[32];
3221 size_t buf_size = min(count, (sizeof(buf)-1));
3222 bool enable;
3223
3224 if (copy_from_user(buf, user_buf, buf_size))
3225 return -EFAULT;
3226
3227 buf[buf_size] = '\0';
3228 if (strtobool(buf, &enable))
3229 return -EINVAL;
3230
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003231 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003232 return -EALREADY;
3233
3234 if (enable) {
3235 struct l2cap_chan *chan;
3236
3237 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3238 if (IS_ERR(chan))
3239 return PTR_ERR(chan);
3240
3241 hdev->smp_bredr_data = chan;
3242 } else {
3243 struct l2cap_chan *chan;
3244
3245 chan = hdev->smp_bredr_data;
3246 hdev->smp_bredr_data = NULL;
3247 smp_del_chan(chan);
3248 }
3249
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003250 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003251
3252 return count;
3253}
3254
3255static const struct file_operations force_bredr_smp_fops = {
3256 .open = simple_open,
3257 .read = force_bredr_smp_read,
3258 .write = force_bredr_smp_write,
3259 .llseek = default_llseek,
3260};
3261
Johan Hedberg2fd36552015-06-11 13:52:26 +03003262static ssize_t le_max_key_size_read(struct file *file,
3263 char __user *user_buf,
3264 size_t count, loff_t *ppos)
3265{
3266 struct hci_dev *hdev = file->private_data;
3267 char buf[4];
3268
3269 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->max_key_size);
3270
3271 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3272}
3273
3274static ssize_t le_max_key_size_write(struct file *file,
3275 const char __user *user_buf,
3276 size_t count, loff_t *ppos)
3277{
3278 struct hci_dev *hdev = file->private_data;
3279 char buf[32];
3280 size_t buf_size = min(count, (sizeof(buf) - 1));
3281 u8 key_size;
3282
3283 if (copy_from_user(buf, user_buf, buf_size))
3284 return -EFAULT;
3285
3286 buf[buf_size] = '\0';
3287
3288 sscanf(buf, "%hhu", &key_size);
3289
3290 if (key_size > SMP_MAX_ENC_KEY_SIZE || key_size < SMP_MIN_ENC_KEY_SIZE)
3291 return -EINVAL;
3292
3293 SMP_DEV(hdev)->max_key_size = key_size;
3294
3295 return count;
3296}
3297
3298static const struct file_operations le_max_key_size_fops = {
3299 .open = simple_open,
3300 .read = le_max_key_size_read,
3301 .write = le_max_key_size_write,
3302 .llseek = default_llseek,
3303};
3304
Johan Hedbergef8efe42014-08-13 15:12:32 +03003305int smp_register(struct hci_dev *hdev)
3306{
3307 struct l2cap_chan *chan;
3308
3309 BT_DBG("%s", hdev->name);
3310
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003311 /* If the controller does not support Low Energy operation, then
3312 * there is also no need to register any SMP channel.
3313 */
3314 if (!lmp_le_capable(hdev))
3315 return 0;
3316
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003317 if (WARN_ON(hdev->smp_data)) {
3318 chan = hdev->smp_data;
3319 hdev->smp_data = NULL;
3320 smp_del_chan(chan);
3321 }
3322
Johan Hedbergef8efe42014-08-13 15:12:32 +03003323 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3324 if (IS_ERR(chan))
3325 return PTR_ERR(chan);
3326
3327 hdev->smp_data = chan;
3328
Johan Hedberg2fd36552015-06-11 13:52:26 +03003329 debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3330 &le_max_key_size_fops);
3331
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003332 /* If the controller does not support BR/EDR Secure Connections
3333 * feature, then the BR/EDR SMP channel shall not be present.
3334 *
3335 * To test this with Bluetooth 4.0 controllers, create a debugfs
3336 * switch that allows forcing BR/EDR SMP support and accepting
3337 * cross-transport pairing on non-AES encrypted connections.
3338 */
3339 if (!lmp_sc_capable(hdev)) {
3340 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3341 hdev, &force_bredr_smp_fops);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003342 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003343 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003344
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003345 if (WARN_ON(hdev->smp_bredr_data)) {
3346 chan = hdev->smp_bredr_data;
3347 hdev->smp_bredr_data = NULL;
3348 smp_del_chan(chan);
3349 }
3350
Johan Hedbergef8efe42014-08-13 15:12:32 +03003351 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3352 if (IS_ERR(chan)) {
3353 int err = PTR_ERR(chan);
3354 chan = hdev->smp_data;
3355 hdev->smp_data = NULL;
3356 smp_del_chan(chan);
3357 return err;
3358 }
3359
3360 hdev->smp_bredr_data = chan;
3361
3362 return 0;
3363}
3364
3365void smp_unregister(struct hci_dev *hdev)
3366{
3367 struct l2cap_chan *chan;
3368
3369 if (hdev->smp_bredr_data) {
3370 chan = hdev->smp_bredr_data;
3371 hdev->smp_bredr_data = NULL;
3372 smp_del_chan(chan);
3373 }
3374
3375 if (hdev->smp_data) {
3376 chan = hdev->smp_data;
3377 hdev->smp_data = NULL;
3378 smp_del_chan(chan);
3379 }
3380}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003381
3382#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3383
Johan Hedbergcfc41982014-12-30 09:50:40 +02003384static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3385{
3386 const u8 irk[16] = {
3387 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3388 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3389 const u8 r[3] = { 0x94, 0x81, 0x70 };
3390 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3391 u8 res[3];
3392 int err;
3393
3394 err = smp_ah(tfm_aes, irk, r, res);
3395 if (err)
3396 return err;
3397
3398 if (memcmp(res, exp, 3))
3399 return -EINVAL;
3400
3401 return 0;
3402}
3403
3404static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3405{
3406 const u8 k[16] = {
3407 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3408 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3409 const u8 r[16] = {
3410 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3411 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3412 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3413 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3414 const u8 _iat = 0x01;
3415 const u8 _rat = 0x00;
3416 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3417 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3418 const u8 exp[16] = {
3419 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3420 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3421 u8 res[16];
3422 int err;
3423
3424 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3425 if (err)
3426 return err;
3427
3428 if (memcmp(res, exp, 16))
3429 return -EINVAL;
3430
3431 return 0;
3432}
3433
3434static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3435{
3436 const u8 k[16] = {
3437 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3439 const u8 r1[16] = {
3440 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3441 const u8 r2[16] = {
3442 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3443 const u8 exp[16] = {
3444 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3445 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3446 u8 res[16];
3447 int err;
3448
3449 err = smp_s1(tfm_aes, k, r1, r2, res);
3450 if (err)
3451 return err;
3452
3453 if (memcmp(res, exp, 16))
3454 return -EINVAL;
3455
3456 return 0;
3457}
3458
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003459static int __init test_f4(struct crypto_hash *tfm_cmac)
3460{
3461 const u8 u[32] = {
3462 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3463 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3464 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3465 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3466 const u8 v[32] = {
3467 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3468 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3469 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3470 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3471 const u8 x[16] = {
3472 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3473 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3474 const u8 z = 0x00;
3475 const u8 exp[16] = {
3476 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3477 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3478 u8 res[16];
3479 int err;
3480
3481 err = smp_f4(tfm_cmac, u, v, x, z, res);
3482 if (err)
3483 return err;
3484
3485 if (memcmp(res, exp, 16))
3486 return -EINVAL;
3487
3488 return 0;
3489}
3490
3491static int __init test_f5(struct crypto_hash *tfm_cmac)
3492{
3493 const u8 w[32] = {
3494 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3495 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3496 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3497 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3498 const u8 n1[16] = {
3499 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3500 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3501 const u8 n2[16] = {
3502 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3503 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3504 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3505 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3506 const u8 exp_ltk[16] = {
3507 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3508 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3509 const u8 exp_mackey[16] = {
3510 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3511 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3512 u8 mackey[16], ltk[16];
3513 int err;
3514
3515 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3516 if (err)
3517 return err;
3518
3519 if (memcmp(mackey, exp_mackey, 16))
3520 return -EINVAL;
3521
3522 if (memcmp(ltk, exp_ltk, 16))
3523 return -EINVAL;
3524
3525 return 0;
3526}
3527
3528static int __init test_f6(struct crypto_hash *tfm_cmac)
3529{
3530 const u8 w[16] = {
3531 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3532 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3533 const u8 n1[16] = {
3534 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3535 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3536 const u8 n2[16] = {
3537 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3538 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3539 const u8 r[16] = {
3540 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3541 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3542 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3543 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3544 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3545 const u8 exp[16] = {
3546 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3547 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3548 u8 res[16];
3549 int err;
3550
3551 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3552 if (err)
3553 return err;
3554
3555 if (memcmp(res, exp, 16))
3556 return -EINVAL;
3557
3558 return 0;
3559}
3560
3561static int __init test_g2(struct crypto_hash *tfm_cmac)
3562{
3563 const u8 u[32] = {
3564 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3565 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3566 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3567 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3568 const u8 v[32] = {
3569 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3570 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3571 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3572 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3573 const u8 x[16] = {
3574 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3575 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3576 const u8 y[16] = {
3577 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3578 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3579 const u32 exp_val = 0x2f9ed5ba % 1000000;
3580 u32 val;
3581 int err;
3582
3583 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3584 if (err)
3585 return err;
3586
3587 if (val != exp_val)
3588 return -EINVAL;
3589
3590 return 0;
3591}
3592
3593static int __init test_h6(struct crypto_hash *tfm_cmac)
3594{
3595 const u8 w[16] = {
3596 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3597 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3598 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3599 const u8 exp[16] = {
3600 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3601 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3602 u8 res[16];
3603 int err;
3604
3605 err = smp_h6(tfm_cmac, w, key_id, res);
3606 if (err)
3607 return err;
3608
3609 if (memcmp(res, exp, 16))
3610 return -EINVAL;
3611
3612 return 0;
3613}
3614
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003615static char test_smp_buffer[32];
3616
3617static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3618 size_t count, loff_t *ppos)
3619{
3620 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3621 strlen(test_smp_buffer));
3622}
3623
3624static const struct file_operations test_smp_fops = {
3625 .open = simple_open,
3626 .read = test_smp_read,
3627 .llseek = default_llseek,
3628};
3629
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003630static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3631 struct crypto_hash *tfm_cmac)
3632{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003633 ktime_t calltime, delta, rettime;
3634 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003635 int err;
3636
Marcel Holtmann255047b2014-12-30 00:11:20 -08003637 calltime = ktime_get();
3638
Johan Hedbergcfc41982014-12-30 09:50:40 +02003639 err = test_ah(tfm_aes);
3640 if (err) {
3641 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003642 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003643 }
3644
3645 err = test_c1(tfm_aes);
3646 if (err) {
3647 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003648 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003649 }
3650
3651 err = test_s1(tfm_aes);
3652 if (err) {
3653 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003654 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003655 }
3656
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003657 err = test_f4(tfm_cmac);
3658 if (err) {
3659 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003660 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003661 }
3662
3663 err = test_f5(tfm_cmac);
3664 if (err) {
3665 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003666 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003667 }
3668
3669 err = test_f6(tfm_cmac);
3670 if (err) {
3671 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003672 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003673 }
3674
3675 err = test_g2(tfm_cmac);
3676 if (err) {
3677 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003678 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003679 }
3680
3681 err = test_h6(tfm_cmac);
3682 if (err) {
3683 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003684 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003685 }
3686
Marcel Holtmann255047b2014-12-30 00:11:20 -08003687 rettime = ktime_get();
3688 delta = ktime_sub(rettime, calltime);
3689 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3690
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003691 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003692
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003693done:
3694 if (!err)
3695 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3696 "PASS (%llu usecs)\n", duration);
3697 else
3698 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3699
3700 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3701 &test_smp_fops);
3702
3703 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003704}
3705
3706int __init bt_selftest_smp(void)
3707{
3708 struct crypto_blkcipher *tfm_aes;
3709 struct crypto_hash *tfm_cmac;
3710 int err;
3711
3712 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3713 if (IS_ERR(tfm_aes)) {
3714 BT_ERR("Unable to create ECB crypto context");
3715 return PTR_ERR(tfm_aes);
3716 }
3717
3718 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3719 if (IS_ERR(tfm_cmac)) {
3720 BT_ERR("Unable to create CMAC crypto context");
3721 crypto_free_blkcipher(tfm_aes);
3722 return PTR_ERR(tfm_cmac);
3723 }
3724
3725 err = run_selftests(tfm_aes, tfm_cmac);
3726
3727 crypto_free_hash(tfm_cmac);
3728 crypto_free_blkcipher(tfm_aes);
3729
3730 return err;
3731}
3732
3733#endif