前言
研究生研究方向为侧信道攻击,导师布置的任务是实现相关能量攻击(CPA),之前没有接触过这些,同时也算是C++小白,写起来还是遇到了很多问题,最终实现,特此记录。
CPA流程
1.波形采集阶段:对单片机进行能量波形采集,我的波形文件都是导师给的采集好的,共100条波形,每条波有8500个点,使用AES算法加密。 2.密钥恢复阶段:猜测密钥K,K的取值为0~255,将每一个K的可能取值与明文异或通过S盒得到中间值。 3.相关系数计算:计算中间值与波形的相关系数,相关系数最大的便是正确密钥。
CPA代码
读取明文、密文等实现
int HW_variable[100][256];
double corr[256][8500];
double temp[8500][100];
int hw[256][100];
double Deta[256];
int SBox[16][16] = {
{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}
};
string Num2String(int i)
{
if (i > 0 && i < 10)
return "00" + to_string(i);
if (i > 9 && i < 100)
return "0" + to_string(i);
if (i == 100)
return to_string(i);
return " ";
}
vector<double> TraceRead(int i)
{
ifstream infile;
string str1 = "C:/Users/Wei/Desktop/CPA测试数据/CPA测试数据/相关能量分析_原始波_无滤波_100条_8500点/Trace000";
string str2 = Num2String(i) + ".csv";
string path = str1 + str2;
infile.open(path);
vector<string> vec;
string temp;
while (getline(infile, temp, ','))
{
vec.push_back(temp);
}
vector<double> radius;
for (auto it = vec.begin(); it != vec.end(); it++)
{
istringstream is(*it);
string s;
int pam = 0;
while (is >> s)
{
if (pam == 0)
{
double r = atof(s.c_str());
radius.push_back(r);
}
pam++;
}
}
auto k = radius.begin();
radius.erase(k);
infile.close();
return radius;
}
vector<int> PlainTextRead()
{
ifstream infile;
infile.open("C:/Users/Wei/Desktop/CPA测试数据/CPA测试数据/相关能量分析_原始波_无滤波_100条_8500点/Plaintext.csv");
vector<string> vec;
string temp;
int count = 1;
while (getline(infile, temp))
{
if (count % 4 == 3)
{
vec.push_back(temp);
}
count++;
}
vector<int> plain;
for (auto it = vec.begin(); it != vec.end(); it++)
{
istringstream is(*it);
string s;
while (is >> s)
{
int r = strtol(s.c_str(), 0, 16);
plain.push_back(r);
}
}
infile.close();
return plain;
}
double Corrcoef(int A[], double B[], int Length)
{
double sumA, sumB, aveA, aveB;
int i;
sumA = 0;
for (i = 0; i < Length; i++)
{
sumA += A[i];
}
sumB = 0;
for (i = 0; i < Length; i++)
{
sumB += B[i];
}
aveA = sumA / double(Length);
aveB = sumB / double(Length);
double R1 = 0, R2 = 0, R3 = 0;
for (long i = 0; i < Length; i++)
{
R1 += (A[i] - aveA) * (B[i] - aveB);
R2 += pow((A[i] - aveA), 2);
R3 += pow((B[i] - aveB), 2);
}
double r = (R1 / sqrt(R2 * R3));
return r;
}
int HW(int s)
{
int count = 0;
int bit;
for (int i = 7; i >= 0; i--)
{
bit = s % 2;
if (bit == 1)
count++;
s = s / 2;
}
return count;
}
int SBoxOut(int x)
{
int row = (x & 0xf0) >> 4;
int col = x & 0x0f;
return SBox[row][col];
}
CPA函数
extern int HW_variable[100][256];
extern double corr[256][8500];
extern double temp[8500][100];
extern int hw[256][100];
void CPA(int num)
{
double maxCorr = 0;
int point = 0;
int key = 0;
vector<vector<int>> vec1(100);
for (int i = 0; i < 100; i++)
{
vec1[i].resize(16);
}
for (int i = 0; i < 1600; i++)
{
vec1[i / 16][i % 16] = PlainTextRead()[i];
}
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 100; j++)
{
int tmp = (vec1[j][num]) ^ i;
HW_variable[j][i] = HW(SBoxOut(tmp));
}
}
vector<vector<double>> vec2(100);
for (int i = 0; i < 100; i++)
{
vec2[i].resize(8500);
}
for (int i = 1; i < 101; i++)
{
vec2[i - 1] = TraceRead(i);
}
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 100; j++)
{
hw[i][j] = HW_variable[j][i];
}
}
for (int k = 0; k < 8500; k++)
{
for (int m = 0; m < 100; m++)
{
temp[k][m] = vec2[m][k];
}
}
for (int i = 0; i < 256; i++)
{
for (int j = 0; j < 8500; j++)
{
corr[i][j] = Corrcoef(hw[i], temp[j], 100);
if (abs(corr[i][j]) > abs(maxCorr))
{
maxCorr = corr[i][j];
point = j;
key = i;
}
}
}
cout << "最大相关系数为" << maxCorr << "此时点为" << dec << point << "密钥为";
cout << hex << key << endl;
}
主函数
int main()
{
for (int i = 0; i < 16; i++)
{
cout << "第" << dec << i+1 << "个密钥为: ";
CPA(i);
}
cin.get();
}
总结
第一次接触CPA,在实现过程中遇到了很多问题,写的代码也是很乱,希望看到这篇文章的大佬能提出意见与建议供本人日后改正。
|