Explorar o código

Merge branch 'bugfix/ble_mesh_not_use_same_private_key_v4.1' into 'release/v4.1'

ble_mesh: Not using pre-initialized private key (v4.1)

See merge request espressif/esp-idf!8945
Island %!s(int64=5) %!d(string=hai) anos
pai
achega
6971cdbad5

+ 12 - 12
components/bt/esp_ble_mesh/mesh_core/bluedroid_host/mesh_bearer_adapt.c

@@ -46,12 +46,7 @@ struct bt_mesh_dev bt_mesh_dev;
 
 /* P-256 Variables */
 static u8_t bt_mesh_public_key[64];
-static BT_OCTET32 bt_mesh_private_key = {
-    0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38,
-    0x74, 0xc9, 0xb3, 0xe3, 0xd2, 0x10, 0x3f, 0x50,
-    0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99,
-    0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd
-};
+static BT_OCTET32 bt_mesh_private_key;
 
 /* Scan related functions */
 static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb;
@@ -102,11 +97,6 @@ int bt_mesh_host_init(void)
     return 0;
 }
 
-int bt_mesh_host_deinit(void)
-{
-    return 0;
-}
-
 void bt_mesh_hci_init(void)
 {
     const uint8_t *features = controller_get_interface()->get_features_ble()->as_array;
@@ -1797,8 +1787,17 @@ void bt_mesh_gatt_deinit(void)
 void bt_mesh_adapt_init(void)
 {
     BT_DBG("%s", __func__);
+
     /* initialization of P-256 parameters */
     p_256_init_curve(KEY_LENGTH_DWORDS_P256);
+
+    /* Set "bt_mesh_dev.flags" to 0 (only the "BLE_MESH_DEV_HAS_PUB_KEY"
+     * flag is used) here, because we need to make sure each time after
+     * the private key is initialized, a corresponding public key must
+     * be generated.
+     */
+    bt_mesh_atomic_set(bt_mesh_dev.flags, 0);
+    bt_mesh_rand(bt_mesh_private_key, sizeof(bt_mesh_private_key));
 }
 
 int bt_mesh_rand(void *buf, size_t len)
@@ -1854,7 +1853,8 @@ const u8_t *bt_mesh_pub_key_get(void)
     memcpy(bt_mesh_public_key + BT_OCTET32_LEN, public_key.y, BT_OCTET32_LEN);
 
     bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY);
-    BT_DBG("gen the bt_mesh_public_key:%s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key)));
+
+    BT_DBG("Public Key %s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key)));
 
     return bt_mesh_public_key;
 }

+ 0 - 1
components/bt/esp_ble_mesh/mesh_core/include/mesh_bearer_adapt.h

@@ -665,7 +665,6 @@ struct bt_mesh_gatt_attr {
 }
 
 int bt_mesh_host_init(void);
-int bt_mesh_host_deinit(void);
 
 int bt_le_adv_start(const struct bt_mesh_adv_param *param,
                     const struct bt_mesh_adv_data *ad, size_t ad_len,

+ 12 - 7
components/bt/esp_ble_mesh/mesh_core/nimble_host/mesh_bearer_adapt.c

@@ -63,12 +63,7 @@ struct bt_mesh_dev bt_mesh_dev;
 
 /* P-256 Variables */
 static u8_t bt_mesh_public_key[64];
-static u8_t bt_mesh_private_key[32] = {
-    0x3f, 0x49, 0xf6, 0xd4, 0xa3, 0xc5, 0x5f, 0x38,
-    0x74, 0xc9, 0xb3, 0xe3, 0xd2, 0x10, 0x3f, 0x50,
-    0x4a, 0xff, 0x60, 0x7b, 0xeb, 0x40, 0xb7, 0x99,
-    0x58, 0x99, 0xb8, 0xa6, 0xcd, 0x3c, 0x1a, 0xbd
-};
+static u8_t bt_mesh_private_key[32];
 
 /* Scan related functions */
 static bt_mesh_scan_cb_t *bt_mesh_scan_dev_found_cb;
@@ -1758,8 +1753,17 @@ void ble_sm_alg_ecc_init(void);
 void bt_mesh_adapt_init(void)
 {
     BT_DBG("%s", __func__);
+
     /* initialization of P-256 parameters */
     ble_sm_alg_ecc_init();
+
+    /* Set "bt_mesh_dev.flags" to 0 (only the "BLE_MESH_DEV_HAS_PUB_KEY"
+     * flag is used) here, because we need to make sure each time after
+     * the private key is initialized, a corresponding public key must
+     * be generated.
+     */
+    bt_mesh_atomic_set(bt_mesh_dev.flags, 0);
+    bt_mesh_rand(bt_mesh_private_key, sizeof(bt_mesh_private_key));
 }
 
 int bt_mesh_rand(void *buf, size_t len)
@@ -1815,7 +1819,8 @@ const u8_t *bt_mesh_pub_key_get(void)
     memcpy(bt_mesh_private_key, pri_key, 32);
 
     bt_mesh_atomic_set_bit(bt_mesh_dev.flags, BLE_MESH_DEV_HAS_PUB_KEY);
-    BT_DBG("gen the bt_mesh_public_key:%s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key)));
+
+    BT_DBG("Public Key %s", bt_hex(bt_mesh_public_key, sizeof(bt_mesh_public_key)));
 
     return bt_mesh_public_key;
 }

+ 1 - 1
components/bt/esp_ble_mesh/mesh_core/prov.c

@@ -98,7 +98,7 @@ enum {
     REMOTE_PUB_KEY,        /* Remote key has been received */
     OOB_PUB_KEY,           /* OOB public key is available */
     LINK_ACTIVE,           /* Link has been opened */
-    HAVE_DHKEY,            /* DHKey has been calcualted */
+    HAVE_DHKEY,            /* DHKey has been calculated */
     SEND_CONFIRM,          /* Waiting to send Confirm value */
     WAIT_NUMBER,           /* Waiting for number input from user */
     WAIT_STRING,           /* Waiting for string input from user */