blob: c91353841e40500790c13d2ce894460e1cbbe9e3 [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
Johan Hedberge1e930f2014-09-08 17:09:49 -0700814 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300815
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300816 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300817 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800818}
819
Brian Gix2b64d152011-12-21 16:12:12 -0800820#define JUST_WORKS 0x00
821#define JUST_CFM 0x01
822#define REQ_PASSKEY 0x02
823#define CFM_PASSKEY 0x03
824#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300825#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800826#define OVERLAP 0xFF
827
828static const u8 gen_method[5][5] = {
829 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
830 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
831 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
832 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
833 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
834};
835
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300836static const u8 sc_method[5][5] = {
837 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
838 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
839 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
840 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
841 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
842};
843
Johan Hedberg581370c2014-06-17 13:07:38 +0300844static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
845{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300846 /* If either side has unknown io_caps, use JUST_CFM (which gets
847 * converted later to JUST_WORKS if we're initiators.
848 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300849 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
850 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300851 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300852
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300853 if (test_bit(SMP_FLAG_SC, &smp->flags))
854 return sc_method[remote_io][local_io];
855
Johan Hedberg581370c2014-06-17 13:07:38 +0300856 return gen_method[remote_io][local_io];
857}
858
Brian Gix2b64d152011-12-21 16:12:12 -0800859static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
860 u8 local_io, u8 remote_io)
861{
862 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300863 struct l2cap_chan *chan = conn->smp;
864 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800865 u32 passkey = 0;
866 int ret = 0;
867
868 /* Initialize key for JUST WORKS */
869 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300870 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800871
872 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
873
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300874 /* If neither side wants MITM, either "just" confirm an incoming
875 * request or use just-works for outgoing ones. The JUST_CFM
876 * will be converted to JUST_WORKS if necessary later in this
877 * function. If either side has MITM look up the method from the
878 * table.
879 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300880 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300881 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800882 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300883 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800884
Johan Hedberga82505c2014-03-24 14:39:07 +0200885 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300886 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
887 &smp->flags))
888 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200889
Johan Hedberg02f3e252014-07-16 15:09:13 +0300890 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300891 if (smp->method == JUST_CFM &&
892 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
893 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300894
Brian Gix2b64d152011-12-21 16:12:12 -0800895 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300896 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300897 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800898 return 0;
899 }
900
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200901 /* If this function is used for SC -> legacy fallback we
902 * can only recover the just-works case.
903 */
904 if (test_bit(SMP_FLAG_SC, &smp->flags))
905 return -EINVAL;
906
Brian Gix2b64d152011-12-21 16:12:12 -0800907 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300908 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300909 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300910 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
911 hcon->pending_sec_level = BT_SECURITY_HIGH;
912 }
Brian Gix2b64d152011-12-21 16:12:12 -0800913
914 /* If both devices have Keyoard-Display I/O, the master
915 * Confirms and the slave Enters the passkey.
916 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300917 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300918 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300919 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800920 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300921 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800922 }
923
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200924 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300925 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200926 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800927 get_random_bytes(&passkey, sizeof(passkey));
928 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200929 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800930 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300931 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800932 }
933
Johan Hedberg783e0572014-05-31 18:48:26 +0300934 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700935 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200936 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300937 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200938 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
939 hcon->type, hcon->dst_type,
940 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800941 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200942 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200943 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200944 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800945
Brian Gix2b64d152011-12-21 16:12:12 -0800946 return ret;
947}
948
Johan Hedberg1cc61142014-05-20 09:45:52 +0300949static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300950{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300951 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300952 struct smp_cmd_pairing_confirm cp;
953 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300954
955 BT_DBG("conn %p", conn);
956
Johan Hedberge491eaf2014-10-25 21:15:37 +0200957 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200958 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200959 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
960 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300961 if (ret)
962 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300963
Johan Hedberg4a74d652014-05-20 09:45:50 +0300964 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800965
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300966 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
967
Johan Hedbergb28b4942014-09-05 22:19:55 +0300968 if (conn->hcon->out)
969 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
970 else
971 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
972
Johan Hedberg1cc61142014-05-20 09:45:52 +0300973 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300974}
975
Johan Hedberg861580a2014-05-20 09:45:51 +0300976static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300977{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300978 struct l2cap_conn *conn = smp->conn;
979 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300980 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300981 int ret;
982
Johan Hedbergec70f362014-06-27 14:23:04 +0300983 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300984 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300985
986 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
987
Johan Hedberge491eaf2014-10-25 21:15:37 +0200988 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200989 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200990 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300991 if (ret)
992 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300993
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300994 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
995 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300996 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300997 }
998
999 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001000 u8 stk[16];
1001 __le64 rand = 0;
1002 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001003
Johan Hedberge491eaf2014-10-25 21:15:37 +02001004 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001005
Johan Hedberg861580a2014-05-20 09:45:51 +03001006 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1007 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001008
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001009 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001010 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001011 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001012 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001013 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001014 __le64 rand = 0;
1015 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001016
Johan Hedberg943a7322014-03-18 12:58:24 +02001017 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1018 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001019
Johan Hedberge491eaf2014-10-25 21:15:37 +02001020 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001021
Johan Hedbergfff34902014-06-10 15:19:50 +03001022 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1023 auth = 1;
1024 else
1025 auth = 0;
1026
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001027 /* Even though there's no _SLAVE suffix this is the
1028 * slave STK we're adding for later lookup (the master
1029 * STK never needs to be stored).
1030 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001031 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001032 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001033 }
1034
Johan Hedberg861580a2014-05-20 09:45:51 +03001035 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001036}
1037
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001038static void smp_notify_keys(struct l2cap_conn *conn)
1039{
1040 struct l2cap_chan *chan = conn->smp;
1041 struct smp_chan *smp = chan->data;
1042 struct hci_conn *hcon = conn->hcon;
1043 struct hci_dev *hdev = hcon->hdev;
1044 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1045 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1046 bool persistent;
1047
Johan Hedbergcad20c22015-10-12 13:36:19 +02001048 if (hcon->type == ACL_LINK) {
1049 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1050 persistent = false;
1051 else
1052 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1053 &hcon->flags);
1054 } else {
1055 /* The LTKs, IRKs and CSRKs should be persistent only if
1056 * both sides had the bonding bit set in their
1057 * authentication requests.
1058 */
1059 persistent = !!((req->auth_req & rsp->auth_req) &
1060 SMP_AUTH_BONDING);
1061 }
1062
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001063 if (smp->remote_irk) {
Johan Hedbergcad20c22015-10-12 13:36:19 +02001064 mgmt_new_irk(hdev, smp->remote_irk, persistent);
1065
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001066 /* Now that user space can be considered to know the
1067 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001068 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001069 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001070 if (hcon->type == LE_LINK) {
1071 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1072 hcon->dst_type = smp->remote_irk->addr_type;
1073 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1074 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001075
1076 /* When receiving an indentity resolving key for
1077 * a remote device that does not use a resolvable
1078 * private address, just remove the key so that
1079 * it is possible to use the controller white
1080 * list for scanning.
1081 *
1082 * Userspace will have been told to not store
1083 * this key at this point. So it is safe to
1084 * just remove it.
1085 */
1086 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +02001087 list_del_rcu(&smp->remote_irk->list);
1088 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001089 smp->remote_irk = NULL;
1090 }
1091 }
1092
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001093 if (smp->csrk) {
1094 smp->csrk->bdaddr_type = hcon->dst_type;
1095 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1096 mgmt_new_csrk(hdev, smp->csrk, persistent);
1097 }
1098
1099 if (smp->slave_csrk) {
1100 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1101 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1102 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1103 }
1104
1105 if (smp->ltk) {
1106 smp->ltk->bdaddr_type = hcon->dst_type;
1107 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1108 mgmt_new_ltk(hdev, smp->ltk, persistent);
1109 }
1110
1111 if (smp->slave_ltk) {
1112 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1113 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1114 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1115 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001116
1117 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001118 struct link_key *key;
1119 u8 type;
1120
1121 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1122 type = HCI_LK_DEBUG_COMBINATION;
1123 else if (hcon->sec_level == BT_SECURITY_FIPS)
1124 type = HCI_LK_AUTH_COMBINATION_P256;
1125 else
1126 type = HCI_LK_UNAUTH_COMBINATION_P256;
1127
1128 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1129 smp->link_key, type, 0, &persistent);
1130 if (key) {
1131 mgmt_new_link_key(hdev, key, persistent);
1132
1133 /* Don't keep debug keys around if the relevant
1134 * flag is not set.
1135 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001136 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001137 key->type == HCI_LK_DEBUG_COMBINATION) {
1138 list_del_rcu(&key->list);
1139 kfree_rcu(key, rcu);
1140 }
1141 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001142 }
1143}
1144
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001145static void sc_add_ltk(struct smp_chan *smp)
1146{
1147 struct hci_conn *hcon = smp->conn->hcon;
1148 u8 key_type, auth;
1149
1150 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1151 key_type = SMP_LTK_P256_DEBUG;
1152 else
1153 key_type = SMP_LTK_P256;
1154
1155 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1156 auth = 1;
1157 else
1158 auth = 0;
1159
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001160 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1161 key_type, auth, smp->tk, smp->enc_key_size,
1162 0, 0);
1163}
1164
Johan Hedberg6a770832014-06-06 11:54:04 +03001165static void sc_generate_link_key(struct smp_chan *smp)
1166{
1167 /* These constants are as specified in the core specification.
1168 * In ASCII they spell out to 'tmp1' and 'lebr'.
1169 */
1170 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1171 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1172
1173 smp->link_key = kzalloc(16, GFP_KERNEL);
1174 if (!smp->link_key)
1175 return;
1176
1177 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001178 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001179 smp->link_key = NULL;
1180 return;
1181 }
1182
1183 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001184 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001185 smp->link_key = NULL;
1186 return;
1187 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001188}
1189
Johan Hedbergb28b4942014-09-05 22:19:55 +03001190static void smp_allow_key_dist(struct smp_chan *smp)
1191{
1192 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1193 * will be allowed in each PDU handler to ensure we receive
1194 * them in the correct order.
1195 */
1196 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1197 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1198 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1199 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1200 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1201 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1202}
1203
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001204static void sc_generate_ltk(struct smp_chan *smp)
1205{
1206 /* These constants are as specified in the core specification.
1207 * In ASCII they spell out to 'tmp2' and 'brle'.
1208 */
1209 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1210 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1211 struct hci_conn *hcon = smp->conn->hcon;
1212 struct hci_dev *hdev = hcon->hdev;
1213 struct link_key *key;
1214
1215 key = hci_find_link_key(hdev, &hcon->dst);
1216 if (!key) {
1217 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1218 return;
1219 }
1220
1221 if (key->type == HCI_LK_DEBUG_COMBINATION)
1222 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1223
1224 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1225 return;
1226
1227 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1228 return;
1229
1230 sc_add_ltk(smp);
1231}
1232
Johan Hedbergd6268e82014-09-05 22:19:51 +03001233static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001234{
1235 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001236 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001237 struct hci_conn *hcon = conn->hcon;
1238 struct hci_dev *hdev = hcon->hdev;
1239 __u8 *keydist;
1240
1241 BT_DBG("conn %p", conn);
1242
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001243 rsp = (void *) &smp->prsp[1];
1244
1245 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001246 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1247 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001248 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001249 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001250
1251 req = (void *) &smp->preq[1];
1252
1253 if (hcon->out) {
1254 keydist = &rsp->init_key_dist;
1255 *keydist &= req->init_key_dist;
1256 } else {
1257 keydist = &rsp->resp_key_dist;
1258 *keydist &= req->resp_key_dist;
1259 }
1260
Johan Hedberg6a770832014-06-06 11:54:04 +03001261 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001262 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001263 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001264 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1265 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001266
1267 /* Clear the keys which are generated but not distributed */
1268 *keydist &= ~SMP_SC_NO_DIST;
1269 }
1270
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001271 BT_DBG("keydist 0x%x", *keydist);
1272
1273 if (*keydist & SMP_DIST_ENC_KEY) {
1274 struct smp_cmd_encrypt_info enc;
1275 struct smp_cmd_master_ident ident;
1276 struct smp_ltk *ltk;
1277 u8 authenticated;
1278 __le16 ediv;
1279 __le64 rand;
1280
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001281 /* Make sure we generate only the significant amount of
1282 * bytes based on the encryption key size, and set the rest
1283 * of the value to zeroes.
1284 */
1285 get_random_bytes(enc.ltk, smp->enc_key_size);
1286 memset(enc.ltk + smp->enc_key_size, 0,
1287 sizeof(enc.ltk) - smp->enc_key_size);
1288
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001289 get_random_bytes(&ediv, sizeof(ediv));
1290 get_random_bytes(&rand, sizeof(rand));
1291
1292 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1293
1294 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1295 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1296 SMP_LTK_SLAVE, authenticated, enc.ltk,
1297 smp->enc_key_size, ediv, rand);
1298 smp->slave_ltk = ltk;
1299
1300 ident.ediv = ediv;
1301 ident.rand = rand;
1302
1303 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1304
1305 *keydist &= ~SMP_DIST_ENC_KEY;
1306 }
1307
1308 if (*keydist & SMP_DIST_ID_KEY) {
1309 struct smp_cmd_ident_addr_info addrinfo;
1310 struct smp_cmd_ident_info idinfo;
1311
1312 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1313
1314 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1315
1316 /* The hci_conn contains the local identity address
1317 * after the connection has been established.
1318 *
1319 * This is true even when the connection has been
1320 * established using a resolvable random address.
1321 */
1322 bacpy(&addrinfo.bdaddr, &hcon->src);
1323 addrinfo.addr_type = hcon->src_type;
1324
1325 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1326 &addrinfo);
1327
1328 *keydist &= ~SMP_DIST_ID_KEY;
1329 }
1330
1331 if (*keydist & SMP_DIST_SIGN) {
1332 struct smp_cmd_sign_info sign;
1333 struct smp_csrk *csrk;
1334
1335 /* Generate a new random key */
1336 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1337
1338 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1339 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001340 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1341 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1342 else
1343 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001344 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1345 }
1346 smp->slave_csrk = csrk;
1347
1348 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1349
1350 *keydist &= ~SMP_DIST_SIGN;
1351 }
1352
1353 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001354 if (smp->remote_key_dist & KEY_DIST_MASK) {
1355 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001356 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001357 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001358
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001359 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1360 smp_notify_keys(conn);
1361
1362 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001363}
1364
Johan Hedbergb68fda62014-08-11 22:06:40 +03001365static void smp_timeout(struct work_struct *work)
1366{
1367 struct smp_chan *smp = container_of(work, struct smp_chan,
1368 security_timer.work);
1369 struct l2cap_conn *conn = smp->conn;
1370
1371 BT_DBG("conn %p", conn);
1372
Johan Hedberg1e91c292014-08-18 20:33:29 +03001373 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001374}
1375
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001376static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1377{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001378 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001379 struct smp_chan *smp;
1380
Marcel Holtmannf1560462013-10-13 05:43:25 -07001381 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001382 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001383 return NULL;
1384
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001385 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1386 if (IS_ERR(smp->tfm_aes)) {
1387 BT_ERR("Unable to create ECB crypto context");
Marcel Holtmann276812e2015-03-16 01:10:18 -07001388 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001389 return NULL;
1390 }
1391
Johan Hedberg407cecf2014-05-02 14:19:47 +03001392 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1393 if (IS_ERR(smp->tfm_cmac)) {
1394 BT_ERR("Unable to create CMAC crypto context");
1395 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann276812e2015-03-16 01:10:18 -07001396 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001397 return NULL;
1398 }
1399
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001400 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001401 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001402
Johan Hedbergb28b4942014-09-05 22:19:55 +03001403 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1404
Johan Hedbergb68fda62014-08-11 22:06:40 +03001405 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1406
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001407 hci_conn_hold(conn->hcon);
1408
1409 return smp;
1410}
1411
Johan Hedberg760b0182014-06-06 11:44:05 +03001412static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1413{
1414 struct hci_conn *hcon = smp->conn->hcon;
1415 u8 *na, *nb, a[7], b[7];
1416
1417 if (hcon->out) {
1418 na = smp->prnd;
1419 nb = smp->rrnd;
1420 } else {
1421 na = smp->rrnd;
1422 nb = smp->prnd;
1423 }
1424
1425 memcpy(a, &hcon->init_addr, 6);
1426 memcpy(b, &hcon->resp_addr, 6);
1427 a[6] = hcon->init_addr_type;
1428 b[6] = hcon->resp_addr_type;
1429
1430 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1431}
1432
Johan Hedberg38606f12014-06-25 11:10:28 +03001433static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001434{
1435 struct hci_conn *hcon = smp->conn->hcon;
1436 struct smp_cmd_dhkey_check check;
1437 u8 a[7], b[7], *local_addr, *remote_addr;
1438 u8 io_cap[3], r[16];
1439
Johan Hedberg760b0182014-06-06 11:44:05 +03001440 memcpy(a, &hcon->init_addr, 6);
1441 memcpy(b, &hcon->resp_addr, 6);
1442 a[6] = hcon->init_addr_type;
1443 b[6] = hcon->resp_addr_type;
1444
1445 if (hcon->out) {
1446 local_addr = a;
1447 remote_addr = b;
1448 memcpy(io_cap, &smp->preq[1], 3);
1449 } else {
1450 local_addr = b;
1451 remote_addr = a;
1452 memcpy(io_cap, &smp->prsp[1], 3);
1453 }
1454
Johan Hedbergdddd3052014-06-01 15:38:09 +03001455 memset(r, 0, sizeof(r));
1456
1457 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001458 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001459
Johan Hedberga29b0732014-10-28 15:17:05 +01001460 if (smp->method == REQ_OOB)
1461 memcpy(r, smp->rr, 16);
1462
Johan Hedberg760b0182014-06-06 11:44:05 +03001463 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1464 local_addr, remote_addr, check.e);
1465
1466 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001467}
1468
Johan Hedberg38606f12014-06-25 11:10:28 +03001469static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1470{
1471 struct l2cap_conn *conn = smp->conn;
1472 struct hci_conn *hcon = conn->hcon;
1473 struct smp_cmd_pairing_confirm cfm;
1474 u8 r;
1475
1476 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1477 r |= 0x80;
1478
1479 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1480
1481 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1482 cfm.confirm_val))
1483 return SMP_UNSPECIFIED;
1484
1485 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1486
1487 return 0;
1488}
1489
1490static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1491{
1492 struct l2cap_conn *conn = smp->conn;
1493 struct hci_conn *hcon = conn->hcon;
1494 struct hci_dev *hdev = hcon->hdev;
1495 u8 cfm[16], r;
1496
1497 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1498 if (smp->passkey_round >= 20)
1499 return 0;
1500
1501 switch (smp_op) {
1502 case SMP_CMD_PAIRING_RANDOM:
1503 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1504 r |= 0x80;
1505
1506 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1507 smp->rrnd, r, cfm))
1508 return SMP_UNSPECIFIED;
1509
1510 if (memcmp(smp->pcnf, cfm, 16))
1511 return SMP_CONFIRM_FAILED;
1512
1513 smp->passkey_round++;
1514
1515 if (smp->passkey_round == 20) {
1516 /* Generate MacKey and LTK */
1517 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1518 return SMP_UNSPECIFIED;
1519 }
1520
1521 /* The round is only complete when the initiator
1522 * receives pairing random.
1523 */
1524 if (!hcon->out) {
1525 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1526 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001527 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001528 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001529 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001530 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001531 return 0;
1532 }
1533
1534 /* Start the next round */
1535 if (smp->passkey_round != 20)
1536 return sc_passkey_round(smp, 0);
1537
1538 /* Passkey rounds are complete - start DHKey Check */
1539 sc_dhkey_check(smp);
1540 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1541
1542 break;
1543
1544 case SMP_CMD_PAIRING_CONFIRM:
1545 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1546 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1547 return 0;
1548 }
1549
1550 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1551
1552 if (hcon->out) {
1553 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1554 sizeof(smp->prnd), smp->prnd);
1555 return 0;
1556 }
1557
1558 return sc_passkey_send_confirm(smp);
1559
1560 case SMP_CMD_PUBLIC_KEY:
1561 default:
1562 /* Initiating device starts the round */
1563 if (!hcon->out)
1564 return 0;
1565
1566 BT_DBG("%s Starting passkey round %u", hdev->name,
1567 smp->passkey_round + 1);
1568
1569 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1570
1571 return sc_passkey_send_confirm(smp);
1572 }
1573
1574 return 0;
1575}
1576
Johan Hedbergdddd3052014-06-01 15:38:09 +03001577static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1578{
Johan Hedberg38606f12014-06-25 11:10:28 +03001579 struct l2cap_conn *conn = smp->conn;
1580 struct hci_conn *hcon = conn->hcon;
1581 u8 smp_op;
1582
1583 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1584
Johan Hedbergdddd3052014-06-01 15:38:09 +03001585 switch (mgmt_op) {
1586 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1587 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1588 return 0;
1589 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1590 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1591 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001592 case MGMT_OP_USER_PASSKEY_REPLY:
1593 hcon->passkey_notify = le32_to_cpu(passkey);
1594 smp->passkey_round = 0;
1595
1596 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1597 smp_op = SMP_CMD_PAIRING_CONFIRM;
1598 else
1599 smp_op = 0;
1600
1601 if (sc_passkey_round(smp, smp_op))
1602 return -EIO;
1603
1604 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001605 }
1606
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001607 /* Initiator sends DHKey check first */
1608 if (hcon->out) {
1609 sc_dhkey_check(smp);
1610 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1611 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1612 sc_dhkey_check(smp);
1613 sc_add_ltk(smp);
1614 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001615
1616 return 0;
1617}
1618
Brian Gix2b64d152011-12-21 16:12:12 -08001619int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1620{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001621 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001622 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001623 struct smp_chan *smp;
1624 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001625 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001626
1627 BT_DBG("");
1628
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001629 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001630 return -ENOTCONN;
1631
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001632 chan = conn->smp;
1633 if (!chan)
1634 return -ENOTCONN;
1635
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001636 l2cap_chan_lock(chan);
1637 if (!chan->data) {
1638 err = -ENOTCONN;
1639 goto unlock;
1640 }
1641
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001642 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001643
Johan Hedberg760b0182014-06-06 11:44:05 +03001644 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1645 err = sc_user_reply(smp, mgmt_op, passkey);
1646 goto unlock;
1647 }
1648
Brian Gix2b64d152011-12-21 16:12:12 -08001649 switch (mgmt_op) {
1650 case MGMT_OP_USER_PASSKEY_REPLY:
1651 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001652 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001653 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001654 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001655 /* Fall Through */
1656 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001657 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001658 break;
1659 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1660 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001661 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001662 err = 0;
1663 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001664 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001665 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001666 err = -EOPNOTSUPP;
1667 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001668 }
1669
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001670 err = 0;
1671
Brian Gix2b64d152011-12-21 16:12:12 -08001672 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001673 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1674 u8 rsp = smp_confirm(smp);
1675 if (rsp)
1676 smp_failure(conn, rsp);
1677 }
Brian Gix2b64d152011-12-21 16:12:12 -08001678
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001679unlock:
1680 l2cap_chan_unlock(chan);
1681 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001682}
1683
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001684static void build_bredr_pairing_cmd(struct smp_chan *smp,
1685 struct smp_cmd_pairing *req,
1686 struct smp_cmd_pairing *rsp)
1687{
1688 struct l2cap_conn *conn = smp->conn;
1689 struct hci_dev *hdev = conn->hcon->hdev;
1690 u8 local_dist = 0, remote_dist = 0;
1691
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001692 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001693 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1694 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1695 }
1696
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001697 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001698 remote_dist |= SMP_DIST_ID_KEY;
1699
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001700 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001701 local_dist |= SMP_DIST_ID_KEY;
1702
1703 if (!rsp) {
1704 memset(req, 0, sizeof(*req));
1705
1706 req->init_key_dist = local_dist;
1707 req->resp_key_dist = remote_dist;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001708 req->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001709
1710 smp->remote_key_dist = remote_dist;
1711
1712 return;
1713 }
1714
1715 memset(rsp, 0, sizeof(*rsp));
1716
Johan Hedberge3f6a252015-06-11 13:52:30 +03001717 rsp->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001718 rsp->init_key_dist = req->init_key_dist & remote_dist;
1719 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1720
1721 smp->remote_key_dist = rsp->init_key_dist;
1722}
1723
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001724static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001725{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001726 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001727 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001728 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001729 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001730 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001731 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001732
1733 BT_DBG("conn %p", conn);
1734
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001735 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001736 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001737
Johan Hedberg40bef302014-07-16 11:42:27 +03001738 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001739 return SMP_CMD_NOTSUPP;
1740
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001741 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001742 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001743 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001744 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001745
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001746 if (!smp)
1747 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001748
Johan Hedbergc05b9332014-09-10 17:37:42 -07001749 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001750 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001751
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001752 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001753 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001754 return SMP_PAIRING_NOTSUPP;
1755
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001756 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001757 return SMP_AUTH_REQUIREMENTS;
1758
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001759 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1760 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001761 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001762
Johan Hedbergcb06d362015-03-16 21:12:34 +02001763 /* If the remote side's OOB flag is set it means it has
1764 * successfully received our local OOB data - therefore set the
1765 * flag to indicate that local OOB is in use.
1766 */
Johan Hedberg58428562015-03-16 11:45:45 +02001767 if (req->oob_flag == SMP_OOB_PRESENT)
1768 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1769
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001770 /* SMP over BR/EDR requires special treatment */
1771 if (conn->hcon->type == ACL_LINK) {
1772 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001773 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001774 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001775 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1776
1777 set_bit(SMP_FLAG_SC, &smp->flags);
1778
1779 build_bredr_pairing_cmd(smp, req, &rsp);
1780
1781 key_size = min(req->max_key_size, rsp.max_key_size);
1782 if (check_enc_key_size(conn, key_size))
1783 return SMP_ENC_KEY_SIZE;
1784
1785 /* Clear bits which are generated but not distributed */
1786 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1787
1788 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1789 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1790 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1791
1792 smp_distribute_keys(smp);
1793 return 0;
1794 }
1795
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001796 build_pairing_cmd(conn, req, &rsp, auth);
1797
1798 if (rsp.auth_req & SMP_AUTH_SC)
1799 set_bit(SMP_FLAG_SC, &smp->flags);
1800
Johan Hedberg5be5e272014-09-10 17:58:54 -07001801 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001802 sec_level = BT_SECURITY_MEDIUM;
1803 else
1804 sec_level = authreq_to_seclevel(auth);
1805
Johan Hedbergc7262e72014-06-17 13:07:37 +03001806 if (sec_level > conn->hcon->pending_sec_level)
1807 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001808
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001809 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001810 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1811 u8 method;
1812
1813 method = get_auth_method(smp, conn->hcon->io_capability,
1814 req->io_capability);
1815 if (method == JUST_WORKS || method == JUST_CFM)
1816 return SMP_AUTH_REQUIREMENTS;
1817 }
1818
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001819 key_size = min(req->max_key_size, rsp.max_key_size);
1820 if (check_enc_key_size(conn, key_size))
1821 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001822
Johan Hedberge84a6b12013-12-02 10:49:03 +02001823 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001824
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001825 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1826 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001827
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001828 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001829
1830 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1831
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001832 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1833 * SC case, however some implementations incorrectly copy RFU auth
1834 * req bits from our security request, which may create a false
1835 * positive SC enablement.
1836 */
1837 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1838
Johan Hedberg3b191462014-06-06 10:50:15 +03001839 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1840 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1841 /* Clear bits which are generated but not distributed */
1842 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1843 /* Wait for Public Key from Initiating Device */
1844 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001845 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001846
Brian Gix2b64d152011-12-21 16:12:12 -08001847 /* Request setup of TK */
1848 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1849 if (ret)
1850 return SMP_UNSPECIFIED;
1851
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001852 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001853}
1854
Johan Hedberg3b191462014-06-06 10:50:15 +03001855static u8 sc_send_public_key(struct smp_chan *smp)
1856{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001857 struct hci_dev *hdev = smp->conn->hcon->hdev;
1858
Johan Hedberg3b191462014-06-06 10:50:15 +03001859 BT_DBG("");
1860
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001861 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001862 struct l2cap_chan *chan = hdev->smp_data;
1863 struct smp_dev *smp_dev;
1864
1865 if (!chan || !chan->data)
1866 return SMP_UNSPECIFIED;
1867
1868 smp_dev = chan->data;
1869
1870 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1871 memcpy(smp->local_sk, smp_dev->local_sk, 32);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001872 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001873
1874 if (smp_dev->debug_key)
1875 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1876
1877 goto done;
1878 }
1879
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001880 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001881 BT_DBG("Using debug keys");
1882 memcpy(smp->local_pk, debug_pk, 64);
1883 memcpy(smp->local_sk, debug_sk, 32);
1884 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1885 } else {
1886 while (true) {
1887 /* Generate local key pair for Secure Connections */
1888 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1889 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001890
Johan Hedberg70157ef2014-06-24 15:22:59 +03001891 /* This is unlikely, but we need to check that
1892 * we didn't accidentially generate a debug key.
1893 */
1894 if (memcmp(smp->local_sk, debug_sk, 32))
1895 break;
1896 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001897 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001898
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001899done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001900 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001901 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001902 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001903
1904 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1905
1906 return 0;
1907}
1908
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001909static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001910{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001911 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001912 struct l2cap_chan *chan = conn->smp;
1913 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001914 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001915 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001916 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001917
1918 BT_DBG("conn %p", conn);
1919
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001920 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001921 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001922
Johan Hedberg40bef302014-07-16 11:42:27 +03001923 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001924 return SMP_CMD_NOTSUPP;
1925
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001926 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001927
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001928 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001929
1930 key_size = min(req->max_key_size, rsp->max_key_size);
1931 if (check_enc_key_size(conn, key_size))
1932 return SMP_ENC_KEY_SIZE;
1933
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001934 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001935
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001936 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001937 return SMP_AUTH_REQUIREMENTS;
1938
Johan Hedbergcb06d362015-03-16 21:12:34 +02001939 /* If the remote side's OOB flag is set it means it has
1940 * successfully received our local OOB data - therefore set the
1941 * flag to indicate that local OOB is in use.
1942 */
Johan Hedberg58428562015-03-16 11:45:45 +02001943 if (rsp->oob_flag == SMP_OOB_PRESENT)
1944 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1945
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001946 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1947 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1948
1949 /* Update remote key distribution in case the remote cleared
1950 * some bits that we had enabled in our request.
1951 */
1952 smp->remote_key_dist &= rsp->resp_key_dist;
1953
1954 /* For BR/EDR this means we're done and can start phase 3 */
1955 if (conn->hcon->type == ACL_LINK) {
1956 /* Clear bits which are generated but not distributed */
1957 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1958 smp_distribute_keys(smp);
1959 return 0;
1960 }
1961
Johan Hedberg65668772014-05-16 11:03:34 +03001962 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1963 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001964 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1965 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001966
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001967 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001968 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1969 u8 method;
1970
1971 method = get_auth_method(smp, req->io_capability,
1972 rsp->io_capability);
1973 if (method == JUST_WORKS || method == JUST_CFM)
1974 return SMP_AUTH_REQUIREMENTS;
1975 }
1976
Johan Hedberge84a6b12013-12-02 10:49:03 +02001977 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001978
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001979 /* Update remote key distribution in case the remote cleared
1980 * some bits that we had enabled in our request.
1981 */
1982 smp->remote_key_dist &= rsp->resp_key_dist;
1983
Johan Hedberg3b191462014-06-06 10:50:15 +03001984 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1985 /* Clear bits which are generated but not distributed */
1986 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1987 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1988 return sc_send_public_key(smp);
1989 }
1990
Johan Hedbergc05b9332014-09-10 17:37:42 -07001991 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001992
Johan Hedberg476585e2012-06-06 18:54:15 +08001993 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001994 if (ret)
1995 return SMP_UNSPECIFIED;
1996
Johan Hedberg4a74d652014-05-20 09:45:50 +03001997 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001998
1999 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03002000 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002001 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002002
2003 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002004}
2005
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002006static u8 sc_check_confirm(struct smp_chan *smp)
2007{
2008 struct l2cap_conn *conn = smp->conn;
2009
2010 BT_DBG("");
2011
Johan Hedberg38606f12014-06-25 11:10:28 +03002012 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2013 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2014
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002015 if (conn->hcon->out) {
2016 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2017 smp->prnd);
2018 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2019 }
2020
2021 return 0;
2022}
2023
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002024/* Work-around for some implementations that incorrectly copy RFU bits
2025 * from our security request and thereby create the impression that
2026 * we're doing SC when in fact the remote doesn't support it.
2027 */
2028static int fixup_sc_false_positive(struct smp_chan *smp)
2029{
2030 struct l2cap_conn *conn = smp->conn;
2031 struct hci_conn *hcon = conn->hcon;
2032 struct hci_dev *hdev = hcon->hdev;
2033 struct smp_cmd_pairing *req, *rsp;
2034 u8 auth;
2035
2036 /* The issue is only observed when we're in slave role */
2037 if (hcon->out)
2038 return SMP_UNSPECIFIED;
2039
2040 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
2041 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2042 return SMP_UNSPECIFIED;
2043 }
2044
2045 BT_ERR("Trying to fall back to legacy SMP");
2046
2047 req = (void *) &smp->preq[1];
2048 rsp = (void *) &smp->prsp[1];
2049
2050 /* Rebuild key dist flags which may have been cleared for SC */
2051 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2052
2053 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2054
2055 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
2056 BT_ERR("Failed to fall back to legacy SMP");
2057 return SMP_UNSPECIFIED;
2058 }
2059
2060 clear_bit(SMP_FLAG_SC, &smp->flags);
2061
2062 return 0;
2063}
2064
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002065static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002066{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002067 struct l2cap_chan *chan = conn->smp;
2068 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002069
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002070 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2071
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002072 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002073 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002074
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002075 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2076 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002077
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002078 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2079 int ret;
2080
2081 /* Public Key exchange must happen before any other steps */
2082 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2083 return sc_check_confirm(smp);
2084
2085 BT_ERR("Unexpected SMP Pairing Confirm");
2086
2087 ret = fixup_sc_false_positive(smp);
2088 if (ret)
2089 return ret;
2090 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002091
Johan Hedbergb28b4942014-09-05 22:19:55 +03002092 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002093 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2094 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002095 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2096 return 0;
2097 }
2098
2099 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002100 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002101
2102 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002103
2104 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002105}
2106
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002107static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002108{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002109 struct l2cap_chan *chan = conn->smp;
2110 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002111 struct hci_conn *hcon = conn->hcon;
2112 u8 *pkax, *pkbx, *na, *nb;
2113 u32 passkey;
2114 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002115
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002116 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002117
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002118 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002119 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002120
Johan Hedberg943a7322014-03-18 12:58:24 +02002121 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002122 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002123
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002124 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2125 return smp_random(smp);
2126
Johan Hedberg580039e2014-12-03 16:26:37 +02002127 if (hcon->out) {
2128 pkax = smp->local_pk;
2129 pkbx = smp->remote_pk;
2130 na = smp->prnd;
2131 nb = smp->rrnd;
2132 } else {
2133 pkax = smp->remote_pk;
2134 pkbx = smp->local_pk;
2135 na = smp->rrnd;
2136 nb = smp->prnd;
2137 }
2138
Johan Hedberga29b0732014-10-28 15:17:05 +01002139 if (smp->method == REQ_OOB) {
2140 if (!hcon->out)
2141 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2142 sizeof(smp->prnd), smp->prnd);
2143 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2144 goto mackey_and_ltk;
2145 }
2146
Johan Hedberg38606f12014-06-25 11:10:28 +03002147 /* Passkey entry has special treatment */
2148 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2149 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2150
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002151 if (hcon->out) {
2152 u8 cfm[16];
2153
2154 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2155 smp->rrnd, 0, cfm);
2156 if (err)
2157 return SMP_UNSPECIFIED;
2158
2159 if (memcmp(smp->pcnf, cfm, 16))
2160 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002161 } else {
2162 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2163 smp->prnd);
2164 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002165 }
2166
Johan Hedberga29b0732014-10-28 15:17:05 +01002167mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002168 /* Generate MacKey and LTK */
2169 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2170 if (err)
2171 return SMP_UNSPECIFIED;
2172
Johan Hedberga29b0732014-10-28 15:17:05 +01002173 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002174 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002175 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002176 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2177 }
2178 return 0;
2179 }
2180
Johan Hedberg38606f12014-06-25 11:10:28 +03002181 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002182 if (err)
2183 return SMP_UNSPECIFIED;
2184
Johan Hedberg38606f12014-06-25 11:10:28 +03002185 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2186 hcon->dst_type, passkey, 0);
2187 if (err)
2188 return SMP_UNSPECIFIED;
2189
2190 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2191
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002192 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002193}
2194
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002195static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002196{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002197 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002198 struct hci_conn *hcon = conn->hcon;
2199
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002200 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002201 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002202 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002203
Johan Hedberga6f78332014-09-10 17:37:45 -07002204 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002205 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002206
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002207 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002208 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002209
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002210 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002211 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002212
Johan Hedbergfe59a052014-07-01 19:14:12 +03002213 /* We never store STKs for master role, so clear this flag */
2214 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2215
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002216 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002217}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002218
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002219bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2220 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002221{
2222 if (sec_level == BT_SECURITY_LOW)
2223 return true;
2224
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002225 /* If we're encrypted with an STK but the caller prefers using
2226 * LTK claim insufficient security. This way we allow the
2227 * connection to be re-encrypted with an LTK, even if the LTK
2228 * provides the same level of security. Only exception is if we
2229 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002230 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002231 if (key_pref == SMP_USE_LTK &&
2232 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002233 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002234 return false;
2235
Johan Hedberg854f4722014-07-01 18:40:20 +03002236 if (hcon->sec_level >= sec_level)
2237 return true;
2238
2239 return false;
2240}
2241
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002242static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002243{
2244 struct smp_cmd_security_req *rp = (void *) skb->data;
2245 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002246 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002247 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002248 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002249 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002250
2251 BT_DBG("conn %p", conn);
2252
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002253 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002254 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002255
Johan Hedberg40bef302014-07-16 11:42:27 +03002256 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002257 return SMP_CMD_NOTSUPP;
2258
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002259 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002260
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002261 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002262 return SMP_AUTH_REQUIREMENTS;
2263
Johan Hedberg5be5e272014-09-10 17:58:54 -07002264 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002265 sec_level = BT_SECURITY_MEDIUM;
2266 else
2267 sec_level = authreq_to_seclevel(auth);
2268
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002269 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002270 return 0;
2271
Johan Hedbergc7262e72014-06-17 13:07:37 +03002272 if (sec_level > hcon->pending_sec_level)
2273 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002274
Johan Hedberg4dab7862012-06-07 14:58:37 +08002275 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002276 return 0;
2277
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002278 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002279 if (!smp)
2280 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002281
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002282 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002283 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002284 return SMP_PAIRING_NOTSUPP;
2285
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002286 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002287
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002288 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002289 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002290
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002291 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2292 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002293
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002294 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002295 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002296
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002297 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002298}
2299
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002300int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002301{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002302 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002303 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002304 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002305 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002306 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002307
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002308 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2309
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002310 /* This may be NULL if there's an unexpected disconnection */
2311 if (!conn)
2312 return 1;
2313
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002314 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002315 return 1;
2316
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002317 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002318 return 1;
2319
Johan Hedbergc7262e72014-06-17 13:07:37 +03002320 if (sec_level > hcon->pending_sec_level)
2321 hcon->pending_sec_level = sec_level;
2322
Johan Hedberg40bef302014-07-16 11:42:27 +03002323 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002324 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2325 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002326
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002327 chan = conn->smp;
2328 if (!chan) {
2329 BT_ERR("SMP security requested but not available");
2330 return 1;
2331 }
2332
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002333 l2cap_chan_lock(chan);
2334
2335 /* If SMP is already in progress ignore this request */
2336 if (chan->data) {
2337 ret = 0;
2338 goto unlock;
2339 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002340
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002341 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002342 if (!smp) {
2343 ret = 1;
2344 goto unlock;
2345 }
Brian Gix2b64d152011-12-21 16:12:12 -08002346
2347 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002348
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002349 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002350 authreq |= SMP_AUTH_SC;
2351
Johan Hedberg79897d22014-06-01 09:45:24 +03002352 /* Require MITM if IO Capability allows or the security level
2353 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002354 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002355 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002356 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002357 authreq |= SMP_AUTH_MITM;
2358
Johan Hedberg40bef302014-07-16 11:42:27 +03002359 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002360 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002361
Brian Gix2b64d152011-12-21 16:12:12 -08002362 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002363 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2364 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002365
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002366 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002367 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002368 } else {
2369 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002370 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002371 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002372 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002373 }
2374
Johan Hedberg4a74d652014-05-20 09:45:50 +03002375 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002376 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002377
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002378unlock:
2379 l2cap_chan_unlock(chan);
2380 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002381}
2382
Johan Hedbergc81d5552015-10-22 09:38:35 +03002383void smp_cancel_pairing(struct hci_conn *hcon)
2384{
2385 struct l2cap_conn *conn = hcon->l2cap_data;
2386 struct l2cap_chan *chan;
2387 struct smp_chan *smp;
2388
2389 if (!conn)
2390 return;
2391
2392 chan = conn->smp;
2393 if (!chan)
2394 return;
2395
2396 l2cap_chan_lock(chan);
2397
2398 smp = chan->data;
2399 if (smp) {
2400 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags))
2401 smp_failure(conn, 0);
2402 else
2403 smp_failure(conn, SMP_UNSPECIFIED);
2404 }
2405
2406 l2cap_chan_unlock(chan);
2407}
2408
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002409static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2410{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002411 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002412 struct l2cap_chan *chan = conn->smp;
2413 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002414
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002415 BT_DBG("conn %p", conn);
2416
2417 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002418 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002419
Johan Hedbergb28b4942014-09-05 22:19:55 +03002420 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002421
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002422 skb_pull(skb, sizeof(*rp));
2423
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002424 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002425
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002426 return 0;
2427}
2428
2429static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2430{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002431 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002432 struct l2cap_chan *chan = conn->smp;
2433 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002434 struct hci_dev *hdev = conn->hcon->hdev;
2435 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002436 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002437 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002438
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002439 BT_DBG("conn %p", conn);
2440
2441 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002442 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002443
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002444 /* Mark the information as received */
2445 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2446
Johan Hedbergb28b4942014-09-05 22:19:55 +03002447 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2448 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002449 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2450 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002451
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002452 skb_pull(skb, sizeof(*rp));
2453
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002454 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002455 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002456 authenticated, smp->tk, smp->enc_key_size,
2457 rp->ediv, rp->rand);
2458 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002459 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002460 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002461
2462 return 0;
2463}
2464
Johan Hedbergfd349c02014-02-18 10:19:36 +02002465static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2466{
2467 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002468 struct l2cap_chan *chan = conn->smp;
2469 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002470
2471 BT_DBG("");
2472
2473 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002474 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002475
Johan Hedbergb28b4942014-09-05 22:19:55 +03002476 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002477
Johan Hedbergfd349c02014-02-18 10:19:36 +02002478 skb_pull(skb, sizeof(*info));
2479
2480 memcpy(smp->irk, info->irk, 16);
2481
2482 return 0;
2483}
2484
2485static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2486 struct sk_buff *skb)
2487{
2488 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002489 struct l2cap_chan *chan = conn->smp;
2490 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002491 struct hci_conn *hcon = conn->hcon;
2492 bdaddr_t rpa;
2493
2494 BT_DBG("");
2495
2496 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002497 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002498
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002499 /* Mark the information as received */
2500 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2501
Johan Hedbergb28b4942014-09-05 22:19:55 +03002502 if (smp->remote_key_dist & SMP_DIST_SIGN)
2503 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2504
Johan Hedbergfd349c02014-02-18 10:19:36 +02002505 skb_pull(skb, sizeof(*info));
2506
Johan Hedberga9a58f82014-02-25 22:24:37 +02002507 /* Strictly speaking the Core Specification (4.1) allows sending
2508 * an empty address which would force us to rely on just the IRK
2509 * as "identity information". However, since such
2510 * implementations are not known of and in order to not over
2511 * complicate our implementation, simply pretend that we never
2512 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002513 *
2514 * The Identity Address must also be a Static Random or Public
2515 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002516 */
Johan Hedberge12af482015-01-14 20:51:37 +02002517 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2518 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002519 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002520 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002521 }
2522
Johan Hedbergfd349c02014-02-18 10:19:36 +02002523 bacpy(&smp->id_addr, &info->bdaddr);
2524 smp->id_addr_type = info->addr_type;
2525
2526 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2527 bacpy(&rpa, &hcon->dst);
2528 else
2529 bacpy(&rpa, BDADDR_ANY);
2530
Johan Hedberg23d0e122014-02-19 14:57:46 +02002531 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2532 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002533
Johan Hedberg31dd6242014-06-27 14:23:02 +03002534distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002535 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2536 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002537
2538 return 0;
2539}
2540
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002541static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2542{
2543 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002544 struct l2cap_chan *chan = conn->smp;
2545 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002546 struct smp_csrk *csrk;
2547
2548 BT_DBG("conn %p", conn);
2549
2550 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002551 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002552
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002553 /* Mark the information as received */
2554 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2555
2556 skb_pull(skb, sizeof(*rp));
2557
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002558 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2559 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002560 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2561 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2562 else
2563 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002564 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2565 }
2566 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002567 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002568
2569 return 0;
2570}
2571
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002572static u8 sc_select_method(struct smp_chan *smp)
2573{
2574 struct l2cap_conn *conn = smp->conn;
2575 struct hci_conn *hcon = conn->hcon;
2576 struct smp_cmd_pairing *local, *remote;
2577 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2578
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002579 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2580 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002581 return REQ_OOB;
2582
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002583 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2584 * which are needed as inputs to some crypto functions. To get
2585 * the "struct smp_cmd_pairing" from them we need to skip the
2586 * first byte which contains the opcode.
2587 */
2588 if (hcon->out) {
2589 local = (void *) &smp->preq[1];
2590 remote = (void *) &smp->prsp[1];
2591 } else {
2592 local = (void *) &smp->prsp[1];
2593 remote = (void *) &smp->preq[1];
2594 }
2595
2596 local_io = local->io_capability;
2597 remote_io = remote->io_capability;
2598
2599 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2600 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2601
2602 /* If either side wants MITM, look up the method from the table,
2603 * otherwise use JUST WORKS.
2604 */
2605 if (local_mitm || remote_mitm)
2606 method = get_auth_method(smp, local_io, remote_io);
2607 else
2608 method = JUST_WORKS;
2609
2610 /* Don't confirm locally initiated pairing attempts */
2611 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2612 method = JUST_WORKS;
2613
2614 return method;
2615}
2616
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002617static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2618{
2619 struct smp_cmd_public_key *key = (void *) skb->data;
2620 struct hci_conn *hcon = conn->hcon;
2621 struct l2cap_chan *chan = conn->smp;
2622 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002623 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002624 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002625 int err;
2626
2627 BT_DBG("conn %p", conn);
2628
2629 if (skb->len < sizeof(*key))
2630 return SMP_INVALID_PARAMS;
2631
2632 memcpy(smp->remote_pk, key, 64);
2633
Johan Hedberga8ca6172015-03-16 18:12:57 +02002634 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2635 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2636 smp->rr, 0, cfm.confirm_val);
2637 if (err)
2638 return SMP_UNSPECIFIED;
2639
2640 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2641 return SMP_CONFIRM_FAILED;
2642 }
2643
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002644 /* Non-initiating device sends its public key after receiving
2645 * the key from the initiating device.
2646 */
2647 if (!hcon->out) {
2648 err = sc_send_public_key(smp);
2649 if (err)
2650 return err;
2651 }
2652
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002653 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002654 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002655
2656 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2657 return SMP_UNSPECIFIED;
2658
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002659 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002660
2661 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2662
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002663 smp->method = sc_select_method(smp);
2664
2665 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2666
2667 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2668 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2669 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2670 else
2671 hcon->pending_sec_level = BT_SECURITY_FIPS;
2672
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002673 if (!memcmp(debug_pk, smp->remote_pk, 64))
2674 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2675
Johan Hedberg38606f12014-06-25 11:10:28 +03002676 if (smp->method == DSP_PASSKEY) {
2677 get_random_bytes(&hcon->passkey_notify,
2678 sizeof(hcon->passkey_notify));
2679 hcon->passkey_notify %= 1000000;
2680 hcon->passkey_entered = 0;
2681 smp->passkey_round = 0;
2682 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2683 hcon->dst_type,
2684 hcon->passkey_notify,
2685 hcon->passkey_entered))
2686 return SMP_UNSPECIFIED;
2687 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2688 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2689 }
2690
Johan Hedberg94ea7252015-03-16 11:45:46 +02002691 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002692 if (hcon->out)
2693 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2694 sizeof(smp->prnd), smp->prnd);
2695
2696 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2697
2698 return 0;
2699 }
2700
Johan Hedberg38606f12014-06-25 11:10:28 +03002701 if (hcon->out)
2702 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2703
2704 if (smp->method == REQ_PASSKEY) {
2705 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2706 hcon->dst_type))
2707 return SMP_UNSPECIFIED;
2708 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2709 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2710 return 0;
2711 }
2712
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002713 /* The Initiating device waits for the non-initiating device to
2714 * send the confirm value.
2715 */
2716 if (conn->hcon->out)
2717 return 0;
2718
2719 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2720 0, cfm.confirm_val);
2721 if (err)
2722 return SMP_UNSPECIFIED;
2723
2724 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2725 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2726
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002727 return 0;
2728}
2729
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002730static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2731{
2732 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2733 struct l2cap_chan *chan = conn->smp;
2734 struct hci_conn *hcon = conn->hcon;
2735 struct smp_chan *smp = chan->data;
2736 u8 a[7], b[7], *local_addr, *remote_addr;
2737 u8 io_cap[3], r[16], e[16];
2738 int err;
2739
2740 BT_DBG("conn %p", conn);
2741
2742 if (skb->len < sizeof(*check))
2743 return SMP_INVALID_PARAMS;
2744
2745 memcpy(a, &hcon->init_addr, 6);
2746 memcpy(b, &hcon->resp_addr, 6);
2747 a[6] = hcon->init_addr_type;
2748 b[6] = hcon->resp_addr_type;
2749
2750 if (hcon->out) {
2751 local_addr = a;
2752 remote_addr = b;
2753 memcpy(io_cap, &smp->prsp[1], 3);
2754 } else {
2755 local_addr = b;
2756 remote_addr = a;
2757 memcpy(io_cap, &smp->preq[1], 3);
2758 }
2759
2760 memset(r, 0, sizeof(r));
2761
Johan Hedberg38606f12014-06-25 11:10:28 +03002762 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2763 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002764 else if (smp->method == REQ_OOB)
2765 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002766
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002767 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2768 io_cap, remote_addr, local_addr, e);
2769 if (err)
2770 return SMP_UNSPECIFIED;
2771
2772 if (memcmp(check->e, e, 16))
2773 return SMP_DHKEY_CHECK_FAILED;
2774
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002775 if (!hcon->out) {
2776 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2777 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2778 return 0;
2779 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002780
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002781 /* Slave sends DHKey check as response to master */
2782 sc_dhkey_check(smp);
2783 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002784
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002785 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002786
2787 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002788 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002789 hcon->enc_key_size = smp->enc_key_size;
2790 }
2791
2792 return 0;
2793}
2794
Johan Hedberg1408bb62014-06-04 22:45:57 +03002795static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2796 struct sk_buff *skb)
2797{
2798 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2799
2800 BT_DBG("value 0x%02x", kp->value);
2801
2802 return 0;
2803}
2804
Johan Hedberg4befb862014-08-11 22:06:38 +03002805static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002806{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002807 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002808 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002809 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002810 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002811 int err = 0;
2812
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002813 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002814 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002815
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002816 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002817 reason = SMP_PAIRING_NOTSUPP;
2818 goto done;
2819 }
2820
Marcel Holtmann92381f52013-10-03 01:23:08 -07002821 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002822 skb_pull(skb, sizeof(code));
2823
Johan Hedbergb28b4942014-09-05 22:19:55 +03002824 smp = chan->data;
2825
2826 if (code > SMP_CMD_MAX)
2827 goto drop;
2828
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002829 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002830 goto drop;
2831
2832 /* If we don't have a context the only allowed commands are
2833 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002834 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002835 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2836 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002837
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002838 switch (code) {
2839 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002840 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002841 break;
2842
2843 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002844 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002845 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002846 break;
2847
2848 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002849 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002850 break;
2851
2852 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002853 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002854 break;
2855
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002856 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002857 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002858 break;
2859
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002860 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002861 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002862 break;
2863
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002864 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002865 reason = smp_cmd_encrypt_info(conn, skb);
2866 break;
2867
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002868 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002869 reason = smp_cmd_master_ident(conn, skb);
2870 break;
2871
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002872 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002873 reason = smp_cmd_ident_info(conn, skb);
2874 break;
2875
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002876 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002877 reason = smp_cmd_ident_addr_info(conn, skb);
2878 break;
2879
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002880 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002881 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002882 break;
2883
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002884 case SMP_CMD_PUBLIC_KEY:
2885 reason = smp_cmd_public_key(conn, skb);
2886 break;
2887
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002888 case SMP_CMD_DHKEY_CHECK:
2889 reason = smp_cmd_dhkey_check(conn, skb);
2890 break;
2891
Johan Hedberg1408bb62014-06-04 22:45:57 +03002892 case SMP_CMD_KEYPRESS_NOTIFY:
2893 reason = smp_cmd_keypress_notify(conn, skb);
2894 break;
2895
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002896 default:
2897 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002898 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002899 goto done;
2900 }
2901
2902done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002903 if (!err) {
2904 if (reason)
2905 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002906 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002907 }
2908
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002909 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002910
2911drop:
2912 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2913 code, &hcon->dst);
2914 kfree_skb(skb);
2915 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002916}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002917
Johan Hedberg70db83c2014-08-08 09:37:16 +03002918static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2919{
2920 struct l2cap_conn *conn = chan->conn;
2921
2922 BT_DBG("chan %p", chan);
2923
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002924 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002925 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002926
Johan Hedberg70db83c2014-08-08 09:37:16 +03002927 conn->smp = NULL;
2928 l2cap_chan_put(chan);
2929}
2930
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002931static void bredr_pairing(struct l2cap_chan *chan)
2932{
2933 struct l2cap_conn *conn = chan->conn;
2934 struct hci_conn *hcon = conn->hcon;
2935 struct hci_dev *hdev = hcon->hdev;
2936 struct smp_cmd_pairing req;
2937 struct smp_chan *smp;
2938
2939 BT_DBG("chan %p", chan);
2940
2941 /* Only new pairings are interesting */
2942 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2943 return;
2944
2945 /* Don't bother if we're not encrypted */
2946 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2947 return;
2948
2949 /* Only master may initiate SMP over BR/EDR */
2950 if (hcon->role != HCI_ROLE_MASTER)
2951 return;
2952
2953 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002954 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002955 return;
2956
2957 /* BR/EDR must use Secure Connections for SMP */
2958 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002959 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002960 return;
2961
2962 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002963 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002964 return;
2965
2966 /* Don't bother if remote LE support is not enabled */
2967 if (!lmp_host_le_capable(hcon))
2968 return;
2969
2970 /* Remote must support SMP fixed chan for BR/EDR */
2971 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2972 return;
2973
2974 /* Don't bother if SMP is already ongoing */
2975 if (chan->data)
2976 return;
2977
2978 smp = smp_chan_create(conn);
2979 if (!smp) {
2980 BT_ERR("%s unable to create SMP context for BR/EDR",
2981 hdev->name);
2982 return;
2983 }
2984
2985 set_bit(SMP_FLAG_SC, &smp->flags);
2986
2987 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2988
2989 /* Prepare and send the BR/EDR SMP Pairing Request */
2990 build_bredr_pairing_cmd(smp, &req, NULL);
2991
2992 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2993 memcpy(&smp->preq[1], &req, sizeof(req));
2994
2995 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2996 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2997}
2998
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002999static void smp_resume_cb(struct l2cap_chan *chan)
3000{
Johan Hedbergb68fda62014-08-11 22:06:40 +03003001 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003002 struct l2cap_conn *conn = chan->conn;
3003 struct hci_conn *hcon = conn->hcon;
3004
3005 BT_DBG("chan %p", chan);
3006
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003007 if (hcon->type == ACL_LINK) {
3008 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003009 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003010 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003011
Johan Hedberg86d14072014-08-11 22:06:43 +03003012 if (!smp)
3013 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03003014
Johan Hedberg84bc0db2014-09-05 22:19:49 +03003015 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3016 return;
3017
Johan Hedberg86d14072014-08-11 22:06:43 +03003018 cancel_delayed_work(&smp->security_timer);
3019
Johan Hedbergd6268e82014-09-05 22:19:51 +03003020 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003021}
3022
Johan Hedberg70db83c2014-08-08 09:37:16 +03003023static void smp_ready_cb(struct l2cap_chan *chan)
3024{
3025 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003026 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003027
3028 BT_DBG("chan %p", chan);
3029
3030 conn->smp = chan;
3031 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003032
3033 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3034 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003035}
3036
Johan Hedberg4befb862014-08-11 22:06:38 +03003037static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3038{
3039 int err;
3040
3041 BT_DBG("chan %p", chan);
3042
3043 err = smp_sig_channel(chan, skb);
3044 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003045 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003046
Johan Hedbergb68fda62014-08-11 22:06:40 +03003047 if (smp)
3048 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003049
Johan Hedberg1e91c292014-08-18 20:33:29 +03003050 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003051 }
3052
3053 return err;
3054}
3055
Johan Hedberg70db83c2014-08-08 09:37:16 +03003056static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3057 unsigned long hdr_len,
3058 unsigned long len, int nb)
3059{
3060 struct sk_buff *skb;
3061
3062 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3063 if (!skb)
3064 return ERR_PTR(-ENOMEM);
3065
3066 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003067 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003068
3069 return skb;
3070}
3071
3072static const struct l2cap_ops smp_chan_ops = {
3073 .name = "Security Manager",
3074 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003075 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003076 .alloc_skb = smp_alloc_skb_cb,
3077 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003078 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003079
3080 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003081 .state_change = l2cap_chan_no_state_change,
3082 .close = l2cap_chan_no_close,
3083 .defer = l2cap_chan_no_defer,
3084 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003085 .set_shutdown = l2cap_chan_no_set_shutdown,
3086 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003087};
3088
3089static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3090{
3091 struct l2cap_chan *chan;
3092
3093 BT_DBG("pchan %p", pchan);
3094
3095 chan = l2cap_chan_create();
3096 if (!chan)
3097 return NULL;
3098
3099 chan->chan_type = pchan->chan_type;
3100 chan->ops = &smp_chan_ops;
3101 chan->scid = pchan->scid;
3102 chan->dcid = chan->scid;
3103 chan->imtu = pchan->imtu;
3104 chan->omtu = pchan->omtu;
3105 chan->mode = pchan->mode;
3106
Johan Hedbergabe84902014-11-12 22:22:21 +02003107 /* Other L2CAP channels may request SMP routines in order to
3108 * change the security level. This means that the SMP channel
3109 * lock must be considered in its own category to avoid lockdep
3110 * warnings.
3111 */
3112 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3113
Johan Hedberg70db83c2014-08-08 09:37:16 +03003114 BT_DBG("created chan %p", chan);
3115
3116 return chan;
3117}
3118
3119static const struct l2cap_ops smp_root_chan_ops = {
3120 .name = "Security Manager Root",
3121 .new_connection = smp_new_conn_cb,
3122
3123 /* None of these are implemented for the root channel */
3124 .close = l2cap_chan_no_close,
3125 .alloc_skb = l2cap_chan_no_alloc_skb,
3126 .recv = l2cap_chan_no_recv,
3127 .state_change = l2cap_chan_no_state_change,
3128 .teardown = l2cap_chan_no_teardown,
3129 .ready = l2cap_chan_no_ready,
3130 .defer = l2cap_chan_no_defer,
3131 .suspend = l2cap_chan_no_suspend,
3132 .resume = l2cap_chan_no_resume,
3133 .set_shutdown = l2cap_chan_no_set_shutdown,
3134 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003135};
3136
Johan Hedbergef8efe42014-08-13 15:12:32 +03003137static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003138{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003139 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003140 struct smp_dev *smp;
3141 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003142 struct crypto_hash *tfm_cmac;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003143
Johan Hedbergef8efe42014-08-13 15:12:32 +03003144 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003145 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003146 goto create_chan;
3147 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003148
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003149 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3150 if (!smp)
3151 return ERR_PTR(-ENOMEM);
3152
3153 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003154 if (IS_ERR(tfm_aes)) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003155 BT_ERR("Unable to create ECB crypto context");
3156 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003157 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003158 }
3159
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003160 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3161 if (IS_ERR(tfm_cmac)) {
3162 BT_ERR("Unable to create CMAC crypto context");
3163 crypto_free_blkcipher(tfm_aes);
3164 kzfree(smp);
3165 return ERR_CAST(tfm_cmac);
3166 }
3167
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003168 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003169 smp->tfm_cmac = tfm_cmac;
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003170 smp->min_key_size = SMP_MIN_ENC_KEY_SIZE;
Johan Hedberg2fd36552015-06-11 13:52:26 +03003171 smp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003172
Johan Hedbergef8efe42014-08-13 15:12:32 +03003173create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003174 chan = l2cap_chan_create();
3175 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003176 if (smp) {
3177 crypto_free_blkcipher(smp->tfm_aes);
3178 crypto_free_hash(smp->tfm_cmac);
3179 kzfree(smp);
3180 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003181 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003182 }
3183
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003184 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003185
Johan Hedbergef8efe42014-08-13 15:12:32 +03003186 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003187
3188 l2cap_chan_set_defaults(chan);
3189
Marcel Holtmann157029b2015-01-14 15:43:09 -08003190 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003191 u8 bdaddr_type;
3192
3193 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3194
3195 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003196 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003197 else
3198 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003199 } else {
3200 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003201 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003202 }
3203
Johan Hedberg70db83c2014-08-08 09:37:16 +03003204 chan->state = BT_LISTEN;
3205 chan->mode = L2CAP_MODE_BASIC;
3206 chan->imtu = L2CAP_DEFAULT_MTU;
3207 chan->ops = &smp_root_chan_ops;
3208
Johan Hedbergabe84902014-11-12 22:22:21 +02003209 /* Set correct nesting level for a parent/listening channel */
3210 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3211
Johan Hedbergef8efe42014-08-13 15:12:32 +03003212 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003213}
3214
Johan Hedbergef8efe42014-08-13 15:12:32 +03003215static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003216{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003217 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003218
Johan Hedbergef8efe42014-08-13 15:12:32 +03003219 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003220
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003221 smp = chan->data;
3222 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003223 chan->data = NULL;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003224 if (smp->tfm_aes)
3225 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003226 if (smp->tfm_cmac)
3227 crypto_free_hash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003228 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003229 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003230
Johan Hedberg70db83c2014-08-08 09:37:16 +03003231 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003232}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003233
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003234static ssize_t force_bredr_smp_read(struct file *file,
3235 char __user *user_buf,
3236 size_t count, loff_t *ppos)
3237{
3238 struct hci_dev *hdev = file->private_data;
3239 char buf[3];
3240
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003241 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003242 buf[1] = '\n';
3243 buf[2] = '\0';
3244 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3245}
3246
3247static ssize_t force_bredr_smp_write(struct file *file,
3248 const char __user *user_buf,
3249 size_t count, loff_t *ppos)
3250{
3251 struct hci_dev *hdev = file->private_data;
3252 char buf[32];
3253 size_t buf_size = min(count, (sizeof(buf)-1));
3254 bool enable;
3255
3256 if (copy_from_user(buf, user_buf, buf_size))
3257 return -EFAULT;
3258
3259 buf[buf_size] = '\0';
3260 if (strtobool(buf, &enable))
3261 return -EINVAL;
3262
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003263 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003264 return -EALREADY;
3265
3266 if (enable) {
3267 struct l2cap_chan *chan;
3268
3269 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3270 if (IS_ERR(chan))
3271 return PTR_ERR(chan);
3272
3273 hdev->smp_bredr_data = chan;
3274 } else {
3275 struct l2cap_chan *chan;
3276
3277 chan = hdev->smp_bredr_data;
3278 hdev->smp_bredr_data = NULL;
3279 smp_del_chan(chan);
3280 }
3281
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003282 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003283
3284 return count;
3285}
3286
3287static const struct file_operations force_bredr_smp_fops = {
3288 .open = simple_open,
3289 .read = force_bredr_smp_read,
3290 .write = force_bredr_smp_write,
3291 .llseek = default_llseek,
3292};
3293
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003294static ssize_t le_min_key_size_read(struct file *file,
3295 char __user *user_buf,
3296 size_t count, loff_t *ppos)
3297{
3298 struct hci_dev *hdev = file->private_data;
3299 char buf[4];
3300
3301 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->min_key_size);
3302
3303 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3304}
3305
3306static ssize_t le_min_key_size_write(struct file *file,
3307 const char __user *user_buf,
3308 size_t count, loff_t *ppos)
3309{
3310 struct hci_dev *hdev = file->private_data;
3311 char buf[32];
3312 size_t buf_size = min(count, (sizeof(buf) - 1));
3313 u8 key_size;
3314
3315 if (copy_from_user(buf, user_buf, buf_size))
3316 return -EFAULT;
3317
3318 buf[buf_size] = '\0';
3319
3320 sscanf(buf, "%hhu", &key_size);
3321
3322 if (key_size > SMP_DEV(hdev)->max_key_size ||
3323 key_size < SMP_MIN_ENC_KEY_SIZE)
3324 return -EINVAL;
3325
3326 SMP_DEV(hdev)->min_key_size = key_size;
3327
3328 return count;
3329}
3330
3331static const struct file_operations le_min_key_size_fops = {
3332 .open = simple_open,
3333 .read = le_min_key_size_read,
3334 .write = le_min_key_size_write,
3335 .llseek = default_llseek,
3336};
3337
Johan Hedberg2fd36552015-06-11 13:52:26 +03003338static ssize_t le_max_key_size_read(struct file *file,
3339 char __user *user_buf,
3340 size_t count, loff_t *ppos)
3341{
3342 struct hci_dev *hdev = file->private_data;
3343 char buf[4];
3344
3345 snprintf(buf, sizeof(buf), "%2u\n", SMP_DEV(hdev)->max_key_size);
3346
3347 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3348}
3349
3350static ssize_t le_max_key_size_write(struct file *file,
3351 const char __user *user_buf,
3352 size_t count, loff_t *ppos)
3353{
3354 struct hci_dev *hdev = file->private_data;
3355 char buf[32];
3356 size_t buf_size = min(count, (sizeof(buf) - 1));
3357 u8 key_size;
3358
3359 if (copy_from_user(buf, user_buf, buf_size))
3360 return -EFAULT;
3361
3362 buf[buf_size] = '\0';
3363
3364 sscanf(buf, "%hhu", &key_size);
3365
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003366 if (key_size > SMP_MAX_ENC_KEY_SIZE ||
3367 key_size < SMP_DEV(hdev)->min_key_size)
Johan Hedberg2fd36552015-06-11 13:52:26 +03003368 return -EINVAL;
3369
3370 SMP_DEV(hdev)->max_key_size = key_size;
3371
3372 return count;
3373}
3374
3375static const struct file_operations le_max_key_size_fops = {
3376 .open = simple_open,
3377 .read = le_max_key_size_read,
3378 .write = le_max_key_size_write,
3379 .llseek = default_llseek,
3380};
3381
Johan Hedbergef8efe42014-08-13 15:12:32 +03003382int smp_register(struct hci_dev *hdev)
3383{
3384 struct l2cap_chan *chan;
3385
3386 BT_DBG("%s", hdev->name);
3387
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003388 /* If the controller does not support Low Energy operation, then
3389 * there is also no need to register any SMP channel.
3390 */
3391 if (!lmp_le_capable(hdev))
3392 return 0;
3393
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003394 if (WARN_ON(hdev->smp_data)) {
3395 chan = hdev->smp_data;
3396 hdev->smp_data = NULL;
3397 smp_del_chan(chan);
3398 }
3399
Johan Hedbergef8efe42014-08-13 15:12:32 +03003400 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3401 if (IS_ERR(chan))
3402 return PTR_ERR(chan);
3403
3404 hdev->smp_data = chan;
3405
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003406 debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
3407 &le_min_key_size_fops);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003408 debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3409 &le_max_key_size_fops);
3410
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003411 /* If the controller does not support BR/EDR Secure Connections
3412 * feature, then the BR/EDR SMP channel shall not be present.
3413 *
3414 * To test this with Bluetooth 4.0 controllers, create a debugfs
3415 * switch that allows forcing BR/EDR SMP support and accepting
3416 * cross-transport pairing on non-AES encrypted connections.
3417 */
3418 if (!lmp_sc_capable(hdev)) {
3419 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3420 hdev, &force_bredr_smp_fops);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003421 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003422 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003423
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003424 if (WARN_ON(hdev->smp_bredr_data)) {
3425 chan = hdev->smp_bredr_data;
3426 hdev->smp_bredr_data = NULL;
3427 smp_del_chan(chan);
3428 }
3429
Johan Hedbergef8efe42014-08-13 15:12:32 +03003430 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3431 if (IS_ERR(chan)) {
3432 int err = PTR_ERR(chan);
3433 chan = hdev->smp_data;
3434 hdev->smp_data = NULL;
3435 smp_del_chan(chan);
3436 return err;
3437 }
3438
3439 hdev->smp_bredr_data = chan;
3440
3441 return 0;
3442}
3443
3444void smp_unregister(struct hci_dev *hdev)
3445{
3446 struct l2cap_chan *chan;
3447
3448 if (hdev->smp_bredr_data) {
3449 chan = hdev->smp_bredr_data;
3450 hdev->smp_bredr_data = NULL;
3451 smp_del_chan(chan);
3452 }
3453
3454 if (hdev->smp_data) {
3455 chan = hdev->smp_data;
3456 hdev->smp_data = NULL;
3457 smp_del_chan(chan);
3458 }
3459}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003460
3461#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3462
Johan Hedbergcfc41982014-12-30 09:50:40 +02003463static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3464{
3465 const u8 irk[16] = {
3466 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3467 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3468 const u8 r[3] = { 0x94, 0x81, 0x70 };
3469 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3470 u8 res[3];
3471 int err;
3472
3473 err = smp_ah(tfm_aes, irk, r, res);
3474 if (err)
3475 return err;
3476
3477 if (memcmp(res, exp, 3))
3478 return -EINVAL;
3479
3480 return 0;
3481}
3482
3483static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3484{
3485 const u8 k[16] = {
3486 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3487 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3488 const u8 r[16] = {
3489 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3490 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3491 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3492 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3493 const u8 _iat = 0x01;
3494 const u8 _rat = 0x00;
3495 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3496 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3497 const u8 exp[16] = {
3498 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3499 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3500 u8 res[16];
3501 int err;
3502
3503 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3504 if (err)
3505 return err;
3506
3507 if (memcmp(res, exp, 16))
3508 return -EINVAL;
3509
3510 return 0;
3511}
3512
3513static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3514{
3515 const u8 k[16] = {
3516 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3517 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3518 const u8 r1[16] = {
3519 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3520 const u8 r2[16] = {
3521 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3522 const u8 exp[16] = {
3523 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3524 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3525 u8 res[16];
3526 int err;
3527
3528 err = smp_s1(tfm_aes, k, r1, r2, res);
3529 if (err)
3530 return err;
3531
3532 if (memcmp(res, exp, 16))
3533 return -EINVAL;
3534
3535 return 0;
3536}
3537
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003538static int __init test_f4(struct crypto_hash *tfm_cmac)
3539{
3540 const u8 u[32] = {
3541 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3542 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3543 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3544 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3545 const u8 v[32] = {
3546 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3547 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3548 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3549 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3550 const u8 x[16] = {
3551 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3552 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3553 const u8 z = 0x00;
3554 const u8 exp[16] = {
3555 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3556 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3557 u8 res[16];
3558 int err;
3559
3560 err = smp_f4(tfm_cmac, u, v, x, z, res);
3561 if (err)
3562 return err;
3563
3564 if (memcmp(res, exp, 16))
3565 return -EINVAL;
3566
3567 return 0;
3568}
3569
3570static int __init test_f5(struct crypto_hash *tfm_cmac)
3571{
3572 const u8 w[32] = {
3573 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3574 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3575 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3576 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3577 const u8 n1[16] = {
3578 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3579 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3580 const u8 n2[16] = {
3581 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3582 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3583 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3584 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3585 const u8 exp_ltk[16] = {
3586 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3587 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3588 const u8 exp_mackey[16] = {
3589 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3590 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3591 u8 mackey[16], ltk[16];
3592 int err;
3593
3594 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3595 if (err)
3596 return err;
3597
3598 if (memcmp(mackey, exp_mackey, 16))
3599 return -EINVAL;
3600
3601 if (memcmp(ltk, exp_ltk, 16))
3602 return -EINVAL;
3603
3604 return 0;
3605}
3606
3607static int __init test_f6(struct crypto_hash *tfm_cmac)
3608{
3609 const u8 w[16] = {
3610 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3611 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3612 const u8 n1[16] = {
3613 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3614 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3615 const u8 n2[16] = {
3616 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3617 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3618 const u8 r[16] = {
3619 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3620 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3621 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3622 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3623 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3624 const u8 exp[16] = {
3625 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3626 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3627 u8 res[16];
3628 int err;
3629
3630 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3631 if (err)
3632 return err;
3633
3634 if (memcmp(res, exp, 16))
3635 return -EINVAL;
3636
3637 return 0;
3638}
3639
3640static int __init test_g2(struct crypto_hash *tfm_cmac)
3641{
3642 const u8 u[32] = {
3643 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3644 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3645 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3646 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3647 const u8 v[32] = {
3648 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3649 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3650 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3651 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3652 const u8 x[16] = {
3653 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3654 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3655 const u8 y[16] = {
3656 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3657 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3658 const u32 exp_val = 0x2f9ed5ba % 1000000;
3659 u32 val;
3660 int err;
3661
3662 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3663 if (err)
3664 return err;
3665
3666 if (val != exp_val)
3667 return -EINVAL;
3668
3669 return 0;
3670}
3671
3672static int __init test_h6(struct crypto_hash *tfm_cmac)
3673{
3674 const u8 w[16] = {
3675 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3676 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3677 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3678 const u8 exp[16] = {
3679 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3680 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3681 u8 res[16];
3682 int err;
3683
3684 err = smp_h6(tfm_cmac, w, key_id, res);
3685 if (err)
3686 return err;
3687
3688 if (memcmp(res, exp, 16))
3689 return -EINVAL;
3690
3691 return 0;
3692}
3693
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003694static char test_smp_buffer[32];
3695
3696static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3697 size_t count, loff_t *ppos)
3698{
3699 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3700 strlen(test_smp_buffer));
3701}
3702
3703static const struct file_operations test_smp_fops = {
3704 .open = simple_open,
3705 .read = test_smp_read,
3706 .llseek = default_llseek,
3707};
3708
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003709static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3710 struct crypto_hash *tfm_cmac)
3711{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003712 ktime_t calltime, delta, rettime;
3713 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003714 int err;
3715
Marcel Holtmann255047b2014-12-30 00:11:20 -08003716 calltime = ktime_get();
3717
Johan Hedbergcfc41982014-12-30 09:50:40 +02003718 err = test_ah(tfm_aes);
3719 if (err) {
3720 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003721 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003722 }
3723
3724 err = test_c1(tfm_aes);
3725 if (err) {
3726 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003727 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003728 }
3729
3730 err = test_s1(tfm_aes);
3731 if (err) {
3732 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003733 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003734 }
3735
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003736 err = test_f4(tfm_cmac);
3737 if (err) {
3738 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003739 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003740 }
3741
3742 err = test_f5(tfm_cmac);
3743 if (err) {
3744 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003745 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003746 }
3747
3748 err = test_f6(tfm_cmac);
3749 if (err) {
3750 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003751 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003752 }
3753
3754 err = test_g2(tfm_cmac);
3755 if (err) {
3756 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003757 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003758 }
3759
3760 err = test_h6(tfm_cmac);
3761 if (err) {
3762 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003763 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003764 }
3765
Marcel Holtmann255047b2014-12-30 00:11:20 -08003766 rettime = ktime_get();
3767 delta = ktime_sub(rettime, calltime);
3768 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3769
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003770 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003771
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003772done:
3773 if (!err)
3774 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3775 "PASS (%llu usecs)\n", duration);
3776 else
3777 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3778
3779 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3780 &test_smp_fops);
3781
3782 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003783}
3784
3785int __init bt_selftest_smp(void)
3786{
3787 struct crypto_blkcipher *tfm_aes;
3788 struct crypto_hash *tfm_cmac;
3789 int err;
3790
3791 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3792 if (IS_ERR(tfm_aes)) {
3793 BT_ERR("Unable to create ECB crypto context");
3794 return PTR_ERR(tfm_aes);
3795 }
3796
3797 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3798 if (IS_ERR(tfm_cmac)) {
3799 BT_ERR("Unable to create CMAC crypto context");
3800 crypto_free_blkcipher(tfm_aes);
3801 return PTR_ERR(tfm_cmac);
3802 }
3803
3804 err = run_selftests(tfm_aes, tfm_cmac);
3805
3806 crypto_free_hash(tfm_cmac);
3807 crypto_free_blkcipher(tfm_aes);
3808
3809 return err;
3810}
3811
3812#endif