You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/06/05 11:51:54 UTC

[07/43] incubator-mynewt-core git commit: BLE Host - Move state repetition go->process_result.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9b09325/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
new file mode 100644
index 0000000..8100e24
--- /dev/null
+++ b/net/nimble/host/src/ble_sm_cmd.c
@@ -0,0 +1,633 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include "console/console.h"
+#include "nimble/ble.h"
+#include "nimble/nimble_opt.h"
+#include "ble_hs_priv.h"
+
+#if NIMBLE_OPT(SM)
+
+static int
+ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    BLE_HS_DBG_ASSERT(ble_hs_thread_safe());
+
+    STATS_INC(ble_l2cap_stats, sm_tx);
+
+    rc = ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
+                                         &conn, &chan);
+    if (rc == 0) {
+        BLE_HS_LOG(DEBUG, "ble_sm_tx(): ");
+        ble_hs_misc_log_mbuf(txom);
+        BLE_HS_LOG(DEBUG, "\n");
+        rc = ble_l2cap_tx(conn, chan, txom);
+    }
+
+    return rc;
+}
+
+static int
+ble_sm_init_req(uint16_t initial_sz, struct os_mbuf **out_txom)
+{
+    void *buf;
+    int rc;
+
+    *out_txom = ble_hs_misc_pkthdr();
+    if (*out_txom == NULL) {
+        rc = BLE_HS_ENOMEM;
+        goto err;
+    }
+
+    buf = os_mbuf_extend(*out_txom, BLE_SM_HDR_SZ + initial_sz);
+    if (buf == NULL) {
+        rc = BLE_HS_ENOMEM;
+        goto err;
+    }
+
+    return 0;
+
+err:
+    os_mbuf_free_chain(*out_txom);
+    *out_txom = NULL;
+    return rc;
+}
+
+void
+ble_sm_pair_cmd_parse(void *payload, int len, struct ble_sm_pair_cmd *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_PAIR_CMD_SZ);
+
+    u8ptr = payload;
+    cmd->io_cap = u8ptr[0];
+    cmd->oob_data_flag = u8ptr[1];
+    cmd->authreq = u8ptr[2];
+    cmd->max_enc_key_size = u8ptr[3];
+    cmd->init_key_dist = u8ptr[4];
+    cmd->resp_key_dist = u8ptr[5];
+}
+
+int
+ble_sm_pair_cmd_is_valid(struct ble_sm_pair_cmd *cmd)
+{
+    if (cmd->io_cap >= BLE_SM_IO_CAP_RESERVED) {
+        return 0;
+    }
+
+    if (cmd->oob_data_flag >= BLE_SM_PAIR_OOB_RESERVED) {
+        return 0;
+    }
+
+    if (cmd->authreq & BLE_SM_PAIR_AUTHREQ_RESERVED) {
+        return 0;
+    }
+
+    if (cmd->max_enc_key_size < BLE_SM_PAIR_KEY_SZ_MIN ||
+        cmd->max_enc_key_size > BLE_SM_PAIR_KEY_SZ_MAX) {
+
+        return 0;
+    }
+
+    if (cmd->init_key_dist & BLE_SM_PAIR_KEY_DIST_RESERVED) {
+        return 0;
+    }
+
+    if (cmd->resp_key_dist & BLE_SM_PAIR_KEY_DIST_RESERVED) {
+        return 0;
+    }
+
+    return 1;
+}
+
+void
+ble_sm_pair_cmd_write(void *payload, int len, int is_req,
+                      struct ble_sm_pair_cmd *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ);
+
+    u8ptr = payload;
+    u8ptr[0] = is_req ? BLE_SM_OP_PAIR_REQ : BLE_SM_OP_PAIR_RSP;
+    u8ptr[1] = cmd->io_cap;
+    u8ptr[2] = cmd->oob_data_flag;
+    u8ptr[3] = cmd->authreq;
+    u8ptr[4] = cmd->max_enc_key_size;
+    u8ptr[5] = cmd->init_key_dist;
+    u8ptr[6] = cmd->resp_key_dist;
+}
+
+int
+ble_sm_pair_cmd_tx(uint16_t conn_handle, int is_req,
+                   struct ble_sm_pair_cmd *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_PAIR_CMD_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    ble_sm_pair_cmd_write(txom->om_data, txom->om_len, is_req, cmd);
+    BLE_HS_DBG_ASSERT(ble_sm_pair_cmd_is_valid(cmd));
+
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_pair_confirm_parse(void *payload, int len,
+                          struct ble_sm_pair_confirm *cmd)
+{
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_PAIR_CONFIRM_SZ);
+    memcpy(cmd->value, payload, sizeof cmd->value);
+}
+
+void
+ble_sm_pair_confirm_write(void *payload, int len,
+                          struct ble_sm_pair_confirm *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >=
+                      BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ);
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_SM_OP_PAIR_CONFIRM;
+    memcpy(u8ptr + BLE_SM_HDR_SZ, cmd->value, sizeof cmd->value);
+}
+
+int
+ble_sm_pair_confirm_tx(uint16_t conn_handle,
+                       struct ble_sm_pair_confirm *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_PAIR_CONFIRM_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    ble_sm_pair_confirm_write(txom->om_data, txom->om_len, cmd);
+
+    BLE_HS_LOG(DEBUG, "ble_sm_pair_confirm_tx(): ");
+    ble_hs_misc_log_mbuf(txom);
+    BLE_HS_LOG(DEBUG, "\n");
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_pair_random_parse(void *payload, int len,
+                         struct ble_sm_pair_random *cmd)
+{
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_PAIR_RANDOM_SZ);
+    memcpy(cmd->value, payload, sizeof cmd->value);
+}
+
+void
+ble_sm_pair_random_write(void *payload, int len,
+                         struct ble_sm_pair_random *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >=
+                      BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ);
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_SM_OP_PAIR_RANDOM;
+    memcpy(u8ptr + BLE_SM_HDR_SZ, cmd->value, sizeof cmd->value);
+}
+
+int
+ble_sm_pair_random_tx(uint16_t conn_handle,
+                      struct ble_sm_pair_random *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_PAIR_RANDOM_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    ble_sm_pair_random_write(txom->om_data, txom->om_len, cmd);
+
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_pair_fail_parse(void *payload, int len,
+                       struct ble_sm_pair_fail *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_PAIR_FAIL_SZ);
+
+    u8ptr = payload;
+    cmd->reason = u8ptr[0];
+}
+
+void
+ble_sm_pair_fail_write(void *payload, int len, struct ble_sm_pair_fail *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_PAIR_FAIL_SZ);
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_SM_OP_PAIR_FAIL;
+    u8ptr[1] = cmd->reason;
+}
+
+/* XXX: Should not require locked. */
+int
+ble_sm_pair_fail_tx(uint16_t conn_handle, uint8_t reason)
+{
+    struct ble_sm_pair_fail cmd;
+    struct os_mbuf *txom;
+    int rc;
+
+    BLE_HS_DBG_ASSERT(reason > 0 && reason < BLE_SM_ERR_MAX_PLUS_1);
+
+    rc = ble_sm_init_req(BLE_SM_PAIR_FAIL_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    cmd.reason = reason;
+    ble_sm_pair_fail_write(txom->om_data, txom->om_len, &cmd);
+
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_enc_info_parse(void *payload, int len, struct ble_sm_enc_info *cmd)
+{
+    uint8_t *u8ptr = payload;
+    memcpy(cmd->ltk_le, u8ptr, 16);
+}
+
+int
+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);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    txom->om_data[0] = BLE_SM_OP_ENC_INFO;
+    memcpy(txom->om_data + 1, cmd->ltk_le, sizeof cmd->ltk_le);
+    
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_master_iden_parse(void *payload, int len,
+                         struct ble_sm_master_iden *cmd)
+{
+    uint8_t *u8ptr = payload;
+    cmd->ediv = le16toh(u8ptr);
+    cmd->rand_val = le64toh(u8ptr + 2);
+}
+
+int
+ble_sm_master_iden_tx(uint16_t conn_handle, struct ble_sm_master_iden *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_MASTER_IDEN_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    txom->om_data[0] = BLE_SM_OP_MASTER_ID;
+    htole16(txom->om_data + 1, cmd->ediv);
+    htole64(txom->om_data + 3, cmd->rand_val);
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_iden_info_parse(void *payload, int len, struct ble_sm_iden_info *cmd)
+{
+    uint8_t *u8ptr = payload;
+    memcpy(cmd->irk_le, u8ptr, 16);
+}
+
+int
+ble_sm_iden_info_tx(uint16_t conn_handle, struct ble_sm_iden_info *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_IDEN_INFO_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    txom->om_data[0] = BLE_SM_OP_IDENTITY_INFO;
+    memcpy(txom->om_data + 1, cmd->irk_le, sizeof cmd->irk_le);
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_iden_addr_parse(void *payload, int len,
+                       struct ble_sm_iden_addr_info *cmd)
+{
+    uint8_t *u8ptr = payload;
+    cmd->addr_type = *u8ptr;
+    memcpy(cmd->bd_addr_le, u8ptr + 1, 6);
+}
+
+int
+ble_sm_iden_addr_tx(uint16_t conn_handle, struct ble_sm_iden_addr_info *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_IDEN_ADDR_INFO_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    txom->om_data[0] = BLE_SM_OP_IDENTITY_ADDR_INFO;
+    txom->om_data[1] = cmd->addr_type;
+    memcpy(txom->om_data + 2, cmd->bd_addr_le, sizeof cmd->bd_addr_le);
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_signing_info_parse(void *payload, int len,
+                         struct ble_sm_signing_info *cmd)
+{
+    uint8_t *u8ptr = payload;
+    memcpy(cmd->sig_key_le, u8ptr, 16);
+}
+
+int
+ble_sm_signing_info_tx(uint16_t conn_handle, struct ble_sm_signing_info *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_SIGNING_INFO_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    txom->om_data[0] = BLE_SM_OP_SIGN_INFO;
+    memcpy(txom->om_data + 1, cmd->sig_key_le, sizeof cmd->sig_key_le);
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+void
+ble_sm_sec_req_parse(void *payload, int len, struct ble_sm_sec_req *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_SEC_REQ_SZ);
+
+    u8ptr = payload;
+    cmd->authreq = *u8ptr;
+}
+
+void
+ble_sm_sec_req_write(void *payload, int len, struct ble_sm_sec_req *cmd)
+{
+    uint8_t *u8ptr;
+
+    BLE_HS_DBG_ASSERT(len >= BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ);
+
+    u8ptr = payload;
+
+    u8ptr[0] = BLE_SM_OP_SEC_REQ;
+    u8ptr[1] = cmd->authreq;
+}
+
+int
+ble_sm_sec_req_tx(uint16_t conn_handle, struct ble_sm_sec_req *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_SEC_REQ_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    ble_sm_sec_req_write(txom->om_data, txom->om_len, cmd);
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_sm_public_key_parse(void *payload, int len, struct ble_sm_public_key *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_SM_PUBLIC_KEY_SZ) {
+        return BLE_HS_EBADDATA;
+    }
+
+    u8ptr = payload;
+
+    memcpy(cmd->x, u8ptr, sizeof cmd->x);
+    u8ptr += sizeof cmd->x;
+
+    memcpy(cmd->y, u8ptr, sizeof cmd->y);
+    u8ptr += sizeof cmd->y;
+
+    return 0;
+}
+
+int
+ble_sm_public_key_write(void *payload, int len, struct ble_sm_public_key *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ) {
+        return BLE_HS_EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    *u8ptr = BLE_SM_OP_PAIR_PUBLIC_KEY;
+    u8ptr++;
+
+    memcpy(u8ptr, cmd->x, sizeof cmd->x);
+    memcpy(u8ptr + 32, cmd->y, sizeof cmd->y);
+
+    return 0;
+}
+
+int
+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);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    rc = ble_sm_public_key_write(txom->om_data, txom->om_len, cmd);
+    assert(rc == 0);
+
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+int
+ble_sm_dhkey_check_parse(void *payload, int len,
+                         struct ble_sm_dhkey_check *cmd)
+{
+    if (len < BLE_SM_DHKEY_CHECK_SZ) {
+        return BLE_HS_EBADDATA;
+    }
+
+    memcpy(cmd->value, payload, sizeof cmd->value);
+
+    return 0;
+}
+
+int
+ble_sm_dhkey_check_write(void *payload, int len,
+                         struct ble_sm_dhkey_check *cmd)
+{
+    uint8_t *u8ptr;
+
+    if (len < BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ) {
+        return BLE_HS_EMSGSIZE;
+    }
+
+    u8ptr = payload;
+
+    *u8ptr = BLE_SM_OP_PAIR_DHKEY_CHECK;
+    u8ptr++;
+
+    memcpy(u8ptr, cmd->value, sizeof cmd->value);
+
+    return 0;
+}
+
+int
+ble_sm_dhkey_check_tx(uint16_t conn_handle, struct ble_sm_dhkey_check *cmd)
+{
+    struct os_mbuf *txom;
+    int rc;
+
+    rc = ble_sm_init_req(BLE_SM_DHKEY_CHECK_SZ, &txom);
+    if (rc != 0) {
+        rc = BLE_HS_ENOMEM;
+        goto done;
+    }
+
+    rc = ble_sm_dhkey_check_write(txom->om_data, txom->om_len, cmd);
+    assert(rc == 0);
+
+    rc = ble_sm_tx(conn_handle, txom);
+    txom = NULL;
+
+done:
+    os_mbuf_free_chain(txom);
+    return rc;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9b09325/net/nimble/host/src/ble_sm_lgcy.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_lgcy.c b/net/nimble/host/src/ble_sm_lgcy.c
index 01a09bd..9748d3b 100644
--- a/net/nimble/host/src/ble_sm_lgcy.c
+++ b/net/nimble/host/src/ble_sm_lgcy.c
@@ -57,17 +57,17 @@ static const uint8_t ble_sm_lgcy_resp_pka[5 /*init*/ ][5 /*resp */] =
 };
 
 int
-ble_sm_lgcy_passkey_action(struct ble_l2cap_sm_proc *proc)
+ble_sm_lgcy_passkey_action(struct ble_sm_proc *proc)
 {
     int action;
 
     if (proc->pair_req.oob_data_flag && proc->pair_rsp.oob_data_flag) {
         action = BLE_GAP_PKACT_OOB;
-    } else if (!(proc->pair_req.authreq & BLE_L2CAP_SM_PAIR_AUTHREQ_MITM) ||
-               !(proc->pair_rsp.authreq & BLE_L2CAP_SM_PAIR_AUTHREQ_MITM)) {
+    } else if (!(proc->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_MITM) ||
+               !(proc->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_MITM)) {
 
         action = BLE_GAP_PKACT_NONE;
-    } else if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) {
+    } else if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
         action = ble_sm_lgcy_init_pka[proc->pair_req.io_cap]
                                      [proc->pair_rsp.io_cap];
     } else {
@@ -77,18 +77,18 @@ ble_sm_lgcy_passkey_action(struct ble_l2cap_sm_proc *proc)
 
     switch (action) {
     case BLE_GAP_PKACT_NONE:
-        proc->pair_alg = BLE_L2CAP_SM_PAIR_ALG_JW;
+        proc->pair_alg = BLE_SM_PAIR_ALG_JW;
         break;
 
     case BLE_GAP_PKACT_OOB:
-        proc->pair_alg = BLE_L2CAP_SM_PAIR_ALG_OOB;
-        proc->flags |= BLE_L2CAP_SM_PROC_F_AUTHENTICATED;
+        proc->pair_alg = BLE_SM_PAIR_ALG_OOB;
+        proc->flags |= BLE_SM_PROC_F_AUTHENTICATED;
         break;
 
     case BLE_GAP_PKACT_INPUT:
     case BLE_GAP_PKACT_DISP:
-        proc->pair_alg = BLE_L2CAP_SM_PAIR_ALG_PASSKEY;
-        proc->flags |= BLE_L2CAP_SM_PROC_F_AUTHENTICATED;
+        proc->pair_alg = BLE_SM_PAIR_ALG_PASSKEY;
+        proc->flags |= BLE_SM_PROC_F_AUTHENTICATED;
         break;
 
     default:
@@ -100,7 +100,7 @@ ble_sm_lgcy_passkey_action(struct ble_l2cap_sm_proc *proc)
 }
 
 static int
-ble_sm_lgcy_confirm_prepare_args(struct ble_l2cap_sm_proc *proc,
+ble_sm_lgcy_confirm_prepare_args(struct ble_sm_proc *proc,
                                  uint8_t *k, uint8_t *preq, uint8_t *pres,
                                  uint8_t *iat, uint8_t *rat,
                                  uint8_t *ia, uint8_t *ra)
@@ -111,7 +111,7 @@ ble_sm_lgcy_confirm_prepare_args(struct ble_l2cap_sm_proc *proc,
 
     conn = ble_hs_conn_find(proc->conn_handle);
     if (conn != NULL) {
-        if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) {
+        if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
             *iat = BLE_ADDR_TYPE_PUBLIC; /* XXX: Support random addresses. */
             memcpy(ia, ble_hs_our_dev.public_addr, 6);
 
@@ -132,24 +132,23 @@ ble_sm_lgcy_confirm_prepare_args(struct ble_l2cap_sm_proc *proc,
 
     memcpy(k, proc->tk, sizeof proc->tk);
 
-    ble_l2cap_sm_pair_cmd_write(
-        preq, BLE_L2CAP_SM_HDR_SZ + BLE_L2CAP_SM_PAIR_CMD_SZ, 1,
+    ble_sm_pair_cmd_write(
+        preq, BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ, 1,
         &proc->pair_req);
 
-    ble_l2cap_sm_pair_cmd_write(
-        pres, BLE_L2CAP_SM_HDR_SZ + BLE_L2CAP_SM_PAIR_CMD_SZ, 0,
+    ble_sm_pair_cmd_write(
+        pres, BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ, 0,
         &proc->pair_rsp);
 
     return 0;
 }
 
 void
-ble_sm_lgcy_confirm_go(struct ble_l2cap_sm_proc *proc,
-                       struct ble_l2cap_sm_result *res)
+ble_sm_lgcy_confirm_go(struct ble_sm_proc *proc, struct ble_sm_result *res)
 {
-    struct ble_l2cap_sm_pair_confirm cmd;
-    uint8_t preq[BLE_L2CAP_SM_HDR_SZ + BLE_L2CAP_SM_PAIR_CMD_SZ];
-    uint8_t pres[BLE_L2CAP_SM_HDR_SZ + BLE_L2CAP_SM_PAIR_CMD_SZ];
+    struct ble_sm_pair_confirm cmd;
+    uint8_t preq[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ];
+    uint8_t pres[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ];
     uint8_t k[16];
     uint8_t ia[6];
     uint8_t ra[6];
@@ -157,44 +156,42 @@ ble_sm_lgcy_confirm_go(struct ble_l2cap_sm_proc *proc,
     uint8_t rat;
     int rc;
 
-    res->do_tx = 0;
-
     rc = ble_sm_lgcy_confirm_prepare_args(proc, k, preq, pres,
                                           &iat, &rat, ia, ra);
     if (rc != 0) {
         goto err;
     }
 
-    rc = ble_l2cap_sm_alg_c1(k, ble_l2cap_sm_our_pair_rand(proc),
+    rc = ble_sm_alg_c1(k, ble_sm_our_pair_rand(proc),
                              preq, pres, iat, rat, ia, ra, cmd.value);
     if (rc != 0) {
         goto err;
     }
 
-    rc = ble_l2cap_sm_pair_confirm_tx(proc->conn_handle, &cmd);
+    rc = ble_sm_pair_confirm_tx(proc->conn_handle, &cmd);
     if (rc != 0) {
         goto err;
     }
 
-    if (!(proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR)) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_RANDOM;
+    if (!(proc->flags & BLE_SM_PROC_F_INITIATOR)) {
+        proc->state = BLE_SM_PROC_STATE_RANDOM;
     }
 
     return;
 
 err:
     res->app_status = rc;
-    res->do_cb = 1;
-    res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
+    res->enc_cb = 1;
+    res->sm_err = BLE_SM_ERR_UNSPECIFIED;
 }
 
 static int
-ble_l2cap_sm_gen_stk(struct ble_l2cap_sm_proc *proc)
+ble_sm_gen_stk(struct ble_sm_proc *proc)
 {
     uint8_t key[16];
     int rc;
 
-    rc = ble_l2cap_sm_alg_s1(proc->tk, proc->rands, proc->randm, key);
+    rc = ble_sm_alg_s1(proc->tk, proc->rands, proc->randm, key);
     if (rc != 0) {
         return rc;
     }
@@ -205,34 +202,32 @@ ble_l2cap_sm_gen_stk(struct ble_l2cap_sm_proc *proc)
 }
 
 void
-ble_sm_lgcy_random_go(struct ble_l2cap_sm_proc *proc,
-                      struct ble_l2cap_sm_result *res)
+ble_sm_lgcy_random_go(struct ble_sm_proc *proc,
+                      struct ble_sm_result *res)
 {
-    struct ble_l2cap_sm_pair_random cmd;
+    struct ble_sm_pair_random cmd;
     int rc;
 
-    memcpy(cmd.value, ble_l2cap_sm_our_pair_rand(proc), 16);
+    memcpy(cmd.value, ble_sm_our_pair_rand(proc), 16);
 
-    rc = ble_l2cap_sm_pair_random_tx(proc->conn_handle, &cmd);
+    rc = ble_sm_pair_random_tx(proc->conn_handle, &cmd);
     if (rc != 0) {
         res->app_status = rc;
-        res->do_cb = 1;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
         return;
     }
 
-    if (!(proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR)) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_LTK_START;
+    if (!(proc->flags & BLE_SM_PROC_F_INITIATOR)) {
+        proc->state = BLE_SM_PROC_STATE_LTK_START;
     }
 }
 
 void
-ble_sm_lgcy_random_handle(struct ble_l2cap_sm_proc *proc,
-                          struct ble_l2cap_sm_result *res)
+ble_sm_lgcy_random_handle(struct ble_sm_proc *proc, struct ble_sm_result *res)
 {
-    struct hci_start_encrypt enc_cmd;
-    uint8_t preq[BLE_L2CAP_SM_HDR_SZ + BLE_L2CAP_SM_PAIR_CMD_SZ];
-    uint8_t pres[BLE_L2CAP_SM_HDR_SZ + BLE_L2CAP_SM_PAIR_CMD_SZ];
+    uint8_t preq[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ];
+    uint8_t pres[BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ];
     uint8_t confirm_val[16];
     uint8_t k[16];
     uint8_t ia[6];
@@ -245,51 +240,44 @@ ble_sm_lgcy_random_handle(struct ble_l2cap_sm_proc *proc,
                                           &iat, &rat, ia, ra);
     if (rc != 0) {
         res->app_status = rc;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
-        res->do_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
         return;
     }
 
-    rc = ble_l2cap_sm_alg_c1(k, ble_l2cap_sm_their_pair_rand(proc), preq, pres,
+    rc = ble_sm_alg_c1(k, ble_sm_their_pair_rand(proc), preq, pres,
                              iat, rat, ia, ra, confirm_val);
     if (rc != 0) {
         res->app_status = rc;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
-        res->do_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
         return;
     }
 
     if (memcmp(proc->confirm_their, confirm_val, 16) != 0) {
         /* Random number mismatch. */
-        res->app_status = BLE_HS_SM_US_ERR(BLE_L2CAP_SM_ERR_CONFIRM_MISMATCH);
-        res->sm_err = BLE_L2CAP_SM_ERR_CONFIRM_MISMATCH;
-        res->do_cb = 1;
+        res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH);
+        res->sm_err = BLE_SM_ERR_CONFIRM_MISMATCH;
+        res->enc_cb = 1;
         return;
     }
 
     /* Generate the key. */
-    rc = ble_l2cap_sm_gen_stk(proc);
+    rc = ble_sm_gen_stk(proc);
     if (rc != 0) {
         res->app_status = rc;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
-        res->do_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
         return;
     }
 
-    if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) {
+    if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
         /* Send the start-encrypt HCI command to the controller.   For
          * short-term key generation, we always set ediv and rand to 0.
          * (Vol. 3, part H, 2.4.4.1).
          */
-        proc->state = BLE_L2CAP_SM_PROC_STATE_ENC_START;
-        enc_cmd.connection_handle = proc->conn_handle;
-        enc_cmd.encrypted_diversifier = 0;
-        enc_cmd.random_number = 0;
-        memcpy(enc_cmd.long_term_key, proc->ltk,
-               sizeof enc_cmd.long_term_key);
-        ble_l2cap_sm_go(proc, res, &enc_cmd);
-    } else {
-        /* We are the repsonder; send the random response. */
-        ble_l2cap_sm_go(proc, res, NULL);
+        proc->state = BLE_SM_PROC_STATE_ENC_START;
     }
+
+    res->do_state = 1;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9b09325/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
new file mode 100644
index 0000000..25130df
--- /dev/null
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -0,0 +1,445 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef H_BLE_SM_
+#define H_BLE_SM_
+
+#include <inttypes.h>
+#include "os/queue.h"
+#include "nimble/nimble_opt.h"
+
+struct ble_gap_sec_state;
+struct hci_le_lt_key_req;
+struct hci_encrypt_change;
+
+#define BLE_SM_MTU            65
+
+#define BLE_SM_HDR_SZ         1
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x01/0x02 [req/rsp])         | 1                 |
+ * | IO Capability                      | 1                 |
+ * | OOB data flag                      | 1                 |
+ * | AuthReq                            | 1                 |
+ * | Maximum Encryption Key Size        | 1                 |
+ * | Initiator Key Distribution         | 1                 |
+ * | Responder Key Distribution         | 1                 |
+ */
+#define BLE_SM_PAIR_CMD_SZ        6
+struct ble_sm_pair_cmd {
+    uint8_t io_cap;
+    uint8_t oob_data_flag;
+    uint8_t authreq;
+    uint8_t max_enc_key_size;
+    uint8_t init_key_dist;
+    uint8_t resp_key_dist;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x03)                        | 1                 |
+ * | Confirm Value                      | 16                |
+ */
+#define BLE_SM_PAIR_CONFIRM_SZ    16
+struct ble_sm_pair_confirm {
+    uint8_t value[16];
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x04)                        | 1                 |
+ * | Random Value                       | 16                |
+ */
+#define BLE_SM_PAIR_RANDOM_SZ     16
+struct ble_sm_pair_random {
+    uint8_t value[16];
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x05)                        | 1                 |
+ * | Reason                             | 1                 |
+ */
+#define BLE_SM_PAIR_FAIL_SZ       1
+struct ble_sm_pair_fail {
+    uint8_t reason;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x06)                        | 1                 |
+ * | ltk                                | 16                |
+ */
+#define BLE_SM_ENC_INFO_SZ        16
+struct ble_sm_enc_info {
+    uint8_t ltk_le[16];
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x07)                        | 1                 |
+ * | EDIV                               | 2                 |
+ * | RAND                               | 8                 |
+ */
+#define BLE_SM_MASTER_IDEN_SZ     10
+struct ble_sm_master_iden {
+    uint16_t ediv;
+    uint64_t rand_val;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x08)                        | 1                 |
+ * | irk                                | 16                |
+ */
+#define BLE_SM_IDEN_INFO_SZ      16
+struct ble_sm_iden_info {
+    /* Stored in little-endian. */
+    uint8_t irk_le[16];
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x09)                        | 1                 |
+ * | addr_type                          | 1                 |
+ * | address                            | 6                 |
+ */
+#define BLE_SM_IDEN_ADDR_INFO_SZ  7
+struct ble_sm_iden_addr_info {
+    uint8_t addr_type;
+    uint8_t bd_addr_le[6];
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x0A)                        | 1                 |
+ * | csrk                               | 16                |
+ */
+#define BLE_SM_SIGNING_INFO_SZ    16
+struct ble_sm_signing_info {
+    uint8_t sig_key_le[16];
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x0B)                        | 1                 |
+ * | authreq                            | 1                 |
+ */
+#define BLE_SM_SEC_REQ_SZ         1
+struct ble_sm_sec_req {
+    uint8_t authreq;
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x0c)                        | 1                 |
+ * | 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];
+};
+
+/**
+ * | Parameter                          | Size (octets)     |
+ * +------------------------------------+-------------------+
+ * | (Code=0x0d)                        | 1                 |
+ * | DHKey Check                        | 16                |
+ */
+#define BLE_SM_DHKEY_CHECK_SZ     16
+struct ble_sm_dhkey_check {
+    uint8_t value[16];
+};
+
+#if NIMBLE_OPT(SM)
+
+#define BLE_SM_PROC_STATE_NONE              ((uint8_t)-1)
+    
+#define BLE_SM_PROC_STATE_PAIR              0
+#define BLE_SM_PROC_STATE_CONFIRM           1
+#define BLE_SM_PROC_STATE_RANDOM            2
+#define BLE_SM_PROC_STATE_LTK_START         3
+#define BLE_SM_PROC_STATE_LTK_RESTORE       4
+#define BLE_SM_PROC_STATE_ENC_START         5
+#define BLE_SM_PROC_STATE_ENC_RESTORE       6
+#define BLE_SM_PROC_STATE_KEY_EXCH          7
+#define BLE_SM_PROC_STATE_SEC_REQ           8
+#define BLE_SM_PROC_STATE_PUBLIC_KEY        9
+#define BLE_SM_PROC_STATE_DHKEY_CHECK       10
+#define BLE_SM_PROC_STATE_CNT               11
+
+#define BLE_SM_PROC_F_INITIATOR             0x01
+#define BLE_SM_PROC_F_TK_VALID              0x02
+#define BLE_SM_PROC_F_RX_CONFIRM            0x04
+#define BLE_SM_PROC_F_AUTHENTICATED         0x08
+#define BLE_SM_PROC_F_KEY_EXCHANGE          0x10
+#define BLE_SM_PROC_F_BONDED                0x20
+#define BLE_SM_PROC_F_SC                    0x40
+
+#define BLE_SM_KE_F_ENC_INFO                0x01
+#define BLE_SM_KE_F_MASTER_IDEN             0x02
+#define BLE_SM_KE_F_IDEN_INFO               0x04
+#define BLE_SM_KE_F_ADDR_INFO               0x08
+#define BLE_SM_KE_F_SIGN_INFO               0x10
+
+typedef uint8_t ble_sm_proc_flags;
+
+struct ble_sm_keys {
+    unsigned ltk_valid:1;
+    unsigned ediv_rand_valid:1;
+    unsigned irk_valid:1;
+    unsigned csrk_valid:1;
+    unsigned addr_valid:1;
+    uint16_t ediv;
+    uint64_t rand_val;
+    uint8_t addr_type;
+    uint8_t ltk[16];
+    uint8_t irk[16];
+    uint8_t csrk[16];
+    uint8_t addr[6];
+};
+
+struct ble_sm_proc {
+    STAILQ_ENTRY(ble_sm_proc) next;
+
+    uint32_t exp_os_ticks;
+    ble_sm_proc_flags flags;
+    uint16_t conn_handle;
+    uint8_t pair_alg;
+    uint8_t state;
+    uint8_t rx_key_flags;
+    /* XXX: Minimum security requirements. */
+
+    struct ble_sm_pair_cmd pair_req;
+    struct ble_sm_pair_cmd pair_rsp;
+    struct ble_sm_public_key pub_key_our;
+    struct ble_sm_public_key pub_key_their;
+    uint8_t priv_key_our[32];
+    uint8_t tk[16];
+    uint8_t confirm_their[16];
+    uint8_t randm[16];
+    uint8_t rands[16];
+    uint8_t ltk[16];
+    uint8_t mackey[16];
+
+    uint16_t ediv;
+    uint64_t rand_num;
+
+    struct ble_sm_keys our_keys;
+    struct ble_sm_keys peer_keys;
+};
+
+struct ble_sm_result {
+    int app_status;
+    uint8_t sm_err;
+    uint8_t passkey_action;
+    void *state_arg;
+    unsigned do_state:1;
+    unsigned enc_cb:1;
+    unsigned persist_keys:1;
+};
+
+#ifdef BLE_HS_DEBUG
+void ble_sm_dbg_set_next_pair_rand(uint8_t *next_pair_rand);
+void ble_sm_dbg_set_next_ediv(uint16_t next_ediv);
+void ble_sm_dbg_set_next_start_rand(uint64_t next_start_rand);
+void ble_sm_dbg_set_next_ltk(uint8_t *next_ltk);
+void ble_sm_dbg_set_next_irk(uint8_t *next_irk);
+void ble_sm_dbg_set_next_csrk(uint8_t *next_csrk);
+int ble_sm_dbg_num_procs(void);
+#endif
+
+uint8_t ble_sm_build_authreq(void);
+
+struct ble_l2cap_chan *ble_sm_create_chan(void);
+
+void ble_sm_pair_cmd_parse(void *payload, int len,
+                           struct ble_sm_pair_cmd *cmd);
+int ble_sm_pair_cmd_is_valid(struct ble_sm_pair_cmd *cmd);
+void ble_sm_pair_cmd_write(void *payload, int len, int is_req,
+                           struct ble_sm_pair_cmd *cmd);
+int ble_sm_pair_cmd_tx(uint16_t conn_handle, int is_req,
+                       struct ble_sm_pair_cmd *cmd);
+void ble_sm_pair_confirm_parse(void *payload, int len,
+                               struct ble_sm_pair_confirm *cmd);
+void ble_sm_pair_confirm_write(void *payload, int len,
+                               struct ble_sm_pair_confirm *cmd);
+int ble_sm_pair_confirm_tx(uint16_t conn_handle,
+                           struct ble_sm_pair_confirm *cmd);
+void ble_sm_pair_random_parse(void *payload, int len,
+                              struct ble_sm_pair_random *cmd);
+void ble_sm_pair_random_write(void *payload, int len,
+                              struct ble_sm_pair_random *cmd);
+int ble_sm_pair_random_tx(uint16_t conn_handle,
+                          struct ble_sm_pair_random *cmd);
+void ble_sm_pair_fail_parse(void *payload, int len,
+                            struct ble_sm_pair_fail *cmd);
+void ble_sm_pair_fail_write(void *payload, int len,
+                            struct ble_sm_pair_fail *cmd);
+int ble_sm_pair_fail_tx(uint16_t conn_handle, uint8_t reason);
+
+int ble_sm_alg_s1(uint8_t *k, uint8_t *r1, uint8_t *r2, uint8_t *out);
+int ble_sm_alg_c1(uint8_t *k, uint8_t *r,
+                  uint8_t *preq, uint8_t *pres,
+                  uint8_t iat, uint8_t rat,
+                  uint8_t *ia, uint8_t *ra,
+                  uint8_t *out_enc_data);
+int ble_sm_alg_f4(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t z,
+                  uint8_t *out_enc_data);
+int ble_sm_alg_g2(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t *y,
+                  uint32_t *passkey);
+int ble_sm_alg_f5(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t a1t,
+                  uint8_t *a1, uint8_t a2t, uint8_t *a2,
+                  uint8_t *mackey, uint8_t *ltk);
+int ble_sm_alg_f6(uint8_t *w, uint8_t *n1, uint8_t *n2, uint8_t *r,
+                  uint8_t *iocap, uint8_t a1t, uint8_t *a1,
+                  uint8_t a2t, uint8_t *a2, uint8_t *check);
+int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv);
+
+void ble_sm_enc_info_parse(void *payload, int len,
+                           struct ble_sm_enc_info *cmd);
+int ble_sm_enc_info_tx(uint16_t conn_handle,
+                       struct ble_sm_enc_info *cmd);
+void ble_sm_master_iden_parse(void *payload, int len,
+                              struct ble_sm_master_iden *cmd);
+int ble_sm_master_iden_tx(uint16_t conn_handle,
+                          struct ble_sm_master_iden *cmd);
+void ble_sm_iden_info_parse(void *payload, int len,
+                            struct ble_sm_iden_info *cmd);
+int ble_sm_iden_info_tx(uint16_t conn_handle,
+                        struct ble_sm_iden_info *cmd);
+void ble_sm_iden_addr_parse(void *payload, int len,
+                            struct ble_sm_iden_addr_info *cmd);
+int ble_sm_iden_addr_tx(uint16_t conn_handle,
+                        struct ble_sm_iden_addr_info *cmd);
+void ble_sm_signing_info_parse(void *payload, int len,
+                               struct ble_sm_signing_info *cmd);
+int ble_sm_signing_info_tx(uint16_t conn_handle,
+                           struct ble_sm_signing_info *cmd);
+void ble_sm_sec_req_parse(void *payload, int len,
+                          struct ble_sm_sec_req *cmd);
+void ble_sm_sec_req_write(void *payload, int len,
+                          struct ble_sm_sec_req *cmd);
+int ble_sm_sec_req_tx(uint16_t conn_handle,
+                      struct ble_sm_sec_req *cmd);
+int ble_sm_public_key_parse(void *payload, int len,
+                            struct ble_sm_public_key *cmd);
+int ble_sm_public_key_write(void *payload, int len,
+                            struct ble_sm_public_key *cmd);
+int ble_sm_public_key_tx(uint16_t conn_handle,
+                         struct ble_sm_public_key *cmd);
+int ble_sm_dhkey_check_parse(void *payload, int len,
+                             struct ble_sm_dhkey_check *cmd);
+int ble_sm_dhkey_check_write(void *payload, int len,
+                             struct ble_sm_dhkey_check *cmd);
+int ble_sm_dhkey_check_tx(uint16_t conn_handle,
+                          struct ble_sm_dhkey_check *cmd);
+
+void ble_sm_rx_encryption_change(struct hci_encrypt_change *evt);
+int ble_sm_rx_lt_key_req(struct hci_le_lt_key_req *evt);
+
+int ble_sm_lgcy_next_state(struct ble_sm_proc *proc);
+int ble_sm_lgcy_passkey_action(struct ble_sm_proc *proc);
+void ble_sm_lgcy_confirm_go(struct ble_sm_proc *proc,
+                            struct ble_sm_result *res);
+void ble_sm_lgcy_random_go(struct ble_sm_proc *proc,
+                           struct ble_sm_result *res);
+void ble_sm_lgcy_random_handle(struct ble_sm_proc *proc,
+                               struct ble_sm_result *res);
+
+int ble_sm_sc_next_state(struct ble_sm_proc *proc);
+int ble_sm_sc_passkey_action(struct ble_sm_proc *proc);
+void ble_sm_sc_confirm_go(struct ble_sm_proc *proc,
+                          struct ble_sm_result *res);
+void ble_sm_sc_random_go(struct ble_sm_proc *proc,
+                         struct ble_sm_result *res);
+void ble_sm_sc_random_handle(struct ble_sm_proc *proc,
+                             struct ble_sm_result *res);
+void ble_sm_sc_public_key_go(struct ble_sm_proc *proc,
+                             struct ble_sm_result *res,
+                             void *arg);
+void ble_sm_sc_public_key_handle(struct ble_sm_proc *proc,
+                                 struct ble_sm_public_key *cmd,
+                                 struct ble_sm_result *res);
+void ble_sm_sc_rx_public_key(uint16_t conn_handle, uint8_t op,
+                             struct os_mbuf **om, struct ble_sm_result *res);
+void ble_sm_sc_dhkey_check_go(struct ble_sm_proc *proc,
+                                 struct ble_sm_result *res, void *arg);
+void ble_sm_sc_rx_dhkey_check(uint16_t conn_handle, uint8_t op,
+                              struct os_mbuf **om, struct ble_sm_result *res);
+
+struct ble_sm_proc *ble_sm_proc_find(uint16_t conn_handle, uint8_t state,
+                                     int is_initiator,
+                                     struct ble_sm_proc **out_prev);
+int ble_sm_gen_pub_priv(struct ble_sm_proc *proc,
+                        uint8_t *pub, uint8_t *priv);
+uint8_t *ble_sm_our_pair_rand(struct ble_sm_proc *proc);
+uint8_t *ble_sm_their_pair_rand(struct ble_sm_proc *proc);
+void ble_sm_go(struct ble_sm_proc *proc, struct ble_sm_result *res, void *arg);
+void ble_sm_process_result(uint16_t conn_handle, struct ble_sm_result *res);
+
+
+void ble_sm_heartbeat(void);
+int ble_sm_pair_initiate(uint16_t conn_handle);
+int ble_sm_slave_initiate(uint16_t conn_handle);
+int ble_sm_enc_initiate(uint16_t conn_handle, uint8_t *ltk,
+                        uint16_t ediv, uint64_t rand_val, int auth);
+int ble_sm_init(void);
+
+#else
+
+#ifdef BLE_HS_DEBUG
+#define ble_sm_dbg_set_next_rand(next_rand)
+#define ble_sm_dbg_num_procs() 0
+#endif
+
+#define ble_sm_create_chan() NULL
+
+#define ble_sm_pair_cmd_tx(conn_handle, is_req, cmd) BLE_HS_ENOTSUP
+#define ble_sm_pair_confirm_tx(conn_handle, cmd) BLE_HS_ENOTSUP
+#define ble_sm_pair_random_tx(conn_handle, cmd) BLE_HS_ENOTSUP
+#define ble_sm_pair_fail_tx(conn_handle, cmd) BLE_HS_ENOTSUP
+
+#define ble_sm_alg_s1(k, r1, r2, out) BLE_HS_ENOTSUP
+#define ble_sm_alg_c1(k, r, preq, pres, iat, rat, ia, ra, out_enc_data) \
+    BLE_HS_ENOTSUP
+
+#define ble_sm_rx_encryption_change(evt) ((void)(evt))
+#define ble_sm_rx_lt_key_req(evt) ((void)(evt))
+
+#define ble_sm_heartbeat()
+#define ble_sm_init() 0
+
+#endif
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9b09325/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 538ee4d..ca12025 100644
--- a/net/nimble/host/src/ble_sm_sc.c
+++ b/net/nimble/host/src/ble_sm_sc.c
@@ -20,26 +20,26 @@
 #include <string.h>
 
 #include "ble_hs_priv.h"
-#include "ble_l2cap_sm_priv.h"
+#include "ble_sm_priv.h"
 
 static int
-ble_sm_sc_initiator_txes_confirm(struct ble_l2cap_sm_proc *proc)
+ble_sm_sc_initiator_txes_confirm(struct ble_sm_proc *proc)
 {
-    BLE_HS_DBG_ASSERT(proc->flags & BLE_L2CAP_SM_PROC_F_SC);
+    BLE_HS_DBG_ASSERT(proc->flags & BLE_SM_PROC_F_SC);
 
     /* Initiator does not send a confirm when pairing algorithm is any of:
      *     o just works
      *     o numeric comparison
      * (vol. 3, part H, 2.3.5.6.2)
      */
-    return proc->pair_alg != BLE_L2CAP_SM_PAIR_ALG_JW &&
-           proc->pair_alg != BLE_L2CAP_SM_PAIR_ALG_NUM_CMP;
+    return proc->pair_alg != BLE_SM_PAIR_ALG_JW &&
+           proc->pair_alg != BLE_SM_PAIR_ALG_NUM_CMP;
 }
 
 static int
-ble_sm_sc_responder_verifies_random(struct ble_l2cap_sm_proc *proc)
+ble_sm_sc_responder_verifies_random(struct ble_sm_proc *proc)
 {
-    BLE_HS_DBG_ASSERT(proc->flags & BLE_L2CAP_SM_PROC_F_SC);
+    BLE_HS_DBG_ASSERT(proc->flags & BLE_SM_PROC_F_SC);
 
     /* Responder does not verify the initiator's random number when pairing
      * algorithm is any of:
@@ -47,215 +47,197 @@ ble_sm_sc_responder_verifies_random(struct ble_l2cap_sm_proc *proc)
      *     o numeric comparison
      * (vol. 3, part H, 2.3.5.6.2)
      */
-    return proc->pair_alg != BLE_L2CAP_SM_PAIR_ALG_JW &&
-           proc->pair_alg != BLE_L2CAP_SM_PAIR_ALG_NUM_CMP;
+    return proc->pair_alg != BLE_SM_PAIR_ALG_JW &&
+           proc->pair_alg != BLE_SM_PAIR_ALG_NUM_CMP;
 }
 
 int
-ble_sm_sc_passkey_action(struct ble_l2cap_sm_proc *proc)
+ble_sm_sc_passkey_action(struct ble_sm_proc *proc)
 {
     return 0;
 }
 
 void
-ble_sm_sc_confirm_go(struct ble_l2cap_sm_proc *proc,
-                     struct ble_l2cap_sm_result *res)
+ble_sm_sc_confirm_go(struct ble_sm_proc *proc, struct ble_sm_result *res)
 {
-    struct ble_l2cap_sm_pair_confirm cmd;
+    struct ble_sm_pair_confirm cmd;
     int rc;
 
-    rc = ble_l2cap_sm_alg_f4(proc->pub_key_our.x, proc->pub_key_their.x,
-                             ble_l2cap_sm_our_pair_rand(proc), 0,
+    rc = ble_sm_alg_f4(proc->pub_key_our.x, proc->pub_key_their.x,
+                             ble_sm_our_pair_rand(proc), 0,
                              cmd.value);
     if (rc != 0) {
         res->app_status = rc;
-        res->do_cb = 1;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
         return;
     }
 
-    rc = ble_l2cap_sm_pair_confirm_tx(proc->conn_handle, &cmd);
+    rc = ble_sm_pair_confirm_tx(proc->conn_handle, &cmd);
     if (rc != 0) {
         res->app_status = rc;
-        res->do_cb = 1;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
         return;
     }
 
-    if (!(proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR)) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_RANDOM;
-    }
-}
-
-static void
-ble_sm_sc_random_verify(struct ble_l2cap_sm_proc *proc,
-                        struct ble_l2cap_sm_result *res)
-{
-    uint8_t confirm_val[16];
-    int rc;
-
-    rc = ble_l2cap_sm_alg_f4(proc->pub_key_our.x,
-                             proc->pub_key_their.x,
-                             ble_l2cap_sm_their_pair_rand(proc), 0,
-                             confirm_val);
-    if (rc != 0) {
-        res->app_status = rc;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
-        return;
-    }
-
-    if (memcmp(proc->confirm_their, confirm_val, 16) != 0) {
-        /* Random number mismatch. */
-        res->app_status = BLE_HS_SM_US_ERR(BLE_L2CAP_SM_ERR_CONFIRM_MISMATCH);
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
-        return;
+    if (!(proc->flags & BLE_SM_PROC_F_INITIATOR)) {
+        proc->state = BLE_SM_PROC_STATE_RANDOM;
     }
 }
 
 void
-ble_sm_sc_random_go(struct ble_l2cap_sm_proc *proc,
-                    struct ble_l2cap_sm_result *res)
+ble_sm_sc_random_go(struct ble_sm_proc *proc,
+                    struct ble_sm_result *res)
 {
-    struct ble_l2cap_sm_pair_random cmd;
+    struct ble_sm_pair_random cmd;
     int rc;
 
-    memcpy(cmd.value, ble_l2cap_sm_our_pair_rand(proc), 16);
+    memcpy(cmd.value, ble_sm_our_pair_rand(proc), 16);
 
-    rc = ble_l2cap_sm_pair_random_tx(proc->conn_handle, &cmd);
+    rc = ble_sm_pair_random_tx(proc->conn_handle, &cmd);
     if (rc != 0) {
         res->app_status = rc;
-        res->do_cb = 1;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
         return;
     }
 
-    if (!(proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR)) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_DHKEY_CHECK;
+    if (!(proc->flags & BLE_SM_PROC_F_INITIATOR)) {
+        proc->state = BLE_SM_PROC_STATE_DHKEY_CHECK;
     }
 }
 
 void
-ble_sm_sc_random_handle(struct ble_l2cap_sm_proc *proc,
-                        struct ble_l2cap_sm_result *res)
+ble_sm_sc_random_handle(struct ble_sm_proc *proc,
+                        struct ble_sm_result *res)
 {
+    uint8_t confirm_val[16];
     int rc;
 
-    if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR ||
+    if (proc->flags & BLE_SM_PROC_F_INITIATOR ||
         ble_sm_sc_responder_verifies_random(proc)) {
 
-        ble_sm_sc_random_verify(proc, res);
-        if (res->app_status != 0) {
+        rc = ble_sm_alg_f4(proc->pub_key_our.x,
+                                 proc->pub_key_their.x,
+                                 ble_sm_their_pair_rand(proc), 0,
+                                 confirm_val);
+        if (rc != 0) {
+            res->app_status = rc;
+            res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+            res->enc_cb = 1;
+            return;
+        }
+
+        if (memcmp(proc->confirm_their, confirm_val, 16) != 0) {
+            /* Random number mismatch. */
+            res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH);
+            res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+            res->enc_cb = 1;
             return;
         }
     }
 
     /* Calculate the mac key and ltk. */
-    rc = ble_l2cap_sm_alg_f5(NULL, NULL, NULL, 0, NULL, 0, NULL,
+    rc = ble_sm_alg_f5(NULL, NULL, NULL, 0, NULL, 0, NULL,
                              proc->mackey, proc->ltk);
     if (rc != 0) {
         res->app_status = rc;
-        res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
-        res->do_cb = 1;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+        res->enc_cb = 1;
         return;
     }
 
-    if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_DHKEY_CHECK;
+    if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
+        proc->state = BLE_SM_PROC_STATE_DHKEY_CHECK;
     }
 
-    ble_l2cap_sm_go(proc, res, NULL);
+    res->do_state = 1;
 }
 
 void
-ble_sm_sc_public_key_go(struct ble_l2cap_sm_proc *proc,
-                        struct ble_l2cap_sm_result *res,
+ble_sm_sc_public_key_go(struct ble_sm_proc *proc,
+                        struct ble_sm_result *res,
                         void *arg)
 {
     int initiator_txes;
     int is_initiator;
     int rc;
 
-    rc = ble_l2cap_sm_gen_pub_priv(proc, proc->pub_key_our.x,
+    rc = ble_sm_gen_pub_priv(proc, proc->pub_key_our.x,
                                    proc->priv_key_our);
     if (rc != 0) {
         goto err;
     }
 
-    rc = ble_l2cap_sm_public_key_tx(proc->conn_handle, &proc->pub_key_our);
+    rc = ble_sm_public_key_tx(proc->conn_handle, &proc->pub_key_our);
     if (rc != 0) {
         goto err;
     }
 
     initiator_txes = ble_sm_sc_initiator_txes_confirm(proc);
-    is_initiator = proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR;
+    is_initiator = proc->flags & BLE_SM_PROC_F_INITIATOR;
     if ((initiator_txes  && is_initiator) ||
         (!initiator_txes && !is_initiator)) {
 
-        proc->state = BLE_L2CAP_SM_PROC_STATE_CONFIRM;
-        res->do_tx = 1;
+        proc->state = BLE_SM_PROC_STATE_CONFIRM;
+        res->do_state = 1;
     }
 
     return;
 
 err:
     res->app_status = rc;
-    res->do_cb = 1;
-    res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
+    res->enc_cb = 1;
+    res->sm_err = BLE_SM_ERR_UNSPECIFIED;
 }
 
 void
-ble_sm_sc_public_key_handle(struct ble_l2cap_sm_proc *proc,
-                            struct ble_l2cap_sm_public_key *cmd,
-                            struct ble_l2cap_sm_result *res)
+ble_sm_sc_public_key_handle(struct ble_sm_proc *proc,
+                            struct ble_sm_public_key *cmd,
+                            struct ble_sm_result *res)
 {
     proc->pub_key_their = *cmd;
 
-    if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_CONFIRM;
+    if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
+        proc->state = BLE_SM_PROC_STATE_CONFIRM;
 
         if (ble_sm_sc_initiator_txes_confirm(proc)) {
-            ble_l2cap_sm_go(proc, res, NULL);
+            res->do_state = 1;
         }
     } else {
-        ble_l2cap_sm_go(proc, res, NULL);
+        res->do_state = 1;
     }
 }
 
-int
-ble_sm_sc_rx_public_key(uint16_t conn_handle, uint8_t op, struct os_mbuf **om)
+void
+ble_sm_sc_rx_public_key(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
+                        struct ble_sm_result *res)
 {
-    struct ble_l2cap_sm_public_key cmd;
-    struct ble_l2cap_sm_result res;
-    struct ble_l2cap_sm_proc *proc;
-    struct ble_l2cap_sm_proc *prev;
-    int rc;
+    struct ble_sm_public_key cmd;
+    struct ble_sm_proc *proc;
+    struct ble_sm_proc *prev;
 
-    rc = ble_hs_misc_pullup_base(om, BLE_L2CAP_SM_PUBLIC_KEY_SZ);
-    if (rc != 0) {
-        return rc;
+    res->app_status = ble_hs_misc_pullup_base(om, BLE_SM_PUBLIC_KEY_SZ);
+    if (res->app_status != 0) {
+        res->enc_cb = 1;
+        return;
     }
 
-    ble_l2cap_sm_public_key_parse((*om)->om_data, (*om)->om_len, &cmd);
+    ble_sm_public_key_parse((*om)->om_data, (*om)->om_len, &cmd);
 
     BLE_HS_LOG(DEBUG, "rxed sm public key cmd\n");
 
     ble_hs_lock();
-    proc = ble_l2cap_sm_proc_find(conn_handle,
-                                  BLE_L2CAP_SM_PROC_STATE_PUBLIC_KEY,
-                                  -1, &prev);
-    if (proc != NULL) {
-        memset(&res, 0, sizeof res);
-        ble_sm_sc_public_key_handle(proc, &cmd, &res);
-    }
-    ble_hs_unlock();
-
+    proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_PUBLIC_KEY, -1,
+                            &prev);
     if (proc == NULL) {
-        return BLE_HS_ENOENT;
+        res->app_status = BLE_HS_ENOENT;
+        res->sm_err = BLE_SM_ERR_UNSPECIFIED;
+    } else {
+        ble_sm_sc_public_key_handle(proc, &cmd, res);
     }
-
-    ble_l2cap_sm_process_result(conn_handle, &res);
-
-    return rc;
+    ble_hs_unlock();
 }
 
 /*****************************************************************************
@@ -263,97 +245,91 @@ ble_sm_sc_rx_public_key(uint16_t conn_handle, uint8_t op, struct os_mbuf **om)
  *****************************************************************************/
 
 void
-ble_sm_sc_dhkey_check_go(struct ble_l2cap_sm_proc *proc,
-                            struct ble_l2cap_sm_result *res, void *arg)
+ble_sm_sc_dhkey_check_go(struct ble_sm_proc *proc, struct ble_sm_result *res,
+                         void *arg)
 {
-    struct ble_l2cap_sm_dhkey_check cmd;
+    struct ble_sm_dhkey_check cmd;
     int rc;
 
-    rc = ble_l2cap_sm_alg_f6(NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, NULL,
+    rc = ble_sm_alg_f6(NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, NULL,
                              cmd.value);
     if (rc != 0) {
         goto err;
     }
 
-    rc = ble_l2cap_sm_dhkey_check_tx(proc->conn_handle, &cmd);
+    rc = ble_sm_dhkey_check_tx(proc->conn_handle, &cmd);
     if (rc != 0) {
         goto err;
     }
 
-    if (!(proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR)) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_LTK_START;
+    if (!(proc->flags & BLE_SM_PROC_F_INITIATOR)) {
+        proc->state = BLE_SM_PROC_STATE_LTK_START;
     }
 
     return;
 
 err:
     res->app_status = rc;
-    res->do_cb = 1;
-    res->sm_err = BLE_L2CAP_SM_ERR_UNSPECIFIED;
+    res->enc_cb = 1;
+    res->sm_err = BLE_SM_ERR_UNSPECIFIED;
 }
 
 static void
-ble_l2cap_sm_dhkey_check_handle(struct ble_l2cap_sm_proc *proc,
-                                struct ble_l2cap_sm_dhkey_check *cmd,
-                                struct ble_l2cap_sm_result *res)
+ble_sm_dhkey_check_handle(struct ble_sm_proc *proc,
+                          struct ble_sm_dhkey_check *cmd,
+                          struct ble_sm_result *res)
 {
     uint8_t exp_value[16];
 
-    res->app_status = ble_l2cap_sm_alg_f6(NULL, NULL, NULL, NULL, NULL, 0,
-                                          NULL, 0, NULL, exp_value);
+    res->app_status = ble_sm_alg_f6(NULL, NULL, NULL, NULL, NULL, 0,
+                                    NULL, 0, NULL, exp_value);
     if (res->app_status != 0) {
         return;
     }
     if (memcmp(cmd->value, exp_value, 16) != 0) {
         /* Random number mismatch. */
-        res->sm_err = BLE_L2CAP_SM_ERR_DHKEY;
-        res->app_status = BLE_HS_SM_US_ERR(BLE_L2CAP_SM_ERR_DHKEY);
+        res->sm_err = BLE_SM_ERR_DHKEY;
+        res->app_status = BLE_HS_SM_US_ERR(BLE_SM_ERR_DHKEY);
         return;
     }
 
-    if (proc->flags & BLE_L2CAP_SM_PROC_F_INITIATOR) {
-        proc->state = BLE_L2CAP_SM_PROC_STATE_ENC_START;
+    if (proc->flags & BLE_SM_PROC_F_INITIATOR) {
+        proc->state = BLE_SM_PROC_STATE_ENC_START;
     }
 
-    ble_l2cap_sm_go(proc, res, NULL);
+    res->do_state = 1;
 }
 
-int
-ble_sm_sc_rx_dhkey_check(uint16_t conn_handle, uint8_t op, struct os_mbuf **om)
+void
+ble_sm_sc_rx_dhkey_check(uint16_t conn_handle, uint8_t op, struct os_mbuf **om,
+                         struct ble_sm_result *res)
 {
-    struct ble_l2cap_sm_dhkey_check cmd;
-    struct ble_l2cap_sm_result res;
-    struct ble_l2cap_sm_proc *proc;
-    struct ble_l2cap_sm_proc *prev;
-    int rc;
+    struct ble_sm_dhkey_check cmd;
+    struct ble_sm_proc *proc;
+    struct ble_sm_proc *prev;
 
-    rc = ble_hs_misc_pullup_base(om, BLE_L2CAP_SM_DHKEY_CHECK_SZ);
-    if (rc != 0) {
-        return rc;
+    res->app_status = ble_hs_misc_pullup_base(om, BLE_SM_DHKEY_CHECK_SZ);
+    if (res->app_status != 0) {
+        res->enc_cb = 1;
+        return;
     }
 
-    rc = ble_l2cap_sm_dhkey_check_parse((*om)->om_data, (*om)->om_len, &cmd);
-    if (rc != 0) {
-        return rc;
+    res->app_status = ble_sm_dhkey_check_parse((*om)->om_data, (*om)->om_len,
+                                               &cmd);
+    if (res->app_status != 0) {
+        res->enc_cb = 1;
+        return;
     }
 
     BLE_HS_LOG(DEBUG, "rxed sm dhkey check cmd\n");
 
     ble_hs_lock();
-    proc = ble_l2cap_sm_proc_find(conn_handle,
-                                  BLE_L2CAP_SM_PROC_STATE_DHKEY_CHECK,
-                                  -1, &prev);
-    if (proc != NULL) {
-        memset(&res, 0, sizeof res);
-        ble_l2cap_sm_dhkey_check_handle(proc, &cmd, &res);
-    }
-    ble_hs_unlock();
-
+    proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_DHKEY_CHECK, -1,
+                            &prev);
     if (proc == NULL) {
-        return BLE_HS_ENOENT;
+        res->app_status = BLE_HS_ENOENT;
+    } else {
+        ble_sm_dhkey_check_handle(proc, &cmd, res);
     }
-
-    ble_l2cap_sm_process_result(conn_handle, &res);
-
-    return res.app_status;
+    ble_hs_unlock();
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9b09325/net/nimble/host/src/host_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci.c b/net/nimble/host/src/host_hci.c
index 2bbdd8f..b16ddba 100644
--- a/net/nimble/host/src/host_hci.c
+++ b/net/nimble/host/src/host_hci.c
@@ -174,7 +174,7 @@ host_hci_rx_encrypt_change(uint8_t event_code, uint8_t *data, int len)
     evt.connection_handle = le16toh(data + 3);
     evt.encryption_enabled = data[5];
 
-    ble_l2cap_sm_rx_encryption_change(&evt);
+    ble_sm_rx_encryption_change(&evt);
 
     return 0;
 }
@@ -437,7 +437,7 @@ host_hci_rx_le_lt_key_req(uint8_t subevent, uint8_t *data, int len)
     evt.random_number = le64toh(data + 3);
     evt.encrypted_diversifier = le16toh(data + 11);
 
-    ble_l2cap_sm_rx_lt_key_req(&evt);
+    ble_sm_rx_lt_key_req(&evt);
 
     return 0;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d9b09325/net/nimble/host/src/test/ble_hs_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test.c b/net/nimble/host/src/test/ble_hs_test.c
index e51e2d4..23f8326 100644
--- a/net/nimble/host/src/test/ble_hs_test.c
+++ b/net/nimble/host/src/test/ble_hs_test.c
@@ -66,7 +66,7 @@ main(int argc, char **argv)
     ble_host_hci_test_all();
     ble_hs_adv_test_all();
     ble_hs_conn_test_all();
-    ble_l2cap_sm_test_all();
+    ble_sm_test_all();
     ble_l2cap_test_all();
     ble_os_test_all();
     ble_uuid_test_all();