Berlin
发布于 2025-04-07 / 62 阅读
0
0

哈希消息认证码(HMAC)计算原理|The computational principles of the Hash-based Message Authentication Code (HMAC)

一、什么是HMAC | What is HMAC

HMAC(基于哈希的消息认证码)是一种基于密码学哈希函数与密钥结合而构造的消息认证机制,常用于验证消息的完整性与认证。

HMAC (Hash-based Message Authentication Code) is a mechanism that combines a cryptographic hash function with a secret key to verify the integrity and authenticity of a message.

它解决了单纯哈希函数无法使用密钥的问题,同时兼具效率和安全性。

It addresses the limitation that hash functions alone do not rely on a secret key, while maintaining both efficiency and security.


二、HMAC的设计动机 | Motivation for HMAC

  • 哈希函数(如SHA-1)在软件中通常比传统加密算法(如DES)执行得更快
    Cryptographic hash functions like SHA-1 generally execute faster than traditional encryption algorithms such as DES

  • 但SHA-1等哈希函数不能直接作为MAC使用,因为它们不依赖密钥
    However, hash functions like SHA-1 cannot be directly used as MACs since they do not involve a secret key

  • 因此,研究者提出将密钥引入哈希算法中,最成功的方案就是HMAC
    Hence, proposals emerged to incorporate a secret key into hashing. The most successful approach is HMAC


三、HMAC的设计目标 | Design Objectives of HMAC

  1. 使用现有哈希函数,无需修改算法本身
    To use existing hash functions without modification

  2. 支持在需要时替换为更快或更安全的哈希函数
    To allow easy replacement of the underlying hash function if necessary

  3. 保持哈希函数的原始性能,不显著降低效率
    To preserve the original performance of the hash function

  4. 简单地处理和使用密钥
    To handle keys in a simple manner

  5. 在理论上有良好的密码学分析基础
    To provide strong theoretical security based on the embedded hash function


四、HMAC结构 | Structure of HMAC

HMAC通过对密钥进行两次填充(内填充ipad和外填充opad)并结合消息执行两次哈希,来实现安全的认证机制。

HMAC computes a MAC by performing two rounds of hashing using a padded key combined with the message.

结构如下:

  • 内层哈希:H((K ⊕ ipad) || M)

  • 外层哈希:H((K ⊕ opad) || 内层哈希结果)

最终得到的结果就是:
The final result is:

HMAC(K,M)=H((K⊕opad) ∣∣ H((K⊕ipad) ∣∣ M))

其中:

  • ⊕:按位异或(XOR)

  • ∣∣:连接操作


五、HMAC算法步骤 | HMAC Algorithm Steps

1. 密钥长度调整(Key Size Adjustments)

  • 如果密钥 K 长于区块大小 b,先通过哈希函数压缩为 b 位
    If the key is longer than block size b, hash it to shorten

  • 如果短于 b,则在右侧补零以对齐
    If shorter, pad with zeros to reach b bits

2. 填充值常量(Padding Constants)

  • ipad0x36,内层填充值

  • opad0x5C,外层填充值

这些常量与密钥异或后用于保护哈希的结构。
These constants are XORed with the key to pseudorandomly modify it.

3. 双重哈希(Double Hashing)

  • 第一轮:计算 H(K⊕ipad ∣∣ M)
    Inner hash over modified key and message

  • 第二轮:将第一轮结果作为输入,计算 H(K⊕opad ∣∣ InnerHash)
    Outer hash adds another layer of security


六、简化示例练习 | Simplified Practice Example

设定条件:

  1. 哈希函数:统计输入中1的个数并模256
    H(X)=sum of bits in Xmod  256

(这个的H(X)是密钥K扔进H(X)计算得到计算后的K(值和位数),这个H(X)公式由题目给出)

  1. 密钥 K=0b10101010(8位)

  2. 消息 M=0b11110000(8位)

0b二进制数(binary number) 的前缀

  1. 区块大小 b=8

  2. 填充值:

    • ipad = 0b00110110 (0x36)

    • opad = 0b01011100 (0x5C)

计算步骤:

  • K ⊕ ipad = 0b10101010 ⊕ 0b00110110 = 0b10011100

  • 内部消息拼接:10011100 || 11110000

  • 哈希(统计1的个数):4 (来自10011100) + 4 (来自11110000) = 8

(看条件使用哈希函数H(X))

  • 内部哈希结果:8

然后继续外层哈希:

  • K ⊕ opad = 0b10101010 ⊕ 0b01011100 = 0b11110110

  • 外部消息拼接:11110110 || 00001000(因为内部哈希结果是8)

  • 总1的个数是7(看条件使用哈希函数H(X))

最终HMAC为:7
Final HMAC output:

HMAC(K,M)=7

第二个例子:H(x)=((x>>4)⊕ x) and 0FF

(H代表的是十六进制)


七、总结 | Summary

HMAC是一种安全、灵活、效率高的认证机制,其主要特性包括:

特性

描述

安全性

基于密钥的双重哈希设计

高效性

可以使用已有高性能哈希函数

可替换性

支持更换底层哈希算法(如SHA-1、SHA-256)

广泛应用

常用于TLS、IPSec、JWT等协议与框架

HMAC provides a well-balanced tradeoff between security and performance, and is widely adopted in secure communications and data integrity verification.


评论