Skip to content

Commit 3a0259b

Browse files
vcgomesGustavo F. Padovan
authored andcommitted
Bluetooth: Add support for using the crypto subsystem
This will allow using the crypto subsystem for encrypting data. As SMP (Security Manager Protocol) is implemented almost entirely on the host side and the crypto module already implements the needed methods (AES-128), it makes sense to use it. There's now a new module option to enable/disable SMP support. Signed-off-by: Vinicius Costa Gomes <vinicius.gomes@openbossa.org> Signed-off-by: Anderson Briglia <anderson.briglia@openbossa.org> Signed-off-by: Gustavo F. Padovan <padovan@profusion.mobi>
1 parent 88ba43b commit 3a0259b

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

‎include/net/bluetooth/hci_core.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ struct hci_dev {
177177

178178
__u16 init_last_cmd;
179179

180+
struct crypto_blkcipher *tfm;
181+
180182
struct inquiry_cache inq_cache;
181183
struct hci_conn_hash conn_hash;
182184
struct list_head blacklist;

‎net/bluetooth/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ menuconfig BT
2222
BNEP Module (Bluetooth Network Encapsulation Protocol)
2323
CMTP Module (CAPI Message Transport Protocol)
2424
HIDP Module (Human Interface Device Protocol)
25+
SMP Module (Security Manager Protocol)
2526

2627
Say Y here to compile Bluetooth support into the kernel or say M to
2728
compile it as module (bluetooth).
@@ -36,11 +37,18 @@ if BT != n
3637
config BT_L2CAP
3738
bool "L2CAP protocol support"
3839
select CRC16
40+
select CRYPTO
41+
select CRYPTO_BLKCIPHER
42+
select CRYPTO_AES
43+
select CRYPTO_ECB
3944
help
4045
L2CAP (Logical Link Control and Adaptation Protocol) provides
4146
connection oriented and connection-less data transport. L2CAP
4247
support is required for most Bluetooth applications.
4348

49+
Also included is support for SMP (Security Manager Protocol) which
50+
is the security layer on top of LE (Low Energy) links.
51+
4452
config BT_SCO
4553
bool "SCO links support"
4654
help

‎net/bluetooth/hci_core.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/notifier.h>
4343
#include <linux/rfkill.h>
4444
#include <linux/timer.h>
45+
#include <linux/crypto.h>
4546
#include <net/sock.h>
4647

4748
#include <asm/system.h>
@@ -59,6 +60,8 @@ static void hci_tx_task(unsigned long arg);
5960

6061
static DEFINE_RWLOCK(hci_task_lock);
6162

63+
static int enable_smp;
64+
6265
/* HCI device list */
6366
LIST_HEAD(hci_dev_list);
6467
DEFINE_RWLOCK(hci_dev_list_lock);
@@ -1274,6 +1277,14 @@ int hci_add_adv_entry(struct hci_dev *hdev,
12741277
return 0;
12751278
}
12761279

1280+
static struct crypto_blkcipher *alloc_cypher(void)
1281+
{
1282+
if (enable_smp)
1283+
return crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1284+
1285+
return ERR_PTR(-ENOTSUPP);
1286+
}
1287+
12771288
/* Register HCI device */
12781289
int hci_register_dev(struct hci_dev *hdev)
12791290
{
@@ -1358,6 +1369,11 @@ int hci_register_dev(struct hci_dev *hdev)
13581369
if (!hdev->workqueue)
13591370
goto nomem;
13601371

1372+
hdev->tfm = alloc_cypher();
1373+
if (IS_ERR(hdev->tfm))
1374+
BT_INFO("Failed to load transform for ecb(aes): %ld",
1375+
PTR_ERR(hdev->tfm));
1376+
13611377
hci_register_sysfs(hdev);
13621378

13631379
hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
@@ -1406,6 +1422,9 @@ int hci_unregister_dev(struct hci_dev *hdev)
14061422
!test_bit(HCI_SETUP, &hdev->flags))
14071423
mgmt_index_removed(hdev->id);
14081424

1425+
if (!IS_ERR(hdev->tfm))
1426+
crypto_free_blkcipher(hdev->tfm);
1427+
14091428
hci_notify(hdev, HCI_DEV_UNREG);
14101429

14111430
if (hdev->rfkill) {
@@ -2242,3 +2261,6 @@ static void hci_cmd_task(unsigned long arg)
22422261
}
22432262
}
22442263
}
2264+
2265+
module_param(enable_smp, bool, 0644);
2266+
MODULE_PARM_DESC(enable_smp, "Enable SMP support (LE only)");

‎net/bluetooth/smp.c‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,13 @@ static void smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
154154

155155
int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
156156
{
157+
struct hci_conn *hcon = conn->hcon;
157158
__u8 authreq;
158159

159-
BT_DBG("conn %p hcon %p level 0x%2.2x", conn, conn->hcon, sec_level);
160+
BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
161+
162+
if (IS_ERR(hcon->hdev->tfm))
163+
return 1;
160164

161165
switch (sec_level) {
162166
case BT_SECURITY_MEDIUM:
@@ -174,7 +178,7 @@ int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
174178
return 1;
175179
}
176180

177-
if (conn->hcon->link_mode & HCI_LM_MASTER) {
181+
if (hcon->link_mode & HCI_LM_MASTER) {
178182
struct smp_cmd_pairing cp;
179183
cp.io_capability = 0x00;
180184
cp.oob_flag = 0x00;
@@ -198,6 +202,12 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
198202
__u8 reason;
199203
int err = 0;
200204

205+
if (IS_ERR(conn->hcon->hdev->tfm)) {
206+
err = PTR_ERR(conn->hcon->hdev->tfm);
207+
reason = SMP_PAIRING_NOTSUPP;
208+
goto done;
209+
}
210+
201211
skb_pull(skb, sizeof(code));
202212

203213
switch (code) {
@@ -233,11 +243,15 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
233243
BT_DBG("Unknown command code 0x%2.2x", code);
234244

235245
reason = SMP_CMD_NOTSUPP;
236-
smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
237-
&reason);
238246
err = -EOPNOTSUPP;
247+
goto done;
239248
}
240249

250+
done:
251+
if (reason)
252+
smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
253+
&reason);
254+
241255
kfree_skb(skb);
242256
return err;
243257
}

0 commit comments

Comments
 (0)