nimble/sm: Use packed structures for encrypt info
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/28428530 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/28428530 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/28428530 Branch: refs/heads/develop Commit: 284285304f0df393b1bf3fcee705856fc6113db9 Parents: 19d27a1 Author: Szymon Janc <[email protected]> Authored: Tue Jan 17 19:24:32 2017 +0100 Committer: Szymon Janc <[email protected]> Committed: Wed Jan 25 15:44:15 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/src/ble_sm.c | 31 ++++++++++++++++-------- net/nimble/host/src/ble_sm_cmd.c | 4 +-- net/nimble/host/src/ble_sm_priv.h | 3 +-- net/nimble/host/test/src/ble_sm_test_util.c | 6 ++--- 4 files changed, 27 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28428530/net/nimble/host/src/ble_sm.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c index b6738b0..7da2f18 100644 --- a/net/nimble/host/src/ble_sm.c +++ b/net/nimble/host/src/ble_sm.c @@ -1722,12 +1722,13 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, struct ble_hs_conn_addrs addrs; struct ble_sm_sign_info sign_info; struct ble_sm_master_id master_id; - struct ble_sm_enc_info enc_info; + struct ble_sm_enc_info *enc_info; struct ble_sm_id_info id_info; struct ble_hs_conn *conn; uint8_t init_key_dist; uint8_t resp_key_dist; uint8_t our_key_dist; + struct os_mbuf *txom; const uint8_t *irk; int rc; @@ -1740,16 +1741,26 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, if (our_key_dist & BLE_SM_PAIR_KEY_DIST_ENC) { /* Send encryption information. */ - rc = ble_sm_gen_ltk(proc, enc_info.ltk); - if (rc != 0) { + enc_info = ble_sm_cmd_get(BLE_SM_OP_ENC_INFO, sizeof(*enc_info), &txom); + if (!enc_info) { + rc = BLE_HS_ENOMEM; goto err; } - rc = ble_sm_enc_info_tx(proc->conn_handle, &enc_info); + + rc = ble_sm_gen_ltk(proc, enc_info->ltk); if (rc != 0) { + os_mbuf_free_chain(txom); goto err; } + + /* store LTK before sending since ble_sm_tx consumes tx mbuf */ + memcpy(proc->our_keys.ltk, enc_info->ltk, 16); proc->our_keys.ltk_valid = 1; - memcpy(proc->our_keys.ltk, enc_info.ltk, 16); + + rc = ble_sm_tx(proc->conn_handle, txom); + if (rc != 0) { + goto err; + } /* Send master identification. */ rc = ble_sm_gen_ediv(&master_id.ediv); @@ -1849,18 +1860,18 @@ static void ble_sm_enc_info_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, struct ble_sm_result *res) { - struct ble_sm_enc_info cmd; + struct ble_sm_enc_info *cmd; struct ble_sm_proc *proc; - res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_ENC_INFO_SZ); + res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*cmd)); if (res->app_status != 0) { res->sm_err = BLE_SM_ERR_UNSPECIFIED; res->enc_cb = 1; return; } - ble_sm_enc_info_parse((*om)->om_data, (*om)->om_len, &cmd); - BLE_SM_LOG_CMD(0, "enc info", conn_handle, ble_sm_enc_info_log, &cmd); + cmd = (struct ble_sm_enc_info *)(*om)->om_data; + BLE_SM_LOG_CMD(0, "enc info", conn_handle, ble_sm_enc_info_log, cmd); ble_hs_lock(); @@ -1871,7 +1882,7 @@ ble_sm_enc_info_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, } else { proc->rx_key_flags &= ~BLE_SM_KE_F_ENC_INFO; proc->peer_keys.ltk_valid = 1; - memcpy(proc->peer_keys.ltk, cmd.ltk, 16); + memcpy(proc->peer_keys.ltk, cmd->ltk, 16); ble_sm_key_rxed(proc, res); } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28428530/net/nimble/host/src/ble_sm_cmd.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_cmd.c b/net/nimble/host/src/ble_sm_cmd.c index bca2ef0..a5b0bc0 100644 --- a/net/nimble/host/src/ble_sm_cmd.c +++ b/net/nimble/host/src/ble_sm_cmd.c @@ -331,7 +331,7 @@ ble_sm_enc_info_write(void *payload, int len, struct ble_sm_enc_info *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_ENC_INFO_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_enc_info)); u8ptr = payload; @@ -345,7 +345,7 @@ ble_sm_enc_info_tx(uint16_t conn_handle, struct ble_sm_enc_info *cmd) struct os_mbuf *txom; int rc; - rc = ble_sm_init_req(BLE_SM_ENC_INFO_SZ, &txom); + rc = ble_sm_init_req(sizeof(struct ble_sm_enc_info), &txom); if (rc != 0) { return BLE_HS_ENOMEM; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28428530/net/nimble/host/src/ble_sm_priv.h ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_priv.h b/net/nimble/host/src/ble_sm_priv.h index 80a2294..e70c10a 100644 --- a/net/nimble/host/src/ble_sm_priv.h +++ b/net/nimble/host/src/ble_sm_priv.h @@ -113,10 +113,9 @@ struct ble_sm_pair_fail { * | (Code=0x06) | 1 | * | ltk | 16 | */ -#define BLE_SM_ENC_INFO_SZ 16 struct ble_sm_enc_info { uint8_t ltk[16]; -}; +} __attribute__((packed)); /** * | Parameter | Size (octets) | http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28428530/net/nimble/host/test/src/ble_sm_test_util.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/test/src/ble_sm_test_util.c b/net/nimble/host/test/src/ble_sm_test_util.c index 490fe35..6448fc3 100644 --- a/net/nimble/host/test/src/ble_sm_test_util.c +++ b/net/nimble/host/test/src/ble_sm_test_util.c @@ -469,12 +469,12 @@ ble_sm_test_util_rx_enc_info(uint16_t conn_handle, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_ENC_INFO_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_enc_info)); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_ENC_INFO_SZ; + payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_enc_info); v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -724,7 +724,7 @@ ble_sm_test_util_verify_tx_enc_info(struct ble_sm_enc_info *exp_cmd) ble_hs_test_util_tx_all(); om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_ENC_INFO, - BLE_SM_ENC_INFO_SZ); + sizeof(struct ble_sm_enc_info)); ble_sm_enc_info_parse(om->om_data, om->om_len, &cmd); TEST_ASSERT(memcmp(cmd.ltk, exp_cmd->ltk, 16) == 0);
