Skip to content

Commit bc2abd5

Browse files
wopu-otjhedberg
authored andcommitted
Bluetooth: host: Fold consecutive calls to bt_rand into one
Calls to bt_rand can be expensive, folding consecutive calls into one reduces the overhead. Signed-off-by: Wolfgang Puffitsch <[email protected]>
1 parent d50fb31 commit bc2abd5

File tree

1 file changed

+14
-12
lines changed
  • subsys/bluetooth/host

1 file changed

+14
-12
lines changed

subsys/bluetooth/host/smp.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,13 +1890,14 @@ static void legacy_distribute_keys(struct bt_smp *smp)
18901890
struct bt_smp_encrypt_info *info;
18911891
struct bt_smp_master_ident *ident;
18921892
struct net_buf *buf;
1893-
u8_t key[16];
1894-
u8_t rand[8];
1895-
u8_t ediv[2];
1893+
/* Use struct to get randomness in single call to bt_rand */
1894+
struct {
1895+
u8_t key[16];
1896+
u8_t rand[8];
1897+
u8_t ediv[2];
1898+
} rand;
18961899

1897-
bt_rand(key, sizeof(key));
1898-
bt_rand(&rand, sizeof(rand));
1899-
bt_rand(&ediv, sizeof(ediv));
1900+
bt_rand((void *)&rand, sizeof(rand));
19001901

19011902
buf = smp_create_pdu(smp, BT_SMP_CMD_ENCRYPT_INFO,
19021903
sizeof(*info));
@@ -1908,7 +1909,7 @@ static void legacy_distribute_keys(struct bt_smp *smp)
19081909
info = net_buf_add(buf, sizeof(*info));
19091910

19101911
/* distributed only enc_size bytes of key */
1911-
memcpy(info->ltk, key, keys->enc_size);
1912+
memcpy(info->ltk, rand.key, keys->enc_size);
19121913
if (keys->enc_size < sizeof(info->ltk)) {
19131914
(void)memset(info->ltk + keys->enc_size, 0,
19141915
sizeof(info->ltk) - keys->enc_size);
@@ -1924,18 +1925,19 @@ static void legacy_distribute_keys(struct bt_smp *smp)
19241925
}
19251926

19261927
ident = net_buf_add(buf, sizeof(*ident));
1927-
memcpy(ident->rand, rand, sizeof(ident->rand));
1928-
memcpy(ident->ediv, ediv, sizeof(ident->ediv));
1928+
memcpy(ident->rand, rand.rand, sizeof(ident->rand));
1929+
memcpy(ident->ediv, rand.ediv, sizeof(ident->ediv));
19291930

19301931
smp_send(smp, buf, smp_ident_sent, NULL);
19311932

19321933
if (atomic_test_bit(smp->flags, SMP_FLAG_BOND)) {
19331934
bt_keys_add_type(keys, BT_KEYS_SLAVE_LTK);
19341935

1935-
memcpy(keys->slave_ltk.val, key,
1936+
memcpy(keys->slave_ltk.val, rand.key,
19361937
sizeof(keys->slave_ltk.val));
1937-
memcpy(keys->slave_ltk.rand, rand, sizeof(rand));
1938-
memcpy(keys->slave_ltk.ediv, ediv,
1938+
memcpy(keys->slave_ltk.rand, rand.rand,
1939+
sizeof(keys->slave_ltk.rand));
1940+
memcpy(keys->slave_ltk.ediv, rand.ediv,
19391941
sizeof(keys->slave_ltk.ediv));
19401942
}
19411943
}

0 commit comments

Comments
 (0)