You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by we...@apache.org on 2016/03/25 04:39:45 UTC

incubator-mynewt-core git commit: Add LE encrypt command

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 0544eb4f5 -> 312859344


Add LE encrypt command


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

Branch: refs/heads/develop
Commit: 3128593448c9c70ae5c4d3fadb04b48ff22352ea
Parents: 0544eb4
Author: wes3 <wi...@micosa.io>
Authored: Thu Mar 24 20:39:24 2016 -0700
Committer: wes3 <wi...@micosa.io>
Committed: Thu Mar 24 20:39:35 2016 -0700

----------------------------------------------------------------------
 apps/bletest/src/main.c                         | 32 ++++++++++++++-
 .../controller/include/controller/ble_hw.h      |  4 ++
 .../controller/include/controller/ble_ll.h      |  1 +
 net/nimble/controller/src/ble_ll_hci.c          | 43 +++++++++++++++++++-
 net/nimble/controller/src/ble_ll_supp_cmd.c     |  2 +-
 net/nimble/drivers/native/src/ble_hw.c          |  9 ++++
 net/nimble/drivers/nrf51/src/ble_hw.c           | 33 +++++++++++++++
 net/nimble/drivers/nrf52/src/ble_hw.c           | 33 +++++++++++++++
 net/nimble/host/include/host/host_hci.h         |  1 +
 net/nimble/host/src/host_dbg.c                  | 10 +++++
 net/nimble/host/src/host_hci_cmd.c              | 22 ++++++++++
 net/nimble/include/nimble/ble.h                 | 11 +++++
 net/nimble/include/nimble/hci_common.h          |  3 ++
 net/nimble/src/hci_common.c                     |  2 +-
 net/nimble/src/util.c                           | 12 ++++++
 15 files changed, 214 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index 67ba80a..0961bb0 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -77,7 +77,7 @@ uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
 uint8_t g_host_adv_len;
 
 /* Create a mbuf pool of BLE mbufs */
-#define MBUF_NUM_MBUFS      (42)
+#define MBUF_NUM_MBUFS      (32)
 #define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
 #define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
 #define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
@@ -141,6 +141,30 @@ uint16_t g_bletest_handle;
 uint16_t g_bletest_completed_pkts;
 uint16_t g_bletest_outstanding_pkts;
 
+/* --- For LE encryption testing --- */
+/* Key: 0x4C68384139F574D836BCF34E9DFB01BF */
+const uint8_t g_ble_ll_encrypt_test_key[16] = 
+{
+    0x4c, 0x68, 0x38, 0x41, 0x39, 0xf5, 0x74, 0xd8,
+    0x36, 0xbc, 0xf3, 0x4e, 0x9d, 0xfb, 0x01, 0xbf
+};
+
+/* Plaint text: 0x0213243546576879acbdcedfe0f10213 */
+const uint8_t g_ble_ll_encrypt_test_plain_text[16] = 
+{
+    0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, 
+    0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1, 0x02, 0x13
+};
+
+/* Encrypted data: 0x99ad1b5226a37e3e058e3b8e27c2c666 */
+const uint8_t g_ble_ll_encrypt_test_encrypted_data[16] = 
+{
+    0x99, 0xad, 0x1b, 0x52, 0x26, 0xa3, 0x7e, 0x3e, 
+    0x05, 0x8e, 0x3b, 0x8e, 0x27, 0xc2, 0xc6, 0x66
+};
+
+uint8_t g_ble_ll_encrypted_data[16];
+
 #if (BLETEST_THROUGHPUT_TEST == 1)
 void
 bletest_completed_pkt(uint16_t handle)
@@ -817,6 +841,12 @@ bletest_task_handler(void *arg)
     assert(rc == 0);
     host_hci_outstanding_opcode = 0;
 
+    /* Encrypt a block */
+    rc = host_hci_cmd_le_encrypt((uint8_t *)g_ble_ll_encrypt_test_key, 
+                                 (uint8_t *)g_ble_ll_encrypt_test_plain_text);
+    assert(rc == 0);
+    host_hci_outstanding_opcode = 0;
+
     /* Wait some time before starting */
     os_time_delay(OS_TICKS_PER_SEC);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/controller/include/controller/ble_hw.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_hw.h b/net/nimble/controller/include/controller/ble_hw.h
index e357f17..a0f69e7 100644
--- a/net/nimble/controller/include/controller/ble_hw.h
+++ b/net/nimble/controller/include/controller/ble_hw.h
@@ -47,4 +47,8 @@ void ble_hw_whitelist_disable(void);
 /* Boolean function returning true if address matches a whitelist entry */
 int ble_hw_whitelist_match(void);
 
+/* Encrypt data */
+struct ble_encryption_block;
+int ble_hw_encrypt_block(struct ble_encryption_block *ecb);
+
 #endif /* H_BLE_HW_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
index 76574c0..bd4a325 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -22,6 +22,7 @@
 
 #include "stats/stats.h"
 #include "hal/hal_cputime.h"
+#include "ble_hw.h"
 
 /* Controller revision. */
 #define BLE_LL_SUB_VERS_NR      (0x0000)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/controller/src/ble_ll_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci.c b/net/nimble/controller/src/ble_ll_hci.c
index e726fd3..9f22326 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -101,6 +101,44 @@ ble_ll_hci_send_noop(void)
     return rc;
 }
 
+/**
+ * LE encrypt command
+ * 
+ * @param cmdbuf 
+ * @param rspbuf 
+ * @param rsplen 
+ * 
+ * @return int 
+ */
+static int
+ble_ll_hci_le_encrypt(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen)
+{
+    int rc;
+    struct ble_encryption_block ecb;
+
+    /* Call the link layer to encrypt the data */
+    swap_buf(ecb.key, cmdbuf, BLE_ENC_BLOCK_SIZE);
+    swap_buf(ecb.plain_text, cmdbuf + BLE_ENC_BLOCK_SIZE, BLE_ENC_BLOCK_SIZE);
+    rc = ble_hw_encrypt_block(&ecb);
+    if (!rc) {
+        swap_buf(rspbuf, ecb.cipher_text, BLE_ENC_BLOCK_SIZE);
+        *rsplen = BLE_ENC_BLOCK_SIZE;
+        rc = BLE_ERR_SUCCESS;
+    } else {
+        *rsplen = 0;
+        rc = BLE_ERR_CTLR_BUSY;
+    }
+    return rc;
+}
+
+/**
+ * Read local version
+ * 
+ * @param rspbuf 
+ * @param rsplen 
+ * 
+ * @return int 
+ */
 static int
 ble_ll_hci_rd_local_version(uint8_t *rspbuf, uint8_t *rsplen)
 {    
@@ -123,7 +161,7 @@ ble_ll_hci_rd_local_version(uint8_t *rspbuf, uint8_t *rsplen)
 }
 
 /**
- * Reade local supported features
+ * Read local supported features
  * 
  * @param rspbuf 
  * @param rsplen 
@@ -473,6 +511,9 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
     case BLE_HCI_OCF_LE_RD_REM_FEAT:
         rc = ble_ll_conn_hci_read_rem_features(cmdbuf);
         break;
+    case BLE_HCI_OCF_LE_ENCRYPT:
+        rc = ble_ll_hci_le_encrypt(cmdbuf, rspbuf, rsplen);
+        break;
     case BLE_HCI_OCF_LE_RD_SUPP_STATES :
         rc = ble_ll_hci_le_read_supp_states(rspbuf, rsplen);
         break;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/controller/src/ble_ll_supp_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_supp_cmd.c b/net/nimble/controller/src/ble_ll_supp_cmd.c
index 1ef5780..f3e9868 100644
--- a/net/nimble/controller/src/ble_ll_supp_cmd.c
+++ b/net/nimble/controller/src/ble_ll_supp_cmd.c
@@ -106,7 +106,7 @@
 #define BLE_SUPP_CMD_LE_SET_HOST_CHAN_CLASS (1 << 3)
 #define BLE_SUPP_CMD_LE_RD_CHAN_MAP         (1 << 4)
 #define BLE_SUPP_CMD_LE_RD_REM_USED_FEAT    (1 << 5)
-#define BLE_SUPP_CMD_LE_ENCRYPT             (0 << 6)
+#define BLE_SUPP_CMD_LE_ENCRYPT             (1 << 6)
 #define BLE_SUPP_CMD_LE_RAND                (0 << 7)
 
 #define BLE_LL_SUPP_CMD_OCTET_27            \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/drivers/native/src/ble_hw.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/native/src/ble_hw.c b/net/nimble/drivers/native/src/ble_hw.c
index 425d541..35b25ff 100644
--- a/net/nimble/drivers/native/src/ble_hw.c
+++ b/net/nimble/drivers/native/src/ble_hw.c
@@ -19,9 +19,11 @@
 
 #include <stdint.h>
 #include <assert.h>
+#include <string.h>
 #include "os/os.h"
 #include "nimble/ble.h"
 
+/* Total number of white list elements supported by nrf52 */
 #define BLE_HW_WHITE_LIST_SIZE      (0)
 
 /* We use this to keep track of which entries are set to valid addresses */
@@ -105,3 +107,10 @@ ble_hw_whitelist_match(void)
 {
     return 0;
 }
+
+/* Encrypt data */
+int
+ble_hw_encrypt_block(struct ble_encryption_block *ecb)
+{
+    return -1;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/drivers/nrf51/src/ble_hw.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/nrf51/src/ble_hw.c b/net/nimble/drivers/nrf51/src/ble_hw.c
index 2428d49..b624016 100644
--- a/net/nimble/drivers/nrf51/src/ble_hw.c
+++ b/net/nimble/drivers/nrf51/src/ble_hw.c
@@ -19,10 +19,12 @@
 
 #include <stdint.h>
 #include <assert.h>
+#include <string.h>
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "mcu/nrf51_bitfields.h"
 
+/* Total number of white list elements supported by nrf52 */
 #define BLE_HW_WHITE_LIST_SIZE      (8)
 
 /* We use this to keep track of which entries are set to valid addresses */
@@ -160,3 +162,34 @@ ble_hw_whitelist_match(void)
 {
     return (int)NRF_RADIO->EVENTS_DEVMATCH;
 }
+
+/* Encrypt data */
+int
+ble_hw_encrypt_block(struct ble_encryption_block *ecb)
+{
+    int rc;
+
+    /* Stop ECB */
+    NRF_ECB->TASKS_STOPECB = 1;
+    NRF_ECB->EVENTS_ENDECB = 0;
+    NRF_ECB->EVENTS_ERRORECB = 0;
+    NRF_ECB->ECBDATAPTR = (uint32_t)ecb;
+
+    /* Start ECB */
+    NRF_ECB->TASKS_STARTECB = 1;
+
+    /* Wait till error or done */
+    while (1) {
+        if (NRF_ECB->EVENTS_ENDECB != 0) {
+            rc = 0;
+            break;
+        }
+
+        if (NRF_ECB->EVENTS_ERRORECB != 0) {
+            rc = -1;
+            break;
+        }
+    }
+
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/drivers/nrf52/src/ble_hw.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/nrf52/src/ble_hw.c b/net/nimble/drivers/nrf52/src/ble_hw.c
index affd83a..b9a086d 100644
--- a/net/nimble/drivers/nrf52/src/ble_hw.c
+++ b/net/nimble/drivers/nrf52/src/ble_hw.c
@@ -19,10 +19,12 @@
 
 #include <stdint.h>
 #include <assert.h>
+#include <string.h>
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "mcu/nrf52_bitfields.h"
 
+/* Total number of white list elements supported by nrf52 */
 #define BLE_HW_WHITE_LIST_SIZE      (8)
 
 /* We use this to keep track of which entries are set to valid addresses */
@@ -160,3 +162,34 @@ ble_hw_whitelist_match(void)
 {
     return (int)NRF_RADIO->EVENTS_DEVMATCH;
 }
+
+/* Encrypt data */
+int
+ble_hw_encrypt_block(struct ble_encryption_block *ecb)
+{
+    int rc;
+
+    /* Stop ECB */
+    NRF_ECB->TASKS_STOPECB = 1;
+    NRF_ECB->EVENTS_ENDECB = 0;
+    NRF_ECB->EVENTS_ERRORECB = 0;
+    NRF_ECB->ECBDATAPTR = (uint32_t)ecb;
+
+    /* Start ECB */
+    NRF_ECB->TASKS_STARTECB = 1;
+
+    /* Wait till error or done */
+    while (1) {
+        if (NRF_ECB->EVENTS_ENDECB != 0) {
+            rc = 0;
+            break;
+        }
+
+        if (NRF_ECB->EVENTS_ERRORECB != 0) {
+            rc = -1;
+            break;
+        }
+    }
+
+    return rc;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/host/include/host/host_hci.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/host_hci.h b/net/nimble/host/include/host/host_hci.h
index d920d18..064a2dd 100644
--- a/net/nimble/host/include/host/host_hci.h
+++ b/net/nimble/host/include/host/host_hci.h
@@ -64,6 +64,7 @@ int host_hci_cmd_le_conn_param_reply(struct hci_conn_param_reply *hcr);
 int host_hci_cmd_le_conn_param_neg_reply(struct hci_conn_param_neg_reply *hcn);
 int host_hci_cmd_le_read_supp_states(void);
 int host_hci_cmd_le_read_max_datalen(void);
+int host_hci_cmd_le_encrypt(uint8_t *key, uint8_t *pt);
 int host_hci_set_buf_size(uint16_t pktlen, uint8_t max_pkts);
 
 uint16_t host_hci_handle_pb_bc_join(uint16_t handle, uint8_t pb, uint8_t bc);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/host/src/host_dbg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_dbg.c b/net/nimble/host/src/host_dbg.c
index e17ae4b..0fb8bf1 100644
--- a/net/nimble/host/src/host_dbg.c
+++ b/net/nimble/host/src/host_dbg.c
@@ -302,6 +302,16 @@ host_hci_dbg_cmd_complete_disp(uint8_t *evdata, uint8_t len)
             BLE_HS_LOG(DEBUG, "states=0x%lx%08lx", le32toh(evdata + 8),
                        le32toh(evdata + 4));
             break;
+        case BLE_HCI_OCF_LE_ENCRYPT:
+            evdata += 4;
+            BLE_HS_LOG(DEBUG, "encdata=0x%02x%02x%02x%02x%02x%02x%02x%02x",
+                       evdata[15], evdata[14], evdata[13], evdata[12],
+                       evdata[11], evdata[10], evdata[9], evdata[8]);
+            BLE_HS_LOG(DEBUG, "%02x%02x%02x%02x%02x%02x%02x%02x",
+                       evdata[7], evdata[6], evdata[5], evdata[4],
+                       evdata[3], evdata[2], evdata[1], evdata[0]);
+
+            break;
         default:
             break;
         }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/host/src/host_hci_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/host_hci_cmd.c b/net/nimble/host/src/host_hci_cmd.c
index 39f7784..5e000fb 100644
--- a/net/nimble/host/src/host_hci_cmd.c
+++ b/net/nimble/host/src/host_hci_cmd.c
@@ -731,6 +731,28 @@ host_hci_cmd_le_set_host_chan_class(uint8_t *chanmap)
 }
 
 /**
+ * Encrypt a block.
+ * 
+ * 
+ * @param key 
+ * @param pt 
+ * 
+ * @return int 
+ */
+int
+host_hci_cmd_le_encrypt(uint8_t *key, uint8_t *pt)
+{
+    int rc;
+    uint8_t cmd[BLE_HCI_LE_ENCRYPT_LEN];
+
+    swap_buf(cmd, key, BLE_ENC_BLOCK_SIZE);
+    swap_buf(cmd + BLE_ENC_BLOCK_SIZE, pt, BLE_ENC_BLOCK_SIZE);
+    rc = host_hci_le_cmd_send(BLE_HCI_OCF_LE_ENCRYPT, BLE_HCI_LE_ENCRYPT_LEN, 
+                              cmd);
+    return rc;
+}
+
+/**
  * Read the RSSI for a given connection handle
  * 
  * NOTE: OGF=0x05 OCF=0x0005 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/include/nimble/ble.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/ble.h b/net/nimble/include/nimble/ble.h
index 90d1093..d84e7d1 100644
--- a/net/nimble/include/nimble/ble.h
+++ b/net/nimble/include/nimble/ble.h
@@ -23,6 +23,16 @@
 /* XXX: some or all of these should not be here */
 #include "os/os.h"
 
+/* BLE encryption block definitions */
+#define BLE_ENC_BLOCK_SIZE       (16)
+
+struct ble_encryption_block
+{
+    uint8_t     key[BLE_ENC_BLOCK_SIZE];
+    uint8_t     plain_text[BLE_ENC_BLOCK_SIZE];
+    uint8_t     cipher_text[BLE_ENC_BLOCK_SIZE];
+};
+
 /* Shared command pool for transort between host and controller */
 extern struct os_mempool g_hci_cmd_pool;
 extern struct os_mempool g_hci_os_event_pool;
@@ -120,6 +130,7 @@ uint16_t le16toh(void *buf);
 uint32_t le32toh(void *buf);
 uint64_t le64toh(void *buf);
 void swap_in_place(void *buf, int len);
+void swap_buf(uint8_t *dst, uint8_t *src, int len);
 /* XXX */
 
 /* BLE Error Codes (Core v4.2 Vol 2 part D) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/include/nimble/hci_common.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/hci_common.h b/net/nimble/include/nimble/hci_common.h
index 331a2ed..408c3a3 100644
--- a/net/nimble/include/nimble/hci_common.h
+++ b/net/nimble/include/nimble/hci_common.h
@@ -295,6 +295,9 @@
 /* --- LE read remote features (OCF 0x0016) */
 #define BLE_HCI_CONN_RD_REM_FEAT_LEN        (2)
 
+/* --- LE encrypt (OCF 0x0017) */
+#define BLE_HCI_LE_ENCRYPT_LEN              (32)
+
 /* --- LE read supported states (OCF 0x001C) --- */
 #define BLE_HCI_RD_SUPP_STATES_RSPLEN       (8)
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/src/hci_common.c
----------------------------------------------------------------------
diff --git a/net/nimble/src/hci_common.c b/net/nimble/src/hci_common.c
index 4480e10..42bbb30 100644
--- a/net/nimble/src/hci_common.c
+++ b/net/nimble/src/hci_common.c
@@ -51,7 +51,7 @@ const uint8_t g_ble_hci_le_cmd_len[BLE_HCI_NUM_LE_CMDS] =
     BLE_HCI_SET_HOST_CHAN_CLASS_LEN,    /* 0x0014: set host chan class */
     sizeof(uint16_t),                   /* 0x0015: read channel map */
     BLE_HCI_CONN_RD_REM_FEAT_LEN,       /* 0x0016: read remote features */
-    0xFF,
+    BLE_HCI_LE_ENCRYPT_LEN,             /* 0x0017: encrypt */
     0xFF,
     0xFF,
     0xFF,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/31285934/net/nimble/src/util.c
----------------------------------------------------------------------
diff --git a/net/nimble/src/util.c b/net/nimble/src/util.c
index d4f786c..8d105d4 100644
--- a/net/nimble/src/util.c
+++ b/net/nimble/src/util.c
@@ -121,3 +121,15 @@ swap_in_place(void *buf, int len)
         u8ptr[j] = tmp;
     }
 }
+
+/* swap octets */
+void
+swap_buf(uint8_t *dst, uint8_t *src, int len)
+{
+    int i;
+
+    for (i = 0; i < len; i++) {
+        dst[len - 1 - i] = src[i];
+    }
+}
+