blob: 229d88eebf4e9a9ffbcef8a7d54b6bbc86f387f4 [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 Hedbergb1f663c2015-06-11 13:52:27 +030087 u8 min_key_size;
Johan Hedberg2fd36552015-06-11 13:52:26 +030088 u8 max_key_size;
89
Marcel Holtmann88a479d2015-03-16 01:10:19 -070090 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -070091 struct crypto_hash *tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070092};
93
Johan Hedberg4bc58f52014-05-20 09:45:47 +030094struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030095 struct l2cap_conn *conn;
96 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030097 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030098
Johan Hedberg4bc58f52014-05-20 09:45:47 +030099 u8 preq[7]; /* SMP Pairing Request */
100 u8 prsp[7]; /* SMP Pairing Response */
101 u8 prnd[16]; /* SMP Pairing Random (local) */
102 u8 rrnd[16]; /* SMP Pairing Random (remote) */
103 u8 pcnf[16]; /* SMP Pairing Confirm */
104 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +0200105 u8 rr[16]; /* Remote OOB ra/rb value */
106 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300107 u8 enc_key_size;
108 u8 remote_key_dist;
109 bdaddr_t id_addr;
110 u8 id_addr_type;
111 u8 irk[16];
112 struct smp_csrk *csrk;
113 struct smp_csrk *slave_csrk;
114 struct smp_ltk *ltk;
115 struct smp_ltk *slave_ltk;
116 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300117 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300118 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300119 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300120 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300121
Johan Hedberg3b191462014-06-06 10:50:15 +0300122 /* Secure Connections variables */
123 u8 local_pk[64];
124 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300125 u8 remote_pk[64];
126 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300127 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300128
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300129 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300130 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300131};
132
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300133/* These debug key values are defined in the SMP section of the core
134 * specification. debug_pk is the public debug key and debug_sk the
135 * private debug key.
136 */
137static const u8 debug_pk[64] = {
138 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
139 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
140 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
141 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
142
143 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
144 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
145 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
146 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
147};
148
149static const u8 debug_sk[32] = {
150 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
151 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
152 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
153 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
154};
155
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300156static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300157{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300158 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300159
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300160 for (i = 0; i < len; i++)
161 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300162}
163
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200164/* The following functions map to the LE SC SMP crypto functions
165 * AES-CMAC, f4, f5, f6, g2 and h6.
166 */
167
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300168static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
169 size_t len, u8 mac[16])
170{
171 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
172 struct hash_desc desc;
173 struct scatterlist sg;
174 int err;
175
176 if (len > CMAC_MSG_MAX)
177 return -EFBIG;
178
179 if (!tfm) {
180 BT_ERR("tfm %p", tfm);
181 return -EINVAL;
182 }
183
184 desc.tfm = tfm;
185 desc.flags = 0;
186
187 crypto_hash_init(&desc);
188
189 /* Swap key and message from LSB to MSB */
190 swap_buf(k, tmp, 16);
191 swap_buf(m, msg_msb, len);
192
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200193 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
194 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300195
196 err = crypto_hash_setkey(tfm, tmp, 16);
197 if (err) {
198 BT_ERR("cipher setkey failed: %d", err);
199 return err;
200 }
201
202 sg_init_one(&sg, msg_msb, len);
203
204 err = crypto_hash_update(&desc, &sg, len);
205 if (err) {
206 BT_ERR("Hash update error %d", err);
207 return err;
208 }
209
210 err = crypto_hash_final(&desc, mac_msb);
211 if (err) {
212 BT_ERR("Hash final error %d", err);
213 return err;
214 }
215
216 swap_buf(mac_msb, mac, 16);
217
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200218 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300219
220 return 0;
221}
222
223static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
224 const u8 x[16], u8 z, u8 res[16])
225{
226 u8 m[65];
227 int err;
228
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200229 SMP_DBG("u %32phN", u);
230 SMP_DBG("v %32phN", v);
231 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300232
233 m[0] = z;
234 memcpy(m + 1, v, 32);
235 memcpy(m + 33, u, 32);
236
237 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
238 if (err)
239 return err;
240
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200241 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300242
243 return err;
244}
245
Johan Hedberg4da50de2014-12-29 12:04:10 +0200246static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32],
247 const u8 n1[16], const u8 n2[16], const u8 a1[7],
248 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300249{
250 /* The btle, salt and length "magic" values are as defined in
251 * the SMP section of the Bluetooth core specification. In ASCII
252 * the btle value ends up being 'btle'. The salt is just a
253 * random number whereas length is the value 256 in little
254 * endian format.
255 */
256 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
257 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
258 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
259 const u8 length[2] = { 0x00, 0x01 };
260 u8 m[53], t[16];
261 int err;
262
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200263 SMP_DBG("w %32phN", w);
264 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
265 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300266
267 err = aes_cmac(tfm_cmac, salt, w, 32, t);
268 if (err)
269 return err;
270
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200271 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300272
273 memcpy(m, length, 2);
274 memcpy(m + 2, a2, 7);
275 memcpy(m + 9, a1, 7);
276 memcpy(m + 16, n2, 16);
277 memcpy(m + 32, n1, 16);
278 memcpy(m + 48, btle, 4);
279
280 m[52] = 0; /* Counter */
281
282 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
283 if (err)
284 return err;
285
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200286 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300287
288 m[52] = 1; /* Counter */
289
290 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
291 if (err)
292 return err;
293
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200294 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300295
296 return 0;
297}
298
299static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200300 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300301 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
302 u8 res[16])
303{
304 u8 m[65];
305 int err;
306
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200307 SMP_DBG("w %16phN", w);
308 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
309 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300310
311 memcpy(m, a2, 7);
312 memcpy(m + 7, a1, 7);
313 memcpy(m + 14, io_cap, 3);
314 memcpy(m + 17, r, 16);
315 memcpy(m + 33, n2, 16);
316 memcpy(m + 49, n1, 16);
317
318 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
319 if (err)
320 return err;
321
Marcel Holtmann203de212014-12-31 20:01:22 -0800322 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300323
324 return err;
325}
326
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300327static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
328 const u8 x[16], const u8 y[16], u32 *val)
329{
330 u8 m[80], tmp[16];
331 int err;
332
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200333 SMP_DBG("u %32phN", u);
334 SMP_DBG("v %32phN", v);
335 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300336
337 memcpy(m, y, 16);
338 memcpy(m + 16, v, 32);
339 memcpy(m + 48, u, 32);
340
341 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
342 if (err)
343 return err;
344
345 *val = get_unaligned_le32(tmp);
346 *val %= 1000000;
347
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200348 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300349
350 return 0;
351}
352
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200353static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
354 const u8 key_id[4], u8 res[16])
355{
356 int err;
357
358 SMP_DBG("w %16phN key_id %4phN", w, key_id);
359
360 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
361 if (err)
362 return err;
363
364 SMP_DBG("res %16phN", res);
365
366 return err;
367}
368
369/* The following functions map to the legacy SMP crypto functions e, c1,
370 * s1 and ah.
371 */
372
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300373static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
374{
375 struct blkcipher_desc desc;
376 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200377 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200378 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300379
Johan Hedberg011c3912015-05-19 21:06:04 +0300380 SMP_DBG("k %16phN r %16phN", k, r);
381
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200382 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300383 BT_ERR("tfm %p", tfm);
384 return -EINVAL;
385 }
386
387 desc.tfm = tfm;
388 desc.flags = 0;
389
Johan Hedberg943a7322014-03-18 12:58:24 +0200390 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300391 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200392
393 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300394 if (err) {
395 BT_ERR("cipher setkey failed: %d", err);
396 return err;
397 }
398
Johan Hedberg943a7322014-03-18 12:58:24 +0200399 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300400 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200401
402 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300403
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300404 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
405 if (err)
406 BT_ERR("Encrypt data error %d", err);
407
Johan Hedberg943a7322014-03-18 12:58:24 +0200408 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300409 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200410
Johan Hedberg011c3912015-05-19 21:06:04 +0300411 SMP_DBG("r %16phN", r);
412
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300413 return err;
414}
415
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200416static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
417 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
418 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
419{
420 u8 p1[16], p2[16];
421 int err;
422
Johan Hedberg011c3912015-05-19 21:06:04 +0300423 SMP_DBG("k %16phN r %16phN", k, r);
424 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
425 SMP_DBG("preq %7phN pres %7phN", preq, pres);
426
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200427 memset(p1, 0, 16);
428
429 /* p1 = pres || preq || _rat || _iat */
430 p1[0] = _iat;
431 p1[1] = _rat;
432 memcpy(p1 + 2, preq, 7);
433 memcpy(p1 + 9, pres, 7);
434
Johan Hedberg011c3912015-05-19 21:06:04 +0300435 SMP_DBG("p1 %16phN", p1);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200436
437 /* res = r XOR p1 */
438 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
439
440 /* res = e(k, res) */
441 err = smp_e(tfm_aes, k, res);
442 if (err) {
443 BT_ERR("Encrypt data error");
444 return err;
445 }
446
Johan Hedberg011c3912015-05-19 21:06:04 +0300447 /* p2 = padding || ia || ra */
448 memcpy(p2, ra, 6);
449 memcpy(p2 + 6, ia, 6);
450 memset(p2 + 12, 0, 4);
451
452 SMP_DBG("p2 %16phN", p2);
453
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200454 /* res = res XOR p2 */
455 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
456
457 /* res = e(k, res) */
458 err = smp_e(tfm_aes, k, res);
459 if (err)
460 BT_ERR("Encrypt data error");
461
462 return err;
463}
464
465static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
466 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300467{
468 int err;
469
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200470 /* Just least significant octets from r1 and r2 are considered */
471 memcpy(_r, r2, 8);
472 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300473
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200474 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300475 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200476 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300477
478 return err;
479}
480
Johan Hedbergcd082792014-12-02 13:37:41 +0200481static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
482 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200483{
Johan Hedberg943a7322014-03-18 12:58:24 +0200484 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200485 int err;
486
487 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200488 memcpy(_res, r, 3);
489 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200490
Johan Hedberg943a7322014-03-18 12:58:24 +0200491 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200492 if (err) {
493 BT_ERR("Encrypt error");
494 return err;
495 }
496
497 /* The output of the random address function ah is:
Marcel Holtmannc5080d42015-09-04 17:08:18 +0200498 * ah(k, r) = e(k, r') mod 2^24
Johan Hedberg60478052014-02-18 10:19:31 +0200499 * The output of the security function e is then truncated to 24 bits
500 * by taking the least significant 24 bits of the output of e as the
501 * result of ah.
502 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200503 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200504
505 return 0;
506}
507
Johan Hedbergcd082792014-12-02 13:37:41 +0200508bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
509 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200510{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300511 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700512 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200513 u8 hash[3];
514 int err;
515
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300516 if (!chan || !chan->data)
517 return false;
518
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700519 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300520
Johan Hedberg60478052014-02-18 10:19:31 +0200521 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
522
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700523 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200524 if (err)
525 return false;
526
527 return !memcmp(bdaddr->b, hash, 3);
528}
529
Johan Hedbergcd082792014-12-02 13:37:41 +0200530int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200531{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300532 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700533 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200534 int err;
535
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300536 if (!chan || !chan->data)
537 return -EOPNOTSUPP;
538
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700539 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300540
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200541 get_random_bytes(&rpa->b[3], 3);
542
543 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
544 rpa->b[5] |= 0x40; /* Set second most significant bit */
545
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700546 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200547 if (err < 0)
548 return err;
549
550 BT_DBG("RPA %pMR", rpa);
551
552 return 0;
553}
554
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700555int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
556{
557 struct l2cap_chan *chan = hdev->smp_data;
558 struct smp_dev *smp;
559 int err;
560
561 if (!chan || !chan->data)
562 return -EOPNOTSUPP;
563
564 smp = chan->data;
565
566 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
567 BT_DBG("Using debug keys");
568 memcpy(smp->local_pk, debug_pk, 64);
569 memcpy(smp->local_sk, debug_sk, 32);
570 smp->debug_key = true;
571 } else {
572 while (true) {
573 /* Generate local key pair for Secure Connections */
574 if (!ecc_make_key(smp->local_pk, smp->local_sk))
575 return -EIO;
576
577 /* This is unlikely, but we need to check that
578 * we didn't accidentially generate a debug key.
579 */
580 if (memcmp(smp->local_sk, debug_sk, 32))
581 break;
582 }
583 smp->debug_key = false;
584 }
585
586 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
587 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
588 SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
589
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700590 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700591
592 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700593 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700594 if (err < 0)
595 return err;
596
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700597 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700598
599 return 0;
600}
601
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300602static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
603{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300604 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300605 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300606 struct kvec iv[2];
607 struct msghdr msg;
608
609 if (!chan)
610 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300611
612 BT_DBG("code 0x%2.2x", code);
613
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300614 iv[0].iov_base = &code;
615 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300616
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300617 iv[1].iov_base = data;
618 iv[1].iov_len = len;
619
620 memset(&msg, 0, sizeof(msg));
621
Al Viro17836392014-11-24 17:07:38 -0500622 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300623
624 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300625
Johan Hedbergb68fda62014-08-11 22:06:40 +0300626 if (!chan->data)
627 return;
628
629 smp = chan->data;
630
631 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300632 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300633}
634
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300635static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800636{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300637 if (authreq & SMP_AUTH_MITM) {
638 if (authreq & SMP_AUTH_SC)
639 return BT_SECURITY_FIPS;
640 else
641 return BT_SECURITY_HIGH;
642 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800643 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300644 }
Brian Gix2b64d152011-12-21 16:12:12 -0800645}
646
647static __u8 seclevel_to_authreq(__u8 sec_level)
648{
649 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300650 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800651 case BT_SECURITY_HIGH:
652 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
653 case BT_SECURITY_MEDIUM:
654 return SMP_AUTH_BONDING;
655 default:
656 return SMP_AUTH_NONE;
657 }
658}
659
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300660static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700661 struct smp_cmd_pairing *req,
662 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300663{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300664 struct l2cap_chan *chan = conn->smp;
665 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200666 struct hci_conn *hcon = conn->hcon;
667 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100668 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300669
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700670 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700671 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
672 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300673 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800674 } else {
675 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300676 }
677
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700678 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200679 remote_dist |= SMP_DIST_ID_KEY;
680
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700681 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200682 local_dist |= SMP_DIST_ID_KEY;
683
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700684 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100685 (authreq & SMP_AUTH_SC)) {
686 struct oob_data *oob_data;
687 u8 bdaddr_type;
688
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700689 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300690 local_dist |= SMP_DIST_LINK_KEY;
691 remote_dist |= SMP_DIST_LINK_KEY;
692 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100693
694 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
695 bdaddr_type = BDADDR_LE_PUBLIC;
696 else
697 bdaddr_type = BDADDR_LE_RANDOM;
698
699 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
700 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800701 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200702 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100703 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100704 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100705 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700706 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
707 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100708 }
709
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300710 } else {
711 authreq &= ~SMP_AUTH_SC;
712 }
713
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300714 if (rsp == NULL) {
715 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100716 req->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300717 req->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200718 req->init_key_dist = local_dist;
719 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300720 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200721
722 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300723 return;
724 }
725
726 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100727 rsp->oob_flag = oob_flag;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300728 rsp->max_key_size = SMP_DEV(hdev)->max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200729 rsp->init_key_dist = req->init_key_dist & remote_dist;
730 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300731 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200732
733 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300734}
735
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300736static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
737{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300738 struct l2cap_chan *chan = conn->smp;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300739 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300740 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300741
Johan Hedberg2fd36552015-06-11 13:52:26 +0300742 if (max_key_size > SMP_DEV(hdev)->max_key_size ||
743 max_key_size < SMP_MIN_ENC_KEY_SIZE)
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300744 return SMP_ENC_KEY_SIZE;
745
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300746 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300747
748 return 0;
749}
750
Johan Hedberg6f48e262014-08-11 22:06:44 +0300751static void smp_chan_destroy(struct l2cap_conn *conn)
752{
753 struct l2cap_chan *chan = conn->smp;
754 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200755 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300756 bool complete;
757
758 BUG_ON(!smp);
759
760 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300761
Johan Hedberg6f48e262014-08-11 22:06:44 +0300762 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200763 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300764
Marcel Holtmann276812e2015-03-16 01:10:18 -0700765 kzfree(smp->csrk);
766 kzfree(smp->slave_csrk);
767 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300768
769 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300770 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300771
Johan Hedberg923e2412014-12-03 12:43:39 +0200772 /* Ensure that we don't leave any debug key around if debug key
773 * support hasn't been explicitly enabled.
774 */
775 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700776 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200777 list_del_rcu(&smp->ltk->list);
778 kfree_rcu(smp->ltk, rcu);
779 smp->ltk = NULL;
780 }
781
Johan Hedberg6f48e262014-08-11 22:06:44 +0300782 /* If pairing failed clean up any keys we might have */
783 if (!complete) {
784 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200785 list_del_rcu(&smp->ltk->list);
786 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300787 }
788
789 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200790 list_del_rcu(&smp->slave_ltk->list);
791 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300792 }
793
794 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200795 list_del_rcu(&smp->remote_irk->list);
796 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300797 }
798 }
799
800 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700801 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200802 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300803}
804
Johan Hedberg84794e12013-11-06 11:24:57 +0200805static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800806{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200807 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300808 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200809
Johan Hedberg84794e12013-11-06 11:24:57 +0200810 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800811 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700812 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800813
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700814 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700815 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300816
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300817 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300818 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800819}
820
Brian Gix2b64d152011-12-21 16:12:12 -0800821#define JUST_WORKS 0x00
822#define JUST_CFM 0x01
823#define REQ_PASSKEY 0x02
824#define CFM_PASSKEY 0x03
825#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300826#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800827#define OVERLAP 0xFF
828
829static const u8 gen_method[5][5] = {
830 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
831 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
832 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
833 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
834 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
835};
836
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300837static const u8 sc_method[5][5] = {
838 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
839 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
840 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
841 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
842 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
843};
844
Johan Hedberg581370c2014-06-17 13:07:38 +0300845static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
846{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300847 /* If either side has unknown io_caps, use JUST_CFM (which gets
848 * converted later to JUST_WORKS if we're initiators.
849 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300850 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
851 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300852 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300853
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300854 if (test_bit(SMP_FLAG_SC, &smp->flags))
855 return sc_method[remote_io][local_io];
856
Johan Hedberg581370c2014-06-17 13:07:38 +0300857 return gen_method[remote_io][local_io];
858}
859
Brian Gix2b64d152011-12-21 16:12:12 -0800860static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
861 u8 local_io, u8 remote_io)
862{
863 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300864 struct l2cap_chan *chan = conn->smp;
865 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800866 u32 passkey = 0;
867 int ret = 0;
868
869 /* Initialize key for JUST WORKS */
870 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300871 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800872
873 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
874
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300875 /* If neither side wants MITM, either "just" confirm an incoming
876 * request or use just-works for outgoing ones. The JUST_CFM
877 * will be converted to JUST_WORKS if necessary later in this
878 * function. If either side has MITM look up the method from the
879 * table.
880 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300881 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300882 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800883 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300884 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800885
Johan Hedberga82505c2014-03-24 14:39:07 +0200886 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300887 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
888 &smp->flags))
889 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200890
Johan Hedberg02f3e252014-07-16 15:09:13 +0300891 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300892 if (smp->method == JUST_CFM &&
893 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
894 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300895
Brian Gix2b64d152011-12-21 16:12:12 -0800896 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300897 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300898 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800899 return 0;
900 }
901
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200902 /* If this function is used for SC -> legacy fallback we
903 * can only recover the just-works case.
904 */
905 if (test_bit(SMP_FLAG_SC, &smp->flags))
906 return -EINVAL;
907
Brian Gix2b64d152011-12-21 16:12:12 -0800908 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300909 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300910 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300911 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
912 hcon->pending_sec_level = BT_SECURITY_HIGH;
913 }
Brian Gix2b64d152011-12-21 16:12:12 -0800914
915 /* If both devices have Keyoard-Display I/O, the master
916 * Confirms and the slave Enters the passkey.
917 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300918 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300919 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300920 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800921 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300922 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800923 }
924
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200925 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300926 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200927 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800928 get_random_bytes(&passkey, sizeof(passkey));
929 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200930 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800931 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300932 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800933 }
934
Johan Hedberg783e0572014-05-31 18:48:26 +0300935 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700936 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200937 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300938 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200939 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
940 hcon->type, hcon->dst_type,
941 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800942 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200943 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200944 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200945 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800946
Brian Gix2b64d152011-12-21 16:12:12 -0800947 return ret;
948}
949
Johan Hedberg1cc61142014-05-20 09:45:52 +0300950static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300951{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300952 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300953 struct smp_cmd_pairing_confirm cp;
954 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300955
956 BT_DBG("conn %p", conn);
957
Johan Hedberge491eaf2014-10-25 21:15:37 +0200958 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200959 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200960 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
961 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300962 if (ret)
963 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300964
Johan Hedberg4a74d652014-05-20 09:45:50 +0300965 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800966
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300967 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
968
Johan Hedbergb28b4942014-09-05 22:19:55 +0300969 if (conn->hcon->out)
970 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
971 else
972 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
973
Johan Hedberg1cc61142014-05-20 09:45:52 +0300974 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300975}
976
Johan Hedberg861580a2014-05-20 09:45:51 +0300977static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300978{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300979 struct l2cap_conn *conn = smp->conn;
980 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300981 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300982 int ret;
983
Johan Hedbergec70f362014-06-27 14:23:04 +0300984 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300985 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300986
987 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
988
Johan Hedberge491eaf2014-10-25 21:15:37 +0200989 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200990 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200991 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300992 if (ret)
993 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300994
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300995 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
996 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300997 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300998 }
999
1000 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001001 u8 stk[16];
1002 __le64 rand = 0;
1003 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001004
Johan Hedberge491eaf2014-10-25 21:15:37 +02001005 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001006
Johan Hedberg861580a2014-05-20 09:45:51 +03001007 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1008 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001009
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001010 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001011 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001012 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001013 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001014 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001015 __le64 rand = 0;
1016 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001017
Johan Hedberg943a7322014-03-18 12:58:24 +02001018 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1019 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001020
Johan Hedberge491eaf2014-10-25 21:15:37 +02001021 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001022
Johan Hedbergfff34902014-06-10 15:19:50 +03001023 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1024 auth = 1;
1025 else
1026 auth = 0;
1027
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001028 /* Even though there's no _SLAVE suffix this is the
1029 * slave STK we're adding for later lookup (the master
1030 * STK never needs to be stored).
1031 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001032 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001033 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001034 }
1035
Johan Hedberg861580a2014-05-20 09:45:51 +03001036 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001037}
1038
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001039static void smp_notify_keys(struct l2cap_conn *conn)
1040{
1041 struct l2cap_chan *chan = conn->smp;
1042 struct smp_chan *smp = chan->data;
1043 struct hci_conn *hcon = conn->hcon;
1044 struct hci_dev *hdev = hcon->hdev;
1045 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1046 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1047 bool persistent;
1048
Johan Hedbergcad20c22015-10-12 13:36:19 +02001049 if (hcon->type == ACL_LINK) {
1050 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1051 persistent = false;
1052 else
1053 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1054 &hcon->flags);
1055 } else {
1056 /* The LTKs, IRKs and CSRKs should be persistent only if
1057 * both sides had the bonding bit set in their
1058 * authentication requests.
1059 */
1060 persistent = !!((req->auth_req & rsp->auth_req) &
1061 SMP_AUTH_BONDING);
1062 }
1063
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001064 if (smp->remote_irk) {
Johan Hedbergcad20c22015-10-12 13:36:19 +02001065 mgmt_new_irk(hdev, smp->remote_irk, persistent);
1066
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001067 /* Now that user space can be considered to know the
1068 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001069 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001070 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001071 if (hcon->type == LE_LINK) {
1072 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1073 hcon->dst_type = smp->remote_irk->addr_type;
1074 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1075 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001076
1077 /* When receiving an indentity resolving key for
1078 * a remote device that does not use a resolvable
1079 * private address, just remove the key so that
1080 * it is possible to use the controller white
1081 * list for scanning.
1082 *
1083 * Userspace will have been told to not store
1084 * this key at this point. So it is safe to
1085 * just remove it.
1086 */
1087 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +02001088 list_del_rcu(&smp->remote_irk->list);
1089 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001090 smp->remote_irk = NULL;
1091 }
1092 }
1093
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001094 if (smp->csrk) {
1095 smp->csrk->bdaddr_type = hcon->dst_type;
1096 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1097 mgmt_new_csrk(hdev, smp->csrk, persistent);
1098 }
1099
1100 if (smp->slave_csrk) {
1101 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1102 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1103 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1104 }
1105
1106 if (smp->ltk) {
1107 smp->ltk->bdaddr_type = hcon->dst_type;
1108 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1109 mgmt_new_ltk(hdev, smp->ltk, persistent);
1110 }
1111
1112 if (smp->slave_ltk) {
1113 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1114 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1115 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1116 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001117
1118 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001119 struct link_key *key;
1120 u8 type;
1121
1122 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1123 type = HCI_LK_DEBUG_COMBINATION;
1124 else if (hcon->sec_level == BT_SECURITY_FIPS)
1125 type = HCI_LK_AUTH_COMBINATION_P256;
1126 else
1127 type = HCI_LK_UNAUTH_COMBINATION_P256;
1128
1129 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1130 smp->link_key, type, 0, &persistent);
1131 if (key) {
1132 mgmt_new_link_key(hdev, key, persistent);
1133
1134 /* Don't keep debug keys around if the relevant
1135 * flag is not set.
1136 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001137 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001138 key->type == HCI_LK_DEBUG_COMBINATION) {
1139 list_del_rcu(&key->list);
1140 kfree_rcu(key, rcu);
1141 }
1142 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001143 }
1144}
1145
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001146static void sc_add_ltk(struct smp_chan *smp)
1147{
1148 struct hci_conn *hcon = smp->conn->hcon;
1149 u8 key_type, auth;
1150
1151 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1152 key_type = SMP_LTK_P256_DEBUG;
1153 else
1154 key_type = SMP_LTK_P256;
1155
1156 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1157 auth = 1;
1158 else
1159 auth = 0;
1160
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001161 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1162 key_type, auth, smp->tk, smp->enc_key_size,
1163 0, 0);
1164}
1165
Johan Hedberg6a770832014-06-06 11:54:04 +03001166static void sc_generate_link_key(struct smp_chan *smp)
1167{
1168 /* These constants are as specified in the core specification.
1169 * In ASCII they spell out to 'tmp1' and 'lebr'.
1170 */
1171 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1172 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1173
1174 smp->link_key = kzalloc(16, GFP_KERNEL);
1175 if (!smp->link_key)
1176 return;
1177
1178 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001179 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001180 smp->link_key = NULL;
1181 return;
1182 }
1183
1184 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001185 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001186 smp->link_key = NULL;
1187 return;
1188 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001189}
1190
Johan Hedbergb28b4942014-09-05 22:19:55 +03001191static void smp_allow_key_dist(struct smp_chan *smp)
1192{
1193 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1194 * will be allowed in each PDU handler to ensure we receive
1195 * them in the correct order.
1196 */
1197 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1198 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1199 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1200 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1201 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1202 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1203}
1204
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001205static void sc_generate_ltk(struct smp_chan *smp)
1206{
1207 /* These constants are as specified in the core specification.
1208 * In ASCII they spell out to 'tmp2' and 'brle'.
1209 */
1210 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1211 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1212 struct hci_conn *hcon = smp->conn->hcon;
1213 struct hci_dev *hdev = hcon->hdev;
1214 struct link_key *key;
1215
1216 key = hci_find_link_key(hdev, &hcon->dst);
1217 if (!key) {
1218 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1219 return;
1220 }
1221
1222 if (key->type == HCI_LK_DEBUG_COMBINATION)
1223 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1224
1225 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1226 return;
1227
1228 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1229 return;
1230
1231 sc_add_ltk(smp);
1232}
1233
Johan Hedbergd6268e82014-09-05 22:19:51 +03001234static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001235{
1236 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001237 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001238 struct hci_conn *hcon = conn->hcon;
1239 struct hci_dev *hdev = hcon->hdev;
1240 __u8 *keydist;
1241
1242 BT_DBG("conn %p", conn);
1243
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001244 rsp = (void *) &smp->prsp[1];
1245
1246 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001247 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1248 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001249 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001250 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001251
1252 req = (void *) &smp->preq[1];
1253
1254 if (hcon->out) {
1255 keydist = &rsp->init_key_dist;
1256 *keydist &= req->init_key_dist;
1257 } else {
1258 keydist = &rsp->resp_key_dist;
1259 *keydist &= req->resp_key_dist;
1260 }
1261
Johan Hedberg6a770832014-06-06 11:54:04 +03001262 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001263 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001264 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001265 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1266 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001267
1268 /* Clear the keys which are generated but not distributed */
1269 *keydist &= ~SMP_SC_NO_DIST;
1270 }
1271
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001272 BT_DBG("keydist 0x%x", *keydist);
1273
1274 if (*keydist & SMP_DIST_ENC_KEY) {
1275 struct smp_cmd_encrypt_info enc;
1276 struct smp_cmd_master_ident ident;
1277 struct smp_ltk *ltk;
1278 u8 authenticated;
1279 __le16 ediv;
1280 __le64 rand;
1281
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001282 /* Make sure we generate only the significant amount of
1283 * bytes based on the encryption key size, and set the rest
1284 * of the value to zeroes.
1285 */
1286 get_random_bytes(enc.ltk, smp->enc_key_size);
1287 memset(enc.ltk + smp->enc_key_size, 0,
1288 sizeof(enc.ltk) - smp->enc_key_size);
1289
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001290 get_random_bytes(&ediv, sizeof(ediv));
1291 get_random_bytes(&rand, sizeof(rand));
1292
1293 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1294
1295 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1296 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1297 SMP_LTK_SLAVE, authenticated, enc.ltk,
1298 smp->enc_key_size, ediv, rand);
1299 smp->slave_ltk = ltk;
1300
1301 ident.ediv = ediv;
1302 ident.rand = rand;
1303
1304 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1305
1306 *keydist &= ~SMP_DIST_ENC_KEY;
1307 }
1308
1309 if (*keydist & SMP_DIST_ID_KEY) {
1310 struct smp_cmd_ident_addr_info addrinfo;
1311 struct smp_cmd_ident_info idinfo;
1312
1313 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1314
1315 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1316
1317 /* The hci_conn contains the local identity address
1318 * after the connection has been established.
1319 *
1320 * This is true even when the connection has been
1321 * established using a resolvable random address.
1322 */
1323 bacpy(&addrinfo.bdaddr, &hcon->src);
1324 addrinfo.addr_type = hcon->src_type;
1325
1326 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1327 &addrinfo);
1328
1329 *keydist &= ~SMP_DIST_ID_KEY;
1330 }
1331
1332 if (*keydist & SMP_DIST_SIGN) {
1333 struct smp_cmd_sign_info sign;
1334 struct smp_csrk *csrk;
1335
1336 /* Generate a new random key */
1337 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1338
1339 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1340 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001341 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1342 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1343 else
1344 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001345 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1346 }
1347 smp->slave_csrk = csrk;
1348
1349 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1350
1351 *keydist &= ~SMP_DIST_SIGN;
1352 }
1353
1354 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001355 if (smp->remote_key_dist & KEY_DIST_MASK) {
1356 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001357 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001358 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001359
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001360 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1361 smp_notify_keys(conn);
1362
1363 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001364}
1365
Johan Hedbergb68fda62014-08-11 22:06:40 +03001366static void smp_timeout(struct work_struct *work)
1367{
1368 struct smp_chan *smp = container_of(work, struct smp_chan,
1369 security_timer.work);
1370 struct l2cap_conn *conn = smp->conn;
1371
1372 BT_DBG("conn %p", conn);
1373
Johan Hedberg1e91c292014-08-18 20:33:29 +03001374 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001375}
1376
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001377static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1378{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001379 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001380 struct smp_chan *smp;
1381
Marcel Holtmannf1560462013-10-13 05:43:25 -07001382 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001383 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001384 return NULL;
1385
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001386 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1387 if (IS_ERR(smp->tfm_aes)) {
1388 BT_ERR("Unable to create ECB crypto context");
Marcel Holtmann276812e2015-03-16 01:10:18 -07001389 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001390 return NULL;
1391 }
1392
Johan Hedberg407cecf2014-05-02 14:19:47 +03001393 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1394 if (IS_ERR(smp->tfm_cmac)) {
1395 BT_ERR("Unable to create CMAC crypto context");
1396 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann276812e2015-03-16 01:10:18 -07001397 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001398 return NULL;
1399 }
1400
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001401 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001402 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001403
Johan Hedbergb28b4942014-09-05 22:19:55 +03001404 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1405
Johan Hedbergb68fda62014-08-11 22:06:40 +03001406 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1407
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001408 hci_conn_hold(conn->hcon);
1409
1410 return smp;
1411}
1412
Johan Hedberg760b0182014-06-06 11:44:05 +03001413static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1414{
1415 struct hci_conn *hcon = smp->conn->hcon;
1416 u8 *na, *nb, a[7], b[7];
1417
1418 if (hcon->out) {
1419 na = smp->prnd;
1420 nb = smp->rrnd;
1421 } else {
1422 na = smp->rrnd;
1423 nb = smp->prnd;
1424 }
1425
1426 memcpy(a, &hcon->init_addr, 6);
1427 memcpy(b, &hcon->resp_addr, 6);
1428 a[6] = hcon->init_addr_type;
1429 b[6] = hcon->resp_addr_type;
1430
1431 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1432}
1433
Johan Hedberg38606f12014-06-25 11:10:28 +03001434static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001435{
1436 struct hci_conn *hcon = smp->conn->hcon;
1437 struct smp_cmd_dhkey_check check;
1438 u8 a[7], b[7], *local_addr, *remote_addr;
1439 u8 io_cap[3], r[16];
1440
Johan Hedberg760b0182014-06-06 11:44:05 +03001441 memcpy(a, &hcon->init_addr, 6);
1442 memcpy(b, &hcon->resp_addr, 6);
1443 a[6] = hcon->init_addr_type;
1444 b[6] = hcon->resp_addr_type;
1445
1446 if (hcon->out) {
1447 local_addr = a;
1448 remote_addr = b;
1449 memcpy(io_cap, &smp->preq[1], 3);
1450 } else {
1451 local_addr = b;
1452 remote_addr = a;
1453 memcpy(io_cap, &smp->prsp[1], 3);
1454 }
1455
Johan Hedbergdddd3052014-06-01 15:38:09 +03001456 memset(r, 0, sizeof(r));
1457
1458 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001459 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001460
Johan Hedberga29b0732014-10-28 15:17:05 +01001461 if (smp->method == REQ_OOB)
1462 memcpy(r, smp->rr, 16);
1463
Johan Hedberg760b0182014-06-06 11:44:05 +03001464 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1465 local_addr, remote_addr, check.e);
1466
1467 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001468}
1469
Johan Hedberg38606f12014-06-25 11:10:28 +03001470static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1471{
1472 struct l2cap_conn *conn = smp->conn;
1473 struct hci_conn *hcon = conn->hcon;
1474 struct smp_cmd_pairing_confirm cfm;
1475 u8 r;
1476
1477 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1478 r |= 0x80;
1479
1480 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1481
1482 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1483 cfm.confirm_val))
1484 return SMP_UNSPECIFIED;
1485
1486 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1487
1488 return 0;
1489}
1490
1491static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1492{
1493 struct l2cap_conn *conn = smp->conn;
1494 struct hci_conn *hcon = conn->hcon;
1495 struct hci_dev *hdev = hcon->hdev;
1496 u8 cfm[16], r;
1497
1498 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1499 if (smp->passkey_round >= 20)
1500 return 0;
1501
1502 switch (smp_op) {
1503 case SMP_CMD_PAIRING_RANDOM:
1504 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1505 r |= 0x80;
1506
1507 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1508 smp->rrnd, r, cfm))
1509 return SMP_UNSPECIFIED;
1510
1511 if (memcmp(smp->pcnf, cfm, 16))
1512 return SMP_CONFIRM_FAILED;
1513
1514 smp->passkey_round++;
1515
1516 if (smp->passkey_round == 20) {
1517 /* Generate MacKey and LTK */
1518 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1519 return SMP_UNSPECIFIED;
1520 }
1521
1522 /* The round is only complete when the initiator
1523 * receives pairing random.
1524 */
1525 if (!hcon->out) {
1526 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1527 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001528 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001529 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001530 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001531 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001532 return 0;
1533 }
1534
1535 /* Start the next round */
1536 if (smp->passkey_round != 20)
1537 return sc_passkey_round(smp, 0);
1538
1539 /* Passkey rounds are complete - start DHKey Check */
1540 sc_dhkey_check(smp);
1541 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1542
1543 break;
1544
1545 case SMP_CMD_PAIRING_CONFIRM:
1546 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1547 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1548 return 0;
1549 }
1550
1551 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1552
1553 if (hcon->out) {
1554 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1555 sizeof(smp->prnd), smp->prnd);
1556 return 0;
1557 }
1558
1559 return sc_passkey_send_confirm(smp);
1560
1561 case SMP_CMD_PUBLIC_KEY:
1562 default:
1563 /* Initiating device starts the round */
1564 if (!hcon->out)
1565 return 0;
1566
1567 BT_DBG("%s Starting passkey round %u", hdev->name,
1568 smp->passkey_round + 1);
1569
1570 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1571
1572 return sc_passkey_send_confirm(smp);
1573 }
1574
1575 return 0;
1576}
1577
Johan Hedbergdddd3052014-06-01 15:38:09 +03001578static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1579{
Johan Hedberg38606f12014-06-25 11:10:28 +03001580 struct l2cap_conn *conn = smp->conn;
1581 struct hci_conn *hcon = conn->hcon;
1582 u8 smp_op;
1583
1584 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1585
Johan Hedbergdddd3052014-06-01 15:38:09 +03001586 switch (mgmt_op) {
1587 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1588 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1589 return 0;
1590 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1591 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1592 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001593 case MGMT_OP_USER_PASSKEY_REPLY:
1594 hcon->passkey_notify = le32_to_cpu(passkey);
1595 smp->passkey_round = 0;
1596
1597 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1598 smp_op = SMP_CMD_PAIRING_CONFIRM;
1599 else
1600 smp_op = 0;
1601
1602 if (sc_passkey_round(smp, smp_op))
1603 return -EIO;
1604
1605 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001606 }
1607
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001608 /* Initiator sends DHKey check first */
1609 if (hcon->out) {
1610 sc_dhkey_check(smp);
1611 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1612 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1613 sc_dhkey_check(smp);
1614 sc_add_ltk(smp);
1615 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001616
1617 return 0;
1618}
1619
Brian Gix2b64d152011-12-21 16:12:12 -08001620int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1621{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001622 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001623 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001624 struct smp_chan *smp;
1625 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001626 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001627
1628 BT_DBG("");
1629
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001630 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001631 return -ENOTCONN;
1632
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001633 chan = conn->smp;
1634 if (!chan)
1635 return -ENOTCONN;
1636
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001637 l2cap_chan_lock(chan);
1638 if (!chan->data) {
1639 err = -ENOTCONN;
1640 goto unlock;
1641 }
1642
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001643 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001644
Johan Hedberg760b0182014-06-06 11:44:05 +03001645 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1646 err = sc_user_reply(smp, mgmt_op, passkey);
1647 goto unlock;
1648 }
1649
Brian Gix2b64d152011-12-21 16:12:12 -08001650 switch (mgmt_op) {
1651 case MGMT_OP_USER_PASSKEY_REPLY:
1652 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001653 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001654 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001655 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001656 /* Fall Through */
1657 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001658 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001659 break;
1660 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1661 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001662 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001663 err = 0;
1664 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001665 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001666 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001667 err = -EOPNOTSUPP;
1668 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001669 }
1670
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001671 err = 0;
1672
Brian Gix2b64d152011-12-21 16:12:12 -08001673 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001674 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1675 u8 rsp = smp_confirm(smp);
1676 if (rsp)
1677 smp_failure(conn, rsp);
1678 }
Brian Gix2b64d152011-12-21 16:12:12 -08001679
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001680unlock:
1681 l2cap_chan_unlock(chan);
1682 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001683}
1684
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001685static void build_bredr_pairing_cmd(struct smp_chan *smp,
1686 struct smp_cmd_pairing *req,
1687 struct smp_cmd_pairing *rsp)
1688{
1689 struct l2cap_conn *conn = smp->conn;
1690 struct hci_dev *hdev = conn->hcon->hdev;
1691 u8 local_dist = 0, remote_dist = 0;
1692
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001693 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001694 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1695 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1696 }
1697
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001698 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001699 remote_dist |= SMP_DIST_ID_KEY;
1700
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001701 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001702 local_dist |= SMP_DIST_ID_KEY;
1703
1704 if (!rsp) {
1705 memset(req, 0, sizeof(*req));
1706
1707 req->init_key_dist = local_dist;
1708 req->resp_key_dist = remote_dist;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001709 req->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001710
1711 smp->remote_key_dist = remote_dist;
1712
1713 return;
1714 }
1715
1716 memset(rsp, 0, sizeof(*rsp));
1717
Johan Hedberge3f6a252015-06-11 13:52:30 +03001718 rsp->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001719 rsp->init_key_dist = req->init_key_dist & remote_dist;
1720 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1721
1722 smp->remote_key_dist = rsp->init_key_dist;
1723}
1724
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001725static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001726{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001727 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001728 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001729 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001730 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001731 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001732 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001733
1734 BT_DBG("conn %p", conn);
1735
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001736 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001737 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001738
Johan Hedberg40bef302014-07-16 11:42:27 +03001739 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001740 return SMP_CMD_NOTSUPP;
1741
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001742 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001743 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001744 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001745 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001746
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001747 if (!smp)
1748 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001749
Johan Hedbergc05b9332014-09-10 17:37:42 -07001750 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001751 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001752
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001753 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001754 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001755 return SMP_PAIRING_NOTSUPP;
1756
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001757 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001758 return SMP_AUTH_REQUIREMENTS;
1759
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001760 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1761 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001762 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001763
Johan Hedbergcb06d362015-03-16 21:12:34 +02001764 /* If the remote side's OOB flag is set it means it has
1765 * successfully received our local OOB data - therefore set the
1766 * flag to indicate that local OOB is in use.
1767 */
Johan Hedberg58428562015-03-16 11:45:45 +02001768 if (req->oob_flag == SMP_OOB_PRESENT)
1769 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1770
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001771 /* SMP over BR/EDR requires special treatment */
1772 if (conn->hcon->type == ACL_LINK) {
1773 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001774 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001775 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001776 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1777
1778 set_bit(SMP_FLAG_SC, &smp->flags);
1779
1780 build_bredr_pairing_cmd(smp, req, &rsp);
1781
1782 key_size = min(req->max_key_size, rsp.max_key_size);
1783 if (check_enc_key_size(conn, key_size))
1784 return SMP_ENC_KEY_SIZE;
1785
1786 /* Clear bits which are generated but not distributed */
1787 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1788
1789 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1790 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1791 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1792
1793 smp_distribute_keys(smp);
1794 return 0;
1795 }
1796
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001797 build_pairing_cmd(conn, req, &rsp, auth);
1798
1799 if (rsp.auth_req & SMP_AUTH_SC)
1800 set_bit(SMP_FLAG_SC, &smp->flags);
1801
Johan Hedberg5be5e272014-09-10 17:58:54 -07001802 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001803 sec_level = BT_SECURITY_MEDIUM;
1804 else
1805 sec_level = authreq_to_seclevel(auth);
1806
Johan Hedbergc7262e72014-06-17 13:07:37 +03001807 if (sec_level > conn->hcon->pending_sec_level)
1808 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001809
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001810 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001811 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1812 u8 method;
1813
1814 method = get_auth_method(smp, conn->hcon->io_capability,
1815 req->io_capability);
1816 if (method == JUST_WORKS || method == JUST_CFM)
1817 return SMP_AUTH_REQUIREMENTS;
1818 }
1819
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001820 key_size = min(req->max_key_size, rsp.max_key_size);
1821 if (check_enc_key_size(conn, key_size))
1822 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001823
Johan Hedberge84a6b12013-12-02 10:49:03 +02001824 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001825
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001826 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1827 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001828
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001829 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001830
1831 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1832
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001833 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1834 * SC case, however some implementations incorrectly copy RFU auth
1835 * req bits from our security request, which may create a false
1836 * positive SC enablement.
1837 */
1838 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1839
Johan Hedberg3b191462014-06-06 10:50:15 +03001840 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1841 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1842 /* Clear bits which are generated but not distributed */
1843 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1844 /* Wait for Public Key from Initiating Device */
1845 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001846 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001847
Brian Gix2b64d152011-12-21 16:12:12 -08001848 /* Request setup of TK */
1849 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1850 if (ret)
1851 return SMP_UNSPECIFIED;
1852
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001853 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001854}
1855
Johan Hedberg3b191462014-06-06 10:50:15 +03001856static u8 sc_send_public_key(struct smp_chan *smp)
1857{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001858 struct hci_dev *hdev = smp->conn->hcon->hdev;
1859
Johan Hedberg3b191462014-06-06 10:50:15 +03001860 BT_DBG("");
1861
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001862 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001863 struct l2cap_chan *chan = hdev->smp_data;
1864 struct smp_dev *smp_dev;
1865
1866 if (!chan || !chan->data)
1867 return SMP_UNSPECIFIED;
1868
1869 smp_dev = chan->data;
1870
1871 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1872 memcpy(smp->local_sk, smp_dev->local_sk, 32);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001873 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001874
1875 if (smp_dev->debug_key)
1876 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1877
1878 goto done;
1879 }
1880
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001881 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001882 BT_DBG("Using debug keys");
1883 memcpy(smp->local_pk, debug_pk, 64);
1884 memcpy(smp->local_sk, debug_sk, 32);
1885 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1886 } else {
1887 while (true) {
1888 /* Generate local key pair for Secure Connections */
1889 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1890 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001891
Johan Hedberg70157ef2014-06-24 15:22:59 +03001892 /* This is unlikely, but we need to check that
1893 * we didn't accidentially generate a debug key.
1894 */
1895 if (memcmp(smp->local_sk, debug_sk, 32))
1896 break;
1897 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001898 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001899
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001900done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001901 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001902 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001903 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001904
1905 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1906
1907 return 0;
1908}
1909
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001910static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001911{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001912 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001913 struct l2cap_chan *chan = conn->smp;
1914 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001915 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001916 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001917 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001918
1919 BT_DBG("conn %p", conn);
1920
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001921 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001922 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001923
Johan Hedberg40bef302014-07-16 11:42:27 +03001924 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001925 return SMP_CMD_NOTSUPP;
1926
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001927 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001928
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001929 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001930
1931 key_size = min(req->max_key_size, rsp->max_key_size);
1932 if (check_enc_key_size(conn, key_size))
1933 return SMP_ENC_KEY_SIZE;
1934
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001935 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001936
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001937 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001938 return SMP_AUTH_REQUIREMENTS;
1939
Johan Hedbergcb06d362015-03-16 21:12:34 +02001940 /* If the remote side's OOB flag is set it means it has
1941 * successfully received our local OOB data - therefore set the
1942 * flag to indicate that local OOB is in use.
1943 */
Johan Hedberg58428562015-03-16 11:45:45 +02001944 if (rsp->oob_flag == SMP_OOB_PRESENT)
1945 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1946
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001947 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1948 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1949
1950 /* Update remote key distribution in case the remote cleared
1951 * some bits that we had enabled in our request.
1952 */
1953 smp->remote_key_dist &= rsp->resp_key_dist;
1954
1955 /* For BR/EDR this means we're done and can start phase 3 */
1956 if (conn->hcon->type == ACL_LINK) {
1957 /* Clear bits which are generated but not distributed */
1958 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1959 smp_distribute_keys(smp);
1960 return 0;
1961 }
1962
Johan Hedberg65668772014-05-16 11:03:34 +03001963 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1964 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001965 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1966 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001967
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001968 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001969 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1970 u8 method;
1971
1972 method = get_auth_method(smp, req->io_capability,
1973 rsp->io_capability);
1974 if (method == JUST_WORKS || method == JUST_CFM)
1975 return SMP_AUTH_REQUIREMENTS;
1976 }
1977
Johan Hedberge84a6b12013-12-02 10:49:03 +02001978 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001979
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001980 /* Update remote key distribution in case the remote cleared
1981 * some bits that we had enabled in our request.
1982 */
1983 smp->remote_key_dist &= rsp->resp_key_dist;
1984
Johan Hedberg3b191462014-06-06 10:50:15 +03001985 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1986 /* Clear bits which are generated but not distributed */
1987 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1988 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1989 return sc_send_public_key(smp);
1990 }
1991
Johan Hedbergc05b9332014-09-10 17:37:42 -07001992 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001993
Johan Hedberg476585e2012-06-06 18:54:15 +08001994 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001995 if (ret)
1996 return SMP_UNSPECIFIED;
1997
Johan Hedberg4a74d652014-05-20 09:45:50 +03001998 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001999
2000 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03002001 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002002 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002003
2004 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002005}
2006
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002007static u8 sc_check_confirm(struct smp_chan *smp)
2008{
2009 struct l2cap_conn *conn = smp->conn;
2010
2011 BT_DBG("");
2012
Johan Hedberg38606f12014-06-25 11:10:28 +03002013 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2014 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2015
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002016 if (conn->hcon->out) {
2017 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2018 smp->prnd);
2019 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2020 }
2021
2022 return 0;
2023}
2024
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002025/* Work-around for some implementations that incorrectly copy RFU bits
2026 * from our security request and thereby create the impression that
2027 * we're doing SC when in fact the remote doesn't support it.
2028 */
2029static int fixup_sc_false_positive(struct smp_chan *smp)
2030{
2031 struct l2cap_conn *conn = smp->conn;
2032 struct hci_conn *hcon = conn->hcon;
2033 struct hci_dev *hdev = hcon->hdev;
2034 struct smp_cmd_pairing *req, *rsp;
2035 u8 auth;
2036
2037 /* The issue is only observed when we're in slave role */
2038 if (hcon->out)
2039 return SMP_UNSPECIFIED;
2040
2041 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
2042 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2043 return SMP_UNSPECIFIED;
2044 }
2045
2046 BT_ERR("Trying to fall back to legacy SMP");
2047
2048 req = (void *) &smp->preq[1];
2049 rsp = (void *) &smp->prsp[1];
2050
2051 /* Rebuild key dist flags which may have been cleared for SC */
2052 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2053
2054 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2055
2056 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
2057 BT_ERR("Failed to fall back to legacy SMP");
2058 return SMP_UNSPECIFIED;
2059 }
2060
2061 clear_bit(SMP_FLAG_SC, &smp->flags);
2062
2063 return 0;
2064}
2065
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002066static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002067{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002068 struct l2cap_chan *chan = conn->smp;
2069 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002070
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002071 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2072
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002073 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002074 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002075
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002076 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2077 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002078
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002079 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2080 int ret;
2081
2082 /* Public Key exchange must happen before any other steps */
2083 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2084 return sc_check_confirm(smp);
2085
2086 BT_ERR("Unexpected SMP Pairing Confirm");
2087
2088 ret = fixup_sc_false_positive(smp);
2089 if (ret)
2090 return ret;
2091 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002092
Johan Hedbergb28b4942014-09-05 22:19:55 +03002093 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002094 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2095 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002096 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2097 return 0;
2098 }
2099
2100 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002101 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002102
2103 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002104
2105 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002106}
2107
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002108static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002109{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002110 struct l2cap_chan *chan = conn->smp;
2111 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002112 struct hci_conn *hcon = conn->hcon;
2113 u8 *pkax, *pkbx, *na, *nb;
2114 u32 passkey;
2115 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002116
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002117 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002118
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002119 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002120 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002121
Johan Hedberg943a7322014-03-18 12:58:24 +02002122 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002123 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002124
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002125 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2126 return smp_random(smp);
2127
Johan Hedberg580039e2014-12-03 16:26:37 +02002128 if (hcon->out) {
2129 pkax = smp->local_pk;
2130 pkbx = smp->remote_pk;
2131 na = smp->prnd;
2132 nb = smp->rrnd;
2133 } else {
2134 pkax = smp->remote_pk;
2135 pkbx = smp->local_pk;
2136 na = smp->rrnd;
2137 nb = smp->prnd;
2138 }
2139
Johan Hedberga29b0732014-10-28 15:17:05 +01002140 if (smp->method == REQ_OOB) {
2141 if (!hcon->out)
2142 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2143 sizeof(smp->prnd), smp->prnd);
2144 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2145 goto mackey_and_ltk;
2146 }
2147
Johan Hedberg38606f12014-06-25 11:10:28 +03002148 /* Passkey entry has special treatment */
2149 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2150 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2151
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002152 if (hcon->out) {
2153 u8 cfm[16];
2154
2155 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2156 smp->rrnd, 0, cfm);
2157 if (err)
2158 return SMP_UNSPECIFIED;
2159
2160 if (memcmp(smp->pcnf, cfm, 16))
2161 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002162 } else {
2163 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2164 smp->prnd);
2165 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002166 }
2167
Johan Hedberga29b0732014-10-28 15:17:05 +01002168mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002169 /* Generate MacKey and LTK */
2170 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2171 if (err)
2172 return SMP_UNSPECIFIED;
2173
Johan Hedberga29b0732014-10-28 15:17:05 +01002174 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002175 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002176 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002177 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2178 }
2179 return 0;
2180 }
2181
Johan Hedberg38606f12014-06-25 11:10:28 +03002182 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002183 if (err)
2184 return SMP_UNSPECIFIED;
2185
Johan Hedberg38606f12014-06-25 11:10:28 +03002186 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2187 hcon->dst_type, passkey, 0);
2188 if (err)
2189 return SMP_UNSPECIFIED;
2190
2191 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2192
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002193 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002194}
2195
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002196static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002197{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002198 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002199 struct hci_conn *hcon = conn->hcon;
2200
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002201 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002202 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002203 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002204
Johan Hedberga6f78332014-09-10 17:37:45 -07002205 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002206 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002207
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002208 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002209 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002210
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002211 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002212 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002213
Johan Hedbergfe59a052014-07-01 19:14:12 +03002214 /* We never store STKs for master role, so clear this flag */
2215 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2216
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002217 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002218}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002219
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002220bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2221 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002222{
2223 if (sec_level == BT_SECURITY_LOW)
2224 return true;
2225
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002226 /* If we're encrypted with an STK but the caller prefers using
2227 * LTK claim insufficient security. This way we allow the
2228 * connection to be re-encrypted with an LTK, even if the LTK
2229 * provides the same level of security. Only exception is if we
2230 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002231 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002232 if (key_pref == SMP_USE_LTK &&
2233 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002234 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002235 return false;
2236
Johan Hedberg854f4722014-07-01 18:40:20 +03002237 if (hcon->sec_level >= sec_level)
2238 return true;
2239
2240 return false;
2241}
2242
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002243static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002244{
2245 struct smp_cmd_security_req *rp = (void *) skb->data;
2246 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002247 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002248 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002249 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002250 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002251
2252 BT_DBG("conn %p", conn);
2253
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002254 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002255 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002256
Johan Hedberg40bef302014-07-16 11:42:27 +03002257 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002258 return SMP_CMD_NOTSUPP;
2259
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002260 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002261
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002262 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002263 return SMP_AUTH_REQUIREMENTS;
2264
Johan Hedberg5be5e272014-09-10 17:58:54 -07002265 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002266 sec_level = BT_SECURITY_MEDIUM;
2267 else
2268 sec_level = authreq_to_seclevel(auth);
2269
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002270 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002271 return 0;
2272
Johan Hedbergc7262e72014-06-17 13:07:37 +03002273 if (sec_level > hcon->pending_sec_level)
2274 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002275
Johan Hedberg4dab7862012-06-07 14:58:37 +08002276 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002277 return 0;
2278
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002279 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002280 if (!smp)
2281 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002282
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002283 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002284 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002285 return SMP_PAIRING_NOTSUPP;
2286
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002287 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002288
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002289 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002290 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002291
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002292 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2293 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002294
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002295 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002296 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002297
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002298 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002299}
2300
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002301int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002302{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002303 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002304 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002305 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002306 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002307 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002308
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002309 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2310
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002311 /* This may be NULL if there's an unexpected disconnection */
2312 if (!conn)
2313 return 1;
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 Hedbergd8949aa2015-09-04 12:22:46 +03002328 chan = conn->smp;
2329 if (!chan) {
2330 BT_ERR("SMP security requested but not available");
2331 return 1;
2332 }
2333
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002334 l2cap_chan_lock(chan);
2335
2336 /* If SMP is already in progress ignore this request */
2337 if (chan->data) {
2338 ret = 0;
2339 goto unlock;
2340 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002341
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002342 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002343 if (!smp) {
2344 ret = 1;
2345 goto unlock;
2346 }
Brian Gix2b64d152011-12-21 16:12:12 -08002347
2348 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002349
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002350 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002351 authreq |= SMP_AUTH_SC;
2352
Johan Hedberg79897d22014-06-01 09:45:24 +03002353 /* Require MITM if IO Capability allows or the security level
2354 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002355 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002356 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002357 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002358 authreq |= SMP_AUTH_MITM;
2359
Johan Hedberg40bef302014-07-16 11:42:27 +03002360 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002361 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002362
Brian Gix2b64d152011-12-21 16:12:12 -08002363 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002364 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2365 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002366
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002367 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002368 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002369 } else {
2370 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002371 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002372 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002373 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002374 }
2375
Johan Hedberg4a74d652014-05-20 09:45:50 +03002376 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002377 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002378
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002379unlock:
2380 l2cap_chan_unlock(chan);
2381 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002382}
2383
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002384static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2385{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002386 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002387 struct l2cap_chan *chan = conn->smp;
2388 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002389
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002390 BT_DBG("conn %p", conn);
2391
2392 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002393 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002394
Johan Hedbergb28b4942014-09-05 22:19:55 +03002395 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002396
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002397 skb_pull(skb, sizeof(*rp));
2398
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002399 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002400
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002401 return 0;
2402}
2403
2404static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2405{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002406 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002407 struct l2cap_chan *chan = conn->smp;
2408 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002409 struct hci_dev *hdev = conn->hcon->hdev;
2410 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002411 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002412 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002413
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002414 BT_DBG("conn %p", conn);
2415
2416 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002417 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002418
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002419 /* Mark the information as received */
2420 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2421
Johan Hedbergb28b4942014-09-05 22:19:55 +03002422 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2423 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002424 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2425 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002426
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002427 skb_pull(skb, sizeof(*rp));
2428
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002429 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002430 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002431 authenticated, smp->tk, smp->enc_key_size,
2432 rp->ediv, rp->rand);
2433 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002434 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002435 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002436
2437 return 0;
2438}
2439
Johan Hedbergfd349c02014-02-18 10:19:36 +02002440static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2441{
2442 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002443 struct l2cap_chan *chan = conn->smp;
2444 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002445
2446 BT_DBG("");
2447
2448 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002449 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002450
Johan Hedbergb28b4942014-09-05 22:19:55 +03002451 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002452
Johan Hedbergfd349c02014-02-18 10:19:36 +02002453 skb_pull(skb, sizeof(*info));
2454
2455 memcpy(smp->irk, info->irk, 16);
2456
2457 return 0;
2458}
2459
2460static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2461 struct sk_buff *skb)
2462{
2463 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002464 struct l2cap_chan *chan = conn->smp;
2465 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002466 struct hci_conn *hcon = conn->hcon;
2467 bdaddr_t rpa;
2468
2469 BT_DBG("");
2470
2471 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002472 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002473
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002474 /* Mark the information as received */
2475 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2476
Johan Hedbergb28b4942014-09-05 22:19:55 +03002477 if (smp->remote_key_dist & SMP_DIST_SIGN)
2478 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2479
Johan Hedbergfd349c02014-02-18 10:19:36 +02002480 skb_pull(skb, sizeof(*info));
2481
Johan Hedberga9a58f82014-02-25 22:24:37 +02002482 /* Strictly speaking the Core Specification (4.1) allows sending
2483 * an empty address which would force us to rely on just the IRK
2484 * as "identity information". However, since such
2485 * implementations are not known of and in order to not over
2486 * complicate our implementation, simply pretend that we never
2487 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002488 *
2489 * The Identity Address must also be a Static Random or Public
2490 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002491 */
Johan Hedberge12af482015-01-14 20:51:37 +02002492 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2493 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002494 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002495 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002496 }
2497
Johan Hedbergfd349c02014-02-18 10:19:36 +02002498 bacpy(&smp->id_addr, &info->bdaddr);
2499 smp->id_addr_type = info->addr_type;
2500
2501 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2502 bacpy(&rpa, &hcon->dst);
2503 else
2504 bacpy(&rpa, BDADDR_ANY);
2505
Johan Hedberg23d0e122014-02-19 14:57:46 +02002506 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2507 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002508
Johan Hedberg31dd6242014-06-27 14:23:02 +03002509distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002510 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2511 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002512
2513 return 0;
2514}
2515
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002516static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2517{
2518 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002519 struct l2cap_chan *chan = conn->smp;
2520 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002521 struct smp_csrk *csrk;
2522
2523 BT_DBG("conn %p", conn);
2524
2525 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002526 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002527
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002528 /* Mark the information as received */
2529 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2530
2531 skb_pull(skb, sizeof(*rp));
2532
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002533 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2534 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002535 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2536 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2537 else
2538 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002539 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2540 }
2541 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002542 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002543
2544 return 0;
2545}
2546
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002547static u8 sc_select_method(struct smp_chan *smp)
2548{
2549 struct l2cap_conn *conn = smp->conn;
2550 struct hci_conn *hcon = conn->hcon;
2551 struct smp_cmd_pairing *local, *remote;
2552 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2553
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002554 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2555 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002556 return REQ_OOB;
2557
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002558 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2559 * which are needed as inputs to some crypto functions. To get
2560 * the "struct smp_cmd_pairing" from them we need to skip the
2561 * first byte which contains the opcode.
2562 */
2563 if (hcon->out) {
2564 local = (void *) &smp->preq[1];
2565 remote = (void *) &smp->prsp[1];
2566 } else {
2567 local = (void *) &smp->prsp[1];
2568 remote = (void *) &smp->preq[1];
2569 }
2570
2571 local_io = local->io_capability;
2572 remote_io = remote->io_capability;
2573
2574 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2575 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2576
2577 /* If either side wants MITM, look up the method from the table,
2578 * otherwise use JUST WORKS.
2579 */
2580 if (local_mitm || remote_mitm)
2581 method = get_auth_method(smp, local_io, remote_io);
2582 else
2583 method = JUST_WORKS;
2584
2585 /* Don't confirm locally initiated pairing attempts */
2586 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2587 method = JUST_WORKS;
2588
2589 return method;
2590}
2591
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002592static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2593{
2594 struct smp_cmd_public_key *key = (void *) skb->data;
2595 struct hci_conn *hcon = conn->hcon;
2596 struct l2cap_chan *chan = conn->smp;
2597 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002598 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002599 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002600 int err;
2601
2602 BT_DBG("conn %p", conn);
2603
2604 if (skb->len < sizeof(*key))
2605 return SMP_INVALID_PARAMS;
2606
2607 memcpy(smp->remote_pk, key, 64);
2608
Johan Hedberga8ca6172015-03-16 18:12:57 +02002609 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2610 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2611 smp->rr, 0, cfm.confirm_val);
2612 if (err)
2613 return SMP_UNSPECIFIED;
2614
2615 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2616 return SMP_CONFIRM_FAILED;
2617 }
2618
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002619 /* Non-initiating device sends its public key after receiving
2620 * the key from the initiating device.
2621 */
2622 if (!hcon->out) {
2623 err = sc_send_public_key(smp);
2624 if (err)
2625 return err;
2626 }
2627
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002628 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002629 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002630
2631 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2632 return SMP_UNSPECIFIED;
2633
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002634 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002635
2636 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2637
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002638 smp->method = sc_select_method(smp);
2639
2640 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2641
2642 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2643 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2644 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2645 else
2646 hcon->pending_sec_level = BT_SECURITY_FIPS;
2647
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002648 if (!memcmp(debug_pk, smp->remote_pk, 64))
2649 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2650
Johan Hedberg38606f12014-06-25 11:10:28 +03002651 if (smp->method == DSP_PASSKEY) {
2652 get_random_bytes(&hcon->passkey_notify,
2653 sizeof(hcon->passkey_notify));
2654 hcon->passkey_notify %= 1000000;
2655 hcon->passkey_entered = 0;
2656 smp->passkey_round = 0;
2657 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2658 hcon->dst_type,
2659 hcon->passkey_notify,
2660 hcon->passkey_entered))
2661 return SMP_UNSPECIFIED;
2662 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2663 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2664 }
2665
Johan Hedberg94ea7252015-03-16 11:45:46 +02002666 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002667 if (hcon->out)
2668 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2669 sizeof(smp->prnd), smp->prnd);
2670
2671 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2672
2673 return 0;
2674 }
2675
Johan Hedberg38606f12014-06-25 11:10:28 +03002676 if (hcon->out)
2677 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2678
2679 if (smp->method == REQ_PASSKEY) {
2680 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2681 hcon->dst_type))
2682 return SMP_UNSPECIFIED;
2683 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2684 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2685 return 0;
2686 }
2687
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002688 /* The Initiating device waits for the non-initiating device to
2689 * send the confirm value.
2690 */
2691 if (conn->hcon->out)
2692 return 0;
2693
2694 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2695 0, cfm.confirm_val);
2696 if (err)
2697 return SMP_UNSPECIFIED;
2698
2699 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2700 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2701
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002702 return 0;
2703}
2704
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002705static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2706{
2707 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2708 struct l2cap_chan *chan = conn->smp;
2709 struct hci_conn *hcon = conn->hcon;
2710 struct smp_chan *smp = chan->data;
2711 u8 a[7], b[7], *local_addr, *remote_addr;
2712 u8 io_cap[3], r[16], e[16];
2713 int err;
2714
2715 BT_DBG("conn %p", conn);
2716
2717 if (skb->len < sizeof(*check))
2718 return SMP_INVALID_PARAMS;
2719
2720 memcpy(a, &hcon->init_addr, 6);
2721 memcpy(b, &hcon->resp_addr, 6);
2722 a[6] = hcon->init_addr_type;
2723 b[6] = hcon->resp_addr_type;
2724
2725 if (hcon->out) {
2726 local_addr = a;
2727 remote_addr = b;
2728 memcpy(io_cap, &smp->prsp[1], 3);
2729 } else {
2730 local_addr = b;
2731 remote_addr = a;
2732 memcpy(io_cap, &smp->preq[1], 3);
2733 }
2734
2735 memset(r, 0, sizeof(r));
2736
Johan Hedberg38606f12014-06-25 11:10:28 +03002737 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2738 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002739 else if (smp->method == REQ_OOB)
2740 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002741
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002742 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2743 io_cap, remote_addr, local_addr, e);
2744 if (err)
2745 return SMP_UNSPECIFIED;
2746
2747 if (memcmp(check->e, e, 16))
2748 return SMP_DHKEY_CHECK_FAILED;
2749
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002750 if (!hcon->out) {
2751 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2752 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2753 return 0;
2754 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002755
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002756 /* Slave sends DHKey check as response to master */
2757 sc_dhkey_check(smp);
2758 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002759
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002760 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002761
2762 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002763 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002764 hcon->enc_key_size = smp->enc_key_size;
2765 }
2766
2767 return 0;
2768}
2769
Johan Hedberg1408bb62014-06-04 22:45:57 +03002770static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2771 struct sk_buff *skb)
2772{
2773 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2774
2775 BT_DBG("value 0x%02x", kp->value);
2776
2777 return 0;
2778}
2779
Johan Hedberg4befb862014-08-11 22:06:38 +03002780static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002781{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002782 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002783 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002784 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002785 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002786 int err = 0;
2787
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002788 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002789 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002790
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002791 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002792 reason = SMP_PAIRING_NOTSUPP;
2793 goto done;
2794 }
2795
Marcel Holtmann92381f52013-10-03 01:23:08 -07002796 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002797 skb_pull(skb, sizeof(code));
2798
Johan Hedbergb28b4942014-09-05 22:19:55 +03002799 smp = chan->data;
2800
2801 if (code > SMP_CMD_MAX)
2802 goto drop;
2803
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002804 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002805 goto drop;
2806
2807 /* If we don't have a context the only allowed commands are
2808 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002809 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002810 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2811 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002812
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002813 switch (code) {
2814 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002815 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002816 break;
2817
2818 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002819 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002820 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002821 break;
2822
2823 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002824 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002825 break;
2826
2827 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002828 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002829 break;
2830
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002831 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002832 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002833 break;
2834
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002835 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002836 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002837 break;
2838
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002839 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002840 reason = smp_cmd_encrypt_info(conn, skb);
2841 break;
2842
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002843 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002844 reason = smp_cmd_master_ident(conn, skb);
2845 break;
2846
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002847 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002848 reason = smp_cmd_ident_info(conn, skb);
2849 break;
2850
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002851 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002852 reason = smp_cmd_ident_addr_info(conn, skb);
2853 break;
2854
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002855 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002856 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002857 break;
2858
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002859 case SMP_CMD_PUBLIC_KEY:
2860 reason = smp_cmd_public_key(conn, skb);
2861 break;
2862
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002863 case SMP_CMD_DHKEY_CHECK:
2864 reason = smp_cmd_dhkey_check(conn, skb);
2865 break;
2866
Johan Hedberg1408bb62014-06-04 22:45:57 +03002867 case SMP_CMD_KEYPRESS_NOTIFY:
2868 reason = smp_cmd_keypress_notify(conn, skb);
2869 break;
2870
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002871 default:
2872 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002873 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002874 goto done;
2875 }
2876
2877done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002878 if (!err) {
2879 if (reason)
2880 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002881 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002882 }
2883
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002884 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002885
2886drop:
2887 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2888 code, &hcon->dst);
2889 kfree_skb(skb);
2890 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002891}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002892
Johan Hedberg70db83c2014-08-08 09:37:16 +03002893static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2894{
2895 struct l2cap_conn *conn = chan->conn;
2896
2897 BT_DBG("chan %p", chan);
2898
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002899 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002900 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002901
Johan Hedberg70db83c2014-08-08 09:37:16 +03002902 conn->smp = NULL;
2903 l2cap_chan_put(chan);
2904}
2905
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002906static void bredr_pairing(struct l2cap_chan *chan)
2907{
2908 struct l2cap_conn *conn = chan->conn;
2909 struct hci_conn *hcon = conn->hcon;
2910 struct hci_dev *hdev = hcon->hdev;
2911 struct smp_cmd_pairing req;
2912 struct smp_chan *smp;
2913
2914 BT_DBG("chan %p", chan);
2915
2916 /* Only new pairings are interesting */
2917 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2918 return;
2919
2920 /* Don't bother if we're not encrypted */
2921 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2922 return;
2923
2924 /* Only master may initiate SMP over BR/EDR */
2925 if (hcon->role != HCI_ROLE_MASTER)
2926 return;
2927
2928 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002929 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002930 return;
2931
2932 /* BR/EDR must use Secure Connections for SMP */
2933 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002934 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002935 return;
2936
2937 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002938 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002939 return;
2940
2941 /* Don't bother if remote LE support is not enabled */
2942 if (!lmp_host_le_capable(hcon))
2943 return;
2944
2945 /* Remote must support SMP fixed chan for BR/EDR */
2946 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2947 return;
2948
2949 /* Don't bother if SMP is already ongoing */
2950 if (chan->data)
2951 return;
2952
2953 smp = smp_chan_create(conn);
2954 if (!smp) {
2955 BT_ERR("%s unable to create SMP context for BR/EDR",
2956 hdev->name);
2957 return;
2958 }
2959
2960 set_bit(SMP_FLAG_SC, &smp->flags);
2961
2962 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2963
2964 /* Prepare and send the BR/EDR SMP Pairing Request */
2965 build_bredr_pairing_cmd(smp, &req, NULL);
2966
2967 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2968 memcpy(&smp->preq[1], &req, sizeof(req));
2969
2970 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2971 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2972}
2973
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002974static void smp_resume_cb(struct l2cap_chan *chan)
2975{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002976 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002977 struct l2cap_conn *conn = chan->conn;
2978 struct hci_conn *hcon = conn->hcon;
2979
2980 BT_DBG("chan %p", chan);
2981
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002982 if (hcon->type == ACL_LINK) {
2983 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002984 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002985 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002986
Johan Hedberg86d14072014-08-11 22:06:43 +03002987 if (!smp)
2988 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002989
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002990 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2991 return;
2992
Johan Hedberg86d14072014-08-11 22:06:43 +03002993 cancel_delayed_work(&smp->security_timer);
2994
Johan Hedbergd6268e82014-09-05 22:19:51 +03002995 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002996}
2997
Johan Hedberg70db83c2014-08-08 09:37:16 +03002998static void smp_ready_cb(struct l2cap_chan *chan)
2999{
3000 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003001 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003002
3003 BT_DBG("chan %p", chan);
3004
3005 conn->smp = chan;
3006 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003007
3008 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3009 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003010}
3011
Johan Hedberg4befb862014-08-11 22:06:38 +03003012static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3013{
3014 int err;
3015
3016 BT_DBG("chan %p", chan);
3017
3018 err = smp_sig_channel(chan, skb);
3019 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003020 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003021
Johan Hedbergb68fda62014-08-11 22:06:40 +03003022 if (smp)
3023 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003024
Johan Hedberg1e91c292014-08-18 20:33:29 +03003025 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003026 }
3027
3028 return err;
3029}
3030
Johan Hedberg70db83c2014-08-08 09:37:16 +03003031static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3032 unsigned long hdr_len,
3033 unsigned long len, int nb)
3034{
3035 struct sk_buff *skb;
3036
3037 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3038 if (!skb)
3039 return ERR_PTR(-ENOMEM);
3040
3041 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003042 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003043
3044 return skb;
3045}
3046
3047static const struct l2cap_ops smp_chan_ops = {
3048 .name = "Security Manager",
3049 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003050 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003051 .alloc_skb = smp_alloc_skb_cb,
3052 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003053 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003054
3055 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003056 .state_change = l2cap_chan_no_state_change,
3057 .close = l2cap_chan_no_close,
3058 .defer = l2cap_chan_no_defer,
3059 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003060 .set_shutdown = l2cap_chan_no_set_shutdown,
3061 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003062};
3063
3064static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3065{
3066 struct l2cap_chan *chan;
3067
3068 BT_DBG("pchan %p", pchan);
3069
3070 chan = l2cap_chan_create();
3071 if (!chan)
3072 return NULL;
3073
3074 chan->chan_type = pchan->chan_type;
3075 chan->ops = &smp_chan_ops;
3076 chan->scid = pchan->scid;
3077 chan->dcid = chan->scid;
3078 chan->imtu = pchan->imtu;
3079 chan->omtu = pchan->omtu;
3080 chan->mode = pchan->mode;
3081
Johan Hedbergabe84902014-11-12 22:22:21 +02003082 /* Other L2CAP channels may request SMP routines in order to
3083 * change the security level. This means that the SMP channel
3084 * lock must be considered in its own category to avoid lockdep
3085 * warnings.
3086 */
3087 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3088
Johan Hedberg70db83c2014-08-08 09:37:16 +03003089 BT_DBG("created chan %p", chan);
3090
3091 return chan;
3092}
3093
3094static const struct l2cap_ops smp_root_chan_ops = {
3095 .name = "Security Manager Root",
3096 .new_connection = smp_new_conn_cb,
3097
3098 /* None of these are implemented for the root channel */
3099 .close = l2cap_chan_no_close,
3100 .alloc_skb = l2cap_chan_no_alloc_skb,
3101 .recv = l2cap_chan_no_recv,
3102 .state_change = l2cap_chan_no_state_change,
3103 .teardown = l2cap_chan_no_teardown,
3104 .ready = l2cap_chan_no_ready,
3105 .defer = l2cap_chan_no_defer,
3106 .suspend = l2cap_chan_no_suspend,
3107 .resume = l2cap_chan_no_resume,
3108 .set_shutdown = l2cap_chan_no_set_shutdown,
3109 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003110};
3111
Johan Hedbergef8efe42014-08-13 15:12:32 +03003112static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003113{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003114 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003115 struct smp_dev *smp;
3116 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003117 struct crypto_hash *tfm_cmac;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003118
Johan Hedbergef8efe42014-08-13 15:12:32 +03003119 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003120 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003121 goto create_chan;
3122 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003123
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003124 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3125 if (!smp)
3126 return ERR_PTR(-ENOMEM);
3127
3128 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003129 if (IS_ERR(tfm_aes)) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003130 BT_ERR("Unable to create ECB crypto context");
3131 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003132 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003133 }
3134
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003135 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3136 if (IS_ERR(tfm_cmac)) {
3137 BT_ERR("Unable to create CMAC crypto context");
3138 crypto_free_blkcipher(tfm_aes);
3139 kzfree(smp);
3140 return ERR_CAST(tfm_cmac);
3141 }
3142
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003143 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003144 smp->tfm_cmac = tfm_cmac;
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003145 smp->min_key_size = SMP_MIN_ENC_KEY_SIZE;
Johan Hedberg2fd36552015-06-11 13:52:26 +03003146 smp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003147
Johan Hedbergef8efe42014-08-13 15:12:32 +03003148create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003149 chan = l2cap_chan_create();
3150 if (!chan) {
Marcel Holtmann63511f62015-03-17 11:38:24 -07003151 if (smp) {
3152 crypto_free_blkcipher(smp->tfm_aes);
3153 crypto_free_hash(smp->tfm_cmac);
3154 kzfree(smp);
3155 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003156 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003157 }
3158
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003159 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003160
Johan Hedbergef8efe42014-08-13 15:12:32 +03003161 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003162
3163 l2cap_chan_set_defaults(chan);
3164
Marcel Holtmann157029b2015-01-14 15:43:09 -08003165 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003166 u8 bdaddr_type;
3167
3168 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3169
3170 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003171 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003172 else
3173 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003174 } else {
3175 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003176 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003177 }
3178
Johan Hedberg70db83c2014-08-08 09:37:16 +03003179 chan->state = BT_LISTEN;
3180 chan->mode = L2CAP_MODE_BASIC;
3181 chan->imtu = L2CAP_DEFAULT_MTU;
3182 chan->ops = &smp_root_chan_ops;
3183
Johan Hedbergabe84902014-11-12 22:22:21 +02003184 /* Set correct nesting level for a parent/listening channel */
3185 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3186
Johan Hedbergef8efe42014-08-13 15:12:32 +03003187 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003188}
3189
Johan Hedbergef8efe42014-08-13 15:12:32 +03003190static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003191{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003192 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003193
Johan Hedbergef8efe42014-08-13 15:12:32 +03003194 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003195
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003196 smp = chan->data;
3197 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003198 chan->data = NULL;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003199 if (smp->tfm_aes)
3200 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003201 if (smp->tfm_cmac)
3202 crypto_free_hash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003203 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003204 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003205
Johan Hedberg70db83c2014-08-08 09:37:16 +03003206 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003207}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003208
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003209static ssize_t force_bredr_smp_read(struct file *file,
3210 char __user *user_buf,
3211 size_t count, loff_t *ppos)
3212{
3213 struct hci_dev *hdev = file->private_data;
3214 char buf[3];
3215
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003216 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003217 buf[1] = '\n';
3218 buf[2] = '\0';
3219 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3220}
3221
3222static ssize_t force_bredr_smp_write(struct file *file,
3223 const char __user *user_buf,
3224 size_t count, loff_t *ppos)
3225{
3226 struct hci_dev *hdev = file->private_data;
3227 char buf[32];
3228 size_t buf_size = min(count, (sizeof(buf)-1));
3229 bool enable;
3230
3231 if (copy_from_user(buf, user_buf, buf_size))
3232 return -EFAULT;
3233
3234 buf[buf_size] = '\0';
3235 if (strtobool(buf, &enable))
3236 return -EINVAL;
3237
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003238 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003239 return -EALREADY;
3240
3241 if (enable) {
3242 struct l2cap_chan *chan;
3243
3244 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3245 if (IS_ERR(chan))
3246 return PTR_ERR(chan);
3247
3248 hdev->smp_bredr_data = chan;
3249 } else {
3250 struct l2cap_chan *chan;
3251
3252 chan = hdev->smp_bredr_data;
3253 hdev->smp_bredr_data = NULL;
3254 smp_del_chan(chan);
3255 }
3256
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003257 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003258
3259 return count;
3260}
3261
3262static const struct file_operations force_bredr_smp_fops = {
3263 .open = simple_open,
3264 .read = force_bredr_smp_read,
3265 .write = force_bredr_smp_write,
3266 .llseek = default_llseek,
3267};
3268
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003269static ssize_t le_min_key_size_read(struct file *file,
3270 char __user *user_buf,
3271 size_t count, loff_t *ppos)
3272{
3273 struct hci_dev *hdev = file->private_data;
3274 char buf[4];
3275
3276 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->min_key_size);
3277
3278 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3279}
3280
3281static ssize_t le_min_key_size_write(struct file *file,
3282 const char __user *user_buf,
3283 size_t count, loff_t *ppos)
3284{
3285 struct hci_dev *hdev = file->private_data;
3286 char buf[32];
3287 size_t buf_size = min(count, (sizeof(buf) - 1));
3288 u8 key_size;
3289
3290 if (copy_from_user(buf, user_buf, buf_size))
3291 return -EFAULT;
3292
3293 buf[buf_size] = '\0';
3294
3295 sscanf(buf, "%hhu", &key_size);
3296
3297 if (key_size > SMP_DEV(hdev)->max_key_size ||
3298 key_size < SMP_MIN_ENC_KEY_SIZE)
3299 return -EINVAL;
3300
3301 SMP_DEV(hdev)->min_key_size = key_size;
3302
3303 return count;
3304}
3305
3306static const struct file_operations le_min_key_size_fops = {
3307 .open = simple_open,
3308 .read = le_min_key_size_read,
3309 .write = le_min_key_size_write,
3310 .llseek = default_llseek,
3311};
3312
Johan Hedberg2fd36552015-06-11 13:52:26 +03003313static ssize_t le_max_key_size_read(struct file *file,
3314 char __user *user_buf,
3315 size_t count, loff_t *ppos)
3316{
3317 struct hci_dev *hdev = file->private_data;
3318 char buf[4];
3319
3320 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->max_key_size);
3321
3322 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3323}
3324
3325static ssize_t le_max_key_size_write(struct file *file,
3326 const char __user *user_buf,
3327 size_t count, loff_t *ppos)
3328{
3329 struct hci_dev *hdev = file->private_data;
3330 char buf[32];
3331 size_t buf_size = min(count, (sizeof(buf) - 1));
3332 u8 key_size;
3333
3334 if (copy_from_user(buf, user_buf, buf_size))
3335 return -EFAULT;
3336
3337 buf[buf_size] = '\0';
3338
3339 sscanf(buf, "%hhu", &key_size);
3340
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003341 if (key_size > SMP_MAX_ENC_KEY_SIZE ||
3342 key_size < SMP_DEV(hdev)->min_key_size)
Johan Hedberg2fd36552015-06-11 13:52:26 +03003343 return -EINVAL;
3344
3345 SMP_DEV(hdev)->max_key_size = key_size;
3346
3347 return count;
3348}
3349
3350static const struct file_operations le_max_key_size_fops = {
3351 .open = simple_open,
3352 .read = le_max_key_size_read,
3353 .write = le_max_key_size_write,
3354 .llseek = default_llseek,
3355};
3356
Johan Hedbergef8efe42014-08-13 15:12:32 +03003357int smp_register(struct hci_dev *hdev)
3358{
3359 struct l2cap_chan *chan;
3360
3361 BT_DBG("%s", hdev->name);
3362
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003363 /* If the controller does not support Low Energy operation, then
3364 * there is also no need to register any SMP channel.
3365 */
3366 if (!lmp_le_capable(hdev))
3367 return 0;
3368
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003369 if (WARN_ON(hdev->smp_data)) {
3370 chan = hdev->smp_data;
3371 hdev->smp_data = NULL;
3372 smp_del_chan(chan);
3373 }
3374
Johan Hedbergef8efe42014-08-13 15:12:32 +03003375 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3376 if (IS_ERR(chan))
3377 return PTR_ERR(chan);
3378
3379 hdev->smp_data = chan;
3380
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003381 debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
3382 &le_min_key_size_fops);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003383 debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3384 &le_max_key_size_fops);
3385
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003386 /* If the controller does not support BR/EDR Secure Connections
3387 * feature, then the BR/EDR SMP channel shall not be present.
3388 *
3389 * To test this with Bluetooth 4.0 controllers, create a debugfs
3390 * switch that allows forcing BR/EDR SMP support and accepting
3391 * cross-transport pairing on non-AES encrypted connections.
3392 */
3393 if (!lmp_sc_capable(hdev)) {
3394 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3395 hdev, &force_bredr_smp_fops);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003396 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003397 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003398
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003399 if (WARN_ON(hdev->smp_bredr_data)) {
3400 chan = hdev->smp_bredr_data;
3401 hdev->smp_bredr_data = NULL;
3402 smp_del_chan(chan);
3403 }
3404
Johan Hedbergef8efe42014-08-13 15:12:32 +03003405 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3406 if (IS_ERR(chan)) {
3407 int err = PTR_ERR(chan);
3408 chan = hdev->smp_data;
3409 hdev->smp_data = NULL;
3410 smp_del_chan(chan);
3411 return err;
3412 }
3413
3414 hdev->smp_bredr_data = chan;
3415
3416 return 0;
3417}
3418
3419void smp_unregister(struct hci_dev *hdev)
3420{
3421 struct l2cap_chan *chan;
3422
3423 if (hdev->smp_bredr_data) {
3424 chan = hdev->smp_bredr_data;
3425 hdev->smp_bredr_data = NULL;
3426 smp_del_chan(chan);
3427 }
3428
3429 if (hdev->smp_data) {
3430 chan = hdev->smp_data;
3431 hdev->smp_data = NULL;
3432 smp_del_chan(chan);
3433 }
3434}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003435
3436#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3437
Johan Hedbergcfc41982014-12-30 09:50:40 +02003438static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3439{
3440 const u8 irk[16] = {
3441 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3442 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3443 const u8 r[3] = { 0x94, 0x81, 0x70 };
3444 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3445 u8 res[3];
3446 int err;
3447
3448 err = smp_ah(tfm_aes, irk, r, res);
3449 if (err)
3450 return err;
3451
3452 if (memcmp(res, exp, 3))
3453 return -EINVAL;
3454
3455 return 0;
3456}
3457
3458static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3459{
3460 const u8 k[16] = {
3461 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3462 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3463 const u8 r[16] = {
3464 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3465 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3466 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3467 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3468 const u8 _iat = 0x01;
3469 const u8 _rat = 0x00;
3470 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3471 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3472 const u8 exp[16] = {
3473 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3474 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3475 u8 res[16];
3476 int err;
3477
3478 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3479 if (err)
3480 return err;
3481
3482 if (memcmp(res, exp, 16))
3483 return -EINVAL;
3484
3485 return 0;
3486}
3487
3488static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3489{
3490 const u8 k[16] = {
3491 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3492 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3493 const u8 r1[16] = {
3494 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3495 const u8 r2[16] = {
3496 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3497 const u8 exp[16] = {
3498 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3499 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3500 u8 res[16];
3501 int err;
3502
3503 err = smp_s1(tfm_aes, k, r1, r2, res);
3504 if (err)
3505 return err;
3506
3507 if (memcmp(res, exp, 16))
3508 return -EINVAL;
3509
3510 return 0;
3511}
3512
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003513static int __init test_f4(struct crypto_hash *tfm_cmac)
3514{
3515 const u8 u[32] = {
3516 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3517 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3518 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3519 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3520 const u8 v[32] = {
3521 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3522 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3523 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3524 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3525 const u8 x[16] = {
3526 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3527 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3528 const u8 z = 0x00;
3529 const u8 exp[16] = {
3530 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3531 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3532 u8 res[16];
3533 int err;
3534
3535 err = smp_f4(tfm_cmac, u, v, x, z, res);
3536 if (err)
3537 return err;
3538
3539 if (memcmp(res, exp, 16))
3540 return -EINVAL;
3541
3542 return 0;
3543}
3544
3545static int __init test_f5(struct crypto_hash *tfm_cmac)
3546{
3547 const u8 w[32] = {
3548 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3549 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3550 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3551 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3552 const u8 n1[16] = {
3553 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3554 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3555 const u8 n2[16] = {
3556 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3557 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3558 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3559 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3560 const u8 exp_ltk[16] = {
3561 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3562 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3563 const u8 exp_mackey[16] = {
3564 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3565 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3566 u8 mackey[16], ltk[16];
3567 int err;
3568
3569 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3570 if (err)
3571 return err;
3572
3573 if (memcmp(mackey, exp_mackey, 16))
3574 return -EINVAL;
3575
3576 if (memcmp(ltk, exp_ltk, 16))
3577 return -EINVAL;
3578
3579 return 0;
3580}
3581
3582static int __init test_f6(struct crypto_hash *tfm_cmac)
3583{
3584 const u8 w[16] = {
3585 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3586 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3587 const u8 n1[16] = {
3588 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3589 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3590 const u8 n2[16] = {
3591 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3592 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3593 const u8 r[16] = {
3594 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3595 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3596 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3597 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3598 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3599 const u8 exp[16] = {
3600 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3601 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3602 u8 res[16];
3603 int err;
3604
3605 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3606 if (err)
3607 return err;
3608
3609 if (memcmp(res, exp, 16))
3610 return -EINVAL;
3611
3612 return 0;
3613}
3614
3615static int __init test_g2(struct crypto_hash *tfm_cmac)
3616{
3617 const u8 u[32] = {
3618 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3619 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3620 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3621 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3622 const u8 v[32] = {
3623 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3624 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3625 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3626 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3627 const u8 x[16] = {
3628 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3629 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3630 const u8 y[16] = {
3631 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3632 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3633 const u32 exp_val = 0x2f9ed5ba % 1000000;
3634 u32 val;
3635 int err;
3636
3637 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3638 if (err)
3639 return err;
3640
3641 if (val != exp_val)
3642 return -EINVAL;
3643
3644 return 0;
3645}
3646
3647static int __init test_h6(struct crypto_hash *tfm_cmac)
3648{
3649 const u8 w[16] = {
3650 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3651 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3652 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3653 const u8 exp[16] = {
3654 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3655 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3656 u8 res[16];
3657 int err;
3658
3659 err = smp_h6(tfm_cmac, w, key_id, res);
3660 if (err)
3661 return err;
3662
3663 if (memcmp(res, exp, 16))
3664 return -EINVAL;
3665
3666 return 0;
3667}
3668
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003669static char test_smp_buffer[32];
3670
3671static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3672 size_t count, loff_t *ppos)
3673{
3674 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3675 strlen(test_smp_buffer));
3676}
3677
3678static const struct file_operations test_smp_fops = {
3679 .open = simple_open,
3680 .read = test_smp_read,
3681 .llseek = default_llseek,
3682};
3683
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003684static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3685 struct crypto_hash *tfm_cmac)
3686{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003687 ktime_t calltime, delta, rettime;
3688 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003689 int err;
3690
Marcel Holtmann255047b2014-12-30 00:11:20 -08003691 calltime = ktime_get();
3692
Johan Hedbergcfc41982014-12-30 09:50:40 +02003693 err = test_ah(tfm_aes);
3694 if (err) {
3695 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003696 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003697 }
3698
3699 err = test_c1(tfm_aes);
3700 if (err) {
3701 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003702 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003703 }
3704
3705 err = test_s1(tfm_aes);
3706 if (err) {
3707 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003708 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003709 }
3710
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003711 err = test_f4(tfm_cmac);
3712 if (err) {
3713 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003714 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003715 }
3716
3717 err = test_f5(tfm_cmac);
3718 if (err) {
3719 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003720 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003721 }
3722
3723 err = test_f6(tfm_cmac);
3724 if (err) {
3725 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003726 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003727 }
3728
3729 err = test_g2(tfm_cmac);
3730 if (err) {
3731 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003732 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003733 }
3734
3735 err = test_h6(tfm_cmac);
3736 if (err) {
3737 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003738 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003739 }
3740
Marcel Holtmann255047b2014-12-30 00:11:20 -08003741 rettime = ktime_get();
3742 delta = ktime_sub(rettime, calltime);
3743 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3744
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003745 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003746
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003747done:
3748 if (!err)
3749 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3750 "PASS (%llu usecs)\n", duration);
3751 else
3752 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3753
3754 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3755 &test_smp_fops);
3756
3757 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003758}
3759
3760int __init bt_selftest_smp(void)
3761{
3762 struct crypto_blkcipher *tfm_aes;
3763 struct crypto_hash *tfm_cmac;
3764 int err;
3765
3766 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3767 if (IS_ERR(tfm_aes)) {
3768 BT_ERR("Unable to create ECB crypto context");
3769 return PTR_ERR(tfm_aes);
3770 }
3771
3772 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3773 if (IS_ERR(tfm_cmac)) {
3774 BT_ERR("Unable to create CMAC crypto context");
3775 crypto_free_blkcipher(tfm_aes);
3776 return PTR_ERR(tfm_cmac);
3777 }
3778
3779 err = run_selftests(tfm_aes, tfm_cmac);
3780
3781 crypto_free_hash(tfm_cmac);
3782 crypto_free_blkcipher(tfm_aes);
3783
3784 return err;
3785}
3786
3787#endif