跳转至

第七章:安全加密机制

深入解析蓝牙安全架构:E0 流密码、AES-CCM 加密、配对认证协议、密钥管理及硬件实现


7.1 蓝牙安全架构概述

7.1.1 安全目标

蓝牙安全机制提供以下保障:

┌─────────────────────────────────────────────────────────┐
│                    蓝牙安全目标                          │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  1. 机密性 (Confidentiality)                            │
│     ├─ 防止窃听                                         │
│     └─ 通过加密实现                                     │
│                                                         │
│  2. 完整性 (Integrity)                                  │
│     ├─ 防止数据篡改                                     │
│     └─ 通过 MIC (消息完整性校验) 实现                     │
│                                                         │
│  3. 认证 (Authentication)                               │
│     ├─ 确认设备身份                                     │
│     └─ 通过配对协议实现                                 │
│                                                         │
│  4. 授权 (Authorization)                                │
│     ├─ 控制访问权限                                     │
│     └─ 通过绑定和信任关系实现                           │
│                                                         │
│  5. 隐私 (Privacy)                                      │
│     ├─ 防止设备追踪                                     │
│     └─ 通过地址轮换实现 (BLE 4.2+)                       │
│                                                         │
└─────────────────────────────────────────────────────────┘

7.1.2 安全演进

蓝牙版本与安全特性:

Bluetooth 1.0-2.0:
├─ E0 流密码加密
├─ SAFER+ 认证
└─ 固定地址 (可追踪)

Bluetooth 2.1+EDR:
├─ 安全简单配对 (SSP)
├─ ECDH 密钥交换
├─ Numeric Comparison
└─ OOB 配对

Bluetooth 4.0-4.1 (BLE):
├─ AES-CCM 加密
├─ LE 配对
└─ 临时密钥生成

Bluetooth 4.2+:
├─ LE 安全连接
├─ ECDH P-256
├─ 隐私特性 (地址轮换)
└─ 安全连接确认

Bluetooth 5.0+:
├─ 增强隐私
├─ 广播加密
└─ Mesh 安全

7.2 经典蓝牙 E0 流密码

7.2.1 E0 加密架构

┌─────────────────────────────────────────────────────────┐
│                    E0 流密码架构                         │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  ┌────────┐ │
│  │  LFSR-1  │  │  LFSR-2  │  │  LFSR-3  │  │LFSR-4  │ │
│  │  25 级   │  │  22 级   │  │  22 级   │  │ 17 级  │ │
│  └────┬─────┘  └────┬─────┘  └────┬─────┘  └───┬────┘ │
│       │            │            │             │       │
│       └────────────┴────────────┴─────────────┘       │
│                          │                            │
│                          ▼                            │
│                 ┌─────────────────┐                   │
│                 │   组合器函数    │                   │
│                 │   (非线性)      │                   │
│                 └────────┬────────┘                   │
│                          │                            │
│                          ▼                            │
│                   密钥流输出 z_t                      │
│                          │                            │
│                          ▼                            │
│                    ⊕ 明文数据                         │
│                          │                            │
│                          ▼                            │
│                    密文输出                           │
│                                                         │
└─────────────────────────────────────────────────────────┘

总状态:25 + 22 + 22 + 17 = 86 bit
有效密钥长度:1-128 bit (蓝牙通常用 64 或 128 bit)

7.2.2 LFSR 结构

LFSR 抽头多项式:

LFSR-1 (25 级):
P1(x) = x²⁵ + x²⁰ + x¹² + x⁸ + 1
抽头位置:25, 20, 12, 8

LFSR-2 (22 级):
P2(x) = x²² + x²¹ + x¹⁶ + x¹³ + x⁹ + 1
抽头位置:22, 21, 16, 13, 9

LFSR-3 (22 级):
P3(x) = x²² + x²¹ + x¹⁵ + x¹³ + x⁹ + 1
抽头位置:22, 21, 15, 13, 9

LFSR-4 (17 级):
P4(x) = x¹⁷ + x¹⁴ + 1
抽头位置:17, 14

时钟控制:
每个 LFSR 根据组合器输出 c_t 决定是否时钟
c_t = floor((x1 + x2 + x3 + x4) / 2)

LFSR-i 时钟条件:
├─ c_t = 0: LFSR-1 和 LFSR-4 时钟
└─ c_t = 1: LFSR-2 和 LFSR-3 时钟

7.2.3 E0 加密 Verilog 实现

// E0 流密码加密引擎
module e0_cipher_engine (
    input wire clk,
    input wire reset_n,
    input wire [127:0] key,       // 加密密钥 (最多 128 bit)
    input wire [63:0] bd_addr,    // 蓝牙地址
    input wire [26:0] clk_counter,// 蓝牙时钟
    input wire [7:0] plaintext,   // 明文输入
    output reg [7:0] ciphertext,  // 密文输出
    input wire enable,
    output reg keystream_valid
);

// 4 个 LFSR 寄存器
reg [24:0] lfsr1;  // 25 bit
reg [21:0] lfsr2;  // 22 bit
reg [21:0] lfsr3;  // 22 bit
reg [16:0] lfsr4;  // 17 bit

// 组合器状态
reg [1:0] sum_reg;  // 2 bit 记忆状态

// LFSR 反馈计算
wire lfsr1_feedback = lfsr1[24] ^ lfsr1[19] ^ lfsr1[11] ^ lfsr1[7];
wire lfsr2_feedback = lfsr2[21] ^ lfsr2[20] ^ lfsr2[15] ^ lfsr2[12] ^ lfsr2[8];
wire lfsr3_feedback = lfsr3[21] ^ lfsr3[20] ^ lfsr3[14] ^ lfsr3[12] ^ lfsr3[8];
wire lfsr4_feedback = lfsr4[16] ^ lfsr4[13];

// LFSR 输出位
wire x1 = lfsr1[24];
wire x2 = lfsr2[21];
wire x3 = lfsr3[21];
wire x4 = lfsr4[16];

// 组合器计算
wire [2:0] sum = x1 + x2 + x3 + x4 + sum_reg;
wire z = sum[0];  // 密钥流输出
wire [1:0] next_sum = sum[2:1];  // 下一状态

// LFSR 时钟控制
wire clock_lfsr1_4 = (sum[1] == 1'b0);
wire clock_lfsr2_3 = (sum[1] == 1'b1);

// LFSR 更新
always @(posedge clk or negedge reset_n) begin
    if (!reset_n) begin
        lfsr1 <= 25'h0000001;
        lfsr2 <= 22'h000001;
        lfsr3 <= 22'h000001;
        lfsr4 <= 17'h0001;
        sum_reg <= 2'b00;
    end else if (enable) begin
        // 时钟控制
        if (clock_lfsr1_4) begin
            lfsr1 <= {lfsr1[23:0], lfsr1_feedback};
            lfsr4 <= {lfsr4[15:0], lfsr4_feedback};
        end
        if (clock_lfsr2_3) begin
            lfsr2 <= {lfsr2[20:0], lfsr2_feedback};
            lfsr3 <= {lfsr3[20:0], lfsr3_feedback};
        end

        // 组合器状态更新
        sum_reg <= next_sum;
    end
end

// 密钥流生成和加密
always @(posedge clk) begin
    if (enable) begin
        keystream_valid <= 1'b1;
        ciphertext <= plaintext ^ {8{z}};  // 简单示例,实际应生成 8 bit 密钥流
    end
end

// 密钥加载和初始化
// (省略:需要 2 阶段初始化 - 密钥加载和时钟同步)

endmodule

7.2.4 E0 初始化流程

E0 加密初始化分为两个阶段:

阶段 1: 密钥加载
┌─────────────────────────────────────────────────────────┐
│  输入:K (密钥), BD_ADDR (地址), CLK (时钟)              │
│                                                         │
│  1. 将 K 和 BD_ADDR 混合加载到 4 个 LFSR                  │
│  2. 运行 100 个时钟周期 (不输出)                          │
│  3. LFSR 达到初始状态                                    │
└─────────────────────────────────────────────────────────┘

阶段 2: 每帧初始化
┌─────────────────────────────────────────────────────────┐
│  每个数据包加密前:                                      │
│                                                         │
│  1. 使用 CLK (26 bit) 重新初始化 LFSR                    │
│  2. 运行 100 个时钟周期                                   │
│  3. 开始生成密钥流                                       │
│  4. 与 payload 异或得到密文                              │
└─────────────────────────────────────────────────────────┘

7.3 BLE AES-CCM 加密

7.3.1 AES-CCM 架构

AES-CCM = CTR 模式加密 + CBC-MAC 认证

┌─────────────────────────────────────────────────────────┐
│                    AES-CCM 加密流程                      │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  输入:                                                  │
│  ├─ 明文数据 (P)                                        │
│  ├─ 关联数据 (A) - 不加密但需要认证                      │
│  ├─  nonce (N) - 13 字节                                 │
│  └─  密钥 (K) - 128 bit                                 │
│                                                         │
│  阶段 1: CBC-MAC (认证)                                  │
│  ┌─────────────────────────────────────────────────┐   │
│  │  B0 (标志 + 长度)                                │   │
│  │    │                                            │   │
│  │    ▼                                            │   │
│  │  AES ──► T1                                     │   │
│  │    │                                            │   │
│  │    ▼                                            │   │
│  │  A (关联数据) ──► AES ──► T2                    │   │
│  │    │                                            │   │
│  │    ▼                                            │   │
│  │  P (明文) ──► AES ──► T3                        │   │
│  │    │                                            │   │
│  │    ▼                                            │   │
│  │  填充 ──► AES ──► MAC (4/8 字节)                  │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
│  阶段 2: CTR 模式 (加密)                                 │
│  ┌─────────────────────────────────────────────────┐   │
│  │  Counter 0 ──► AES ──► 加密 MAC                  │   │
│  │  Counter 1 ──► AES ──► 加密 P[0]                 │   │
│  │  Counter 2 ──► AES ──► 加密 P[1]                 │   │
│  │  ...                                            │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
│  输出:                                                  │
│  ├─ 密文数据 (C)                                        │
│  └─  MIC (消息完整性校验码)                             │
│                                                         │
└─────────────────────────────────────────────────────────┘

7.3.2 AES-128 核心实现

// AES-128 加密核心
module aes_128_encrypt (
    input wire clk,
    input wire reset_n,
    input wire [127:0] plaintext,
    input wire [127:0] key,
    output reg [127:0] ciphertext,
    input wire start,
    output reg done
);

// AES 状态寄存器 (4x4 字节矩阵)
reg [127:0] state;
reg [127:0] round_key;

// 轮数计数
reg [3:0] round_count;

// 密钥扩展
wire [127:0] expanded_key;
key_expansion u_key_exp (
    .key(key),
    .round(round_count),
    .round_key(expanded_key)
);

// SubBytes (S 盒替换)
wire [127:0] sub_bytes_out;
sub_bytes u_sub (
    .state_in(state),
    .state_out(sub_bytes_out)
);

// ShiftRows (行移位)
wire [127:0] shift_rows_out;
shift_rows u_shift (
    .state_in(sub_bytes_out),
    .state_out(shift_rows_out)
);

// MixColumns (列混合)
wire [127:0] mix_columns_out;
mix_columns u_mix (
    .state_in(shift_rows_out),
    .state_out(mix_columns_out),
    .last_round(round_count == 4'd10)
);

// AddRoundKey (轮密钥加)
assign round_key = expanded_key;

// 主状态机
always @(posedge clk or negedge reset_n) begin
    if (!reset_n) begin
        state <= 128'd0;
        round_count <= 4'd0;
        done <= 1'b0;
    end else if (start) begin
        state <= plaintext;
        round_count <= 4'd0;
        done <= 1'b0;
    end else if (round_count < 4'd10) begin
        // AES-128: 10 轮
        state <= mix_columns_out ^ round_key;
        round_count <= round_count + 1'b1;
        done <= 1'b0;
    end else if (round_count == 4'd10) begin
        // 最后一轮 (无 MixColumns)
        ciphertext <= (sub_bytes_out ^ shift_rows_out) ^ round_key;
        done <= 1'b1;
    end
end

endmodule

7.3.3 CCM 模式控制器

// AES-CCM 控制器
module aes_ccm_controller (
    input wire clk,
    input wire reset_n,

    // 输入数据
    input wire [127:0] plaintext,
    input wire [103:0] nonce,      // 13 字节 nonce
    input wire [127:0] key,
    input wire [127:0] aad,        // 关联数据
    input wire [15:0] data_length,

    // 输出
    output reg [127:0] ciphertext,
    output reg [31:0] mic,         // 4 字节 MIC

    // 控制
    input wire start,
    output reg done,
    output reg busy
);

localparam STATE_IDLE = 3'd0;
localparam STATE_CBC_INIT = 3'd1;
localparam STATE_CBC_AAD = 3'd2;
localparam STATE_CBC_DATA = 3'd3;
localparam STATE_CBC_FINAL = 3'd4;
localparam STATE_CTR_ENC = 3'd5;
localparam STATE_CTR_MIC = 3'd6;
localparam STATE_DONE = 3'd7;

reg [2:0] state;
reg [3:0] counter;
reg [127:0] cbc_mac;
reg [127:0] ctr_key;

// AES 引擎实例
wire aes_done;
wire [127:0] aes_out;
aes_128_encrypt u_aes (
    .clk(clk),
    .reset_n(reset_n),
    .plaintext(aes_input),
    .key(key),
    .ciphertext(aes_out),
    .start(aes_start),
    .done(aes_done)
);

// 主状态机
always @(posedge clk or negedge reset_n) begin
    if (!reset_n) begin
        state <= STATE_IDLE;
        done <= 1'b0;
        busy <= 1'b0;
        counter <= 4'd0;
    end else begin
        case (state)
            STATE_IDLE: begin
                if (start) begin
                    state <= STATE_CBC_INIT;
                    busy <= 1'b1;
                    counter <= 4'd0;
                end
            end

            STATE_CBC_INIT: begin
                // B0 块:标志 + nonce + 长度
                aes_input <= construct_b0(nonce, data_length);
                aes_start <= 1'b1;
                state <= STATE_CBC_AAD;
            end

            STATE_CBC_AAD: begin
                if (aes_done) begin
                    cbc_mac <= aes_out;
                    // 处理 AAD
                    aes_input <= construct_aad_block(aad, cbc_mac);
                    aes_start <= 1'b1;
                    state <= STATE_CBC_DATA;
                end
            end

            STATE_CBC_DATA: begin
                if (aes_done) begin
                    cbc_mac <= aes_out;
                    if (counter < data_length / 16) begin
                        // 继续处理数据块
                        aes_input <= plaintext[counter*128 +: 128] ^ cbc_mac;
                        aes_start <= 1'b1;
                        counter <= counter + 1'b1;
                    end else begin
                        state <= STATE_CBC_FINAL;
                    end
                end
            end

            STATE_CBC_FINAL: begin
                // CBC-MAC 完成
                mic <= aes_out[127:96];  // 取高 32 位作为 MIC
                state <= STATE_CTR_ENC;
            end

            STATE_CTR_ENC: begin
                // CTR 模式加密
                ctr_key <= construct_counter(nonce, counter);
                aes_input <= ctr_key;
                aes_start <= 1'b1;
                state <= STATE_CTR_MIC;
            end

            STATE_CTR_MIC: begin
                if (aes_done) begin
                    ciphertext <= plaintext ^ aes_out;
                    state <= STATE_DONE;
                end
            end

            STATE_DONE: begin
                done <= 1'b1;
                busy <= 1'b0;
                state <= STATE_IDLE;
            end
        endcase
    end
end

endmodule

7.4 配对与认证协议

7.4.1 经典蓝牙配对

经典蓝牙配对流程 (2.1+ SSP):

┌─────────────────────────────────────────────────────────┐
│                    安全简单配对 (SSP)                    │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  阶段 1: 能力交换                                       │
│  ┌─────────┐              ┌─────────┐                 │
│  │ 设备 A  │              │ 设备 B  │                 │
│  │         │──IO Capabilities──►│                     │
│  │         │◄──IO Capabilities──│                     │
│  │         │  交换 IO 能力、认证要求                    │
│  └─────────┘              └─────────┘                 │
│                                                         │
│  阶段 2: 密钥生成 (DHKey 计算)                            │
│  ┌─────────────────────────────────────────────────┐   │
│  │  1. 生成 P-256 公私钥对                            │   │
│  │     Private Key (随机 256 bit)                   │   │
│  │     Public Key (椭圆曲线点)                       │   │
│  │                                                  │   │
│  │  2. 交换公钥                                     │   │
│  │     A ──► Public Key A ──► B                    │   │
│  │     B ──► Public Key B ──► A                    │   │
│  │                                                  │   │
│  │  3. 计算 DHKey = DH(A_private, B_public)         │   │
│  │     = DH(B_private, A_public)                   │   │
│  │     (双方计算结果相同)                            │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
│  阶段 3: 认证 (4 种方法)                                  │
│  ┌─────────────────────────────────────────────────┐   │
│  │  Numeric Comparison (数字比较):                  │   │
│  │  ├─ 双方显示 6 位数字                              │   │
│  │  ├─ 用户确认是否相同                             │   │
│  │  └─ 用于双显示设备 (手机 + 手机)                    │   │
│  │                                                  │   │
│  │  Passkey Entry (密码输入):                       │   │
│  │  ├─ 一方显示 6 位数字                              │   │
│  │  ├─ 另一方输入                                   │   │
│  │  └─ 用于手机 + 键盘                               │   │
│  │                                                  │   │
│  │  OOB (带外):                                     │   │
│  │  ├─ 通过 NFC 等传输配对信息                        │   │
│  │  └─ 用于支持 NFC 的设备                            │   │
│  │                                                  │   │
│  │  Just Works (直接工作):                          │   │
│  │  ├─ 无用户交互                                   │   │
│  │  └─ 最低安全级别                                 │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
│  阶段 4: 链路密钥生成                                   │
│  ┌─────────────────────────────────────────────────┐   │
│  │  Link Key = f(DHKey, BD_ADDR_A, BD_ADDR_B, ...) │   │
│  │                                                  │   │
│  │  生成后存储,用于后续连接认证                      │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
└─────────────────────────────────────────────────────────┘

7.4.2 BLE 配对流程

BLE 配对流程 (4.2+ LE Secure Connections):

┌─────────────────────────────────────────────────────────┐
│                    LE 安全连接配对                       │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  阶段 1: 特性交换 (Pairing Feature Exchange)            │
│  ┌─────────────────────────────────────────────────┐   │
│  │  Pairing Request  ──────────────────────────►   │   │
│  │  ├─ IO Capability (DisplayOnly/KeyboardOnly/...)│   │
│  │  ├─ OOB Data Flag                               │   │
│  │  ├─ Auth Req (Bonding/MITM/SC/Keypress)         │   │
│  │  ├─ Max Encryption Key Size                     │   │
│  │  └─ Initiator Key Distribution / Responder Key  │   │
│  │                                                  │   │
│  │  Pairing Response ◄──────────────────────────   │   │
│  │  (相同字段)                                      │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
│  阶段 2: 密钥生成 (Key Generation)                      │
│  ┌─────────────────────────────────────────────────┐   │
│  │  1. 生成 P-256 公私钥 (双方)                       │   │
│  │     Private Key: 随机 256 bit                    │   │
│  │     Public Key:  椭圆曲线点 (64 byte)            │   │
│  │                                                  │   │
│  │  2. 交换公钥 (Public Key Exchange)               │   │
│  │     Central ──► Public Key ──► Peripheral       │   │
│  │     Peripheral ──► Public Key ──► Central       │   │
│  │                                                  │   │
│  │  3. 计算 DHKey (双方)                            │   │
│  │     DHKey = P256_DHKey(Private, Peer_Public)    │   │
│  │                                                  │   │
│  │  4. 认证计算 (Numeric Comparison 示例)           │   │
│  │     Na = Random (Central)                        │   │
│  │     Nb = Random (Peripheral)                     │   │
│  │     Ca = f4(PK_a, PK_b, Na, 0)                  │   │
│  │     Cb = f4(PK_b, PK_a, Nb, 0)                  │   │
│  │     显示数字 = g(DHKey, Na, Nb) [6 位数字]         │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
│  阶段 3: 密钥分发 (Key Distribution)                    │
│  ┌─────────────────────────────────────────────────┐   │
│  │  主设备可能发送:                                 │   │
│  │  ├─ LTK (Long Term Key) - 加密密钥               │   │
│  │  ├─ IRK (Identity Resolving Key) - 身份解析      │   │
│  │  └─ CSRK (Connection Signature Key) - 签名      │   │
│  │                                                  │   │
│  │  从设备可能发送:                                 │   │
│  │  ├─ LTK                                         │   │
│  │  ├─ IRK                                         │   │
│  │  └─ 地址信息                                    │   │
│  └─────────────────────────────────────────────────┘   │
│                                                         │
└─────────────────────────────────────────────────────────┘

7.4.3 密钥派生函数

BLE 密钥派生函数 (基于 AES-CMAC):

┌─────────────────────────────────────────────────────────┐
│                    f4 函数 (确认值生成)                   │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  f4(U, V, X, Z) = AES-CMAC                              │
│                   (key=X,                              │
│                    message=U || V || Z)                │
│                                                         │
│  输入:                                                  │
│  ├─ U: 256 bit (公钥)                                   │
│  ├─ V: 256 bit (公钥)                                   │
│  ├─ X: 128 bit (随机数或 0)                             │
│  └─ Z: 8 bit (区分用途)                                 │
│                                                         │
│  输出:128 bit (取低 20 bit 显示为 6 位数字)                │
│                                                         │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│                    g 函数 (数字生成)                      │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  g(DHKey, Na, Nb) =                                    │
│     AES-CMAC(key=Na, message=DHKey || Nb)              │
│                                                         │
│  取结果的最低 20 bit:                                   │
│  Numeric Value = result[19:0] mod 1000000              │
│                                                         │
│  显示为 6 位十进制数字                                    │
│                                                         │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│                    f5 函数 (LTK 派生)                     │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  (W, V) = f5(W, N1, N2, A1, A2)                        │
│       = AES-CMAC(key=T, message=...)                   │
│                                                         │
│  其中 T = f4(DHKey, N1, N2, 0x62)                      │
│                                                         │
│  输出:                                                 │
│  ├─ W: 128 bit (LTK - 长期密钥)                        │   │
│  └─ V: 128 bit (MacKey - MAC 密钥)                      │   │
│                                                         │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│                    f6 函数 (校验值)                       │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  Ea = f6(W, N1, N2, A1, A2, Ca)                        │
│       = AES-CMAC(key=W, message=...)                   │
│                                                         │
│  用于验证配对完整性                                     │
│                                                         │
└─────────────────────────────────────────────────────────┘

7.5 安全硬件实现

7.5.1 加密引擎架构

// 蓝牙安全引擎顶层
module bluetooth_security_engine (
    input wire clk,
    input wire reset_n,

    // 控制接口
    input wire [2:0] cipher_mode,  // 0=E0, 1=AES-CCM, 2=AES-ECB
    input wire [1:0] key_size,     // 0=64bit, 1=128bit
    input wire encrypt_enable,
    input wire decrypt_enable,

    // 密钥输入
    input wire [127:0] encryption_key,
    input wire [63:0] bd_addr,
    input wire [26:0] clk_counter,

    // 数据输入
    input wire [127:0] plaintext,
    input wire [127:0] ciphertext_in,
    input wire [103:0] nonce,
    input wire [127:0] aad,

    // 数据输出
    output reg [127:0] ciphertext_out,
    output reg [127:0] plaintext_out,
    output reg [31:0] mic,

    // 状态
    output reg busy,
    output reg done,
    output reg auth_pass
);

// E0 加密引擎
wire e0_done;
wire [7:0] e0_cipher;
e0_cipher_engine u_e0 (
    .clk(clk),
    .reset_n(reset_n),
    .key(encryption_key[127:0]),
    .bd_addr(bd_addr),
    .clk_counter(clk_counter),
    .plaintext(plaintext[7:0]),
    .ciphertext(e0_cipher),
    .enable(encrypt_enable & (cipher_mode == 3'd0)),
    .keystream_valid(e0_done)
);

// AES-128 核心
wire aes_done;
wire [127:0] aes_out;
aes_128_encrypt u_aes (
    .clk(clk),
    .reset_n(reset_n),
    .plaintext(aes_input),
    .key(encryption_key),
    .ciphertext(aes_out),
    .start(aes_start),
    .done(aes_done)
);

// AES-CCM 控制器
wire ccm_done;
wire [127:0] ccm_cipher;
wire [31:0] ccm_mic;
aes_ccm_controller u_ccm (
    .clk(clk),
    .reset_n(reset_n),
    .plaintext(plaintext),
    .nonce(nonce),
    .key(encryption_key),
    .aad(aad),
    .ciphertext(ccm_cipher),
    .mic(ccm_mic),
    .start(encrypt_enable & (cipher_mode == 3'd1)),
    .done(ccm_done),
    .busy(ccm_busy)
);

// 输出选择
always @(posedge clk) begin
    if (cipher_mode == 3'd0) begin
        // E0 模式
        ciphertext_out <= {120'd0, e0_cipher};
        done <= e0_done;
    end else if (cipher_mode == 3'd1) begin
        // AES-CCM 模式
        ciphertext_out <= ccm_cipher;
        mic <= ccm_mic;
        done <= ccm_done;
    end else begin
        // AES-ECB 模式
        ciphertext_out <= aes_out;
        done <= aes_done;
    end
end

endmodule

7.5.2 真随机数发生器

// 真随机数发生器 (TRNG)
module true_random_generator (
    input wire clk,
    input wire reset_n,
    output reg [127:0] random_out,
    input wire generate,
    output reg random_valid
);

// 基于环形振荡器的 TRNG
// 使用多个不同频率的振荡器采样

wire osc_out [0:7];
reg [31:0] entropy_pool;

// 8 个不同频率的环形振荡器
genvar i;
generate
    for (i = 0; i < 8; i = i + 1) begin
        ring_oscillator u_osc (
            .enable(generate),
            .out(osc_out[i]),
            .delay(i * 2)  // 不同延迟
        );
    end
endgenerate

// 熵收集
always @(posedge clk or negedge reset_n) begin
    if (!reset_n) begin
        entropy_pool <= 32'd0;
        random_valid <= 1'b0;
    end else if (generate) begin
        // 采样振荡器输出
        entropy_pool <= {entropy_pool[30:0], ^osc_out};

        // 收集 128 bit 后输出
        if (random_valid) begin
            random_out <= {entropy_pool, /* 更多采样 */};
            random_valid <= 1'b1;
        end
    end
end

// 后处理 (Von Neumann 校正)
function [127:0] von_neumann_correct;
    input [255:0] raw;
    integer i, j;
    begin
        j = 0;
        for (i = 0; i < 255; i = i + 2) begin
            if (raw[i] != raw[i+1]) begin
                von_neumann_correct[j] = raw[i];
                j = j + 1;
            end
        end
    end
endfunction

endmodule

7.6 安全攻击与防护

7.6.1 常见攻击类型

┌─────────────────────────────────────────────────────────┐
│                    蓝牙安全攻击类型                      │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  1. 窃听 (Eavesdropping)                                │
│     ├─ 被动监听无线信号                                 │
│     ├─ 防护:加密 (E0/AES)                              │
│     └─ 风险:E0 已知漏洞,BLE AES 较安全                  │
│                                                         │
│  2. 中间人攻击 (MITM)                                   │
│     ├─ 攻击者插入通信链路                               │
│     ├─ 防护:认证配对 (Numeric Compare/Passkey)         │
│     └─ 风险:Just Works 模式无 MITM 保护                  │
│                                                         │
│  3. 重放攻击 (Replay)                                   │
│     ├─ 重放旧的加密包                                   │
│     ├─ 防护:序列号、nonce 递增                          │
│     └─ 风险:计数器同步问题                             │
│                                                         │
│  4. 暴力破解 (Brute Force)                              │
│     ├─ 尝试所有可能的密钥                               │
│     ├─ 防护:足够长的密钥 (128 bit)                     │
│     └─ 风险:短密钥 (如 64 bit) 易被破解                  │
│                                                         │
│  5. 设备追踪 (Tracking)                                 │
│     ├─ 通过固定地址追踪设备                             │
│     ├─ 防护:隐私特性 (地址轮换)                        │
│     └─ 风险:旧设备不支持                               │
│                                                         │
│  6. KNOB 攻击 (Key Negotiation of Bluetooth)            │
│     ├─ 强制协商弱加密强度                               │
│     ├─ 防护:强制最小密钥长度                           │
│     └─ 风险:实现缺陷                                   │
│                                                         │
└─────────────────────────────────────────────────────────┘

7.6.2 安全最佳实践

┌─────────────────────────────────────────────────────────┐
│                    蓝牙安全最佳实践                      │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  1. 使用 LE Secure Connections (BLE 4.2+)               │
│     └─ 避免使用旧版配对协议                             │
│                                                         │
│  2. 强制 MITM 保护                                       │
│     └─ AuthReq 设置 MITM 位                              │
│                                                         │
│  3. 使用安全 I/O 能力                                     │
│     ├─ Numeric Comparison (双显示)                      │
│     └─ Passkey Entry (显示 + 输入)                       │
│                                                         │
│  4. 强制最小密钥长度                                     │
│     └─ 至少 128 bit (避免 KNOB 攻击)                      │
│                                                         │
│  5. 启用隐私特性                                         │
│     └─ 使用可解析私有地址 (RPA)                          │
│                                                         │
│  6. 安全绑定存储                                         │
│     └─ 加密存储密钥,防止物理提取                        │
│                                                         │
│  7. 定期更新固件                                         │
│     └─ 修复已知漏洞                                     │
│                                                         │
│  8. 实施安全审计                                         │
│     └─ 渗透测试、代码审查                                │
│                                                         │
└─────────────────────────────────────────────────────────┘

7.7 本章小结

本章深入讲解了蓝牙安全加密机制:

  1. E0 流密码:4 个 LFSR、组合器函数、Verilog 实现
  2. AES-CCM:CTR 加密 + CBC-MAC 认证、AES-128 核心
  3. 配对协议:经典蓝牙 SSP、BLE LE Secure Connections
  4. 密钥派生:f4/f5/f6 函数、ECDH 密钥交换
  5. 硬件实现:加密引擎、TRNG、安全架构
  6. 攻击防护:常见攻击类型、最佳实践

下一章将对比分析主流蓝牙芯片架构和特性。


上一章:06-芯片实现详解
下一章:08-主流芯片架构对比