2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
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;
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.
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.
23 #include <linux/debugfs.h>
24 #include <linux/scatterlist.h>
25 #include <linux/crypto.h>
26 #include <crypto/b128ops.h>
27 #include <crypto/hash.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31 #include <net/bluetooth/l2cap.h>
32 #include <net/bluetooth/mgmt.h>
34 #include "ecdh_helper.h"
37 #define SMP_DEV(hdev) \
38 ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data)
40 /* Low-level debug macros to be used for stuff that we don't want
41 * accidentially in dmesg, i.e. the values of the various crypto keys
42 * and the inputs & outputs of crypto functions.
45 #define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
48 #define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
52 #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
54 /* Keys which are not distributed with Secure Connections */
55 #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
57 #define SMP_TIMEOUT msecs_to_jiffies(30000)
59 #define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
61 #define KEY_DIST_MASK 0x07
63 /* Maximum message length that can be passed to aes_cmac */
64 #define CMAC_MSG_MAX 80
76 SMP_FLAG_DHKEY_PENDING
,
83 /* Secure Connections OOB data */
92 struct crypto_cipher
*tfm_aes
;
93 struct crypto_shash
*tfm_cmac
;
97 struct l2cap_conn
*conn
;
98 struct delayed_work security_timer
;
99 unsigned long allow_cmd
; /* Bitmask of allowed commands */
101 u8 preq
[7]; /* SMP Pairing Request */
102 u8 prsp
[7]; /* SMP Pairing Response */
103 u8 prnd
[16]; /* SMP Pairing Random (local) */
104 u8 rrnd
[16]; /* SMP Pairing Random (remote) */
105 u8 pcnf
[16]; /* SMP Pairing Confirm */
106 u8 tk
[16]; /* SMP Temporary Key */
107 u8 rr
[16]; /* Remote OOB ra/rb value */
108 u8 lr
[16]; /* Local OOB ra/rb value */
114 struct smp_csrk
*csrk
;
115 struct smp_csrk
*slave_csrk
;
117 struct smp_ltk
*slave_ltk
;
118 struct smp_irk
*remote_irk
;
124 /* Secure Connections variables */
131 struct crypto_cipher
*tfm_aes
;
132 struct crypto_shash
*tfm_cmac
;
135 /* These debug key values are defined in the SMP section of the core
136 * specification. debug_pk is the public debug key and debug_sk the
139 static const u8 debug_pk
[64] = {
140 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
141 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
142 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
143 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
145 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
146 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
147 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
148 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
151 static const u8 debug_sk
[32] = {
152 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
153 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
154 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
155 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
158 static inline void swap_buf(const u8
*src
, u8
*dst
, size_t len
)
162 for (i
= 0; i
< len
; i
++)
163 dst
[len
- 1 - i
] = src
[i
];
166 /* The following functions map to the LE SC SMP crypto functions
167 * AES-CMAC, f4, f5, f6, g2 and h6.
170 static int aes_cmac(struct crypto_shash
*tfm
, const u8 k
[16], const u8
*m
,
171 size_t len
, u8 mac
[16])
173 uint8_t tmp
[16], mac_msb
[16], msg_msb
[CMAC_MSG_MAX
];
174 SHASH_DESC_ON_STACK(desc
, tfm
);
177 if (len
> CMAC_MSG_MAX
)
181 BT_ERR("tfm %p", tfm
);
188 /* Swap key and message from LSB to MSB */
189 swap_buf(k
, tmp
, 16);
190 swap_buf(m
, msg_msb
, len
);
192 SMP_DBG("msg (len %zu) %*phN", len
, (int) len
, m
);
193 SMP_DBG("key %16phN", k
);
195 err
= crypto_shash_setkey(tfm
, tmp
, 16);
197 BT_ERR("cipher setkey failed: %d", err
);
201 err
= crypto_shash_digest(desc
, msg_msb
, len
, mac_msb
);
202 shash_desc_zero(desc
);
204 BT_ERR("Hash computation error %d", err
);
208 swap_buf(mac_msb
, mac
, 16);
210 SMP_DBG("mac %16phN", mac
);
215 static int smp_f4(struct crypto_shash
*tfm_cmac
, const u8 u
[32],
216 const u8 v
[32], const u8 x
[16], u8 z
, u8 res
[16])
221 SMP_DBG("u %32phN", u
);
222 SMP_DBG("v %32phN", v
);
223 SMP_DBG("x %16phN z %02x", x
, z
);
226 memcpy(m
+ 1, v
, 32);
227 memcpy(m
+ 33, u
, 32);
229 err
= aes_cmac(tfm_cmac
, x
, m
, sizeof(m
), res
);
233 SMP_DBG("res %16phN", res
);
238 static int smp_f5(struct crypto_shash
*tfm_cmac
, const u8 w
[32],
239 const u8 n1
[16], const u8 n2
[16], const u8 a1
[7],
240 const u8 a2
[7], u8 mackey
[16], u8 ltk
[16])
242 /* The btle, salt and length "magic" values are as defined in
243 * the SMP section of the Bluetooth core specification. In ASCII
244 * the btle value ends up being 'btle'. The salt is just a
245 * random number whereas length is the value 256 in little
248 const u8 btle
[4] = { 0x65, 0x6c, 0x74, 0x62 };
249 const u8 salt
[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
250 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
251 const u8 length
[2] = { 0x00, 0x01 };
255 SMP_DBG("w %32phN", w
);
256 SMP_DBG("n1 %16phN n2 %16phN", n1
, n2
);
257 SMP_DBG("a1 %7phN a2 %7phN", a1
, a2
);
259 err
= aes_cmac(tfm_cmac
, salt
, w
, 32, t
);
263 SMP_DBG("t %16phN", t
);
265 memcpy(m
, length
, 2);
266 memcpy(m
+ 2, a2
, 7);
267 memcpy(m
+ 9, a1
, 7);
268 memcpy(m
+ 16, n2
, 16);
269 memcpy(m
+ 32, n1
, 16);
270 memcpy(m
+ 48, btle
, 4);
272 m
[52] = 0; /* Counter */
274 err
= aes_cmac(tfm_cmac
, t
, m
, sizeof(m
), mackey
);
278 SMP_DBG("mackey %16phN", mackey
);
280 m
[52] = 1; /* Counter */
282 err
= aes_cmac(tfm_cmac
, t
, m
, sizeof(m
), ltk
);
286 SMP_DBG("ltk %16phN", ltk
);
291 static int smp_f6(struct crypto_shash
*tfm_cmac
, const u8 w
[16],
292 const u8 n1
[16], const u8 n2
[16], const u8 r
[16],
293 const u8 io_cap
[3], const u8 a1
[7], const u8 a2
[7],
299 SMP_DBG("w %16phN", w
);
300 SMP_DBG("n1 %16phN n2 %16phN", n1
, n2
);
301 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r
, io_cap
, a1
, a2
);
304 memcpy(m
+ 7, a1
, 7);
305 memcpy(m
+ 14, io_cap
, 3);
306 memcpy(m
+ 17, r
, 16);
307 memcpy(m
+ 33, n2
, 16);
308 memcpy(m
+ 49, n1
, 16);
310 err
= aes_cmac(tfm_cmac
, w
, m
, sizeof(m
), res
);
314 SMP_DBG("res %16phN", res
);
319 static int smp_g2(struct crypto_shash
*tfm_cmac
, const u8 u
[32], const u8 v
[32],
320 const u8 x
[16], const u8 y
[16], u32
*val
)
325 SMP_DBG("u %32phN", u
);
326 SMP_DBG("v %32phN", v
);
327 SMP_DBG("x %16phN y %16phN", x
, y
);
330 memcpy(m
+ 16, v
, 32);
331 memcpy(m
+ 48, u
, 32);
333 err
= aes_cmac(tfm_cmac
, x
, m
, sizeof(m
), tmp
);
337 *val
= get_unaligned_le32(tmp
);
340 SMP_DBG("val %06u", *val
);
345 static int smp_h6(struct crypto_shash
*tfm_cmac
, const u8 w
[16],
346 const u8 key_id
[4], u8 res
[16])
350 SMP_DBG("w %16phN key_id %4phN", w
, key_id
);
352 err
= aes_cmac(tfm_cmac
, w
, key_id
, 4, res
);
356 SMP_DBG("res %16phN", res
);
361 static int smp_h7(struct crypto_shash
*tfm_cmac
, const u8 w
[16],
362 const u8 salt
[16], u8 res
[16])
366 SMP_DBG("w %16phN salt %16phN", w
, salt
);
368 err
= aes_cmac(tfm_cmac
, salt
, w
, 16, res
);
372 SMP_DBG("res %16phN", res
);
377 /* The following functions map to the legacy SMP crypto functions e, c1,
381 static int smp_e(struct crypto_cipher
*tfm
, const u8
*k
, u8
*r
)
383 uint8_t tmp
[16], data
[16];
386 SMP_DBG("k %16phN r %16phN", k
, r
);
389 BT_ERR("tfm %p", tfm
);
393 /* The most significant octet of key corresponds to k[0] */
394 swap_buf(k
, tmp
, 16);
396 err
= crypto_cipher_setkey(tfm
, tmp
, 16);
398 BT_ERR("cipher setkey failed: %d", err
);
402 /* Most significant octet of plaintextData corresponds to data[0] */
403 swap_buf(r
, data
, 16);
405 crypto_cipher_encrypt_one(tfm
, data
, data
);
407 /* Most significant octet of encryptedData corresponds to data[0] */
408 swap_buf(data
, r
, 16);
410 SMP_DBG("r %16phN", r
);
415 static int smp_c1(struct crypto_cipher
*tfm_aes
, const u8 k
[16],
416 const u8 r
[16], const u8 preq
[7], const u8 pres
[7], u8 _iat
,
417 const bdaddr_t
*ia
, u8 _rat
, const bdaddr_t
*ra
, u8 res
[16])
422 SMP_DBG("k %16phN r %16phN", k
, r
);
423 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat
, ia
, _rat
, ra
);
424 SMP_DBG("preq %7phN pres %7phN", preq
, pres
);
428 /* p1 = pres || preq || _rat || _iat */
431 memcpy(p1
+ 2, preq
, 7);
432 memcpy(p1
+ 9, pres
, 7);
434 SMP_DBG("p1 %16phN", p1
);
437 u128_xor((u128
*) res
, (u128
*) r
, (u128
*) p1
);
439 /* res = e(k, res) */
440 err
= smp_e(tfm_aes
, k
, res
);
442 BT_ERR("Encrypt data error");
446 /* p2 = padding || ia || ra */
448 memcpy(p2
+ 6, ia
, 6);
449 memset(p2
+ 12, 0, 4);
451 SMP_DBG("p2 %16phN", p2
);
453 /* res = res XOR p2 */
454 u128_xor((u128
*) res
, (u128
*) res
, (u128
*) p2
);
456 /* res = e(k, res) */
457 err
= smp_e(tfm_aes
, k
, res
);
459 BT_ERR("Encrypt data error");
464 static int smp_s1(struct crypto_cipher
*tfm_aes
, const u8 k
[16],
465 const u8 r1
[16], const u8 r2
[16], u8 _r
[16])
469 /* Just least significant octets from r1 and r2 are considered */
471 memcpy(_r
+ 8, r1
, 8);
473 err
= smp_e(tfm_aes
, k
, _r
);
475 BT_ERR("Encrypt data error");
480 static int smp_ah(struct crypto_cipher
*tfm
, const u8 irk
[16],
481 const u8 r
[3], u8 res
[3])
486 /* r' = padding || r */
488 memset(_res
+ 3, 0, 13);
490 err
= smp_e(tfm
, irk
, _res
);
492 BT_ERR("Encrypt error");
496 /* The output of the random address function ah is:
497 * ah(k, r) = e(k, r') mod 2^24
498 * The output of the security function e is then truncated to 24 bits
499 * by taking the least significant 24 bits of the output of e as the
502 memcpy(res
, _res
, 3);
507 bool smp_irk_matches(struct hci_dev
*hdev
, const u8 irk
[16],
508 const bdaddr_t
*bdaddr
)
510 struct l2cap_chan
*chan
= hdev
->smp_data
;
515 if (!chan
|| !chan
->data
)
520 BT_DBG("RPA %pMR IRK %*phN", bdaddr
, 16, irk
);
522 err
= smp_ah(smp
->tfm_aes
, irk
, &bdaddr
->b
[3], hash
);
526 return !memcmp(bdaddr
->b
, hash
, 3);
529 int smp_generate_rpa(struct hci_dev
*hdev
, const u8 irk
[16], bdaddr_t
*rpa
)
531 struct l2cap_chan
*chan
= hdev
->smp_data
;
535 if (!chan
|| !chan
->data
)
540 get_random_bytes(&rpa
->b
[3], 3);
542 rpa
->b
[5] &= 0x3f; /* Clear two most significant bits */
543 rpa
->b
[5] |= 0x40; /* Set second most significant bit */
545 err
= smp_ah(smp
->tfm_aes
, irk
, &rpa
->b
[3], rpa
->b
);
549 BT_DBG("RPA %pMR", rpa
);
554 int smp_generate_oob(struct hci_dev
*hdev
, u8 hash
[16], u8 rand
[16])
556 struct l2cap_chan
*chan
= hdev
->smp_data
;
560 if (!chan
|| !chan
->data
)
565 if (hci_dev_test_flag(hdev
, HCI_USE_DEBUG_KEYS
)) {
566 BT_DBG("Using debug keys");
567 memcpy(smp
->local_pk
, debug_pk
, 64);
568 memcpy(smp
->local_sk
, debug_sk
, 32);
569 smp
->debug_key
= true;
572 /* Seed private key with random number */
573 get_random_bytes(smp
->local_sk
, 32);
575 /* Generate local key pair for Secure Connections */
576 if (!generate_ecdh_keys(smp
->local_pk
, smp
->local_sk
))
579 /* This is unlikely, but we need to check that
580 * we didn't accidentially generate a debug key.
582 if (memcmp(smp
->local_sk
, debug_sk
, 32))
585 smp
->debug_key
= false;
588 SMP_DBG("OOB Public Key X: %32phN", smp
->local_pk
);
589 SMP_DBG("OOB Public Key Y: %32phN", smp
->local_pk
+ 32);
590 SMP_DBG("OOB Private Key: %32phN", smp
->local_sk
);
592 get_random_bytes(smp
->local_rand
, 16);
594 err
= smp_f4(smp
->tfm_cmac
, smp
->local_pk
, smp
->local_pk
,
595 smp
->local_rand
, 0, hash
);
599 memcpy(rand
, smp
->local_rand
, 16);
604 static void smp_send_cmd(struct l2cap_conn
*conn
, u8 code
, u16 len
, void *data
)
606 struct l2cap_chan
*chan
= conn
->smp
;
607 struct smp_chan
*smp
;
614 BT_DBG("code 0x%2.2x", code
);
616 iv
[0].iov_base
= &code
;
619 iv
[1].iov_base
= data
;
622 memset(&msg
, 0, sizeof(msg
));
624 iov_iter_kvec(&msg
.msg_iter
, WRITE
| ITER_KVEC
, iv
, 2, 1 + len
);
626 l2cap_chan_send(chan
, &msg
, 1 + len
);
633 cancel_delayed_work_sync(&smp
->security_timer
);
634 schedule_delayed_work(&smp
->security_timer
, SMP_TIMEOUT
);
637 static u8
authreq_to_seclevel(u8 authreq
)
639 if (authreq
& SMP_AUTH_MITM
) {
640 if (authreq
& SMP_AUTH_SC
)
641 return BT_SECURITY_FIPS
;
643 return BT_SECURITY_HIGH
;
645 return BT_SECURITY_MEDIUM
;
649 static __u8
seclevel_to_authreq(__u8 sec_level
)
652 case BT_SECURITY_FIPS
:
653 case BT_SECURITY_HIGH
:
654 return SMP_AUTH_MITM
| SMP_AUTH_BONDING
;
655 case BT_SECURITY_MEDIUM
:
656 return SMP_AUTH_BONDING
;
658 return SMP_AUTH_NONE
;
662 static void build_pairing_cmd(struct l2cap_conn
*conn
,
663 struct smp_cmd_pairing
*req
,
664 struct smp_cmd_pairing
*rsp
, __u8 authreq
)
666 struct l2cap_chan
*chan
= conn
->smp
;
667 struct smp_chan
*smp
= chan
->data
;
668 struct hci_conn
*hcon
= conn
->hcon
;
669 struct hci_dev
*hdev
= hcon
->hdev
;
670 u8 local_dist
= 0, remote_dist
= 0, oob_flag
= SMP_OOB_NOT_PRESENT
;
672 if (hci_dev_test_flag(hdev
, HCI_BONDABLE
)) {
673 local_dist
= SMP_DIST_ENC_KEY
| SMP_DIST_SIGN
;
674 remote_dist
= SMP_DIST_ENC_KEY
| SMP_DIST_SIGN
;
675 authreq
|= SMP_AUTH_BONDING
;
677 authreq
&= ~SMP_AUTH_BONDING
;
680 if (hci_dev_test_flag(hdev
, HCI_RPA_RESOLVING
))
681 remote_dist
|= SMP_DIST_ID_KEY
;
683 if (hci_dev_test_flag(hdev
, HCI_PRIVACY
))
684 local_dist
|= SMP_DIST_ID_KEY
;
686 if (hci_dev_test_flag(hdev
, HCI_SC_ENABLED
) &&
687 (authreq
& SMP_AUTH_SC
)) {
688 struct oob_data
*oob_data
;
691 if (hci_dev_test_flag(hdev
, HCI_SSP_ENABLED
)) {
692 local_dist
|= SMP_DIST_LINK_KEY
;
693 remote_dist
|= SMP_DIST_LINK_KEY
;
696 if (hcon
->dst_type
== ADDR_LE_DEV_PUBLIC
)
697 bdaddr_type
= BDADDR_LE_PUBLIC
;
699 bdaddr_type
= BDADDR_LE_RANDOM
;
701 oob_data
= hci_find_remote_oob_data(hdev
, &hcon
->dst
,
703 if (oob_data
&& oob_data
->present
) {
704 set_bit(SMP_FLAG_REMOTE_OOB
, &smp
->flags
);
705 oob_flag
= SMP_OOB_PRESENT
;
706 memcpy(smp
->rr
, oob_data
->rand256
, 16);
707 memcpy(smp
->pcnf
, oob_data
->hash256
, 16);
708 SMP_DBG("OOB Remote Confirmation: %16phN", smp
->pcnf
);
709 SMP_DBG("OOB Remote Random: %16phN", smp
->rr
);
713 authreq
&= ~SMP_AUTH_SC
;
717 req
->io_capability
= conn
->hcon
->io_capability
;
718 req
->oob_flag
= oob_flag
;
719 req
->max_key_size
= SMP_DEV(hdev
)->max_key_size
;
720 req
->init_key_dist
= local_dist
;
721 req
->resp_key_dist
= remote_dist
;
722 req
->auth_req
= (authreq
& AUTH_REQ_MASK(hdev
));
724 smp
->remote_key_dist
= remote_dist
;
728 rsp
->io_capability
= conn
->hcon
->io_capability
;
729 rsp
->oob_flag
= oob_flag
;
730 rsp
->max_key_size
= SMP_DEV(hdev
)->max_key_size
;
731 rsp
->init_key_dist
= req
->init_key_dist
& remote_dist
;
732 rsp
->resp_key_dist
= req
->resp_key_dist
& local_dist
;
733 rsp
->auth_req
= (authreq
& AUTH_REQ_MASK(hdev
));
735 smp
->remote_key_dist
= rsp
->init_key_dist
;
738 static u8
check_enc_key_size(struct l2cap_conn
*conn
, __u8 max_key_size
)
740 struct l2cap_chan
*chan
= conn
->smp
;
741 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
742 struct smp_chan
*smp
= chan
->data
;
744 if (max_key_size
> SMP_DEV(hdev
)->max_key_size
||
745 max_key_size
< SMP_MIN_ENC_KEY_SIZE
)
746 return SMP_ENC_KEY_SIZE
;
748 smp
->enc_key_size
= max_key_size
;
753 static void smp_chan_destroy(struct l2cap_conn
*conn
)
755 struct l2cap_chan
*chan
= conn
->smp
;
756 struct smp_chan
*smp
= chan
->data
;
757 struct hci_conn
*hcon
= conn
->hcon
;
762 cancel_delayed_work_sync(&smp
->security_timer
);
764 complete
= test_bit(SMP_FLAG_COMPLETE
, &smp
->flags
);
765 mgmt_smp_complete(hcon
, complete
);
768 kzfree(smp
->slave_csrk
);
769 kzfree(smp
->link_key
);
771 crypto_free_cipher(smp
->tfm_aes
);
772 crypto_free_shash(smp
->tfm_cmac
);
774 /* Ensure that we don't leave any debug key around if debug key
775 * support hasn't been explicitly enabled.
777 if (smp
->ltk
&& smp
->ltk
->type
== SMP_LTK_P256_DEBUG
&&
778 !hci_dev_test_flag(hcon
->hdev
, HCI_KEEP_DEBUG_KEYS
)) {
779 list_del_rcu(&smp
->ltk
->list
);
780 kfree_rcu(smp
->ltk
, rcu
);
784 /* If pairing failed clean up any keys we might have */
787 list_del_rcu(&smp
->ltk
->list
);
788 kfree_rcu(smp
->ltk
, rcu
);
791 if (smp
->slave_ltk
) {
792 list_del_rcu(&smp
->slave_ltk
->list
);
793 kfree_rcu(smp
->slave_ltk
, rcu
);
796 if (smp
->remote_irk
) {
797 list_del_rcu(&smp
->remote_irk
->list
);
798 kfree_rcu(smp
->remote_irk
, rcu
);
807 static void smp_failure(struct l2cap_conn
*conn
, u8 reason
)
809 struct hci_conn
*hcon
= conn
->hcon
;
810 struct l2cap_chan
*chan
= conn
->smp
;
813 smp_send_cmd(conn
, SMP_CMD_PAIRING_FAIL
, sizeof(reason
),
816 mgmt_auth_failed(hcon
, HCI_ERROR_AUTH_FAILURE
);
819 smp_chan_destroy(conn
);
822 #define JUST_WORKS 0x00
823 #define JUST_CFM 0x01
824 #define REQ_PASSKEY 0x02
825 #define CFM_PASSKEY 0x03
827 #define DSP_PASSKEY 0x05
830 static const u8 gen_method
[5][5] = {
831 { JUST_WORKS
, JUST_CFM
, REQ_PASSKEY
, JUST_WORKS
, REQ_PASSKEY
},
832 { JUST_WORKS
, JUST_CFM
, REQ_PASSKEY
, JUST_WORKS
, REQ_PASSKEY
},
833 { CFM_PASSKEY
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, CFM_PASSKEY
},
834 { JUST_WORKS
, JUST_CFM
, JUST_WORKS
, JUST_WORKS
, JUST_CFM
},
835 { CFM_PASSKEY
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, OVERLAP
},
838 static const u8 sc_method
[5][5] = {
839 { JUST_WORKS
, JUST_CFM
, REQ_PASSKEY
, JUST_WORKS
, REQ_PASSKEY
},
840 { JUST_WORKS
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, CFM_PASSKEY
},
841 { DSP_PASSKEY
, DSP_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, DSP_PASSKEY
},
842 { JUST_WORKS
, JUST_CFM
, JUST_WORKS
, JUST_WORKS
, JUST_CFM
},
843 { DSP_PASSKEY
, CFM_PASSKEY
, REQ_PASSKEY
, JUST_WORKS
, CFM_PASSKEY
},
846 static u8
get_auth_method(struct smp_chan
*smp
, u8 local_io
, u8 remote_io
)
848 /* If either side has unknown io_caps, use JUST_CFM (which gets
849 * converted later to JUST_WORKS if we're initiators.
851 if (local_io
> SMP_IO_KEYBOARD_DISPLAY
||
852 remote_io
> SMP_IO_KEYBOARD_DISPLAY
)
855 if (test_bit(SMP_FLAG_SC
, &smp
->flags
))
856 return sc_method
[remote_io
][local_io
];
858 return gen_method
[remote_io
][local_io
];
861 static int tk_request(struct l2cap_conn
*conn
, u8 remote_oob
, u8 auth
,
862 u8 local_io
, u8 remote_io
)
864 struct hci_conn
*hcon
= conn
->hcon
;
865 struct l2cap_chan
*chan
= conn
->smp
;
866 struct smp_chan
*smp
= chan
->data
;
870 /* Initialize key for JUST WORKS */
871 memset(smp
->tk
, 0, sizeof(smp
->tk
));
872 clear_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
874 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth
, local_io
, remote_io
);
876 /* If neither side wants MITM, either "just" confirm an incoming
877 * request or use just-works for outgoing ones. The JUST_CFM
878 * will be converted to JUST_WORKS if necessary later in this
879 * function. If either side has MITM look up the method from the
882 if (!(auth
& SMP_AUTH_MITM
))
883 smp
->method
= JUST_CFM
;
885 smp
->method
= get_auth_method(smp
, local_io
, remote_io
);
887 /* Don't confirm locally initiated pairing attempts */
888 if (smp
->method
== JUST_CFM
&& test_bit(SMP_FLAG_INITIATOR
,
890 smp
->method
= JUST_WORKS
;
892 /* Don't bother user space with no IO capabilities */
893 if (smp
->method
== JUST_CFM
&&
894 hcon
->io_capability
== HCI_IO_NO_INPUT_OUTPUT
)
895 smp
->method
= JUST_WORKS
;
897 /* If Just Works, Continue with Zero TK */
898 if (smp
->method
== JUST_WORKS
) {
899 set_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
903 /* If this function is used for SC -> legacy fallback we
904 * can only recover the just-works case.
906 if (test_bit(SMP_FLAG_SC
, &smp
->flags
))
909 /* Not Just Works/Confirm results in MITM Authentication */
910 if (smp
->method
!= JUST_CFM
) {
911 set_bit(SMP_FLAG_MITM_AUTH
, &smp
->flags
);
912 if (hcon
->pending_sec_level
< BT_SECURITY_HIGH
)
913 hcon
->pending_sec_level
= BT_SECURITY_HIGH
;
916 /* If both devices have Keyoard-Display I/O, the master
917 * Confirms and the slave Enters the passkey.
919 if (smp
->method
== OVERLAP
) {
920 if (hcon
->role
== HCI_ROLE_MASTER
)
921 smp
->method
= CFM_PASSKEY
;
923 smp
->method
= REQ_PASSKEY
;
926 /* Generate random passkey. */
927 if (smp
->method
== CFM_PASSKEY
) {
928 memset(smp
->tk
, 0, sizeof(smp
->tk
));
929 get_random_bytes(&passkey
, sizeof(passkey
));
931 put_unaligned_le32(passkey
, smp
->tk
);
932 BT_DBG("PassKey: %d", passkey
);
933 set_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
936 if (smp
->method
== REQ_PASSKEY
)
937 ret
= mgmt_user_passkey_request(hcon
->hdev
, &hcon
->dst
,
938 hcon
->type
, hcon
->dst_type
);
939 else if (smp
->method
== JUST_CFM
)
940 ret
= mgmt_user_confirm_request(hcon
->hdev
, &hcon
->dst
,
941 hcon
->type
, hcon
->dst_type
,
944 ret
= mgmt_user_passkey_notify(hcon
->hdev
, &hcon
->dst
,
945 hcon
->type
, hcon
->dst_type
,
951 static u8
smp_confirm(struct smp_chan
*smp
)
953 struct l2cap_conn
*conn
= smp
->conn
;
954 struct smp_cmd_pairing_confirm cp
;
957 BT_DBG("conn %p", conn
);
959 ret
= smp_c1(smp
->tfm_aes
, smp
->tk
, smp
->prnd
, smp
->preq
, smp
->prsp
,
960 conn
->hcon
->init_addr_type
, &conn
->hcon
->init_addr
,
961 conn
->hcon
->resp_addr_type
, &conn
->hcon
->resp_addr
,
964 return SMP_UNSPECIFIED
;
966 clear_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
);
968 smp_send_cmd(smp
->conn
, SMP_CMD_PAIRING_CONFIRM
, sizeof(cp
), &cp
);
971 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_CONFIRM
);
973 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RANDOM
);
978 static u8
smp_random(struct smp_chan
*smp
)
980 struct l2cap_conn
*conn
= smp
->conn
;
981 struct hci_conn
*hcon
= conn
->hcon
;
985 if (IS_ERR_OR_NULL(smp
->tfm_aes
))
986 return SMP_UNSPECIFIED
;
988 BT_DBG("conn %p %s", conn
, conn
->hcon
->out
? "master" : "slave");
990 ret
= smp_c1(smp
->tfm_aes
, smp
->tk
, smp
->rrnd
, smp
->preq
, smp
->prsp
,
991 hcon
->init_addr_type
, &hcon
->init_addr
,
992 hcon
->resp_addr_type
, &hcon
->resp_addr
, confirm
);
994 return SMP_UNSPECIFIED
;
996 if (memcmp(smp
->pcnf
, confirm
, sizeof(smp
->pcnf
)) != 0) {
997 BT_ERR("Pairing failed (confirmation values mismatch)");
998 return SMP_CONFIRM_FAILED
;
1006 smp_s1(smp
->tfm_aes
, smp
->tk
, smp
->rrnd
, smp
->prnd
, stk
);
1008 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &hcon
->flags
))
1009 return SMP_UNSPECIFIED
;
1011 hci_le_start_enc(hcon
, ediv
, rand
, stk
, smp
->enc_key_size
);
1012 hcon
->enc_key_size
= smp
->enc_key_size
;
1013 set_bit(HCI_CONN_STK_ENCRYPT
, &hcon
->flags
);
1019 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(smp
->prnd
),
1022 smp_s1(smp
->tfm_aes
, smp
->tk
, smp
->prnd
, smp
->rrnd
, stk
);
1024 if (hcon
->pending_sec_level
== BT_SECURITY_HIGH
)
1029 /* Even though there's no _SLAVE suffix this is the
1030 * slave STK we're adding for later lookup (the master
1031 * STK never needs to be stored).
1033 hci_add_ltk(hcon
->hdev
, &hcon
->dst
, hcon
->dst_type
,
1034 SMP_STK
, auth
, stk
, smp
->enc_key_size
, ediv
, rand
);
1040 static void smp_notify_keys(struct l2cap_conn
*conn
)
1042 struct l2cap_chan
*chan
= conn
->smp
;
1043 struct smp_chan
*smp
= chan
->data
;
1044 struct hci_conn
*hcon
= conn
->hcon
;
1045 struct hci_dev
*hdev
= hcon
->hdev
;
1046 struct smp_cmd_pairing
*req
= (void *) &smp
->preq
[1];
1047 struct smp_cmd_pairing
*rsp
= (void *) &smp
->prsp
[1];
1050 if (hcon
->type
== ACL_LINK
) {
1051 if (hcon
->key_type
== HCI_LK_DEBUG_COMBINATION
)
1054 persistent
= !test_bit(HCI_CONN_FLUSH_KEY
,
1057 /* The LTKs, IRKs and CSRKs should be persistent only if
1058 * both sides had the bonding bit set in their
1059 * authentication requests.
1061 persistent
= !!((req
->auth_req
& rsp
->auth_req
) &
1065 if (smp
->remote_irk
) {
1066 mgmt_new_irk(hdev
, smp
->remote_irk
, persistent
);
1068 /* Now that user space can be considered to know the
1069 * identity address track the connection based on it
1070 * from now on (assuming this is an LE link).
1072 if (hcon
->type
== LE_LINK
) {
1073 bacpy(&hcon
->dst
, &smp
->remote_irk
->bdaddr
);
1074 hcon
->dst_type
= smp
->remote_irk
->addr_type
;
1075 queue_work(hdev
->workqueue
, &conn
->id_addr_update_work
);
1080 smp
->csrk
->bdaddr_type
= hcon
->dst_type
;
1081 bacpy(&smp
->csrk
->bdaddr
, &hcon
->dst
);
1082 mgmt_new_csrk(hdev
, smp
->csrk
, persistent
);
1085 if (smp
->slave_csrk
) {
1086 smp
->slave_csrk
->bdaddr_type
= hcon
->dst_type
;
1087 bacpy(&smp
->slave_csrk
->bdaddr
, &hcon
->dst
);
1088 mgmt_new_csrk(hdev
, smp
->slave_csrk
, persistent
);
1092 smp
->ltk
->bdaddr_type
= hcon
->dst_type
;
1093 bacpy(&smp
->ltk
->bdaddr
, &hcon
->dst
);
1094 mgmt_new_ltk(hdev
, smp
->ltk
, persistent
);
1097 if (smp
->slave_ltk
) {
1098 smp
->slave_ltk
->bdaddr_type
= hcon
->dst_type
;
1099 bacpy(&smp
->slave_ltk
->bdaddr
, &hcon
->dst
);
1100 mgmt_new_ltk(hdev
, smp
->slave_ltk
, persistent
);
1103 if (smp
->link_key
) {
1104 struct link_key
*key
;
1107 if (test_bit(SMP_FLAG_DEBUG_KEY
, &smp
->flags
))
1108 type
= HCI_LK_DEBUG_COMBINATION
;
1109 else if (hcon
->sec_level
== BT_SECURITY_FIPS
)
1110 type
= HCI_LK_AUTH_COMBINATION_P256
;
1112 type
= HCI_LK_UNAUTH_COMBINATION_P256
;
1114 key
= hci_add_link_key(hdev
, smp
->conn
->hcon
, &hcon
->dst
,
1115 smp
->link_key
, type
, 0, &persistent
);
1117 mgmt_new_link_key(hdev
, key
, persistent
);
1119 /* Don't keep debug keys around if the relevant
1122 if (!hci_dev_test_flag(hdev
, HCI_KEEP_DEBUG_KEYS
) &&
1123 key
->type
== HCI_LK_DEBUG_COMBINATION
) {
1124 list_del_rcu(&key
->list
);
1125 kfree_rcu(key
, rcu
);
1131 static void sc_add_ltk(struct smp_chan
*smp
)
1133 struct hci_conn
*hcon
= smp
->conn
->hcon
;
1136 if (test_bit(SMP_FLAG_DEBUG_KEY
, &smp
->flags
))
1137 key_type
= SMP_LTK_P256_DEBUG
;
1139 key_type
= SMP_LTK_P256
;
1141 if (hcon
->pending_sec_level
== BT_SECURITY_FIPS
)
1146 smp
->ltk
= hci_add_ltk(hcon
->hdev
, &hcon
->dst
, hcon
->dst_type
,
1147 key_type
, auth
, smp
->tk
, smp
->enc_key_size
,
1151 static void sc_generate_link_key(struct smp_chan
*smp
)
1153 /* From core spec. Spells out in ASCII as 'lebr'. */
1154 const u8 lebr
[4] = { 0x72, 0x62, 0x65, 0x6c };
1156 smp
->link_key
= kzalloc(16, GFP_KERNEL
);
1160 if (test_bit(SMP_FLAG_CT2
, &smp
->flags
)) {
1161 /* SALT = 0x00000000000000000000000000000000746D7031 */
1162 const u8 salt
[16] = { 0x31, 0x70, 0x6d, 0x74 };
1164 if (smp_h7(smp
->tfm_cmac
, smp
->tk
, salt
, smp
->link_key
)) {
1165 kzfree(smp
->link_key
);
1166 smp
->link_key
= NULL
;
1170 /* From core spec. Spells out in ASCII as 'tmp1'. */
1171 const u8 tmp1
[4] = { 0x31, 0x70, 0x6d, 0x74 };
1173 if (smp_h6(smp
->tfm_cmac
, smp
->tk
, tmp1
, smp
->link_key
)) {
1174 kzfree(smp
->link_key
);
1175 smp
->link_key
= NULL
;
1180 if (smp_h6(smp
->tfm_cmac
, smp
->link_key
, lebr
, smp
->link_key
)) {
1181 kzfree(smp
->link_key
);
1182 smp
->link_key
= NULL
;
1187 static void smp_allow_key_dist(struct smp_chan
*smp
)
1189 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1190 * will be allowed in each PDU handler to ensure we receive
1191 * them in the correct order.
1193 if (smp
->remote_key_dist
& SMP_DIST_ENC_KEY
)
1194 SMP_ALLOW_CMD(smp
, SMP_CMD_ENCRYPT_INFO
);
1195 else if (smp
->remote_key_dist
& SMP_DIST_ID_KEY
)
1196 SMP_ALLOW_CMD(smp
, SMP_CMD_IDENT_INFO
);
1197 else if (smp
->remote_key_dist
& SMP_DIST_SIGN
)
1198 SMP_ALLOW_CMD(smp
, SMP_CMD_SIGN_INFO
);
1201 static void sc_generate_ltk(struct smp_chan
*smp
)
1203 /* From core spec. Spells out in ASCII as 'brle'. */
1204 const u8 brle
[4] = { 0x65, 0x6c, 0x72, 0x62 };
1205 struct hci_conn
*hcon
= smp
->conn
->hcon
;
1206 struct hci_dev
*hdev
= hcon
->hdev
;
1207 struct link_key
*key
;
1209 key
= hci_find_link_key(hdev
, &hcon
->dst
);
1211 BT_ERR("%s No Link Key found to generate LTK", hdev
->name
);
1215 if (key
->type
== HCI_LK_DEBUG_COMBINATION
)
1216 set_bit(SMP_FLAG_DEBUG_KEY
, &smp
->flags
);
1218 if (test_bit(SMP_FLAG_CT2
, &smp
->flags
)) {
1219 /* SALT = 0x00000000000000000000000000000000746D7032 */
1220 const u8 salt
[16] = { 0x32, 0x70, 0x6d, 0x74 };
1222 if (smp_h7(smp
->tfm_cmac
, key
->val
, salt
, smp
->tk
))
1225 /* From core spec. Spells out in ASCII as 'tmp2'. */
1226 const u8 tmp2
[4] = { 0x32, 0x70, 0x6d, 0x74 };
1228 if (smp_h6(smp
->tfm_cmac
, key
->val
, tmp2
, smp
->tk
))
1232 if (smp_h6(smp
->tfm_cmac
, smp
->tk
, brle
, smp
->tk
))
1238 static void smp_distribute_keys(struct smp_chan
*smp
)
1240 struct smp_cmd_pairing
*req
, *rsp
;
1241 struct l2cap_conn
*conn
= smp
->conn
;
1242 struct hci_conn
*hcon
= conn
->hcon
;
1243 struct hci_dev
*hdev
= hcon
->hdev
;
1246 BT_DBG("conn %p", conn
);
1248 rsp
= (void *) &smp
->prsp
[1];
1250 /* The responder sends its keys first */
1251 if (hcon
->out
&& (smp
->remote_key_dist
& KEY_DIST_MASK
)) {
1252 smp_allow_key_dist(smp
);
1256 req
= (void *) &smp
->preq
[1];
1259 keydist
= &rsp
->init_key_dist
;
1260 *keydist
&= req
->init_key_dist
;
1262 keydist
= &rsp
->resp_key_dist
;
1263 *keydist
&= req
->resp_key_dist
;
1266 if (test_bit(SMP_FLAG_SC
, &smp
->flags
)) {
1267 if (hcon
->type
== LE_LINK
&& (*keydist
& SMP_DIST_LINK_KEY
))
1268 sc_generate_link_key(smp
);
1269 if (hcon
->type
== ACL_LINK
&& (*keydist
& SMP_DIST_ENC_KEY
))
1270 sc_generate_ltk(smp
);
1272 /* Clear the keys which are generated but not distributed */
1273 *keydist
&= ~SMP_SC_NO_DIST
;
1276 BT_DBG("keydist 0x%x", *keydist
);
1278 if (*keydist
& SMP_DIST_ENC_KEY
) {
1279 struct smp_cmd_encrypt_info enc
;
1280 struct smp_cmd_master_ident ident
;
1281 struct smp_ltk
*ltk
;
1286 /* Make sure we generate only the significant amount of
1287 * bytes based on the encryption key size, and set the rest
1288 * of the value to zeroes.
1290 get_random_bytes(enc
.ltk
, smp
->enc_key_size
);
1291 memset(enc
.ltk
+ smp
->enc_key_size
, 0,
1292 sizeof(enc
.ltk
) - smp
->enc_key_size
);
1294 get_random_bytes(&ediv
, sizeof(ediv
));
1295 get_random_bytes(&rand
, sizeof(rand
));
1297 smp_send_cmd(conn
, SMP_CMD_ENCRYPT_INFO
, sizeof(enc
), &enc
);
1299 authenticated
= hcon
->sec_level
== BT_SECURITY_HIGH
;
1300 ltk
= hci_add_ltk(hdev
, &hcon
->dst
, hcon
->dst_type
,
1301 SMP_LTK_SLAVE
, authenticated
, enc
.ltk
,
1302 smp
->enc_key_size
, ediv
, rand
);
1303 smp
->slave_ltk
= ltk
;
1308 smp_send_cmd(conn
, SMP_CMD_MASTER_IDENT
, sizeof(ident
), &ident
);
1310 *keydist
&= ~SMP_DIST_ENC_KEY
;
1313 if (*keydist
& SMP_DIST_ID_KEY
) {
1314 struct smp_cmd_ident_addr_info addrinfo
;
1315 struct smp_cmd_ident_info idinfo
;
1317 memcpy(idinfo
.irk
, hdev
->irk
, sizeof(idinfo
.irk
));
1319 smp_send_cmd(conn
, SMP_CMD_IDENT_INFO
, sizeof(idinfo
), &idinfo
);
1321 /* The hci_conn contains the local identity address
1322 * after the connection has been established.
1324 * This is true even when the connection has been
1325 * established using a resolvable random address.
1327 bacpy(&addrinfo
.bdaddr
, &hcon
->src
);
1328 addrinfo
.addr_type
= hcon
->src_type
;
1330 smp_send_cmd(conn
, SMP_CMD_IDENT_ADDR_INFO
, sizeof(addrinfo
),
1333 *keydist
&= ~SMP_DIST_ID_KEY
;
1336 if (*keydist
& SMP_DIST_SIGN
) {
1337 struct smp_cmd_sign_info sign
;
1338 struct smp_csrk
*csrk
;
1340 /* Generate a new random key */
1341 get_random_bytes(sign
.csrk
, sizeof(sign
.csrk
));
1343 csrk
= kzalloc(sizeof(*csrk
), GFP_KERNEL
);
1345 if (hcon
->sec_level
> BT_SECURITY_MEDIUM
)
1346 csrk
->type
= MGMT_CSRK_LOCAL_AUTHENTICATED
;
1348 csrk
->type
= MGMT_CSRK_LOCAL_UNAUTHENTICATED
;
1349 memcpy(csrk
->val
, sign
.csrk
, sizeof(csrk
->val
));
1351 smp
->slave_csrk
= csrk
;
1353 smp_send_cmd(conn
, SMP_CMD_SIGN_INFO
, sizeof(sign
), &sign
);
1355 *keydist
&= ~SMP_DIST_SIGN
;
1358 /* If there are still keys to be received wait for them */
1359 if (smp
->remote_key_dist
& KEY_DIST_MASK
) {
1360 smp_allow_key_dist(smp
);
1364 set_bit(SMP_FLAG_COMPLETE
, &smp
->flags
);
1365 smp_notify_keys(conn
);
1367 smp_chan_destroy(conn
);
1370 static void smp_timeout(struct work_struct
*work
)
1372 struct smp_chan
*smp
= container_of(work
, struct smp_chan
,
1373 security_timer
.work
);
1374 struct l2cap_conn
*conn
= smp
->conn
;
1376 BT_DBG("conn %p", conn
);
1378 hci_disconnect(conn
->hcon
, HCI_ERROR_REMOTE_USER_TERM
);
1381 static struct smp_chan
*smp_chan_create(struct l2cap_conn
*conn
)
1383 struct l2cap_chan
*chan
= conn
->smp
;
1384 struct smp_chan
*smp
;
1386 smp
= kzalloc(sizeof(*smp
), GFP_ATOMIC
);
1390 smp
->tfm_aes
= crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC
);
1391 if (IS_ERR(smp
->tfm_aes
)) {
1392 BT_ERR("Unable to create AES crypto context");
1397 smp
->tfm_cmac
= crypto_alloc_shash("cmac(aes)", 0, 0);
1398 if (IS_ERR(smp
->tfm_cmac
)) {
1399 BT_ERR("Unable to create CMAC crypto context");
1400 crypto_free_cipher(smp
->tfm_aes
);
1408 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_FAIL
);
1410 INIT_DELAYED_WORK(&smp
->security_timer
, smp_timeout
);
1412 hci_conn_hold(conn
->hcon
);
1417 static int sc_mackey_and_ltk(struct smp_chan
*smp
, u8 mackey
[16], u8 ltk
[16])
1419 struct hci_conn
*hcon
= smp
->conn
->hcon
;
1420 u8
*na
, *nb
, a
[7], b
[7];
1430 memcpy(a
, &hcon
->init_addr
, 6);
1431 memcpy(b
, &hcon
->resp_addr
, 6);
1432 a
[6] = hcon
->init_addr_type
;
1433 b
[6] = hcon
->resp_addr_type
;
1435 return smp_f5(smp
->tfm_cmac
, smp
->dhkey
, na
, nb
, a
, b
, mackey
, ltk
);
1438 static void sc_dhkey_check(struct smp_chan
*smp
)
1440 struct hci_conn
*hcon
= smp
->conn
->hcon
;
1441 struct smp_cmd_dhkey_check check
;
1442 u8 a
[7], b
[7], *local_addr
, *remote_addr
;
1443 u8 io_cap
[3], r
[16];
1445 memcpy(a
, &hcon
->init_addr
, 6);
1446 memcpy(b
, &hcon
->resp_addr
, 6);
1447 a
[6] = hcon
->init_addr_type
;
1448 b
[6] = hcon
->resp_addr_type
;
1453 memcpy(io_cap
, &smp
->preq
[1], 3);
1457 memcpy(io_cap
, &smp
->prsp
[1], 3);
1460 memset(r
, 0, sizeof(r
));
1462 if (smp
->method
== REQ_PASSKEY
|| smp
->method
== DSP_PASSKEY
)
1463 put_unaligned_le32(hcon
->passkey_notify
, r
);
1465 if (smp
->method
== REQ_OOB
)
1466 memcpy(r
, smp
->rr
, 16);
1468 smp_f6(smp
->tfm_cmac
, smp
->mackey
, smp
->prnd
, smp
->rrnd
, r
, io_cap
,
1469 local_addr
, remote_addr
, check
.e
);
1471 smp_send_cmd(smp
->conn
, SMP_CMD_DHKEY_CHECK
, sizeof(check
), &check
);
1474 static u8
sc_passkey_send_confirm(struct smp_chan
*smp
)
1476 struct l2cap_conn
*conn
= smp
->conn
;
1477 struct hci_conn
*hcon
= conn
->hcon
;
1478 struct smp_cmd_pairing_confirm cfm
;
1481 r
= ((hcon
->passkey_notify
>> smp
->passkey_round
) & 0x01);
1484 get_random_bytes(smp
->prnd
, sizeof(smp
->prnd
));
1486 if (smp_f4(smp
->tfm_cmac
, smp
->local_pk
, smp
->remote_pk
, smp
->prnd
, r
,
1488 return SMP_UNSPECIFIED
;
1490 smp_send_cmd(conn
, SMP_CMD_PAIRING_CONFIRM
, sizeof(cfm
), &cfm
);
1495 static u8
sc_passkey_round(struct smp_chan
*smp
, u8 smp_op
)
1497 struct l2cap_conn
*conn
= smp
->conn
;
1498 struct hci_conn
*hcon
= conn
->hcon
;
1499 struct hci_dev
*hdev
= hcon
->hdev
;
1502 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1503 if (smp
->passkey_round
>= 20)
1507 case SMP_CMD_PAIRING_RANDOM
:
1508 r
= ((hcon
->passkey_notify
>> smp
->passkey_round
) & 0x01);
1511 if (smp_f4(smp
->tfm_cmac
, smp
->remote_pk
, smp
->local_pk
,
1513 return SMP_UNSPECIFIED
;
1515 if (memcmp(smp
->pcnf
, cfm
, 16))
1516 return SMP_CONFIRM_FAILED
;
1518 smp
->passkey_round
++;
1520 if (smp
->passkey_round
== 20) {
1521 /* Generate MacKey and LTK */
1522 if (sc_mackey_and_ltk(smp
, smp
->mackey
, smp
->tk
))
1523 return SMP_UNSPECIFIED
;
1526 /* The round is only complete when the initiator
1527 * receives pairing random.
1530 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
,
1531 sizeof(smp
->prnd
), smp
->prnd
);
1532 if (smp
->passkey_round
== 20)
1533 SMP_ALLOW_CMD(smp
, SMP_CMD_DHKEY_CHECK
);
1535 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_CONFIRM
);
1539 /* Start the next round */
1540 if (smp
->passkey_round
!= 20)
1541 return sc_passkey_round(smp
, 0);
1543 /* Passkey rounds are complete - start DHKey Check */
1544 sc_dhkey_check(smp
);
1545 SMP_ALLOW_CMD(smp
, SMP_CMD_DHKEY_CHECK
);
1549 case SMP_CMD_PAIRING_CONFIRM
:
1550 if (test_bit(SMP_FLAG_WAIT_USER
, &smp
->flags
)) {
1551 set_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
);
1555 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RANDOM
);
1558 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
,
1559 sizeof(smp
->prnd
), smp
->prnd
);
1563 return sc_passkey_send_confirm(smp
);
1565 case SMP_CMD_PUBLIC_KEY
:
1567 /* Initiating device starts the round */
1571 BT_DBG("%s Starting passkey round %u", hdev
->name
,
1572 smp
->passkey_round
+ 1);
1574 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_CONFIRM
);
1576 return sc_passkey_send_confirm(smp
);
1582 static int sc_user_reply(struct smp_chan
*smp
, u16 mgmt_op
, __le32 passkey
)
1584 struct l2cap_conn
*conn
= smp
->conn
;
1585 struct hci_conn
*hcon
= conn
->hcon
;
1588 clear_bit(SMP_FLAG_WAIT_USER
, &smp
->flags
);
1591 case MGMT_OP_USER_PASSKEY_NEG_REPLY
:
1592 smp_failure(smp
->conn
, SMP_PASSKEY_ENTRY_FAILED
);
1594 case MGMT_OP_USER_CONFIRM_NEG_REPLY
:
1595 smp_failure(smp
->conn
, SMP_NUMERIC_COMP_FAILED
);
1597 case MGMT_OP_USER_PASSKEY_REPLY
:
1598 hcon
->passkey_notify
= le32_to_cpu(passkey
);
1599 smp
->passkey_round
= 0;
1601 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
))
1602 smp_op
= SMP_CMD_PAIRING_CONFIRM
;
1606 if (sc_passkey_round(smp
, smp_op
))
1612 /* Initiator sends DHKey check first */
1614 sc_dhkey_check(smp
);
1615 SMP_ALLOW_CMD(smp
, SMP_CMD_DHKEY_CHECK
);
1616 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING
, &smp
->flags
)) {
1617 sc_dhkey_check(smp
);
1624 int smp_user_confirm_reply(struct hci_conn
*hcon
, u16 mgmt_op
, __le32 passkey
)
1626 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
1627 struct l2cap_chan
*chan
;
1628 struct smp_chan
*smp
;
1641 l2cap_chan_lock(chan
);
1649 if (test_bit(SMP_FLAG_SC
, &smp
->flags
)) {
1650 err
= sc_user_reply(smp
, mgmt_op
, passkey
);
1655 case MGMT_OP_USER_PASSKEY_REPLY
:
1656 value
= le32_to_cpu(passkey
);
1657 memset(smp
->tk
, 0, sizeof(smp
->tk
));
1658 BT_DBG("PassKey: %d", value
);
1659 put_unaligned_le32(value
, smp
->tk
);
1661 case MGMT_OP_USER_CONFIRM_REPLY
:
1662 set_bit(SMP_FLAG_TK_VALID
, &smp
->flags
);
1664 case MGMT_OP_USER_PASSKEY_NEG_REPLY
:
1665 case MGMT_OP_USER_CONFIRM_NEG_REPLY
:
1666 smp_failure(conn
, SMP_PASSKEY_ENTRY_FAILED
);
1670 smp_failure(conn
, SMP_PASSKEY_ENTRY_FAILED
);
1677 /* If it is our turn to send Pairing Confirm, do so now */
1678 if (test_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
)) {
1679 u8 rsp
= smp_confirm(smp
);
1681 smp_failure(conn
, rsp
);
1685 l2cap_chan_unlock(chan
);
1689 static void build_bredr_pairing_cmd(struct smp_chan
*smp
,
1690 struct smp_cmd_pairing
*req
,
1691 struct smp_cmd_pairing
*rsp
)
1693 struct l2cap_conn
*conn
= smp
->conn
;
1694 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
1695 u8 local_dist
= 0, remote_dist
= 0;
1697 if (hci_dev_test_flag(hdev
, HCI_BONDABLE
)) {
1698 local_dist
= SMP_DIST_ENC_KEY
| SMP_DIST_SIGN
;
1699 remote_dist
= SMP_DIST_ENC_KEY
| SMP_DIST_SIGN
;
1702 if (hci_dev_test_flag(hdev
, HCI_RPA_RESOLVING
))
1703 remote_dist
|= SMP_DIST_ID_KEY
;
1705 if (hci_dev_test_flag(hdev
, HCI_PRIVACY
))
1706 local_dist
|= SMP_DIST_ID_KEY
;
1709 memset(req
, 0, sizeof(*req
));
1711 req
->auth_req
= SMP_AUTH_CT2
;
1712 req
->init_key_dist
= local_dist
;
1713 req
->resp_key_dist
= remote_dist
;
1714 req
->max_key_size
= conn
->hcon
->enc_key_size
;
1716 smp
->remote_key_dist
= remote_dist
;
1721 memset(rsp
, 0, sizeof(*rsp
));
1723 rsp
->auth_req
= SMP_AUTH_CT2
;
1724 rsp
->max_key_size
= conn
->hcon
->enc_key_size
;
1725 rsp
->init_key_dist
= req
->init_key_dist
& remote_dist
;
1726 rsp
->resp_key_dist
= req
->resp_key_dist
& local_dist
;
1728 smp
->remote_key_dist
= rsp
->init_key_dist
;
1731 static u8
smp_cmd_pairing_req(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
1733 struct smp_cmd_pairing rsp
, *req
= (void *) skb
->data
;
1734 struct l2cap_chan
*chan
= conn
->smp
;
1735 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
1736 struct smp_chan
*smp
;
1737 u8 key_size
, auth
, sec_level
;
1740 BT_DBG("conn %p", conn
);
1742 if (skb
->len
< sizeof(*req
))
1743 return SMP_INVALID_PARAMS
;
1745 if (conn
->hcon
->role
!= HCI_ROLE_SLAVE
)
1746 return SMP_CMD_NOTSUPP
;
1749 smp
= smp_chan_create(conn
);
1754 return SMP_UNSPECIFIED
;
1756 /* We didn't start the pairing, so match remote */
1757 auth
= req
->auth_req
& AUTH_REQ_MASK(hdev
);
1759 if (!hci_dev_test_flag(hdev
, HCI_BONDABLE
) &&
1760 (auth
& SMP_AUTH_BONDING
))
1761 return SMP_PAIRING_NOTSUPP
;
1763 if (hci_dev_test_flag(hdev
, HCI_SC_ONLY
) && !(auth
& SMP_AUTH_SC
))
1764 return SMP_AUTH_REQUIREMENTS
;
1766 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
1767 memcpy(&smp
->preq
[1], req
, sizeof(*req
));
1768 skb_pull(skb
, sizeof(*req
));
1770 /* If the remote side's OOB flag is set it means it has
1771 * successfully received our local OOB data - therefore set the
1772 * flag to indicate that local OOB is in use.
1774 if (req
->oob_flag
== SMP_OOB_PRESENT
)
1775 set_bit(SMP_FLAG_LOCAL_OOB
, &smp
->flags
);
1777 /* SMP over BR/EDR requires special treatment */
1778 if (conn
->hcon
->type
== ACL_LINK
) {
1779 /* We must have a BR/EDR SC link */
1780 if (!test_bit(HCI_CONN_AES_CCM
, &conn
->hcon
->flags
) &&
1781 !hci_dev_test_flag(hdev
, HCI_FORCE_BREDR_SMP
))
1782 return SMP_CROSS_TRANSP_NOT_ALLOWED
;
1784 set_bit(SMP_FLAG_SC
, &smp
->flags
);
1786 build_bredr_pairing_cmd(smp
, req
, &rsp
);
1788 if (req
->auth_req
& SMP_AUTH_CT2
)
1789 set_bit(SMP_FLAG_CT2
, &smp
->flags
);
1791 key_size
= min(req
->max_key_size
, rsp
.max_key_size
);
1792 if (check_enc_key_size(conn
, key_size
))
1793 return SMP_ENC_KEY_SIZE
;
1795 /* Clear bits which are generated but not distributed */
1796 smp
->remote_key_dist
&= ~SMP_SC_NO_DIST
;
1798 smp
->prsp
[0] = SMP_CMD_PAIRING_RSP
;
1799 memcpy(&smp
->prsp
[1], &rsp
, sizeof(rsp
));
1800 smp_send_cmd(conn
, SMP_CMD_PAIRING_RSP
, sizeof(rsp
), &rsp
);
1802 smp_distribute_keys(smp
);
1806 build_pairing_cmd(conn
, req
, &rsp
, auth
);
1808 if (rsp
.auth_req
& SMP_AUTH_SC
) {
1809 set_bit(SMP_FLAG_SC
, &smp
->flags
);
1811 if (rsp
.auth_req
& SMP_AUTH_CT2
)
1812 set_bit(SMP_FLAG_CT2
, &smp
->flags
);
1815 if (conn
->hcon
->io_capability
== HCI_IO_NO_INPUT_OUTPUT
)
1816 sec_level
= BT_SECURITY_MEDIUM
;
1818 sec_level
= authreq_to_seclevel(auth
);
1820 if (sec_level
> conn
->hcon
->pending_sec_level
)
1821 conn
->hcon
->pending_sec_level
= sec_level
;
1823 /* If we need MITM check that it can be achieved */
1824 if (conn
->hcon
->pending_sec_level
>= BT_SECURITY_HIGH
) {
1827 method
= get_auth_method(smp
, conn
->hcon
->io_capability
,
1828 req
->io_capability
);
1829 if (method
== JUST_WORKS
|| method
== JUST_CFM
)
1830 return SMP_AUTH_REQUIREMENTS
;
1833 key_size
= min(req
->max_key_size
, rsp
.max_key_size
);
1834 if (check_enc_key_size(conn
, key_size
))
1835 return SMP_ENC_KEY_SIZE
;
1837 get_random_bytes(smp
->prnd
, sizeof(smp
->prnd
));
1839 smp
->prsp
[0] = SMP_CMD_PAIRING_RSP
;
1840 memcpy(&smp
->prsp
[1], &rsp
, sizeof(rsp
));
1842 smp_send_cmd(conn
, SMP_CMD_PAIRING_RSP
, sizeof(rsp
), &rsp
);
1844 clear_bit(SMP_FLAG_INITIATOR
, &smp
->flags
);
1846 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1847 * SC case, however some implementations incorrectly copy RFU auth
1848 * req bits from our security request, which may create a false
1849 * positive SC enablement.
1851 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_CONFIRM
);
1853 if (test_bit(SMP_FLAG_SC
, &smp
->flags
)) {
1854 SMP_ALLOW_CMD(smp
, SMP_CMD_PUBLIC_KEY
);
1855 /* Clear bits which are generated but not distributed */
1856 smp
->remote_key_dist
&= ~SMP_SC_NO_DIST
;
1857 /* Wait for Public Key from Initiating Device */
1861 /* Request setup of TK */
1862 ret
= tk_request(conn
, 0, auth
, rsp
.io_capability
, req
->io_capability
);
1864 return SMP_UNSPECIFIED
;
1869 static u8
sc_send_public_key(struct smp_chan
*smp
)
1871 struct hci_dev
*hdev
= smp
->conn
->hcon
->hdev
;
1875 if (test_bit(SMP_FLAG_LOCAL_OOB
, &smp
->flags
)) {
1876 struct l2cap_chan
*chan
= hdev
->smp_data
;
1877 struct smp_dev
*smp_dev
;
1879 if (!chan
|| !chan
->data
)
1880 return SMP_UNSPECIFIED
;
1882 smp_dev
= chan
->data
;
1884 memcpy(smp
->local_pk
, smp_dev
->local_pk
, 64);
1885 memcpy(smp
->local_sk
, smp_dev
->local_sk
, 32);
1886 memcpy(smp
->lr
, smp_dev
->local_rand
, 16);
1888 if (smp_dev
->debug_key
)
1889 set_bit(SMP_FLAG_DEBUG_KEY
, &smp
->flags
);
1894 if (hci_dev_test_flag(hdev
, HCI_USE_DEBUG_KEYS
)) {
1895 BT_DBG("Using debug keys");
1896 memcpy(smp
->local_pk
, debug_pk
, 64);
1897 memcpy(smp
->local_sk
, debug_sk
, 32);
1898 set_bit(SMP_FLAG_DEBUG_KEY
, &smp
->flags
);
1901 /* Seed private key with random number */
1902 get_random_bytes(smp
->local_sk
, 32);
1904 /* Generate local key pair for Secure Connections */
1905 if (!generate_ecdh_keys(smp
->local_pk
, smp
->local_sk
))
1906 return SMP_UNSPECIFIED
;
1908 /* This is unlikely, but we need to check that
1909 * we didn't accidentially generate a debug key.
1911 if (memcmp(smp
->local_sk
, debug_sk
, 32))
1917 SMP_DBG("Local Public Key X: %32phN", smp
->local_pk
);
1918 SMP_DBG("Local Public Key Y: %32phN", smp
->local_pk
+ 32);
1919 SMP_DBG("Local Private Key: %32phN", smp
->local_sk
);
1921 smp_send_cmd(smp
->conn
, SMP_CMD_PUBLIC_KEY
, 64, smp
->local_pk
);
1926 static u8
smp_cmd_pairing_rsp(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
1928 struct smp_cmd_pairing
*req
, *rsp
= (void *) skb
->data
;
1929 struct l2cap_chan
*chan
= conn
->smp
;
1930 struct smp_chan
*smp
= chan
->data
;
1931 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
1935 BT_DBG("conn %p", conn
);
1937 if (skb
->len
< sizeof(*rsp
))
1938 return SMP_INVALID_PARAMS
;
1940 if (conn
->hcon
->role
!= HCI_ROLE_MASTER
)
1941 return SMP_CMD_NOTSUPP
;
1943 skb_pull(skb
, sizeof(*rsp
));
1945 req
= (void *) &smp
->preq
[1];
1947 key_size
= min(req
->max_key_size
, rsp
->max_key_size
);
1948 if (check_enc_key_size(conn
, key_size
))
1949 return SMP_ENC_KEY_SIZE
;
1951 auth
= rsp
->auth_req
& AUTH_REQ_MASK(hdev
);
1953 if (hci_dev_test_flag(hdev
, HCI_SC_ONLY
) && !(auth
& SMP_AUTH_SC
))
1954 return SMP_AUTH_REQUIREMENTS
;
1956 /* If the remote side's OOB flag is set it means it has
1957 * successfully received our local OOB data - therefore set the
1958 * flag to indicate that local OOB is in use.
1960 if (rsp
->oob_flag
== SMP_OOB_PRESENT
)
1961 set_bit(SMP_FLAG_LOCAL_OOB
, &smp
->flags
);
1963 smp
->prsp
[0] = SMP_CMD_PAIRING_RSP
;
1964 memcpy(&smp
->prsp
[1], rsp
, sizeof(*rsp
));
1966 /* Update remote key distribution in case the remote cleared
1967 * some bits that we had enabled in our request.
1969 smp
->remote_key_dist
&= rsp
->resp_key_dist
;
1971 if ((req
->auth_req
& SMP_AUTH_CT2
) && (auth
& SMP_AUTH_CT2
))
1972 set_bit(SMP_FLAG_CT2
, &smp
->flags
);
1974 /* For BR/EDR this means we're done and can start phase 3 */
1975 if (conn
->hcon
->type
== ACL_LINK
) {
1976 /* Clear bits which are generated but not distributed */
1977 smp
->remote_key_dist
&= ~SMP_SC_NO_DIST
;
1978 smp_distribute_keys(smp
);
1982 if ((req
->auth_req
& SMP_AUTH_SC
) && (auth
& SMP_AUTH_SC
))
1983 set_bit(SMP_FLAG_SC
, &smp
->flags
);
1984 else if (conn
->hcon
->pending_sec_level
> BT_SECURITY_HIGH
)
1985 conn
->hcon
->pending_sec_level
= BT_SECURITY_HIGH
;
1987 /* If we need MITM check that it can be achieved */
1988 if (conn
->hcon
->pending_sec_level
>= BT_SECURITY_HIGH
) {
1991 method
= get_auth_method(smp
, req
->io_capability
,
1992 rsp
->io_capability
);
1993 if (method
== JUST_WORKS
|| method
== JUST_CFM
)
1994 return SMP_AUTH_REQUIREMENTS
;
1997 get_random_bytes(smp
->prnd
, sizeof(smp
->prnd
));
1999 /* Update remote key distribution in case the remote cleared
2000 * some bits that we had enabled in our request.
2002 smp
->remote_key_dist
&= rsp
->resp_key_dist
;
2004 if (test_bit(SMP_FLAG_SC
, &smp
->flags
)) {
2005 /* Clear bits which are generated but not distributed */
2006 smp
->remote_key_dist
&= ~SMP_SC_NO_DIST
;
2007 SMP_ALLOW_CMD(smp
, SMP_CMD_PUBLIC_KEY
);
2008 return sc_send_public_key(smp
);
2011 auth
|= req
->auth_req
;
2013 ret
= tk_request(conn
, 0, auth
, req
->io_capability
, rsp
->io_capability
);
2015 return SMP_UNSPECIFIED
;
2017 set_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
);
2019 /* Can't compose response until we have been confirmed */
2020 if (test_bit(SMP_FLAG_TK_VALID
, &smp
->flags
))
2021 return smp_confirm(smp
);
2026 static u8
sc_check_confirm(struct smp_chan
*smp
)
2028 struct l2cap_conn
*conn
= smp
->conn
;
2032 if (smp
->method
== REQ_PASSKEY
|| smp
->method
== DSP_PASSKEY
)
2033 return sc_passkey_round(smp
, SMP_CMD_PAIRING_CONFIRM
);
2035 if (conn
->hcon
->out
) {
2036 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(smp
->prnd
),
2038 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RANDOM
);
2044 /* Work-around for some implementations that incorrectly copy RFU bits
2045 * from our security request and thereby create the impression that
2046 * we're doing SC when in fact the remote doesn't support it.
2048 static int fixup_sc_false_positive(struct smp_chan
*smp
)
2050 struct l2cap_conn
*conn
= smp
->conn
;
2051 struct hci_conn
*hcon
= conn
->hcon
;
2052 struct hci_dev
*hdev
= hcon
->hdev
;
2053 struct smp_cmd_pairing
*req
, *rsp
;
2056 /* The issue is only observed when we're in slave role */
2058 return SMP_UNSPECIFIED
;
2060 if (hci_dev_test_flag(hdev
, HCI_SC_ONLY
)) {
2061 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2062 return SMP_UNSPECIFIED
;
2065 BT_ERR("Trying to fall back to legacy SMP");
2067 req
= (void *) &smp
->preq
[1];
2068 rsp
= (void *) &smp
->prsp
[1];
2070 /* Rebuild key dist flags which may have been cleared for SC */
2071 smp
->remote_key_dist
= (req
->init_key_dist
& rsp
->resp_key_dist
);
2073 auth
= req
->auth_req
& AUTH_REQ_MASK(hdev
);
2075 if (tk_request(conn
, 0, auth
, rsp
->io_capability
, req
->io_capability
)) {
2076 BT_ERR("Failed to fall back to legacy SMP");
2077 return SMP_UNSPECIFIED
;
2080 clear_bit(SMP_FLAG_SC
, &smp
->flags
);
2085 static u8
smp_cmd_pairing_confirm(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2087 struct l2cap_chan
*chan
= conn
->smp
;
2088 struct smp_chan
*smp
= chan
->data
;
2090 BT_DBG("conn %p %s", conn
, conn
->hcon
->out
? "master" : "slave");
2092 if (skb
->len
< sizeof(smp
->pcnf
))
2093 return SMP_INVALID_PARAMS
;
2095 memcpy(smp
->pcnf
, skb
->data
, sizeof(smp
->pcnf
));
2096 skb_pull(skb
, sizeof(smp
->pcnf
));
2098 if (test_bit(SMP_FLAG_SC
, &smp
->flags
)) {
2101 /* Public Key exchange must happen before any other steps */
2102 if (test_bit(SMP_FLAG_REMOTE_PK
, &smp
->flags
))
2103 return sc_check_confirm(smp
);
2105 BT_ERR("Unexpected SMP Pairing Confirm");
2107 ret
= fixup_sc_false_positive(smp
);
2112 if (conn
->hcon
->out
) {
2113 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(smp
->prnd
),
2115 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RANDOM
);
2119 if (test_bit(SMP_FLAG_TK_VALID
, &smp
->flags
))
2120 return smp_confirm(smp
);
2122 set_bit(SMP_FLAG_CFM_PENDING
, &smp
->flags
);
2127 static u8
smp_cmd_pairing_random(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2129 struct l2cap_chan
*chan
= conn
->smp
;
2130 struct smp_chan
*smp
= chan
->data
;
2131 struct hci_conn
*hcon
= conn
->hcon
;
2132 u8
*pkax
, *pkbx
, *na
, *nb
;
2136 BT_DBG("conn %p", conn
);
2138 if (skb
->len
< sizeof(smp
->rrnd
))
2139 return SMP_INVALID_PARAMS
;
2141 memcpy(smp
->rrnd
, skb
->data
, sizeof(smp
->rrnd
));
2142 skb_pull(skb
, sizeof(smp
->rrnd
));
2144 if (!test_bit(SMP_FLAG_SC
, &smp
->flags
))
2145 return smp_random(smp
);
2148 pkax
= smp
->local_pk
;
2149 pkbx
= smp
->remote_pk
;
2153 pkax
= smp
->remote_pk
;
2154 pkbx
= smp
->local_pk
;
2159 if (smp
->method
== REQ_OOB
) {
2161 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
,
2162 sizeof(smp
->prnd
), smp
->prnd
);
2163 SMP_ALLOW_CMD(smp
, SMP_CMD_DHKEY_CHECK
);
2164 goto mackey_and_ltk
;
2167 /* Passkey entry has special treatment */
2168 if (smp
->method
== REQ_PASSKEY
|| smp
->method
== DSP_PASSKEY
)
2169 return sc_passkey_round(smp
, SMP_CMD_PAIRING_RANDOM
);
2174 err
= smp_f4(smp
->tfm_cmac
, smp
->remote_pk
, smp
->local_pk
,
2177 return SMP_UNSPECIFIED
;
2179 if (memcmp(smp
->pcnf
, cfm
, 16))
2180 return SMP_CONFIRM_FAILED
;
2182 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
, sizeof(smp
->prnd
),
2184 SMP_ALLOW_CMD(smp
, SMP_CMD_DHKEY_CHECK
);
2188 /* Generate MacKey and LTK */
2189 err
= sc_mackey_and_ltk(smp
, smp
->mackey
, smp
->tk
);
2191 return SMP_UNSPECIFIED
;
2193 if (smp
->method
== JUST_WORKS
|| smp
->method
== REQ_OOB
) {
2195 sc_dhkey_check(smp
);
2196 SMP_ALLOW_CMD(smp
, SMP_CMD_DHKEY_CHECK
);
2201 err
= smp_g2(smp
->tfm_cmac
, pkax
, pkbx
, na
, nb
, &passkey
);
2203 return SMP_UNSPECIFIED
;
2205 err
= mgmt_user_confirm_request(hcon
->hdev
, &hcon
->dst
, hcon
->type
,
2206 hcon
->dst_type
, passkey
, 0);
2208 return SMP_UNSPECIFIED
;
2210 set_bit(SMP_FLAG_WAIT_USER
, &smp
->flags
);
2215 static bool smp_ltk_encrypt(struct l2cap_conn
*conn
, u8 sec_level
)
2217 struct smp_ltk
*key
;
2218 struct hci_conn
*hcon
= conn
->hcon
;
2220 key
= hci_find_ltk(hcon
->hdev
, &hcon
->dst
, hcon
->dst_type
, hcon
->role
);
2224 if (smp_ltk_sec_level(key
) < sec_level
)
2227 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND
, &hcon
->flags
))
2230 hci_le_start_enc(hcon
, key
->ediv
, key
->rand
, key
->val
, key
->enc_size
);
2231 hcon
->enc_key_size
= key
->enc_size
;
2233 /* We never store STKs for master role, so clear this flag */
2234 clear_bit(HCI_CONN_STK_ENCRYPT
, &hcon
->flags
);
2239 bool smp_sufficient_security(struct hci_conn
*hcon
, u8 sec_level
,
2240 enum smp_key_pref key_pref
)
2242 if (sec_level
== BT_SECURITY_LOW
)
2245 /* If we're encrypted with an STK but the caller prefers using
2246 * LTK claim insufficient security. This way we allow the
2247 * connection to be re-encrypted with an LTK, even if the LTK
2248 * provides the same level of security. Only exception is if we
2249 * don't have an LTK (e.g. because of key distribution bits).
2251 if (key_pref
== SMP_USE_LTK
&&
2252 test_bit(HCI_CONN_STK_ENCRYPT
, &hcon
->flags
) &&
2253 hci_find_ltk(hcon
->hdev
, &hcon
->dst
, hcon
->dst_type
, hcon
->role
))
2256 if (hcon
->sec_level
>= sec_level
)
2262 static u8
smp_cmd_security_req(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2264 struct smp_cmd_security_req
*rp
= (void *) skb
->data
;
2265 struct smp_cmd_pairing cp
;
2266 struct hci_conn
*hcon
= conn
->hcon
;
2267 struct hci_dev
*hdev
= hcon
->hdev
;
2268 struct smp_chan
*smp
;
2271 BT_DBG("conn %p", conn
);
2273 if (skb
->len
< sizeof(*rp
))
2274 return SMP_INVALID_PARAMS
;
2276 if (hcon
->role
!= HCI_ROLE_MASTER
)
2277 return SMP_CMD_NOTSUPP
;
2279 auth
= rp
->auth_req
& AUTH_REQ_MASK(hdev
);
2281 if (hci_dev_test_flag(hdev
, HCI_SC_ONLY
) && !(auth
& SMP_AUTH_SC
))
2282 return SMP_AUTH_REQUIREMENTS
;
2284 if (hcon
->io_capability
== HCI_IO_NO_INPUT_OUTPUT
)
2285 sec_level
= BT_SECURITY_MEDIUM
;
2287 sec_level
= authreq_to_seclevel(auth
);
2289 if (smp_sufficient_security(hcon
, sec_level
, SMP_USE_LTK
))
2292 if (sec_level
> hcon
->pending_sec_level
)
2293 hcon
->pending_sec_level
= sec_level
;
2295 if (smp_ltk_encrypt(conn
, hcon
->pending_sec_level
))
2298 smp
= smp_chan_create(conn
);
2300 return SMP_UNSPECIFIED
;
2302 if (!hci_dev_test_flag(hdev
, HCI_BONDABLE
) &&
2303 (auth
& SMP_AUTH_BONDING
))
2304 return SMP_PAIRING_NOTSUPP
;
2306 skb_pull(skb
, sizeof(*rp
));
2308 memset(&cp
, 0, sizeof(cp
));
2309 build_pairing_cmd(conn
, &cp
, NULL
, auth
);
2311 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
2312 memcpy(&smp
->preq
[1], &cp
, sizeof(cp
));
2314 smp_send_cmd(conn
, SMP_CMD_PAIRING_REQ
, sizeof(cp
), &cp
);
2315 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RSP
);
2320 int smp_conn_security(struct hci_conn
*hcon
, __u8 sec_level
)
2322 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
2323 struct l2cap_chan
*chan
;
2324 struct smp_chan
*smp
;
2328 BT_DBG("conn %p hcon %p level 0x%2.2x", conn
, hcon
, sec_level
);
2330 /* This may be NULL if there's an unexpected disconnection */
2334 if (!hci_dev_test_flag(hcon
->hdev
, HCI_LE_ENABLED
))
2337 if (smp_sufficient_security(hcon
, sec_level
, SMP_USE_LTK
))
2340 if (sec_level
> hcon
->pending_sec_level
)
2341 hcon
->pending_sec_level
= sec_level
;
2343 if (hcon
->role
== HCI_ROLE_MASTER
)
2344 if (smp_ltk_encrypt(conn
, hcon
->pending_sec_level
))
2349 BT_ERR("SMP security requested but not available");
2353 l2cap_chan_lock(chan
);
2355 /* If SMP is already in progress ignore this request */
2361 smp
= smp_chan_create(conn
);
2367 authreq
= seclevel_to_authreq(sec_level
);
2369 if (hci_dev_test_flag(hcon
->hdev
, HCI_SC_ENABLED
)) {
2370 authreq
|= SMP_AUTH_SC
;
2371 if (hci_dev_test_flag(hcon
->hdev
, HCI_SSP_ENABLED
))
2372 authreq
|= SMP_AUTH_CT2
;
2375 /* Require MITM if IO Capability allows or the security level
2378 if (hcon
->io_capability
!= HCI_IO_NO_INPUT_OUTPUT
||
2379 hcon
->pending_sec_level
> BT_SECURITY_MEDIUM
)
2380 authreq
|= SMP_AUTH_MITM
;
2382 if (hcon
->role
== HCI_ROLE_MASTER
) {
2383 struct smp_cmd_pairing cp
;
2385 build_pairing_cmd(conn
, &cp
, NULL
, authreq
);
2386 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
2387 memcpy(&smp
->preq
[1], &cp
, sizeof(cp
));
2389 smp_send_cmd(conn
, SMP_CMD_PAIRING_REQ
, sizeof(cp
), &cp
);
2390 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RSP
);
2392 struct smp_cmd_security_req cp
;
2393 cp
.auth_req
= authreq
;
2394 smp_send_cmd(conn
, SMP_CMD_SECURITY_REQ
, sizeof(cp
), &cp
);
2395 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_REQ
);
2398 set_bit(SMP_FLAG_INITIATOR
, &smp
->flags
);
2402 l2cap_chan_unlock(chan
);
2406 void smp_cancel_pairing(struct hci_conn
*hcon
)
2408 struct l2cap_conn
*conn
= hcon
->l2cap_data
;
2409 struct l2cap_chan
*chan
;
2410 struct smp_chan
*smp
;
2419 l2cap_chan_lock(chan
);
2423 if (test_bit(SMP_FLAG_COMPLETE
, &smp
->flags
))
2424 smp_failure(conn
, 0);
2426 smp_failure(conn
, SMP_UNSPECIFIED
);
2429 l2cap_chan_unlock(chan
);
2432 static int smp_cmd_encrypt_info(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2434 struct smp_cmd_encrypt_info
*rp
= (void *) skb
->data
;
2435 struct l2cap_chan
*chan
= conn
->smp
;
2436 struct smp_chan
*smp
= chan
->data
;
2438 BT_DBG("conn %p", conn
);
2440 if (skb
->len
< sizeof(*rp
))
2441 return SMP_INVALID_PARAMS
;
2443 SMP_ALLOW_CMD(smp
, SMP_CMD_MASTER_IDENT
);
2445 skb_pull(skb
, sizeof(*rp
));
2447 memcpy(smp
->tk
, rp
->ltk
, sizeof(smp
->tk
));
2452 static int smp_cmd_master_ident(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2454 struct smp_cmd_master_ident
*rp
= (void *) skb
->data
;
2455 struct l2cap_chan
*chan
= conn
->smp
;
2456 struct smp_chan
*smp
= chan
->data
;
2457 struct hci_dev
*hdev
= conn
->hcon
->hdev
;
2458 struct hci_conn
*hcon
= conn
->hcon
;
2459 struct smp_ltk
*ltk
;
2462 BT_DBG("conn %p", conn
);
2464 if (skb
->len
< sizeof(*rp
))
2465 return SMP_INVALID_PARAMS
;
2467 /* Mark the information as received */
2468 smp
->remote_key_dist
&= ~SMP_DIST_ENC_KEY
;
2470 if (smp
->remote_key_dist
& SMP_DIST_ID_KEY
)
2471 SMP_ALLOW_CMD(smp
, SMP_CMD_IDENT_INFO
);
2472 else if (smp
->remote_key_dist
& SMP_DIST_SIGN
)
2473 SMP_ALLOW_CMD(smp
, SMP_CMD_SIGN_INFO
);
2475 skb_pull(skb
, sizeof(*rp
));
2477 authenticated
= (hcon
->sec_level
== BT_SECURITY_HIGH
);
2478 ltk
= hci_add_ltk(hdev
, &hcon
->dst
, hcon
->dst_type
, SMP_LTK
,
2479 authenticated
, smp
->tk
, smp
->enc_key_size
,
2480 rp
->ediv
, rp
->rand
);
2482 if (!(smp
->remote_key_dist
& KEY_DIST_MASK
))
2483 smp_distribute_keys(smp
);
2488 static int smp_cmd_ident_info(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2490 struct smp_cmd_ident_info
*info
= (void *) skb
->data
;
2491 struct l2cap_chan
*chan
= conn
->smp
;
2492 struct smp_chan
*smp
= chan
->data
;
2496 if (skb
->len
< sizeof(*info
))
2497 return SMP_INVALID_PARAMS
;
2499 SMP_ALLOW_CMD(smp
, SMP_CMD_IDENT_ADDR_INFO
);
2501 skb_pull(skb
, sizeof(*info
));
2503 memcpy(smp
->irk
, info
->irk
, 16);
2508 static int smp_cmd_ident_addr_info(struct l2cap_conn
*conn
,
2509 struct sk_buff
*skb
)
2511 struct smp_cmd_ident_addr_info
*info
= (void *) skb
->data
;
2512 struct l2cap_chan
*chan
= conn
->smp
;
2513 struct smp_chan
*smp
= chan
->data
;
2514 struct hci_conn
*hcon
= conn
->hcon
;
2519 if (skb
->len
< sizeof(*info
))
2520 return SMP_INVALID_PARAMS
;
2522 /* Mark the information as received */
2523 smp
->remote_key_dist
&= ~SMP_DIST_ID_KEY
;
2525 if (smp
->remote_key_dist
& SMP_DIST_SIGN
)
2526 SMP_ALLOW_CMD(smp
, SMP_CMD_SIGN_INFO
);
2528 skb_pull(skb
, sizeof(*info
));
2530 /* Strictly speaking the Core Specification (4.1) allows sending
2531 * an empty address which would force us to rely on just the IRK
2532 * as "identity information". However, since such
2533 * implementations are not known of and in order to not over
2534 * complicate our implementation, simply pretend that we never
2535 * received an IRK for such a device.
2537 * The Identity Address must also be a Static Random or Public
2538 * Address, which hci_is_identity_address() checks for.
2540 if (!bacmp(&info
->bdaddr
, BDADDR_ANY
) ||
2541 !hci_is_identity_address(&info
->bdaddr
, info
->addr_type
)) {
2542 BT_ERR("Ignoring IRK with no identity address");
2546 bacpy(&smp
->id_addr
, &info
->bdaddr
);
2547 smp
->id_addr_type
= info
->addr_type
;
2549 if (hci_bdaddr_is_rpa(&hcon
->dst
, hcon
->dst_type
))
2550 bacpy(&rpa
, &hcon
->dst
);
2552 bacpy(&rpa
, BDADDR_ANY
);
2554 smp
->remote_irk
= hci_add_irk(conn
->hcon
->hdev
, &smp
->id_addr
,
2555 smp
->id_addr_type
, smp
->irk
, &rpa
);
2558 if (!(smp
->remote_key_dist
& KEY_DIST_MASK
))
2559 smp_distribute_keys(smp
);
2564 static int smp_cmd_sign_info(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2566 struct smp_cmd_sign_info
*rp
= (void *) skb
->data
;
2567 struct l2cap_chan
*chan
= conn
->smp
;
2568 struct smp_chan
*smp
= chan
->data
;
2569 struct smp_csrk
*csrk
;
2571 BT_DBG("conn %p", conn
);
2573 if (skb
->len
< sizeof(*rp
))
2574 return SMP_INVALID_PARAMS
;
2576 /* Mark the information as received */
2577 smp
->remote_key_dist
&= ~SMP_DIST_SIGN
;
2579 skb_pull(skb
, sizeof(*rp
));
2581 csrk
= kzalloc(sizeof(*csrk
), GFP_KERNEL
);
2583 if (conn
->hcon
->sec_level
> BT_SECURITY_MEDIUM
)
2584 csrk
->type
= MGMT_CSRK_REMOTE_AUTHENTICATED
;
2586 csrk
->type
= MGMT_CSRK_REMOTE_UNAUTHENTICATED
;
2587 memcpy(csrk
->val
, rp
->csrk
, sizeof(csrk
->val
));
2590 smp_distribute_keys(smp
);
2595 static u8
sc_select_method(struct smp_chan
*smp
)
2597 struct l2cap_conn
*conn
= smp
->conn
;
2598 struct hci_conn
*hcon
= conn
->hcon
;
2599 struct smp_cmd_pairing
*local
, *remote
;
2600 u8 local_mitm
, remote_mitm
, local_io
, remote_io
, method
;
2602 if (test_bit(SMP_FLAG_REMOTE_OOB
, &smp
->flags
) ||
2603 test_bit(SMP_FLAG_LOCAL_OOB
, &smp
->flags
))
2606 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2607 * which are needed as inputs to some crypto functions. To get
2608 * the "struct smp_cmd_pairing" from them we need to skip the
2609 * first byte which contains the opcode.
2612 local
= (void *) &smp
->preq
[1];
2613 remote
= (void *) &smp
->prsp
[1];
2615 local
= (void *) &smp
->prsp
[1];
2616 remote
= (void *) &smp
->preq
[1];
2619 local_io
= local
->io_capability
;
2620 remote_io
= remote
->io_capability
;
2622 local_mitm
= (local
->auth_req
& SMP_AUTH_MITM
);
2623 remote_mitm
= (remote
->auth_req
& SMP_AUTH_MITM
);
2625 /* If either side wants MITM, look up the method from the table,
2626 * otherwise use JUST WORKS.
2628 if (local_mitm
|| remote_mitm
)
2629 method
= get_auth_method(smp
, local_io
, remote_io
);
2631 method
= JUST_WORKS
;
2633 /* Don't confirm locally initiated pairing attempts */
2634 if (method
== JUST_CFM
&& test_bit(SMP_FLAG_INITIATOR
, &smp
->flags
))
2635 method
= JUST_WORKS
;
2640 static int smp_cmd_public_key(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2642 struct smp_cmd_public_key
*key
= (void *) skb
->data
;
2643 struct hci_conn
*hcon
= conn
->hcon
;
2644 struct l2cap_chan
*chan
= conn
->smp
;
2645 struct smp_chan
*smp
= chan
->data
;
2646 struct hci_dev
*hdev
= hcon
->hdev
;
2647 struct smp_cmd_pairing_confirm cfm
;
2650 BT_DBG("conn %p", conn
);
2652 if (skb
->len
< sizeof(*key
))
2653 return SMP_INVALID_PARAMS
;
2655 memcpy(smp
->remote_pk
, key
, 64);
2657 if (test_bit(SMP_FLAG_REMOTE_OOB
, &smp
->flags
)) {
2658 err
= smp_f4(smp
->tfm_cmac
, smp
->remote_pk
, smp
->remote_pk
,
2659 smp
->rr
, 0, cfm
.confirm_val
);
2661 return SMP_UNSPECIFIED
;
2663 if (memcmp(cfm
.confirm_val
, smp
->pcnf
, 16))
2664 return SMP_CONFIRM_FAILED
;
2667 /* Non-initiating device sends its public key after receiving
2668 * the key from the initiating device.
2671 err
= sc_send_public_key(smp
);
2676 SMP_DBG("Remote Public Key X: %32phN", smp
->remote_pk
);
2677 SMP_DBG("Remote Public Key Y: %32phN", smp
->remote_pk
+ 32);
2679 if (!compute_ecdh_secret(smp
->remote_pk
, smp
->local_sk
, smp
->dhkey
))
2680 return SMP_UNSPECIFIED
;
2682 SMP_DBG("DHKey %32phN", smp
->dhkey
);
2684 set_bit(SMP_FLAG_REMOTE_PK
, &smp
->flags
);
2686 smp
->method
= sc_select_method(smp
);
2688 BT_DBG("%s selected method 0x%02x", hdev
->name
, smp
->method
);
2690 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2691 if (smp
->method
== JUST_WORKS
|| smp
->method
== JUST_CFM
)
2692 hcon
->pending_sec_level
= BT_SECURITY_MEDIUM
;
2694 hcon
->pending_sec_level
= BT_SECURITY_FIPS
;
2696 if (!memcmp(debug_pk
, smp
->remote_pk
, 64))
2697 set_bit(SMP_FLAG_DEBUG_KEY
, &smp
->flags
);
2699 if (smp
->method
== DSP_PASSKEY
) {
2700 get_random_bytes(&hcon
->passkey_notify
,
2701 sizeof(hcon
->passkey_notify
));
2702 hcon
->passkey_notify
%= 1000000;
2703 hcon
->passkey_entered
= 0;
2704 smp
->passkey_round
= 0;
2705 if (mgmt_user_passkey_notify(hdev
, &hcon
->dst
, hcon
->type
,
2707 hcon
->passkey_notify
,
2708 hcon
->passkey_entered
))
2709 return SMP_UNSPECIFIED
;
2710 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_CONFIRM
);
2711 return sc_passkey_round(smp
, SMP_CMD_PUBLIC_KEY
);
2714 if (smp
->method
== REQ_OOB
) {
2716 smp_send_cmd(conn
, SMP_CMD_PAIRING_RANDOM
,
2717 sizeof(smp
->prnd
), smp
->prnd
);
2719 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RANDOM
);
2725 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_CONFIRM
);
2727 if (smp
->method
== REQ_PASSKEY
) {
2728 if (mgmt_user_passkey_request(hdev
, &hcon
->dst
, hcon
->type
,
2730 return SMP_UNSPECIFIED
;
2731 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_CONFIRM
);
2732 set_bit(SMP_FLAG_WAIT_USER
, &smp
->flags
);
2736 /* The Initiating device waits for the non-initiating device to
2737 * send the confirm value.
2739 if (conn
->hcon
->out
)
2742 err
= smp_f4(smp
->tfm_cmac
, smp
->local_pk
, smp
->remote_pk
, smp
->prnd
,
2743 0, cfm
.confirm_val
);
2745 return SMP_UNSPECIFIED
;
2747 smp_send_cmd(conn
, SMP_CMD_PAIRING_CONFIRM
, sizeof(cfm
), &cfm
);
2748 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RANDOM
);
2753 static int smp_cmd_dhkey_check(struct l2cap_conn
*conn
, struct sk_buff
*skb
)
2755 struct smp_cmd_dhkey_check
*check
= (void *) skb
->data
;
2756 struct l2cap_chan
*chan
= conn
->smp
;
2757 struct hci_conn
*hcon
= conn
->hcon
;
2758 struct smp_chan
*smp
= chan
->data
;
2759 u8 a
[7], b
[7], *local_addr
, *remote_addr
;
2760 u8 io_cap
[3], r
[16], e
[16];
2763 BT_DBG("conn %p", conn
);
2765 if (skb
->len
< sizeof(*check
))
2766 return SMP_INVALID_PARAMS
;
2768 memcpy(a
, &hcon
->init_addr
, 6);
2769 memcpy(b
, &hcon
->resp_addr
, 6);
2770 a
[6] = hcon
->init_addr_type
;
2771 b
[6] = hcon
->resp_addr_type
;
2776 memcpy(io_cap
, &smp
->prsp
[1], 3);
2780 memcpy(io_cap
, &smp
->preq
[1], 3);
2783 memset(r
, 0, sizeof(r
));
2785 if (smp
->method
== REQ_PASSKEY
|| smp
->method
== DSP_PASSKEY
)
2786 put_unaligned_le32(hcon
->passkey_notify
, r
);
2787 else if (smp
->method
== REQ_OOB
)
2788 memcpy(r
, smp
->lr
, 16);
2790 err
= smp_f6(smp
->tfm_cmac
, smp
->mackey
, smp
->rrnd
, smp
->prnd
, r
,
2791 io_cap
, remote_addr
, local_addr
, e
);
2793 return SMP_UNSPECIFIED
;
2795 if (memcmp(check
->e
, e
, 16))
2796 return SMP_DHKEY_CHECK_FAILED
;
2799 if (test_bit(SMP_FLAG_WAIT_USER
, &smp
->flags
)) {
2800 set_bit(SMP_FLAG_DHKEY_PENDING
, &smp
->flags
);
2804 /* Slave sends DHKey check as response to master */
2805 sc_dhkey_check(smp
);
2811 hci_le_start_enc(hcon
, 0, 0, smp
->tk
, smp
->enc_key_size
);
2812 hcon
->enc_key_size
= smp
->enc_key_size
;
2818 static int smp_cmd_keypress_notify(struct l2cap_conn
*conn
,
2819 struct sk_buff
*skb
)
2821 struct smp_cmd_keypress_notify
*kp
= (void *) skb
->data
;
2823 BT_DBG("value 0x%02x", kp
->value
);
2828 static int smp_sig_channel(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
2830 struct l2cap_conn
*conn
= chan
->conn
;
2831 struct hci_conn
*hcon
= conn
->hcon
;
2832 struct smp_chan
*smp
;
2839 if (!hci_dev_test_flag(hcon
->hdev
, HCI_LE_ENABLED
)) {
2840 reason
= SMP_PAIRING_NOTSUPP
;
2844 code
= skb
->data
[0];
2845 skb_pull(skb
, sizeof(code
));
2849 if (code
> SMP_CMD_MAX
)
2852 if (smp
&& !test_and_clear_bit(code
, &smp
->allow_cmd
))
2855 /* If we don't have a context the only allowed commands are
2856 * pairing request and security request.
2858 if (!smp
&& code
!= SMP_CMD_PAIRING_REQ
&& code
!= SMP_CMD_SECURITY_REQ
)
2862 case SMP_CMD_PAIRING_REQ
:
2863 reason
= smp_cmd_pairing_req(conn
, skb
);
2866 case SMP_CMD_PAIRING_FAIL
:
2867 smp_failure(conn
, 0);
2871 case SMP_CMD_PAIRING_RSP
:
2872 reason
= smp_cmd_pairing_rsp(conn
, skb
);
2875 case SMP_CMD_SECURITY_REQ
:
2876 reason
= smp_cmd_security_req(conn
, skb
);
2879 case SMP_CMD_PAIRING_CONFIRM
:
2880 reason
= smp_cmd_pairing_confirm(conn
, skb
);
2883 case SMP_CMD_PAIRING_RANDOM
:
2884 reason
= smp_cmd_pairing_random(conn
, skb
);
2887 case SMP_CMD_ENCRYPT_INFO
:
2888 reason
= smp_cmd_encrypt_info(conn
, skb
);
2891 case SMP_CMD_MASTER_IDENT
:
2892 reason
= smp_cmd_master_ident(conn
, skb
);
2895 case SMP_CMD_IDENT_INFO
:
2896 reason
= smp_cmd_ident_info(conn
, skb
);
2899 case SMP_CMD_IDENT_ADDR_INFO
:
2900 reason
= smp_cmd_ident_addr_info(conn
, skb
);
2903 case SMP_CMD_SIGN_INFO
:
2904 reason
= smp_cmd_sign_info(conn
, skb
);
2907 case SMP_CMD_PUBLIC_KEY
:
2908 reason
= smp_cmd_public_key(conn
, skb
);
2911 case SMP_CMD_DHKEY_CHECK
:
2912 reason
= smp_cmd_dhkey_check(conn
, skb
);
2915 case SMP_CMD_KEYPRESS_NOTIFY
:
2916 reason
= smp_cmd_keypress_notify(conn
, skb
);
2920 BT_DBG("Unknown command code 0x%2.2x", code
);
2921 reason
= SMP_CMD_NOTSUPP
;
2928 smp_failure(conn
, reason
);
2935 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon
->hdev
->name
,
2941 static void smp_teardown_cb(struct l2cap_chan
*chan
, int err
)
2943 struct l2cap_conn
*conn
= chan
->conn
;
2945 BT_DBG("chan %p", chan
);
2948 smp_chan_destroy(conn
);
2951 l2cap_chan_put(chan
);
2954 static void bredr_pairing(struct l2cap_chan
*chan
)
2956 struct l2cap_conn
*conn
= chan
->conn
;
2957 struct hci_conn
*hcon
= conn
->hcon
;
2958 struct hci_dev
*hdev
= hcon
->hdev
;
2959 struct smp_cmd_pairing req
;
2960 struct smp_chan
*smp
;
2962 BT_DBG("chan %p", chan
);
2964 /* Only new pairings are interesting */
2965 if (!test_bit(HCI_CONN_NEW_LINK_KEY
, &hcon
->flags
))
2968 /* Don't bother if we're not encrypted */
2969 if (!test_bit(HCI_CONN_ENCRYPT
, &hcon
->flags
))
2972 /* Only master may initiate SMP over BR/EDR */
2973 if (hcon
->role
!= HCI_ROLE_MASTER
)
2976 /* Secure Connections support must be enabled */
2977 if (!hci_dev_test_flag(hdev
, HCI_SC_ENABLED
))
2980 /* BR/EDR must use Secure Connections for SMP */
2981 if (!test_bit(HCI_CONN_AES_CCM
, &hcon
->flags
) &&
2982 !hci_dev_test_flag(hdev
, HCI_FORCE_BREDR_SMP
))
2985 /* If our LE support is not enabled don't do anything */
2986 if (!hci_dev_test_flag(hdev
, HCI_LE_ENABLED
))
2989 /* Don't bother if remote LE support is not enabled */
2990 if (!lmp_host_le_capable(hcon
))
2993 /* Remote must support SMP fixed chan for BR/EDR */
2994 if (!(conn
->remote_fixed_chan
& L2CAP_FC_SMP_BREDR
))
2997 /* Don't bother if SMP is already ongoing */
3001 smp
= smp_chan_create(conn
);
3003 BT_ERR("%s unable to create SMP context for BR/EDR",
3008 set_bit(SMP_FLAG_SC
, &smp
->flags
);
3010 BT_DBG("%s starting SMP over BR/EDR", hdev
->name
);
3012 /* Prepare and send the BR/EDR SMP Pairing Request */
3013 build_bredr_pairing_cmd(smp
, &req
, NULL
);
3015 smp
->preq
[0] = SMP_CMD_PAIRING_REQ
;
3016 memcpy(&smp
->preq
[1], &req
, sizeof(req
));
3018 smp_send_cmd(conn
, SMP_CMD_PAIRING_REQ
, sizeof(req
), &req
);
3019 SMP_ALLOW_CMD(smp
, SMP_CMD_PAIRING_RSP
);
3022 static void smp_resume_cb(struct l2cap_chan
*chan
)
3024 struct smp_chan
*smp
= chan
->data
;
3025 struct l2cap_conn
*conn
= chan
->conn
;
3026 struct hci_conn
*hcon
= conn
->hcon
;
3028 BT_DBG("chan %p", chan
);
3030 if (hcon
->type
== ACL_LINK
) {
3031 bredr_pairing(chan
);
3038 if (!test_bit(HCI_CONN_ENCRYPT
, &hcon
->flags
))
3041 cancel_delayed_work(&smp
->security_timer
);
3043 smp_distribute_keys(smp
);
3046 static void smp_ready_cb(struct l2cap_chan
*chan
)
3048 struct l2cap_conn
*conn
= chan
->conn
;
3049 struct hci_conn
*hcon
= conn
->hcon
;
3051 BT_DBG("chan %p", chan
);
3053 /* No need to call l2cap_chan_hold() here since we already own
3054 * the reference taken in smp_new_conn_cb(). This is just the
3055 * first time that we tie it to a specific pointer. The code in
3056 * l2cap_core.c ensures that there's no risk this function wont
3057 * get called if smp_new_conn_cb was previously called.
3061 if (hcon
->type
== ACL_LINK
&& test_bit(HCI_CONN_ENCRYPT
, &hcon
->flags
))
3062 bredr_pairing(chan
);
3065 static int smp_recv_cb(struct l2cap_chan
*chan
, struct sk_buff
*skb
)
3069 BT_DBG("chan %p", chan
);
3071 err
= smp_sig_channel(chan
, skb
);
3073 struct smp_chan
*smp
= chan
->data
;
3076 cancel_delayed_work_sync(&smp
->security_timer
);
3078 hci_disconnect(chan
->conn
->hcon
, HCI_ERROR_AUTH_FAILURE
);
3084 static struct sk_buff
*smp_alloc_skb_cb(struct l2cap_chan
*chan
,
3085 unsigned long hdr_len
,
3086 unsigned long len
, int nb
)
3088 struct sk_buff
*skb
;
3090 skb
= bt_skb_alloc(hdr_len
+ len
, GFP_KERNEL
);
3092 return ERR_PTR(-ENOMEM
);
3094 skb
->priority
= HCI_PRIO_MAX
;
3095 bt_cb(skb
)->l2cap
.chan
= chan
;
3100 static const struct l2cap_ops smp_chan_ops
= {
3101 .name
= "Security Manager",
3102 .ready
= smp_ready_cb
,
3103 .recv
= smp_recv_cb
,
3104 .alloc_skb
= smp_alloc_skb_cb
,
3105 .teardown
= smp_teardown_cb
,
3106 .resume
= smp_resume_cb
,
3108 .new_connection
= l2cap_chan_no_new_connection
,
3109 .state_change
= l2cap_chan_no_state_change
,
3110 .close
= l2cap_chan_no_close
,
3111 .defer
= l2cap_chan_no_defer
,
3112 .suspend
= l2cap_chan_no_suspend
,
3113 .set_shutdown
= l2cap_chan_no_set_shutdown
,
3114 .get_sndtimeo
= l2cap_chan_no_get_sndtimeo
,
3117 static inline struct l2cap_chan
*smp_new_conn_cb(struct l2cap_chan
*pchan
)
3119 struct l2cap_chan
*chan
;
3121 BT_DBG("pchan %p", pchan
);
3123 chan
= l2cap_chan_create();
3127 chan
->chan_type
= pchan
->chan_type
;
3128 chan
->ops
= &smp_chan_ops
;
3129 chan
->scid
= pchan
->scid
;
3130 chan
->dcid
= chan
->scid
;
3131 chan
->imtu
= pchan
->imtu
;
3132 chan
->omtu
= pchan
->omtu
;
3133 chan
->mode
= pchan
->mode
;
3135 /* Other L2CAP channels may request SMP routines in order to
3136 * change the security level. This means that the SMP channel
3137 * lock must be considered in its own category to avoid lockdep
3140 atomic_set(&chan
->nesting
, L2CAP_NESTING_SMP
);
3142 BT_DBG("created chan %p", chan
);
3147 static const struct l2cap_ops smp_root_chan_ops
= {
3148 .name
= "Security Manager Root",
3149 .new_connection
= smp_new_conn_cb
,
3151 /* None of these are implemented for the root channel */
3152 .close
= l2cap_chan_no_close
,
3153 .alloc_skb
= l2cap_chan_no_alloc_skb
,
3154 .recv
= l2cap_chan_no_recv
,
3155 .state_change
= l2cap_chan_no_state_change
,
3156 .teardown
= l2cap_chan_no_teardown
,
3157 .ready
= l2cap_chan_no_ready
,
3158 .defer
= l2cap_chan_no_defer
,
3159 .suspend
= l2cap_chan_no_suspend
,
3160 .resume
= l2cap_chan_no_resume
,
3161 .set_shutdown
= l2cap_chan_no_set_shutdown
,
3162 .get_sndtimeo
= l2cap_chan_no_get_sndtimeo
,
3165 static struct l2cap_chan
*smp_add_cid(struct hci_dev
*hdev
, u16 cid
)
3167 struct l2cap_chan
*chan
;
3168 struct smp_dev
*smp
;
3169 struct crypto_cipher
*tfm_aes
;
3170 struct crypto_shash
*tfm_cmac
;
3172 if (cid
== L2CAP_CID_SMP_BREDR
) {
3177 smp
= kzalloc(sizeof(*smp
), GFP_KERNEL
);
3179 return ERR_PTR(-ENOMEM
);
3181 tfm_aes
= crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC
);
3182 if (IS_ERR(tfm_aes
)) {
3183 BT_ERR("Unable to create AES crypto context");
3185 return ERR_CAST(tfm_aes
);
3188 tfm_cmac
= crypto_alloc_shash("cmac(aes)", 0, 0);
3189 if (IS_ERR(tfm_cmac
)) {
3190 BT_ERR("Unable to create CMAC crypto context");
3191 crypto_free_cipher(tfm_aes
);
3193 return ERR_CAST(tfm_cmac
);
3196 smp
->tfm_aes
= tfm_aes
;
3197 smp
->tfm_cmac
= tfm_cmac
;
3198 smp
->min_key_size
= SMP_MIN_ENC_KEY_SIZE
;
3199 smp
->max_key_size
= SMP_MAX_ENC_KEY_SIZE
;
3202 chan
= l2cap_chan_create();
3205 crypto_free_cipher(smp
->tfm_aes
);
3206 crypto_free_shash(smp
->tfm_cmac
);
3209 return ERR_PTR(-ENOMEM
);
3214 l2cap_add_scid(chan
, cid
);
3216 l2cap_chan_set_defaults(chan
);
3218 if (cid
== L2CAP_CID_SMP
) {
3221 hci_copy_identity_address(hdev
, &chan
->src
, &bdaddr_type
);
3223 if (bdaddr_type
== ADDR_LE_DEV_PUBLIC
)
3224 chan
->src_type
= BDADDR_LE_PUBLIC
;
3226 chan
->src_type
= BDADDR_LE_RANDOM
;
3228 bacpy(&chan
->src
, &hdev
->bdaddr
);
3229 chan
->src_type
= BDADDR_BREDR
;
3232 chan
->state
= BT_LISTEN
;
3233 chan
->mode
= L2CAP_MODE_BASIC
;
3234 chan
->imtu
= L2CAP_DEFAULT_MTU
;
3235 chan
->ops
= &smp_root_chan_ops
;
3237 /* Set correct nesting level for a parent/listening channel */
3238 atomic_set(&chan
->nesting
, L2CAP_NESTING_PARENT
);
3243 static void smp_del_chan(struct l2cap_chan
*chan
)
3245 struct smp_dev
*smp
;
3247 BT_DBG("chan %p", chan
);
3252 crypto_free_cipher(smp
->tfm_aes
);
3253 crypto_free_shash(smp
->tfm_cmac
);
3257 l2cap_chan_put(chan
);
3260 static ssize_t
force_bredr_smp_read(struct file
*file
,
3261 char __user
*user_buf
,
3262 size_t count
, loff_t
*ppos
)
3264 struct hci_dev
*hdev
= file
->private_data
;
3267 buf
[0] = hci_dev_test_flag(hdev
, HCI_FORCE_BREDR_SMP
) ? 'Y': 'N';
3270 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
3273 static ssize_t
force_bredr_smp_write(struct file
*file
,
3274 const char __user
*user_buf
,
3275 size_t count
, loff_t
*ppos
)
3277 struct hci_dev
*hdev
= file
->private_data
;
3279 size_t buf_size
= min(count
, (sizeof(buf
)-1));
3282 if (copy_from_user(buf
, user_buf
, buf_size
))
3285 buf
[buf_size
] = '\0';
3286 if (strtobool(buf
, &enable
))
3289 if (enable
== hci_dev_test_flag(hdev
, HCI_FORCE_BREDR_SMP
))
3293 struct l2cap_chan
*chan
;
3295 chan
= smp_add_cid(hdev
, L2CAP_CID_SMP_BREDR
);
3297 return PTR_ERR(chan
);
3299 hdev
->smp_bredr_data
= chan
;
3301 struct l2cap_chan
*chan
;
3303 chan
= hdev
->smp_bredr_data
;
3304 hdev
->smp_bredr_data
= NULL
;
3308 hci_dev_change_flag(hdev
, HCI_FORCE_BREDR_SMP
);
3313 static const struct file_operations force_bredr_smp_fops
= {
3314 .open
= simple_open
,
3315 .read
= force_bredr_smp_read
,
3316 .write
= force_bredr_smp_write
,
3317 .llseek
= default_llseek
,
3320 static ssize_t
le_min_key_size_read(struct file
*file
,
3321 char __user
*user_buf
,
3322 size_t count
, loff_t
*ppos
)
3324 struct hci_dev
*hdev
= file
->private_data
;
3327 snprintf(buf
, sizeof(buf
), "%2u\n", SMP_DEV(hdev
)->min_key_size
);
3329 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, strlen(buf
));
3332 static ssize_t
le_min_key_size_write(struct file
*file
,
3333 const char __user
*user_buf
,
3334 size_t count
, loff_t
*ppos
)
3336 struct hci_dev
*hdev
= file
->private_data
;
3338 size_t buf_size
= min(count
, (sizeof(buf
) - 1));
3341 if (copy_from_user(buf
, user_buf
, buf_size
))
3344 buf
[buf_size
] = '\0';
3346 sscanf(buf
, "%hhu", &key_size
);
3348 if (key_size
> SMP_DEV(hdev
)->max_key_size
||
3349 key_size
< SMP_MIN_ENC_KEY_SIZE
)
3352 SMP_DEV(hdev
)->min_key_size
= key_size
;
3357 static const struct file_operations le_min_key_size_fops
= {
3358 .open
= simple_open
,
3359 .read
= le_min_key_size_read
,
3360 .write
= le_min_key_size_write
,
3361 .llseek
= default_llseek
,
3364 static ssize_t
le_max_key_size_read(struct file
*file
,
3365 char __user
*user_buf
,
3366 size_t count
, loff_t
*ppos
)
3368 struct hci_dev
*hdev
= file
->private_data
;
3371 snprintf(buf
, sizeof(buf
), "%2u\n", SMP_DEV(hdev
)->max_key_size
);
3373 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, strlen(buf
));
3376 static ssize_t
le_max_key_size_write(struct file
*file
,
3377 const char __user
*user_buf
,
3378 size_t count
, loff_t
*ppos
)
3380 struct hci_dev
*hdev
= file
->private_data
;
3382 size_t buf_size
= min(count
, (sizeof(buf
) - 1));
3385 if (copy_from_user(buf
, user_buf
, buf_size
))
3388 buf
[buf_size
] = '\0';
3390 sscanf(buf
, "%hhu", &key_size
);
3392 if (key_size
> SMP_MAX_ENC_KEY_SIZE
||
3393 key_size
< SMP_DEV(hdev
)->min_key_size
)
3396 SMP_DEV(hdev
)->max_key_size
= key_size
;
3401 static const struct file_operations le_max_key_size_fops
= {
3402 .open
= simple_open
,
3403 .read
= le_max_key_size_read
,
3404 .write
= le_max_key_size_write
,
3405 .llseek
= default_llseek
,
3408 int smp_register(struct hci_dev
*hdev
)
3410 struct l2cap_chan
*chan
;
3412 BT_DBG("%s", hdev
->name
);
3414 /* If the controller does not support Low Energy operation, then
3415 * there is also no need to register any SMP channel.
3417 if (!lmp_le_capable(hdev
))
3420 if (WARN_ON(hdev
->smp_data
)) {
3421 chan
= hdev
->smp_data
;
3422 hdev
->smp_data
= NULL
;
3426 chan
= smp_add_cid(hdev
, L2CAP_CID_SMP
);
3428 return PTR_ERR(chan
);
3430 hdev
->smp_data
= chan
;
3432 debugfs_create_file("le_min_key_size", 0644, hdev
->debugfs
, hdev
,
3433 &le_min_key_size_fops
);
3434 debugfs_create_file("le_max_key_size", 0644, hdev
->debugfs
, hdev
,
3435 &le_max_key_size_fops
);
3437 /* If the controller does not support BR/EDR Secure Connections
3438 * feature, then the BR/EDR SMP channel shall not be present.
3440 * To test this with Bluetooth 4.0 controllers, create a debugfs
3441 * switch that allows forcing BR/EDR SMP support and accepting
3442 * cross-transport pairing on non-AES encrypted connections.
3444 if (!lmp_sc_capable(hdev
)) {
3445 debugfs_create_file("force_bredr_smp", 0644, hdev
->debugfs
,
3446 hdev
, &force_bredr_smp_fops
);
3448 /* Flag can be already set here (due to power toggle) */
3449 if (!hci_dev_test_flag(hdev
, HCI_FORCE_BREDR_SMP
))
3453 if (WARN_ON(hdev
->smp_bredr_data
)) {
3454 chan
= hdev
->smp_bredr_data
;
3455 hdev
->smp_bredr_data
= NULL
;
3459 chan
= smp_add_cid(hdev
, L2CAP_CID_SMP_BREDR
);
3461 int err
= PTR_ERR(chan
);
3462 chan
= hdev
->smp_data
;
3463 hdev
->smp_data
= NULL
;
3468 hdev
->smp_bredr_data
= chan
;
3473 void smp_unregister(struct hci_dev
*hdev
)
3475 struct l2cap_chan
*chan
;
3477 if (hdev
->smp_bredr_data
) {
3478 chan
= hdev
->smp_bredr_data
;
3479 hdev
->smp_bredr_data
= NULL
;
3483 if (hdev
->smp_data
) {
3484 chan
= hdev
->smp_data
;
3485 hdev
->smp_data
= NULL
;
3490 #if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3492 static inline void swap_digits(u64
*in
, u64
*out
, unsigned int ndigits
)
3496 for (i
= 0; i
< ndigits
; i
++)
3497 out
[i
] = __swab64(in
[ndigits
- 1 - i
]);
3500 static int __init
test_debug_key(void)
3504 swap_digits((u64
*)debug_sk
, (u64
*)sk
, 4);
3506 if (!generate_ecdh_keys(pk
, sk
))
3509 if (memcmp(sk
, debug_sk
, 32))
3512 if (memcmp(pk
, debug_pk
, 64))
3518 static int __init
test_ah(struct crypto_cipher
*tfm_aes
)
3520 const u8 irk
[16] = {
3521 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3522 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3523 const u8 r
[3] = { 0x94, 0x81, 0x70 };
3524 const u8 exp
[3] = { 0xaa, 0xfb, 0x0d };
3528 err
= smp_ah(tfm_aes
, irk
, r
, res
);
3532 if (memcmp(res
, exp
, 3))
3538 static int __init
test_c1(struct crypto_cipher
*tfm_aes
)
3541 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3542 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3544 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3545 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3546 const u8 preq
[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3547 const u8 pres
[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3548 const u8 _iat
= 0x01;
3549 const u8 _rat
= 0x00;
3550 const bdaddr_t ra
= { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3551 const bdaddr_t ia
= { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3552 const u8 exp
[16] = {
3553 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3554 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3558 err
= smp_c1(tfm_aes
, k
, r
, preq
, pres
, _iat
, &ia
, _rat
, &ra
, res
);
3562 if (memcmp(res
, exp
, 16))
3568 static int __init
test_s1(struct crypto_cipher
*tfm_aes
)
3571 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3572 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3574 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3576 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3577 const u8 exp
[16] = {
3578 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3579 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3583 err
= smp_s1(tfm_aes
, k
, r1
, r2
, res
);
3587 if (memcmp(res
, exp
, 16))
3593 static int __init
test_f4(struct crypto_shash
*tfm_cmac
)
3596 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3597 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3598 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3599 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3601 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3602 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3603 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3604 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3606 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3607 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3609 const u8 exp
[16] = {
3610 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3611 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3615 err
= smp_f4(tfm_cmac
, u
, v
, x
, z
, res
);
3619 if (memcmp(res
, exp
, 16))
3625 static int __init
test_f5(struct crypto_shash
*tfm_cmac
)
3628 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3629 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3630 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3631 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3633 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3634 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3636 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3637 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3638 const u8 a1
[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3639 const u8 a2
[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3640 const u8 exp_ltk
[16] = {
3641 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3642 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3643 const u8 exp_mackey
[16] = {
3644 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3645 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3646 u8 mackey
[16], ltk
[16];
3649 err
= smp_f5(tfm_cmac
, w
, n1
, n2
, a1
, a2
, mackey
, ltk
);
3653 if (memcmp(mackey
, exp_mackey
, 16))
3656 if (memcmp(ltk
, exp_ltk
, 16))
3662 static int __init
test_f6(struct crypto_shash
*tfm_cmac
)
3665 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3666 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3668 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3669 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3671 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3672 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3674 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3675 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3676 const u8 io_cap
[3] = { 0x02, 0x01, 0x01 };
3677 const u8 a1
[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3678 const u8 a2
[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3679 const u8 exp
[16] = {
3680 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3681 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3685 err
= smp_f6(tfm_cmac
, w
, n1
, n2
, r
, io_cap
, a1
, a2
, res
);
3689 if (memcmp(res
, exp
, 16))
3695 static int __init
test_g2(struct crypto_shash
*tfm_cmac
)
3698 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3699 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3700 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3701 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3703 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3704 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3705 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3706 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3708 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3709 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3711 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3712 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3713 const u32 exp_val
= 0x2f9ed5ba % 1000000;
3717 err
= smp_g2(tfm_cmac
, u
, v
, x
, y
, &val
);
3727 static int __init
test_h6(struct crypto_shash
*tfm_cmac
)
3730 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3731 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3732 const u8 key_id
[4] = { 0x72, 0x62, 0x65, 0x6c };
3733 const u8 exp
[16] = {
3734 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3735 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3739 err
= smp_h6(tfm_cmac
, w
, key_id
, res
);
3743 if (memcmp(res
, exp
, 16))
3749 static char test_smp_buffer
[32];
3751 static ssize_t
test_smp_read(struct file
*file
, char __user
*user_buf
,
3752 size_t count
, loff_t
*ppos
)
3754 return simple_read_from_buffer(user_buf
, count
, ppos
, test_smp_buffer
,
3755 strlen(test_smp_buffer
));
3758 static const struct file_operations test_smp_fops
= {
3759 .open
= simple_open
,
3760 .read
= test_smp_read
,
3761 .llseek
= default_llseek
,
3764 static int __init
run_selftests(struct crypto_cipher
*tfm_aes
,
3765 struct crypto_shash
*tfm_cmac
)
3767 ktime_t calltime
, delta
, rettime
;
3768 unsigned long long duration
;
3771 calltime
= ktime_get();
3773 err
= test_debug_key();
3775 BT_ERR("debug_key test failed");
3779 err
= test_ah(tfm_aes
);
3781 BT_ERR("smp_ah test failed");
3785 err
= test_c1(tfm_aes
);
3787 BT_ERR("smp_c1 test failed");
3791 err
= test_s1(tfm_aes
);
3793 BT_ERR("smp_s1 test failed");
3797 err
= test_f4(tfm_cmac
);
3799 BT_ERR("smp_f4 test failed");
3803 err
= test_f5(tfm_cmac
);
3805 BT_ERR("smp_f5 test failed");
3809 err
= test_f6(tfm_cmac
);
3811 BT_ERR("smp_f6 test failed");
3815 err
= test_g2(tfm_cmac
);
3817 BT_ERR("smp_g2 test failed");
3821 err
= test_h6(tfm_cmac
);
3823 BT_ERR("smp_h6 test failed");
3827 rettime
= ktime_get();
3828 delta
= ktime_sub(rettime
, calltime
);
3829 duration
= (unsigned long long) ktime_to_ns(delta
) >> 10;
3831 BT_INFO("SMP test passed in %llu usecs", duration
);
3835 snprintf(test_smp_buffer
, sizeof(test_smp_buffer
),
3836 "PASS (%llu usecs)\n", duration
);
3838 snprintf(test_smp_buffer
, sizeof(test_smp_buffer
), "FAIL\n");
3840 debugfs_create_file("selftest_smp", 0444, bt_debugfs
, NULL
,
3846 int __init
bt_selftest_smp(void)
3848 struct crypto_cipher
*tfm_aes
;
3849 struct crypto_shash
*tfm_cmac
;
3852 tfm_aes
= crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC
);
3853 if (IS_ERR(tfm_aes
)) {
3854 BT_ERR("Unable to create AES crypto context");
3855 return PTR_ERR(tfm_aes
);
3858 tfm_cmac
= crypto_alloc_shash("cmac(aes)", 0, CRYPTO_ALG_ASYNC
);
3859 if (IS_ERR(tfm_cmac
)) {
3860 BT_ERR("Unable to create CMAC crypto context");
3861 crypto_free_cipher(tfm_aes
);
3862 return PTR_ERR(tfm_cmac
);
3865 err
= run_selftests(tfm_aes
, tfm_cmac
);
3867 crypto_free_shash(tfm_cmac
);
3868 crypto_free_cipher(tfm_aes
);