nimble/sm: Use packed structures for master id

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/be16f180
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/be16f180
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/be16f180

Branch: refs/heads/develop
Commit: be16f180a5dcc4021eae9cb94188971e705d293b
Parents: 2842853
Author: Szymon Janc <[email protected]>
Authored: Tue Jan 17 19:24:34 2017 +0100
Committer: Szymon Janc <[email protected]>
Committed: Wed Jan 25 15:44:15 2017 +0100

----------------------------------------------------------------------
 net/nimble/host/src/ble_sm.c                | 36 +++++++++++++++---------
 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, 29 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/be16f180/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 7da2f18..81f6824 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -1721,7 +1721,7 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct 
ble_sm_result *res,
     struct ble_sm_id_addr_info addr_info;
     struct ble_hs_conn_addrs addrs;
     struct ble_sm_sign_info sign_info;
-    struct ble_sm_master_id master_id;
+    struct ble_sm_master_id *master_id;
     struct ble_sm_enc_info *enc_info;
     struct ble_sm_id_info id_info;
     struct ble_hs_conn *conn;
@@ -1763,21 +1763,30 @@ ble_sm_key_exch_exec(struct ble_sm_proc *proc, struct 
ble_sm_result *res,
         }
 
         /* Send master identification. */
-        rc = ble_sm_gen_ediv(&master_id.ediv);
-        if (rc != 0) {
+        master_id = ble_sm_cmd_get(BLE_SM_OP_MASTER_ID, sizeof(*master_id),
+                                   &txom);
+        if (!master_id) {
+            rc = BLE_HS_ENOMEM;
             goto err;
         }
-        rc = ble_sm_gen_master_id_rand(&master_id.rand_val);
+
+        rc = ble_sm_gen_ediv(&master_id->ediv);
         if (rc != 0) {
             goto err;
         }
-        rc = ble_sm_master_id_tx(proc->conn_handle, &master_id);
+        rc = ble_sm_gen_master_id_rand(&master_id->rand_val);
         if (rc != 0) {
             goto err;
         }
+
         proc->our_keys.ediv_rand_valid = 1;
-        proc->our_keys.rand_val = master_id.rand_val;
-        proc->our_keys.ediv = master_id.ediv;
+        proc->our_keys.rand_val = master_id->rand_val;
+        proc->our_keys.ediv = master_id->ediv;
+
+        rc = ble_sm_tx(proc->conn_handle, txom);
+        if (rc != 0) {
+            goto err;
+        }
     }
 
     if (our_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
@@ -1894,18 +1903,18 @@ static void
 ble_sm_master_id_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
                     struct ble_sm_result *res)
 {
-    struct ble_sm_master_id cmd;
+    struct ble_sm_master_id *cmd;
     struct ble_sm_proc *proc;
 
-    res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_MASTER_ID_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_master_id_parse((*om)->om_data, (*om)->om_len, &cmd);
-    BLE_SM_LOG_CMD(0, "master id", conn_handle, ble_sm_master_id_log, &cmd);
+    cmd = (struct ble_sm_master_id *)(*om)->om_data;
+    BLE_SM_LOG_CMD(0, "master id", conn_handle, ble_sm_master_id_log, cmd);
 
     ble_hs_lock();
 
@@ -1916,8 +1925,9 @@ ble_sm_master_id_rx(uint16_t conn_handle, uint8_t op, 
struct os_mbuf **om,
     } else {
         proc->rx_key_flags &= ~BLE_SM_KE_F_MASTER_ID;
         proc->peer_keys.ediv_rand_valid = 1;
-        proc->peer_keys.ediv = cmd.ediv;
-        proc->peer_keys.rand_val = cmd.rand_val;
+
+        proc->peer_keys.ediv = le16toh(cmd->ediv);
+        proc->peer_keys.rand_val = le64toh(cmd->rand_val);
 
         ble_sm_key_rxed(proc, res);
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/be16f180/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 a5b0bc0..f7e218f 100644
--- a/net/nimble/host/src/ble_sm_cmd.c
+++ b/net/nimble/host/src/ble_sm_cmd.c
@@ -385,7 +385,7 @@ ble_sm_master_id_write(void *payload, int len, struct 
ble_sm_master_id *cmd)
 {
     uint8_t *u8ptr;
 
-    BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_MASTER_ID_SZ);
+    BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + sizeof(struct 
ble_sm_master_id));
 
     u8ptr = payload;
 
@@ -400,7 +400,7 @@ ble_sm_master_id_tx(uint16_t conn_handle, struct 
ble_sm_master_id *cmd)
     struct os_mbuf *txom;
     int rc;
 
-    rc = ble_sm_init_req(BLE_SM_MASTER_ID_SZ, &txom);
+    rc = ble_sm_init_req(sizeof(struct ble_sm_master_id), &txom);
     if (rc != 0) {
         return BLE_HS_ENOMEM;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/be16f180/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 e70c10a..2447c19 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -124,11 +124,10 @@ struct ble_sm_enc_info {
  * | EDIV                               | 2                 |
  * | RAND                               | 8                 |
  */
-#define BLE_SM_MASTER_ID_SZ         10
 struct ble_sm_master_id {
     uint16_t ediv;
     uint64_t rand_val;
-};
+} __attribute__((packed));
 
 /**
  * | Parameter                          | Size (octets)     |

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/be16f180/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 6448fc3..21aa443 100644
--- a/net/nimble/host/test/src/ble_sm_test_util.c
+++ b/net/nimble/host/test/src/ble_sm_test_util.c
@@ -499,12 +499,12 @@ ble_sm_test_util_rx_master_id(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_MASTER_ID_SZ);
+        BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct 
ble_sm_master_id));
 
     om = ble_hs_mbuf_l2cap_pkt();
     TEST_ASSERT_FATAL(om != NULL);
 
-    payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_MASTER_ID_SZ;
+    payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_master_id);
 
     v = os_mbuf_extend(om, payload_len);
     TEST_ASSERT_FATAL(v != NULL);
@@ -741,7 +741,7 @@ ble_sm_test_util_verify_tx_master_id(struct 
ble_sm_master_id *exp_cmd)
 
     ble_hs_test_util_tx_all();
     om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_MASTER_ID,
-                                        BLE_SM_MASTER_ID_SZ);
+                                        sizeof(struct ble_sm_master_id));
     ble_sm_master_id_parse(om->om_data, om->om_len, &cmd);
 
     TEST_ASSERT(cmd.ediv == exp_cmd->ediv);

Reply via email to