blob: 73643a6ca3cf120b87b76f2c77850af3102e4e91 [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
Johan Hedberg0edb14d2014-05-26 13:29:28 +030055#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
56 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
76struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030077 struct l2cap_conn *conn;
78 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030079 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030080
Johan Hedberg4bc58f52014-05-20 09:45:47 +030081 u8 preq[7]; /* SMP Pairing Request */
82 u8 prsp[7]; /* SMP Pairing Response */
83 u8 prnd[16]; /* SMP Pairing Random (local) */
84 u8 rrnd[16]; /* SMP Pairing Random (remote) */
85 u8 pcnf[16]; /* SMP Pairing Confirm */
86 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberga29b0732014-10-28 15:17:05 +010087 u8 rr[16];
Johan Hedberg4bc58f52014-05-20 09:45:47 +030088 u8 enc_key_size;
89 u8 remote_key_dist;
90 bdaddr_t id_addr;
91 u8 id_addr_type;
92 u8 irk[16];
93 struct smp_csrk *csrk;
94 struct smp_csrk *slave_csrk;
95 struct smp_ltk *ltk;
96 struct smp_ltk *slave_ltk;
97 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +030098 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +030099 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300100 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300101 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300102
Johan Hedberg3b191462014-06-06 10:50:15 +0300103 /* Secure Connections variables */
104 u8 local_pk[64];
105 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300106 u8 remote_pk[64];
107 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300108 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300109
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300110 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300111 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300112};
113
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300114/* These debug key values are defined in the SMP section of the core
115 * specification. debug_pk is the public debug key and debug_sk the
116 * private debug key.
117 */
118static const u8 debug_pk[64] = {
119 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
120 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
121 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
122 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
123
124 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
125 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
126 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
127 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
128};
129
130static const u8 debug_sk[32] = {
131 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
132 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
133 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
134 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
135};
136
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300137static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300138{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300139 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300140
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300141 for (i = 0; i < len; i++)
142 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300143}
144
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200145/* The following functions map to the LE SC SMP crypto functions
146 * AES-CMAC, f4, f5, f6, g2 and h6.
147 */
148
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300149static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
150 size_t len, u8 mac[16])
151{
152 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
153 struct hash_desc desc;
154 struct scatterlist sg;
155 int err;
156
157 if (len > CMAC_MSG_MAX)
158 return -EFBIG;
159
160 if (!tfm) {
161 BT_ERR("tfm %p", tfm);
162 return -EINVAL;
163 }
164
165 desc.tfm = tfm;
166 desc.flags = 0;
167
168 crypto_hash_init(&desc);
169
170 /* Swap key and message from LSB to MSB */
171 swap_buf(k, tmp, 16);
172 swap_buf(m, msg_msb, len);
173
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200174 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
175 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300176
177 err = crypto_hash_setkey(tfm, tmp, 16);
178 if (err) {
179 BT_ERR("cipher setkey failed: %d", err);
180 return err;
181 }
182
183 sg_init_one(&sg, msg_msb, len);
184
185 err = crypto_hash_update(&desc, &sg, len);
186 if (err) {
187 BT_ERR("Hash update error %d", err);
188 return err;
189 }
190
191 err = crypto_hash_final(&desc, mac_msb);
192 if (err) {
193 BT_ERR("Hash final error %d", err);
194 return err;
195 }
196
197 swap_buf(mac_msb, mac, 16);
198
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200199 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300200
201 return 0;
202}
203
204static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
205 const u8 x[16], u8 z, u8 res[16])
206{
207 u8 m[65];
208 int err;
209
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200210 SMP_DBG("u %32phN", u);
211 SMP_DBG("v %32phN", v);
212 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300213
214 m[0] = z;
215 memcpy(m + 1, v, 32);
216 memcpy(m + 33, u, 32);
217
218 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
219 if (err)
220 return err;
221
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200222 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300223
224 return err;
225}
226
Johan Hedberg4da50de2014-12-29 12:04:10 +0200227static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32],
228 const u8 n1[16], const u8 n2[16], const u8 a1[7],
229 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300230{
231 /* The btle, salt and length "magic" values are as defined in
232 * the SMP section of the Bluetooth core specification. In ASCII
233 * the btle value ends up being 'btle'. The salt is just a
234 * random number whereas length is the value 256 in little
235 * endian format.
236 */
237 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
238 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
239 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
240 const u8 length[2] = { 0x00, 0x01 };
241 u8 m[53], t[16];
242 int err;
243
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200244 SMP_DBG("w %32phN", w);
245 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
246 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300247
248 err = aes_cmac(tfm_cmac, salt, w, 32, t);
249 if (err)
250 return err;
251
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200252 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300253
254 memcpy(m, length, 2);
255 memcpy(m + 2, a2, 7);
256 memcpy(m + 9, a1, 7);
257 memcpy(m + 16, n2, 16);
258 memcpy(m + 32, n1, 16);
259 memcpy(m + 48, btle, 4);
260
261 m[52] = 0; /* Counter */
262
263 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
264 if (err)
265 return err;
266
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200267 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300268
269 m[52] = 1; /* Counter */
270
271 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
272 if (err)
273 return err;
274
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200275 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300276
277 return 0;
278}
279
280static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200281 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300282 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
283 u8 res[16])
284{
285 u8 m[65];
286 int err;
287
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200288 SMP_DBG("w %16phN", w);
289 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
290 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300291
292 memcpy(m, a2, 7);
293 memcpy(m + 7, a1, 7);
294 memcpy(m + 14, io_cap, 3);
295 memcpy(m + 17, r, 16);
296 memcpy(m + 33, n2, 16);
297 memcpy(m + 49, n1, 16);
298
299 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
300 if (err)
301 return err;
302
303 BT_DBG("res %16phN", res);
304
305 return err;
306}
307
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300308static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
309 const u8 x[16], const u8 y[16], u32 *val)
310{
311 u8 m[80], tmp[16];
312 int err;
313
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200314 SMP_DBG("u %32phN", u);
315 SMP_DBG("v %32phN", v);
316 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300317
318 memcpy(m, y, 16);
319 memcpy(m + 16, v, 32);
320 memcpy(m + 48, u, 32);
321
322 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
323 if (err)
324 return err;
325
326 *val = get_unaligned_le32(tmp);
327 *val %= 1000000;
328
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200329 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300330
331 return 0;
332}
333
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200334static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
335 const u8 key_id[4], u8 res[16])
336{
337 int err;
338
339 SMP_DBG("w %16phN key_id %4phN", w, key_id);
340
341 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
342 if (err)
343 return err;
344
345 SMP_DBG("res %16phN", res);
346
347 return err;
348}
349
350/* The following functions map to the legacy SMP crypto functions e, c1,
351 * s1 and ah.
352 */
353
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300354static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
355{
356 struct blkcipher_desc desc;
357 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200358 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200359 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300360
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200361 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300362 BT_ERR("tfm %p", tfm);
363 return -EINVAL;
364 }
365
366 desc.tfm = tfm;
367 desc.flags = 0;
368
Johan Hedberg943a7322014-03-18 12:58:24 +0200369 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300370 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200371
372 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300373 if (err) {
374 BT_ERR("cipher setkey failed: %d", err);
375 return err;
376 }
377
Johan Hedberg943a7322014-03-18 12:58:24 +0200378 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300379 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200380
381 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300382
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300383 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
384 if (err)
385 BT_ERR("Encrypt data error %d", err);
386
Johan Hedberg943a7322014-03-18 12:58:24 +0200387 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300388 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200389
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300390 return err;
391}
392
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200393static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
394 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
395 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
396{
397 u8 p1[16], p2[16];
398 int err;
399
400 memset(p1, 0, 16);
401
402 /* p1 = pres || preq || _rat || _iat */
403 p1[0] = _iat;
404 p1[1] = _rat;
405 memcpy(p1 + 2, preq, 7);
406 memcpy(p1 + 9, pres, 7);
407
408 /* p2 = padding || ia || ra */
409 memcpy(p2, ra, 6);
410 memcpy(p2 + 6, ia, 6);
411 memset(p2 + 12, 0, 4);
412
413 /* res = r XOR p1 */
414 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
415
416 /* res = e(k, res) */
417 err = smp_e(tfm_aes, k, res);
418 if (err) {
419 BT_ERR("Encrypt data error");
420 return err;
421 }
422
423 /* res = res XOR p2 */
424 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
425
426 /* res = e(k, res) */
427 err = smp_e(tfm_aes, k, res);
428 if (err)
429 BT_ERR("Encrypt data error");
430
431 return err;
432}
433
434static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
435 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300436{
437 int err;
438
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200439 /* Just least significant octets from r1 and r2 are considered */
440 memcpy(_r, r2, 8);
441 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300442
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200443 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300444 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200445 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300446
447 return err;
448}
449
Johan Hedbergcd082792014-12-02 13:37:41 +0200450static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
451 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200452{
Johan Hedberg943a7322014-03-18 12:58:24 +0200453 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200454 int err;
455
456 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200457 memcpy(_res, r, 3);
458 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200459
Johan Hedberg943a7322014-03-18 12:58:24 +0200460 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200461 if (err) {
462 BT_ERR("Encrypt error");
463 return err;
464 }
465
466 /* The output of the random address function ah is:
467 * ah(h, r) = e(k, r') mod 2^24
468 * The output of the security function e is then truncated to 24 bits
469 * by taking the least significant 24 bits of the output of e as the
470 * result of ah.
471 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200472 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200473
474 return 0;
475}
476
Johan Hedbergcd082792014-12-02 13:37:41 +0200477bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
478 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200479{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300480 struct l2cap_chan *chan = hdev->smp_data;
481 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200482 u8 hash[3];
483 int err;
484
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300485 if (!chan || !chan->data)
486 return false;
487
488 tfm = chan->data;
489
Johan Hedberg60478052014-02-18 10:19:31 +0200490 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
491
492 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
493 if (err)
494 return false;
495
496 return !memcmp(bdaddr->b, hash, 3);
497}
498
Johan Hedbergcd082792014-12-02 13:37:41 +0200499int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200500{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300501 struct l2cap_chan *chan = hdev->smp_data;
502 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200503 int err;
504
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300505 if (!chan || !chan->data)
506 return -EOPNOTSUPP;
507
508 tfm = chan->data;
509
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200510 get_random_bytes(&rpa->b[3], 3);
511
512 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
513 rpa->b[5] |= 0x40; /* Set second most significant bit */
514
515 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
516 if (err < 0)
517 return err;
518
519 BT_DBG("RPA %pMR", rpa);
520
521 return 0;
522}
523
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300524static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
525{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300526 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300527 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300528 struct kvec iv[2];
529 struct msghdr msg;
530
531 if (!chan)
532 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300533
534 BT_DBG("code 0x%2.2x", code);
535
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300536 iv[0].iov_base = &code;
537 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300538
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300539 iv[1].iov_base = data;
540 iv[1].iov_len = len;
541
542 memset(&msg, 0, sizeof(msg));
543
Al Viro17836392014-11-24 17:07:38 -0500544 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300545
546 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300547
Johan Hedbergb68fda62014-08-11 22:06:40 +0300548 if (!chan->data)
549 return;
550
551 smp = chan->data;
552
553 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300554 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300555}
556
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300557static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800558{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300559 if (authreq & SMP_AUTH_MITM) {
560 if (authreq & SMP_AUTH_SC)
561 return BT_SECURITY_FIPS;
562 else
563 return BT_SECURITY_HIGH;
564 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800565 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300566 }
Brian Gix2b64d152011-12-21 16:12:12 -0800567}
568
569static __u8 seclevel_to_authreq(__u8 sec_level)
570{
571 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300572 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800573 case BT_SECURITY_HIGH:
574 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
575 case BT_SECURITY_MEDIUM:
576 return SMP_AUTH_BONDING;
577 default:
578 return SMP_AUTH_NONE;
579 }
580}
581
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300582static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700583 struct smp_cmd_pairing *req,
584 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300585{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300586 struct l2cap_chan *chan = conn->smp;
587 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200588 struct hci_conn *hcon = conn->hcon;
589 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100590 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300591
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300592 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700593 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
594 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300595 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800596 } else {
597 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300598 }
599
Johan Hedbergfd349c02014-02-18 10:19:36 +0200600 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
601 remote_dist |= SMP_DIST_ID_KEY;
602
Johan Hedberg863efaf2014-02-22 19:06:32 +0200603 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
604 local_dist |= SMP_DIST_ID_KEY;
605
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100606 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
607 (authreq & SMP_AUTH_SC)) {
608 struct oob_data *oob_data;
609 u8 bdaddr_type;
610
611 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300612 local_dist |= SMP_DIST_LINK_KEY;
613 remote_dist |= SMP_DIST_LINK_KEY;
614 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100615
616 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
617 bdaddr_type = BDADDR_LE_PUBLIC;
618 else
619 bdaddr_type = BDADDR_LE_RANDOM;
620
621 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
622 bdaddr_type);
623 if (oob_data) {
624 set_bit(SMP_FLAG_OOB, &smp->flags);
625 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100626 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100627 memcpy(smp->pcnf, oob_data->hash256, 16);
628 }
629
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300630 } else {
631 authreq &= ~SMP_AUTH_SC;
632 }
633
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300634 if (rsp == NULL) {
635 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100636 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300637 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200638 req->init_key_dist = local_dist;
639 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300640 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200641
642 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300643 return;
644 }
645
646 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100647 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300648 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200649 rsp->init_key_dist = req->init_key_dist & remote_dist;
650 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300651 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200652
653 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300654}
655
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300656static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
657{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300658 struct l2cap_chan *chan = conn->smp;
659 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300660
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300661 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700662 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300663 return SMP_ENC_KEY_SIZE;
664
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300665 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300666
667 return 0;
668}
669
Johan Hedberg6f48e262014-08-11 22:06:44 +0300670static void smp_chan_destroy(struct l2cap_conn *conn)
671{
672 struct l2cap_chan *chan = conn->smp;
673 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200674 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300675 bool complete;
676
677 BUG_ON(!smp);
678
679 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300680
Johan Hedberg6f48e262014-08-11 22:06:44 +0300681 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200682 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300683
684 kfree(smp->csrk);
685 kfree(smp->slave_csrk);
Johan Hedberg6a770832014-06-06 11:54:04 +0300686 kfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300687
688 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300689 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300690
Johan Hedberg923e2412014-12-03 12:43:39 +0200691 /* Ensure that we don't leave any debug key around if debug key
692 * support hasn't been explicitly enabled.
693 */
694 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
695 !test_bit(HCI_KEEP_DEBUG_KEYS, &hcon->hdev->dev_flags)) {
696 list_del_rcu(&smp->ltk->list);
697 kfree_rcu(smp->ltk, rcu);
698 smp->ltk = NULL;
699 }
700
Johan Hedberg6f48e262014-08-11 22:06:44 +0300701 /* If pairing failed clean up any keys we might have */
702 if (!complete) {
703 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200704 list_del_rcu(&smp->ltk->list);
705 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300706 }
707
708 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200709 list_del_rcu(&smp->slave_ltk->list);
710 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300711 }
712
713 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200714 list_del_rcu(&smp->remote_irk->list);
715 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300716 }
717 }
718
719 chan->data = NULL;
720 kfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200721 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300722}
723
Johan Hedberg84794e12013-11-06 11:24:57 +0200724static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800725{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200726 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300727 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200728
Johan Hedberg84794e12013-11-06 11:24:57 +0200729 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800730 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700731 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800732
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700733 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700734 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300735
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300736 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300737 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800738}
739
Brian Gix2b64d152011-12-21 16:12:12 -0800740#define JUST_WORKS 0x00
741#define JUST_CFM 0x01
742#define REQ_PASSKEY 0x02
743#define CFM_PASSKEY 0x03
744#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300745#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800746#define OVERLAP 0xFF
747
748static const u8 gen_method[5][5] = {
749 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
750 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
751 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
752 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
753 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
754};
755
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300756static const u8 sc_method[5][5] = {
757 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
758 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
759 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
760 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
761 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
762};
763
Johan Hedberg581370c2014-06-17 13:07:38 +0300764static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
765{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300766 /* If either side has unknown io_caps, use JUST_CFM (which gets
767 * converted later to JUST_WORKS if we're initiators.
768 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300769 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
770 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300771 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300772
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300773 if (test_bit(SMP_FLAG_SC, &smp->flags))
774 return sc_method[remote_io][local_io];
775
Johan Hedberg581370c2014-06-17 13:07:38 +0300776 return gen_method[remote_io][local_io];
777}
778
Brian Gix2b64d152011-12-21 16:12:12 -0800779static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
780 u8 local_io, u8 remote_io)
781{
782 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300783 struct l2cap_chan *chan = conn->smp;
784 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800785 u32 passkey = 0;
786 int ret = 0;
787
788 /* Initialize key for JUST WORKS */
789 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300790 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800791
792 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
793
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300794 /* If neither side wants MITM, either "just" confirm an incoming
795 * request or use just-works for outgoing ones. The JUST_CFM
796 * will be converted to JUST_WORKS if necessary later in this
797 * function. If either side has MITM look up the method from the
798 * table.
799 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300800 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300801 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800802 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300803 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800804
Johan Hedberga82505c2014-03-24 14:39:07 +0200805 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300806 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
807 &smp->flags))
808 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200809
Johan Hedberg02f3e252014-07-16 15:09:13 +0300810 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300811 if (smp->method == JUST_CFM &&
812 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
813 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300814
Brian Gix2b64d152011-12-21 16:12:12 -0800815 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300816 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300817 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800818 return 0;
819 }
820
821 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300822 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300823 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300824 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
825 hcon->pending_sec_level = BT_SECURITY_HIGH;
826 }
Brian Gix2b64d152011-12-21 16:12:12 -0800827
828 /* If both devices have Keyoard-Display I/O, the master
829 * Confirms and the slave Enters the passkey.
830 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300831 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300832 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300833 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800834 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300835 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800836 }
837
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200838 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300839 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200840 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800841 get_random_bytes(&passkey, sizeof(passkey));
842 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200843 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800844 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300845 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800846 }
847
Johan Hedberg783e0572014-05-31 18:48:26 +0300848 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700849 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200850 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300851 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200852 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
853 hcon->type, hcon->dst_type,
854 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800855 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200856 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200857 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200858 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800859
Brian Gix2b64d152011-12-21 16:12:12 -0800860 return ret;
861}
862
Johan Hedberg1cc61142014-05-20 09:45:52 +0300863static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300864{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300865 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300866 struct smp_cmd_pairing_confirm cp;
867 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300868
869 BT_DBG("conn %p", conn);
870
Johan Hedberge491eaf2014-10-25 21:15:37 +0200871 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200872 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200873 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
874 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300875 if (ret)
876 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300877
Johan Hedberg4a74d652014-05-20 09:45:50 +0300878 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800879
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300880 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
881
Johan Hedbergb28b4942014-09-05 22:19:55 +0300882 if (conn->hcon->out)
883 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
884 else
885 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
886
Johan Hedberg1cc61142014-05-20 09:45:52 +0300887 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300888}
889
Johan Hedberg861580a2014-05-20 09:45:51 +0300890static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300891{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300892 struct l2cap_conn *conn = smp->conn;
893 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300894 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300895 int ret;
896
Johan Hedbergec70f362014-06-27 14:23:04 +0300897 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300898 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300899
900 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
901
Johan Hedberge491eaf2014-10-25 21:15:37 +0200902 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200903 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200904 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300905 if (ret)
906 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300907
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300908 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
909 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300910 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300911 }
912
913 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800914 u8 stk[16];
915 __le64 rand = 0;
916 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300917
Johan Hedberge491eaf2014-10-25 21:15:37 +0200918 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300919
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300920 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300921 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300922
Johan Hedberg861580a2014-05-20 09:45:51 +0300923 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
924 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300925
926 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300927 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300928 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300929 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300930 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800931 __le64 rand = 0;
932 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300933
Johan Hedberg943a7322014-03-18 12:58:24 +0200934 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
935 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300936
Johan Hedberge491eaf2014-10-25 21:15:37 +0200937 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300938
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300939 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700940 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300941
Johan Hedbergfff34902014-06-10 15:19:50 +0300942 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
943 auth = 1;
944 else
945 auth = 0;
946
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300947 /* Even though there's no _SLAVE suffix this is the
948 * slave STK we're adding for later lookup (the master
949 * STK never needs to be stored).
950 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700951 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300952 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300953 }
954
Johan Hedberg861580a2014-05-20 09:45:51 +0300955 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300956}
957
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300958static void smp_notify_keys(struct l2cap_conn *conn)
959{
960 struct l2cap_chan *chan = conn->smp;
961 struct smp_chan *smp = chan->data;
962 struct hci_conn *hcon = conn->hcon;
963 struct hci_dev *hdev = hcon->hdev;
964 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
965 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
966 bool persistent;
967
968 if (smp->remote_irk) {
969 mgmt_new_irk(hdev, smp->remote_irk);
970 /* Now that user space can be considered to know the
971 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300972 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300973 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300974 if (hcon->type == LE_LINK) {
975 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
976 hcon->dst_type = smp->remote_irk->addr_type;
977 queue_work(hdev->workqueue, &conn->id_addr_update_work);
978 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300979
980 /* When receiving an indentity resolving key for
981 * a remote device that does not use a resolvable
982 * private address, just remove the key so that
983 * it is possible to use the controller white
984 * list for scanning.
985 *
986 * Userspace will have been told to not store
987 * this key at this point. So it is safe to
988 * just remove it.
989 */
990 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200991 list_del_rcu(&smp->remote_irk->list);
992 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300993 smp->remote_irk = NULL;
994 }
995 }
996
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300997 if (hcon->type == ACL_LINK) {
998 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
999 persistent = false;
1000 else
1001 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1002 &hcon->flags);
1003 } else {
1004 /* The LTKs and CSRKs should be persistent only if both sides
1005 * had the bonding bit set in their authentication requests.
1006 */
1007 persistent = !!((req->auth_req & rsp->auth_req) &
1008 SMP_AUTH_BONDING);
1009 }
1010
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001011
1012 if (smp->csrk) {
1013 smp->csrk->bdaddr_type = hcon->dst_type;
1014 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1015 mgmt_new_csrk(hdev, smp->csrk, persistent);
1016 }
1017
1018 if (smp->slave_csrk) {
1019 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1020 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1021 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1022 }
1023
1024 if (smp->ltk) {
1025 smp->ltk->bdaddr_type = hcon->dst_type;
1026 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1027 mgmt_new_ltk(hdev, smp->ltk, persistent);
1028 }
1029
1030 if (smp->slave_ltk) {
1031 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1032 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1033 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1034 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001035
1036 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001037 struct link_key *key;
1038 u8 type;
1039
1040 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1041 type = HCI_LK_DEBUG_COMBINATION;
1042 else if (hcon->sec_level == BT_SECURITY_FIPS)
1043 type = HCI_LK_AUTH_COMBINATION_P256;
1044 else
1045 type = HCI_LK_UNAUTH_COMBINATION_P256;
1046
1047 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1048 smp->link_key, type, 0, &persistent);
1049 if (key) {
1050 mgmt_new_link_key(hdev, key, persistent);
1051
1052 /* Don't keep debug keys around if the relevant
1053 * flag is not set.
1054 */
1055 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) &&
1056 key->type == HCI_LK_DEBUG_COMBINATION) {
1057 list_del_rcu(&key->list);
1058 kfree_rcu(key, rcu);
1059 }
1060 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001061 }
1062}
1063
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001064static void sc_add_ltk(struct smp_chan *smp)
1065{
1066 struct hci_conn *hcon = smp->conn->hcon;
1067 u8 key_type, auth;
1068
1069 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1070 key_type = SMP_LTK_P256_DEBUG;
1071 else
1072 key_type = SMP_LTK_P256;
1073
1074 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1075 auth = 1;
1076 else
1077 auth = 0;
1078
1079 memset(smp->tk + smp->enc_key_size, 0,
1080 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1081
1082 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1083 key_type, auth, smp->tk, smp->enc_key_size,
1084 0, 0);
1085}
1086
Johan Hedberg6a770832014-06-06 11:54:04 +03001087static void sc_generate_link_key(struct smp_chan *smp)
1088{
1089 /* These constants are as specified in the core specification.
1090 * In ASCII they spell out to 'tmp1' and 'lebr'.
1091 */
1092 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1093 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1094
1095 smp->link_key = kzalloc(16, GFP_KERNEL);
1096 if (!smp->link_key)
1097 return;
1098
1099 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1100 kfree(smp->link_key);
1101 smp->link_key = NULL;
1102 return;
1103 }
1104
1105 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1106 kfree(smp->link_key);
1107 smp->link_key = NULL;
1108 return;
1109 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001110}
1111
Johan Hedbergb28b4942014-09-05 22:19:55 +03001112static void smp_allow_key_dist(struct smp_chan *smp)
1113{
1114 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1115 * will be allowed in each PDU handler to ensure we receive
1116 * them in the correct order.
1117 */
1118 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1119 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1120 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1121 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1122 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1123 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1124}
1125
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001126static void sc_generate_ltk(struct smp_chan *smp)
1127{
1128 /* These constants are as specified in the core specification.
1129 * In ASCII they spell out to 'tmp2' and 'brle'.
1130 */
1131 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1132 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1133 struct hci_conn *hcon = smp->conn->hcon;
1134 struct hci_dev *hdev = hcon->hdev;
1135 struct link_key *key;
1136
1137 key = hci_find_link_key(hdev, &hcon->dst);
1138 if (!key) {
1139 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1140 return;
1141 }
1142
1143 if (key->type == HCI_LK_DEBUG_COMBINATION)
1144 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1145
1146 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1147 return;
1148
1149 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1150 return;
1151
1152 sc_add_ltk(smp);
1153}
1154
Johan Hedbergd6268e82014-09-05 22:19:51 +03001155static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001156{
1157 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001158 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001159 struct hci_conn *hcon = conn->hcon;
1160 struct hci_dev *hdev = hcon->hdev;
1161 __u8 *keydist;
1162
1163 BT_DBG("conn %p", conn);
1164
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001165 rsp = (void *) &smp->prsp[1];
1166
1167 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001168 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1169 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001170 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001171 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001172
1173 req = (void *) &smp->preq[1];
1174
1175 if (hcon->out) {
1176 keydist = &rsp->init_key_dist;
1177 *keydist &= req->init_key_dist;
1178 } else {
1179 keydist = &rsp->resp_key_dist;
1180 *keydist &= req->resp_key_dist;
1181 }
1182
Johan Hedberg6a770832014-06-06 11:54:04 +03001183 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001184 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001185 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001186 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1187 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001188
1189 /* Clear the keys which are generated but not distributed */
1190 *keydist &= ~SMP_SC_NO_DIST;
1191 }
1192
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001193 BT_DBG("keydist 0x%x", *keydist);
1194
1195 if (*keydist & SMP_DIST_ENC_KEY) {
1196 struct smp_cmd_encrypt_info enc;
1197 struct smp_cmd_master_ident ident;
1198 struct smp_ltk *ltk;
1199 u8 authenticated;
1200 __le16 ediv;
1201 __le64 rand;
1202
1203 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1204 get_random_bytes(&ediv, sizeof(ediv));
1205 get_random_bytes(&rand, sizeof(rand));
1206
1207 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1208
1209 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1210 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1211 SMP_LTK_SLAVE, authenticated, enc.ltk,
1212 smp->enc_key_size, ediv, rand);
1213 smp->slave_ltk = ltk;
1214
1215 ident.ediv = ediv;
1216 ident.rand = rand;
1217
1218 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1219
1220 *keydist &= ~SMP_DIST_ENC_KEY;
1221 }
1222
1223 if (*keydist & SMP_DIST_ID_KEY) {
1224 struct smp_cmd_ident_addr_info addrinfo;
1225 struct smp_cmd_ident_info idinfo;
1226
1227 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1228
1229 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1230
1231 /* The hci_conn contains the local identity address
1232 * after the connection has been established.
1233 *
1234 * This is true even when the connection has been
1235 * established using a resolvable random address.
1236 */
1237 bacpy(&addrinfo.bdaddr, &hcon->src);
1238 addrinfo.addr_type = hcon->src_type;
1239
1240 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1241 &addrinfo);
1242
1243 *keydist &= ~SMP_DIST_ID_KEY;
1244 }
1245
1246 if (*keydist & SMP_DIST_SIGN) {
1247 struct smp_cmd_sign_info sign;
1248 struct smp_csrk *csrk;
1249
1250 /* Generate a new random key */
1251 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1252
1253 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1254 if (csrk) {
1255 csrk->master = 0x00;
1256 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1257 }
1258 smp->slave_csrk = csrk;
1259
1260 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1261
1262 *keydist &= ~SMP_DIST_SIGN;
1263 }
1264
1265 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001266 if (smp->remote_key_dist & KEY_DIST_MASK) {
1267 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001268 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001269 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001270
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001271 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1272 smp_notify_keys(conn);
1273
1274 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001275}
1276
Johan Hedbergb68fda62014-08-11 22:06:40 +03001277static void smp_timeout(struct work_struct *work)
1278{
1279 struct smp_chan *smp = container_of(work, struct smp_chan,
1280 security_timer.work);
1281 struct l2cap_conn *conn = smp->conn;
1282
1283 BT_DBG("conn %p", conn);
1284
Johan Hedberg1e91c292014-08-18 20:33:29 +03001285 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001286}
1287
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001288static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1289{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001290 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001291 struct smp_chan *smp;
1292
Marcel Holtmannf1560462013-10-13 05:43:25 -07001293 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001294 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001295 return NULL;
1296
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001297 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1298 if (IS_ERR(smp->tfm_aes)) {
1299 BT_ERR("Unable to create ECB crypto context");
1300 kfree(smp);
1301 return NULL;
1302 }
1303
Johan Hedberg407cecf2014-05-02 14:19:47 +03001304 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1305 if (IS_ERR(smp->tfm_cmac)) {
1306 BT_ERR("Unable to create CMAC crypto context");
1307 crypto_free_blkcipher(smp->tfm_aes);
1308 kfree(smp);
1309 return NULL;
1310 }
1311
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001312 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001313 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001314
Johan Hedbergb28b4942014-09-05 22:19:55 +03001315 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1316
Johan Hedbergb68fda62014-08-11 22:06:40 +03001317 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1318
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001319 hci_conn_hold(conn->hcon);
1320
1321 return smp;
1322}
1323
Johan Hedberg760b0182014-06-06 11:44:05 +03001324static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1325{
1326 struct hci_conn *hcon = smp->conn->hcon;
1327 u8 *na, *nb, a[7], b[7];
1328
1329 if (hcon->out) {
1330 na = smp->prnd;
1331 nb = smp->rrnd;
1332 } else {
1333 na = smp->rrnd;
1334 nb = smp->prnd;
1335 }
1336
1337 memcpy(a, &hcon->init_addr, 6);
1338 memcpy(b, &hcon->resp_addr, 6);
1339 a[6] = hcon->init_addr_type;
1340 b[6] = hcon->resp_addr_type;
1341
1342 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1343}
1344
Johan Hedberg38606f12014-06-25 11:10:28 +03001345static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001346{
1347 struct hci_conn *hcon = smp->conn->hcon;
1348 struct smp_cmd_dhkey_check check;
1349 u8 a[7], b[7], *local_addr, *remote_addr;
1350 u8 io_cap[3], r[16];
1351
Johan Hedberg760b0182014-06-06 11:44:05 +03001352 memcpy(a, &hcon->init_addr, 6);
1353 memcpy(b, &hcon->resp_addr, 6);
1354 a[6] = hcon->init_addr_type;
1355 b[6] = hcon->resp_addr_type;
1356
1357 if (hcon->out) {
1358 local_addr = a;
1359 remote_addr = b;
1360 memcpy(io_cap, &smp->preq[1], 3);
1361 } else {
1362 local_addr = b;
1363 remote_addr = a;
1364 memcpy(io_cap, &smp->prsp[1], 3);
1365 }
1366
Johan Hedbergdddd3052014-06-01 15:38:09 +03001367 memset(r, 0, sizeof(r));
1368
1369 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001370 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001371
Johan Hedberga29b0732014-10-28 15:17:05 +01001372 if (smp->method == REQ_OOB)
1373 memcpy(r, smp->rr, 16);
1374
Johan Hedberg760b0182014-06-06 11:44:05 +03001375 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1376 local_addr, remote_addr, check.e);
1377
1378 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001379}
1380
Johan Hedberg38606f12014-06-25 11:10:28 +03001381static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1382{
1383 struct l2cap_conn *conn = smp->conn;
1384 struct hci_conn *hcon = conn->hcon;
1385 struct smp_cmd_pairing_confirm cfm;
1386 u8 r;
1387
1388 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1389 r |= 0x80;
1390
1391 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1392
1393 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1394 cfm.confirm_val))
1395 return SMP_UNSPECIFIED;
1396
1397 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1398
1399 return 0;
1400}
1401
1402static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1403{
1404 struct l2cap_conn *conn = smp->conn;
1405 struct hci_conn *hcon = conn->hcon;
1406 struct hci_dev *hdev = hcon->hdev;
1407 u8 cfm[16], r;
1408
1409 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1410 if (smp->passkey_round >= 20)
1411 return 0;
1412
1413 switch (smp_op) {
1414 case SMP_CMD_PAIRING_RANDOM:
1415 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1416 r |= 0x80;
1417
1418 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1419 smp->rrnd, r, cfm))
1420 return SMP_UNSPECIFIED;
1421
1422 if (memcmp(smp->pcnf, cfm, 16))
1423 return SMP_CONFIRM_FAILED;
1424
1425 smp->passkey_round++;
1426
1427 if (smp->passkey_round == 20) {
1428 /* Generate MacKey and LTK */
1429 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1430 return SMP_UNSPECIFIED;
1431 }
1432
1433 /* The round is only complete when the initiator
1434 * receives pairing random.
1435 */
1436 if (!hcon->out) {
1437 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1438 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001439 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001440 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001441 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001442 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001443 return 0;
1444 }
1445
1446 /* Start the next round */
1447 if (smp->passkey_round != 20)
1448 return sc_passkey_round(smp, 0);
1449
1450 /* Passkey rounds are complete - start DHKey Check */
1451 sc_dhkey_check(smp);
1452 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1453
1454 break;
1455
1456 case SMP_CMD_PAIRING_CONFIRM:
1457 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1458 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1459 return 0;
1460 }
1461
1462 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1463
1464 if (hcon->out) {
1465 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1466 sizeof(smp->prnd), smp->prnd);
1467 return 0;
1468 }
1469
1470 return sc_passkey_send_confirm(smp);
1471
1472 case SMP_CMD_PUBLIC_KEY:
1473 default:
1474 /* Initiating device starts the round */
1475 if (!hcon->out)
1476 return 0;
1477
1478 BT_DBG("%s Starting passkey round %u", hdev->name,
1479 smp->passkey_round + 1);
1480
1481 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1482
1483 return sc_passkey_send_confirm(smp);
1484 }
1485
1486 return 0;
1487}
1488
Johan Hedbergdddd3052014-06-01 15:38:09 +03001489static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1490{
Johan Hedberg38606f12014-06-25 11:10:28 +03001491 struct l2cap_conn *conn = smp->conn;
1492 struct hci_conn *hcon = conn->hcon;
1493 u8 smp_op;
1494
1495 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1496
Johan Hedbergdddd3052014-06-01 15:38:09 +03001497 switch (mgmt_op) {
1498 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1499 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1500 return 0;
1501 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1502 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1503 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001504 case MGMT_OP_USER_PASSKEY_REPLY:
1505 hcon->passkey_notify = le32_to_cpu(passkey);
1506 smp->passkey_round = 0;
1507
1508 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1509 smp_op = SMP_CMD_PAIRING_CONFIRM;
1510 else
1511 smp_op = 0;
1512
1513 if (sc_passkey_round(smp, smp_op))
1514 return -EIO;
1515
1516 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001517 }
1518
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001519 /* Initiator sends DHKey check first */
1520 if (hcon->out) {
1521 sc_dhkey_check(smp);
1522 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1523 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1524 sc_dhkey_check(smp);
1525 sc_add_ltk(smp);
1526 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001527
1528 return 0;
1529}
1530
Brian Gix2b64d152011-12-21 16:12:12 -08001531int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1532{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001533 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001534 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001535 struct smp_chan *smp;
1536 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001537 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001538
1539 BT_DBG("");
1540
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001541 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001542 return -ENOTCONN;
1543
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001544 chan = conn->smp;
1545 if (!chan)
1546 return -ENOTCONN;
1547
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001548 l2cap_chan_lock(chan);
1549 if (!chan->data) {
1550 err = -ENOTCONN;
1551 goto unlock;
1552 }
1553
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001554 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001555
Johan Hedberg760b0182014-06-06 11:44:05 +03001556 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1557 err = sc_user_reply(smp, mgmt_op, passkey);
1558 goto unlock;
1559 }
1560
Brian Gix2b64d152011-12-21 16:12:12 -08001561 switch (mgmt_op) {
1562 case MGMT_OP_USER_PASSKEY_REPLY:
1563 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001564 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001565 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001566 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001567 /* Fall Through */
1568 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001569 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001570 break;
1571 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1572 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001573 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001574 err = 0;
1575 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001576 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001577 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001578 err = -EOPNOTSUPP;
1579 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001580 }
1581
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001582 err = 0;
1583
Brian Gix2b64d152011-12-21 16:12:12 -08001584 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001585 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1586 u8 rsp = smp_confirm(smp);
1587 if (rsp)
1588 smp_failure(conn, rsp);
1589 }
Brian Gix2b64d152011-12-21 16:12:12 -08001590
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001591unlock:
1592 l2cap_chan_unlock(chan);
1593 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001594}
1595
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001596static void build_bredr_pairing_cmd(struct smp_chan *smp,
1597 struct smp_cmd_pairing *req,
1598 struct smp_cmd_pairing *rsp)
1599{
1600 struct l2cap_conn *conn = smp->conn;
1601 struct hci_dev *hdev = conn->hcon->hdev;
1602 u8 local_dist = 0, remote_dist = 0;
1603
1604 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) {
1605 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1606 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1607 }
1608
1609 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
1610 remote_dist |= SMP_DIST_ID_KEY;
1611
1612 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
1613 local_dist |= SMP_DIST_ID_KEY;
1614
1615 if (!rsp) {
1616 memset(req, 0, sizeof(*req));
1617
1618 req->init_key_dist = local_dist;
1619 req->resp_key_dist = remote_dist;
1620 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1621
1622 smp->remote_key_dist = remote_dist;
1623
1624 return;
1625 }
1626
1627 memset(rsp, 0, sizeof(*rsp));
1628
1629 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1630 rsp->init_key_dist = req->init_key_dist & remote_dist;
1631 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1632
1633 smp->remote_key_dist = rsp->init_key_dist;
1634}
1635
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001636static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001637{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001638 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001639 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001640 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001641 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001642 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001643 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001644
1645 BT_DBG("conn %p", conn);
1646
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001647 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001648 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001649
Johan Hedberg40bef302014-07-16 11:42:27 +03001650 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001651 return SMP_CMD_NOTSUPP;
1652
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001653 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001654 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001655 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001656 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001657
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001658 if (!smp)
1659 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001660
Johan Hedbergc05b9332014-09-10 17:37:42 -07001661 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001662 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001663
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001664 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001665 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001666 return SMP_PAIRING_NOTSUPP;
1667
Johan Hedberg903b71c2014-09-08 16:59:18 -07001668 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1669 return SMP_AUTH_REQUIREMENTS;
1670
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001671 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1672 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001673 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001674
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001675 /* SMP over BR/EDR requires special treatment */
1676 if (conn->hcon->type == ACL_LINK) {
1677 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001678 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmann300acfde2014-12-31 14:43:16 -08001679 !test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001680 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1681
1682 set_bit(SMP_FLAG_SC, &smp->flags);
1683
1684 build_bredr_pairing_cmd(smp, req, &rsp);
1685
1686 key_size = min(req->max_key_size, rsp.max_key_size);
1687 if (check_enc_key_size(conn, key_size))
1688 return SMP_ENC_KEY_SIZE;
1689
1690 /* Clear bits which are generated but not distributed */
1691 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1692
1693 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1694 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1695 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1696
1697 smp_distribute_keys(smp);
1698 return 0;
1699 }
1700
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001701 build_pairing_cmd(conn, req, &rsp, auth);
1702
1703 if (rsp.auth_req & SMP_AUTH_SC)
1704 set_bit(SMP_FLAG_SC, &smp->flags);
1705
Johan Hedberg5be5e272014-09-10 17:58:54 -07001706 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001707 sec_level = BT_SECURITY_MEDIUM;
1708 else
1709 sec_level = authreq_to_seclevel(auth);
1710
Johan Hedbergc7262e72014-06-17 13:07:37 +03001711 if (sec_level > conn->hcon->pending_sec_level)
1712 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001713
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001714 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001715 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1716 u8 method;
1717
1718 method = get_auth_method(smp, conn->hcon->io_capability,
1719 req->io_capability);
1720 if (method == JUST_WORKS || method == JUST_CFM)
1721 return SMP_AUTH_REQUIREMENTS;
1722 }
1723
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001724 key_size = min(req->max_key_size, rsp.max_key_size);
1725 if (check_enc_key_size(conn, key_size))
1726 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001727
Johan Hedberge84a6b12013-12-02 10:49:03 +02001728 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001729
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001730 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1731 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001732
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001733 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001734
1735 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1736
1737 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1738 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1739 /* Clear bits which are generated but not distributed */
1740 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1741 /* Wait for Public Key from Initiating Device */
1742 return 0;
1743 } else {
1744 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1745 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001746
Brian Gix2b64d152011-12-21 16:12:12 -08001747 /* Request setup of TK */
1748 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1749 if (ret)
1750 return SMP_UNSPECIFIED;
1751
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001752 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001753}
1754
Johan Hedberg3b191462014-06-06 10:50:15 +03001755static u8 sc_send_public_key(struct smp_chan *smp)
1756{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001757 struct hci_dev *hdev = smp->conn->hcon->hdev;
1758
Johan Hedberg3b191462014-06-06 10:50:15 +03001759 BT_DBG("");
1760
Johan Hedberg70157ef2014-06-24 15:22:59 +03001761 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) {
1762 BT_DBG("Using debug keys");
1763 memcpy(smp->local_pk, debug_pk, 64);
1764 memcpy(smp->local_sk, debug_sk, 32);
1765 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1766 } else {
1767 while (true) {
1768 /* Generate local key pair for Secure Connections */
1769 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1770 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001771
Johan Hedberg70157ef2014-06-24 15:22:59 +03001772 /* This is unlikely, but we need to check that
1773 * we didn't accidentially generate a debug key.
1774 */
1775 if (memcmp(smp->local_sk, debug_sk, 32))
1776 break;
1777 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001778 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001779
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001780 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1781 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1782 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001783
1784 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1785
1786 return 0;
1787}
1788
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001789static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001790{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001791 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001792 struct l2cap_chan *chan = conn->smp;
1793 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001794 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001795 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001796 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001797
1798 BT_DBG("conn %p", conn);
1799
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001800 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001801 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001802
Johan Hedberg40bef302014-07-16 11:42:27 +03001803 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001804 return SMP_CMD_NOTSUPP;
1805
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001806 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001807
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001808 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001809
1810 key_size = min(req->max_key_size, rsp->max_key_size);
1811 if (check_enc_key_size(conn, key_size))
1812 return SMP_ENC_KEY_SIZE;
1813
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001814 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001815
Johan Hedberg903b71c2014-09-08 16:59:18 -07001816 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1817 return SMP_AUTH_REQUIREMENTS;
1818
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001819 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1820 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1821
1822 /* Update remote key distribution in case the remote cleared
1823 * some bits that we had enabled in our request.
1824 */
1825 smp->remote_key_dist &= rsp->resp_key_dist;
1826
1827 /* For BR/EDR this means we're done and can start phase 3 */
1828 if (conn->hcon->type == ACL_LINK) {
1829 /* Clear bits which are generated but not distributed */
1830 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1831 smp_distribute_keys(smp);
1832 return 0;
1833 }
1834
Johan Hedberg65668772014-05-16 11:03:34 +03001835 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1836 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001837 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1838 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001839
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001840 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001841 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1842 u8 method;
1843
1844 method = get_auth_method(smp, req->io_capability,
1845 rsp->io_capability);
1846 if (method == JUST_WORKS || method == JUST_CFM)
1847 return SMP_AUTH_REQUIREMENTS;
1848 }
1849
Johan Hedberge84a6b12013-12-02 10:49:03 +02001850 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001851
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001852 /* Update remote key distribution in case the remote cleared
1853 * some bits that we had enabled in our request.
1854 */
1855 smp->remote_key_dist &= rsp->resp_key_dist;
1856
Johan Hedberg3b191462014-06-06 10:50:15 +03001857 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1858 /* Clear bits which are generated but not distributed */
1859 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1860 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1861 return sc_send_public_key(smp);
1862 }
1863
Johan Hedbergc05b9332014-09-10 17:37:42 -07001864 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001865
Johan Hedberg476585e2012-06-06 18:54:15 +08001866 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001867 if (ret)
1868 return SMP_UNSPECIFIED;
1869
Johan Hedberg4a74d652014-05-20 09:45:50 +03001870 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001871
1872 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001873 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001874 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001875
1876 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001877}
1878
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001879static u8 sc_check_confirm(struct smp_chan *smp)
1880{
1881 struct l2cap_conn *conn = smp->conn;
1882
1883 BT_DBG("");
1884
1885 /* Public Key exchange must happen before any other steps */
1886 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1887 return SMP_UNSPECIFIED;
1888
Johan Hedberg38606f12014-06-25 11:10:28 +03001889 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1890 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1891
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001892 if (conn->hcon->out) {
1893 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1894 smp->prnd);
1895 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1896 }
1897
1898 return 0;
1899}
1900
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001901static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001902{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001903 struct l2cap_chan *chan = conn->smp;
1904 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001905
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001906 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1907
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001908 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001909 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001910
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001911 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1912 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001913
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001914 if (test_bit(SMP_FLAG_SC, &smp->flags))
1915 return sc_check_confirm(smp);
1916
Johan Hedbergb28b4942014-09-05 22:19:55 +03001917 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001918 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1919 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001920 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1921 return 0;
1922 }
1923
1924 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001925 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001926 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001927 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001928
1929 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001930}
1931
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001932static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001933{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001934 struct l2cap_chan *chan = conn->smp;
1935 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001936 struct hci_conn *hcon = conn->hcon;
1937 u8 *pkax, *pkbx, *na, *nb;
1938 u32 passkey;
1939 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001940
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001941 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001942
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001943 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001944 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001945
Johan Hedberg943a7322014-03-18 12:58:24 +02001946 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001947 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001948
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001949 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1950 return smp_random(smp);
1951
Johan Hedberg580039e2014-12-03 16:26:37 +02001952 if (hcon->out) {
1953 pkax = smp->local_pk;
1954 pkbx = smp->remote_pk;
1955 na = smp->prnd;
1956 nb = smp->rrnd;
1957 } else {
1958 pkax = smp->remote_pk;
1959 pkbx = smp->local_pk;
1960 na = smp->rrnd;
1961 nb = smp->prnd;
1962 }
1963
Johan Hedberga29b0732014-10-28 15:17:05 +01001964 if (smp->method == REQ_OOB) {
1965 if (!hcon->out)
1966 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1967 sizeof(smp->prnd), smp->prnd);
1968 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1969 goto mackey_and_ltk;
1970 }
1971
Johan Hedberg38606f12014-06-25 11:10:28 +03001972 /* Passkey entry has special treatment */
1973 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1974 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1975
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001976 if (hcon->out) {
1977 u8 cfm[16];
1978
1979 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1980 smp->rrnd, 0, cfm);
1981 if (err)
1982 return SMP_UNSPECIFIED;
1983
1984 if (memcmp(smp->pcnf, cfm, 16))
1985 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001986 } else {
1987 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1988 smp->prnd);
1989 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001990 }
1991
Johan Hedberga29b0732014-10-28 15:17:05 +01001992mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03001993 /* Generate MacKey and LTK */
1994 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1995 if (err)
1996 return SMP_UNSPECIFIED;
1997
Johan Hedberga29b0732014-10-28 15:17:05 +01001998 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03001999 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002000 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002001 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2002 }
2003 return 0;
2004 }
2005
Johan Hedberg38606f12014-06-25 11:10:28 +03002006 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002007 if (err)
2008 return SMP_UNSPECIFIED;
2009
Johan Hedberg38606f12014-06-25 11:10:28 +03002010 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2011 hcon->dst_type, passkey, 0);
2012 if (err)
2013 return SMP_UNSPECIFIED;
2014
2015 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2016
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002017 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002018}
2019
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002020static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002021{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002022 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002023 struct hci_conn *hcon = conn->hcon;
2024
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002025 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002026 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002027 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002028
Johan Hedberga6f78332014-09-10 17:37:45 -07002029 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002030 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002031
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002032 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002033 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002034
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002035 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2036 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002037
Johan Hedbergfe59a052014-07-01 19:14:12 +03002038 /* We never store STKs for master role, so clear this flag */
2039 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2040
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002041 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002042}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002043
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002044bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2045 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002046{
2047 if (sec_level == BT_SECURITY_LOW)
2048 return true;
2049
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002050 /* If we're encrypted with an STK but the caller prefers using
2051 * LTK claim insufficient security. This way we allow the
2052 * connection to be re-encrypted with an LTK, even if the LTK
2053 * provides the same level of security. Only exception is if we
2054 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002055 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002056 if (key_pref == SMP_USE_LTK &&
2057 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002058 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002059 return false;
2060
Johan Hedberg854f4722014-07-01 18:40:20 +03002061 if (hcon->sec_level >= sec_level)
2062 return true;
2063
2064 return false;
2065}
2066
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002067static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002068{
2069 struct smp_cmd_security_req *rp = (void *) skb->data;
2070 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002071 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002072 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002073 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002074 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002075
2076 BT_DBG("conn %p", conn);
2077
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002078 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002079 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002080
Johan Hedberg40bef302014-07-16 11:42:27 +03002081 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002082 return SMP_CMD_NOTSUPP;
2083
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002084 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002085
Johan Hedberg903b71c2014-09-08 16:59:18 -07002086 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
2087 return SMP_AUTH_REQUIREMENTS;
2088
Johan Hedberg5be5e272014-09-10 17:58:54 -07002089 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002090 sec_level = BT_SECURITY_MEDIUM;
2091 else
2092 sec_level = authreq_to_seclevel(auth);
2093
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002094 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002095 return 0;
2096
Johan Hedbergc7262e72014-06-17 13:07:37 +03002097 if (sec_level > hcon->pending_sec_level)
2098 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002099
Johan Hedberg4dab7862012-06-07 14:58:37 +08002100 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002101 return 0;
2102
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002103 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002104 if (!smp)
2105 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002106
Johan Hedbergb6ae8452014-07-30 09:22:22 +03002107 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002108 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002109 return SMP_PAIRING_NOTSUPP;
2110
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002111 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002112
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002113 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002114 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002115
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002116 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2117 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002118
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002119 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002120 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002121
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002122 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002123}
2124
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002125int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002126{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002127 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002128 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002129 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002130 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002131 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002132
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002133 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2134
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002135 /* This may be NULL if there's an unexpected disconnection */
2136 if (!conn)
2137 return 1;
2138
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002139 chan = conn->smp;
2140
Johan Hedberg757aee02013-04-24 13:05:32 +03002141 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002142 return 1;
2143
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002144 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002145 return 1;
2146
Johan Hedbergc7262e72014-06-17 13:07:37 +03002147 if (sec_level > hcon->pending_sec_level)
2148 hcon->pending_sec_level = sec_level;
2149
Johan Hedberg40bef302014-07-16 11:42:27 +03002150 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002151 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2152 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002153
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002154 l2cap_chan_lock(chan);
2155
2156 /* If SMP is already in progress ignore this request */
2157 if (chan->data) {
2158 ret = 0;
2159 goto unlock;
2160 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002161
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002162 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002163 if (!smp) {
2164 ret = 1;
2165 goto unlock;
2166 }
Brian Gix2b64d152011-12-21 16:12:12 -08002167
2168 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002169
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002170 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
2171 authreq |= SMP_AUTH_SC;
2172
Johan Hedberg79897d22014-06-01 09:45:24 +03002173 /* Require MITM if IO Capability allows or the security level
2174 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002175 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002176 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002177 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002178 authreq |= SMP_AUTH_MITM;
2179
Johan Hedberg40bef302014-07-16 11:42:27 +03002180 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002181 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002182
Brian Gix2b64d152011-12-21 16:12:12 -08002183 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002184 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2185 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002186
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002187 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002188 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002189 } else {
2190 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002191 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002192 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002193 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002194 }
2195
Johan Hedberg4a74d652014-05-20 09:45:50 +03002196 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002197 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002198
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002199unlock:
2200 l2cap_chan_unlock(chan);
2201 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002202}
2203
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002204static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2205{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002206 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002207 struct l2cap_chan *chan = conn->smp;
2208 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002209
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002210 BT_DBG("conn %p", conn);
2211
2212 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002213 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002214
Johan Hedbergb28b4942014-09-05 22:19:55 +03002215 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002216
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002217 skb_pull(skb, sizeof(*rp));
2218
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002219 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002220
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002221 return 0;
2222}
2223
2224static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2225{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002226 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002227 struct l2cap_chan *chan = conn->smp;
2228 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002229 struct hci_dev *hdev = conn->hcon->hdev;
2230 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002231 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002232 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002233
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002234 BT_DBG("conn %p", conn);
2235
2236 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002237 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002238
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002239 /* Mark the information as received */
2240 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2241
Johan Hedbergb28b4942014-09-05 22:19:55 +03002242 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2243 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002244 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2245 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002246
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002247 skb_pull(skb, sizeof(*rp));
2248
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002249 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002250 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002251 authenticated, smp->tk, smp->enc_key_size,
2252 rp->ediv, rp->rand);
2253 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002254 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002255 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002256
2257 return 0;
2258}
2259
Johan Hedbergfd349c02014-02-18 10:19:36 +02002260static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2261{
2262 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002263 struct l2cap_chan *chan = conn->smp;
2264 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002265
2266 BT_DBG("");
2267
2268 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002269 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002270
Johan Hedbergb28b4942014-09-05 22:19:55 +03002271 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002272
Johan Hedbergfd349c02014-02-18 10:19:36 +02002273 skb_pull(skb, sizeof(*info));
2274
2275 memcpy(smp->irk, info->irk, 16);
2276
2277 return 0;
2278}
2279
2280static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2281 struct sk_buff *skb)
2282{
2283 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002284 struct l2cap_chan *chan = conn->smp;
2285 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002286 struct hci_conn *hcon = conn->hcon;
2287 bdaddr_t rpa;
2288
2289 BT_DBG("");
2290
2291 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002292 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002293
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002294 /* Mark the information as received */
2295 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2296
Johan Hedbergb28b4942014-09-05 22:19:55 +03002297 if (smp->remote_key_dist & SMP_DIST_SIGN)
2298 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2299
Johan Hedbergfd349c02014-02-18 10:19:36 +02002300 skb_pull(skb, sizeof(*info));
2301
Johan Hedberga9a58f82014-02-25 22:24:37 +02002302 /* Strictly speaking the Core Specification (4.1) allows sending
2303 * an empty address which would force us to rely on just the IRK
2304 * as "identity information". However, since such
2305 * implementations are not known of and in order to not over
2306 * complicate our implementation, simply pretend that we never
2307 * received an IRK for such a device.
2308 */
2309 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
2310 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002311 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002312 }
2313
Johan Hedbergfd349c02014-02-18 10:19:36 +02002314 bacpy(&smp->id_addr, &info->bdaddr);
2315 smp->id_addr_type = info->addr_type;
2316
2317 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2318 bacpy(&rpa, &hcon->dst);
2319 else
2320 bacpy(&rpa, BDADDR_ANY);
2321
Johan Hedberg23d0e122014-02-19 14:57:46 +02002322 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2323 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002324
Johan Hedberg31dd6242014-06-27 14:23:02 +03002325distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002326 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2327 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002328
2329 return 0;
2330}
2331
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002332static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2333{
2334 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002335 struct l2cap_chan *chan = conn->smp;
2336 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002337 struct smp_csrk *csrk;
2338
2339 BT_DBG("conn %p", conn);
2340
2341 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002342 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002343
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002344 /* Mark the information as received */
2345 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2346
2347 skb_pull(skb, sizeof(*rp));
2348
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002349 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2350 if (csrk) {
2351 csrk->master = 0x01;
2352 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2353 }
2354 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002355 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002356
2357 return 0;
2358}
2359
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002360static u8 sc_select_method(struct smp_chan *smp)
2361{
2362 struct l2cap_conn *conn = smp->conn;
2363 struct hci_conn *hcon = conn->hcon;
2364 struct smp_cmd_pairing *local, *remote;
2365 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2366
Johan Hedberga29b0732014-10-28 15:17:05 +01002367 if (test_bit(SMP_FLAG_OOB, &smp->flags))
2368 return REQ_OOB;
2369
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002370 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2371 * which are needed as inputs to some crypto functions. To get
2372 * the "struct smp_cmd_pairing" from them we need to skip the
2373 * first byte which contains the opcode.
2374 */
2375 if (hcon->out) {
2376 local = (void *) &smp->preq[1];
2377 remote = (void *) &smp->prsp[1];
2378 } else {
2379 local = (void *) &smp->prsp[1];
2380 remote = (void *) &smp->preq[1];
2381 }
2382
2383 local_io = local->io_capability;
2384 remote_io = remote->io_capability;
2385
2386 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2387 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2388
2389 /* If either side wants MITM, look up the method from the table,
2390 * otherwise use JUST WORKS.
2391 */
2392 if (local_mitm || remote_mitm)
2393 method = get_auth_method(smp, local_io, remote_io);
2394 else
2395 method = JUST_WORKS;
2396
2397 /* Don't confirm locally initiated pairing attempts */
2398 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2399 method = JUST_WORKS;
2400
2401 return method;
2402}
2403
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002404static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2405{
2406 struct smp_cmd_public_key *key = (void *) skb->data;
2407 struct hci_conn *hcon = conn->hcon;
2408 struct l2cap_chan *chan = conn->smp;
2409 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002410 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002411 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002412 int err;
2413
2414 BT_DBG("conn %p", conn);
2415
2416 if (skb->len < sizeof(*key))
2417 return SMP_INVALID_PARAMS;
2418
2419 memcpy(smp->remote_pk, key, 64);
2420
2421 /* Non-initiating device sends its public key after receiving
2422 * the key from the initiating device.
2423 */
2424 if (!hcon->out) {
2425 err = sc_send_public_key(smp);
2426 if (err)
2427 return err;
2428 }
2429
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002430 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2431 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002432
2433 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2434 return SMP_UNSPECIFIED;
2435
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002436 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002437
2438 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2439
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002440 smp->method = sc_select_method(smp);
2441
2442 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2443
2444 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2445 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2446 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2447 else
2448 hcon->pending_sec_level = BT_SECURITY_FIPS;
2449
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002450 if (!memcmp(debug_pk, smp->remote_pk, 64))
2451 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2452
Johan Hedberg38606f12014-06-25 11:10:28 +03002453 if (smp->method == DSP_PASSKEY) {
2454 get_random_bytes(&hcon->passkey_notify,
2455 sizeof(hcon->passkey_notify));
2456 hcon->passkey_notify %= 1000000;
2457 hcon->passkey_entered = 0;
2458 smp->passkey_round = 0;
2459 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2460 hcon->dst_type,
2461 hcon->passkey_notify,
2462 hcon->passkey_entered))
2463 return SMP_UNSPECIFIED;
2464 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2465 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2466 }
2467
Johan Hedberga29b0732014-10-28 15:17:05 +01002468 if (smp->method == REQ_OOB) {
2469 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2470 smp->rr, 0, cfm.confirm_val);
2471 if (err)
2472 return SMP_UNSPECIFIED;
2473
2474 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2475 return SMP_CONFIRM_FAILED;
2476
2477 if (hcon->out)
2478 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2479 sizeof(smp->prnd), smp->prnd);
2480
2481 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2482
2483 return 0;
2484 }
2485
Johan Hedberg38606f12014-06-25 11:10:28 +03002486 if (hcon->out)
2487 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2488
2489 if (smp->method == REQ_PASSKEY) {
2490 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2491 hcon->dst_type))
2492 return SMP_UNSPECIFIED;
2493 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2494 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2495 return 0;
2496 }
2497
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002498 /* The Initiating device waits for the non-initiating device to
2499 * send the confirm value.
2500 */
2501 if (conn->hcon->out)
2502 return 0;
2503
2504 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2505 0, cfm.confirm_val);
2506 if (err)
2507 return SMP_UNSPECIFIED;
2508
2509 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2510 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2511
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002512 return 0;
2513}
2514
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002515static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2516{
2517 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2518 struct l2cap_chan *chan = conn->smp;
2519 struct hci_conn *hcon = conn->hcon;
2520 struct smp_chan *smp = chan->data;
2521 u8 a[7], b[7], *local_addr, *remote_addr;
2522 u8 io_cap[3], r[16], e[16];
2523 int err;
2524
2525 BT_DBG("conn %p", conn);
2526
2527 if (skb->len < sizeof(*check))
2528 return SMP_INVALID_PARAMS;
2529
2530 memcpy(a, &hcon->init_addr, 6);
2531 memcpy(b, &hcon->resp_addr, 6);
2532 a[6] = hcon->init_addr_type;
2533 b[6] = hcon->resp_addr_type;
2534
2535 if (hcon->out) {
2536 local_addr = a;
2537 remote_addr = b;
2538 memcpy(io_cap, &smp->prsp[1], 3);
2539 } else {
2540 local_addr = b;
2541 remote_addr = a;
2542 memcpy(io_cap, &smp->preq[1], 3);
2543 }
2544
2545 memset(r, 0, sizeof(r));
2546
Johan Hedberg38606f12014-06-25 11:10:28 +03002547 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2548 put_unaligned_le32(hcon->passkey_notify, r);
2549
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002550 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2551 io_cap, remote_addr, local_addr, e);
2552 if (err)
2553 return SMP_UNSPECIFIED;
2554
2555 if (memcmp(check->e, e, 16))
2556 return SMP_DHKEY_CHECK_FAILED;
2557
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002558 if (!hcon->out) {
2559 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2560 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2561 return 0;
2562 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002563
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002564 /* Slave sends DHKey check as response to master */
2565 sc_dhkey_check(smp);
2566 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002567
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002568 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002569
2570 if (hcon->out) {
2571 hci_le_start_enc(hcon, 0, 0, smp->tk);
2572 hcon->enc_key_size = smp->enc_key_size;
2573 }
2574
2575 return 0;
2576}
2577
Johan Hedberg1408bb62014-06-04 22:45:57 +03002578static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2579 struct sk_buff *skb)
2580{
2581 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2582
2583 BT_DBG("value 0x%02x", kp->value);
2584
2585 return 0;
2586}
2587
Johan Hedberg4befb862014-08-11 22:06:38 +03002588static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002589{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002590 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002591 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002592 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002593 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002594 int err = 0;
2595
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002596 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002597 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002598
Marcel Holtmann06ae3312013-10-18 03:43:00 -07002599 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002600 reason = SMP_PAIRING_NOTSUPP;
2601 goto done;
2602 }
2603
Marcel Holtmann92381f52013-10-03 01:23:08 -07002604 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002605 skb_pull(skb, sizeof(code));
2606
Johan Hedbergb28b4942014-09-05 22:19:55 +03002607 smp = chan->data;
2608
2609 if (code > SMP_CMD_MAX)
2610 goto drop;
2611
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002612 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002613 goto drop;
2614
2615 /* If we don't have a context the only allowed commands are
2616 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002617 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002618 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2619 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002620
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002621 switch (code) {
2622 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002623 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002624 break;
2625
2626 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002627 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002628 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002629 break;
2630
2631 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002632 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002633 break;
2634
2635 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002636 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002637 break;
2638
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002639 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002640 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002641 break;
2642
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002643 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002644 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002645 break;
2646
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002647 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002648 reason = smp_cmd_encrypt_info(conn, skb);
2649 break;
2650
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002651 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002652 reason = smp_cmd_master_ident(conn, skb);
2653 break;
2654
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002655 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002656 reason = smp_cmd_ident_info(conn, skb);
2657 break;
2658
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002659 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002660 reason = smp_cmd_ident_addr_info(conn, skb);
2661 break;
2662
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002663 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002664 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002665 break;
2666
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002667 case SMP_CMD_PUBLIC_KEY:
2668 reason = smp_cmd_public_key(conn, skb);
2669 break;
2670
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002671 case SMP_CMD_DHKEY_CHECK:
2672 reason = smp_cmd_dhkey_check(conn, skb);
2673 break;
2674
Johan Hedberg1408bb62014-06-04 22:45:57 +03002675 case SMP_CMD_KEYPRESS_NOTIFY:
2676 reason = smp_cmd_keypress_notify(conn, skb);
2677 break;
2678
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002679 default:
2680 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002681 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002682 goto done;
2683 }
2684
2685done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002686 if (!err) {
2687 if (reason)
2688 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002689 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002690 }
2691
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002692 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002693
2694drop:
2695 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2696 code, &hcon->dst);
2697 kfree_skb(skb);
2698 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002699}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002700
Johan Hedberg70db83c2014-08-08 09:37:16 +03002701static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2702{
2703 struct l2cap_conn *conn = chan->conn;
2704
2705 BT_DBG("chan %p", chan);
2706
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002707 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002708 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002709
Johan Hedberg70db83c2014-08-08 09:37:16 +03002710 conn->smp = NULL;
2711 l2cap_chan_put(chan);
2712}
2713
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002714static void bredr_pairing(struct l2cap_chan *chan)
2715{
2716 struct l2cap_conn *conn = chan->conn;
2717 struct hci_conn *hcon = conn->hcon;
2718 struct hci_dev *hdev = hcon->hdev;
2719 struct smp_cmd_pairing req;
2720 struct smp_chan *smp;
2721
2722 BT_DBG("chan %p", chan);
2723
2724 /* Only new pairings are interesting */
2725 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2726 return;
2727
2728 /* Don't bother if we're not encrypted */
2729 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2730 return;
2731
2732 /* Only master may initiate SMP over BR/EDR */
2733 if (hcon->role != HCI_ROLE_MASTER)
2734 return;
2735
2736 /* Secure Connections support must be enabled */
2737 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
2738 return;
2739
2740 /* BR/EDR must use Secure Connections for SMP */
2741 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmann300acfde2014-12-31 14:43:16 -08002742 !test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002743 return;
2744
2745 /* If our LE support is not enabled don't do anything */
2746 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
2747 return;
2748
2749 /* Don't bother if remote LE support is not enabled */
2750 if (!lmp_host_le_capable(hcon))
2751 return;
2752
2753 /* Remote must support SMP fixed chan for BR/EDR */
2754 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2755 return;
2756
2757 /* Don't bother if SMP is already ongoing */
2758 if (chan->data)
2759 return;
2760
2761 smp = smp_chan_create(conn);
2762 if (!smp) {
2763 BT_ERR("%s unable to create SMP context for BR/EDR",
2764 hdev->name);
2765 return;
2766 }
2767
2768 set_bit(SMP_FLAG_SC, &smp->flags);
2769
2770 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2771
2772 /* Prepare and send the BR/EDR SMP Pairing Request */
2773 build_bredr_pairing_cmd(smp, &req, NULL);
2774
2775 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2776 memcpy(&smp->preq[1], &req, sizeof(req));
2777
2778 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2779 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2780}
2781
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002782static void smp_resume_cb(struct l2cap_chan *chan)
2783{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002784 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002785 struct l2cap_conn *conn = chan->conn;
2786 struct hci_conn *hcon = conn->hcon;
2787
2788 BT_DBG("chan %p", chan);
2789
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002790 if (hcon->type == ACL_LINK) {
2791 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002792 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002793 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002794
Johan Hedberg86d14072014-08-11 22:06:43 +03002795 if (!smp)
2796 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002797
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002798 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2799 return;
2800
Johan Hedberg86d14072014-08-11 22:06:43 +03002801 cancel_delayed_work(&smp->security_timer);
2802
Johan Hedbergd6268e82014-09-05 22:19:51 +03002803 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002804}
2805
Johan Hedberg70db83c2014-08-08 09:37:16 +03002806static void smp_ready_cb(struct l2cap_chan *chan)
2807{
2808 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002809 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002810
2811 BT_DBG("chan %p", chan);
2812
2813 conn->smp = chan;
2814 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002815
2816 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2817 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002818}
2819
Johan Hedberg4befb862014-08-11 22:06:38 +03002820static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2821{
2822 int err;
2823
2824 BT_DBG("chan %p", chan);
2825
2826 err = smp_sig_channel(chan, skb);
2827 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002828 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002829
Johan Hedbergb68fda62014-08-11 22:06:40 +03002830 if (smp)
2831 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002832
Johan Hedberg1e91c292014-08-18 20:33:29 +03002833 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002834 }
2835
2836 return err;
2837}
2838
Johan Hedberg70db83c2014-08-08 09:37:16 +03002839static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2840 unsigned long hdr_len,
2841 unsigned long len, int nb)
2842{
2843 struct sk_buff *skb;
2844
2845 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2846 if (!skb)
2847 return ERR_PTR(-ENOMEM);
2848
2849 skb->priority = HCI_PRIO_MAX;
2850 bt_cb(skb)->chan = chan;
2851
2852 return skb;
2853}
2854
2855static const struct l2cap_ops smp_chan_ops = {
2856 .name = "Security Manager",
2857 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002858 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002859 .alloc_skb = smp_alloc_skb_cb,
2860 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002861 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002862
2863 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002864 .state_change = l2cap_chan_no_state_change,
2865 .close = l2cap_chan_no_close,
2866 .defer = l2cap_chan_no_defer,
2867 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002868 .set_shutdown = l2cap_chan_no_set_shutdown,
2869 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002870};
2871
2872static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2873{
2874 struct l2cap_chan *chan;
2875
2876 BT_DBG("pchan %p", pchan);
2877
2878 chan = l2cap_chan_create();
2879 if (!chan)
2880 return NULL;
2881
2882 chan->chan_type = pchan->chan_type;
2883 chan->ops = &smp_chan_ops;
2884 chan->scid = pchan->scid;
2885 chan->dcid = chan->scid;
2886 chan->imtu = pchan->imtu;
2887 chan->omtu = pchan->omtu;
2888 chan->mode = pchan->mode;
2889
Johan Hedbergabe84902014-11-12 22:22:21 +02002890 /* Other L2CAP channels may request SMP routines in order to
2891 * change the security level. This means that the SMP channel
2892 * lock must be considered in its own category to avoid lockdep
2893 * warnings.
2894 */
2895 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2896
Johan Hedberg70db83c2014-08-08 09:37:16 +03002897 BT_DBG("created chan %p", chan);
2898
2899 return chan;
2900}
2901
2902static const struct l2cap_ops smp_root_chan_ops = {
2903 .name = "Security Manager Root",
2904 .new_connection = smp_new_conn_cb,
2905
2906 /* None of these are implemented for the root channel */
2907 .close = l2cap_chan_no_close,
2908 .alloc_skb = l2cap_chan_no_alloc_skb,
2909 .recv = l2cap_chan_no_recv,
2910 .state_change = l2cap_chan_no_state_change,
2911 .teardown = l2cap_chan_no_teardown,
2912 .ready = l2cap_chan_no_ready,
2913 .defer = l2cap_chan_no_defer,
2914 .suspend = l2cap_chan_no_suspend,
2915 .resume = l2cap_chan_no_resume,
2916 .set_shutdown = l2cap_chan_no_set_shutdown,
2917 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002918};
2919
Johan Hedbergef8efe42014-08-13 15:12:32 +03002920static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002921{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002922 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002923 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002924
Johan Hedbergef8efe42014-08-13 15:12:32 +03002925 if (cid == L2CAP_CID_SMP_BREDR) {
2926 tfm_aes = NULL;
2927 goto create_chan;
2928 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03002929
Johan Hedbergadae20c2014-11-13 14:37:48 +02002930 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002931 if (IS_ERR(tfm_aes)) {
Johan Hedberg711eafe2014-08-08 09:32:52 +03002932 BT_ERR("Unable to create crypto context");
Fengguang Wufe700772014-12-08 03:04:38 +08002933 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002934 }
2935
Johan Hedbergef8efe42014-08-13 15:12:32 +03002936create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03002937 chan = l2cap_chan_create();
2938 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002939 crypto_free_blkcipher(tfm_aes);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002940 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002941 }
2942
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002943 chan->data = tfm_aes;
2944
Johan Hedbergef8efe42014-08-13 15:12:32 +03002945 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002946
2947 l2cap_chan_set_defaults(chan);
2948
2949 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002950 if (cid == L2CAP_CID_SMP)
2951 chan->src_type = BDADDR_LE_PUBLIC;
2952 else
2953 chan->src_type = BDADDR_BREDR;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002954 chan->state = BT_LISTEN;
2955 chan->mode = L2CAP_MODE_BASIC;
2956 chan->imtu = L2CAP_DEFAULT_MTU;
2957 chan->ops = &smp_root_chan_ops;
2958
Johan Hedbergabe84902014-11-12 22:22:21 +02002959 /* Set correct nesting level for a parent/listening channel */
2960 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2961
Johan Hedbergef8efe42014-08-13 15:12:32 +03002962 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03002963}
2964
Johan Hedbergef8efe42014-08-13 15:12:32 +03002965static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002966{
Johan Hedbergef8efe42014-08-13 15:12:32 +03002967 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002968
Johan Hedbergef8efe42014-08-13 15:12:32 +03002969 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002970
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002971 tfm_aes = chan->data;
2972 if (tfm_aes) {
2973 chan->data = NULL;
2974 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002975 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002976
Johan Hedberg70db83c2014-08-08 09:37:16 +03002977 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002978}
Johan Hedbergef8efe42014-08-13 15:12:32 +03002979
Marcel Holtmann300acfde2014-12-31 14:43:16 -08002980static ssize_t force_bredr_smp_read(struct file *file,
2981 char __user *user_buf,
2982 size_t count, loff_t *ppos)
2983{
2984 struct hci_dev *hdev = file->private_data;
2985 char buf[3];
2986
2987 buf[0] = test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags) ? 'Y': 'N';
2988 buf[1] = '\n';
2989 buf[2] = '\0';
2990 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2991}
2992
2993static ssize_t force_bredr_smp_write(struct file *file,
2994 const char __user *user_buf,
2995 size_t count, loff_t *ppos)
2996{
2997 struct hci_dev *hdev = file->private_data;
2998 char buf[32];
2999 size_t buf_size = min(count, (sizeof(buf)-1));
3000 bool enable;
3001
3002 if (copy_from_user(buf, user_buf, buf_size))
3003 return -EFAULT;
3004
3005 buf[buf_size] = '\0';
3006 if (strtobool(buf, &enable))
3007 return -EINVAL;
3008
3009 if (enable == test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags))
3010 return -EALREADY;
3011
3012 if (enable) {
3013 struct l2cap_chan *chan;
3014
3015 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3016 if (IS_ERR(chan))
3017 return PTR_ERR(chan);
3018
3019 hdev->smp_bredr_data = chan;
3020 } else {
3021 struct l2cap_chan *chan;
3022
3023 chan = hdev->smp_bredr_data;
3024 hdev->smp_bredr_data = NULL;
3025 smp_del_chan(chan);
3026 }
3027
3028 change_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags);
3029
3030 return count;
3031}
3032
3033static const struct file_operations force_bredr_smp_fops = {
3034 .open = simple_open,
3035 .read = force_bredr_smp_read,
3036 .write = force_bredr_smp_write,
3037 .llseek = default_llseek,
3038};
3039
Johan Hedbergef8efe42014-08-13 15:12:32 +03003040int smp_register(struct hci_dev *hdev)
3041{
3042 struct l2cap_chan *chan;
3043
3044 BT_DBG("%s", hdev->name);
3045
3046 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3047 if (IS_ERR(chan))
3048 return PTR_ERR(chan);
3049
3050 hdev->smp_data = chan;
3051
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003052 /* If the controller does not support BR/EDR Secure Connections
3053 * feature, then the BR/EDR SMP channel shall not be present.
3054 *
3055 * To test this with Bluetooth 4.0 controllers, create a debugfs
3056 * switch that allows forcing BR/EDR SMP support and accepting
3057 * cross-transport pairing on non-AES encrypted connections.
3058 */
3059 if (!lmp_sc_capable(hdev)) {
3060 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3061 hdev, &force_bredr_smp_fops);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003062 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003063 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003064
3065 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3066 if (IS_ERR(chan)) {
3067 int err = PTR_ERR(chan);
3068 chan = hdev->smp_data;
3069 hdev->smp_data = NULL;
3070 smp_del_chan(chan);
3071 return err;
3072 }
3073
3074 hdev->smp_bredr_data = chan;
3075
3076 return 0;
3077}
3078
3079void smp_unregister(struct hci_dev *hdev)
3080{
3081 struct l2cap_chan *chan;
3082
3083 if (hdev->smp_bredr_data) {
3084 chan = hdev->smp_bredr_data;
3085 hdev->smp_bredr_data = NULL;
3086 smp_del_chan(chan);
3087 }
3088
3089 if (hdev->smp_data) {
3090 chan = hdev->smp_data;
3091 hdev->smp_data = NULL;
3092 smp_del_chan(chan);
3093 }
3094}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003095
3096#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3097
Johan Hedbergcfc41982014-12-30 09:50:40 +02003098static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3099{
3100 const u8 irk[16] = {
3101 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3102 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3103 const u8 r[3] = { 0x94, 0x81, 0x70 };
3104 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3105 u8 res[3];
3106 int err;
3107
3108 err = smp_ah(tfm_aes, irk, r, res);
3109 if (err)
3110 return err;
3111
3112 if (memcmp(res, exp, 3))
3113 return -EINVAL;
3114
3115 return 0;
3116}
3117
3118static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3119{
3120 const u8 k[16] = {
3121 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3122 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3123 const u8 r[16] = {
3124 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3125 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3126 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3127 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3128 const u8 _iat = 0x01;
3129 const u8 _rat = 0x00;
3130 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3131 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3132 const u8 exp[16] = {
3133 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3134 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3135 u8 res[16];
3136 int err;
3137
3138 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3139 if (err)
3140 return err;
3141
3142 if (memcmp(res, exp, 16))
3143 return -EINVAL;
3144
3145 return 0;
3146}
3147
3148static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3149{
3150 const u8 k[16] = {
3151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3153 const u8 r1[16] = {
3154 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3155 const u8 r2[16] = {
3156 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3157 const u8 exp[16] = {
3158 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3159 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3160 u8 res[16];
3161 int err;
3162
3163 err = smp_s1(tfm_aes, k, r1, r2, res);
3164 if (err)
3165 return err;
3166
3167 if (memcmp(res, exp, 16))
3168 return -EINVAL;
3169
3170 return 0;
3171}
3172
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003173static int __init test_f4(struct crypto_hash *tfm_cmac)
3174{
3175 const u8 u[32] = {
3176 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3177 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3178 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3179 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3180 const u8 v[32] = {
3181 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3182 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3183 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3184 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3185 const u8 x[16] = {
3186 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3187 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3188 const u8 z = 0x00;
3189 const u8 exp[16] = {
3190 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3191 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3192 u8 res[16];
3193 int err;
3194
3195 err = smp_f4(tfm_cmac, u, v, x, z, res);
3196 if (err)
3197 return err;
3198
3199 if (memcmp(res, exp, 16))
3200 return -EINVAL;
3201
3202 return 0;
3203}
3204
3205static int __init test_f5(struct crypto_hash *tfm_cmac)
3206{
3207 const u8 w[32] = {
3208 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3209 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3210 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3211 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3212 const u8 n1[16] = {
3213 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3214 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3215 const u8 n2[16] = {
3216 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3217 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3218 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3219 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3220 const u8 exp_ltk[16] = {
3221 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3222 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3223 const u8 exp_mackey[16] = {
3224 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3225 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3226 u8 mackey[16], ltk[16];
3227 int err;
3228
3229 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3230 if (err)
3231 return err;
3232
3233 if (memcmp(mackey, exp_mackey, 16))
3234 return -EINVAL;
3235
3236 if (memcmp(ltk, exp_ltk, 16))
3237 return -EINVAL;
3238
3239 return 0;
3240}
3241
3242static int __init test_f6(struct crypto_hash *tfm_cmac)
3243{
3244 const u8 w[16] = {
3245 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3246 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3247 const u8 n1[16] = {
3248 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3249 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3250 const u8 n2[16] = {
3251 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3252 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3253 const u8 r[16] = {
3254 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3255 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3256 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3257 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3258 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3259 const u8 exp[16] = {
3260 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3261 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3262 u8 res[16];
3263 int err;
3264
3265 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3266 if (err)
3267 return err;
3268
3269 if (memcmp(res, exp, 16))
3270 return -EINVAL;
3271
3272 return 0;
3273}
3274
3275static int __init test_g2(struct crypto_hash *tfm_cmac)
3276{
3277 const u8 u[32] = {
3278 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3279 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3280 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3281 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3282 const u8 v[32] = {
3283 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3284 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3285 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3286 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3287 const u8 x[16] = {
3288 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3289 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3290 const u8 y[16] = {
3291 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3292 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3293 const u32 exp_val = 0x2f9ed5ba % 1000000;
3294 u32 val;
3295 int err;
3296
3297 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3298 if (err)
3299 return err;
3300
3301 if (val != exp_val)
3302 return -EINVAL;
3303
3304 return 0;
3305}
3306
3307static int __init test_h6(struct crypto_hash *tfm_cmac)
3308{
3309 const u8 w[16] = {
3310 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3311 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3312 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3313 const u8 exp[16] = {
3314 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3315 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3316 u8 res[16];
3317 int err;
3318
3319 err = smp_h6(tfm_cmac, w, key_id, res);
3320 if (err)
3321 return err;
3322
3323 if (memcmp(res, exp, 16))
3324 return -EINVAL;
3325
3326 return 0;
3327}
3328
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003329static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3330 struct crypto_hash *tfm_cmac)
3331{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003332 ktime_t calltime, delta, rettime;
3333 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003334 int err;
3335
Marcel Holtmann255047b2014-12-30 00:11:20 -08003336 calltime = ktime_get();
3337
Johan Hedbergcfc41982014-12-30 09:50:40 +02003338 err = test_ah(tfm_aes);
3339 if (err) {
3340 BT_ERR("smp_ah test failed");
3341 return err;
3342 }
3343
3344 err = test_c1(tfm_aes);
3345 if (err) {
3346 BT_ERR("smp_c1 test failed");
3347 return err;
3348 }
3349
3350 err = test_s1(tfm_aes);
3351 if (err) {
3352 BT_ERR("smp_s1 test failed");
3353 return err;
3354 }
3355
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003356 err = test_f4(tfm_cmac);
3357 if (err) {
3358 BT_ERR("smp_f4 test failed");
3359 return err;
3360 }
3361
3362 err = test_f5(tfm_cmac);
3363 if (err) {
3364 BT_ERR("smp_f5 test failed");
3365 return err;
3366 }
3367
3368 err = test_f6(tfm_cmac);
3369 if (err) {
3370 BT_ERR("smp_f6 test failed");
3371 return err;
3372 }
3373
3374 err = test_g2(tfm_cmac);
3375 if (err) {
3376 BT_ERR("smp_g2 test failed");
3377 return err;
3378 }
3379
3380 err = test_h6(tfm_cmac);
3381 if (err) {
3382 BT_ERR("smp_h6 test failed");
3383 return err;
3384 }
3385
Marcel Holtmann255047b2014-12-30 00:11:20 -08003386 rettime = ktime_get();
3387 delta = ktime_sub(rettime, calltime);
3388 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3389
3390 BT_INFO("SMP test passed in %lld usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003391
3392 return 0;
3393}
3394
3395int __init bt_selftest_smp(void)
3396{
3397 struct crypto_blkcipher *tfm_aes;
3398 struct crypto_hash *tfm_cmac;
3399 int err;
3400
3401 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3402 if (IS_ERR(tfm_aes)) {
3403 BT_ERR("Unable to create ECB crypto context");
3404 return PTR_ERR(tfm_aes);
3405 }
3406
3407 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3408 if (IS_ERR(tfm_cmac)) {
3409 BT_ERR("Unable to create CMAC crypto context");
3410 crypto_free_blkcipher(tfm_aes);
3411 return PTR_ERR(tfm_cmac);
3412 }
3413
3414 err = run_selftests(tfm_aes, tfm_cmac);
3415
3416 crypto_free_hash(tfm_cmac);
3417 crypto_free_blkcipher(tfm_aes);
3418
3419 return err;
3420}
3421
3422#endif