KNCn23/aes-sca-sim

GitHub: KNCn23/aes-sca-sim

该工具通过合成功率轨迹与相关性功耗分析(CPA),模拟从 AES-128 加密的物理泄漏中逐字节恢复密钥的侧信道攻击过程。

Stars: 0 | Forks: 0

aes-sca-sim — 合成功率轨迹与相关性功耗分析,逐字节恢复 AES-128 密钥 [![主要语言](https://img.shields.io/github/languages/top/KNCn23/aes-sca-sim?style=flat-square&labelColor=0a0e14&color=58e6d9)](https://github.com/KNCn23/aes-sca-sim) ![攻击](https://img.shields.io/badge/attack-Correlation%20Power%20Analysis-7c8cff?style=flat-square&labelColor=0a0e14) [![个人主页](https://img.shields.io/badge/portfolio-kncn23.github.io-58e6d9?style=flat-square&labelColor=0a0e14)](https://kncn23.github.io)
这是一个独立的演示,展示了一种真实的密码分析攻击——**相关性功耗分析 (CPA)**——如何从泄漏的侧信道信息中提取隐藏的 AES-128 密钥,而**无需攻击密码算法本身的数学原理**。这对于嵌入式安全教育、侧信道研究以及理解为什么常量时间实现(constant-time implementations)非常重要非常有用。 ## 它的功能 ``` ┌─────────────────────────────────────────────────────────────────┐ │ trace-gen (C) │ │ • Encrypts N random plaintexts with the secret key │ │ • Simulates power traces leaking HW(SBOX[P ⊕ K]) + Gaussian │ │ noise — same leakage model used by real CMOS hardware │ │ • Writes traces.bin + plaintexts.csv │ └─────────────────────────────────────────────────────────────────┘ ▼ ┌─────────────────────────────────────────────────────────────────┐ │ cpa_attack.py (Python) │ │ • For each of 16 key bytes, tries all 256 candidate values │ │ • For each candidate, computes Hamming-weight hypothesis │ │ • Correlates hypothesis with the simulated traces │ │ • The maximum-correlation candidate IS the secret key byte │ └─────────────────────────────────────────────────────────────────┘ ``` 核心原理在于:即使攻击者从未读取内存、从未见过密钥,并且密码算法本身在数学上是安全的,**来自 S-box 计算的物理泄漏**也足以独立恢复全部 16 个字节。 ## 构建与运行 ``` make ./trace-gen 2000 64 1.5 # 2000 traces, 64 samples each, σ=1.5 pip install -r python/requirements.txt python python/cpa_attack.py ``` ## 示例输出 ``` AES-128 Side-Channel Attack Simulator ═════════════════════════════════════ [self-test] AES-128 FIPS-197 vector PASSED Secret key: 2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c Generating traces: n=2000 samples=64 noise σ=1.50 Wrote 2000 traces (64 samples each) to traces.bin + plaintexts.csv ``` 然后执行攻击: ``` Loaded 2000 traces × 64 samples Loaded 2000 plaintexts Byte Best Top-5 candidates Truth Match ---------------------------------------------------------------- 0 0x2b 2b 6b ab 0b cb 0x2b ✔ 1 0x7e 7e fe 3e be 9e 0x7e ✔ 2 0x15 15 95 55 d5 35 0x15 ✔ ... 15 0x3c 3c bc 7c 1c fc 0x3c ✔ Recovered key: 2b7e151628aed2a6abf7158809cf4f3c Correct bytes: 16/16 (FULL KEY RECOVERED) ``` ## 可视化 传入 `--plot` 参数可以渲染一个 4×4 的网格,显示每个密钥字节下每个候选值的峰值相关性;真实值会在噪声底噪之上形成一个明显的尖峰: ``` python python/cpa_attack.py --plot ``` ## 工作原理(泄漏模型) 在真实的 CMOS 硬件中,计算中间值时的功耗与其**汉明重量**(Hamming weight,即 1 的位数)成正比。AES 第一轮 S-box 的输出 `SBOX[P ⊕ K]` 同时取决于已知的明文字节和未知的密钥字节。通过尝试所有 256 个候选密钥,并将每个假设与测量到的轨迹进行相关性分析,正确的猜测会在**统计上脱颖而出**——而其他的看起来就像随机噪声。 根据 FIPS 的防御对策,现实世界中的实现会通过掩码、隐藏、混淆以及常量时间逻辑来防御此类攻击。本项目特意展示了*未受保护*的情况。 ## 文件 ``` ├── include/ │ ├── aes.h │ └── trace_gen.h ├── src/ │ ├── aes.c # FIPS-197 reference AES-128 + S-box │ ├── trace_gen.c # Hamming-weight leakage + Gaussian noise │ └── main.c # Self-test + trace generation ├── python/ │ ├── cpa_attack.py # The CPA attack — ~120 lines, NumPy only │ └── requirements.txt └── Makefile ``` ## 调整攻击参数 | 参数 | 效果 | |---|---| | `n_traces` | 轨迹越多 → SNR 越高,在更高噪声下也能成功。对于未受保护的硬件,1000–5000 是比较现实的范围。 | | `samples` | 每条轨迹的采样点数——必须 ≥ 8 + byte_idx,以确保泄漏尖峰落在窗口范围内。 | | `noise σ` | σ 越大 = 攻击越难。当 σ ≈ 4 以上时,通常需要 1 万条以上的轨迹。 | 可以尝试运行 `./trace-gen 500 64 0.3` 来进行一次简单的测试,或者运行 `./trace-gen 10000 64 4.0` 来模拟一个高噪声的环境。 ## 参考文献 - Kocher, Jaffe, Jun, *"Differential Power Analysis"*, CRYPTO 1999. - Brier, Clavier, Olivier, *"Correlation Power Analysis with a Leakage Model"*, CHES 2004. - NIST FIPS 197, *"Advanced Encryption Standard"*. ## 许可证 MIT
标签:AES, StruQ, 侧信道攻击, 功耗分析, 客户端加密, 密码分析, 密码学, 手动系统调用, 教育演示, 逆向工具