跳到主要内容

5. HMAC 和伪随机函数 (HMAC and the Pseudorandom Function)

TLS record layer 使用 keyed Message Authentication Code (MAC) 保护 message integrity. 本文档定义的 cipher suite 使用称为 HMAC 的构造, 见 [HMAC], 它基于 hash function. 如果需要, 其他 cipher suite 可以定义自己的 MAC 构造.

此外, 为了 key generation 或 validation, 还需要一种把 secret 扩展为数据块的构造. 该 pseudorandom function (PRF) 以 secret, seed 和标识 label 作为输入, 生成任意长度输出.

本节定义一个基于 HMAC 的 PRF. 当协商 TLS 1.2 时, 本文档以及本文档之前发布的 TLS 文档中定义的所有 cipher suite 都使用带 SHA-256 hash function 的 PRF. 新 cipher suite 必须明确指定 PRF, 且通常应该使用带 SHA-256 或更强标准 hash function 的 TLS PRF.

首先定义数据扩展函数 P_hash(secret, data), 它使用单个 hash function 将 secret 和 seed 扩展为任意数量输出:

P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
HMAC_hash(secret, A(2) + seed) +
HMAC_hash(secret, A(3) + seed) + ...

其中 + 表示串接.

A() 定义如下:

A(0) = seed
A(i) = HMAC_hash(secret, A(i-1))

P_hash 可按需要迭代, 以产生所需数量的数据. 例如, 如果使用 P_SHA256 创建 80 byte 数据, 需要迭代三次直到 A(3), 产生 96 byte 输出, 然后丢弃最后一次迭代的最后 16 byte, 留下 80 byte 输出.

TLS 的 PRF 通过将 P_hash 应用于 secret 创建:

PRF(secret, label, seed) = P_<hash>(secret, label + seed)

label 是 ASCII string. 它应以给定的精确形式包含, 不带 length byte 或尾随 null character.