nimble/sm: Use packed structures for pub key

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

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

----------------------------------------------------------------------
 net/nimble/host/src/ble_sm_cmd.c            |  4 ++--
 net/nimble/host/src/ble_sm_priv.h           |  3 +--
 net/nimble/host/src/ble_sm_sc.c             | 28 ++++++++++++++++--------
 net/nimble/host/test/src/ble_sm_test_util.c |  6 ++---
 4 files changed, 25 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6ab4a714/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 b4c88ce..427c8d7 100644
--- a/net/nimble/host/src/ble_sm_cmd.c
+++ b/net/nimble/host/src/ble_sm_cmd.c
@@ -654,7 +654,7 @@ ble_sm_public_key_write(void *payload, int len, struct 
ble_sm_public_key *cmd)
 {
     uint8_t *u8ptr;
 
-    if (len < sizeof(struct ble_sm_hdr) + BLE_SM_PUBLIC_KEY_SZ) {
+    if (len < sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_public_key)) {
         return BLE_HS_EMSGSIZE;
     }
 
@@ -675,7 +675,7 @@ ble_sm_public_key_tx(uint16_t conn_handle, struct 
ble_sm_public_key *cmd)
     struct os_mbuf *txom;
     int rc;
 
-    rc = ble_sm_init_req(BLE_SM_PUBLIC_KEY_SZ, &txom);
+    rc = ble_sm_init_req(sizeof(struct ble_sm_public_key), &txom);
     if (rc != 0) {
         return BLE_HS_ENOMEM;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6ab4a714/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 57f45c9..131385b 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -178,11 +178,10 @@ struct ble_sm_sec_req {
  * | Public Key X                       | 32                |
  * | Public Key Y                       | 32                |
  */
-#define BLE_SM_PUBLIC_KEY_SZ        64
 struct ble_sm_public_key {
     uint8_t x[32];
     uint8_t y[32];
-};
+} __attribute__((packed));
 
 /**
  * | Parameter                          | Size (octets)     |

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6ab4a714/net/nimble/host/src/ble_sm_sc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_sc.c b/net/nimble/host/src/ble_sm_sc.c
index a88ffc2..b8ced1a 100644
--- a/net/nimble/host/src/ble_sm_sc.c
+++ b/net/nimble/host/src/ble_sm_sc.c
@@ -500,7 +500,8 @@ void
 ble_sm_sc_public_key_exec(struct ble_sm_proc *proc, struct ble_sm_result *res,
                           void *arg)
 {
-    struct ble_sm_public_key cmd;
+    struct ble_sm_public_key *cmd;
+    struct os_mbuf *txom;
     uint8_t ioact;
 
     res->app_status = ble_sm_sc_ensure_keys_generated();
@@ -510,9 +511,18 @@ ble_sm_sc_public_key_exec(struct ble_sm_proc *proc, struct 
ble_sm_result *res,
         return;
     }
 
-    memcpy(cmd.x, ble_sm_sc_pub_key.u8 + 0, 32);
-    memcpy(cmd.y, ble_sm_sc_pub_key.u8 + 32, 32);
-    res->app_status = ble_sm_public_key_tx(proc->conn_handle, &cmd);
+    cmd = ble_sm_cmd_get(BLE_SM_OP_PAIR_PUBLIC_KEY, sizeof(*cmd), &txom);
+    if (!cmd) {
+        res->app_status = BLE_HS_ENOMEM;
+        res->enc_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+        return;
+    }
+
+    memcpy(cmd->x, ble_sm_sc_pub_key.u8 + 0, 32);
+    memcpy(cmd->y, ble_sm_sc_pub_key.u8 + 32, 32);
+
+    res->app_status = ble_sm_tx(proc->conn_handle, txom);
     if (res->app_status != 0) {
         res->enc_cb = 1;
         res->sm_err = BLE_SM_ERR_UNSPECIFIED;
@@ -539,12 +549,12 @@ void
 ble_sm_sc_public_key_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
                         struct ble_sm_result *res)
 {
-    struct ble_sm_public_key cmd;
+    struct ble_sm_public_key *cmd;
     struct ble_sm_proc *proc;
     uint8_t ioact;
     int rc;
 
-    res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_PUBLIC_KEY_SZ);
+    res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*cmd));
     if (res->app_status != 0) {
         res->enc_cb = 1;
         return;
@@ -557,8 +567,8 @@ ble_sm_sc_public_key_rx(uint16_t conn_handle, uint8_t op, 
struct os_mbuf **om,
         return;
     }
 
-    ble_sm_public_key_parse((*om)->om_data, (*om)->om_len, &cmd);
-    BLE_SM_LOG_CMD(0, "public key", conn_handle, ble_sm_public_key_log, &cmd);
+    cmd = (struct ble_sm_public_key *)(*om)->om_data;
+    BLE_SM_LOG_CMD(0, "public key", conn_handle, ble_sm_public_key_log, cmd);
 
     ble_hs_lock();
     proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_PUBLIC_KEY, -1,
@@ -567,7 +577,7 @@ ble_sm_sc_public_key_rx(uint16_t conn_handle, uint8_t op, 
struct os_mbuf **om,
         res->app_status = BLE_HS_ENOENT;
         res->sm_err = BLE_SM_ERR_UNSPECIFIED;
     } else {
-        proc->pub_key_peer = cmd;
+        memcpy(&proc->pub_key_peer, cmd, sizeof(*cmd));
         rc = ble_sm_alg_gen_dhkey(proc->pub_key_peer.x,
                                   proc->pub_key_peer.y,
                                   ble_sm_sc_priv_key.u32,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6ab4a714/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 e1f00bf..f501ba8 100644
--- a/net/nimble/host/test/src/ble_sm_test_util.c
+++ b/net/nimble/host/test/src/ble_sm_test_util.c
@@ -409,12 +409,12 @@ ble_sm_test_util_rx_public_key(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_PUBLIC_KEY_SZ);
+        BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct 
ble_sm_public_key));
 
     om = ble_hs_mbuf_l2cap_pkt();
     TEST_ASSERT_FATAL(om != NULL);
 
-    payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_PUBLIC_KEY_SZ;
+    payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_public_key);
 
     v = os_mbuf_extend(om, payload_len);
     TEST_ASSERT_FATAL(v != NULL);
@@ -695,7 +695,7 @@ ble_sm_test_util_verify_tx_public_key(
     ble_hs_test_util_tx_all();
 
     om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_PUBLIC_KEY,
-                                              BLE_SM_PUBLIC_KEY_SZ);
+                                        sizeof(struct ble_sm_public_key));
     ble_sm_public_key_parse(om->om_data, om->om_len, &cmd);
 
     TEST_ASSERT(memcmp(cmd.x, exp_cmd->x, sizeof cmd.x) == 0);

Reply via email to