blob: 3a4333b5801af1f41676f7f76e0902b0201334de [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
Gustavo Padovan8c520a52012-05-23 04:04:22 -030023#include <linux/crypto.h>
24#include <linux/scatterlist.h>
25#include <crypto/b128ops.h>
26
Anderson Brigliaeb492e02011-06-09 18:50:40 -030027#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080030#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070031
Johan Hedberg3b191462014-06-06 10:50:15 +030032#include "ecc.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070033#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030034
Johan Hedbergc7a3d572014-12-01 22:03:16 +020035/* Low-level debug macros to be used for stuff that we don't want
36 * accidentially in dmesg, i.e. the values of the various crypto keys
37 * and the inputs & outputs of crypto functions.
38 */
39#ifdef DEBUG
40#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
41 ##__VA_ARGS__)
42#else
43#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
44 ##__VA_ARGS__)
45#endif
46
Johan Hedbergb28b4942014-09-05 22:19:55 +030047#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030048
Johan Hedberg3b191462014-06-06 10:50:15 +030049/* Keys which are not distributed with Secure Connections */
50#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
51
Marcel Holtmann17b02e62012-03-01 14:32:37 -080052#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030053
Johan Hedberg0edb14d2014-05-26 13:29:28 +030054#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \
55 0x1f : 0x07)
56#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020057
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030058/* Maximum message length that can be passed to aes_cmac */
59#define CMAC_MSG_MAX 80
60
Johan Hedberg533e35d2014-06-16 19:25:18 +030061enum {
62 SMP_FLAG_TK_VALID,
63 SMP_FLAG_CFM_PENDING,
64 SMP_FLAG_MITM_AUTH,
65 SMP_FLAG_COMPLETE,
66 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030067 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030068 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030069 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030070 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030071 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg02b05bd2014-10-26 21:19:10 +010072 SMP_FLAG_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030073};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030074
75struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030076 struct l2cap_conn *conn;
77 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030078 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030079
Johan Hedberg4bc58f52014-05-20 09:45:47 +030080 u8 preq[7]; /* SMP Pairing Request */
81 u8 prsp[7]; /* SMP Pairing Response */
82 u8 prnd[16]; /* SMP Pairing Random (local) */
83 u8 rrnd[16]; /* SMP Pairing Random (remote) */
84 u8 pcnf[16]; /* SMP Pairing Confirm */
85 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberga29b0732014-10-28 15:17:05 +010086 u8 rr[16];
Johan Hedberg4bc58f52014-05-20 09:45:47 +030087 u8 enc_key_size;
88 u8 remote_key_dist;
89 bdaddr_t id_addr;
90 u8 id_addr_type;
91 u8 irk[16];
92 struct smp_csrk *csrk;
93 struct smp_csrk *slave_csrk;
94 struct smp_ltk *ltk;
95 struct smp_ltk *slave_ltk;
96 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +030097 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +030098 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +030099 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300100 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300101
Johan Hedberg3b191462014-06-06 10:50:15 +0300102 /* Secure Connections variables */
103 u8 local_pk[64];
104 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300105 u8 remote_pk[64];
106 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300107 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300108
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300109 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300110 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300111};
112
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300113/* These debug key values are defined in the SMP section of the core
114 * specification. debug_pk is the public debug key and debug_sk the
115 * private debug key.
116 */
117static const u8 debug_pk[64] = {
118 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
119 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
120 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
121 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
122
123 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
124 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
125 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
126 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
127};
128
129static const u8 debug_sk[32] = {
130 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
131 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
132 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
133 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
134};
135
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300136static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300137{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300138 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300139
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300140 for (i = 0; i < len; i++)
141 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300142}
143
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200144/* The following functions map to the LE SC SMP crypto functions
145 * AES-CMAC, f4, f5, f6, g2 and h6.
146 */
147
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300148static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
149 size_t len, u8 mac[16])
150{
151 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
152 struct hash_desc desc;
153 struct scatterlist sg;
154 int err;
155
156 if (len > CMAC_MSG_MAX)
157 return -EFBIG;
158
159 if (!tfm) {
160 BT_ERR("tfm %p", tfm);
161 return -EINVAL;
162 }
163
164 desc.tfm = tfm;
165 desc.flags = 0;
166
167 crypto_hash_init(&desc);
168
169 /* Swap key and message from LSB to MSB */
170 swap_buf(k, tmp, 16);
171 swap_buf(m, msg_msb, len);
172
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200173 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
174 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300175
176 err = crypto_hash_setkey(tfm, tmp, 16);
177 if (err) {
178 BT_ERR("cipher setkey failed: %d", err);
179 return err;
180 }
181
182 sg_init_one(&sg, msg_msb, len);
183
184 err = crypto_hash_update(&desc, &sg, len);
185 if (err) {
186 BT_ERR("Hash update error %d", err);
187 return err;
188 }
189
190 err = crypto_hash_final(&desc, mac_msb);
191 if (err) {
192 BT_ERR("Hash final error %d", err);
193 return err;
194 }
195
196 swap_buf(mac_msb, mac, 16);
197
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200198 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300199
200 return 0;
201}
202
203static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
204 const u8 x[16], u8 z, u8 res[16])
205{
206 u8 m[65];
207 int err;
208
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200209 SMP_DBG("u %32phN", u);
210 SMP_DBG("v %32phN", v);
211 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300212
213 m[0] = z;
214 memcpy(m + 1, v, 32);
215 memcpy(m + 33, u, 32);
216
217 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
218 if (err)
219 return err;
220
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200221 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300222
223 return err;
224}
225
Johan Hedberg4da50de2014-12-29 12:04:10 +0200226static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32],
227 const u8 n1[16], const u8 n2[16], const u8 a1[7],
228 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300229{
230 /* The btle, salt and length "magic" values are as defined in
231 * the SMP section of the Bluetooth core specification. In ASCII
232 * the btle value ends up being 'btle'. The salt is just a
233 * random number whereas length is the value 256 in little
234 * endian format.
235 */
236 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
237 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
238 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
239 const u8 length[2] = { 0x00, 0x01 };
240 u8 m[53], t[16];
241 int err;
242
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200243 SMP_DBG("w %32phN", w);
244 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
245 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300246
247 err = aes_cmac(tfm_cmac, salt, w, 32, t);
248 if (err)
249 return err;
250
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200251 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300252
253 memcpy(m, length, 2);
254 memcpy(m + 2, a2, 7);
255 memcpy(m + 9, a1, 7);
256 memcpy(m + 16, n2, 16);
257 memcpy(m + 32, n1, 16);
258 memcpy(m + 48, btle, 4);
259
260 m[52] = 0; /* Counter */
261
262 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
263 if (err)
264 return err;
265
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200266 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300267
268 m[52] = 1; /* Counter */
269
270 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
271 if (err)
272 return err;
273
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200274 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300275
276 return 0;
277}
278
279static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200280 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300281 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
282 u8 res[16])
283{
284 u8 m[65];
285 int err;
286
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200287 SMP_DBG("w %16phN", w);
288 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
289 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300290
291 memcpy(m, a2, 7);
292 memcpy(m + 7, a1, 7);
293 memcpy(m + 14, io_cap, 3);
294 memcpy(m + 17, r, 16);
295 memcpy(m + 33, n2, 16);
296 memcpy(m + 49, n1, 16);
297
298 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
299 if (err)
300 return err;
301
302 BT_DBG("res %16phN", res);
303
304 return err;
305}
306
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300307static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
308 const u8 x[16], const u8 y[16], u32 *val)
309{
310 u8 m[80], tmp[16];
311 int err;
312
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200313 SMP_DBG("u %32phN", u);
314 SMP_DBG("v %32phN", v);
315 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300316
317 memcpy(m, y, 16);
318 memcpy(m + 16, v, 32);
319 memcpy(m + 48, u, 32);
320
321 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
322 if (err)
323 return err;
324
325 *val = get_unaligned_le32(tmp);
326 *val %= 1000000;
327
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200328 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300329
330 return 0;
331}
332
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200333static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
334 const u8 key_id[4], u8 res[16])
335{
336 int err;
337
338 SMP_DBG("w %16phN key_id %4phN", w, key_id);
339
340 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
341 if (err)
342 return err;
343
344 SMP_DBG("res %16phN", res);
345
346 return err;
347}
348
349/* The following functions map to the legacy SMP crypto functions e, c1,
350 * s1 and ah.
351 */
352
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300353static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
354{
355 struct blkcipher_desc desc;
356 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200357 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200358 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300359
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200360 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300361 BT_ERR("tfm %p", tfm);
362 return -EINVAL;
363 }
364
365 desc.tfm = tfm;
366 desc.flags = 0;
367
Johan Hedberg943a7322014-03-18 12:58:24 +0200368 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300369 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200370
371 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300372 if (err) {
373 BT_ERR("cipher setkey failed: %d", err);
374 return err;
375 }
376
Johan Hedberg943a7322014-03-18 12:58:24 +0200377 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300378 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200379
380 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300381
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300382 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
383 if (err)
384 BT_ERR("Encrypt data error %d", err);
385
Johan Hedberg943a7322014-03-18 12:58:24 +0200386 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300387 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200388
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300389 return err;
390}
391
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200392static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
393 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
394 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
395{
396 u8 p1[16], p2[16];
397 int err;
398
399 memset(p1, 0, 16);
400
401 /* p1 = pres || preq || _rat || _iat */
402 p1[0] = _iat;
403 p1[1] = _rat;
404 memcpy(p1 + 2, preq, 7);
405 memcpy(p1 + 9, pres, 7);
406
407 /* p2 = padding || ia || ra */
408 memcpy(p2, ra, 6);
409 memcpy(p2 + 6, ia, 6);
410 memset(p2 + 12, 0, 4);
411
412 /* res = r XOR p1 */
413 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
414
415 /* res = e(k, res) */
416 err = smp_e(tfm_aes, k, res);
417 if (err) {
418 BT_ERR("Encrypt data error");
419 return err;
420 }
421
422 /* res = res XOR p2 */
423 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
424
425 /* res = e(k, res) */
426 err = smp_e(tfm_aes, k, res);
427 if (err)
428 BT_ERR("Encrypt data error");
429
430 return err;
431}
432
433static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
434 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300435{
436 int err;
437
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200438 /* Just least significant octets from r1 and r2 are considered */
439 memcpy(_r, r2, 8);
440 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300441
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200442 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300443 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200444 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300445
446 return err;
447}
448
Johan Hedbergcd082792014-12-02 13:37:41 +0200449static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
450 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200451{
Johan Hedberg943a7322014-03-18 12:58:24 +0200452 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200453 int err;
454
455 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200456 memcpy(_res, r, 3);
457 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200458
Johan Hedberg943a7322014-03-18 12:58:24 +0200459 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200460 if (err) {
461 BT_ERR("Encrypt error");
462 return err;
463 }
464
465 /* The output of the random address function ah is:
466 * ah(h, r) = e(k, r') mod 2^24
467 * The output of the security function e is then truncated to 24 bits
468 * by taking the least significant 24 bits of the output of e as the
469 * result of ah.
470 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200471 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200472
473 return 0;
474}
475
Johan Hedbergcd082792014-12-02 13:37:41 +0200476bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
477 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200478{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300479 struct l2cap_chan *chan = hdev->smp_data;
480 struct crypto_blkcipher *tfm;
Johan Hedberg60478052014-02-18 10:19:31 +0200481 u8 hash[3];
482 int err;
483
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300484 if (!chan || !chan->data)
485 return false;
486
487 tfm = chan->data;
488
Johan Hedberg60478052014-02-18 10:19:31 +0200489 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
490
491 err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
492 if (err)
493 return false;
494
495 return !memcmp(bdaddr->b, hash, 3);
496}
497
Johan Hedbergcd082792014-12-02 13:37:41 +0200498int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200499{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300500 struct l2cap_chan *chan = hdev->smp_data;
501 struct crypto_blkcipher *tfm;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200502 int err;
503
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300504 if (!chan || !chan->data)
505 return -EOPNOTSUPP;
506
507 tfm = chan->data;
508
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200509 get_random_bytes(&rpa->b[3], 3);
510
511 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
512 rpa->b[5] |= 0x40; /* Set second most significant bit */
513
514 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
515 if (err < 0)
516 return err;
517
518 BT_DBG("RPA %pMR", rpa);
519
520 return 0;
521}
522
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300523static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
524{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300525 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300526 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300527 struct kvec iv[2];
528 struct msghdr msg;
529
530 if (!chan)
531 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300532
533 BT_DBG("code 0x%2.2x", code);
534
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300535 iv[0].iov_base = &code;
536 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300537
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300538 iv[1].iov_base = data;
539 iv[1].iov_len = len;
540
541 memset(&msg, 0, sizeof(msg));
542
Al Viro17836392014-11-24 17:07:38 -0500543 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300544
545 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300546
Johan Hedbergb68fda62014-08-11 22:06:40 +0300547 if (!chan->data)
548 return;
549
550 smp = chan->data;
551
552 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300553 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300554}
555
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300556static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800557{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300558 if (authreq & SMP_AUTH_MITM) {
559 if (authreq & SMP_AUTH_SC)
560 return BT_SECURITY_FIPS;
561 else
562 return BT_SECURITY_HIGH;
563 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800564 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300565 }
Brian Gix2b64d152011-12-21 16:12:12 -0800566}
567
568static __u8 seclevel_to_authreq(__u8 sec_level)
569{
570 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300571 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800572 case BT_SECURITY_HIGH:
573 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
574 case BT_SECURITY_MEDIUM:
575 return SMP_AUTH_BONDING;
576 default:
577 return SMP_AUTH_NONE;
578 }
579}
580
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300581static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700582 struct smp_cmd_pairing *req,
583 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300584{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300585 struct l2cap_chan *chan = conn->smp;
586 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200587 struct hci_conn *hcon = conn->hcon;
588 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100589 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300590
Johan Hedbergb6ae8452014-07-30 09:22:22 +0300591 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700592 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
593 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300594 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800595 } else {
596 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300597 }
598
Johan Hedbergfd349c02014-02-18 10:19:36 +0200599 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
600 remote_dist |= SMP_DIST_ID_KEY;
601
Johan Hedberg863efaf2014-02-22 19:06:32 +0200602 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
603 local_dist |= SMP_DIST_ID_KEY;
604
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100605 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) &&
606 (authreq & SMP_AUTH_SC)) {
607 struct oob_data *oob_data;
608 u8 bdaddr_type;
609
610 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300611 local_dist |= SMP_DIST_LINK_KEY;
612 remote_dist |= SMP_DIST_LINK_KEY;
613 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100614
615 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
616 bdaddr_type = BDADDR_LE_PUBLIC;
617 else
618 bdaddr_type = BDADDR_LE_RANDOM;
619
620 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
621 bdaddr_type);
622 if (oob_data) {
623 set_bit(SMP_FLAG_OOB, &smp->flags);
624 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100625 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100626 memcpy(smp->pcnf, oob_data->hash256, 16);
627 }
628
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300629 } else {
630 authreq &= ~SMP_AUTH_SC;
631 }
632
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300633 if (rsp == NULL) {
634 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100635 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300636 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200637 req->init_key_dist = local_dist;
638 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300639 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200640
641 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300642 return;
643 }
644
645 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100646 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300647 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200648 rsp->init_key_dist = req->init_key_dist & remote_dist;
649 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300650 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200651
652 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300653}
654
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300655static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
656{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300657 struct l2cap_chan *chan = conn->smp;
658 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300659
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300660 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700661 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300662 return SMP_ENC_KEY_SIZE;
663
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300664 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300665
666 return 0;
667}
668
Johan Hedberg6f48e262014-08-11 22:06:44 +0300669static void smp_chan_destroy(struct l2cap_conn *conn)
670{
671 struct l2cap_chan *chan = conn->smp;
672 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200673 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300674 bool complete;
675
676 BUG_ON(!smp);
677
678 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300679
Johan Hedberg6f48e262014-08-11 22:06:44 +0300680 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200681 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300682
683 kfree(smp->csrk);
684 kfree(smp->slave_csrk);
Johan Hedberg6a770832014-06-06 11:54:04 +0300685 kfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300686
687 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300688 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300689
Johan Hedberg923e2412014-12-03 12:43:39 +0200690 /* Ensure that we don't leave any debug key around if debug key
691 * support hasn't been explicitly enabled.
692 */
693 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
694 !test_bit(HCI_KEEP_DEBUG_KEYS, &hcon->hdev->dev_flags)) {
695 list_del_rcu(&smp->ltk->list);
696 kfree_rcu(smp->ltk, rcu);
697 smp->ltk = NULL;
698 }
699
Johan Hedberg6f48e262014-08-11 22:06:44 +0300700 /* If pairing failed clean up any keys we might have */
701 if (!complete) {
702 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200703 list_del_rcu(&smp->ltk->list);
704 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300705 }
706
707 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200708 list_del_rcu(&smp->slave_ltk->list);
709 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300710 }
711
712 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200713 list_del_rcu(&smp->remote_irk->list);
714 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300715 }
716 }
717
718 chan->data = NULL;
719 kfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200720 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300721}
722
Johan Hedberg84794e12013-11-06 11:24:57 +0200723static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800724{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200725 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300726 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200727
Johan Hedberg84794e12013-11-06 11:24:57 +0200728 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800729 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700730 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800731
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700732 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700733 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300734
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300735 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300736 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800737}
738
Brian Gix2b64d152011-12-21 16:12:12 -0800739#define JUST_WORKS 0x00
740#define JUST_CFM 0x01
741#define REQ_PASSKEY 0x02
742#define CFM_PASSKEY 0x03
743#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300744#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800745#define OVERLAP 0xFF
746
747static const u8 gen_method[5][5] = {
748 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
749 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
750 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
751 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
752 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
753};
754
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300755static const u8 sc_method[5][5] = {
756 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
757 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
758 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
759 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
760 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
761};
762
Johan Hedberg581370c2014-06-17 13:07:38 +0300763static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
764{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300765 /* If either side has unknown io_caps, use JUST_CFM (which gets
766 * converted later to JUST_WORKS if we're initiators.
767 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300768 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
769 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300770 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300771
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300772 if (test_bit(SMP_FLAG_SC, &smp->flags))
773 return sc_method[remote_io][local_io];
774
Johan Hedberg581370c2014-06-17 13:07:38 +0300775 return gen_method[remote_io][local_io];
776}
777
Brian Gix2b64d152011-12-21 16:12:12 -0800778static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
779 u8 local_io, u8 remote_io)
780{
781 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300782 struct l2cap_chan *chan = conn->smp;
783 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800784 u32 passkey = 0;
785 int ret = 0;
786
787 /* Initialize key for JUST WORKS */
788 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300789 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800790
791 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
792
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300793 /* If neither side wants MITM, either "just" confirm an incoming
794 * request or use just-works for outgoing ones. The JUST_CFM
795 * will be converted to JUST_WORKS if necessary later in this
796 * function. If either side has MITM look up the method from the
797 * table.
798 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300799 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300800 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800801 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300802 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800803
Johan Hedberga82505c2014-03-24 14:39:07 +0200804 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300805 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
806 &smp->flags))
807 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200808
Johan Hedberg02f3e252014-07-16 15:09:13 +0300809 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300810 if (smp->method == JUST_CFM &&
811 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
812 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300813
Brian Gix2b64d152011-12-21 16:12:12 -0800814 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300815 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300816 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800817 return 0;
818 }
819
820 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300821 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300822 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300823 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
824 hcon->pending_sec_level = BT_SECURITY_HIGH;
825 }
Brian Gix2b64d152011-12-21 16:12:12 -0800826
827 /* If both devices have Keyoard-Display I/O, the master
828 * Confirms and the slave Enters the passkey.
829 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300830 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300831 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300832 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800833 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300834 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800835 }
836
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200837 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300838 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200839 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800840 get_random_bytes(&passkey, sizeof(passkey));
841 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200842 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800843 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300844 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800845 }
846
Johan Hedberg783e0572014-05-31 18:48:26 +0300847 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700848 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200849 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300850 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200851 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
852 hcon->type, hcon->dst_type,
853 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800854 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200855 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200856 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200857 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800858
Brian Gix2b64d152011-12-21 16:12:12 -0800859 return ret;
860}
861
Johan Hedberg1cc61142014-05-20 09:45:52 +0300862static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300863{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300864 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300865 struct smp_cmd_pairing_confirm cp;
866 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300867
868 BT_DBG("conn %p", conn);
869
Johan Hedberge491eaf2014-10-25 21:15:37 +0200870 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200871 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200872 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
873 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300874 if (ret)
875 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300876
Johan Hedberg4a74d652014-05-20 09:45:50 +0300877 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800878
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300879 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
880
Johan Hedbergb28b4942014-09-05 22:19:55 +0300881 if (conn->hcon->out)
882 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
883 else
884 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
885
Johan Hedberg1cc61142014-05-20 09:45:52 +0300886 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300887}
888
Johan Hedberg861580a2014-05-20 09:45:51 +0300889static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300890{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300891 struct l2cap_conn *conn = smp->conn;
892 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300893 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300894 int ret;
895
Johan Hedbergec70f362014-06-27 14:23:04 +0300896 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300897 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300898
899 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
900
Johan Hedberge491eaf2014-10-25 21:15:37 +0200901 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200902 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200903 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300904 if (ret)
905 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300906
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300907 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
908 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300909 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300910 }
911
912 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800913 u8 stk[16];
914 __le64 rand = 0;
915 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300916
Johan Hedberge491eaf2014-10-25 21:15:37 +0200917 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300918
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300919 memset(stk + smp->enc_key_size, 0,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300920 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300921
Johan Hedberg861580a2014-05-20 09:45:51 +0300922 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
923 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300924
925 hci_le_start_enc(hcon, ediv, rand, stk);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300926 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +0300927 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300928 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +0300929 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800930 __le64 rand = 0;
931 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300932
Johan Hedberg943a7322014-03-18 12:58:24 +0200933 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
934 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300935
Johan Hedberge491eaf2014-10-25 21:15:37 +0200936 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300937
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300938 memset(stk + smp->enc_key_size, 0,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700939 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300940
Johan Hedbergfff34902014-06-10 15:19:50 +0300941 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
942 auth = 1;
943 else
944 auth = 0;
945
Johan Hedberg7d5843b2014-06-16 19:25:15 +0300946 /* Even though there's no _SLAVE suffix this is the
947 * slave STK we're adding for later lookup (the master
948 * STK never needs to be stored).
949 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700950 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +0300951 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300952 }
953
Johan Hedberg861580a2014-05-20 09:45:51 +0300954 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300955}
956
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300957static void smp_notify_keys(struct l2cap_conn *conn)
958{
959 struct l2cap_chan *chan = conn->smp;
960 struct smp_chan *smp = chan->data;
961 struct hci_conn *hcon = conn->hcon;
962 struct hci_dev *hdev = hcon->hdev;
963 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
964 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
965 bool persistent;
966
967 if (smp->remote_irk) {
968 mgmt_new_irk(hdev, smp->remote_irk);
969 /* Now that user space can be considered to know the
970 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300971 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300972 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300973 if (hcon->type == LE_LINK) {
974 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
975 hcon->dst_type = smp->remote_irk->addr_type;
976 queue_work(hdev->workqueue, &conn->id_addr_update_work);
977 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300978
979 /* When receiving an indentity resolving key for
980 * a remote device that does not use a resolvable
981 * private address, just remove the key so that
982 * it is possible to use the controller white
983 * list for scanning.
984 *
985 * Userspace will have been told to not store
986 * this key at this point. So it is safe to
987 * just remove it.
988 */
989 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200990 list_del_rcu(&smp->remote_irk->list);
991 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +0300992 smp->remote_irk = NULL;
993 }
994 }
995
Johan Hedbergb5ae3442014-08-14 12:34:26 +0300996 if (hcon->type == ACL_LINK) {
997 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
998 persistent = false;
999 else
1000 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1001 &hcon->flags);
1002 } else {
1003 /* The LTKs and CSRKs should be persistent only if both sides
1004 * had the bonding bit set in their authentication requests.
1005 */
1006 persistent = !!((req->auth_req & rsp->auth_req) &
1007 SMP_AUTH_BONDING);
1008 }
1009
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001010
1011 if (smp->csrk) {
1012 smp->csrk->bdaddr_type = hcon->dst_type;
1013 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1014 mgmt_new_csrk(hdev, smp->csrk, persistent);
1015 }
1016
1017 if (smp->slave_csrk) {
1018 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1019 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1020 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1021 }
1022
1023 if (smp->ltk) {
1024 smp->ltk->bdaddr_type = hcon->dst_type;
1025 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1026 mgmt_new_ltk(hdev, smp->ltk, persistent);
1027 }
1028
1029 if (smp->slave_ltk) {
1030 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1031 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1032 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1033 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001034
1035 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001036 struct link_key *key;
1037 u8 type;
1038
1039 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1040 type = HCI_LK_DEBUG_COMBINATION;
1041 else if (hcon->sec_level == BT_SECURITY_FIPS)
1042 type = HCI_LK_AUTH_COMBINATION_P256;
1043 else
1044 type = HCI_LK_UNAUTH_COMBINATION_P256;
1045
1046 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1047 smp->link_key, type, 0, &persistent);
1048 if (key) {
1049 mgmt_new_link_key(hdev, key, persistent);
1050
1051 /* Don't keep debug keys around if the relevant
1052 * flag is not set.
1053 */
1054 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) &&
1055 key->type == HCI_LK_DEBUG_COMBINATION) {
1056 list_del_rcu(&key->list);
1057 kfree_rcu(key, rcu);
1058 }
1059 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001060 }
1061}
1062
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001063static void sc_add_ltk(struct smp_chan *smp)
1064{
1065 struct hci_conn *hcon = smp->conn->hcon;
1066 u8 key_type, auth;
1067
1068 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1069 key_type = SMP_LTK_P256_DEBUG;
1070 else
1071 key_type = SMP_LTK_P256;
1072
1073 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1074 auth = 1;
1075 else
1076 auth = 0;
1077
1078 memset(smp->tk + smp->enc_key_size, 0,
1079 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
1080
1081 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1082 key_type, auth, smp->tk, smp->enc_key_size,
1083 0, 0);
1084}
1085
Johan Hedberg6a770832014-06-06 11:54:04 +03001086static void sc_generate_link_key(struct smp_chan *smp)
1087{
1088 /* These constants are as specified in the core specification.
1089 * In ASCII they spell out to 'tmp1' and 'lebr'.
1090 */
1091 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1092 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1093
1094 smp->link_key = kzalloc(16, GFP_KERNEL);
1095 if (!smp->link_key)
1096 return;
1097
1098 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1099 kfree(smp->link_key);
1100 smp->link_key = NULL;
1101 return;
1102 }
1103
1104 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1105 kfree(smp->link_key);
1106 smp->link_key = NULL;
1107 return;
1108 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001109}
1110
Johan Hedbergb28b4942014-09-05 22:19:55 +03001111static void smp_allow_key_dist(struct smp_chan *smp)
1112{
1113 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1114 * will be allowed in each PDU handler to ensure we receive
1115 * them in the correct order.
1116 */
1117 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1118 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1119 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1120 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1121 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1122 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1123}
1124
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001125static void sc_generate_ltk(struct smp_chan *smp)
1126{
1127 /* These constants are as specified in the core specification.
1128 * In ASCII they spell out to 'tmp2' and 'brle'.
1129 */
1130 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1131 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1132 struct hci_conn *hcon = smp->conn->hcon;
1133 struct hci_dev *hdev = hcon->hdev;
1134 struct link_key *key;
1135
1136 key = hci_find_link_key(hdev, &hcon->dst);
1137 if (!key) {
1138 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1139 return;
1140 }
1141
1142 if (key->type == HCI_LK_DEBUG_COMBINATION)
1143 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1144
1145 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1146 return;
1147
1148 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1149 return;
1150
1151 sc_add_ltk(smp);
1152}
1153
Johan Hedbergd6268e82014-09-05 22:19:51 +03001154static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001155{
1156 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001157 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001158 struct hci_conn *hcon = conn->hcon;
1159 struct hci_dev *hdev = hcon->hdev;
1160 __u8 *keydist;
1161
1162 BT_DBG("conn %p", conn);
1163
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001164 rsp = (void *) &smp->prsp[1];
1165
1166 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001167 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1168 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001169 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001170 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001171
1172 req = (void *) &smp->preq[1];
1173
1174 if (hcon->out) {
1175 keydist = &rsp->init_key_dist;
1176 *keydist &= req->init_key_dist;
1177 } else {
1178 keydist = &rsp->resp_key_dist;
1179 *keydist &= req->resp_key_dist;
1180 }
1181
Johan Hedberg6a770832014-06-06 11:54:04 +03001182 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001183 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001184 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001185 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1186 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001187
1188 /* Clear the keys which are generated but not distributed */
1189 *keydist &= ~SMP_SC_NO_DIST;
1190 }
1191
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001192 BT_DBG("keydist 0x%x", *keydist);
1193
1194 if (*keydist & SMP_DIST_ENC_KEY) {
1195 struct smp_cmd_encrypt_info enc;
1196 struct smp_cmd_master_ident ident;
1197 struct smp_ltk *ltk;
1198 u8 authenticated;
1199 __le16 ediv;
1200 __le64 rand;
1201
1202 get_random_bytes(enc.ltk, sizeof(enc.ltk));
1203 get_random_bytes(&ediv, sizeof(ediv));
1204 get_random_bytes(&rand, sizeof(rand));
1205
1206 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1207
1208 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1209 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1210 SMP_LTK_SLAVE, authenticated, enc.ltk,
1211 smp->enc_key_size, ediv, rand);
1212 smp->slave_ltk = ltk;
1213
1214 ident.ediv = ediv;
1215 ident.rand = rand;
1216
1217 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1218
1219 *keydist &= ~SMP_DIST_ENC_KEY;
1220 }
1221
1222 if (*keydist & SMP_DIST_ID_KEY) {
1223 struct smp_cmd_ident_addr_info addrinfo;
1224 struct smp_cmd_ident_info idinfo;
1225
1226 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1227
1228 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1229
1230 /* The hci_conn contains the local identity address
1231 * after the connection has been established.
1232 *
1233 * This is true even when the connection has been
1234 * established using a resolvable random address.
1235 */
1236 bacpy(&addrinfo.bdaddr, &hcon->src);
1237 addrinfo.addr_type = hcon->src_type;
1238
1239 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1240 &addrinfo);
1241
1242 *keydist &= ~SMP_DIST_ID_KEY;
1243 }
1244
1245 if (*keydist & SMP_DIST_SIGN) {
1246 struct smp_cmd_sign_info sign;
1247 struct smp_csrk *csrk;
1248
1249 /* Generate a new random key */
1250 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1251
1252 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1253 if (csrk) {
1254 csrk->master = 0x00;
1255 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1256 }
1257 smp->slave_csrk = csrk;
1258
1259 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1260
1261 *keydist &= ~SMP_DIST_SIGN;
1262 }
1263
1264 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001265 if (smp->remote_key_dist & KEY_DIST_MASK) {
1266 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001267 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001268 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001269
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001270 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1271 smp_notify_keys(conn);
1272
1273 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001274}
1275
Johan Hedbergb68fda62014-08-11 22:06:40 +03001276static void smp_timeout(struct work_struct *work)
1277{
1278 struct smp_chan *smp = container_of(work, struct smp_chan,
1279 security_timer.work);
1280 struct l2cap_conn *conn = smp->conn;
1281
1282 BT_DBG("conn %p", conn);
1283
Johan Hedberg1e91c292014-08-18 20:33:29 +03001284 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001285}
1286
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001287static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1288{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001289 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001290 struct smp_chan *smp;
1291
Marcel Holtmannf1560462013-10-13 05:43:25 -07001292 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001293 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001294 return NULL;
1295
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001296 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1297 if (IS_ERR(smp->tfm_aes)) {
1298 BT_ERR("Unable to create ECB crypto context");
1299 kfree(smp);
1300 return NULL;
1301 }
1302
Johan Hedberg407cecf2014-05-02 14:19:47 +03001303 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1304 if (IS_ERR(smp->tfm_cmac)) {
1305 BT_ERR("Unable to create CMAC crypto context");
1306 crypto_free_blkcipher(smp->tfm_aes);
1307 kfree(smp);
1308 return NULL;
1309 }
1310
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001311 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001312 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001313
Johan Hedbergb28b4942014-09-05 22:19:55 +03001314 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1315
Johan Hedbergb68fda62014-08-11 22:06:40 +03001316 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1317
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001318 hci_conn_hold(conn->hcon);
1319
1320 return smp;
1321}
1322
Johan Hedberg760b0182014-06-06 11:44:05 +03001323static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1324{
1325 struct hci_conn *hcon = smp->conn->hcon;
1326 u8 *na, *nb, a[7], b[7];
1327
1328 if (hcon->out) {
1329 na = smp->prnd;
1330 nb = smp->rrnd;
1331 } else {
1332 na = smp->rrnd;
1333 nb = smp->prnd;
1334 }
1335
1336 memcpy(a, &hcon->init_addr, 6);
1337 memcpy(b, &hcon->resp_addr, 6);
1338 a[6] = hcon->init_addr_type;
1339 b[6] = hcon->resp_addr_type;
1340
1341 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1342}
1343
Johan Hedberg38606f12014-06-25 11:10:28 +03001344static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001345{
1346 struct hci_conn *hcon = smp->conn->hcon;
1347 struct smp_cmd_dhkey_check check;
1348 u8 a[7], b[7], *local_addr, *remote_addr;
1349 u8 io_cap[3], r[16];
1350
Johan Hedberg760b0182014-06-06 11:44:05 +03001351 memcpy(a, &hcon->init_addr, 6);
1352 memcpy(b, &hcon->resp_addr, 6);
1353 a[6] = hcon->init_addr_type;
1354 b[6] = hcon->resp_addr_type;
1355
1356 if (hcon->out) {
1357 local_addr = a;
1358 remote_addr = b;
1359 memcpy(io_cap, &smp->preq[1], 3);
1360 } else {
1361 local_addr = b;
1362 remote_addr = a;
1363 memcpy(io_cap, &smp->prsp[1], 3);
1364 }
1365
Johan Hedbergdddd3052014-06-01 15:38:09 +03001366 memset(r, 0, sizeof(r));
1367
1368 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001369 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001370
Johan Hedberga29b0732014-10-28 15:17:05 +01001371 if (smp->method == REQ_OOB)
1372 memcpy(r, smp->rr, 16);
1373
Johan Hedberg760b0182014-06-06 11:44:05 +03001374 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1375 local_addr, remote_addr, check.e);
1376
1377 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001378}
1379
Johan Hedberg38606f12014-06-25 11:10:28 +03001380static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1381{
1382 struct l2cap_conn *conn = smp->conn;
1383 struct hci_conn *hcon = conn->hcon;
1384 struct smp_cmd_pairing_confirm cfm;
1385 u8 r;
1386
1387 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1388 r |= 0x80;
1389
1390 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1391
1392 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1393 cfm.confirm_val))
1394 return SMP_UNSPECIFIED;
1395
1396 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1397
1398 return 0;
1399}
1400
1401static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1402{
1403 struct l2cap_conn *conn = smp->conn;
1404 struct hci_conn *hcon = conn->hcon;
1405 struct hci_dev *hdev = hcon->hdev;
1406 u8 cfm[16], r;
1407
1408 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1409 if (smp->passkey_round >= 20)
1410 return 0;
1411
1412 switch (smp_op) {
1413 case SMP_CMD_PAIRING_RANDOM:
1414 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1415 r |= 0x80;
1416
1417 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1418 smp->rrnd, r, cfm))
1419 return SMP_UNSPECIFIED;
1420
1421 if (memcmp(smp->pcnf, cfm, 16))
1422 return SMP_CONFIRM_FAILED;
1423
1424 smp->passkey_round++;
1425
1426 if (smp->passkey_round == 20) {
1427 /* Generate MacKey and LTK */
1428 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1429 return SMP_UNSPECIFIED;
1430 }
1431
1432 /* The round is only complete when the initiator
1433 * receives pairing random.
1434 */
1435 if (!hcon->out) {
1436 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1437 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001438 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001439 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001440 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001441 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001442 return 0;
1443 }
1444
1445 /* Start the next round */
1446 if (smp->passkey_round != 20)
1447 return sc_passkey_round(smp, 0);
1448
1449 /* Passkey rounds are complete - start DHKey Check */
1450 sc_dhkey_check(smp);
1451 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1452
1453 break;
1454
1455 case SMP_CMD_PAIRING_CONFIRM:
1456 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1457 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1458 return 0;
1459 }
1460
1461 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1462
1463 if (hcon->out) {
1464 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1465 sizeof(smp->prnd), smp->prnd);
1466 return 0;
1467 }
1468
1469 return sc_passkey_send_confirm(smp);
1470
1471 case SMP_CMD_PUBLIC_KEY:
1472 default:
1473 /* Initiating device starts the round */
1474 if (!hcon->out)
1475 return 0;
1476
1477 BT_DBG("%s Starting passkey round %u", hdev->name,
1478 smp->passkey_round + 1);
1479
1480 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1481
1482 return sc_passkey_send_confirm(smp);
1483 }
1484
1485 return 0;
1486}
1487
Johan Hedbergdddd3052014-06-01 15:38:09 +03001488static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1489{
Johan Hedberg38606f12014-06-25 11:10:28 +03001490 struct l2cap_conn *conn = smp->conn;
1491 struct hci_conn *hcon = conn->hcon;
1492 u8 smp_op;
1493
1494 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1495
Johan Hedbergdddd3052014-06-01 15:38:09 +03001496 switch (mgmt_op) {
1497 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1498 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1499 return 0;
1500 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1501 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1502 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001503 case MGMT_OP_USER_PASSKEY_REPLY:
1504 hcon->passkey_notify = le32_to_cpu(passkey);
1505 smp->passkey_round = 0;
1506
1507 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1508 smp_op = SMP_CMD_PAIRING_CONFIRM;
1509 else
1510 smp_op = 0;
1511
1512 if (sc_passkey_round(smp, smp_op))
1513 return -EIO;
1514
1515 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001516 }
1517
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001518 /* Initiator sends DHKey check first */
1519 if (hcon->out) {
1520 sc_dhkey_check(smp);
1521 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1522 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1523 sc_dhkey_check(smp);
1524 sc_add_ltk(smp);
1525 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001526
1527 return 0;
1528}
1529
Brian Gix2b64d152011-12-21 16:12:12 -08001530int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1531{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001532 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001533 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001534 struct smp_chan *smp;
1535 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001536 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001537
1538 BT_DBG("");
1539
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001540 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001541 return -ENOTCONN;
1542
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001543 chan = conn->smp;
1544 if (!chan)
1545 return -ENOTCONN;
1546
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001547 l2cap_chan_lock(chan);
1548 if (!chan->data) {
1549 err = -ENOTCONN;
1550 goto unlock;
1551 }
1552
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001553 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001554
Johan Hedberg760b0182014-06-06 11:44:05 +03001555 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1556 err = sc_user_reply(smp, mgmt_op, passkey);
1557 goto unlock;
1558 }
1559
Brian Gix2b64d152011-12-21 16:12:12 -08001560 switch (mgmt_op) {
1561 case MGMT_OP_USER_PASSKEY_REPLY:
1562 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001563 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001564 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001565 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001566 /* Fall Through */
1567 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001568 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001569 break;
1570 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1571 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001572 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001573 err = 0;
1574 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001575 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001576 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001577 err = -EOPNOTSUPP;
1578 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001579 }
1580
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001581 err = 0;
1582
Brian Gix2b64d152011-12-21 16:12:12 -08001583 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001584 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1585 u8 rsp = smp_confirm(smp);
1586 if (rsp)
1587 smp_failure(conn, rsp);
1588 }
Brian Gix2b64d152011-12-21 16:12:12 -08001589
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001590unlock:
1591 l2cap_chan_unlock(chan);
1592 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001593}
1594
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001595static void build_bredr_pairing_cmd(struct smp_chan *smp,
1596 struct smp_cmd_pairing *req,
1597 struct smp_cmd_pairing *rsp)
1598{
1599 struct l2cap_conn *conn = smp->conn;
1600 struct hci_dev *hdev = conn->hcon->hdev;
1601 u8 local_dist = 0, remote_dist = 0;
1602
1603 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) {
1604 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1605 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1606 }
1607
1608 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags))
1609 remote_dist |= SMP_DIST_ID_KEY;
1610
1611 if (test_bit(HCI_PRIVACY, &hdev->dev_flags))
1612 local_dist |= SMP_DIST_ID_KEY;
1613
1614 if (!rsp) {
1615 memset(req, 0, sizeof(*req));
1616
1617 req->init_key_dist = local_dist;
1618 req->resp_key_dist = remote_dist;
1619 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1620
1621 smp->remote_key_dist = remote_dist;
1622
1623 return;
1624 }
1625
1626 memset(rsp, 0, sizeof(*rsp));
1627
1628 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1629 rsp->init_key_dist = req->init_key_dist & remote_dist;
1630 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1631
1632 smp->remote_key_dist = rsp->init_key_dist;
1633}
1634
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001635static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001636{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001637 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001638 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001639 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001640 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001641 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001642 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001643
1644 BT_DBG("conn %p", conn);
1645
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001646 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001647 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001648
Johan Hedberg40bef302014-07-16 11:42:27 +03001649 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001650 return SMP_CMD_NOTSUPP;
1651
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001652 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001653 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001654 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001655 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001656
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001657 if (!smp)
1658 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001659
Johan Hedbergc05b9332014-09-10 17:37:42 -07001660 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001661 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001662
Johan Hedbergb6ae8452014-07-30 09:22:22 +03001663 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001664 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001665 return SMP_PAIRING_NOTSUPP;
1666
Johan Hedberg903b71c2014-09-08 16:59:18 -07001667 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1668 return SMP_AUTH_REQUIREMENTS;
1669
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001670 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1671 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001672 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001673
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001674 /* SMP over BR/EDR requires special treatment */
1675 if (conn->hcon->type == ACL_LINK) {
1676 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001677 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
1678 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001679 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1680
1681 set_bit(SMP_FLAG_SC, &smp->flags);
1682
1683 build_bredr_pairing_cmd(smp, req, &rsp);
1684
1685 key_size = min(req->max_key_size, rsp.max_key_size);
1686 if (check_enc_key_size(conn, key_size))
1687 return SMP_ENC_KEY_SIZE;
1688
1689 /* Clear bits which are generated but not distributed */
1690 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1691
1692 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1693 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1694 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1695
1696 smp_distribute_keys(smp);
1697 return 0;
1698 }
1699
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001700 build_pairing_cmd(conn, req, &rsp, auth);
1701
1702 if (rsp.auth_req & SMP_AUTH_SC)
1703 set_bit(SMP_FLAG_SC, &smp->flags);
1704
Johan Hedberg5be5e272014-09-10 17:58:54 -07001705 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001706 sec_level = BT_SECURITY_MEDIUM;
1707 else
1708 sec_level = authreq_to_seclevel(auth);
1709
Johan Hedbergc7262e72014-06-17 13:07:37 +03001710 if (sec_level > conn->hcon->pending_sec_level)
1711 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001712
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001713 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001714 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1715 u8 method;
1716
1717 method = get_auth_method(smp, conn->hcon->io_capability,
1718 req->io_capability);
1719 if (method == JUST_WORKS || method == JUST_CFM)
1720 return SMP_AUTH_REQUIREMENTS;
1721 }
1722
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001723 key_size = min(req->max_key_size, rsp.max_key_size);
1724 if (check_enc_key_size(conn, key_size))
1725 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001726
Johan Hedberge84a6b12013-12-02 10:49:03 +02001727 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001728
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001729 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1730 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001731
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001732 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001733
1734 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1735
1736 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1737 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1738 /* Clear bits which are generated but not distributed */
1739 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1740 /* Wait for Public Key from Initiating Device */
1741 return 0;
1742 } else {
1743 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1744 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001745
Brian Gix2b64d152011-12-21 16:12:12 -08001746 /* Request setup of TK */
1747 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1748 if (ret)
1749 return SMP_UNSPECIFIED;
1750
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001751 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001752}
1753
Johan Hedberg3b191462014-06-06 10:50:15 +03001754static u8 sc_send_public_key(struct smp_chan *smp)
1755{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001756 struct hci_dev *hdev = smp->conn->hcon->hdev;
1757
Johan Hedberg3b191462014-06-06 10:50:15 +03001758 BT_DBG("");
1759
Johan Hedberg70157ef2014-06-24 15:22:59 +03001760 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) {
1761 BT_DBG("Using debug keys");
1762 memcpy(smp->local_pk, debug_pk, 64);
1763 memcpy(smp->local_sk, debug_sk, 32);
1764 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1765 } else {
1766 while (true) {
1767 /* Generate local key pair for Secure Connections */
1768 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1769 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001770
Johan Hedberg70157ef2014-06-24 15:22:59 +03001771 /* This is unlikely, but we need to check that
1772 * we didn't accidentially generate a debug key.
1773 */
1774 if (memcmp(smp->local_sk, debug_sk, 32))
1775 break;
1776 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001777 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001778
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001779 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1780 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]);
1781 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001782
1783 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1784
1785 return 0;
1786}
1787
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001788static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001789{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001790 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001791 struct l2cap_chan *chan = conn->smp;
1792 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001793 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001794 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001795 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001796
1797 BT_DBG("conn %p", conn);
1798
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001799 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001800 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001801
Johan Hedberg40bef302014-07-16 11:42:27 +03001802 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001803 return SMP_CMD_NOTSUPP;
1804
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001805 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001806
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001807 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001808
1809 key_size = min(req->max_key_size, rsp->max_key_size);
1810 if (check_enc_key_size(conn, key_size))
1811 return SMP_ENC_KEY_SIZE;
1812
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001813 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001814
Johan Hedberg903b71c2014-09-08 16:59:18 -07001815 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
1816 return SMP_AUTH_REQUIREMENTS;
1817
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001818 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1819 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1820
1821 /* Update remote key distribution in case the remote cleared
1822 * some bits that we had enabled in our request.
1823 */
1824 smp->remote_key_dist &= rsp->resp_key_dist;
1825
1826 /* For BR/EDR this means we're done and can start phase 3 */
1827 if (conn->hcon->type == ACL_LINK) {
1828 /* Clear bits which are generated but not distributed */
1829 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1830 smp_distribute_keys(smp);
1831 return 0;
1832 }
1833
Johan Hedberg65668772014-05-16 11:03:34 +03001834 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1835 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001836 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1837 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001838
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001839 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001840 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1841 u8 method;
1842
1843 method = get_auth_method(smp, req->io_capability,
1844 rsp->io_capability);
1845 if (method == JUST_WORKS || method == JUST_CFM)
1846 return SMP_AUTH_REQUIREMENTS;
1847 }
1848
Johan Hedberge84a6b12013-12-02 10:49:03 +02001849 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001850
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001851 /* Update remote key distribution in case the remote cleared
1852 * some bits that we had enabled in our request.
1853 */
1854 smp->remote_key_dist &= rsp->resp_key_dist;
1855
Johan Hedberg3b191462014-06-06 10:50:15 +03001856 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1857 /* Clear bits which are generated but not distributed */
1858 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1859 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1860 return sc_send_public_key(smp);
1861 }
1862
Johan Hedbergc05b9332014-09-10 17:37:42 -07001863 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001864
Johan Hedberg476585e2012-06-06 18:54:15 +08001865 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001866 if (ret)
1867 return SMP_UNSPECIFIED;
1868
Johan Hedberg4a74d652014-05-20 09:45:50 +03001869 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001870
1871 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001872 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001873 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001874
1875 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001876}
1877
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001878static u8 sc_check_confirm(struct smp_chan *smp)
1879{
1880 struct l2cap_conn *conn = smp->conn;
1881
1882 BT_DBG("");
1883
1884 /* Public Key exchange must happen before any other steps */
1885 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1886 return SMP_UNSPECIFIED;
1887
Johan Hedberg38606f12014-06-25 11:10:28 +03001888 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1889 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1890
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001891 if (conn->hcon->out) {
1892 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1893 smp->prnd);
1894 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1895 }
1896
1897 return 0;
1898}
1899
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001900static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001901{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001902 struct l2cap_chan *chan = conn->smp;
1903 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001904
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001905 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
1906
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001907 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001908 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001909
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001910 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1911 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001912
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001913 if (test_bit(SMP_FLAG_SC, &smp->flags))
1914 return sc_check_confirm(smp);
1915
Johan Hedbergb28b4942014-09-05 22:19:55 +03001916 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02001917 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1918 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03001919 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1920 return 0;
1921 }
1922
1923 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001924 return smp_confirm(smp);
Johan Hedberg943a7322014-03-18 12:58:24 +02001925 else
Johan Hedberg4a74d652014-05-20 09:45:50 +03001926 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001927
1928 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001929}
1930
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001931static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001932{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001933 struct l2cap_chan *chan = conn->smp;
1934 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001935 struct hci_conn *hcon = conn->hcon;
1936 u8 *pkax, *pkbx, *na, *nb;
1937 u32 passkey;
1938 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001939
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001940 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001941
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001942 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001943 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001944
Johan Hedberg943a7322014-03-18 12:58:24 +02001945 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001946 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001947
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001948 if (!test_bit(SMP_FLAG_SC, &smp->flags))
1949 return smp_random(smp);
1950
Johan Hedberg580039e2014-12-03 16:26:37 +02001951 if (hcon->out) {
1952 pkax = smp->local_pk;
1953 pkbx = smp->remote_pk;
1954 na = smp->prnd;
1955 nb = smp->rrnd;
1956 } else {
1957 pkax = smp->remote_pk;
1958 pkbx = smp->local_pk;
1959 na = smp->rrnd;
1960 nb = smp->prnd;
1961 }
1962
Johan Hedberga29b0732014-10-28 15:17:05 +01001963 if (smp->method == REQ_OOB) {
1964 if (!hcon->out)
1965 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1966 sizeof(smp->prnd), smp->prnd);
1967 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1968 goto mackey_and_ltk;
1969 }
1970
Johan Hedberg38606f12014-06-25 11:10:28 +03001971 /* Passkey entry has special treatment */
1972 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1973 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
1974
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001975 if (hcon->out) {
1976 u8 cfm[16];
1977
1978 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1979 smp->rrnd, 0, cfm);
1980 if (err)
1981 return SMP_UNSPECIFIED;
1982
1983 if (memcmp(smp->pcnf, cfm, 16))
1984 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001985 } else {
1986 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1987 smp->prnd);
1988 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03001989 }
1990
Johan Hedberga29b0732014-10-28 15:17:05 +01001991mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03001992 /* Generate MacKey and LTK */
1993 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
1994 if (err)
1995 return SMP_UNSPECIFIED;
1996
Johan Hedberga29b0732014-10-28 15:17:05 +01001997 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03001998 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03001999 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002000 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2001 }
2002 return 0;
2003 }
2004
Johan Hedberg38606f12014-06-25 11:10:28 +03002005 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002006 if (err)
2007 return SMP_UNSPECIFIED;
2008
Johan Hedberg38606f12014-06-25 11:10:28 +03002009 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2010 hcon->dst_type, passkey, 0);
2011 if (err)
2012 return SMP_UNSPECIFIED;
2013
2014 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2015
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002016 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002017}
2018
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002019static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002020{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002021 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002022 struct hci_conn *hcon = conn->hcon;
2023
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002024 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002025 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002026 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002027
Johan Hedberga6f78332014-09-10 17:37:45 -07002028 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002029 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002030
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002031 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002032 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002033
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002034 hci_le_start_enc(hcon, key->ediv, key->rand, key->val);
2035 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002036
Johan Hedbergfe59a052014-07-01 19:14:12 +03002037 /* We never store STKs for master role, so clear this flag */
2038 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2039
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002040 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002041}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002042
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002043bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2044 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002045{
2046 if (sec_level == BT_SECURITY_LOW)
2047 return true;
2048
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002049 /* If we're encrypted with an STK but the caller prefers using
2050 * LTK claim insufficient security. This way we allow the
2051 * connection to be re-encrypted with an LTK, even if the LTK
2052 * provides the same level of security. Only exception is if we
2053 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002054 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002055 if (key_pref == SMP_USE_LTK &&
2056 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002057 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002058 return false;
2059
Johan Hedberg854f4722014-07-01 18:40:20 +03002060 if (hcon->sec_level >= sec_level)
2061 return true;
2062
2063 return false;
2064}
2065
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002066static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002067{
2068 struct smp_cmd_security_req *rp = (void *) skb->data;
2069 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002070 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002071 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002072 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002073 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002074
2075 BT_DBG("conn %p", conn);
2076
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002077 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002078 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002079
Johan Hedberg40bef302014-07-16 11:42:27 +03002080 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002081 return SMP_CMD_NOTSUPP;
2082
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002083 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002084
Johan Hedberg903b71c2014-09-08 16:59:18 -07002085 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC))
2086 return SMP_AUTH_REQUIREMENTS;
2087
Johan Hedberg5be5e272014-09-10 17:58:54 -07002088 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002089 sec_level = BT_SECURITY_MEDIUM;
2090 else
2091 sec_level = authreq_to_seclevel(auth);
2092
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002093 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002094 return 0;
2095
Johan Hedbergc7262e72014-06-17 13:07:37 +03002096 if (sec_level > hcon->pending_sec_level)
2097 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002098
Johan Hedberg4dab7862012-06-07 14:58:37 +08002099 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002100 return 0;
2101
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002102 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002103 if (!smp)
2104 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002105
Johan Hedbergb6ae8452014-07-30 09:22:22 +03002106 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002107 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002108 return SMP_PAIRING_NOTSUPP;
2109
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002110 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002111
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002112 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002113 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002114
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002115 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2116 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002117
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002118 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002119 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002120
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002121 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002122}
2123
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002124int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002125{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002126 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002127 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002128 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002129 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002130 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002131
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002132 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2133
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002134 /* This may be NULL if there's an unexpected disconnection */
2135 if (!conn)
2136 return 1;
2137
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002138 chan = conn->smp;
2139
Johan Hedberg757aee02013-04-24 13:05:32 +03002140 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002141 return 1;
2142
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002143 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002144 return 1;
2145
Johan Hedbergc7262e72014-06-17 13:07:37 +03002146 if (sec_level > hcon->pending_sec_level)
2147 hcon->pending_sec_level = sec_level;
2148
Johan Hedberg40bef302014-07-16 11:42:27 +03002149 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002150 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2151 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002152
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002153 l2cap_chan_lock(chan);
2154
2155 /* If SMP is already in progress ignore this request */
2156 if (chan->data) {
2157 ret = 0;
2158 goto unlock;
2159 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002160
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002161 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002162 if (!smp) {
2163 ret = 1;
2164 goto unlock;
2165 }
Brian Gix2b64d152011-12-21 16:12:12 -08002166
2167 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002168
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002169 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags))
2170 authreq |= SMP_AUTH_SC;
2171
Johan Hedberg79897d22014-06-01 09:45:24 +03002172 /* Require MITM if IO Capability allows or the security level
2173 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002174 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002175 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002176 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002177 authreq |= SMP_AUTH_MITM;
2178
Johan Hedberg40bef302014-07-16 11:42:27 +03002179 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002180 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002181
Brian Gix2b64d152011-12-21 16:12:12 -08002182 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002183 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2184 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002185
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002186 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002187 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002188 } else {
2189 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002190 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002191 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002192 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002193 }
2194
Johan Hedberg4a74d652014-05-20 09:45:50 +03002195 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002196 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002197
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002198unlock:
2199 l2cap_chan_unlock(chan);
2200 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002201}
2202
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002203static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2204{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002205 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002206 struct l2cap_chan *chan = conn->smp;
2207 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002208
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002209 BT_DBG("conn %p", conn);
2210
2211 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002212 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002213
Johan Hedbergb28b4942014-09-05 22:19:55 +03002214 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002215
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002216 skb_pull(skb, sizeof(*rp));
2217
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002218 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002219
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002220 return 0;
2221}
2222
2223static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2224{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002225 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002226 struct l2cap_chan *chan = conn->smp;
2227 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002228 struct hci_dev *hdev = conn->hcon->hdev;
2229 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002230 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002231 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002232
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002233 BT_DBG("conn %p", conn);
2234
2235 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002236 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002237
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002238 /* Mark the information as received */
2239 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2240
Johan Hedbergb28b4942014-09-05 22:19:55 +03002241 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2242 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002243 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2244 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002245
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002246 skb_pull(skb, sizeof(*rp));
2247
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002248 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002249 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002250 authenticated, smp->tk, smp->enc_key_size,
2251 rp->ediv, rp->rand);
2252 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002253 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002254 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002255
2256 return 0;
2257}
2258
Johan Hedbergfd349c02014-02-18 10:19:36 +02002259static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2260{
2261 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002262 struct l2cap_chan *chan = conn->smp;
2263 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002264
2265 BT_DBG("");
2266
2267 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002268 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002269
Johan Hedbergb28b4942014-09-05 22:19:55 +03002270 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002271
Johan Hedbergfd349c02014-02-18 10:19:36 +02002272 skb_pull(skb, sizeof(*info));
2273
2274 memcpy(smp->irk, info->irk, 16);
2275
2276 return 0;
2277}
2278
2279static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2280 struct sk_buff *skb)
2281{
2282 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002283 struct l2cap_chan *chan = conn->smp;
2284 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002285 struct hci_conn *hcon = conn->hcon;
2286 bdaddr_t rpa;
2287
2288 BT_DBG("");
2289
2290 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002291 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002292
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002293 /* Mark the information as received */
2294 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2295
Johan Hedbergb28b4942014-09-05 22:19:55 +03002296 if (smp->remote_key_dist & SMP_DIST_SIGN)
2297 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2298
Johan Hedbergfd349c02014-02-18 10:19:36 +02002299 skb_pull(skb, sizeof(*info));
2300
Johan Hedberga9a58f82014-02-25 22:24:37 +02002301 /* Strictly speaking the Core Specification (4.1) allows sending
2302 * an empty address which would force us to rely on just the IRK
2303 * as "identity information". However, since such
2304 * implementations are not known of and in order to not over
2305 * complicate our implementation, simply pretend that we never
2306 * received an IRK for such a device.
2307 */
2308 if (!bacmp(&info->bdaddr, BDADDR_ANY)) {
2309 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002310 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002311 }
2312
Johan Hedbergfd349c02014-02-18 10:19:36 +02002313 bacpy(&smp->id_addr, &info->bdaddr);
2314 smp->id_addr_type = info->addr_type;
2315
2316 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2317 bacpy(&rpa, &hcon->dst);
2318 else
2319 bacpy(&rpa, BDADDR_ANY);
2320
Johan Hedberg23d0e122014-02-19 14:57:46 +02002321 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2322 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002323
Johan Hedberg31dd6242014-06-27 14:23:02 +03002324distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002325 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2326 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002327
2328 return 0;
2329}
2330
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002331static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2332{
2333 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002334 struct l2cap_chan *chan = conn->smp;
2335 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002336 struct smp_csrk *csrk;
2337
2338 BT_DBG("conn %p", conn);
2339
2340 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002341 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002342
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002343 /* Mark the information as received */
2344 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2345
2346 skb_pull(skb, sizeof(*rp));
2347
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002348 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2349 if (csrk) {
2350 csrk->master = 0x01;
2351 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2352 }
2353 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002354 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002355
2356 return 0;
2357}
2358
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002359static u8 sc_select_method(struct smp_chan *smp)
2360{
2361 struct l2cap_conn *conn = smp->conn;
2362 struct hci_conn *hcon = conn->hcon;
2363 struct smp_cmd_pairing *local, *remote;
2364 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2365
Johan Hedberga29b0732014-10-28 15:17:05 +01002366 if (test_bit(SMP_FLAG_OOB, &smp->flags))
2367 return REQ_OOB;
2368
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002369 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2370 * which are needed as inputs to some crypto functions. To get
2371 * the "struct smp_cmd_pairing" from them we need to skip the
2372 * first byte which contains the opcode.
2373 */
2374 if (hcon->out) {
2375 local = (void *) &smp->preq[1];
2376 remote = (void *) &smp->prsp[1];
2377 } else {
2378 local = (void *) &smp->prsp[1];
2379 remote = (void *) &smp->preq[1];
2380 }
2381
2382 local_io = local->io_capability;
2383 remote_io = remote->io_capability;
2384
2385 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2386 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2387
2388 /* If either side wants MITM, look up the method from the table,
2389 * otherwise use JUST WORKS.
2390 */
2391 if (local_mitm || remote_mitm)
2392 method = get_auth_method(smp, local_io, remote_io);
2393 else
2394 method = JUST_WORKS;
2395
2396 /* Don't confirm locally initiated pairing attempts */
2397 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2398 method = JUST_WORKS;
2399
2400 return method;
2401}
2402
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002403static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2404{
2405 struct smp_cmd_public_key *key = (void *) skb->data;
2406 struct hci_conn *hcon = conn->hcon;
2407 struct l2cap_chan *chan = conn->smp;
2408 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002409 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002410 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002411 int err;
2412
2413 BT_DBG("conn %p", conn);
2414
2415 if (skb->len < sizeof(*key))
2416 return SMP_INVALID_PARAMS;
2417
2418 memcpy(smp->remote_pk, key, 64);
2419
2420 /* Non-initiating device sends its public key after receiving
2421 * the key from the initiating device.
2422 */
2423 if (!hcon->out) {
2424 err = sc_send_public_key(smp);
2425 if (err)
2426 return err;
2427 }
2428
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002429 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2430 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002431
2432 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2433 return SMP_UNSPECIFIED;
2434
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002435 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002436
2437 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2438
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002439 smp->method = sc_select_method(smp);
2440
2441 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2442
2443 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2444 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2445 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2446 else
2447 hcon->pending_sec_level = BT_SECURITY_FIPS;
2448
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002449 if (!memcmp(debug_pk, smp->remote_pk, 64))
2450 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2451
Johan Hedberg38606f12014-06-25 11:10:28 +03002452 if (smp->method == DSP_PASSKEY) {
2453 get_random_bytes(&hcon->passkey_notify,
2454 sizeof(hcon->passkey_notify));
2455 hcon->passkey_notify %= 1000000;
2456 hcon->passkey_entered = 0;
2457 smp->passkey_round = 0;
2458 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2459 hcon->dst_type,
2460 hcon->passkey_notify,
2461 hcon->passkey_entered))
2462 return SMP_UNSPECIFIED;
2463 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2464 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2465 }
2466
Johan Hedberga29b0732014-10-28 15:17:05 +01002467 if (smp->method == REQ_OOB) {
2468 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2469 smp->rr, 0, cfm.confirm_val);
2470 if (err)
2471 return SMP_UNSPECIFIED;
2472
2473 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2474 return SMP_CONFIRM_FAILED;
2475
2476 if (hcon->out)
2477 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2478 sizeof(smp->prnd), smp->prnd);
2479
2480 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2481
2482 return 0;
2483 }
2484
Johan Hedberg38606f12014-06-25 11:10:28 +03002485 if (hcon->out)
2486 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2487
2488 if (smp->method == REQ_PASSKEY) {
2489 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2490 hcon->dst_type))
2491 return SMP_UNSPECIFIED;
2492 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2493 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2494 return 0;
2495 }
2496
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002497 /* The Initiating device waits for the non-initiating device to
2498 * send the confirm value.
2499 */
2500 if (conn->hcon->out)
2501 return 0;
2502
2503 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2504 0, cfm.confirm_val);
2505 if (err)
2506 return SMP_UNSPECIFIED;
2507
2508 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2509 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2510
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002511 return 0;
2512}
2513
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002514static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2515{
2516 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2517 struct l2cap_chan *chan = conn->smp;
2518 struct hci_conn *hcon = conn->hcon;
2519 struct smp_chan *smp = chan->data;
2520 u8 a[7], b[7], *local_addr, *remote_addr;
2521 u8 io_cap[3], r[16], e[16];
2522 int err;
2523
2524 BT_DBG("conn %p", conn);
2525
2526 if (skb->len < sizeof(*check))
2527 return SMP_INVALID_PARAMS;
2528
2529 memcpy(a, &hcon->init_addr, 6);
2530 memcpy(b, &hcon->resp_addr, 6);
2531 a[6] = hcon->init_addr_type;
2532 b[6] = hcon->resp_addr_type;
2533
2534 if (hcon->out) {
2535 local_addr = a;
2536 remote_addr = b;
2537 memcpy(io_cap, &smp->prsp[1], 3);
2538 } else {
2539 local_addr = b;
2540 remote_addr = a;
2541 memcpy(io_cap, &smp->preq[1], 3);
2542 }
2543
2544 memset(r, 0, sizeof(r));
2545
Johan Hedberg38606f12014-06-25 11:10:28 +03002546 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2547 put_unaligned_le32(hcon->passkey_notify, r);
2548
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002549 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2550 io_cap, remote_addr, local_addr, e);
2551 if (err)
2552 return SMP_UNSPECIFIED;
2553
2554 if (memcmp(check->e, e, 16))
2555 return SMP_DHKEY_CHECK_FAILED;
2556
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002557 if (!hcon->out) {
2558 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2559 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2560 return 0;
2561 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002562
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002563 /* Slave sends DHKey check as response to master */
2564 sc_dhkey_check(smp);
2565 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002566
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002567 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002568
2569 if (hcon->out) {
2570 hci_le_start_enc(hcon, 0, 0, smp->tk);
2571 hcon->enc_key_size = smp->enc_key_size;
2572 }
2573
2574 return 0;
2575}
2576
Johan Hedberg1408bb62014-06-04 22:45:57 +03002577static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2578 struct sk_buff *skb)
2579{
2580 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2581
2582 BT_DBG("value 0x%02x", kp->value);
2583
2584 return 0;
2585}
2586
Johan Hedberg4befb862014-08-11 22:06:38 +03002587static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002588{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002589 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002590 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002591 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002592 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002593 int err = 0;
2594
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002595 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002596 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002597
Marcel Holtmann06ae3312013-10-18 03:43:00 -07002598 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002599 reason = SMP_PAIRING_NOTSUPP;
2600 goto done;
2601 }
2602
Marcel Holtmann92381f52013-10-03 01:23:08 -07002603 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002604 skb_pull(skb, sizeof(code));
2605
Johan Hedbergb28b4942014-09-05 22:19:55 +03002606 smp = chan->data;
2607
2608 if (code > SMP_CMD_MAX)
2609 goto drop;
2610
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002611 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002612 goto drop;
2613
2614 /* If we don't have a context the only allowed commands are
2615 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002616 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002617 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2618 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002619
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002620 switch (code) {
2621 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002622 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002623 break;
2624
2625 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002626 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002627 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002628 break;
2629
2630 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002631 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002632 break;
2633
2634 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002635 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002636 break;
2637
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002638 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002639 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002640 break;
2641
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002642 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002643 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002644 break;
2645
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002646 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002647 reason = smp_cmd_encrypt_info(conn, skb);
2648 break;
2649
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002650 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002651 reason = smp_cmd_master_ident(conn, skb);
2652 break;
2653
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002654 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002655 reason = smp_cmd_ident_info(conn, skb);
2656 break;
2657
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002658 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002659 reason = smp_cmd_ident_addr_info(conn, skb);
2660 break;
2661
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002662 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002663 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002664 break;
2665
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002666 case SMP_CMD_PUBLIC_KEY:
2667 reason = smp_cmd_public_key(conn, skb);
2668 break;
2669
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002670 case SMP_CMD_DHKEY_CHECK:
2671 reason = smp_cmd_dhkey_check(conn, skb);
2672 break;
2673
Johan Hedberg1408bb62014-06-04 22:45:57 +03002674 case SMP_CMD_KEYPRESS_NOTIFY:
2675 reason = smp_cmd_keypress_notify(conn, skb);
2676 break;
2677
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002678 default:
2679 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002680 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002681 goto done;
2682 }
2683
2684done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002685 if (!err) {
2686 if (reason)
2687 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002688 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002689 }
2690
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002691 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002692
2693drop:
2694 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2695 code, &hcon->dst);
2696 kfree_skb(skb);
2697 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002698}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002699
Johan Hedberg70db83c2014-08-08 09:37:16 +03002700static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2701{
2702 struct l2cap_conn *conn = chan->conn;
2703
2704 BT_DBG("chan %p", chan);
2705
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002706 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002707 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002708
Johan Hedberg70db83c2014-08-08 09:37:16 +03002709 conn->smp = NULL;
2710 l2cap_chan_put(chan);
2711}
2712
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002713static void bredr_pairing(struct l2cap_chan *chan)
2714{
2715 struct l2cap_conn *conn = chan->conn;
2716 struct hci_conn *hcon = conn->hcon;
2717 struct hci_dev *hdev = hcon->hdev;
2718 struct smp_cmd_pairing req;
2719 struct smp_chan *smp;
2720
2721 BT_DBG("chan %p", chan);
2722
2723 /* Only new pairings are interesting */
2724 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2725 return;
2726
2727 /* Don't bother if we're not encrypted */
2728 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2729 return;
2730
2731 /* Only master may initiate SMP over BR/EDR */
2732 if (hcon->role != HCI_ROLE_MASTER)
2733 return;
2734
2735 /* Secure Connections support must be enabled */
2736 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags))
2737 return;
2738
2739 /* BR/EDR must use Secure Connections for SMP */
2740 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
2741 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2742 return;
2743
2744 /* If our LE support is not enabled don't do anything */
2745 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
2746 return;
2747
2748 /* Don't bother if remote LE support is not enabled */
2749 if (!lmp_host_le_capable(hcon))
2750 return;
2751
2752 /* Remote must support SMP fixed chan for BR/EDR */
2753 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2754 return;
2755
2756 /* Don't bother if SMP is already ongoing */
2757 if (chan->data)
2758 return;
2759
2760 smp = smp_chan_create(conn);
2761 if (!smp) {
2762 BT_ERR("%s unable to create SMP context for BR/EDR",
2763 hdev->name);
2764 return;
2765 }
2766
2767 set_bit(SMP_FLAG_SC, &smp->flags);
2768
2769 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2770
2771 /* Prepare and send the BR/EDR SMP Pairing Request */
2772 build_bredr_pairing_cmd(smp, &req, NULL);
2773
2774 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2775 memcpy(&smp->preq[1], &req, sizeof(req));
2776
2777 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2778 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2779}
2780
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002781static void smp_resume_cb(struct l2cap_chan *chan)
2782{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002783 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002784 struct l2cap_conn *conn = chan->conn;
2785 struct hci_conn *hcon = conn->hcon;
2786
2787 BT_DBG("chan %p", chan);
2788
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002789 if (hcon->type == ACL_LINK) {
2790 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002791 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002792 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002793
Johan Hedberg86d14072014-08-11 22:06:43 +03002794 if (!smp)
2795 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002796
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002797 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2798 return;
2799
Johan Hedberg86d14072014-08-11 22:06:43 +03002800 cancel_delayed_work(&smp->security_timer);
2801
Johan Hedbergd6268e82014-09-05 22:19:51 +03002802 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002803}
2804
Johan Hedberg70db83c2014-08-08 09:37:16 +03002805static void smp_ready_cb(struct l2cap_chan *chan)
2806{
2807 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002808 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002809
2810 BT_DBG("chan %p", chan);
2811
2812 conn->smp = chan;
2813 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002814
2815 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2816 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002817}
2818
Johan Hedberg4befb862014-08-11 22:06:38 +03002819static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
2820{
2821 int err;
2822
2823 BT_DBG("chan %p", chan);
2824
2825 err = smp_sig_channel(chan, skb);
2826 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03002827 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03002828
Johan Hedbergb68fda62014-08-11 22:06:40 +03002829 if (smp)
2830 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03002831
Johan Hedberg1e91c292014-08-18 20:33:29 +03002832 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03002833 }
2834
2835 return err;
2836}
2837
Johan Hedberg70db83c2014-08-08 09:37:16 +03002838static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2839 unsigned long hdr_len,
2840 unsigned long len, int nb)
2841{
2842 struct sk_buff *skb;
2843
2844 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
2845 if (!skb)
2846 return ERR_PTR(-ENOMEM);
2847
2848 skb->priority = HCI_PRIO_MAX;
2849 bt_cb(skb)->chan = chan;
2850
2851 return skb;
2852}
2853
2854static const struct l2cap_ops smp_chan_ops = {
2855 .name = "Security Manager",
2856 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002857 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002858 .alloc_skb = smp_alloc_skb_cb,
2859 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002860 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002861
2862 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002863 .state_change = l2cap_chan_no_state_change,
2864 .close = l2cap_chan_no_close,
2865 .defer = l2cap_chan_no_defer,
2866 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002867 .set_shutdown = l2cap_chan_no_set_shutdown,
2868 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002869};
2870
2871static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
2872{
2873 struct l2cap_chan *chan;
2874
2875 BT_DBG("pchan %p", pchan);
2876
2877 chan = l2cap_chan_create();
2878 if (!chan)
2879 return NULL;
2880
2881 chan->chan_type = pchan->chan_type;
2882 chan->ops = &smp_chan_ops;
2883 chan->scid = pchan->scid;
2884 chan->dcid = chan->scid;
2885 chan->imtu = pchan->imtu;
2886 chan->omtu = pchan->omtu;
2887 chan->mode = pchan->mode;
2888
Johan Hedbergabe84902014-11-12 22:22:21 +02002889 /* Other L2CAP channels may request SMP routines in order to
2890 * change the security level. This means that the SMP channel
2891 * lock must be considered in its own category to avoid lockdep
2892 * warnings.
2893 */
2894 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
2895
Johan Hedberg70db83c2014-08-08 09:37:16 +03002896 BT_DBG("created chan %p", chan);
2897
2898 return chan;
2899}
2900
2901static const struct l2cap_ops smp_root_chan_ops = {
2902 .name = "Security Manager Root",
2903 .new_connection = smp_new_conn_cb,
2904
2905 /* None of these are implemented for the root channel */
2906 .close = l2cap_chan_no_close,
2907 .alloc_skb = l2cap_chan_no_alloc_skb,
2908 .recv = l2cap_chan_no_recv,
2909 .state_change = l2cap_chan_no_state_change,
2910 .teardown = l2cap_chan_no_teardown,
2911 .ready = l2cap_chan_no_ready,
2912 .defer = l2cap_chan_no_defer,
2913 .suspend = l2cap_chan_no_suspend,
2914 .resume = l2cap_chan_no_resume,
2915 .set_shutdown = l2cap_chan_no_set_shutdown,
2916 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03002917};
2918
Johan Hedbergef8efe42014-08-13 15:12:32 +03002919static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002920{
Johan Hedberg70db83c2014-08-08 09:37:16 +03002921 struct l2cap_chan *chan;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002922 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002923
Johan Hedbergef8efe42014-08-13 15:12:32 +03002924 if (cid == L2CAP_CID_SMP_BREDR) {
2925 tfm_aes = NULL;
2926 goto create_chan;
2927 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03002928
Johan Hedbergadae20c2014-11-13 14:37:48 +02002929 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002930 if (IS_ERR(tfm_aes)) {
Johan Hedberg711eafe2014-08-08 09:32:52 +03002931 BT_ERR("Unable to create crypto context");
Fengguang Wufe700772014-12-08 03:04:38 +08002932 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002933 }
2934
Johan Hedbergef8efe42014-08-13 15:12:32 +03002935create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03002936 chan = l2cap_chan_create();
2937 if (!chan) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002938 crypto_free_blkcipher(tfm_aes);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002939 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002940 }
2941
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002942 chan->data = tfm_aes;
2943
Johan Hedbergef8efe42014-08-13 15:12:32 +03002944 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002945
2946 l2cap_chan_set_defaults(chan);
2947
2948 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002949 if (cid == L2CAP_CID_SMP)
2950 chan->src_type = BDADDR_LE_PUBLIC;
2951 else
2952 chan->src_type = BDADDR_BREDR;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002953 chan->state = BT_LISTEN;
2954 chan->mode = L2CAP_MODE_BASIC;
2955 chan->imtu = L2CAP_DEFAULT_MTU;
2956 chan->ops = &smp_root_chan_ops;
2957
Johan Hedbergabe84902014-11-12 22:22:21 +02002958 /* Set correct nesting level for a parent/listening channel */
2959 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
2960
Johan Hedbergef8efe42014-08-13 15:12:32 +03002961 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03002962}
2963
Johan Hedbergef8efe42014-08-13 15:12:32 +03002964static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03002965{
Johan Hedbergef8efe42014-08-13 15:12:32 +03002966 struct crypto_blkcipher *tfm_aes;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002967
Johan Hedbergef8efe42014-08-13 15:12:32 +03002968 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002969
Johan Hedbergdefce9e2014-08-08 09:37:17 +03002970 tfm_aes = chan->data;
2971 if (tfm_aes) {
2972 chan->data = NULL;
2973 crypto_free_blkcipher(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002974 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03002975
Johan Hedberg70db83c2014-08-08 09:37:16 +03002976 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03002977}
Johan Hedbergef8efe42014-08-13 15:12:32 +03002978
2979int smp_register(struct hci_dev *hdev)
2980{
2981 struct l2cap_chan *chan;
2982
2983 BT_DBG("%s", hdev->name);
2984
2985 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
2986 if (IS_ERR(chan))
2987 return PTR_ERR(chan);
2988
2989 hdev->smp_data = chan;
2990
2991 if (!lmp_sc_capable(hdev) &&
2992 !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags))
2993 return 0;
2994
2995 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
2996 if (IS_ERR(chan)) {
2997 int err = PTR_ERR(chan);
2998 chan = hdev->smp_data;
2999 hdev->smp_data = NULL;
3000 smp_del_chan(chan);
3001 return err;
3002 }
3003
3004 hdev->smp_bredr_data = chan;
3005
3006 return 0;
3007}
3008
3009void smp_unregister(struct hci_dev *hdev)
3010{
3011 struct l2cap_chan *chan;
3012
3013 if (hdev->smp_bredr_data) {
3014 chan = hdev->smp_bredr_data;
3015 hdev->smp_bredr_data = NULL;
3016 smp_del_chan(chan);
3017 }
3018
3019 if (hdev->smp_data) {
3020 chan = hdev->smp_data;
3021 hdev->smp_data = NULL;
3022 smp_del_chan(chan);
3023 }
3024}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003025
3026#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3027
Johan Hedbergcfc41982014-12-30 09:50:40 +02003028static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3029{
3030 const u8 irk[16] = {
3031 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3032 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3033 const u8 r[3] = { 0x94, 0x81, 0x70 };
3034 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3035 u8 res[3];
3036 int err;
3037
3038 err = smp_ah(tfm_aes, irk, r, res);
3039 if (err)
3040 return err;
3041
3042 if (memcmp(res, exp, 3))
3043 return -EINVAL;
3044
3045 return 0;
3046}
3047
3048static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3049{
3050 const u8 k[16] = {
3051 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3052 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3053 const u8 r[16] = {
3054 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3055 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3056 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3057 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3058 const u8 _iat = 0x01;
3059 const u8 _rat = 0x00;
3060 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3061 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3062 const u8 exp[16] = {
3063 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3064 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3065 u8 res[16];
3066 int err;
3067
3068 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3069 if (err)
3070 return err;
3071
3072 if (memcmp(res, exp, 16))
3073 return -EINVAL;
3074
3075 return 0;
3076}
3077
3078static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3079{
3080 const u8 k[16] = {
3081 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3082 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3083 const u8 r1[16] = {
3084 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3085 const u8 r2[16] = {
3086 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3087 const u8 exp[16] = {
3088 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3089 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3090 u8 res[16];
3091 int err;
3092
3093 err = smp_s1(tfm_aes, k, r1, r2, res);
3094 if (err)
3095 return err;
3096
3097 if (memcmp(res, exp, 16))
3098 return -EINVAL;
3099
3100 return 0;
3101}
3102
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003103static int __init test_f4(struct crypto_hash *tfm_cmac)
3104{
3105 const u8 u[32] = {
3106 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3107 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3108 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3109 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3110 const u8 v[32] = {
3111 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3112 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3113 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3114 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3115 const u8 x[16] = {
3116 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3117 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3118 const u8 z = 0x00;
3119 const u8 exp[16] = {
3120 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3121 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3122 u8 res[16];
3123 int err;
3124
3125 err = smp_f4(tfm_cmac, u, v, x, z, res);
3126 if (err)
3127 return err;
3128
3129 if (memcmp(res, exp, 16))
3130 return -EINVAL;
3131
3132 return 0;
3133}
3134
3135static int __init test_f5(struct crypto_hash *tfm_cmac)
3136{
3137 const u8 w[32] = {
3138 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3139 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3140 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3141 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3142 const u8 n1[16] = {
3143 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3144 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3145 const u8 n2[16] = {
3146 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3147 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3148 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3149 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3150 const u8 exp_ltk[16] = {
3151 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3152 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3153 const u8 exp_mackey[16] = {
3154 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3155 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3156 u8 mackey[16], ltk[16];
3157 int err;
3158
3159 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3160 if (err)
3161 return err;
3162
3163 if (memcmp(mackey, exp_mackey, 16))
3164 return -EINVAL;
3165
3166 if (memcmp(ltk, exp_ltk, 16))
3167 return -EINVAL;
3168
3169 return 0;
3170}
3171
3172static int __init test_f6(struct crypto_hash *tfm_cmac)
3173{
3174 const u8 w[16] = {
3175 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3176 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3177 const u8 n1[16] = {
3178 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3179 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3180 const u8 n2[16] = {
3181 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3182 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3183 const u8 r[16] = {
3184 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3185 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3186 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3187 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3188 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3189 const u8 exp[16] = {
3190 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3191 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3192 u8 res[16];
3193 int err;
3194
3195 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, 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_g2(struct crypto_hash *tfm_cmac)
3206{
3207 const u8 u[32] = {
3208 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3209 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3210 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3211 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3212 const u8 v[32] = {
3213 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3214 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3215 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3216 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3217 const u8 x[16] = {
3218 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3219 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3220 const u8 y[16] = {
3221 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3222 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3223 const u32 exp_val = 0x2f9ed5ba % 1000000;
3224 u32 val;
3225 int err;
3226
3227 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3228 if (err)
3229 return err;
3230
3231 if (val != exp_val)
3232 return -EINVAL;
3233
3234 return 0;
3235}
3236
3237static int __init test_h6(struct crypto_hash *tfm_cmac)
3238{
3239 const u8 w[16] = {
3240 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3241 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3242 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3243 const u8 exp[16] = {
3244 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3245 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3246 u8 res[16];
3247 int err;
3248
3249 err = smp_h6(tfm_cmac, w, key_id, res);
3250 if (err)
3251 return err;
3252
3253 if (memcmp(res, exp, 16))
3254 return -EINVAL;
3255
3256 return 0;
3257}
3258
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003259static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3260 struct crypto_hash *tfm_cmac)
3261{
Johan Hedbergcfc41982014-12-30 09:50:40 +02003262 int err;
3263
3264 err = test_ah(tfm_aes);
3265 if (err) {
3266 BT_ERR("smp_ah test failed");
3267 return err;
3268 }
3269
3270 err = test_c1(tfm_aes);
3271 if (err) {
3272 BT_ERR("smp_c1 test failed");
3273 return err;
3274 }
3275
3276 err = test_s1(tfm_aes);
3277 if (err) {
3278 BT_ERR("smp_s1 test failed");
3279 return err;
3280 }
3281
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003282 err = test_f4(tfm_cmac);
3283 if (err) {
3284 BT_ERR("smp_f4 test failed");
3285 return err;
3286 }
3287
3288 err = test_f5(tfm_cmac);
3289 if (err) {
3290 BT_ERR("smp_f5 test failed");
3291 return err;
3292 }
3293
3294 err = test_f6(tfm_cmac);
3295 if (err) {
3296 BT_ERR("smp_f6 test failed");
3297 return err;
3298 }
3299
3300 err = test_g2(tfm_cmac);
3301 if (err) {
3302 BT_ERR("smp_g2 test failed");
3303 return err;
3304 }
3305
3306 err = test_h6(tfm_cmac);
3307 if (err) {
3308 BT_ERR("smp_h6 test failed");
3309 return err;
3310 }
3311
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003312 BT_INFO("SMP test passed");
3313
3314 return 0;
3315}
3316
3317int __init bt_selftest_smp(void)
3318{
3319 struct crypto_blkcipher *tfm_aes;
3320 struct crypto_hash *tfm_cmac;
3321 int err;
3322
3323 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3324 if (IS_ERR(tfm_aes)) {
3325 BT_ERR("Unable to create ECB crypto context");
3326 return PTR_ERR(tfm_aes);
3327 }
3328
3329 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3330 if (IS_ERR(tfm_cmac)) {
3331 BT_ERR("Unable to create CMAC crypto context");
3332 crypto_free_blkcipher(tfm_aes);
3333 return PTR_ERR(tfm_cmac);
3334 }
3335
3336 err = run_selftests(tfm_aes, tfm_cmac);
3337
3338 crypto_free_hash(tfm_cmac);
3339 crypto_free_blkcipher(tfm_aes);
3340
3341 return err;
3342}
3343
3344#endif