From 2fb56ba74d44b85ba81f1550d432b7997988fd78 Mon Sep 17 00:00:00 2001 From: Trond Einar Snekvik Date: Tue, 3 Nov 2020 09:10:33 +0100 Subject: [PATCH] Bluetooth: Mesh: Account for ASZMIC in encrypt The decision to set aszmic = 1 in the net_tx structure was made after the variable was passed to the crypto context, creating a discrepancy between the two when aszmic is 1. Extracts transport encryption to a separate function. Signed-off-by: Trond Einar Snekvik --- subsys/bluetooth/mesh/transport.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/subsys/bluetooth/mesh/transport.c b/subsys/bluetooth/mesh/transport.c index 9cd6f6d493c..6cd8a54ced2 100644 --- a/subsys/bluetooth/mesh/transport.c +++ b/subsys/bluetooth/mesh/transport.c @@ -597,8 +597,8 @@ static int send_seg(struct bt_mesh_net_tx *net_tx, struct net_buf_simple *sdu, return 0; } -int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, - const struct bt_mesh_send_cb *cb, void *cb_data) +static int trans_encrypt(const struct bt_mesh_net_tx *tx, const uint8_t *key, + struct net_buf_simple *msg) { struct bt_mesh_app_crypto_ctx crypto = { .dev_key = BT_MESH_IS_DEV_KEY(tx->ctx->app_idx), @@ -608,6 +608,17 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, .seq_num = bt_mesh.seq, .iv_index = BT_MESH_NET_IVI_TX, }; + + if (BT_MESH_ADDR_IS_VIRTUAL(tx->ctx->addr)) { + crypto.ad = bt_mesh_va_label_get(tx->ctx->addr); + } + + return bt_mesh_app_encrypt(key, &crypto, msg); +} + +int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, + const struct bt_mesh_send_cb *cb, void *cb_data) +{ const uint8_t *key; uint8_t aid; int err; @@ -649,11 +660,7 @@ int bt_mesh_trans_send(struct bt_mesh_net_tx *tx, struct net_buf_simple *msg, tx->aszmic = 1U; } - if (BT_MESH_ADDR_IS_VIRTUAL(tx->ctx->addr)) { - crypto.ad = bt_mesh_va_label_get(tx->ctx->addr); - } - - err = bt_mesh_app_encrypt(key, &crypto, msg); + err = trans_encrypt(tx, key, msg); if (err) { return err; }