NRF52832 study notes (11) - Bluetooth MAC address

I. Background

A BLE device can use two types of addresses (a BLE devices may be provided with two types of addresses at the same time):

  • Public Device Address (common device address)
  • Random Device Address (random device address) can be divided into two categories:
    • Static Device Address (static device address)
    • Private Device Address (privacy device address) can be divided into two categories:
      • Non-resolvable Private Address (unresolvable private address)
      • Resolvable Private Address (resolvable private address)

1.1 Public Public Device Address Device Address

In the communication system, the device address is used to uniquely identify a physical device, such as a MAC address in a network TCP / IP, Bluetooth conventional Bluetooth address. Address in terms of equipment, an important feature is unique.

For classical Bluetooth (BR / EDR), the device address which is a digital 48bits, called "48-bit universal LAN MAC address". Normally, the address needs to apply to the IEEE, unique.

This address allocation is also retained in the BLE, it is a common device address (Public Device Address). Of the 24-bit and 24-bit company_id company_assigned the composition.

High 24 company logo, the low 24's own internal assignment.

1.2 Random Random Device Address Device Address

However, the BLE era, only public address equipment obviously not enough, there are the following reasons:

  1. Public address equipment needs to IEEE purchase, needed to pay expenses.
  2. Application and management of public address equipment is relatively cumbersome, complex, coupled with the large number of BLE devices (and traditional Bluetooth devices is not an order of magnitude), resulting in increased maintenance costs.
  3. 安全因素。BLE 很大一部分的应用场景是广播通信,这意味着只要知道设备的地址,就可以获取所有的信息,这是很不安全的。因此固定的设备地址,加大了信息泄露的风险。

为了解决上述问题,BLE 协议新增了一种地址:随机设备地址,即设备地址不是固定分配的,而是在设备启动后随机生成的。根据不同的目的,随机设备地址分为静态设备地址和私密设备地址。

1.2.1 静态设备地址 Static Device Address

静态设备地址是设备在上电时随机生成的地址,NRF52832 官方工程默认都是使用静态地址,其格式如下:

静态设备地址的特征可总结为:

  1. 最高两个 bit 为 “11”。
  2. 剩余的 46bits 是一个随机数,不能全部为0,也不能全部为1。
  3. 在一个上电周期内保持不变。
  4. 下一次上电的时候可以改变。但不是强制的,因此也可以保持不变。如果改变,上次保存的连接等信息,将不再有效。

静态设备地址的使用场景可总结为:

  1. 46bits 的随机数,可以很好地解决“设备地址唯一性”的问题,因为两个地址相同的概率很小。
  2. 地址随机生成,可以解决公共设备地址申请所带来的费用和维护问题。

1.2.2 私密设备地址 Private Device Address

静态设备地址通过地址随机生成的方式,解决了部分问题。私密设备地址则更进一步,通过定时更新和地址加密两种方式,提高蓝牙地址的可靠性和安全性。根据设备地址是否加密,又分为两类:
① 不可解析私密地址 Non-resolvable Private Address
不可解析私密地址和静态设备地址类似,不同之处在于不可解析私密地址会定时更新。更新的周期是由 GAP 规定的,称作 T_GAP(private_addr_int),建议值是 15 分钟。其格式如下:

不可解析私密地址的特征可总结为:

  1. 最高两个 bit 为 “00”。
  2. 剩余的 46bits 是一个随机数,不能全部为0,也不能全部为1。
  3. 以 T_GAP(private_addr_int) 为周期,定时更新。

② 可解析私密地址 Resolvable Private Address
可解析私密地址比较有用,它通过一个随机数和一个称作 identity resolving key(IRK) 的密码生成,因此只能被拥有相同 IRK 的设备扫描到,可以防止被未知设备扫描和追踪。其格式如下:

可解析私密地址的特征可总结为:

  1. 高位 24bits 是随机数部分,其中最高两个 bit 为“10”,用于标识地址类型;低位 24bits 是随机数和 IRK 经过 hash 运算得到的 hash值,运算公式为 hash = ah(IRK, prand)。
  2. 当主端 BLE 设备扫描到该类型的蓝牙地址后,会使用保存在本机的 IRK,和该地址中的 prand,进行同样的 hash 运算,并将运算结果和地址中的 hash 字段比较,相同的时候,才进行后续的操作。这个过程称作 resolve(解析),如果不同则继续用下一个 IRK 做上面的过程,直到找到一个关联 IRK 或者一个也没找到。
  3. 以T_GAP(private_addr_int) 为周期,定时更新。哪怕在广播、扫描、已连接等过程中,也可能改变。
  4. Resolvable Private Address 不能单独使用,因此需要使用该类型的地址的话,设备要同时具备 Public Device Address 或者 Static Device Address 中的一种。

二、分析广播包中蓝牙MAC地址

使用抓包工具抓取类似如下数据包:

其中数据包第 6 部分:

其中 TxAdd 表示发送方的地址类型(0 为 public,1为 random)。
RxAdd 表示接收方的地址类型。
对于普通广播来说,只有 TxAdd 的指示是有效的,表示广播发送者的第一类型。而对于定向广播来说,TxAdd 和 RxAdd 都是有效的。

其中数据包第 7 部分:

如果是随机设备地址,则查看地址的最高两位。

  • 如果是 “11” 就是静态随机地址。
  • 如果是 “00” 就是不可解析私密地址。
  • 如果是 “01” 就是可解析私密地址,并执行上面说过的 ah 方法进行解析。

三、读取MAC地址

  1. 包含头文件
#include "ble_gap.h"
  1. 定义结构体变量
ble_gap_addr_t bleAddr;

BLE 的地址由 ble_gap_addr_t 进行管理:

/**@brief Bluetooth Low Energy address. */
typedef struct
{
  uint8_t addr_id_peer : 1;       /**< Only valid for peer addresses.
                                       This bit is set by the SoftDevice to indicate whether the address has been resolved from
                                       a Resolvable Private Address (when the peer is using privacy).
                                       If set to 1, @ref addr and @ref addr_type refer to the identity address of the resolved address.

                                       This bit is ignored when a variable of type @ref ble_gap_addr_t is used as input to API functions. */
  uint8_t addr_type    : 7;       /**< See @ref BLE_GAP_ADDR_TYPES. */
  uint8_t addr[BLE_GAP_ADDR_LEN]; /**< 48-bit address, LSB format.
                                       @ref addr is not used if @ref addr_type is @ref BLE_GAP_ADDR_TYPE_ANONYMOUS. */
} ble_gap_addr_t;
  • addr_id_peer:仅对匹配地址有效。此位由软件设置,以指示该地址是否已从可解析私密地址中解析(当匹配加密时)
  • addr_type:地址类型,作为传入参数时可忽略
  • addr:48-bit 的MAC地址数组,低字节在前,所以与我们实际看到的地址顺序相反
  1. 调用获取地址函数
    uint32_t sd_ble_gap_addr_get(ble_gap_addr_t *p_addr)
sd_ble_gap_addr_get(&bleAddr);
  1. 将地址逆序并打印出来
#include "nrf_log.h"

uint8 address[6];
uint8 i;
for(i = 0; i < 6; i++)
{
  address[i] = bleAddr.addr[5 - i];    // 逆序
}

NRF_LOG_INFO("address:%02x", address[0]);
NRF_LOG_INFO("address:%02x", address[1]);
NRF_LOG_INFO("address:%02x", address[2]);
NRF_LOG_INFO("address:%02x", address[3]);
NRF_LOG_INFO("address:%02x", address[4]);
NRF_LOG_INFO("address:%02x", address[5]);

这样打印出来的地址就跟我们实际扫描到的地址顺序相同。

四、修改MAC地址

首先采用 sd_ble_gap_addr_get 读取官方默认的 MAC 地址,然后再默认地址+1,再用 sd_ble_gap_addr_set 写入到设备中。

void mac_set(void)
{
  ble_gap_addr_t addr;
  uint32_t err_code = sd_ble_gap_addr_get(&addr);
  APP_ERROR_CHECK(err_code);

  // Increase the BLE address by one when advertising openly.
  addr.addr[0] += 1;

  err_code = sd_ble_gap_addr_set(&addr);
  APP_ERROR_CHECK(err_code);
}

主函数中进行调用,注意,一定要在广播开始前设置,下次广播后新的 MAC 地址就设置成功。

/**@brief Application main function.
 */
int main(void)
{
    bool erase_bonds;

    // Initialize.
    uart_init();
    log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
	  mac_set();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    // Start execution.
    advertising_start();

    // Enter main loop.
    for (;;)
    {
        idle_state_handle();
    }
}

新的 MAC 地址加1,设置成功。


• 由 Leung 写于 2020 年 2 月 19 日

• 参考:青风电子社区

发布了89 篇原创文章 · 获赞 134 · 访问量 7万+

Guess you like

Origin blog.csdn.net/qq_36347513/article/details/104392882