blob: 952ba6376e1c12c0494d63ec925a22bc45cce482 [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 Hedbergc7a3d572014-12-01 22:03:16 +020036/* Low-level debug macros to be used for stuff that we don't want
37 * accidentially in dmesg, i.e. the values of the various crypto keys
38 * and the inputs & outputs of crypto functions.
39 */
40#ifdef DEBUG
41#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
42 ##__VA_ARGS__)
43#else
44#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
45 ##__VA_ARGS__)
46#endif
47
Johan Hedbergb28b4942014-09-05 22:19:55 +030048#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030049
Johan Hedberg3b191462014-06-06 10:50:15 +030050/* Keys which are not distributed with Secure Connections */
51#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
52
Marcel Holtmann17b02e62012-03-01 14:32:37 -080053#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030054
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070055#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberg0edb14d2014-05-26 13:29:28 +030056 0x1f : 0x07)
57#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020058
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030059/* Maximum message length that can be passed to aes_cmac */
60#define CMAC_MSG_MAX 80
61
Johan Hedberg533e35d2014-06-16 19:25:18 +030062enum {
63 SMP_FLAG_TK_VALID,
64 SMP_FLAG_CFM_PENDING,
65 SMP_FLAG_MITM_AUTH,
66 SMP_FLAG_COMPLETE,
67 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030068 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030069 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030070 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030071 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030072 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg02b05bd2014-10-26 21:19:10 +010073 SMP_FLAG_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030074};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030075
Marcel Holtmann88a479d2015-03-16 01:10:19 -070076struct smp_dev {
77 struct crypto_blkcipher *tfm_aes;
78};
79
Johan Hedberg4bc58f52014-05-20 09:45:47 +030080struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030081 struct l2cap_conn *conn;
82 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030083 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030084
Johan Hedberg4bc58f52014-05-20 09:45:47 +030085 u8 preq[7]; /* SMP Pairing Request */
86 u8 prsp[7]; /* SMP Pairing Response */
87 u8 prnd[16]; /* SMP Pairing Random (local) */
88 u8 rrnd[16]; /* SMP Pairing Random (remote) */
89 u8 pcnf[16]; /* SMP Pairing Confirm */
90 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberga29b0732014-10-28 15:17:05 +010091 u8 rr[16];
Johan Hedberg4bc58f52014-05-20 09:45:47 +030092 u8 enc_key_size;
93 u8 remote_key_dist;
94 bdaddr_t id_addr;
95 u8 id_addr_type;
96 u8 irk[16];
97 struct smp_csrk *csrk;
98 struct smp_csrk *slave_csrk;
99 struct smp_ltk *ltk;
100 struct smp_ltk *slave_ltk;
101 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300102 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300103 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300104 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300105 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300106
Johan Hedberg3b191462014-06-06 10:50:15 +0300107 /* Secure Connections variables */
108 u8 local_pk[64];
109 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300110 u8 remote_pk[64];
111 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300112 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300113
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300114 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300115 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300116};
117
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300118/* These debug key values are defined in the SMP section of the core
119 * specification. debug_pk is the public debug key and debug_sk the
120 * private debug key.
121 */
122static const u8 debug_pk[64] = {
123 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
124 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
125 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
126 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
127
128 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
129 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
130 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
131 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
132};
133
134static const u8 debug_sk[32] = {
135 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
136 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
137 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
138 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
139};
140
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300141static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300142{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300143 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300144
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300145 for (i = 0; i < len; i++)
146 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300147}
148
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200149/* The following functions map to the LE SC SMP crypto functions
150 * AES-CMAC, f4, f5, f6, g2 and h6.
151 */
152
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300153static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
154 size_t len, u8 mac[16])
155{
156 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
157 struct hash_desc desc;
158 struct scatterlist sg;
159 int err;
160
161 if (len > CMAC_MSG_MAX)
162 return -EFBIG;
163
164 if (!tfm) {
165 BT_ERR("tfm %p", tfm);
166 return -EINVAL;
167 }
168
169 desc.tfm = tfm;
170 desc.flags = 0;
171
172 crypto_hash_init(&desc);
173
174 /* Swap key and message from LSB to MSB */
175 swap_buf(k, tmp, 16);
176 swap_buf(m, msg_msb, len);
177
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200178 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
179 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300180
181 err = crypto_hash_setkey(tfm, tmp, 16);
182 if (err) {
183 BT_ERR("cipher setkey failed: %d", err);
184 return err;
185 }
186
187 sg_init_one(&sg, msg_msb, len);
188
189 err = crypto_hash_update(&desc, &sg, len);
190 if (err) {
191 BT_ERR("Hash update error %d", err);
192 return err;
193 }
194
195 err = crypto_hash_final(&desc, mac_msb);
196 if (err) {
197 BT_ERR("Hash final error %d", err);
198 return err;
199 }
200
201 swap_buf(mac_msb, mac, 16);
202
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200203 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300204
205 return 0;
206}
207
208static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
209 const u8 x[16], u8 z, u8 res[16])
210{
211 u8 m[65];
212 int err;
213
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200214 SMP_DBG("u %32phN", u);
215 SMP_DBG("v %32phN", v);
216 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300217
218 m[0] = z;
219 memcpy(m + 1, v, 32);
220 memcpy(m + 33, u, 32);
221
222 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
223 if (err)
224 return err;
225
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200226 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300227
228 return err;
229}
230
Johan Hedberg4da50de2014-12-29 12:04:10 +0200231static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32],
232 const u8 n1[16], const u8 n2[16], const u8 a1[7],
233 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300234{
235 /* The btle, salt and length "magic" values are as defined in
236 * the SMP section of the Bluetooth core specification. In ASCII
237 * the btle value ends up being 'btle'. The salt is just a
238 * random number whereas length is the value 256 in little
239 * endian format.
240 */
241 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
242 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
243 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
244 const u8 length[2] = { 0x00, 0x01 };
245 u8 m[53], t[16];
246 int err;
247
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200248 SMP_DBG("w %32phN", w);
249 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
250 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300251
252 err = aes_cmac(tfm_cmac, salt, w, 32, t);
253 if (err)
254 return err;
255
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200256 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300257
258 memcpy(m, length, 2);
259 memcpy(m + 2, a2, 7);
260 memcpy(m + 9, a1, 7);
261 memcpy(m + 16, n2, 16);
262 memcpy(m + 32, n1, 16);
263 memcpy(m + 48, btle, 4);
264
265 m[52] = 0; /* Counter */
266
267 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
268 if (err)
269 return err;
270
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200271 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300272
273 m[52] = 1; /* Counter */
274
275 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
276 if (err)
277 return err;
278
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200279 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300280
281 return 0;
282}
283
284static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200285 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300286 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
287 u8 res[16])
288{
289 u8 m[65];
290 int err;
291
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200292 SMP_DBG("w %16phN", w);
293 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
294 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300295
296 memcpy(m, a2, 7);
297 memcpy(m + 7, a1, 7);
298 memcpy(m + 14, io_cap, 3);
299 memcpy(m + 17, r, 16);
300 memcpy(m + 33, n2, 16);
301 memcpy(m + 49, n1, 16);
302
303 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
304 if (err)
305 return err;
306
Marcel Holtmann203de212014-12-31 20:01:22 -0800307 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300308
309 return err;
310}
311
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300312static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
313 const u8 x[16], const u8 y[16], u32 *val)
314{
315 u8 m[80], tmp[16];
316 int err;
317
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200318 SMP_DBG("u %32phN", u);
319 SMP_DBG("v %32phN", v);
320 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300321
322 memcpy(m, y, 16);
323 memcpy(m + 16, v, 32);
324 memcpy(m + 48, u, 32);
325
326 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
327 if (err)
328 return err;
329
330 *val = get_unaligned_le32(tmp);
331 *val %= 1000000;
332
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200333 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300334
335 return 0;
336}
337
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200338static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
339 const u8 key_id[4], u8 res[16])
340{
341 int err;
342
343 SMP_DBG("w %16phN key_id %4phN", w, key_id);
344
345 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
346 if (err)
347 return err;
348
349 SMP_DBG("res %16phN", res);
350
351 return err;
352}
353
354/* The following functions map to the legacy SMP crypto functions e, c1,
355 * s1 and ah.
356 */
357
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300358static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
359{
360 struct blkcipher_desc desc;
361 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200362 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200363 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300364
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200365 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300366 BT_ERR("tfm %p", tfm);
367 return -EINVAL;
368 }
369
370 desc.tfm = tfm;
371 desc.flags = 0;
372
Johan Hedberg943a7322014-03-18 12:58:24 +0200373 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300374 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200375
376 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300377 if (err) {
378 BT_ERR("cipher setkey failed: %d", err);
379 return err;
380 }
381
Johan Hedberg943a7322014-03-18 12:58:24 +0200382 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300383 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200384
385 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300386
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300387 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
388 if (err)
389 BT_ERR("Encrypt data error %d", err);
390
Johan Hedberg943a7322014-03-18 12:58:24 +0200391 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300392 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200393
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300394 return err;
395}
396
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200397static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
398 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
399 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
400{
401 u8 p1[16], p2[16];
402 int err;
403
404 memset(p1, 0, 16);
405
406 /* p1 = pres || preq || _rat || _iat */
407 p1[0] = _iat;
408 p1[1] = _rat;
409 memcpy(p1 + 2, preq, 7);
410 memcpy(p1 + 9, pres, 7);
411
412 /* p2 = padding || ia || ra */
413 memcpy(p2, ra, 6);
414 memcpy(p2 + 6, ia, 6);
415 memset(p2 + 12, 0, 4);
416
417 /* res = r XOR p1 */
418 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
419
420 /* res = e(k, res) */
421 err = smp_e(tfm_aes, k, res);
422 if (err) {
423 BT_ERR("Encrypt data error");
424 return err;
425 }
426
427 /* res = res XOR p2 */
428 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
429
430 /* res = e(k, res) */
431 err = smp_e(tfm_aes, k, res);
432 if (err)
433 BT_ERR("Encrypt data error");
434
435 return err;
436}
437
438static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
439 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300440{
441 int err;
442
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200443 /* Just least significant octets from r1 and r2 are considered */
444 memcpy(_r, r2, 8);
445 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300446
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200447 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300448 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200449 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300450
451 return err;
452}
453
Johan Hedbergcd082792014-12-02 13:37:41 +0200454static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
455 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200456{
Johan Hedberg943a7322014-03-18 12:58:24 +0200457 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200458 int err;
459
460 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200461 memcpy(_res, r, 3);
462 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200463
Johan Hedberg943a7322014-03-18 12:58:24 +0200464 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200465 if (err) {
466 BT_ERR("Encrypt error");
467 return err;
468 }
469
470 /* The output of the random address function ah is:
471 * ah(h, r) = e(k, r') mod 2^24
472 * The output of the security function e is then truncated to 24 bits
473 * by taking the least significant 24 bits of the output of e as the
474 * result of ah.
475 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200476 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200477
478 return 0;
479}
480
Johan Hedbergcd082792014-12-02 13:37:41 +0200481bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
482 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200483{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300484 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700485 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200486 u8 hash[3];
487 int err;
488
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300489 if (!chan || !chan->data)
490 return false;
491
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700492 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300493
Johan Hedberg60478052014-02-18 10:19:31 +0200494 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
495
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700496 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200497 if (err)
498 return false;
499
500 return !memcmp(bdaddr->b, hash, 3);
501}
502
Johan Hedbergcd082792014-12-02 13:37:41 +0200503int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200504{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300505 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700506 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200507 int err;
508
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300509 if (!chan || !chan->data)
510 return -EOPNOTSUPP;
511
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700512 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300513
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200514 get_random_bytes(&rpa->b[3], 3);
515
516 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
517 rpa->b[5] |= 0x40; /* Set second most significant bit */
518
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700519 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200520 if (err < 0)
521 return err;
522
523 BT_DBG("RPA %pMR", rpa);
524
525 return 0;
526}
527
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300528static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
529{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300530 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300531 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300532 struct kvec iv[2];
533 struct msghdr msg;
534
535 if (!chan)
536 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300537
538 BT_DBG("code 0x%2.2x", code);
539
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300540 iv[0].iov_base = &code;
541 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300542
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300543 iv[1].iov_base = data;
544 iv[1].iov_len = len;
545
546 memset(&msg, 0, sizeof(msg));
547
Al Viro17836392014-11-24 17:07:38 -0500548 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300549
550 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300551
Johan Hedbergb68fda62014-08-11 22:06:40 +0300552 if (!chan->data)
553 return;
554
555 smp = chan->data;
556
557 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300558 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300559}
560
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300561static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800562{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300563 if (authreq & SMP_AUTH_MITM) {
564 if (authreq & SMP_AUTH_SC)
565 return BT_SECURITY_FIPS;
566 else
567 return BT_SECURITY_HIGH;
568 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800569 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300570 }
Brian Gix2b64d152011-12-21 16:12:12 -0800571}
572
573static __u8 seclevel_to_authreq(__u8 sec_level)
574{
575 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300576 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800577 case BT_SECURITY_HIGH:
578 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
579 case BT_SECURITY_MEDIUM:
580 return SMP_AUTH_BONDING;
581 default:
582 return SMP_AUTH_NONE;
583 }
584}
585
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300586static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700587 struct smp_cmd_pairing *req,
588 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300589{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300590 struct l2cap_chan *chan = conn->smp;
591 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200592 struct hci_conn *hcon = conn->hcon;
593 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100594 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300595
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700596 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700597 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
598 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300599 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800600 } else {
601 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300602 }
603
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700604 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200605 remote_dist |= SMP_DIST_ID_KEY;
606
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700607 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200608 local_dist |= SMP_DIST_ID_KEY;
609
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700610 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100611 (authreq & SMP_AUTH_SC)) {
612 struct oob_data *oob_data;
613 u8 bdaddr_type;
614
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700615 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300616 local_dist |= SMP_DIST_LINK_KEY;
617 remote_dist |= SMP_DIST_LINK_KEY;
618 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100619
620 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
621 bdaddr_type = BDADDR_LE_PUBLIC;
622 else
623 bdaddr_type = BDADDR_LE_RANDOM;
624
625 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
626 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800627 if (oob_data && oob_data->present) {
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100628 set_bit(SMP_FLAG_OOB, &smp->flags);
629 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100630 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100631 memcpy(smp->pcnf, oob_data->hash256, 16);
632 }
633
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300634 } else {
635 authreq &= ~SMP_AUTH_SC;
636 }
637
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300638 if (rsp == NULL) {
639 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100640 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300641 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200642 req->init_key_dist = local_dist;
643 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300644 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200645
646 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300647 return;
648 }
649
650 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100651 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300652 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200653 rsp->init_key_dist = req->init_key_dist & remote_dist;
654 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300655 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200656
657 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300658}
659
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300660static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
661{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300662 struct l2cap_chan *chan = conn->smp;
663 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300664
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300665 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700666 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300667 return SMP_ENC_KEY_SIZE;
668
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300669 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300670
671 return 0;
672}
673
Johan Hedberg6f48e262014-08-11 22:06:44 +0300674static void smp_chan_destroy(struct l2cap_conn *conn)
675{
676 struct l2cap_chan *chan = conn->smp;
677 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200678 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300679 bool complete;
680
681 BUG_ON(!smp);
682
683 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300684
Johan Hedberg6f48e262014-08-11 22:06:44 +0300685 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200686 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300687
Marcel Holtmann276812e2015-03-16 01:10:18 -0700688 kzfree(smp->csrk);
689 kzfree(smp->slave_csrk);
690 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300691
692 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300693 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300694
Johan Hedberg923e2412014-12-03 12:43:39 +0200695 /* Ensure that we don't leave any debug key around if debug key
696 * support hasn't been explicitly enabled.
697 */
698 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700699 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200700 list_del_rcu(&smp->ltk->list);
701 kfree_rcu(smp->ltk, rcu);
702 smp->ltk = NULL;
703 }
704
Johan Hedberg6f48e262014-08-11 22:06:44 +0300705 /* If pairing failed clean up any keys we might have */
706 if (!complete) {
707 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200708 list_del_rcu(&smp->ltk->list);
709 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300710 }
711
712 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200713 list_del_rcu(&smp->slave_ltk->list);
714 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300715 }
716
717 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200718 list_del_rcu(&smp->remote_irk->list);
719 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300720 }
721 }
722
723 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700724 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200725 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300726}
727
Johan Hedberg84794e12013-11-06 11:24:57 +0200728static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800729{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200730 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300731 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200732
Johan Hedberg84794e12013-11-06 11:24:57 +0200733 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800734 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700735 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800736
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700737 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700738 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300739
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300740 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300741 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800742}
743
Brian Gix2b64d152011-12-21 16:12:12 -0800744#define JUST_WORKS 0x00
745#define JUST_CFM 0x01
746#define REQ_PASSKEY 0x02
747#define CFM_PASSKEY 0x03
748#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300749#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800750#define OVERLAP 0xFF
751
752static const u8 gen_method[5][5] = {
753 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
754 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
755 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
756 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
757 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
758};
759
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300760static const u8 sc_method[5][5] = {
761 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
762 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
763 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
764 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
765 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
766};
767
Johan Hedberg581370c2014-06-17 13:07:38 +0300768static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
769{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300770 /* If either side has unknown io_caps, use JUST_CFM (which gets
771 * converted later to JUST_WORKS if we're initiators.
772 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300773 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
774 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300775 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300776
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300777 if (test_bit(SMP_FLAG_SC, &smp->flags))
778 return sc_method[remote_io][local_io];
779
Johan Hedberg581370c2014-06-17 13:07:38 +0300780 return gen_method[remote_io][local_io];
781}
782
Brian Gix2b64d152011-12-21 16:12:12 -0800783static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
784 u8 local_io, u8 remote_io)
785{
786 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300787 struct l2cap_chan *chan = conn->smp;
788 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800789 u32 passkey = 0;
790 int ret = 0;
791
792 /* Initialize key for JUST WORKS */
793 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300794 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800795
796 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
797
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300798 /* If neither side wants MITM, either "just" confirm an incoming
799 * request or use just-works for outgoing ones. The JUST_CFM
800 * will be converted to JUST_WORKS if necessary later in this
801 * function. If either side has MITM look up the method from the
802 * table.
803 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300804 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300805 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800806 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300807 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800808
Johan Hedberga82505c2014-03-24 14:39:07 +0200809 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300810 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
811 &smp->flags))
812 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200813
Johan Hedberg02f3e252014-07-16 15:09:13 +0300814 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300815 if (smp->method == JUST_CFM &&
816 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
817 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300818
Brian Gix2b64d152011-12-21 16:12:12 -0800819 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300820 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300821 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800822 return 0;
823 }
824
825 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300826 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300827 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300828 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
829 hcon->pending_sec_level = BT_SECURITY_HIGH;
830 }
Brian Gix2b64d152011-12-21 16:12:12 -0800831
832 /* If both devices have Keyoard-Display I/O, the master
833 * Confirms and the slave Enters the passkey.
834 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300835 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300836 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300837 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800838 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300839 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800840 }
841
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200842 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300843 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200844 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800845 get_random_bytes(&passkey, sizeof(passkey));
846 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200847 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800848 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300849 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800850 }
851
Johan Hedberg783e0572014-05-31 18:48:26 +0300852 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700853 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200854 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300855 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200856 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
857 hcon->type, hcon->dst_type,
858 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800859 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200860 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200861 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200862 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800863
Brian Gix2b64d152011-12-21 16:12:12 -0800864 return ret;
865}
866
Johan Hedberg1cc61142014-05-20 09:45:52 +0300867static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300868{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300869 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300870 struct smp_cmd_pairing_confirm cp;
871 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300872
873 BT_DBG("conn %p", conn);
874
Johan Hedberge491eaf2014-10-25 21:15:37 +0200875 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200876 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200877 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
878 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300879 if (ret)
880 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300881
Johan Hedberg4a74d652014-05-20 09:45:50 +0300882 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800883
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300884 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
885
Johan Hedbergb28b4942014-09-05 22:19:55 +0300886 if (conn->hcon->out)
887 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
888 else
889 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
890
Johan Hedberg1cc61142014-05-20 09:45:52 +0300891 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300892}
893
Johan Hedberg861580a2014-05-20 09:45:51 +0300894static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300895{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300896 struct l2cap_conn *conn = smp->conn;
897 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300898 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300899 int ret;
900
Johan Hedbergec70f362014-06-27 14:23:04 +0300901 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300902 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300903
904 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
905
Johan Hedberge491eaf2014-10-25 21:15:37 +0200906 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200907 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200908 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300909 if (ret)
910 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300911
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300912 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
913 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300914 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300915 }
916
917 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800918 u8 stk[16];
919 __le64 rand = 0;
920 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300921
Johan Hedberge491eaf2014-10-25 21:15:37 +0200922 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300923
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300924 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300925 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300926
Johan Hedberg861580a2014-05-20 09:45:51 +0300927 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
928 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300929
930 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300931 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300932 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300933 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300934 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800935 __le64 rand = 0;
936 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300937
Johan Hedberg943a7322014-03-18 12:58:24 +0200938 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
939 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300940
Johan Hedberge491eaf2014-10-25 21:15:37 +0200941 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300942
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300943 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700944 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300945
Johan Hedbergfff34902014-06-10 15:19:50 +0300946 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
947 auth = 1;
948 else
949 auth = 0;
950
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300951 /* Even though there's no _SLAVE suffix this is the
952 * slave STK we're adding for later lookup (the master
953 * STK never needs to be stored).
954 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700955 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300956 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300957 }
958
Johan Hedberg861580a2014-05-20 09:45:51 +0300959 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300960}
961
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300962static void smp_notify_keys(struct l2cap_conn *conn)
963{
964 struct l2cap_chan *chan = conn->smp;
965 struct smp_chan *smp = chan->data;
966 struct hci_conn *hcon = conn->hcon;
967 struct hci_dev *hdev = hcon->hdev;
968 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
969 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
970 bool persistent;
971
972 if (smp->remote_irk) {
973 mgmt_new_irk(hdev, smp->remote_irk);
974 /* Now that user space can be considered to know the
975 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300976 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300977 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300978 if (hcon->type == LE_LINK) {
979 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
980 hcon->dst_type = smp->remote_irk->addr_type;
981 queue_work(hdev->workqueue, &conn->id_addr_update_work);
982 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300983
984 /* When receiving an indentity resolving key for
985 * a remote device that does not use a resolvable
986 * private address, just remove the key so that
987 * it is possible to use the controller white
988 * list for scanning.
989 *
990 * Userspace will have been told to not store
991 * this key at this point. So it is safe to
992 * just remove it.
993 */
994 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200995 list_del_rcu(&smp->remote_irk->list);
996 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300997 smp->remote_irk = NULL;
998 }
999 }
1000
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001001 if (hcon->type == ACL_LINK) {
1002 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1003 persistent = false;
1004 else
1005 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1006 &hcon->flags);
1007 } else {
1008 /* The LTKs and CSRKs should be persistent only if both sides
1009 * had the bonding bit set in their authentication requests.
1010 */
1011 persistent = !!((req->auth_req & rsp->auth_req) &
1012 SMP_AUTH_BONDING);
1013 }
1014
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001015
1016 if (smp->csrk) {
1017 smp->csrk->bdaddr_type = hcon->dst_type;
1018 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1019 mgmt_new_csrk(hdev, smp->csrk, persistent);
1020 }
1021
1022 if (smp->slave_csrk) {
1023 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1024 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1025 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1026 }
1027
1028 if (smp->ltk) {
1029 smp->ltk->bdaddr_type = hcon->dst_type;
1030 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1031 mgmt_new_ltk(hdev, smp->ltk, persistent);
1032 }
1033
1034 if (smp->slave_ltk) {
1035 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1036 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1037 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1038 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001039
1040 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001041 struct link_key *key;
1042 u8 type;
1043
1044 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1045 type = HCI_LK_DEBUG_COMBINATION;
1046 else if (hcon->sec_level == BT_SECURITY_FIPS)
1047 type = HCI_LK_AUTH_COMBINATION_P256;
1048 else
1049 type = HCI_LK_UNAUTH_COMBINATION_P256;
1050
1051 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1052 smp->link_key, type, 0, &persistent);
1053 if (key) {
1054 mgmt_new_link_key(hdev, key, persistent);
1055
1056 /* Don't keep debug keys around if the relevant
1057 * flag is not set.
1058 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001059 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001060 key->type == HCI_LK_DEBUG_COMBINATION) {
1061 list_del_rcu(&key->list);
1062 kfree_rcu(key, rcu);
1063 }
1064 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001065 }
1066}
1067
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001068static void sc_add_ltk(struct smp_chan *smp)
1069{
1070 struct hci_conn *hcon = smp->conn->hcon;
1071 u8 key_type, auth;
1072
1073 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1074 key_type = SMP_LTK_P256_DEBUG;
1075 else
1076 key_type = SMP_LTK_P256;
1077
1078 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1079 auth = 1;
1080 else
1081 auth = 0;
1082
1083 memset(smp->tk + smp->enc_key_size, 0,
1084 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1085
1086 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1087 key_type, auth, smp->tk, smp->enc_key_size,
1088 0, 0);
1089}
1090
Johan Hedberg6a770832014-06-06 11:54:04 +03001091static void sc_generate_link_key(struct smp_chan *smp)
1092{
1093 /* These constants are as specified in the core specification.
1094 * In ASCII they spell out to 'tmp1' and 'lebr'.
1095 */
1096 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1097 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1098
1099 smp->link_key = kzalloc(16, GFP_KERNEL);
1100 if (!smp->link_key)
1101 return;
1102
1103 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001104 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001105 smp->link_key = NULL;
1106 return;
1107 }
1108
1109 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001110 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001111 smp->link_key = NULL;
1112 return;
1113 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001114}
1115
Johan Hedbergb28b4942014-09-05 22:19:55 +03001116static void smp_allow_key_dist(struct smp_chan *smp)
1117{
1118 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1119 * will be allowed in each PDU handler to ensure we receive
1120 * them in the correct order.
1121 */
1122 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1123 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1124 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1125 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1126 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1127 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1128}
1129
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001130static void sc_generate_ltk(struct smp_chan *smp)
1131{
1132 /* These constants are as specified in the core specification.
1133 * In ASCII they spell out to 'tmp2' and 'brle'.
1134 */
1135 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1136 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1137 struct hci_conn *hcon = smp->conn->hcon;
1138 struct hci_dev *hdev = hcon->hdev;
1139 struct link_key *key;
1140
1141 key = hci_find_link_key(hdev, &hcon->dst);
1142 if (!key) {
1143 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1144 return;
1145 }
1146
1147 if (key->type == HCI_LK_DEBUG_COMBINATION)
1148 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1149
1150 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1151 return;
1152
1153 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1154 return;
1155
1156 sc_add_ltk(smp);
1157}
1158
Johan Hedbergd6268e82014-09-05 22:19:51 +03001159static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001160{
1161 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001162 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001163 struct hci_conn *hcon = conn->hcon;
1164 struct hci_dev *hdev = hcon->hdev;
1165 __u8 *keydist;
1166
1167 BT_DBG("conn %p", conn);
1168
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001169 rsp = (void *) &smp->prsp[1];
1170
1171 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001172 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1173 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001174 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001175 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001176
1177 req = (void *) &smp->preq[1];
1178
1179 if (hcon->out) {
1180 keydist = &rsp->init_key_dist;
1181 *keydist &= req->init_key_dist;
1182 } else {
1183 keydist = &rsp->resp_key_dist;
1184 *keydist &= req->resp_key_dist;
1185 }
1186
Johan Hedberg6a770832014-06-06 11:54:04 +03001187 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001188 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001189 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001190 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1191 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001192
1193 /* Clear the keys which are generated but not distributed */
1194 *keydist &= ~SMP_SC_NO_DIST;
1195 }
1196
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001197 BT_DBG("keydist 0x%x", *keydist);
1198
1199 if (*keydist & SMP_DIST_ENC_KEY) {
1200 struct smp_cmd_encrypt_info enc;
1201 struct smp_cmd_master_ident ident;
1202 struct smp_ltk *ltk;
1203 u8 authenticated;
1204 __le16 ediv;
1205 __le64 rand;
1206
1207 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1208 get_random_bytes(&ediv, sizeof(ediv));
1209 get_random_bytes(&rand, sizeof(rand));
1210
1211 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1212
1213 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1214 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1215 SMP_LTK_SLAVE, authenticated, enc.ltk,
1216 smp->enc_key_size, ediv, rand);
1217 smp->slave_ltk = ltk;
1218
1219 ident.ediv = ediv;
1220 ident.rand = rand;
1221
1222 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1223
1224 *keydist &= ~SMP_DIST_ENC_KEY;
1225 }
1226
1227 if (*keydist & SMP_DIST_ID_KEY) {
1228 struct smp_cmd_ident_addr_info addrinfo;
1229 struct smp_cmd_ident_info idinfo;
1230
1231 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1232
1233 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1234
1235 /* The hci_conn contains the local identity address
1236 * after the connection has been established.
1237 *
1238 * This is true even when the connection has been
1239 * established using a resolvable random address.
1240 */
1241 bacpy(&addrinfo.bdaddr, &hcon->src);
1242 addrinfo.addr_type = hcon->src_type;
1243
1244 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1245 &addrinfo);
1246
1247 *keydist &= ~SMP_DIST_ID_KEY;
1248 }
1249
1250 if (*keydist & SMP_DIST_SIGN) {
1251 struct smp_cmd_sign_info sign;
1252 struct smp_csrk *csrk;
1253
1254 /* Generate a new random key */
1255 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1256
1257 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1258 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001259 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1260 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1261 else
1262 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001263 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1264 }
1265 smp->slave_csrk = csrk;
1266
1267 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1268
1269 *keydist &= ~SMP_DIST_SIGN;
1270 }
1271
1272 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001273 if (smp->remote_key_dist & KEY_DIST_MASK) {
1274 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001275 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001276 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001277
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001278 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1279 smp_notify_keys(conn);
1280
1281 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001282}
1283
Johan Hedbergb68fda62014-08-11 22:06:40 +03001284static void smp_timeout(struct work_struct *work)
1285{
1286 struct smp_chan *smp = container_of(work, struct smp_chan,
1287 security_timer.work);
1288 struct l2cap_conn *conn = smp->conn;
1289
1290 BT_DBG("conn %p", conn);
1291
Johan Hedberg1e91c292014-08-18 20:33:29 +03001292 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001293}
1294
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001295static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1296{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001297 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001298 struct smp_chan *smp;
1299
Marcel Holtmannf1560462013-10-13 05:43:25 -07001300 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001301 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001302 return NULL;
1303
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001304 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1305 if (IS_ERR(smp->tfm_aes)) {
1306 BT_ERR("Unable to create ECB crypto context");
Marcel Holtmann276812e2015-03-16 01:10:18 -07001307 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001308 return NULL;
1309 }
1310
Johan Hedberg407cecf2014-05-02 14:19:47 +03001311 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1312 if (IS_ERR(smp->tfm_cmac)) {
1313 BT_ERR("Unable to create CMAC crypto context");
1314 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann276812e2015-03-16 01:10:18 -07001315 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001316 return NULL;
1317 }
1318
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001319 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001320 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001321
Johan Hedbergb28b4942014-09-05 22:19:55 +03001322 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1323
Johan Hedbergb68fda62014-08-11 22:06:40 +03001324 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1325
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001326 hci_conn_hold(conn->hcon);
1327
1328 return smp;
1329}
1330
Johan Hedberg760b0182014-06-06 11:44:05 +03001331static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1332{
1333 struct hci_conn *hcon = smp->conn->hcon;
1334 u8 *na, *nb, a[7], b[7];
1335
1336 if (hcon->out) {
1337 na = smp->prnd;
1338 nb = smp->rrnd;
1339 } else {
1340 na = smp->rrnd;
1341 nb = smp->prnd;
1342 }
1343
1344 memcpy(a, &hcon->init_addr, 6);
1345 memcpy(b, &hcon->resp_addr, 6);
1346 a[6] = hcon->init_addr_type;
1347 b[6] = hcon->resp_addr_type;
1348
1349 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1350}
1351
Johan Hedberg38606f12014-06-25 11:10:28 +03001352static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001353{
1354 struct hci_conn *hcon = smp->conn->hcon;
1355 struct smp_cmd_dhkey_check check;
1356 u8 a[7], b[7], *local_addr, *remote_addr;
1357 u8 io_cap[3], r[16];
1358
Johan Hedberg760b0182014-06-06 11:44:05 +03001359 memcpy(a, &hcon->init_addr, 6);
1360 memcpy(b, &hcon->resp_addr, 6);
1361 a[6] = hcon->init_addr_type;
1362 b[6] = hcon->resp_addr_type;
1363
1364 if (hcon->out) {
1365 local_addr = a;
1366 remote_addr = b;
1367 memcpy(io_cap, &smp->preq[1], 3);
1368 } else {
1369 local_addr = b;
1370 remote_addr = a;
1371 memcpy(io_cap, &smp->prsp[1], 3);
1372 }
1373
Johan Hedbergdddd3052014-06-01 15:38:09 +03001374 memset(r, 0, sizeof(r));
1375
1376 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001377 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001378
Johan Hedberga29b0732014-10-28 15:17:05 +01001379 if (smp->method == REQ_OOB)
1380 memcpy(r, smp->rr, 16);
1381
Johan Hedberg760b0182014-06-06 11:44:05 +03001382 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1383 local_addr, remote_addr, check.e);
1384
1385 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001386}
1387
Johan Hedberg38606f12014-06-25 11:10:28 +03001388static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1389{
1390 struct l2cap_conn *conn = smp->conn;
1391 struct hci_conn *hcon = conn->hcon;
1392 struct smp_cmd_pairing_confirm cfm;
1393 u8 r;
1394
1395 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1396 r |= 0x80;
1397
1398 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1399
1400 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1401 cfm.confirm_val))
1402 return SMP_UNSPECIFIED;
1403
1404 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1405
1406 return 0;
1407}
1408
1409static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1410{
1411 struct l2cap_conn *conn = smp->conn;
1412 struct hci_conn *hcon = conn->hcon;
1413 struct hci_dev *hdev = hcon->hdev;
1414 u8 cfm[16], r;
1415
1416 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1417 if (smp->passkey_round >= 20)
1418 return 0;
1419
1420 switch (smp_op) {
1421 case SMP_CMD_PAIRING_RANDOM:
1422 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1423 r |= 0x80;
1424
1425 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1426 smp->rrnd, r, cfm))
1427 return SMP_UNSPECIFIED;
1428
1429 if (memcmp(smp->pcnf, cfm, 16))
1430 return SMP_CONFIRM_FAILED;
1431
1432 smp->passkey_round++;
1433
1434 if (smp->passkey_round == 20) {
1435 /* Generate MacKey and LTK */
1436 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1437 return SMP_UNSPECIFIED;
1438 }
1439
1440 /* The round is only complete when the initiator
1441 * receives pairing random.
1442 */
1443 if (!hcon->out) {
1444 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1445 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001446 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001447 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001448 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001449 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001450 return 0;
1451 }
1452
1453 /* Start the next round */
1454 if (smp->passkey_round != 20)
1455 return sc_passkey_round(smp, 0);
1456
1457 /* Passkey rounds are complete - start DHKey Check */
1458 sc_dhkey_check(smp);
1459 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1460
1461 break;
1462
1463 case SMP_CMD_PAIRING_CONFIRM:
1464 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1465 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1466 return 0;
1467 }
1468
1469 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1470
1471 if (hcon->out) {
1472 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1473 sizeof(smp->prnd), smp->prnd);
1474 return 0;
1475 }
1476
1477 return sc_passkey_send_confirm(smp);
1478
1479 case SMP_CMD_PUBLIC_KEY:
1480 default:
1481 /* Initiating device starts the round */
1482 if (!hcon->out)
1483 return 0;
1484
1485 BT_DBG("%s Starting passkey round %u", hdev->name,
1486 smp->passkey_round + 1);
1487
1488 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1489
1490 return sc_passkey_send_confirm(smp);
1491 }
1492
1493 return 0;
1494}
1495
Johan Hedbergdddd3052014-06-01 15:38:09 +03001496static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1497{
Johan Hedberg38606f12014-06-25 11:10:28 +03001498 struct l2cap_conn *conn = smp->conn;
1499 struct hci_conn *hcon = conn->hcon;
1500 u8 smp_op;
1501
1502 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1503
Johan Hedbergdddd3052014-06-01 15:38:09 +03001504 switch (mgmt_op) {
1505 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1506 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1507 return 0;
1508 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1509 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1510 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001511 case MGMT_OP_USER_PASSKEY_REPLY:
1512 hcon->passkey_notify = le32_to_cpu(passkey);
1513 smp->passkey_round = 0;
1514
1515 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1516 smp_op = SMP_CMD_PAIRING_CONFIRM;
1517 else
1518 smp_op = 0;
1519
1520 if (sc_passkey_round(smp, smp_op))
1521 return -EIO;
1522
1523 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001524 }
1525
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001526 /* Initiator sends DHKey check first */
1527 if (hcon->out) {
1528 sc_dhkey_check(smp);
1529 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1530 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1531 sc_dhkey_check(smp);
1532 sc_add_ltk(smp);
1533 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001534
1535 return 0;
1536}
1537
Brian Gix2b64d152011-12-21 16:12:12 -08001538int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1539{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001540 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001541 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001542 struct smp_chan *smp;
1543 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001544 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001545
1546 BT_DBG("");
1547
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001548 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001549 return -ENOTCONN;
1550
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001551 chan = conn->smp;
1552 if (!chan)
1553 return -ENOTCONN;
1554
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001555 l2cap_chan_lock(chan);
1556 if (!chan->data) {
1557 err = -ENOTCONN;
1558 goto unlock;
1559 }
1560
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001561 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001562
Johan Hedberg760b0182014-06-06 11:44:05 +03001563 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1564 err = sc_user_reply(smp, mgmt_op, passkey);
1565 goto unlock;
1566 }
1567
Brian Gix2b64d152011-12-21 16:12:12 -08001568 switch (mgmt_op) {
1569 case MGMT_OP_USER_PASSKEY_REPLY:
1570 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001571 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001572 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001573 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001574 /* Fall Through */
1575 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001576 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001577 break;
1578 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1579 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001580 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001581 err = 0;
1582 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001583 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001584 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001585 err = -EOPNOTSUPP;
1586 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001587 }
1588
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001589 err = 0;
1590
Brian Gix2b64d152011-12-21 16:12:12 -08001591 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001592 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1593 u8 rsp = smp_confirm(smp);
1594 if (rsp)
1595 smp_failure(conn, rsp);
1596 }
Brian Gix2b64d152011-12-21 16:12:12 -08001597
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001598unlock:
1599 l2cap_chan_unlock(chan);
1600 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001601}
1602
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001603static void build_bredr_pairing_cmd(struct smp_chan *smp,
1604 struct smp_cmd_pairing *req,
1605 struct smp_cmd_pairing *rsp)
1606{
1607 struct l2cap_conn *conn = smp->conn;
1608 struct hci_dev *hdev = conn->hcon->hdev;
1609 u8 local_dist = 0, remote_dist = 0;
1610
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001611 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001612 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1613 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1614 }
1615
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001616 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001617 remote_dist |= SMP_DIST_ID_KEY;
1618
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001619 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001620 local_dist |= SMP_DIST_ID_KEY;
1621
1622 if (!rsp) {
1623 memset(req, 0, sizeof(*req));
1624
1625 req->init_key_dist = local_dist;
1626 req->resp_key_dist = remote_dist;
1627 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1628
1629 smp->remote_key_dist = remote_dist;
1630
1631 return;
1632 }
1633
1634 memset(rsp, 0, sizeof(*rsp));
1635
1636 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1637 rsp->init_key_dist = req->init_key_dist & remote_dist;
1638 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1639
1640 smp->remote_key_dist = rsp->init_key_dist;
1641}
1642
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001643static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001644{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001645 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001646 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001647 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001648 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001649 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001650 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001651
1652 BT_DBG("conn %p", conn);
1653
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001654 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001655 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001656
Johan Hedberg40bef302014-07-16 11:42:27 +03001657 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001658 return SMP_CMD_NOTSUPP;
1659
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001660 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001661 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001662 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001663 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001664
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001665 if (!smp)
1666 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001667
Johan Hedbergc05b9332014-09-10 17:37:42 -07001668 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001669 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001670
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001671 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001672 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001673 return SMP_PAIRING_NOTSUPP;
1674
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001675 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001676 return SMP_AUTH_REQUIREMENTS;
1677
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001678 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1679 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001680 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001681
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001682 /* SMP over BR/EDR requires special treatment */
1683 if (conn->hcon->type == ACL_LINK) {
1684 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001685 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001686 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001687 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1688
1689 set_bit(SMP_FLAG_SC, &smp->flags);
1690
1691 build_bredr_pairing_cmd(smp, req, &rsp);
1692
1693 key_size = min(req->max_key_size, rsp.max_key_size);
1694 if (check_enc_key_size(conn, key_size))
1695 return SMP_ENC_KEY_SIZE;
1696
1697 /* Clear bits which are generated but not distributed */
1698 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1699
1700 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1701 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1702 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1703
1704 smp_distribute_keys(smp);
1705 return 0;
1706 }
1707
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001708 build_pairing_cmd(conn, req, &rsp, auth);
1709
1710 if (rsp.auth_req & SMP_AUTH_SC)
1711 set_bit(SMP_FLAG_SC, &smp->flags);
1712
Johan Hedberg5be5e272014-09-10 17:58:54 -07001713 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001714 sec_level = BT_SECURITY_MEDIUM;
1715 else
1716 sec_level = authreq_to_seclevel(auth);
1717
Johan Hedbergc7262e72014-06-17 13:07:37 +03001718 if (sec_level > conn->hcon->pending_sec_level)
1719 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001720
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001721 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001722 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1723 u8 method;
1724
1725 method = get_auth_method(smp, conn->hcon->io_capability,
1726 req->io_capability);
1727 if (method == JUST_WORKS || method == JUST_CFM)
1728 return SMP_AUTH_REQUIREMENTS;
1729 }
1730
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001731 key_size = min(req->max_key_size, rsp.max_key_size);
1732 if (check_enc_key_size(conn, key_size))
1733 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001734
Johan Hedberge84a6b12013-12-02 10:49:03 +02001735 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001736
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001737 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1738 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001739
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001740 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001741
1742 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1743
1744 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1745 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1746 /* Clear bits which are generated but not distributed */
1747 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1748 /* Wait for Public Key from Initiating Device */
1749 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001750 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001751
Marcel Holtmann983f9812015-03-11 17:47:40 -07001752 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1753
Brian Gix2b64d152011-12-21 16:12:12 -08001754 /* Request setup of TK */
1755 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1756 if (ret)
1757 return SMP_UNSPECIFIED;
1758
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001759 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001760}
1761
Johan Hedberg3b191462014-06-06 10:50:15 +03001762static u8 sc_send_public_key(struct smp_chan *smp)
1763{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001764 struct hci_dev *hdev = smp->conn->hcon->hdev;
1765
Johan Hedberg3b191462014-06-06 10:50:15 +03001766 BT_DBG("");
1767
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001768 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001769 BT_DBG("Using debug keys");
1770 memcpy(smp->local_pk, debug_pk, 64);
1771 memcpy(smp->local_sk, debug_sk, 32);
1772 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1773 } else {
1774 while (true) {
1775 /* Generate local key pair for Secure Connections */
1776 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1777 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001778
Johan Hedberg70157ef2014-06-24 15:22:59 +03001779 /* This is unlikely, but we need to check that
1780 * we didn't accidentially generate a debug key.
1781 */
1782 if (memcmp(smp->local_sk, debug_sk, 32))
1783 break;
1784 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001785 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001786
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001787 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1788 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1789 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001790
1791 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1792
1793 return 0;
1794}
1795
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001796static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001797{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001798 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001799 struct l2cap_chan *chan = conn->smp;
1800 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001801 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001802 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001803 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001804
1805 BT_DBG("conn %p", conn);
1806
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001807 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001808 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001809
Johan Hedberg40bef302014-07-16 11:42:27 +03001810 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001811 return SMP_CMD_NOTSUPP;
1812
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001813 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001814
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001815 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001816
1817 key_size = min(req->max_key_size, rsp->max_key_size);
1818 if (check_enc_key_size(conn, key_size))
1819 return SMP_ENC_KEY_SIZE;
1820
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001821 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001822
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001823 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001824 return SMP_AUTH_REQUIREMENTS;
1825
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001826 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1827 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1828
1829 /* Update remote key distribution in case the remote cleared
1830 * some bits that we had enabled in our request.
1831 */
1832 smp->remote_key_dist &= rsp->resp_key_dist;
1833
1834 /* For BR/EDR this means we're done and can start phase 3 */
1835 if (conn->hcon->type == ACL_LINK) {
1836 /* Clear bits which are generated but not distributed */
1837 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1838 smp_distribute_keys(smp);
1839 return 0;
1840 }
1841
Johan Hedberg65668772014-05-16 11:03:34 +03001842 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1843 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001844 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1845 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001846
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001847 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001848 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1849 u8 method;
1850
1851 method = get_auth_method(smp, req->io_capability,
1852 rsp->io_capability);
1853 if (method == JUST_WORKS || method == JUST_CFM)
1854 return SMP_AUTH_REQUIREMENTS;
1855 }
1856
Johan Hedberge84a6b12013-12-02 10:49:03 +02001857 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001858
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001859 /* Update remote key distribution in case the remote cleared
1860 * some bits that we had enabled in our request.
1861 */
1862 smp->remote_key_dist &= rsp->resp_key_dist;
1863
Johan Hedberg3b191462014-06-06 10:50:15 +03001864 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1865 /* Clear bits which are generated but not distributed */
1866 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1867 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1868 return sc_send_public_key(smp);
1869 }
1870
Johan Hedbergc05b9332014-09-10 17:37:42 -07001871 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001872
Johan Hedberg476585e2012-06-06 18:54:15 +08001873 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001874 if (ret)
1875 return SMP_UNSPECIFIED;
1876
Johan Hedberg4a74d652014-05-20 09:45:50 +03001877 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001878
1879 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001880 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001881 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001882
1883 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001884}
1885
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001886static u8 sc_check_confirm(struct smp_chan *smp)
1887{
1888 struct l2cap_conn *conn = smp->conn;
1889
1890 BT_DBG("");
1891
1892 /* Public Key exchange must happen before any other steps */
1893 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1894 return SMP_UNSPECIFIED;
1895
Johan Hedberg38606f12014-06-25 11:10:28 +03001896 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1897 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1898
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001899 if (conn->hcon->out) {
1900 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1901 smp->prnd);
1902 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1903 }
1904
1905 return 0;
1906}
1907
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001908static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001909{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001910 struct l2cap_chan *chan = conn->smp;
1911 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001912
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001913 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1914
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001915 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001916 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001917
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001918 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1919 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001920
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001921 if (test_bit(SMP_FLAG_SC, &smp->flags))
1922 return sc_check_confirm(smp);
1923
Johan Hedbergb28b4942014-09-05 22:19:55 +03001924 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001925 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1926 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001927 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1928 return 0;
1929 }
1930
1931 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001932 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07001933
1934 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001935
1936 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001937}
1938
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001939static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001940{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001941 struct l2cap_chan *chan = conn->smp;
1942 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001943 struct hci_conn *hcon = conn->hcon;
1944 u8 *pkax, *pkbx, *na, *nb;
1945 u32 passkey;
1946 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001947
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001948 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001949
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001950 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001951 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001952
Johan Hedberg943a7322014-03-18 12:58:24 +02001953 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001954 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001955
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001956 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1957 return smp_random(smp);
1958
Johan Hedberg580039e2014-12-03 16:26:37 +02001959 if (hcon->out) {
1960 pkax = smp->local_pk;
1961 pkbx = smp->remote_pk;
1962 na = smp->prnd;
1963 nb = smp->rrnd;
1964 } else {
1965 pkax = smp->remote_pk;
1966 pkbx = smp->local_pk;
1967 na = smp->rrnd;
1968 nb = smp->prnd;
1969 }
1970
Johan Hedberga29b0732014-10-28 15:17:05 +01001971 if (smp->method == REQ_OOB) {
1972 if (!hcon->out)
1973 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1974 sizeof(smp->prnd), smp->prnd);
1975 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1976 goto mackey_and_ltk;
1977 }
1978
Johan Hedberg38606f12014-06-25 11:10:28 +03001979 /* Passkey entry has special treatment */
1980 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1981 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1982
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001983 if (hcon->out) {
1984 u8 cfm[16];
1985
1986 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1987 smp->rrnd, 0, cfm);
1988 if (err)
1989 return SMP_UNSPECIFIED;
1990
1991 if (memcmp(smp->pcnf, cfm, 16))
1992 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001993 } else {
1994 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1995 smp->prnd);
1996 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001997 }
1998
Johan Hedberga29b0732014-10-28 15:17:05 +01001999mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002000 /* Generate MacKey and LTK */
2001 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2002 if (err)
2003 return SMP_UNSPECIFIED;
2004
Johan Hedberga29b0732014-10-28 15:17:05 +01002005 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002006 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002007 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002008 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2009 }
2010 return 0;
2011 }
2012
Johan Hedberg38606f12014-06-25 11:10:28 +03002013 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002014 if (err)
2015 return SMP_UNSPECIFIED;
2016
Johan Hedberg38606f12014-06-25 11:10:28 +03002017 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2018 hcon->dst_type, passkey, 0);
2019 if (err)
2020 return SMP_UNSPECIFIED;
2021
2022 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2023
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002024 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002025}
2026
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002027static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002028{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002029 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002030 struct hci_conn *hcon = conn->hcon;
2031
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002032 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002033 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002034 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002035
Johan Hedberga6f78332014-09-10 17:37:45 -07002036 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002037 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002038
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002039 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002040 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002041
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002042 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2043 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002044
Johan Hedbergfe59a052014-07-01 19:14:12 +03002045 /* We never store STKs for master role, so clear this flag */
2046 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2047
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002048 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002049}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002050
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002051bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2052 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002053{
2054 if (sec_level == BT_SECURITY_LOW)
2055 return true;
2056
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002057 /* If we're encrypted with an STK but the caller prefers using
2058 * LTK claim insufficient security. This way we allow the
2059 * connection to be re-encrypted with an LTK, even if the LTK
2060 * provides the same level of security. Only exception is if we
2061 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002062 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002063 if (key_pref == SMP_USE_LTK &&
2064 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002065 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002066 return false;
2067
Johan Hedberg854f4722014-07-01 18:40:20 +03002068 if (hcon->sec_level >= sec_level)
2069 return true;
2070
2071 return false;
2072}
2073
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002074static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002075{
2076 struct smp_cmd_security_req *rp = (void *) skb->data;
2077 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002078 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002079 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002080 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002081 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002082
2083 BT_DBG("conn %p", conn);
2084
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002085 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002086 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002087
Johan Hedberg40bef302014-07-16 11:42:27 +03002088 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002089 return SMP_CMD_NOTSUPP;
2090
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002091 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002092
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002093 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002094 return SMP_AUTH_REQUIREMENTS;
2095
Johan Hedberg5be5e272014-09-10 17:58:54 -07002096 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002097 sec_level = BT_SECURITY_MEDIUM;
2098 else
2099 sec_level = authreq_to_seclevel(auth);
2100
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002101 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002102 return 0;
2103
Johan Hedbergc7262e72014-06-17 13:07:37 +03002104 if (sec_level > hcon->pending_sec_level)
2105 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002106
Johan Hedberg4dab7862012-06-07 14:58:37 +08002107 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002108 return 0;
2109
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002110 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002111 if (!smp)
2112 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002113
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002114 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002115 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002116 return SMP_PAIRING_NOTSUPP;
2117
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002118 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002119
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002120 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002121 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002122
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002123 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2124 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002125
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002126 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002127 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002128
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002129 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002130}
2131
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002132int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002133{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002134 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002135 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002136 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002137 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002138 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002139
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002140 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2141
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002142 /* This may be NULL if there's an unexpected disconnection */
2143 if (!conn)
2144 return 1;
2145
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002146 chan = conn->smp;
2147
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002148 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002149 return 1;
2150
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002151 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002152 return 1;
2153
Johan Hedbergc7262e72014-06-17 13:07:37 +03002154 if (sec_level > hcon->pending_sec_level)
2155 hcon->pending_sec_level = sec_level;
2156
Johan Hedberg40bef302014-07-16 11:42:27 +03002157 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002158 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2159 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002160
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002161 l2cap_chan_lock(chan);
2162
2163 /* If SMP is already in progress ignore this request */
2164 if (chan->data) {
2165 ret = 0;
2166 goto unlock;
2167 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002168
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002169 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002170 if (!smp) {
2171 ret = 1;
2172 goto unlock;
2173 }
Brian Gix2b64d152011-12-21 16:12:12 -08002174
2175 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002176
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002177 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002178 authreq |= SMP_AUTH_SC;
2179
Johan Hedberg79897d22014-06-01 09:45:24 +03002180 /* Require MITM if IO Capability allows or the security level
2181 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002182 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002183 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002184 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002185 authreq |= SMP_AUTH_MITM;
2186
Johan Hedberg40bef302014-07-16 11:42:27 +03002187 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002188 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002189
Brian Gix2b64d152011-12-21 16:12:12 -08002190 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002191 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2192 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002193
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002194 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002195 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002196 } else {
2197 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002198 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002199 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002200 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002201 }
2202
Johan Hedberg4a74d652014-05-20 09:45:50 +03002203 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002204 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002205
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002206unlock:
2207 l2cap_chan_unlock(chan);
2208 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002209}
2210
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002211static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2212{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002213 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002214 struct l2cap_chan *chan = conn->smp;
2215 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002216
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002217 BT_DBG("conn %p", conn);
2218
2219 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002220 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002221
Johan Hedbergb28b4942014-09-05 22:19:55 +03002222 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002223
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002224 skb_pull(skb, sizeof(*rp));
2225
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002226 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002227
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002228 return 0;
2229}
2230
2231static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2232{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002233 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002234 struct l2cap_chan *chan = conn->smp;
2235 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002236 struct hci_dev *hdev = conn->hcon->hdev;
2237 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002238 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002239 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002240
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002241 BT_DBG("conn %p", conn);
2242
2243 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002244 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002245
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002246 /* Mark the information as received */
2247 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2248
Johan Hedbergb28b4942014-09-05 22:19:55 +03002249 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2250 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002251 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2252 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002253
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002254 skb_pull(skb, sizeof(*rp));
2255
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002256 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002257 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002258 authenticated, smp->tk, smp->enc_key_size,
2259 rp->ediv, rp->rand);
2260 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002261 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002262 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002263
2264 return 0;
2265}
2266
Johan Hedbergfd349c02014-02-18 10:19:36 +02002267static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2268{
2269 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002270 struct l2cap_chan *chan = conn->smp;
2271 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002272
2273 BT_DBG("");
2274
2275 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002276 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002277
Johan Hedbergb28b4942014-09-05 22:19:55 +03002278 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002279
Johan Hedbergfd349c02014-02-18 10:19:36 +02002280 skb_pull(skb, sizeof(*info));
2281
2282 memcpy(smp->irk, info->irk, 16);
2283
2284 return 0;
2285}
2286
2287static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2288 struct sk_buff *skb)
2289{
2290 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002291 struct l2cap_chan *chan = conn->smp;
2292 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002293 struct hci_conn *hcon = conn->hcon;
2294 bdaddr_t rpa;
2295
2296 BT_DBG("");
2297
2298 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002299 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002300
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002301 /* Mark the information as received */
2302 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2303
Johan Hedbergb28b4942014-09-05 22:19:55 +03002304 if (smp->remote_key_dist & SMP_DIST_SIGN)
2305 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2306
Johan Hedbergfd349c02014-02-18 10:19:36 +02002307 skb_pull(skb, sizeof(*info));
2308
Johan Hedberga9a58f82014-02-25 22:24:37 +02002309 /* Strictly speaking the Core Specification (4.1) allows sending
2310 * an empty address which would force us to rely on just the IRK
2311 * as "identity information". However, since such
2312 * implementations are not known of and in order to not over
2313 * complicate our implementation, simply pretend that we never
2314 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002315 *
2316 * The Identity Address must also be a Static Random or Public
2317 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002318 */
Johan Hedberge12af482015-01-14 20:51:37 +02002319 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2320 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002321 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002322 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002323 }
2324
Johan Hedbergfd349c02014-02-18 10:19:36 +02002325 bacpy(&smp->id_addr, &info->bdaddr);
2326 smp->id_addr_type = info->addr_type;
2327
2328 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2329 bacpy(&rpa, &hcon->dst);
2330 else
2331 bacpy(&rpa, BDADDR_ANY);
2332
Johan Hedberg23d0e122014-02-19 14:57:46 +02002333 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2334 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002335
Johan Hedberg31dd6242014-06-27 14:23:02 +03002336distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002337 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2338 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002339
2340 return 0;
2341}
2342
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002343static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2344{
2345 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002346 struct l2cap_chan *chan = conn->smp;
2347 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002348 struct smp_csrk *csrk;
2349
2350 BT_DBG("conn %p", conn);
2351
2352 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002353 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002354
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002355 /* Mark the information as received */
2356 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2357
2358 skb_pull(skb, sizeof(*rp));
2359
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002360 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2361 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002362 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2363 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2364 else
2365 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002366 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2367 }
2368 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002369 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002370
2371 return 0;
2372}
2373
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002374static u8 sc_select_method(struct smp_chan *smp)
2375{
2376 struct l2cap_conn *conn = smp->conn;
2377 struct hci_conn *hcon = conn->hcon;
2378 struct smp_cmd_pairing *local, *remote;
2379 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2380
Johan Hedberga29b0732014-10-28 15:17:05 +01002381 if (test_bit(SMP_FLAG_OOB, &smp->flags))
2382 return REQ_OOB;
2383
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002384 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2385 * which are needed as inputs to some crypto functions. To get
2386 * the "struct smp_cmd_pairing" from them we need to skip the
2387 * first byte which contains the opcode.
2388 */
2389 if (hcon->out) {
2390 local = (void *) &smp->preq[1];
2391 remote = (void *) &smp->prsp[1];
2392 } else {
2393 local = (void *) &smp->prsp[1];
2394 remote = (void *) &smp->preq[1];
2395 }
2396
2397 local_io = local->io_capability;
2398 remote_io = remote->io_capability;
2399
2400 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2401 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2402
2403 /* If either side wants MITM, look up the method from the table,
2404 * otherwise use JUST WORKS.
2405 */
2406 if (local_mitm || remote_mitm)
2407 method = get_auth_method(smp, local_io, remote_io);
2408 else
2409 method = JUST_WORKS;
2410
2411 /* Don't confirm locally initiated pairing attempts */
2412 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2413 method = JUST_WORKS;
2414
2415 return method;
2416}
2417
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002418static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2419{
2420 struct smp_cmd_public_key *key = (void *) skb->data;
2421 struct hci_conn *hcon = conn->hcon;
2422 struct l2cap_chan *chan = conn->smp;
2423 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002424 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002425 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002426 int err;
2427
2428 BT_DBG("conn %p", conn);
2429
2430 if (skb->len < sizeof(*key))
2431 return SMP_INVALID_PARAMS;
2432
2433 memcpy(smp->remote_pk, key, 64);
2434
2435 /* Non-initiating device sends its public key after receiving
2436 * the key from the initiating device.
2437 */
2438 if (!hcon->out) {
2439 err = sc_send_public_key(smp);
2440 if (err)
2441 return err;
2442 }
2443
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002444 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2445 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002446
2447 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2448 return SMP_UNSPECIFIED;
2449
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002450 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002451
2452 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2453
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002454 smp->method = sc_select_method(smp);
2455
2456 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2457
2458 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2459 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2460 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2461 else
2462 hcon->pending_sec_level = BT_SECURITY_FIPS;
2463
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002464 if (!memcmp(debug_pk, smp->remote_pk, 64))
2465 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2466
Johan Hedberg38606f12014-06-25 11:10:28 +03002467 if (smp->method == DSP_PASSKEY) {
2468 get_random_bytes(&hcon->passkey_notify,
2469 sizeof(hcon->passkey_notify));
2470 hcon->passkey_notify %= 1000000;
2471 hcon->passkey_entered = 0;
2472 smp->passkey_round = 0;
2473 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2474 hcon->dst_type,
2475 hcon->passkey_notify,
2476 hcon->passkey_entered))
2477 return SMP_UNSPECIFIED;
2478 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2479 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2480 }
2481
Johan Hedberga29b0732014-10-28 15:17:05 +01002482 if (smp->method == REQ_OOB) {
2483 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2484 smp->rr, 0, cfm.confirm_val);
2485 if (err)
2486 return SMP_UNSPECIFIED;
2487
2488 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2489 return SMP_CONFIRM_FAILED;
2490
2491 if (hcon->out)
2492 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2493 sizeof(smp->prnd), smp->prnd);
2494
2495 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2496
2497 return 0;
2498 }
2499
Johan Hedberg38606f12014-06-25 11:10:28 +03002500 if (hcon->out)
2501 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2502
2503 if (smp->method == REQ_PASSKEY) {
2504 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2505 hcon->dst_type))
2506 return SMP_UNSPECIFIED;
2507 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2508 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2509 return 0;
2510 }
2511
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002512 /* The Initiating device waits for the non-initiating device to
2513 * send the confirm value.
2514 */
2515 if (conn->hcon->out)
2516 return 0;
2517
2518 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2519 0, cfm.confirm_val);
2520 if (err)
2521 return SMP_UNSPECIFIED;
2522
2523 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2524 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2525
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002526 return 0;
2527}
2528
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002529static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2530{
2531 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2532 struct l2cap_chan *chan = conn->smp;
2533 struct hci_conn *hcon = conn->hcon;
2534 struct smp_chan *smp = chan->data;
2535 u8 a[7], b[7], *local_addr, *remote_addr;
2536 u8 io_cap[3], r[16], e[16];
2537 int err;
2538
2539 BT_DBG("conn %p", conn);
2540
2541 if (skb->len < sizeof(*check))
2542 return SMP_INVALID_PARAMS;
2543
2544 memcpy(a, &hcon->init_addr, 6);
2545 memcpy(b, &hcon->resp_addr, 6);
2546 a[6] = hcon->init_addr_type;
2547 b[6] = hcon->resp_addr_type;
2548
2549 if (hcon->out) {
2550 local_addr = a;
2551 remote_addr = b;
2552 memcpy(io_cap, &smp->prsp[1], 3);
2553 } else {
2554 local_addr = b;
2555 remote_addr = a;
2556 memcpy(io_cap, &smp->preq[1], 3);
2557 }
2558
2559 memset(r, 0, sizeof(r));
2560
Johan Hedberg38606f12014-06-25 11:10:28 +03002561 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2562 put_unaligned_le32(hcon->passkey_notify, r);
2563
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002564 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2565 io_cap, remote_addr, local_addr, e);
2566 if (err)
2567 return SMP_UNSPECIFIED;
2568
2569 if (memcmp(check->e, e, 16))
2570 return SMP_DHKEY_CHECK_FAILED;
2571
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002572 if (!hcon->out) {
2573 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2574 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2575 return 0;
2576 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002577
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002578 /* Slave sends DHKey check as response to master */
2579 sc_dhkey_check(smp);
2580 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002581
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002582 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002583
2584 if (hcon->out) {
2585 hci_le_start_enc(hcon, 0, 0, smp->tk);
2586 hcon->enc_key_size = smp->enc_key_size;
2587 }
2588
2589 return 0;
2590}
2591
Johan Hedberg1408bb62014-06-04 22:45:57 +03002592static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2593 struct sk_buff *skb)
2594{
2595 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2596
2597 BT_DBG("value 0x%02x", kp->value);
2598
2599 return 0;
2600}
2601
Johan Hedberg4befb862014-08-11 22:06:38 +03002602static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002603{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002604 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002605 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002606 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002607 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002608 int err = 0;
2609
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002610 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002611 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002612
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002613 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002614 reason = SMP_PAIRING_NOTSUPP;
2615 goto done;
2616 }
2617
Marcel Holtmann92381f52013-10-03 01:23:08 -07002618 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002619 skb_pull(skb, sizeof(code));
2620
Johan Hedbergb28b4942014-09-05 22:19:55 +03002621 smp = chan->data;
2622
2623 if (code > SMP_CMD_MAX)
2624 goto drop;
2625
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002626 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002627 goto drop;
2628
2629 /* If we don't have a context the only allowed commands are
2630 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002631 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002632 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2633 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002634
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002635 switch (code) {
2636 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002637 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002638 break;
2639
2640 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002641 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002642 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002643 break;
2644
2645 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002646 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002647 break;
2648
2649 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002650 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002651 break;
2652
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002653 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002654 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002655 break;
2656
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002657 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002658 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002659 break;
2660
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002661 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002662 reason = smp_cmd_encrypt_info(conn, skb);
2663 break;
2664
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002665 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002666 reason = smp_cmd_master_ident(conn, skb);
2667 break;
2668
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002669 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002670 reason = smp_cmd_ident_info(conn, skb);
2671 break;
2672
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002673 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002674 reason = smp_cmd_ident_addr_info(conn, skb);
2675 break;
2676
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002677 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002678 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002679 break;
2680
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002681 case SMP_CMD_PUBLIC_KEY:
2682 reason = smp_cmd_public_key(conn, skb);
2683 break;
2684
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002685 case SMP_CMD_DHKEY_CHECK:
2686 reason = smp_cmd_dhkey_check(conn, skb);
2687 break;
2688
Johan Hedberg1408bb62014-06-04 22:45:57 +03002689 case SMP_CMD_KEYPRESS_NOTIFY:
2690 reason = smp_cmd_keypress_notify(conn, skb);
2691 break;
2692
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002693 default:
2694 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002695 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002696 goto done;
2697 }
2698
2699done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002700 if (!err) {
2701 if (reason)
2702 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002703 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002704 }
2705
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002706 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002707
2708drop:
2709 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2710 code, &hcon->dst);
2711 kfree_skb(skb);
2712 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002713}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002714
Johan Hedberg70db83c2014-08-08 09:37:16 +03002715static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2716{
2717 struct l2cap_conn *conn = chan->conn;
2718
2719 BT_DBG("chan %p", chan);
2720
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002721 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002722 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002723
Johan Hedberg70db83c2014-08-08 09:37:16 +03002724 conn->smp = NULL;
2725 l2cap_chan_put(chan);
2726}
2727
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002728static void bredr_pairing(struct l2cap_chan *chan)
2729{
2730 struct l2cap_conn *conn = chan->conn;
2731 struct hci_conn *hcon = conn->hcon;
2732 struct hci_dev *hdev = hcon->hdev;
2733 struct smp_cmd_pairing req;
2734 struct smp_chan *smp;
2735
2736 BT_DBG("chan %p", chan);
2737
2738 /* Only new pairings are interesting */
2739 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2740 return;
2741
2742 /* Don't bother if we're not encrypted */
2743 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2744 return;
2745
2746 /* Only master may initiate SMP over BR/EDR */
2747 if (hcon->role != HCI_ROLE_MASTER)
2748 return;
2749
2750 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002751 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002752 return;
2753
2754 /* BR/EDR must use Secure Connections for SMP */
2755 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002756 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002757 return;
2758
2759 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002760 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002761 return;
2762
2763 /* Don't bother if remote LE support is not enabled */
2764 if (!lmp_host_le_capable(hcon))
2765 return;
2766
2767 /* Remote must support SMP fixed chan for BR/EDR */
2768 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2769 return;
2770
2771 /* Don't bother if SMP is already ongoing */
2772 if (chan->data)
2773 return;
2774
2775 smp = smp_chan_create(conn);
2776 if (!smp) {
2777 BT_ERR("%s unable to create SMP context for BR/EDR",
2778 hdev->name);
2779 return;
2780 }
2781
2782 set_bit(SMP_FLAG_SC, &smp->flags);
2783
2784 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2785
2786 /* Prepare and send the BR/EDR SMP Pairing Request */
2787 build_bredr_pairing_cmd(smp, &req, NULL);
2788
2789 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2790 memcpy(&smp->preq[1], &req, sizeof(req));
2791
2792 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2793 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2794}
2795
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002796static void smp_resume_cb(struct l2cap_chan *chan)
2797{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002798 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002799 struct l2cap_conn *conn = chan->conn;
2800 struct hci_conn *hcon = conn->hcon;
2801
2802 BT_DBG("chan %p", chan);
2803
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002804 if (hcon->type == ACL_LINK) {
2805 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002806 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002807 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002808
Johan Hedberg86d14072014-08-11 22:06:43 +03002809 if (!smp)
2810 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002811
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002812 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2813 return;
2814
Johan Hedberg86d14072014-08-11 22:06:43 +03002815 cancel_delayed_work(&smp->security_timer);
2816
Johan Hedbergd6268e82014-09-05 22:19:51 +03002817 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002818}
2819
Johan Hedberg70db83c2014-08-08 09:37:16 +03002820static void smp_ready_cb(struct l2cap_chan *chan)
2821{
2822 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002823 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002824
2825 BT_DBG("chan %p", chan);
2826
2827 conn->smp = chan;
2828 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002829
2830 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2831 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002832}
2833
Johan Hedberg4befb862014-08-11 22:06:38 +03002834static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2835{
2836 int err;
2837
2838 BT_DBG("chan %p", chan);
2839
2840 err = smp_sig_channel(chan, skb);
2841 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002842 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002843
Johan Hedbergb68fda62014-08-11 22:06:40 +03002844 if (smp)
2845 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002846
Johan Hedberg1e91c292014-08-18 20:33:29 +03002847 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002848 }
2849
2850 return err;
2851}
2852
Johan Hedberg70db83c2014-08-08 09:37:16 +03002853static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2854 unsigned long hdr_len,
2855 unsigned long len, int nb)
2856{
2857 struct sk_buff *skb;
2858
2859 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2860 if (!skb)
2861 return ERR_PTR(-ENOMEM);
2862
2863 skb->priority = HCI_PRIO_MAX;
2864 bt_cb(skb)->chan = chan;
2865
2866 return skb;
2867}
2868
2869static const struct l2cap_ops smp_chan_ops = {
2870 .name = "Security Manager",
2871 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002872 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002873 .alloc_skb = smp_alloc_skb_cb,
2874 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002875 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002876
2877 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002878 .state_change = l2cap_chan_no_state_change,
2879 .close = l2cap_chan_no_close,
2880 .defer = l2cap_chan_no_defer,
2881 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002882 .set_shutdown = l2cap_chan_no_set_shutdown,
2883 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002884};
2885
2886static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2887{
2888 struct l2cap_chan *chan;
2889
2890 BT_DBG("pchan %p", pchan);
2891
2892 chan = l2cap_chan_create();
2893 if (!chan)
2894 return NULL;
2895
2896 chan->chan_type = pchan->chan_type;
2897 chan->ops = &smp_chan_ops;
2898 chan->scid = pchan->scid;
2899 chan->dcid = chan->scid;
2900 chan->imtu = pchan->imtu;
2901 chan->omtu = pchan->omtu;
2902 chan->mode = pchan->mode;
2903
Johan Hedbergabe84902014-11-12 22:22:21 +02002904 /* Other L2CAP channels may request SMP routines in order to
2905 * change the security level. This means that the SMP channel
2906 * lock must be considered in its own category to avoid lockdep
2907 * warnings.
2908 */
2909 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2910
Johan Hedberg70db83c2014-08-08 09:37:16 +03002911 BT_DBG("created chan %p", chan);
2912
2913 return chan;
2914}
2915
2916static const struct l2cap_ops smp_root_chan_ops = {
2917 .name = "Security Manager Root",
2918 .new_connection = smp_new_conn_cb,
2919
2920 /* None of these are implemented for the root channel */
2921 .close = l2cap_chan_no_close,
2922 .alloc_skb = l2cap_chan_no_alloc_skb,
2923 .recv = l2cap_chan_no_recv,
2924 .state_change = l2cap_chan_no_state_change,
2925 .teardown = l2cap_chan_no_teardown,
2926 .ready = l2cap_chan_no_ready,
2927 .defer = l2cap_chan_no_defer,
2928 .suspend = l2cap_chan_no_suspend,
2929 .resume = l2cap_chan_no_resume,
2930 .set_shutdown = l2cap_chan_no_set_shutdown,
2931 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002932};
2933
Johan Hedbergef8efe42014-08-13 15:12:32 +03002934static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002935{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002936 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002937 struct smp_dev *smp;
2938 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002939
Johan Hedbergef8efe42014-08-13 15:12:32 +03002940 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002941 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03002942 goto create_chan;
2943 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03002944
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002945 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
2946 if (!smp)
2947 return ERR_PTR(-ENOMEM);
2948
2949 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002950 if (IS_ERR(tfm_aes)) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002951 BT_ERR("Unable to create ECB crypto context");
2952 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08002953 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002954 }
2955
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002956 smp->tfm_aes = tfm_aes;
2957
Johan Hedbergef8efe42014-08-13 15:12:32 +03002958create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03002959 chan = l2cap_chan_create();
2960 if (!chan) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002961 crypto_free_blkcipher(smp->tfm_aes);
2962 kzfree(smp);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002963 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002964 }
2965
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002966 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002967
Johan Hedbergef8efe42014-08-13 15:12:32 +03002968 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002969
2970 l2cap_chan_set_defaults(chan);
2971
Marcel Holtmann157029b2015-01-14 15:43:09 -08002972 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02002973 u8 bdaddr_type;
2974
2975 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
2976
2977 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08002978 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02002979 else
2980 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08002981 } else {
2982 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002983 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08002984 }
2985
Johan Hedberg70db83c2014-08-08 09:37:16 +03002986 chan->state = BT_LISTEN;
2987 chan->mode = L2CAP_MODE_BASIC;
2988 chan->imtu = L2CAP_DEFAULT_MTU;
2989 chan->ops = &smp_root_chan_ops;
2990
Johan Hedbergabe84902014-11-12 22:22:21 +02002991 /* Set correct nesting level for a parent/listening channel */
2992 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2993
Johan Hedbergef8efe42014-08-13 15:12:32 +03002994 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03002995}
2996
Johan Hedbergef8efe42014-08-13 15:12:32 +03002997static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002998{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07002999 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003000
Johan Hedbergef8efe42014-08-13 15:12:32 +03003001 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003002
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003003 smp = chan->data;
3004 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003005 chan->data = NULL;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003006 if (smp->tfm_aes)
3007 crypto_free_blkcipher(smp->tfm_aes);
3008 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003009 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003010
Johan Hedberg70db83c2014-08-08 09:37:16 +03003011 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003012}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003013
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003014static ssize_t force_bredr_smp_read(struct file *file,
3015 char __user *user_buf,
3016 size_t count, loff_t *ppos)
3017{
3018 struct hci_dev *hdev = file->private_data;
3019 char buf[3];
3020
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003021 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003022 buf[1] = '\n';
3023 buf[2] = '\0';
3024 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3025}
3026
3027static ssize_t force_bredr_smp_write(struct file *file,
3028 const char __user *user_buf,
3029 size_t count, loff_t *ppos)
3030{
3031 struct hci_dev *hdev = file->private_data;
3032 char buf[32];
3033 size_t buf_size = min(count, (sizeof(buf)-1));
3034 bool enable;
3035
3036 if (copy_from_user(buf, user_buf, buf_size))
3037 return -EFAULT;
3038
3039 buf[buf_size] = '\0';
3040 if (strtobool(buf, &enable))
3041 return -EINVAL;
3042
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003043 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003044 return -EALREADY;
3045
3046 if (enable) {
3047 struct l2cap_chan *chan;
3048
3049 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3050 if (IS_ERR(chan))
3051 return PTR_ERR(chan);
3052
3053 hdev->smp_bredr_data = chan;
3054 } else {
3055 struct l2cap_chan *chan;
3056
3057 chan = hdev->smp_bredr_data;
3058 hdev->smp_bredr_data = NULL;
3059 smp_del_chan(chan);
3060 }
3061
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003062 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003063
3064 return count;
3065}
3066
3067static const struct file_operations force_bredr_smp_fops = {
3068 .open = simple_open,
3069 .read = force_bredr_smp_read,
3070 .write = force_bredr_smp_write,
3071 .llseek = default_llseek,
3072};
3073
Johan Hedbergef8efe42014-08-13 15:12:32 +03003074int smp_register(struct hci_dev *hdev)
3075{
3076 struct l2cap_chan *chan;
3077
3078 BT_DBG("%s", hdev->name);
3079
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003080 /* If the controller does not support Low Energy operation, then
3081 * there is also no need to register any SMP channel.
3082 */
3083 if (!lmp_le_capable(hdev))
3084 return 0;
3085
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003086 if (WARN_ON(hdev->smp_data)) {
3087 chan = hdev->smp_data;
3088 hdev->smp_data = NULL;
3089 smp_del_chan(chan);
3090 }
3091
Johan Hedbergef8efe42014-08-13 15:12:32 +03003092 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3093 if (IS_ERR(chan))
3094 return PTR_ERR(chan);
3095
3096 hdev->smp_data = chan;
3097
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003098 /* If the controller does not support BR/EDR Secure Connections
3099 * feature, then the BR/EDR SMP channel shall not be present.
3100 *
3101 * To test this with Bluetooth 4.0 controllers, create a debugfs
3102 * switch that allows forcing BR/EDR SMP support and accepting
3103 * cross-transport pairing on non-AES encrypted connections.
3104 */
3105 if (!lmp_sc_capable(hdev)) {
3106 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3107 hdev, &force_bredr_smp_fops);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003108 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003109 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003110
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003111 if (WARN_ON(hdev->smp_bredr_data)) {
3112 chan = hdev->smp_bredr_data;
3113 hdev->smp_bredr_data = NULL;
3114 smp_del_chan(chan);
3115 }
3116
Johan Hedbergef8efe42014-08-13 15:12:32 +03003117 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3118 if (IS_ERR(chan)) {
3119 int err = PTR_ERR(chan);
3120 chan = hdev->smp_data;
3121 hdev->smp_data = NULL;
3122 smp_del_chan(chan);
3123 return err;
3124 }
3125
3126 hdev->smp_bredr_data = chan;
3127
3128 return 0;
3129}
3130
3131void smp_unregister(struct hci_dev *hdev)
3132{
3133 struct l2cap_chan *chan;
3134
3135 if (hdev->smp_bredr_data) {
3136 chan = hdev->smp_bredr_data;
3137 hdev->smp_bredr_data = NULL;
3138 smp_del_chan(chan);
3139 }
3140
3141 if (hdev->smp_data) {
3142 chan = hdev->smp_data;
3143 hdev->smp_data = NULL;
3144 smp_del_chan(chan);
3145 }
3146}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003147
3148#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3149
Johan Hedbergcfc41982014-12-30 09:50:40 +02003150static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3151{
3152 const u8 irk[16] = {
3153 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3154 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3155 const u8 r[3] = { 0x94, 0x81, 0x70 };
3156 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3157 u8 res[3];
3158 int err;
3159
3160 err = smp_ah(tfm_aes, irk, r, res);
3161 if (err)
3162 return err;
3163
3164 if (memcmp(res, exp, 3))
3165 return -EINVAL;
3166
3167 return 0;
3168}
3169
3170static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3171{
3172 const u8 k[16] = {
3173 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3174 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3175 const u8 r[16] = {
3176 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3177 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3178 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3179 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3180 const u8 _iat = 0x01;
3181 const u8 _rat = 0x00;
3182 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3183 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3184 const u8 exp[16] = {
3185 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3186 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3187 u8 res[16];
3188 int err;
3189
3190 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3191 if (err)
3192 return err;
3193
3194 if (memcmp(res, exp, 16))
3195 return -EINVAL;
3196
3197 return 0;
3198}
3199
3200static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3201{
3202 const u8 k[16] = {
3203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3204 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3205 const u8 r1[16] = {
3206 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3207 const u8 r2[16] = {
3208 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3209 const u8 exp[16] = {
3210 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3211 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3212 u8 res[16];
3213 int err;
3214
3215 err = smp_s1(tfm_aes, k, r1, r2, res);
3216 if (err)
3217 return err;
3218
3219 if (memcmp(res, exp, 16))
3220 return -EINVAL;
3221
3222 return 0;
3223}
3224
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003225static int __init test_f4(struct crypto_hash *tfm_cmac)
3226{
3227 const u8 u[32] = {
3228 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3229 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3230 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3231 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3232 const u8 v[32] = {
3233 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3234 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3235 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3236 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3237 const u8 x[16] = {
3238 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3239 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3240 const u8 z = 0x00;
3241 const u8 exp[16] = {
3242 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3243 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3244 u8 res[16];
3245 int err;
3246
3247 err = smp_f4(tfm_cmac, u, v, x, z, res);
3248 if (err)
3249 return err;
3250
3251 if (memcmp(res, exp, 16))
3252 return -EINVAL;
3253
3254 return 0;
3255}
3256
3257static int __init test_f5(struct crypto_hash *tfm_cmac)
3258{
3259 const u8 w[32] = {
3260 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3261 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3262 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3263 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3264 const u8 n1[16] = {
3265 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3266 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3267 const u8 n2[16] = {
3268 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3269 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3270 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3271 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3272 const u8 exp_ltk[16] = {
3273 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3274 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3275 const u8 exp_mackey[16] = {
3276 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3277 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3278 u8 mackey[16], ltk[16];
3279 int err;
3280
3281 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3282 if (err)
3283 return err;
3284
3285 if (memcmp(mackey, exp_mackey, 16))
3286 return -EINVAL;
3287
3288 if (memcmp(ltk, exp_ltk, 16))
3289 return -EINVAL;
3290
3291 return 0;
3292}
3293
3294static int __init test_f6(struct crypto_hash *tfm_cmac)
3295{
3296 const u8 w[16] = {
3297 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3298 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3299 const u8 n1[16] = {
3300 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3301 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3302 const u8 n2[16] = {
3303 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3304 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3305 const u8 r[16] = {
3306 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3307 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3308 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3309 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3310 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3311 const u8 exp[16] = {
3312 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3313 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3314 u8 res[16];
3315 int err;
3316
3317 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3318 if (err)
3319 return err;
3320
3321 if (memcmp(res, exp, 16))
3322 return -EINVAL;
3323
3324 return 0;
3325}
3326
3327static int __init test_g2(struct crypto_hash *tfm_cmac)
3328{
3329 const u8 u[32] = {
3330 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3331 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3332 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3333 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3334 const u8 v[32] = {
3335 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3336 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3337 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3338 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3339 const u8 x[16] = {
3340 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3341 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3342 const u8 y[16] = {
3343 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3344 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3345 const u32 exp_val = 0x2f9ed5ba % 1000000;
3346 u32 val;
3347 int err;
3348
3349 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3350 if (err)
3351 return err;
3352
3353 if (val != exp_val)
3354 return -EINVAL;
3355
3356 return 0;
3357}
3358
3359static int __init test_h6(struct crypto_hash *tfm_cmac)
3360{
3361 const u8 w[16] = {
3362 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3363 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3364 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3365 const u8 exp[16] = {
3366 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3367 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3368 u8 res[16];
3369 int err;
3370
3371 err = smp_h6(tfm_cmac, w, key_id, res);
3372 if (err)
3373 return err;
3374
3375 if (memcmp(res, exp, 16))
3376 return -EINVAL;
3377
3378 return 0;
3379}
3380
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003381static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3382 struct crypto_hash *tfm_cmac)
3383{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003384 ktime_t calltime, delta, rettime;
3385 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003386 int err;
3387
Marcel Holtmann255047b2014-12-30 00:11:20 -08003388 calltime = ktime_get();
3389
Johan Hedbergcfc41982014-12-30 09:50:40 +02003390 err = test_ah(tfm_aes);
3391 if (err) {
3392 BT_ERR("smp_ah test failed");
3393 return err;
3394 }
3395
3396 err = test_c1(tfm_aes);
3397 if (err) {
3398 BT_ERR("smp_c1 test failed");
3399 return err;
3400 }
3401
3402 err = test_s1(tfm_aes);
3403 if (err) {
3404 BT_ERR("smp_s1 test failed");
3405 return err;
3406 }
3407
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003408 err = test_f4(tfm_cmac);
3409 if (err) {
3410 BT_ERR("smp_f4 test failed");
3411 return err;
3412 }
3413
3414 err = test_f5(tfm_cmac);
3415 if (err) {
3416 BT_ERR("smp_f5 test failed");
3417 return err;
3418 }
3419
3420 err = test_f6(tfm_cmac);
3421 if (err) {
3422 BT_ERR("smp_f6 test failed");
3423 return err;
3424 }
3425
3426 err = test_g2(tfm_cmac);
3427 if (err) {
3428 BT_ERR("smp_g2 test failed");
3429 return err;
3430 }
3431
3432 err = test_h6(tfm_cmac);
3433 if (err) {
3434 BT_ERR("smp_h6 test failed");
3435 return err;
3436 }
3437
Marcel Holtmann255047b2014-12-30 00:11:20 -08003438 rettime = ktime_get();
3439 delta = ktime_sub(rettime, calltime);
3440 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3441
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003442 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003443
3444 return 0;
3445}
3446
3447int __init bt_selftest_smp(void)
3448{
3449 struct crypto_blkcipher *tfm_aes;
3450 struct crypto_hash *tfm_cmac;
3451 int err;
3452
3453 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3454 if (IS_ERR(tfm_aes)) {
3455 BT_ERR("Unable to create ECB crypto context");
3456 return PTR_ERR(tfm_aes);
3457 }
3458
3459 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3460 if (IS_ERR(tfm_cmac)) {
3461 BT_ERR("Unable to create CMAC crypto context");
3462 crypto_free_blkcipher(tfm_aes);
3463 return PTR_ERR(tfm_cmac);
3464 }
3465
3466 err = run_selftests(tfm_aes, tfm_cmac);
3467
3468 crypto_free_hash(tfm_cmac);
3469 crypto_free_blkcipher(tfm_aes);
3470
3471 return err;
3472}
3473
3474#endif