Skip to content

Commit fe39c7b

Browse files
holtmannjhedberg
authored andcommitted
Bluetooth: Use __le64 type for LE random numbers
The random numbers in Bluetooth Low Energy are 64-bit numbers and should also be little endian since the HCI specification is little endian. Change the whole Low Energy pairing to use __le64 instead of a byte array. Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
1 parent a3172b7 commit fe39c7b

9 files changed

Lines changed: 29 additions & 32 deletions

File tree

‎include/net/bluetooth/hci.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ struct hci_cp_le_conn_update {
12341234
#define HCI_OP_LE_START_ENC 0x2019
12351235
struct hci_cp_le_start_enc {
12361236
__le16 handle;
1237-
__u8 rand[8];
1237+
__le64 rand;
12381238
__le16 ediv;
12391239
__u8 ltk[16];
12401240
} __packed;
@@ -1646,7 +1646,7 @@ struct hci_ev_le_conn_complete {
16461646
#define HCI_EV_LE_LTK_REQ 0x05
16471647
struct hci_ev_le_ltk_req {
16481648
__le16 handle;
1649-
__u8 random[8];
1649+
__le64 rand;
16501650
__le16 ediv;
16511651
} __packed;
16521652

‎include/net/bluetooth/hci_core.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct smp_ltk {
9999
u8 type;
100100
u8 enc_size;
101101
__le16 ediv;
102-
u8 rand[8];
102+
__le64 rand;
103103
u8 val[16];
104104
};
105105

@@ -828,11 +828,11 @@ void hci_link_keys_clear(struct hci_dev *hdev);
828828
struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
829829
int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
830830
bdaddr_t *bdaddr, u8 *val, u8 type, u8 pin_len);
831-
struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8],
831+
struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand,
832832
bool master);
833833
struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
834834
u8 addr_type, u8 type, u8 authenticated,
835-
u8 tk[16], u8 enc_size, __le16 ediv, u8 rand[8]);
835+
u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand);
836836
struct smp_ltk *hci_find_ltk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
837837
u8 addr_type, bool master);
838838
int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
@@ -1293,7 +1293,7 @@ struct hci_sec_filter {
12931293

12941294
void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
12951295
u16 latency, u16 to_multiplier);
1296-
void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
1296+
void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
12971297
__u8 ltk[16]);
12981298

12991299
int hci_update_random_address(struct hci_request *req, bool require_privacy,

‎include/net/bluetooth/mgmt.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ struct mgmt_ltk_info {
187187
__u8 master;
188188
__u8 enc_size;
189189
__le16 ediv;
190-
__u8 rand[8];
190+
__le64 rand;
191191
__u8 val[16];
192192
} __packed;
193193

‎net/bluetooth/hci_conn.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
231231
hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
232232
}
233233

234-
void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
234+
void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
235235
__u8 ltk[16])
236236
{
237237
struct hci_dev *hdev = conn->hdev;
@@ -242,9 +242,9 @@ void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
242242
memset(&cp, 0, sizeof(cp));
243243

244244
cp.handle = cpu_to_le16(conn->handle);
245-
memcpy(cp.ltk, ltk, sizeof(cp.ltk));
245+
cp.rand = rand;
246246
cp.ediv = ediv;
247-
memcpy(cp.rand, rand, sizeof(cp.rand));
247+
memcpy(cp.ltk, ltk, sizeof(cp.ltk));
248248

249249
hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
250250
}

‎net/bluetooth/hci_core.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,10 @@ static int long_term_keys_show(struct seq_file *f, void *ptr)
765765
hci_dev_lock(hdev);
766766
list_for_each_safe(p, n, &hdev->long_term_keys) {
767767
struct smp_ltk *ltk = list_entry(p, struct smp_ltk, list);
768-
seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %*phN %*phN\n",
768+
seq_printf(f, "%pMR (type %u) %u 0x%02x %u %.4x %.16llx %*phN\n",
769769
&ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
770770
ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
771-
8, ltk->rand, 16, ltk->val);
771+
__le64_to_cpu(ltk->rand), 16, ltk->val);
772772
}
773773
hci_dev_unlock(hdev);
774774

@@ -2921,14 +2921,13 @@ static bool ltk_type_master(u8 type)
29212921
return false;
29222922
}
29232923

2924-
struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, u8 rand[8],
2924+
struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, __le16 ediv, __le64 rand,
29252925
bool master)
29262926
{
29272927
struct smp_ltk *k;
29282928

29292929
list_for_each_entry(k, &hdev->long_term_keys, list) {
2930-
if (k->ediv != ediv ||
2931-
memcmp(rand, k->rand, sizeof(k->rand)))
2930+
if (k->ediv != ediv || k->rand != rand)
29322931
continue;
29332932

29342933
if (ltk_type_master(k->type) != master)
@@ -3046,7 +3045,7 @@ int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,
30463045

30473046
struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
30483047
u8 addr_type, u8 type, u8 authenticated,
3049-
u8 tk[16], u8 enc_size, __le16 ediv, u8 rand[8])
3048+
u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand)
30503049
{
30513050
struct smp_ltk *key, *old_key;
30523051
bool master = ltk_type_master(type);
@@ -3066,9 +3065,9 @@ struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
30663065
memcpy(key->val, tk, sizeof(key->val));
30673066
key->authenticated = authenticated;
30683067
key->ediv = ediv;
3068+
key->rand = rand;
30693069
key->enc_size = enc_size;
30703070
key->type = type;
3071-
memcpy(key->rand, rand, sizeof(key->rand));
30723071

30733072
return key;
30743073
}

‎net/bluetooth/hci_event.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3843,7 +3843,7 @@ static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
38433843
if (conn == NULL)
38443844
goto not_found;
38453845

3846-
ltk = hci_find_ltk(hdev, ev->ediv, ev->random, conn->out);
3846+
ltk = hci_find_ltk(hdev, ev->ediv, ev->rand, conn->out);
38473847
if (ltk == NULL)
38483848
goto not_found;
38493849

‎net/bluetooth/mgmt.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5025,11 +5025,11 @@ void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key)
50255025
ev.key.type = key->authenticated;
50265026
ev.key.enc_size = key->enc_size;
50275027
ev.key.ediv = key->ediv;
5028+
ev.key.rand = key->rand;
50285029

50295030
if (key->type == HCI_SMP_LTK)
50305031
ev.key.master = 1;
50315032

5032-
memcpy(ev.key.rand, key->rand, sizeof(key->rand));
50335033
memcpy(ev.key.val, key->val, sizeof(key->val));
50345034

50355035
mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);

‎net/bluetooth/smp.c‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,9 @@ static void random_work(struct work_struct *work)
517517
}
518518

519519
if (hcon->out) {
520-
u8 stk[16], rand[8];
521-
__le16 ediv;
522-
523-
memset(rand, 0, sizeof(rand));
524-
ediv = 0;
520+
u8 stk[16];
521+
__le64 rand = 0;
522+
__le16 ediv = 0;
525523

526524
smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key);
527525
swap128(key, stk);
@@ -537,11 +535,9 @@ static void random_work(struct work_struct *work)
537535
hci_le_start_enc(hcon, ediv, rand, stk);
538536
hcon->enc_key_size = smp->enc_key_size;
539537
} else {
540-
u8 stk[16], r[16], rand[8];
541-
__le16 ediv;
542-
543-
memset(rand, 0, sizeof(rand));
544-
ediv = 0;
538+
u8 stk[16], r[16];
539+
__le64 rand = 0;
540+
__le16 ediv = 0;
545541

546542
swap128(smp->prnd, r);
547543
smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
@@ -1205,20 +1201,22 @@ int smp_distribute_keys(struct l2cap_conn *conn)
12051201
struct smp_ltk *ltk;
12061202
u8 authenticated;
12071203
__le16 ediv;
1204+
__le64 rand;
12081205

12091206
get_random_bytes(enc.ltk, sizeof(enc.ltk));
12101207
get_random_bytes(&ediv, sizeof(ediv));
1211-
get_random_bytes(ident.rand, sizeof(ident.rand));
1208+
get_random_bytes(&rand, sizeof(rand));
12121209

12131210
smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
12141211

12151212
authenticated = hcon->sec_level == BT_SECURITY_HIGH;
12161213
ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
12171214
HCI_SMP_LTK_SLAVE, authenticated, enc.ltk,
1218-
smp->enc_key_size, ediv, ident.rand);
1215+
smp->enc_key_size, ediv, rand);
12191216
smp->slave_ltk = ltk;
12201217

12211218
ident.ediv = ediv;
1219+
ident.rand = rand;
12221220

12231221
smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
12241222

‎net/bluetooth/smp.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct smp_cmd_encrypt_info {
7878
#define SMP_CMD_MASTER_IDENT 0x07
7979
struct smp_cmd_master_ident {
8080
__le16 ediv;
81-
__u8 rand[8];
81+
__le64 rand;
8282
} __packed;
8383

8484
#define SMP_CMD_IDENT_INFO 0x08

0 commit comments

Comments
 (0)