编程 HKDF-SHA256 拿来哈希口令:判据一是输入熵不够,判据二是它算得太快

2026-11-24 00:04:18

HKDF-SHA256 拿来哈希口令:判据一是输入熵不够,判据二是它算得太快

结论:HKDF 是密钥派生函数,不是口令哈希。RFC 5869 §4 明确说,HKDF 的 extract 步骤只能「集中」已有熵,不能「放大」熵;并且规范里没有故意变慢的机制。因此,凡是低熵输入(口令),或者需要靠迭代来抵抗爆破的场景,都不该把输入交给 HKDF-SHA256。

判据一:输入是不是密码学强度的密钥。不是,就不能靠 HKDF 兜底。
判据二:是否需要用故意变慢来承担抗爆破成本。需要,HKDF 里没有这个开关。

参考:RFC 5869

HKDF 的两个步骤和一条硬上限

§2.2 Extract:PRK = HMAC-Hash(salt, IKM)。salt 可选,不提供时设为 HashLen 个 0 字节;PRK 长度固定为 HashLen 字节。这里的关系容易记反——IKM 是 HMAC 的 message,salt 才是 HMAC 的 key。

§2.3 Expand:N = ceil(L/HashLen),T(0) = 空串,T(i) = HMAC-Hash(PRK, T(i-1) | info | 0x0i),OKM 取 T 的前 L 字节。计数器只有一个字节(0x01..0xFF),所以 L <= 255*HashLen。SHA-256 下即 255*32 = 8160 字节。

§3.3 讲了何时可跳过 extract:IKM 本身已是密码学强度的密钥(如 TLS RSA 的 premaster secret)时,可以直接拿 IKM 做 expand 的 HMAC key;但 IKM 是 Diffie-Hellman 值 g^{xy} 时,SHOULD NOT 跳过。

§4 是这篇要说的重点,原文照录:

the derivation of cryptographic keys from a source of low entropy, such as a user's password. The extract step in HKDF can concentrate existing entropy but cannot amplify entropy. In the case of password-based KDFs, a main goal is to slow down dictionary attacks using two ingredients: a salt value, and the intentional slowing of the key derivation computation. HKDF naturally accommodates the use of salt; however, a slowing down mechanism is not part of this specification. Applications interested in a password-based KDF should consider whether, for example, [PKCS5] meets their needs better than HKDF.

salt 这一半 HKDF 满足,变慢这一半不满足。

实测数字:26.8 微秒 vs 0.600 秒

Python 3、单核,按 RFC 算法手写实现(含 Python 解释器开销):

  • 附录 A.1 的 SHA-256 测试向量完全对得上,可作互通校验:IKM=0x0b*22、salt=000102030405060708090a0b0c(13 字节)、info=f0f1f2f3f4f5f6f7f8f9(10 字节)、L=42;PRK=077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5;OKM=3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865。
  • L = 255*32+1 = 8161 被拒绝,报错指出 255*32=8160 是上限。
  • 口令 b"correct horse battery staple" 作 IKM、info=b"login"、L=32,派生 1000 次耗时 0.0268s,即约 26.8 微秒/次(约 3.7 万次/秒/核)。
  • hashlib.pbkdf2_hmac("sha256", 同一口令, 16 字节随机 salt, 600000) 单次 0.600s(约 1.7 次/秒/核)。

同一台机器、同一口令,两者差约四个数量级。HKDF 快是设计目标(它派生的是密钥,不是口令),PBKDF2 慢也是设计目标。把口令塞进快的那条路径,等于把爆破成本压到 3.7 万次/秒/核。

错误的用法

口令是低熵输入,这里既没有变慢机制,也没有「集中熵」可集中:

// 不要这样:口令当 IKM,HKDF-SHA256 不做抗爆破
okm, _ := hkdf.Key(sha256.New, []byte(password), nil, "login", 32)

PHP 同理:

// 不要这样:hash_hkdf 不是 password_hash
$hash = hash_hkdf('sha256', $password, 32, 'login');

Go 1.24 的正确形态:派生密钥,不是哈希口令

Go 1.24 起标准库新增 crypto/hkdf,实现 RFC 5869,代码来自 golang.org/x/crypto/hkdf:

func Extract[H hash.Hash](h func() H, secret, salt []byte) ([]byte, error)
func Expand[H hash.Hash](h func() H, pseudorandomKey []byte, info string, keyLength int) ([]byte, error) // 跳过 extract
func Key[Hash hash.Hash](h func() Hash, secret, salt []byte, info string, keyLength int) ([]byte, error)  // extract+expand 一次完成

文档写明:Extract 只在需要复用 PRK、用不同 info 做多次 Expand 时使用;多数场景(包括派生多把钥匙)直接用 Key。salt 和 info 都可以是 nil;info 是 string,salt 是 []byte——提案 golang/go#61477 里 FiloSottile 给的理由是 info 通常是用途标签、salt 常被误解,类型分开能减少传反。hash 参数要传构造函数 sha256.New,不是 sha256.New()。

派生 enc/mac 两把钥匙:

encKey, err := hkdf.Key(sha256.New, secret, salt, "app:v1:enc", 32)
macKey, err := hkdf.Key(sha256.New, secret, salt, "app:v1:mac", 32)

Go 1.24 Release Notes 同时新增 crypto/hkdf、crypto/pbkdf2、crypto/sha3。其中 crypto/pbkdf2 实现 RFC 8018,是标准库里唯一基于口令的 KDF——口令该走这条路,不该走 hkdf。

PHP hash_hkdf 的参数顺序

hash_hkdf:hash_hkdf(string $algo, string $key, int $length = 0, string $info = "", string $salt = ""): string,PHP 7.1.2+ 内置。$key 是 IKM,$length 为 0 表示输出完整 HashLen 长度。参数是 key, length, info, salt,与 RFC 里 salt 在前、IKM 在后的叙述顺序相反,容易写反:

$enc = hash_hkdf('sha256', $ikm, 32, 'app:v1:enc', $salt);
$mac = hash_hkdf('sha256', $ikm, 32, 'app:v1:mac', $salt);

这里的 $ikm 是密钥材料,不是用户口令。

对照参考

OWASP Password Storage Cheat Sheet 当前建议(非本站实测):PBKDF2-HMAC-SHA256 迭代数 600,000;Argon2id 最低 m=19456 KiB(19 MiB)、t=2、p=1,或 m=47104 KiB(46 MiB)、t=1、p=1;scrypt N=2^17, r=8, p=1。

自查判据清单

  1. 输入是口令或任何低熵字符串吗?是 → 不调 HKDF,改用 crypto/pbkdf2、Argon2id、scrypt 或 password_hash。
  2. 单次派生成本能到几百毫秒量级吗?HKDF-SHA256 实测约 26.8 微秒/次且无迭代参数,答「不能」即判不合格。
  3. 输出长度 L ≤ 255*HashLen(SHA-256 为 8160)吗?超出就分多次调用,不要指望一次 Expand。
  4. Go 里 hash 参数传的是 sha256.New 而不是 sha256.New()?派生多把钥匙用的是 Key 而不是先 Extract?
  5. PHP 调用里第 2 个参数是 IKM、第 5 个是 salt,顺序与 RFC 相反,确认没写反?
复制全文 生成海报 RFC5869 HKDF 密钥派生 密码学 Go PHP

推荐文章

程序员茄子在线接单