Skip to content

Commit e719906

Browse files
Johan Hedbergjhedberg
authored andcommitted
Bluetooth: Mesh: Remove unnecessary logic for key selection
Key Refresh Phase 2 is analogous to the Key Refresh flag being set. This means that the flag can directly be used as the index to the new/old key two-element array. Signed-off-by: Johan Hedberg <[email protected]>
1 parent 46f8c7f commit e719906

File tree

3 files changed

+29
-38
lines changed

3 files changed

+29
-38
lines changed

subsys/bluetooth/host/mesh/friend.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,23 +312,21 @@ static struct net_buf *create_friend_pdu(struct bt_mesh_friend *frnd,
312312
struct bt_mesh_subnet *sub;
313313
const u8_t *enc, *priv;
314314
struct net_buf *buf;
315-
u8_t nid, key_idx;
315+
u8_t nid;
316316

317317
sub = bt_mesh_subnet_get(frnd->net_idx);
318318
__ASSERT_NO_MSG(sub != NULL);
319319

320320
buf = friend_buf_alloc(info->src);
321321

322-
key_idx = (sub->kr_phase == BT_MESH_KR_PHASE_2);
323-
324322
/* Friend Offer needs master security credentials */
325323
if (info->ctl && TRANS_CTL_OP(sdu->data) == TRANS_CTL_OP_FRIEND_OFFER) {
326-
enc = sub->keys[key_idx].enc;
327-
priv = sub->keys[key_idx].privacy;
328-
nid = sub->keys[key_idx].nid;
324+
enc = sub->keys[sub->kr_flag].enc;
325+
priv = sub->keys[sub->kr_flag].privacy;
326+
nid = sub->keys[sub->kr_flag].nid;
329327
} else {
330-
if (bt_mesh_friend_cred_get(sub->net_idx, frnd->lpn, key_idx,
331-
&nid, &enc, &priv)) {
328+
if (bt_mesh_friend_cred_get(sub, frnd->lpn, &nid, &enc,
329+
&priv)) {
332330
BT_ERR("bt_mesh_friend_cred_get failed");
333331
goto failed;
334332
}

subsys/bluetooth/host/mesh/net.c

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,17 @@ int bt_mesh_friend_cred_del(u16_t net_idx, u16_t addr)
354354
return -ENOENT;
355355
}
356356

357-
int bt_mesh_friend_cred_get(u16_t net_idx, u16_t addr, u8_t idx,
358-
u8_t *nid, const u8_t **enc, const u8_t **priv)
357+
int bt_mesh_friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid,
358+
const u8_t **enc, const u8_t **priv)
359359
{
360360
int i;
361361

362-
BT_DBG("net_idx 0x%04x addr 0x%04x idx %u", net_idx, addr, idx);
362+
BT_DBG("net_idx 0x%04x addr 0x%04x", sub->net_idx, addr);
363363

364364
for (i = 0; i < ARRAY_SIZE(friend_cred); i++) {
365365
struct bt_mesh_friend_cred *cred = &friend_cred[i];
366366

367-
if (cred->net_idx != net_idx) {
367+
if (cred->net_idx != sub->net_idx) {
368368
continue;
369369
}
370370

@@ -373,15 +373,15 @@ int bt_mesh_friend_cred_get(u16_t net_idx, u16_t addr, u8_t idx,
373373
}
374374

375375
if (nid) {
376-
*nid = cred->cred[idx].nid;
376+
*nid = cred->cred[sub->kr_flag].nid;
377377
}
378378

379379
if (enc) {
380-
*enc = cred->cred[idx].enc;
380+
*enc = cred->cred[sub->kr_flag].enc;
381381
}
382382

383383
if (priv) {
384-
*priv = cred->cred[idx].privacy;
384+
*priv = cred->cred[sub->kr_flag].privacy;
385385
}
386386

387387
return 0;
@@ -390,8 +390,8 @@ int bt_mesh_friend_cred_get(u16_t net_idx, u16_t addr, u8_t idx,
390390
return -ENOENT;
391391
}
392392
#else
393-
int bt_mesh_friend_cred_get(u16_t net_idx, u16_t addr, u8_t idx,
394-
u8_t *nid, const u8_t **enc, const u8_t **priv)
393+
int bt_mesh_friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid,
394+
const u8_t **enc, const u8_t **priv)
395395
{
396396
return -ENOENT;
397397
}
@@ -770,7 +770,7 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
770770
bool proxy)
771771
{
772772
const bool ctl = (tx->ctx->app_idx == BT_MESH_KEY_UNUSED);
773-
u8_t nid, idx = (tx->sub->kr_phase == BT_MESH_KR_PHASE_2);
773+
u8_t nid;
774774
const u8_t *enc, *priv;
775775
u8_t *seq;
776776
int err;
@@ -801,22 +801,21 @@ int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
801801
}
802802

803803
if (IS_ENABLED(CONFIG_BT_MESH_LOW_POWER) && tx->friend_cred) {
804-
if (bt_mesh_friend_cred_get(tx->sub->net_idx,
805-
BT_MESH_ADDR_UNASSIGNED,
806-
idx, &nid, &enc, &priv)) {
804+
if (bt_mesh_friend_cred_get(tx->sub, BT_MESH_ADDR_UNASSIGNED,
805+
&nid, &enc, &priv)) {
807806
BT_WARN("Falling back to master credentials");
808807

809808
tx->friend_cred = 0;
810809

811-
nid = tx->sub->keys[idx].nid;
812-
enc = tx->sub->keys[idx].enc;
813-
priv = tx->sub->keys[idx].privacy;
810+
nid = tx->sub->keys[tx->sub->kr_flag].nid;
811+
enc = tx->sub->keys[tx->sub->kr_flag].enc;
812+
priv = tx->sub->keys[tx->sub->kr_flag].privacy;
814813
}
815814
} else {
816815
tx->friend_cred = 0;
817-
nid = tx->sub->keys[idx].nid;
818-
enc = tx->sub->keys[idx].enc;
819-
priv = tx->sub->keys[idx].privacy;
816+
nid = tx->sub->keys[tx->sub->kr_flag].nid;
817+
enc = tx->sub->keys[tx->sub->kr_flag].enc;
818+
priv = tx->sub->keys[tx->sub->kr_flag].privacy;
820819
}
821820

822821
net_buf_simple_push_u8(buf, (nid | (BT_MESH_NET_IVI_TX & 1) << 7));
@@ -1109,15 +1108,9 @@ static void bt_mesh_net_relay(struct net_buf_simple *sbuf,
11091108

11101109
net_buf_add_mem(buf, sbuf->data, sbuf->len);
11111110

1112-
if (rx->sub->kr_phase == BT_MESH_KR_PHASE_2) {
1113-
enc = rx->sub->keys[1].enc;
1114-
priv = rx->sub->keys[1].privacy;
1115-
nid = rx->sub->keys[1].nid;
1116-
} else {
1117-
enc = rx->sub->keys[0].enc;
1118-
priv = rx->sub->keys[0].privacy;
1119-
nid = rx->sub->keys[0].nid;
1120-
}
1111+
enc = rx->sub->keys[rx->sub->kr_flag].enc;
1112+
priv = rx->sub->keys[rx->sub->kr_flag].privacy;
1113+
nid = rx->sub->keys[rx->sub->kr_flag].nid;
11211114

11221115
BT_DBG("Relaying packet. TTL is now %u", TTL(buf->data));
11231116

subsys/bluetooth/host/mesh/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
279279

280280
u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub);
281281

282-
int bt_mesh_friend_cred_get(u16_t net_idx, u16_t addr, u8_t idx,
283-
u8_t *nid, const u8_t **enc, const u8_t **priv);
282+
int bt_mesh_friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid,
283+
const u8_t **enc, const u8_t **priv);
284284
int bt_mesh_friend_cred_set(struct bt_mesh_friend_cred *cred, u8_t idx,
285285
const u8_t net_key[16]);
286286
void bt_mesh_friend_cred_refresh(u16_t net_idx);

0 commit comments

Comments
 (0)