【BLE】四.SMP安全配对详解

设备配对流程

SMP专业术语

  • Paring(配对):

    配对能力交换,设备认证,密钥生成,连接加密以及机密信息分发等

    过程

  • Bonding(绑定)

    配对中会生成一个长期密钥(LTK,long-term Key),双方把LTK存储 在Flash,那么这两个设备再次重连就可跳过配对流程 ,且直接使用LTK对蓝牙连接进行加密;

    不存储LTK(不分发LTK),paring完成后连接也是加密的,但重连需再次进行paring流程,否则两者还是明文通信;

    在不引起误解的情况下,我们经常把paring当成paring和bonding两者的组合,因为只paring不bonding的应用情况非常少见,下文就不区分paring和bonding的区别。

  • SM(security manager):

    蓝牙协议栈的安全管理层,包括paring,bonding,以及SMP。

  • SMP(security manager protocol)

    安全管理协议,两个设备之间的蓝牙交互命令序列 ,对paring的空中包进行了严格时序规定。

  • MITM(man in the middle):

    A和B通信过程中,C会插入进来以模拟A或者B,并且具备截获和篡改A和B之间所有通信报文的能力;若需要具备MITM保护能力,通过认证实现,分别为:OOB认证信息,passkey以及numeric comparison。

  • OOB(out of band,带外认证):

    不通过蓝牙射频本身来交互,而是-通过如人眼,NFC,UART等带外方式来交互配对信息。

  • Passkey(pin码):

    用户在键盘中输入的6位数字,以达到认证设备的目的。

  • Numeric comparison(数字比较):

    跟passkey一样,也是用来认证设备的6位数字,显示在显示器上 。如下:

  • IO capabilities(输入输出能力):

    是否有键盘,是否有显示器。

  • IRK(Identity Resolving Key,蓝牙设备地址解析密钥):

    可解析的随机地址,比如iPhone手机,由于其地址随着时间会变化 ;IRK通过解析变化的地址的规律 ,从而确定这些地址是否来自同一个设备 ,即IRK可以用来识别蓝牙设备身份;

    IRK一般由设备出厂时按照一定要求自动生成。

  • Identity Address(设备唯一地址):

    包括public ,random static , private resolvable ,random unresolved 共四类;

    设备不支持privacy,那么identity address就等于public或者random static设备地址;。如果设备支持privacy,即使用private resolvable蓝牙设备地址,在这种情况下,虽然其地址每隔一段时间会变化一次,但是identity address仍然保持不变,其取值还是等于内在的public或者random static设备地址。

    Identity Address和IRK都可以用来唯一标识一个蓝牙设备。

  • TK(Temporary Key,临时密钥)

  • STK(short term key,短期密钥)

  • LTK(long term key,长期密钥)

IO能力

配对方式

只要有一端支持OOB,即使用OOB验证配对;只有都不支持才执行中间人(MITM)设置,即看两端是否有其一是支持使用IO功能的,支持就使用IO功能验证配对,如下所示:

IO功能选择

例如手机端作为发起端,一般都是"Keyboard Only"与"KeyBoard Display",如下所示:

其中的无认证:

示例工程:

SMP相关初始化

  • 初始化流程:

  • 设置安全配置参数:

  • 服务端GATT回调:

  • 服务端GAP回调:

  • 客户端GAP回调:

安全配置参数的代码片段

  • GATTS:
c 复制代码
    /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/
	
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_NO_BOND;			//未启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND;				//启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_MITM;			//开启MITM保护
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_ONLY;		//未启用绑定的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_BOND;		//启用绑定后的安全连接
	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM;		//使用MITM保护和未启用连接的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;	//安全连接,启用MITM保护和连接
	
//  esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;           //NoInputNoOutput
	esp_ble_io_cap_t iocap = ESP_IO_CAP_KBDISP;			//Keyboard display
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IO;				//DisplayYesNo
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;			//DisplayOnly
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IN;				//KeyboardOnly

    uint8_t key_size = 16;      //the key size should be 7~16 bytes
    //启动秘钥通过LTK方式加密
    uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
    //响应秘钥通过IRK方式加密  
    uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
    //set static passkey
    uint32_t passkey = 123456;
    uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
//	uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_ENABLE;			//必须绑定才能开启
	
    uint8_t oob_support = ESP_BLE_OOB_DISABLE;			//关闭OOB
//	uint8_t oob_support = ESP_BLE_OOB_ENABLE;			//开启OOB

    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
    /* If your BLE device acts as a Slave, the init_key means you hope which types of key of the master should distribute to you,
    and the response key means which key you can distribute to the master;
    If your BLE device acts as a master, the response key means you hope which types of key of the slave should distribute to you,
    and the init key means which key you can distribute to the slave. */
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
  • GATTC:
c 复制代码
    /* set the security iocap & auth_req & key size & init key response key parameters to the stack*/
	
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_NO_BOND;			//未启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND;				//启用绑定
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_MITM;			//开启MITM保护
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_ONLY;		//未启用绑定的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_BOND;		//启用绑定后的安全连接
	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM;		//使用MITM保护和未启用连接的安全连接
//	esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;	//安全连接,启用MITM保护和连接
	
//  esp_ble_io_cap_t iocap = ESP_IO_CAP_NONE;           //NoInputNoOutput
	esp_ble_io_cap_t iocap = ESP_IO_CAP_KBDISP;			//Keyboard display
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IO;				//DisplayYesNo
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;			//DisplayOnly
//	esp_ble_io_cap_t iocap = ESP_IO_CAP_IN;				//KeyboardOnly



    uint8_t key_size = 16;      //the key size should be 7~16 bytes
    //启动秘钥通过LTK方式加密
    uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
    //响应秘钥通过IRK方式加密
    uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
//    uint8_t oob_support = ESP_BLE_OOB_DISABLE;		//关闭OOB
	uint8_t oob_support = ESP_BLE_OOB_ENABLE;			//开启OOB

    esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &iocap, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_KEY_SIZE, &key_size, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_OOB_SUPPORT, &oob_support, sizeof(uint8_t));
    /* If your BLE device act as a Slave, the init_key means you hope which types of key of the master should distribute to you,
    and the response key means which key you can distribute to the Master;
    If your BLE device act as a master, the response key means you hope which types of key of the slave should distribute to you,
    and the init key means which key you can distribute to the slave. */
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));
    esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
相关推荐
芯片人00713 小时前
GEK100系列芯片进入上市公司上海移远通信供应链
人工智能·单片机·嵌入式硬件·物联网·芯片
热文新讯1 天前
迎进博·话疗愈:2026酒店“疗愈睡眠”闭门研讨会在沪举行
大数据·人工智能·物联网
数字新视界1 天前
机房动环监控系统助力数据中心高效能管理与安全保障
物联网·数据可视化·数据中心基础设施管理·dcim管理系统·数据中心dcim
数字新视界1 天前
数字孪生技术赋能智能化转型与创新实践
嵌入式硬件·物联网·系统架构·机房管理·动力与环境监控系统
注塑机数据采集联网1 天前
数据采集网关入门:ZK-D1/D3从零开始构建你的数据运用
大数据·数据仓库·人工智能·物联网·数据库架构·注塑机网关
智购科技智能售货柜1 天前
顾客已经关门,视觉售货柜还在处理旧画面:延迟积压怎么查?~YH
python·单片机·嵌入式硬件·物联网·eclipse
microrain2 天前
跟着一条属性报文走一遍 SagooIoT 的接入链路
物联网·架构·golang·sagooiot
笨笨饿2 天前
#60_# 一、Photoshop 2025 如何批量导出 PSD 文件中的所有图层为独立 PNG 图片
stm32·单片机·嵌入式硬件·物联网·ui·51单片机·photoshop
钱工的门窗技术笔记2 天前
室内极窄全景移门的支承路径与预留接口:轨道形式、型材壁厚口径与验收边界
物联网·架构·数据分析
Binovate缤江数智2 天前
水泵远程开关之后,还需要管什么?数智开关在无人值守泵站中的监测、保护与能耗分析
人工智能·物联网