Ble - SMP 協議詳解

SMP Overview

  • 藍牙的配對過程中,加密是核心步驟,SMP關乎到藍牙通信的安全。
  • 在固定信道0x0006中傳輸數據,0x0006 LE SMP(secure manager protocol)b
  • 整個smp過程根據是否支持ble security connection,設備IO capabilities,是否支持OOB(out of band)總共分爲6種情況,最終協商確定1種方式實現smp flow。

SMP 命令格式

如果支持LE security connect , L2CAP MTU = 65
否則 L2CAP MTU = 23
在這裏插入圖片描述
在這裏插入圖片描述
所有的交互從pairing request 開始,如果設備不支持smp,需要回復一個pairing fail event, 攜帶的reason爲not support

超時

開啓smp 之前,會起一個timer,
每次完成一個command ,smp timer 會被重置,重新計時
smp 結束之後,這個timer 需要被關閉
如果timer =30s,smp 仍未完成,smp timeout

SMP 流程

smp 過程大概分爲四個部分

  • step1. 交換信息
  • step2. 產生STK/LTK,(short term key,long term key)
  • step3. 在stk /ltk的基礎上加密link
  • step4. 在加密的link中分發 特殊的key
    以下配圖以LE legacy pairing爲例說明:
    在這裏插入圖片描述

step1:pairing feature exchange

在這一步中,分兩種情況,從master發起或者從slave 發起,後半段操作並無區別。
master 收到slave 的request 之後,會主動發起pairing request。
在這裏插入圖片描述
在這裏插入圖片描述

舉個例子

在這裏插入圖片描述
在這裏插入圖片描述
根據Secure connection pairing參數,決定SMP第二步有兩條分支
LE legacy pairing 和LE Secure Connections,例子中的鏈接屬於LE legacy pairing

step2. AUTHENTICATING AND ENCRYPTING

根據第一步信息的交換,可以決定兩個大分支,
再根據IO cap 和OOB等信息,一個大分支都包含三個小分支,
所以整體的SMP配對過程,總共有6種情況出現,詳細如下:

LE legacy pairing

  1. Just Work
    在這裏插入圖片描述
    類似與remote control與TV 的鏈接,由於rc io cap定義爲NoInputNoOutput,最後的協商結果就是justwork

  2. Pass key Entry
    同樣是根據IO cap 協商的結果
    在這裏插入圖片描述

  3. OOB
    根據OOB 參數協商的結果在這裏插入圖片描述

LE Secure Connections

  1. 區別於legacy ble 鏈接, LE secure connections 包括交換public key 的特殊步驟,

在這裏插入圖片描述
2. 第二個階段根據OBB和IO Cap 不同,可以分爲三種情況。
這一步的核心目的是,雙方根據不同的參數,計算Na,Nb,並且進行校覈,爲下一步計算LTK做準備

just work :ra =rb =0
在這裏插入圖片描述
Passkey Entry: master 會接收到slave 輸入key,雙方都會計算Na,ra,rb,Nb,然後雙方校覈,重複20次

在這裏插入圖片描述
通過oob 管道,雙方互換信息,然後計算,覈對Na,Nb
在這裏插入圖片描述
3.LTK計算
Once the DHKey generation is complete, the Long Term Key (LTK) is
calculated from the DHKey.
在這裏插入圖片描述
4.DHkey Check,加密 的第二個階段
在這裏插入圖片描述

step3 TRANSPORT SPECIFIC KEY DISTRIBUTION

在這裏插入圖片描述

SMP 過程中key 的產生與計算

舉個栗子

LE legacy pairing +Just Work
在這裏插入圖片描述

  • 772 :slave 發起smp 請求
  • 773: master 發起request,攜帶local 信息
  • 780:slave return response, 攜帶slave 信息

根據雙方信息,決定第二步的方式是LE legacy pairing +Just Work, (1/6)

  • 786/788: confirm計算及交換

confirm value :16 octots = Mconfirm
在這裏插入圖片描述
Mconfirm = c1(TK, Mrand,
Pairing Request command, Pairing Response command,
initiating device address type, initiating device address,
responding device address type, responding device address)

重點理解一下c1函數:
c1 (k, r, preq, pres, iat, rat, ia, ra) = e(k, e(k, r XOR p1) XOR p2)

TK值是一個128bit number
在justwork 中tk=0,
passkey entry:tk 0-999999
OOB:tk 128bit

函數e,128bit -AES
128bit key和128bit 一段文本,輸出128bit加密數據
encryptedData = e(key, plaintextData)

計算stk使用s1函數
s1(k, r1, r2) = e(k, r’)
三個輸入參數都是128bit
r’ 是有r1高位 64bit+ r2低位64bit拼接而成(大端模式)

Mconfirm計算

  • 789/791,計算並交換Mrand和Srand
    上一步已經計算出local confirm值,這一步獲取到對端的random值,需要計算出peer confirm 值,如果相等,則會進一步計算STK
    STK = s1(TK, Srand, Mrand)
    根據rand和TK值計算
    發起端需要使用STK值,加密鏈接。

  • 791-808
    最終加密之後交換如下幾個信息:
    Encryption Information:
    此過程中需要根據已經生成的STK產生LTK,重新加密,
    交換LTK
    所有支持l2cap 加密的設備,都要有能力生成 LTK, EDIV, and Rand.
    LTK = d1(ER, DIV, 0)
    CSRK = d1(ER, DIV, 1)
    d1(k, d, r) = e(k, d’)
    d’ = padding || r || d

ER key 產生途徑可以是隨機產生的128bit或者自定義的值

Master Identification
已經被加密或者根據STK重新加密的時候會分發
在這裏插入圖片描述
Identity Information
交換IRK, 根據對端的IRK,可以解析出對端的隨機地址

Note: An all zero Identity Resolving Key data field indicates that a device does
not have a valid resolvable private address
如果全爲0,沒有可解析的地址,不支持RPA
IRK = d1(IR, 1, 0)
DHK = d1(IR, 3, 0)
DHK不會被分發,只會自己根據交換的參數計算。
The generation of DHK using IR is only applicable when doing LE Legacy Pairing
The generation of IRK using IR is applicable both when doing LE Legacy Pairing and LE Secure Connections Pairing.

IR key 產生途徑可以是隨機產生的128bit或者自定義的值

Identity Address Information
交換identify addr 和addr type

Signing Information
Connection Signature Resolving Key (CSRK)
通過d1函數產生

綜上所述,LE legacy pairing 過程中需要交換的key如下:
LTK
EDIV and Rand
IRK
BD_ADDR
CSRK

核心函數e

再舉個栗子

LE security connect +Numeric Comparison
在這裏插入圖片描述
在這種模式下有如下key 會被交換:

  1. IRK
  2. BD ADDR
  3. CSRK

核心函數:
Cipher-based Message Authentication Code (CMAC)
that uses AES-128 as the block cipher function, also known as AES-CMAC
MAC = AES-CMACk(m)
k 128bit key
m可變長度

public key
P256 產生,private key,public key x,public y
當設備獲取到對端的public key 之後,開始計算local DHK
DHkey=P256(Ska,Pkb)
輸入參數爲local private(secret)key和對端的public key

pairing confirm
產生並交換 Ca,Cb
在這裏插入圖片描述
使用到了函數f4
f4(U, V, X, Z) = AES-CMACx (U || V || Z)
Na,Nb,ra,rb是隨機值,作爲key
核心參數是local 和peer public key x

pairing random
交換隨機值,這一步交換的是Na, Nb

DHkey check
傳遞Ea/Eb, DHkey check value, DHkey 本身不會交換。
計算Ea/Eb需要用到f6
在這裏插入圖片描述
A/B是雙方的address,
Mackey 又會引申到需要調用f5實現
MacKey || LTK = f5(DHKey, N_master, N_slave, BD_ADDR_master,BD_ADDR_slave)

f5(W, N1, N2, A1, A2) = AES-CMACt (Counter = 0 || keyID ||N1 || N2|| A1||A2|| Length = 256)||AES-CMACt(Counter = 1 || keyID || N1 || N2|| A1|| A2 ||Length = 256)
counter =0,計算的是MacKey
counter =1,計算的是LTK
在security connect 中,不會交換LTK,根據當前信息,可以計算出local 和peer

w與T的關係
T = AES-CMACsalt (W)
salt 是一個固定值,單詞map帶assic碼,
W此次爲計算的DHKey

另外幾個常見的函數用法
g2 is used to generate the 6-digit numeric comparison values during authentication stage 1 in the pairing process.
h6 is used to generate the LE LTK from a BR/EDR link key derived from Secure Connections and is used to generate the BR/EDR link key from an LE LTK derived from Secure Connections.
h7 is used to generate intermediate keys while generating the LE LTK from a BR/EDR link key derived from Secure Connections and the BR/EDR link key
from an LE LTK derived from Secure Connections.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章