密码数学挑战赛题目分析
赛题描述
给定采集得到关于 AES 密码芯片运行过程中的泄露样本与对应的明文信息,该泄露样本对应于 AES密码芯片执行第一轮第一个 S盒输出值。该 AES密码芯片信息泄露无法使用汉明重量模型准确刻画其信息泄露特征。问题:基于给定泄露样本与对应明文信息编写能能量分析攻击代码恢复 AES 密码芯片所使用密钥第一字节值。提示:该问题主要考察参赛人员利用统计工具准确挖掘给定数据的信息特征的能力,参赛人员可以借助概率密度函数、相关系数、互信息、KS 检测等统计工具或深度学习领域 CNN自动识别工具实现特征提取与密钥恢复的目标。
赛题分析
题目给了六组三对数据,分别对应低中高噪的plain和leakage。其中:
Plain:在AES加密过程中,"plain"通常指的是明文数据,即在加密之前原始的、未经加密的数据。在侧信道攻击中,攻击者的目标是尝试从加密过程中的物理或电磁泄漏中恢复出明文信息。
Leakage:在侧信道攻击中,"leakage"指的是加密设备在执行加密操作时无意中泄露的信息。这种泄露可能包括功耗、电磁辐射、计算时间等,它们可以被攻击者用来推断加密密钥或明文数据。"Leakage"可以进一步细分为不同的级别,如"high"、"medium"和"low"。
因本人知识背景局限性,只针对AES侧信道攻击恢复明文的方法做CPA攻击、互信息代码演示,以求恢复明文信息。
赛题数据
赛题数据可通过全国高校密码数学挑战赛官方网站获取。
赛题代码及结果
CPA攻击
CPA攻击,全称为Correlation Power Analysis(相关功耗分析),是一种侧信道攻击技术,主要用于分析加密设备在执行加密算法时的功耗模式。通过测量设备在处理不同数据时的功耗变化,攻击者可以推断出加密密钥或其他敏感信息。
import numpy as np
from scipy.io import loadmat
from scipy.stats import pearsonr
import matplotlib.pyplot as plt
plain_low = loadmat('plain_low.mat')['plain']
leakage_low = loadmat('leakage_low.mat')['leakage']
plain_low = plain_low % 256
sbox = [
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf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,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
]
def hamming_weight(n):
return bin(n).count("1")
def hypothetical_leakage(plain, key_guess):
num_traces = plain.shape[0]
hypothetical = np.zeros(num_traces)
for i in range(num_traces):
index = (plain[i, 0] ^ key_guess) % 256
intermediate_value = sbox[index]
hypothetical[i] = hamming_weight(intermediate_value)
return hypothetical
def calculate_correlation(leakage, hypothetical):
num_samples = leakage.shape[1]
correlation = np.zeros(num_samples)
for i in range(num_samples):
correlation[i] = pearsonr(leakage[:, i], hypothetical)[0]
return correlation
def plot_correlation(keys, correlations, title):
plt.figure(figsize=(10, 6))
plt.plot(keys, correlations)
plt.title(title)
plt.xlabel('Key Guess')
plt.ylabel('Max Correlation')
plt.grid(True)
plt.show()
def cpa_attack(plain, leakage):
num_guesses = 256
max_correlation = np.zeros(num_guesses)
for key_guess in range(num_guesses):
hypothetical = hypothetical_leakage(plain, key_guess)
correlation = calculate_correlation(leakage, hypothetical)
max_correlation[key_guess] = np.max(np.abs(correlation))
best_guess = np.argmax(max_correlation)
keys = np.arange(num_guesses)
plot_correlation(keys, max_correlation, 'Correlation vs Key Guess')
return best_guess
recovered_key_low = cpa_attack(plain_low, leakage_low)
print(f'恢复的密钥字节(低噪声):{recovered_key_low}')
plain_medium = loadmat('plain_medium.mat')['plain']
leakage_medium = loadmat('leakage_medium.mat')['leakage']
plain_medium = plain_medium % 256
recovered_key_medium = cpa_attack(plain_medium, leakage_medium)
print(f'恢复的密钥字节(中噪声):{recovered_key_medium}')
plain_high = loadmat('plain_high.mat')['plain']
leakage_high = loadmat('leakage_high.mat')['leakage']
plain_high = plain_high % 256
recovered_key_high = cpa_attack(plain_high, leakage_high)
print(f'恢复的密钥字节(高噪声):{recovered_key_high}')
互信息
使用互信息进行能量分析攻击
互信息(Mutual Information)是一种衡量两个随机变量之间相互依赖程度的统计量。在侧信道分析中,我们可以使用互信息来衡量假设密钥和泄漏样本之间的关系,从而恢复密钥。
import numpy as np
from scipy.io import loadmat
from sklearn.metrics import mutual_info_score
from sklearn.preprocessing import KBinsDiscretizer
import matplotlib.pyplot as plt
plain_low = loadmat('plain_low.mat')['plain']
leakage_low = loadmat('leakage_low.mat')['leakage']
plain_medium = loadmat('plain_medium.mat')['plain']
leakage_medium = loadmat('leakage_medium.mat')['leakage']
plain_high = loadmat('plain_high.mat')['plain']
leakage_high = loadmat('leakage_high.mat')['leakage']
plain_low = plain_low % 256
plain_medium = plain_medium % 256
plain_high = plain_high % 256
if plain_low.ndim == 1:
plain_low = plain_low.reshape(-1, 1)
if plain_medium.ndim == 1:
plain_medium = plain_medium.reshape(-1, 1)
if plain_high.ndim == 1:
plain_high = plain_high.reshape(-1, 1)
sbox = [
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
]
def hamming_weight(n):
return bin(n).count("1")
def hypothetical_leakage(plain, key_guess):
num_traces = plain.shape[0]
hypothetical = np.zeros(num_traces)
for i in range(num_traces):
intermediate_value = sbox[plain[i, 0] ^ key_guess]
hypothetical[i] = hamming_weight(intermediate_value)
return hypothetical
def calculate_mutual_information(leakage, hypothetical):
num_samples = leakage.shape[1]
mutual_information = np.zeros(num_samples)
discretizer = KBinsDiscretizer(n_bins=10, encode='ordinal', strategy='uniform', subsample=None)
leakage_discrete = discretizer.fit_transform(leakage)
for i in range(num_samples):
mutual_information[i] = mutual_info_score(leakage_discrete[:, i], hypothetical)
return mutual_information
def plot_mutual_information(keys, mutual_informations, title):
plt.figure(figsize=(10, 6))
plt.plot(keys, mutual_informations)
plt.title(title)
plt.xlabel('Key Guess')
plt.ylabel('Max Mutual Information')
plt.grid(True)
plt.show()
def mia_attack(plain, leakage):
num_guesses = 256
max_mutual_information = np.zeros(num_guesses)
for key_guess in range(num_guesses):
hypothetical = hypothetical_leakage(plain, key_guess)
mutual_information = calculate_mutual_information(leakage, hypothetical)
max_mutual_information[key_guess] = np.max(mutual_information)
best_guess = np.argmax(max_mutual_information)
keys = np.arange(num_guesses)
plot_mutual_information(keys, max_mutual_information, 'Mutual Information vs Key Guess')
return best_guess
recovered_key_low = mia_attack(plain_low, leakage_low)
print(f'恢复的密钥字节(低噪声):{recovered_key_low}')
recovered_key_medium = mia_attack(plain_medium, leakage_medium)
print(f'恢复的密钥字节(中噪声):{recovered_key_medium}')
recovered_key_high = mia_attack(plain_high, leakage_high)
print(f'恢复的密钥字节(高噪声):{recovered_key_high}')
原文始发于微信公众号(攻防SRC):第九届(2024)全国高校密码数学挑战赛赛题三
- 左青龙
- 微信扫一扫
- 右白虎
- 微信扫一扫
评论