多数工程师第一次接触 AES,是在调用接口时把 \(16\)
字节密钥和明文送进黑盒、拿回一串密文。但把黑盒掀开之后,你会发现
AES(NIST FIPS 197,源自 Joan Daemen 与 Vincent Rijmen
设计的
Rijndael)没有一行靠「魔术常数」凑数:它的全部四步轮函数——SubBytes、ShiftRows、MixColumns、AddRoundKey——本质上只做两件事:在有限域
\(\operatorname{GF}(2^8)\)
上求逆以打碎线性关系,再用线性移位与 MDS
矩阵把单个字节的扰动在两轮之内铺满全部 \(16\) 个字节。
本文跳过 API
封装与基础概念复述,直接用程序员最熟悉的数据结构视角(uint8_t
位向量与 \(4\times 4\)
列主序矩阵)拆穿 AES 每一层变换的代数机制:逐比特手算验证
FIPS 197 标准向量中 0x53 -> 0xCA -> 0xED
的 S-Box 求逆与仿射变换、手算还原 Round 1
MixColumns 的每一步 xtime
异或、解释为什么早期软件的 4 KB T-Table
查表优化会在 L1 缓存上泄露密钥,以及 Intel AES-NI
指令集怎样在硬件门电路里把整轮运算压进常数时间。最后附上一份不靠硬编码
S-Box 表、直接从 \(\mathbb{F}_{2^8}\)
代数定义构造完整 AES-128 的可运行 Python
实现,并逐字节比对 NIST FIPS 197 官方测试向量。
一、AES 竞赛与 Rijndael 的透明设计哲学
1997 年 1 月,NIST 正式发起高级加密标准(AES)全球公开征集,要求分组固定为 \(128\) 比特(以解决 DES/3DES 的 \(64\) 比特分组在 \(2^{32}\) 块后遭遇的生日碰撞问题——后来的 Sweet32 攻击验证了这一隐患),并支持 \(128\)、\(192\) 与 \(256\) 比特三种密钥长度。
在收到的 \(15\) 个候选方案中,进入决赛的五个设计代表了完全不同的工程取向:
| 决赛算法 | 架构范式 | 轮数 | 核心设计特征与落选/胜出原因 |
|---|---|---|---|
| Rijndael(胜出) | SPN | \(10 / 12 / 14\) | 宽轨迹策略给出可证明活跃 S-Box 下界;无秘密常数;软硬件与智能卡性能均衡 |
| Serpent | 32 轮 SPN | \(32\) | 安全余量极大、可位切片(Bitslice)常数时间实现,但标准软件吞吐明显偏慢 |
| Twofish | Feistel | \(16\) | 使用密钥相关的动态 S-Box 增加代数分析难度,但密钥切换开销大、不利紧凑硬件 |
| RC6 | Feistel 变体 | \(20\) | 依赖 \(32\) 位整数乘法与数据相关循环移位,在低端 \(8/16\) 位智能卡上乘法代价过高 |
| MARS | 异构混合 | \(32\) | 混用乘法、S-Box 与旋转导致芯片面积偏大、结构分析复杂度高 |
2001 年 11 月 NIST 发布 FIPS 197 将 Rijndael 定名为 AES(严格来说,Rijndael 原始提案支持 \(128/192/256\) 三种分组长度,而 AES 标准将分组长度锁定为 \(128\) 比特)。Rijndael 胜出的决定性优势不仅是速度,而是可分析的数学透明性:没有一个查表数字或移位常量来历不明,这直接兑现了 Kerckhoffs 原则与公开审查的工程要求。
二、\(\operatorname{GF}(2^8)\) 有限域算术:程序员视角的无进位字节运算
理解 AES 的第一个心智门槛,是不要把一个字节
uint8_t 当成 \(0\text{–}255\)
的普通整数(整数模 \(256\)
里 \(2 \times 128 \equiv
0\),存在大量零因子,根本没有乘法逆元),而要把它看作一个\(8\) 比特系数向量——对应
\(\mathbb{F}_2[x]\)
上次数不超过 \(7\)
的二进制多项式:
\[ a = (b_7 b_6 b_5 b_4 b_3 b_2 b_1 b_0)_2 \;\longleftrightarrow\; b_7 x^7 + b_6 x^6 + b_5 x^5 + b_4 x^4 + b_3 x^3 + b_2 x^2 + b_1 x + b_0 \]
例如字节 \(\mathtt{0x57} = (\mathtt{0101\_0111})_2\) 对应多项式 \(x^6 + x^4 + x^2 + x + 1\)。为了把乘法结果永远关在 \(8\) 比特字节内,FIPS 197 选用固定的 \(8\) 次不可约多项式作为模(对应二进制 \(\mathtt{1\_0001\_1011}_2 = \mathtt{0x11B}\)):
\[ m(x) = x^8 + x^4 + x^3 + x + 1 \]
因 \(\mathbb{F}_2\) 上不存在 \(8\) 次不可约三项式(Swan 定理),\(m(x)\) 是汉明重量最小的一类五项式之一,在硬件上模约简只需要 \(4\) 个异或门即可完成。由此构造出的商环 \(\operatorname{GF}(2^8) = \mathbb{F}_2[x]/(m(x))\) 是一个 \(256\) 阶有限域:每个非零字节都有唯一乘法逆元。
2.1 加法、减法与
xtime 移位乘法
在特征 \(2\)
的域中,\(1 + 1 \equiv 0 \pmod
2\),每一位的加法没有任何进位传播,加法与减法完全等价于按位异或
^。例如 \(\mathtt{0x57} + \mathtt{0x83} =
\mathtt{0x57} \oplus \mathtt{0x83} =
\mathtt{0xD4}\)。
乘法的核心原语是乘以 \(x\)(即字节 \(\mathtt{0x02}\)),AES
规范将其记为
xtime(a):先把字节左移一位;如果乘之前最高位
\(b_7 =
1\)(说明出现了越界的 \(x^8\) 项),由于 \(x^8 \equiv x^4 + x^3 + x + 1
\pmod{m(x)}\),直接异或低 \(8\) 位常数 \(\mathtt{0x1B}\)
即可完成模约简:
\[ \operatorname{xtime}(a) = \begin{cases} (a \ll 1) \bmod 256, & \text{if } b_7 = 0 \\[4pt] \bigl((a \ll 1) \bmod 256\bigr) \oplus \mathtt{0x1B}, & \text{if } b_7 = 1 \end{cases} \]
具体乘法手算:计算 \(\mathtt{0x57} \times
\mathtt{0x83}\)。把 \(\mathtt{0x83} =
(\mathtt{1000\_0011})_2\) 拆成 \(2\) 的幂次和 \(x^7 + x + 1\)(即 \(\mathtt{0x80} \oplus \mathtt{0x02}
\oplus \mathtt{0x01}\)),反复调用 xtime
生成 \(\mathtt{0x57}\) 乘以
\(2^0, 2^1, \dots, 2^7\)
的表:
- \(\mathtt{0x57} \times \mathtt{0x01} = \mathtt{0x57}\)
- \(\mathtt{0x57} \times \mathtt{0x02} = \operatorname{xtime}(\mathtt{0x57}) = \mathtt{0xAE}\)(最高位为 \(0\),不异或)
- \(\mathtt{0x57} \times \mathtt{0x04} = \operatorname{xtime}(\mathtt{0xAE}) = (\mathtt{0x15C} \bmod 256) \oplus \mathtt{0x1B} = \mathtt{0x5C} \oplus \mathtt{0x1B} = \mathtt{0x47}\)
- \(\mathtt{0x57} \times \mathtt{0x08} = \mathtt{0x8E}\);乘以 \(\mathtt{0x10} = \mathtt{0x07}\);乘以 \(\mathtt{0x20} = \mathtt{0x0E}\);乘以 \(\mathtt{0x40} = \mathtt{0x1C}\);乘以 \(\mathtt{0x80} = \mathtt{0x38}\)。
最后挑出 \(\mathtt{0x83}\) 为 \(1\) 的比特位异或相加:
\[ \mathtt{0x57} \times \mathtt{0x83} = \mathtt{0x57} \oplus \mathtt{0xAE} \oplus \mathtt{0x38} = \mathtt{0xC1} \]
2.2 乘法逆元与恒定时间平方链
非零群 \(\operatorname{GF}(2^8)^*\) 是 \(255\) 阶循环群,由拉格朗日定理,任意 \(a \ne 0\) 满足 \(a^{255} = 1\),从而逆元可以直接写成幂次 \(a^{-1} = a^{254}\)。在硬件与位切片软件实现中,求逆既可以通过常数时间反复平方链计算(利用 Itoh-Tsujii 加法链 \(254 = 2(2^7 - 1)\) 仅需 \(4\) 次一般域乘加 \(7\) 次线性平方,详见有限域算术),也可以映射到复合塔域 \(\operatorname{GF}(((2^2)^2)^2)\) 用约 \(180\text{–}200\) 个双输入逻辑门无查表完成(Canright, CHES 2005)。
三、SubBytes:\(\mathbb{F}_{2^8}\) 求逆与仿射变换的逐比特拆解
SubBytes 是 AES
轮函数中唯一的非线性变换,也是混淆(Confusion)的唯一来源。它将
\(16\)
个状态字节独立送入同一张 \(8\times
8\) S-Box。FIPS 197 §5.1.1 定义 S-Box
由两步复合而成:
- 有限域乘法求逆:输入字节 \(a \in \operatorname{GF}(2^8)\) 映射为 \(b = a^{-1} = a^{254}\),特殊规定 \(0 \mapsto 0\)。
- \(\mathbb{F}_2\) 上的仿射变换:将求逆结果字节 \(b = (b_7 b_6 \dots b_0)_2\) 左乘固定 \(8\times 8\) 循环矩阵 \(\mathbf{M}\) 再异或常数向量 \(c = \mathtt{0x63} = (\mathtt{0110\_0011})_2\)(按 LSB 到 MSB 为 \(c_0=1, c_1=1, c_2=0, c_3=0, c_4=0, c_5=1, c_6=1, c_7=0\)):
\[ \begin{pmatrix} y_0 \\ y_1 \\ y_2 \\ y_3 \\ y_4 \\ y_5 \\ y_6 \\ y_7 \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1 \\ 1 & 1 & 0 & 0 & 0 & 1 & 1 & 1 \\ 1 & 1 & 1 & 0 & 0 & 0 & 1 & 1 \\ 1 & 1 & 1 & 1 & 0 & 0 & 0 & 1 \\ 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\ 0 & 1 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 0 & 1 & 1 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1 \end{pmatrix} \begin{pmatrix} b_0 \\ b_1 \\ b_2 \\ b_3 \\ b_4 \\ b_5 \\ b_6 \\ b_7 \end{pmatrix} \oplus \begin{pmatrix} 1 \\ 1 \\ 0 \\ 0 \\ 0 \\ 1 \\ 1 \\ 0 \end{pmatrix} \pmod{2} \]
等价于逐比特位方程:
\[ y_i = b_i \oplus b_{(i+4)\bmod 8} \oplus b_{(i+5)\bmod 8} \oplus b_{(i+6)\bmod 8} \oplus b_{(i+7)\bmod 8} \oplus c_i, \quad i \in \{0, \dots, 7\} \]
3.1 具体字节追踪:为什么 \(S(\mathtt{0x53}) = \mathtt{0xED}\)?
取输入字节 \(a = \mathtt{0x53} = (\mathtt{0101\_0011})_2\)(对应多项式 \(x^6 + x^4 + x + 1\)),手算验证两步映射:
第一步求逆:用 \(\mathbb{F}_2[x]\) 扩展欧几里得算法或求幂 \(a^{254}\),得 \(b = \mathtt{0xCA} = (\mathtt{1100\_1010})_2\)(对应 \(x^7 + x^6 + x^3 + x\))。直接手算核验乘积是否为 \(1\):
\[ \begin{aligned} (x^6 + x^4 + x + 1)(x^7 + x^6 + x^3 + x) &= x^{13} + x^{12} + x^{11} + x^{10} + x^9 + x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x \\ &\equiv 1 \pmod{x^8 + x^4 + x^3 + x + 1} \end{aligned} \]
(读者可用下文 Python 代码中的
gf_mul(0x53, 0xCA) == 0x01一秒验算。)第二步仿射变换:将 \(b = \mathtt{0xCA}\) 的 \(8\) 个比特(\(b_7=1, b_6=1, b_5=0, b_4=0, b_3=1, b_2=0, b_1=1, b_0=0\))逐位代入位方程:
- \(y_0 = b_0 \oplus b_4 \oplus b_5 \oplus b_6 \oplus b_7 \oplus c_0 = 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 1 = 1\)
- \(y_1 = b_1 \oplus b_5 \oplus b_6 \oplus b_7 \oplus b_0 \oplus c_1 = 1 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \oplus 1 = 0\)
- \(y_2 = b_2 \oplus b_6 \oplus b_7 \oplus b_0 \oplus b_1 \oplus c_2 = 0 \oplus 1 \oplus 1 \oplus 0 \oplus 1 \oplus 0 = 1\)
- \(y_3 = b_3 \oplus b_7 \oplus b_0 \oplus b_1 \oplus b_2 \oplus c_3 = 1 \oplus 1 \oplus 0 \oplus 1 \oplus 0 \oplus 0 = 1\)
- \(y_4 = b_4 \oplus b_0 \oplus b_1 \oplus b_2 \oplus b_3 \oplus c_4 = 0 \oplus 0 \oplus 1 \oplus 0 \oplus 1 \oplus 0 = 0\)
- \(y_5 = b_5 \oplus b_1 \oplus b_2 \oplus b_3 \oplus b_4 \oplus c_5 = 0 \oplus 1 \oplus 0 \oplus 1 \oplus 0 \oplus 1 = 1\)
- \(y_6 = b_6 \oplus b_2 \oplus b_3 \oplus b_4 \oplus b_5 \oplus c_6 = 1 \oplus 0 \oplus 1 \oplus 0 \oplus 0 \oplus 1 = 1\)
- \(y_7 = b_7 \oplus b_3 \oplus b_4 \oplus b_5 \oplus b_6 \oplus c_7 = 1 \oplus 1 \oplus 0 \oplus 0 \oplus 1 \oplus 0 = 1\)
拼回字节 \((y_7 \dots y_0)_2 =
(\mathtt{1110\_1101})_2 = \mathtt{0xED}\)——这与 FIPS
197 附录 S-Box 查找表中第 5 行第 3
列的条目 ed 完全一致;同时对于零字节 \(a = \mathtt{0x00}\),由于 \(0 \mapsto 0\) 后再异或 \(\mathtt{0x63}\),立刻得 \(S(\mathtt{0x00}) =
\mathtt{0x63}\),常数 \(\mathtt{0x63}\) 的选取确保整个
\(256\)
字节置换表没有任何不动点 \(S(a)=a\),也没有反不动点 \(S(a) = a \oplus
\mathtt{0xFF}\)。
3.2 为什么非得在 \(a^{-1}\) 后面多套一层仿射变换?
单纯的幂逆映射 \(x \mapsto x^{254}\) 在 \(\operatorname{GF}(2^8)\) 上就已经具备极强的抗统计分析能力:差分均匀度 \(\delta = 4\)(任意非零输入差分对应的特定输出差分至多出现 \(4/256 = 2^{-6}\) 次),非线性度达到 \(112\),最大线性逼近偏差仅有 \(16/256 = 2^{-4}\)(按线性偏差 \(|\Pr - 1/2|\) 口径为 \(2^{-4}\);若按 LAT 绝对计数则为 \(16\),相关系数为 \(2^{-3}\))。
但纯求逆映射的代数表达式过于简单——仅是一个单项式 \(x^{-1}\),在 \(\operatorname{GF}(2^8)\) 上容易遭到插值攻击(Interpolation Attack, Jakobsen & Knudsen, FSE 1997)或连分数逼近。套上这层 \(\mathbb{F}_2\)-线性仿射变换后(注意 \(\mathbb{F}_2\)-线性并不等于 \(\operatorname{GF}(2^8)\)-线性!),S-Box 在扩域 \(\operatorname{GF}(2^8)\) 下的多项式立刻膨胀为拥有 \(255\) 个非零项的高次多项式(代数次数保持在 \(7\),接近 \(8\) 比特极限 \(n-1=7\)),既保留了原有的全部差分均匀度与非线性度指标,又彻底打破了单纯单项式的代数规律性。
四、ShiftRows 与 MixColumns:列间洗牌与 MDS 矩阵
AES 把 \(16\)
字节输入按列主序(Column-Major)填入 \(4\times 4\) 字节矩阵:第 \(c \in \{0,1,2,3\}\) 列第 \(r \in \{0,1,2,3\}\)
行的字节对应线性数组下标 \(r +
4c\)。SubBytes
逐字节独立工作,如果直接进
AddRoundKey,各字节之间一辈子不会互通有无。让扰动铺满全分组的扩散层由
ShiftRows 和 MixColumns
协同完成:
ShiftRows(列间打散):第 \(r\) 行循环左移 \(r\) 个字节——第 \(0\) 行不动,第 \(1\) 行左移 \(1\) 字节,第 \(2\) 行左移 \(2\) 字节,第 \(3\) 行左移 \(3\) 字节。它的全部意义在于把原本同一列的 \(4\) 个字节分别踢进 \(4\) 个不同的列里,防止某几列长期「自循环」。MixColumns(列内混合):把每一列 \(4\) 个字节看作 \(\operatorname{GF}(2^8)[X]/(X^4 + 1)\) 中的三次多项式,乘以固定可逆多项式 \(c(X) = \mathtt{03}X^3 + \mathtt{01}X^2 + \mathtt{01}X + \mathtt{02}\),写成矩阵乘法即:
\[ \begin{pmatrix} s'_{0,c} \\ s'_{1,c} \\ s'_{2,c} \\ s'_{3,c} \end{pmatrix} = \begin{pmatrix} \mathtt{02} & \mathtt{03} & \mathtt{01} & \mathtt{01} \\ \mathtt{01} & \mathtt{02} & \mathtt{03} & \mathtt{01} \\ \mathtt{01} & \mathtt{01} & \mathtt{02} & \mathtt{03} \\ \mathtt{03} & \mathtt{01} & \mathtt{01} & \mathtt{02} \end{pmatrix} \begin{pmatrix} s_{0,c} \\ s_{1,c} \\ s_{2,c} \\ s_{3,c} \end{pmatrix} \]
其中乘以 \(\mathtt{01}\)
为恒等,乘以 \(\mathtt{02}\) 即一次
xtime(a),乘以 \(\mathtt{03} = \mathtt{02} \oplus
\mathtt{01}\) 即 xtime(a) ^ a。
4.1 FIPS
197 Round 1 MixColumns 逐字节手算验证
取 FIPS 197 Appendix B 标准测试用例(明文
3243f6a8885a308d313198a2e0370734,密钥
2b7e151628aed2a6abf7158809cf4f3c):
- 初始白化(明文异或密钥 \(RK_0\))得到
193de3bea0f4e22b9ac68d2ae9f84808。 - 经过第 1 轮
SubBytes得d42711aee0bf98f1b8b45de51e415230。 - 经过第 1 轮
ShiftRows得d4bf5d30e0b452aeb84111f11e2798e5,其第 \(0\) 列恰好为 \((\mathtt{0xD4}, \mathtt{0xBF}, \mathtt{0x5D}, \mathtt{0x30})^\top\)。
先分别算出四个输入字节的 xtime(乘以 \(\mathtt{0x02}\))和乘以 \(\mathtt{0x03}\):
- \(\mathtt{0x02}\cdot\mathtt{0xD4} = (\mathtt{0xA8})\oplus \mathtt{0x1B} = \mathtt{0xB3}\),故 \(\mathtt{0x03}\cdot\mathtt{0xD4} = \mathtt{0xB3}\oplus\mathtt{0xD4} = \mathtt{0x67}\)
- \(\mathtt{0x02}\cdot\mathtt{0xBF} = (\mathtt{0x7E})\oplus \mathtt{0x1B} = \mathtt{0x65}\),故 \(\mathtt{0x03}\cdot\mathtt{0xBF} = \mathtt{0x65}\oplus\mathtt{0xBF} = \mathtt{0xDA}\)
- \(\mathtt{0x02}\cdot\mathtt{0x5D} = \mathtt{0xBA}\)(MSB 为 \(0\)),故 \(\mathtt{0x03}\cdot\mathtt{0x5D} = \mathtt{0xBA}\oplus\mathtt{0x5D} = \mathtt{0xE7}\)
- \(\mathtt{0x02}\cdot\mathtt{0x30} = \mathtt{0x60}\)(MSB 为 \(0\)),故 \(\mathtt{0x03}\cdot\mathtt{0x30} = \mathtt{0x60}\oplus\mathtt{0x30} = \mathtt{0x50}\)
逐行异或求和,直接得出输出第 \(0\) 列:
\[ \begin{aligned} s'_{0,0} &= \mathtt{0xB3} \oplus \mathtt{0xDA} \oplus \mathtt{0x5D} \oplus \mathtt{0x30} = \mathtt{0x04} \\ s'_{1,0} &= \mathtt{0xD4} \oplus \mathtt{0x65} \oplus \mathtt{0xE7} \oplus \mathtt{0x30} = \mathtt{0x66} \\ s'_{2,0} &= \mathtt{0xD4} \oplus \mathtt{0xBF} \oplus \mathtt{0xBA} \oplus \mathtt{0x50} = \mathtt{0x81} \\ s'_{3,0} &= \mathtt{0x67} \oplus \mathtt{0xBF} \oplus \mathtt{0x5D} \oplus \mathtt{0x60} = \mathtt{0xE5} \end{aligned} \]
结果 [0x04, 0x66, 0x81, 0xe5] 与 FIPS 197
Appendix B 第 1 轮 MixColumns
输出列完全吻合。
4.2 MDS 分支数 \(\mathcal{B} = 5\) 与宽轨迹两轮全扩散
MixColumns 所用矩阵是由 \([8, 4, 5]\)
最大距离可分码(Maximum Distance Separable,
MDS)导出的循环矩阵。设 \(w(\mathbf{x})\) 表示 \(4\)
字节列向量中的非零字节数,其分支数(Branch
Number)达到理论最大值 \(4 + 1 =
5\):
\[ \mathcal{B}(\mathbf{M}) = \min_{\Delta \mathbf{s} \ne \mathbf{0}} \Bigl(w(\Delta \mathbf{s}) + w(\mathbf{M}\Delta \mathbf{s})\Bigr) = 5 \]
只要输入列有 \(1\)
个字节非零,输出列必有全部 \(4\) 个字节非零;输入 \(2\) 字节非零,输出至少 \(3\) 字节非零。结合
ShiftRows 把单列的 \(4\) 个活跃字节散落到 \(4\)
个不同列,仅需两轮即可完成全状态 \(16\) 字节扩散;而在
Daemen 与 Rijmen 的宽轨迹策略(Wide Trail
Strategy)下,任意连续 \(4\)
轮非平凡差分或线性路径至少激活 \(5
\times 5 = 25\) 个 S-Box。因单个 S-Box
的最大差分概率为 \(4/256 =
2^{-6}\),连续 \(4\)
轮单路径差分概率上界便被压低到 \((2^{-6})^{25} = 2^{-150} \ll
2^{-128}\)(见概率论与密码分析)。
五、AddRoundKey、密钥扩展与等价解密对称性
AddRoundKey 将 \(128\)
比特轮密钥与状态矩阵逐字节异或。它是 AES
唯一注入密钥比特的步骤。密钥扩展算法(KeyExpansion)以 \(32\)
比特字(Word)为单位从主密钥派生全部轮密钥:设主密钥含 \(N_k \in \{4, 6, 8\}\)
个字,总轮数为 \(N_r \in \{10, 12,
14\}\),需生成共 \(4(N_r +
1)\) 个字 \(W[0 \dots 4N_r
+ 3]\):
常规步(\(i \not\equiv 0 \pmod{N_k}\) 且在 AES-256 中 \(i \not\equiv 4 \pmod 8\)):
\[ W[i] = W[i - N_k] \oplus W[i - 1] \]
周期核心步(\(i \equiv 0 \pmod{N_k}\)):
\[ W[i] = W[i - N_k] \oplus \operatorname{SubWord}\bigl(\operatorname{RotWord}(W[i - 1])\bigr) \oplus \operatorname{Rcon}[i / N_k] \]
AES-256 额外非线性步(\(N_k = 8\) 且 \(i \equiv 4 \pmod 8\)):
\[ W[i] = W[i - 8] \oplus \operatorname{SubWord}(W[i - 1]) \]
其中 RotWord 将 \(4\) 字节字循环左移 \(1\) 字节,SubWord
对每个字节应用 S-Box,\(\operatorname{Rcon}[j] =
(\text{rc}[j], 0, 0, 0)\) 满足 \(\text{rc}[j] = \mathtt{0x02}^{j-1} \in
\operatorname{GF}(2^8)\)(依次为
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36),用于打破轮与轮之间的对称性(防范滑动攻击)。
5.1
为什么最终轮要省略
MixColumns?解密矩阵为何更重?
完整的 AES 加密包含 \(1\)
次初始白化(AddRoundKey(RK[0]))、\(N_r - 1\)
个标准轮(SubBytes -> ShiftRows -> MixColumns -> AddRoundKey)以及第
\(N_r\) 轮最终轮(省略
MixColumns)。省略最终轮的
MixColumns 并非安全增强——因为
MixColumns
是公开无密钥的线性层,任何人都可以自己逆回去——而是为了让标准加密与标准解密的结构更加对称,同时省去最后一轮不必要的线性乘法延迟。
标准解密需逆序调用
InvShiftRows、InvSubBytes、AddRoundKey
和 InvMixColumns。求得 \(\operatorname{GF}(2^8)[X]/(X^4+1)\)
上的乘法逆多项式为 \(c^{-1}(X) =
\mathtt{0B}X^3 + \mathtt{0D}X^2 + \mathtt{09}X +
\mathtt{0E}\),对应的 InvMixColumns
逆矩阵为:
\[ \mathbf{M}^{-1} = \begin{pmatrix} \mathtt{0E} & \mathtt{0B} & \mathtt{0D} & \mathtt{09} \\ \mathtt{09} & \mathtt{0E} & \mathtt{0B} & \mathtt{0D} \\ \mathtt{0D} & \mathtt{09} & \mathtt{0E} & \mathtt{0B} \\ \mathtt{0B} & \mathtt{0D} & \mathtt{09} & \mathtt{0E} \end{pmatrix} \]
相比加密矩阵系数 \(\{\mathtt{01}, \mathtt{02},
\mathtt{03}\}\)(最多只需 \(1\) 次
xtime),解密矩阵系数的二进制重量更高(例如
\(\mathtt{0x0E} = \mathtt{0x08}
\oplus \mathtt{0x04} \oplus \mathtt{0x02}\) 需要连续
\(3\) 次 xtime
与异或),导致纯软件或部分硬件解密链路开销天然重于加密(不过在
CTR/GCM 等现代工作模式下根本无需调用
InvMixColumns)。利用 InvShiftRows
与 InvSubBytes
可交换、线性变换可分配进密钥的性质,FIPS 197 §5.3.5
还定义了等价逆密码(Equivalent Inverse
Cipher):预先对第 \(1
\dots N_r-1\) 轮的轮密钥施以
InvMixColumns,即可让解密主循环在顺序上与加密同构(即先
InvSubBytes -> InvShiftRows -> InvMixColumns -> AddRoundKey)。
六、AES-NI 硬件加速:如何彻底根除 T-Table 缓存时序侧信道?
早期 32 位纯软件实现(如旧版 OpenSSL)为了避免逐字节算
xtime,常把
SubBytes、ShiftRows 和
MixColumns 预计算并打包进 \(4\) 张各 \(256\times 4 = 1\text{ KB}\) 的
T-Table(\(T_0,
T_1, T_2, T_3\)),使单轮每列运算化简为 \(4\) 次查表加 \(4\) 次 32 位异或。
但这把优化利刃隐藏着致命的侧信道伤口:查表下标直接等于秘密内部状态字节(第一轮即等于明文字节与主密钥字节的异或 \(p_i \oplus k_i\))。在现代 CPU 上,若 CPU 缓存行大小为 \(64\) 字节,一张 \(1\text{ KB}\) 的表横跨 \(16\) 条缓存行,每次查表命中的缓存行下标会直接泄漏状态字节的高 \(4\) 比特。Bernstein(2005)与 Osvik、Shamir、Tromer(CT-RSA 2006)利用 Prime+Probe 与 Flush+Reload 证明,同一台主机上的无权限进程只需监测加密时的 L1 缓存冲突延迟,就能在毫秒至秒级提取出完整的 AES 密钥。
2010 年 Intel 在 Westmere 微架构引入 AES-NI 指令集(ARMv8 Cryptography Extensions 引入类似指令),在硅片级从根本上解决了速度与侧信道双重问题:
AESENC xmm1, xmm2/AESENCLAST:把整个标准轮(ShiftRows -> SubBytes -> MixColumns -> AddRoundKey;注意与数学等价顺序SubBytes -> ShiftRows可交换)焊死成单条无分支、无内存访问的组合逻辑指令。在 Intel 现代微架构上单条指令延迟约 \(3\text{–}4\) 个周期、流水线吞吐达到每周期 \(1\) 条(AES-128十轮约 \(40\) 个周期级延迟,多块交错流水时可完全打满)。AESDEC/AESDECLAST/AESIMC:执行等价逆密码解密轮及轮密钥InvMixColumns预处理。AESKEYGENASSIST:加速生成每轮SubWord(RotWord(...)) ^ Rcon密钥字。- 配合
PCLMULQDQ(ARMPMULL):提供 \(64\times 64 \to 128\) 位无进位乘法,加速 AES-GCM 的 \(\operatorname{GF}(2^{128})\) GHASH 标签认证。
对于缺少 AES 硬件指令的老旧或低端嵌入式设备,软件防御必须采用位切片(Bitslicing)(把 \(8\) 个或 \(16\) 个块的相同比特位打包进寄存器,以纯布尔门逻辑模拟复合塔域求逆,完全消除秘密索引查表),或直接选用天然不含查找表的 ARX 密码 ChaCha20-Poly1305(相关比较见国密 SM4 vs AES)。
七、AES 的已知攻击版图与量子余量
公开标准化二十余年来,全轮 AES 至今未被任何实用攻击攻破,已知主要理论界限如下:
| 攻击类别 | 目标版本与代表文献 | 复杂度 / 攻击前提 | 实际工程影响 |
|---|---|---|---|
| Biclique 单密钥攻击 | Bogdanov, Khovratovich & Rechberger (ASIACRYPT 2011) | AES-128: \(2^{126.1}\);AES-192: \(2^{189.7}\);AES-256: \(2^{254.4}\) | 仅比暴力穷举快约 \(3\text{–}5\) 倍,毫无实际可利用性 |
| 相关密钥攻击 | Biryukov & Khovratovich (ASIACRYPT 2009) | 全轮 AES-256 时间与数据复杂度 \(2^{99.5}\) | 要求敌手能在满足特定 XOR 差分的多个相关密钥下请求加密;暴露 AES-256 密钥扩展相对较弱 |
| Square / 积分攻击 | Daemen-Knudsen-Rijmen (FSE 1997); Ferguson 等 (2000) | 穿透降轮 AES 至约 \(6\text{–}7\) 轮(\(2^{128}\) 内) | 全轮 \(10/12/14\) 轮拥有 \(3\text{–}7\) 轮以上余量,完全免疫 |
| XSL 代数攻击 | Courtois & Pieprzyk (ASIACRYPT 2002) | 试图将 AES-128 写成约 \(8000\) 元二次稀疏方程组求解 | 后续独立验证发现线性化独立方程不足以支撑亚指数复杂度,未成立 |
| Grover 量子搜索 | Grover (STOC 1996); Grassl 等 (PQCrypto 2016) | 查询步数 \(O(2^{k/2})\)(AES-128 降为 \(2^{64}\) 步量子门操作) | 需串行约 \(2^{64}\) 次量子电路,受限于深度但仍建议长期数据切向 AES-256 |
八、完整可运行代码验证:从 \(\mathbb{F}_{2^8}\) 构造 AES-128 并核验 FIPS 197 向量
8.1 纯 Python 代数实现(零硬编码 S-Box 表,通过 FIPS 197 Appendix B & C.1)
下面这段纯标准库 Python 代码从零构建
xtime、gf_mul、gf_inv(\(a^{254}\))与仿射矩阵生成完整
\(256\) 字节
S-Box,随后逐轮完成
KeyExpansion、SubBytes、ShiftRows
与 MixColumns,并自动断言对齐 NIST FIPS 197
官方测试向量及前文每一步手算中间值:
#!/usr/bin/env python3
"""从 GF(2^8) 代数定义完整构造 AES-128,并核验 FIPS 197 Appendix B / C.1 测试向量。"""
RCON = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36]
def xtime(a: int) -> int:
return ((a << 1) ^ 0x1B) & 0xFF if (a & 0x80) else (a << 1) & 0xFF
def gf_mul(a: int, b: int) -> int:
res = 0
for _ in range(8):
if b & 1:
res ^= a
a = xtime(a)
b >>= 1
return res
def gf_inv(a: int) -> int:
if a == 0:
return 0
res, base, exp = 1, a, 254
while exp:
if exp & 1:
res = gf_mul(res, base)
base = gf_mul(base, base)
exp >>= 1
return res
def affine_transform(b: int) -> int:
out = 0
for i in range(8):
bit = (
(b >> i)
^ (b >> ((i + 4) % 8))
^ (b >> ((i + 5) % 8))
^ (b >> ((i + 6) % 8))
^ (b >> ((i + 7) % 8))
) & 1
out |= bit << i
return out ^ 0x63
SBOX = [affine_transform(gf_inv(i)) for i in range(256)]
def key_expansion_128(key: bytes) -> list[list[int]]:
w = [list(key[4 * i : 4 * i + 4]) for i in range(4)]
for i in range(4, 44):
temp = list(w[i - 1])
if i % 4 == 0:
temp = temp[1:] + temp[:1]
temp = [SBOX[b] for b in temp]
temp[0] ^= RCON[i // 4 - 1]
w.append([w[i - 4][j] ^ temp[j] for j in range(4)])
return [sum(w[4 * r : 4 * r + 4], []) for r in range(11)]
def shift_rows(s: list[int]) -> list[int]:
out = [0] * 16
for r in range(4):
for c in range(4):
out[r + 4 * c] = s[r + 4 * ((c + r) % 4)]
return out
def mix_columns(s: list[int]) -> list[int]:
out = [0] * 16
for c in range(4):
a0, a1, a2, a3 = s[4 * c : 4 * c + 4]
out[4 * c + 0] = gf_mul(2, a0) ^ gf_mul(3, a1) ^ a2 ^ a3
out[4 * c + 1] = a0 ^ gf_mul(2, a1) ^ gf_mul(3, a2) ^ a3
out[4 * c + 2] = a0 ^ a1 ^ gf_mul(2, a2) ^ gf_mul(3, a3)
out[4 * c + 3] = gf_mul(3, a0) ^ a1 ^ a2 ^ gf_mul(2, a3)
return out
def aes128_encrypt_block(pt: bytes, key: bytes) -> bytes:
rks = key_expansion_128(key)
s = [pt[i] ^ rks[0][i] for i in range(16)]
for r in range(1, 10):
s = [SBOX[b] for b in s]
s = shift_rows(s)
s = mix_columns(s)
s = [s[i] ^ rks[r][i] for i in range(16)]
s = [SBOX[b] for b in s]
s = shift_rows(s)
s = [s[i] ^ rks[10][i] for i in range(16)]
return bytes(s)
if __name__ == "__main__":
assert gf_mul(0x57, 0x83) == 0xC1
assert gf_inv(0x53) == 0xCA and gf_mul(0x53, 0xCA) == 0x01
assert SBOX[0x53] == 0xED and SBOX[0x00] == 0x63
assert mix_columns([0xD4, 0xBF, 0x5D, 0x30] + [0] * 12)[:4] == [0x04, 0x66, 0x81, 0xE5]
# FIPS 197 Appendix B
ct_b = aes128_encrypt_block(
bytes.fromhex("3243f6a8885a308d313198a2e0370734"),
bytes.fromhex("2b7e151628aed2a6abf7158809cf4f3c"),
)
assert ct_b.hex() == "3925841d02dc09fbdc118597196a0b32"
# FIPS 197 Appendix C.1
ct_c = aes128_encrypt_block(
bytes.fromhex("00112233445566778899aabbccddeeff"),
bytes.fromhex("000102030405060708090a0b0c0d0e0f"),
)
assert ct_c.hex() == "69c4e0d86a7b0430d8cdb78070b4c55a"
print(f"FIPS 197 App B: {ct_b.hex()}")
print(f"FIPS 197 App C.1: {ct_c.hex()}")在本地 Python 3.12 实测输出(rc=0):
FIPS 197 App B: 3925841d02dc09fbdc118597196a0b32
FIPS 197 App C.1: 69c4e0d86a7b0430d8cdb78070b4c55a
8.2 C 语言参考核心算子与 AES-NI 内联写法
以下代码实现了可独立编译运行的 \(\operatorname{GF}(2^8)\)
求逆、S-Box 生成、单列 MixColumns 与
xtime(可通过
gcc -O2 -Wall -Wextra 直接编译校验):
#include <stdint.h>
#include <stdio.h>
/* xtime: 在 GF(2^8) 中乘以 x (0x02),不可约多项式 m(x) = x^8+x^4+x^3+x+1 (0x11B) */
static uint8_t xtime(uint8_t a)
{
return (uint8_t)((a << 1) ^ ((a >> 7) * 0x1Bu));
}
/* GF(2^8) 乘法(无分支掩码版本避免单轮分支泄漏) */
static uint8_t gf_mul(uint8_t a, uint8_t b)
{
uint8_t result = 0;
for (int i = 0; i < 8; i++) {
uint8_t mask = (uint8_t)(-(b & 1u));
result ^= (uint8_t)(a & mask);
a = xtime(a);
b >>= 1;
}
return result;
}
/* GF(2^8) 求逆: a^{-1} = a^{254} (0 -> 0) */
static uint8_t gf_inv(uint8_t a)
{
uint8_t a2 = gf_mul(a, a);
uint8_t a4 = gf_mul(a2, a2);
uint8_t a8 = gf_mul(a4, a4);
uint8_t a16 = gf_mul(a8, a8);
uint8_t a32 = gf_mul(a16, a16);
uint8_t a64 = gf_mul(a32, a32);
uint8_t a128 = gf_mul(a64, a64);
return gf_mul(gf_mul(gf_mul(a128, a64), gf_mul(a32, a16)),
gf_mul(gf_mul(a8, a4), a2));
}
static uint8_t affine(uint8_t b)
{
uint8_t result = 0;
for (int i = 0; i < 8; i++) {
int bit = ((b >> i) ^ (b >> ((i + 4) % 8)) ^ (b >> ((i + 5) % 8))
^ (b >> ((i + 6) % 8)) ^ (b >> ((i + 7) % 8))) & 1;
result |= (uint8_t)(bit << i);
}
return (uint8_t)(result ^ 0x63u);
}
static void mix_column(uint8_t col[4])
{
uint8_t a[4], b[4];
for (int i = 0; i < 4; i++) {
a[i] = col[i];
b[i] = xtime(col[i]);
}
col[0] = (uint8_t)(b[0] ^ b[1] ^ a[1] ^ a[2] ^ a[3]);
col[1] = (uint8_t)(a[0] ^ b[1] ^ b[2] ^ a[2] ^ a[3]);
col[2] = (uint8_t)(a[0] ^ a[1] ^ b[2] ^ b[3] ^ a[3]);
col[3] = (uint8_t)(b[0] ^ a[0] ^ a[1] ^ a[2] ^ b[3]);
}
int main(void)
{
uint8_t s53 = affine(gf_inv(0x53u));
uint8_t col[4] = {0xD4u, 0xBFu, 0x5Du, 0x30u};
mix_column(col);
printf("S(0x53) = 0x%02x, MixColumns([D4,BF,5D,30]) = [%02x,%02x,%02x,%02x]\n",
s53, col[0], col[1], col[2], col[3]);
return (s53 == 0xEDu && col[0] == 0x04u && col[1] == 0x66u
&& col[2] == 0x81u && col[3] == 0xE5u) ? 0 : 1;
}而在支持 Intel AES-NI 的 x86-64 处理器上(编译时附带
-maes),单分组 10
轮加密的核心内联片段仅需十余行:
#include <wmmintrin.h>
__m128i aes128_encrypt_block(__m128i plaintext, const __m128i round_keys[11])
{
__m128i state = _mm_xor_si128(plaintext, round_keys[0]);
for (int i = 1; i <= 9; i++) {
state = _mm_aesenc_si128(state, round_keys[i]);
}
return _mm_aesenclast_si128(state, round_keys[10]);
}九、开放问题与密码设计权衡
AES 已稳居通用对称加密核心地位超过二十五年,但在以下三个方向上仍存在活跃的理论与工程争论:
- 代数透明度与 MPC / FHE 友好密码的分叉:AES 选择 \(\mathbb{F}_{2^8}\) 求逆换来了极小的硬件面积与宽轨迹安全界,但它的代数次数高达 \(7\),且跨位非线性度在算术零知识证明(zk-SNARK)和全同态加密(FHE)电路里代价极其高昂。近年的设计分叉出 LowMC、Poseidon、Anemoi 与 Rescue 等「算术化友好」原语——代价是放弃成熟几十年了的 AES-NI 硬件生态,转而在较低轮数下承担更大的代数攻击审查风险。
- 侧信道掩码(Masking)与 S-Box 代数次数的冲突:在无 AES-NI 的智能卡或物联网安全芯片上防护差分功耗分析(DPA),往往需要做高阶布尔掩码或阈值实现(Threshold Implementation)。由于 AES S-Box 代数次数高达 \(7\),做高阶掩码所需的随机刷新比特与非线性与门(AND)呈平方级暴涨;这正是 Ascon(NIST SP 800-232 轻量级标准)选择代数次数仅 \(2\) 的 \(5\) 比特 \(\chi\) 映射的核心工程动机。
- 量子 Grovers 模型下的并行度与
MAXDEPTH争议:尽管 Grover 算法理论上把 AES-128 暴力搜索压到 \(O(2^{64})\) 步,但量子并行化只会带来平方根级别的减速——\(K\) 台量子计算机并行只能把深度降到 \(O(2^{64}/\sqrt{K})\)。因此 NIST PQC 评估中引入了基于最大量子门深度MAXDEPTH(如 \(2^{40}\)、\(2^{64}\)、\(2^{96}\))的成本模型,在评估 AES-128/192/256 的物理 T-gate 与量子比特-深度乘积上界方面仍持续演进。
十、参考资料
规范与权威专著(A 级)
- NIST FIPS 197, Advanced Encryption Standard (AES), 2001(2023 年 5 月修订版;含 Appendix A 密钥扩展向量、Appendix B 逐轮状态向量及 Appendix C.1 加密测试向量)。
- Daemen, J., & Rijmen, V., The Design of Rijndael: AES — The Advanced Encryption Standard, Springer, 2002(Wide Trail Strategy 证明、分支数下界与 S-Box 幂逆构造论证)。
核心密码分析与硬件实现论文(A 级)
- Daemen, J., Knudsen, L. R., & Rijmen, V., The Block Cipher Square, FSE 1997(Square / 积分攻击原型)。
- Jakobsen, T., & Knudsen, L. R., The Interpolation Attack on Block Ciphers, FSE 1997(单幂映射插入攻击,解释为何需要仿射层)。
- Courtois, N. T., & Pieprzyk, J., Cryptanalysis of Block Ciphers with Overdefined Systems of Equations, ASIACRYPT 2002(XSL 代数攻击路线及其争议)。
- Canright, D., A Very Compact S-Box for AES, CHES 2005(复合塔域 \(\operatorname{GF}(((2^2)^2)^2)\) 极简无查表门电路)。
- Osvik, D. A., Shamir, A., & Tromer, E., Cache Attacks and Countermeasures: The Case of AES, CT-RSA 2006; Bernstein, D. J., Cache-timing attacks on AES, 2005(T-Table 缓存侧信道密钥恢复)。
- Biryukov, A., & Khovratovich, D., Related-Key Cryptanalysis of the Full AES-192 and AES-256, ASIACRYPT 2009(全轮 AES-256 相关密钥攻击 \(2^{99.5}\))。
- Gueron, S., Intel Advanced Encryption Standard (AES) New Instructions Set, Intel White Paper, 2010(AES-NI 指令语义与流水线延迟特性)。
- Bogdanov, A., Khovratovich, D., & Rechberger, C., Biclique Cryptanalysis of the Full AES, ASIACRYPT 2011(全轮 AES-128 单密钥最优界 \(2^{126.1}\))。
- Grassl, M., Langenberg, B., Roetteler, M., & Steinwandt, R., Applying Grover’s Algorithm to AES: Quantum Resource Estimates, PQCrypto 2016(AES 量子门与比特资源精确估算)。
密码学百科系列 · 第 07 篇
文章导航
读完这篇,下一步读什么
优先读同系列或同问题的下一篇,把单篇消费变成主题集群。
【密码学百科】分组密码工作模式全览:ECB/CBC/CTR/OFB/CFB
拆解 ECB/CBC/CTR/OFB/CFB 五种工作模式:用 AES-128 字节级实例实证 ECB 碰撞、BEAST、Padding Oracle 与 CTR Nonce 重用崩溃,厘清 IND-CPA 归约与 AEAD 权衡。
【密码学百科】分组密码原理:Feistel 网络与 SPN 结构
拆解 Feistel 与 SPN 两大分组密码范式:从 PRP 定义、Luby-Rackoff 定理、16 比特 SPN 逐比特追踪、C 雪崩实测到差分/线性分析与 S 盒非线性的数学界。
【密码学百科】Kerckhoffs 原则与现代密码设计哲学
以 1883 年 Kerckhoffs 六原则为起点,结合 Wide Trail 安全余量推导、Nonce 生日碰撞计算、NaCl/MRAE 误用抵抗设计与形式化验证,拆解现代密码设计哲学。
【密码学百科】有限域算术:GF(2^n) 运算与在 AES/ECC 中的应用
拆解多项式基与复合塔域 GF(((2^2)^2)^2) 的 AES S-Box 求逆、GHASH 比特反转陷阱、Poly1305 伪梅森素数域、NTT 友好素数及正规基 ONB 边界。