📄 modetest.c
字号:
goto error;
#endif
}
av1 /= SAMPLE1;
sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);
*av = *sig = 0.0;
for(i = 0; i < SAMPLE2; ++i)
{
cy = (double)read_tsc();
f_ecb_dec(dcx, pt, pt, blocks * AES_BLOCK_SIZE);
cy = (double)read_tsc() - cy;
if(cy > av1 - sig1 && cy < av1 + sig1)
{
*av += cy;
*sig += cy * cy;
sam_cnt++;
}
#ifdef VALIDATE_IN_TIMING
ECBdec(vb, blocks * AES_BLOCK_SIZE, dcx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error;
#endif
}
if(10 * sam_cnt > 9 * SAMPLE2)
{
*av /= sam_cnt;
*sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
if(*sig > (tol / 100.0) * *av)
sam_cnt = 0;
}
else
{
if(lcnt++ == 10)
{
lcnt = 0; tol += 5;
if(tol > 30)
return 0;
}
sam_cnt = 0;
}
}
return 1;
#ifdef VALIDATE_IN_TIMING
error:
printf("\nECB Encryption data error in timing");
exit(1);
#endif
}
#endif
#ifdef TEST_CBC
int time_cbc_enc(unsigned int k_len, int blocks, double *av, double *sig)
{ int i, tol, lcnt, sam_cnt;
double cy, av1, sig1;
unsigned char key[2 * AES_BLOCK_SIZE];
unsigned char vb[10000 * AES_BLOCK_SIZE];
unsigned char viv[AES_BLOCK_SIZE];
aligned_auto(unsigned char, pt, 10000 * AES_BLOCK_SIZE, 16);
aligned_auto(unsigned char, iv, AES_BLOCK_SIZE, 16);
aligned_auto(f_ectx, ecx, 1, 16);
block_rndfill(key, 2 * AES_BLOCK_SIZE);
f_enc_key(ecx, key, k_len);
block_rndfill(iv, AES_BLOCK_SIZE);
memcpy(viv, iv, AES_BLOCK_SIZE);
block_rndfill(pt, blocks * AES_BLOCK_SIZE);
memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
f_cbc_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
#ifdef VALIDATE_IN_TIMING
CBCenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
cy = (double)read_tsc();
f_cbc_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
av1 += cy;
sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
CBCenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
}
av1 /= SAMPLE1;
sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);
*av = *sig = 0.0;
for(i = 0; i < SAMPLE2; ++i)
{
cy = (double)read_tsc();
f_cbc_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
if(cy > av1 - sig1 && cy < av1 + sig1)
{
*av += cy;
*sig += cy * cy;
sam_cnt++;
}
#ifdef VALIDATE_IN_TIMING
CBCenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
}
if(10 * sam_cnt > 9 * SAMPLE2)
{
*av /= sam_cnt;
*sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
if(*sig > (tol / 100.0) * *av)
sam_cnt = 0;
}
else
{
if(lcnt++ == 10)
{
lcnt = 0; tol += 5;
if(tol > 30)
return 0;
}
sam_cnt = 0;
}
}
return 1;
#ifdef VALIDATE_IN_TIMING
error1:
printf("\nCBC Encryption data error in timing");
exit(1);
error2:
printf("\nCBC Encryption iv error in timing");
exit(1);
#endif
}
int time_cbc_dec(unsigned int k_len, int blocks, double *av, double *sig)
{ int i, tol, lcnt, sam_cnt;
double cy, av1, sig1;
unsigned char key[2 * AES_BLOCK_SIZE];
unsigned char vb[10000 * AES_BLOCK_SIZE];
unsigned char viv[AES_BLOCK_SIZE];
aligned_auto(unsigned char, pt, 10000 * AES_BLOCK_SIZE, 16);
aligned_auto(unsigned char, iv, AES_BLOCK_SIZE, 16);
aligned_auto(f_dctx, dcx, 1, 16);
block_rndfill(key, 2 * AES_BLOCK_SIZE);
f_dec_key(dcx, key, k_len);
block_rndfill(iv, AES_BLOCK_SIZE);
memcpy(viv, iv, AES_BLOCK_SIZE);
block_rndfill(pt, blocks * AES_BLOCK_SIZE);
memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
f_cbc_dec(dcx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
#ifdef VALIDATE_IN_TIMING
CBCdec(vb, blocks * AES_BLOCK_SIZE, viv, dcx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
cy = (double)read_tsc();
f_cbc_dec(dcx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
av1 += cy;
sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
CBCdec(vb, blocks * AES_BLOCK_SIZE, viv, dcx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
}
av1 /= SAMPLE1;
sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);
*av = *sig = 0.0;
for(i = 0; i < SAMPLE2; ++i)
{
cy = (double)read_tsc();
f_cbc_dec(dcx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
if(cy > av1 - sig1 && cy < av1 + sig1)
{
*av += cy;
*sig += cy * cy;
sam_cnt++;
}
#ifdef VALIDATE_IN_TIMING
CBCdec(vb, blocks * AES_BLOCK_SIZE, viv, dcx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
}
if(10 * sam_cnt > 9 * SAMPLE2)
{
*av /= sam_cnt;
*sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
if(*sig > (tol / 100.0) * *av)
sam_cnt = 0;
}
else
{
if(lcnt++ == 10)
{
lcnt = 0; tol += 5;
if(tol > 30)
return 0;
}
sam_cnt = 0;
}
}
return 1;
#ifdef VALIDATE_IN_TIMING
error1:
printf("\nCBC Decryption data error in timing");
exit(1);
error2:
printf("\nCBC Decryption iv error in timing");
exit(1);
#endif
}
#endif
#ifdef TEST_CFB
int time_cfb_enc(unsigned int k_len, int blocks, double *av, double *sig)
{ int i, tol, lcnt, sam_cnt;
double cy, av1, sig1;
unsigned char key[2 * AES_BLOCK_SIZE];
unsigned char vb[10000 * AES_BLOCK_SIZE];
unsigned char viv[AES_BLOCK_SIZE];
aligned_auto(unsigned char, pt, 10000 * AES_BLOCK_SIZE, 16);
aligned_auto(unsigned char, iv, AES_BLOCK_SIZE, 16);
aligned_auto(f_ectx, ecx, 1, 16);
block_rndfill(key, 2 * AES_BLOCK_SIZE);
f_enc_key(ecx, key, k_len);
block_rndfill(iv, AES_BLOCK_SIZE);
memcpy(viv, iv, AES_BLOCK_SIZE);
block_rndfill(pt, blocks * AES_BLOCK_SIZE);
memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
f_cfb_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
#ifdef VALIDATE_IN_TIMING
CFBenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
cy = (double)read_tsc();
f_cfb_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
av1 += cy;
sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
CFBenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
}
av1 /= SAMPLE1;
sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);
*av = *sig = 0.0;
for(i = 0; i < SAMPLE2; ++i)
{
cy = (double)read_tsc();
f_cfb_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
if(cy > av1 - sig1 && cy < av1 + sig1)
{
*av += cy;
*sig += cy * cy;
sam_cnt++;
}
#ifdef VALIDATE_IN_TIMING
CFBenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
}
if(10 * sam_cnt > 9 * SAMPLE2)
{
*av /= sam_cnt;
*sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
if(*sig > (tol / 100.0) * *av)
sam_cnt = 0;
}
else
{
if(lcnt++ == 10)
{
lcnt = 0; tol += 5;
if(tol > 30)
return 0;
}
sam_cnt = 0;
}
}
return 1;
#ifdef VALIDATE_IN_TIMING
error1:
printf("\nCFB Encryption data error in timing");
exit(1);
error2:
printf("\nCFB Encryption iv error in timing");
exit(1);
#endif
}
int time_cfb_dec(unsigned int k_len, int blocks, double *av, double *sig)
{ int i, tol, lcnt, sam_cnt;
double cy, av1, sig1;
unsigned char key[2 * AES_BLOCK_SIZE];
unsigned char vb[10000 * AES_BLOCK_SIZE];
unsigned char viv[AES_BLOCK_SIZE];
aligned_auto(unsigned char, pt, 10000 * AES_BLOCK_SIZE, 16);
aligned_auto(unsigned char, iv, AES_BLOCK_SIZE, 16);
aligned_auto(f_ectx, ecx, 1, 16);
block_rndfill(key, 2 * AES_BLOCK_SIZE);
f_enc_key(ecx, key, k_len);
block_rndfill(iv, AES_BLOCK_SIZE);
memcpy(viv, iv, AES_BLOCK_SIZE);
block_rndfill(pt, blocks * AES_BLOCK_SIZE);
memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
f_cfb_dec(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
#ifdef VALIDATE_IN_TIMING
CFBdec(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
cy = (double)read_tsc();
f_cfb_dec(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
av1 += cy;
sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
CFBdec(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error1;
if(memcmp(viv, iv, AES_BLOCK_SIZE))
goto error2;
#endif
}
av1 /= SAMPLE1;
sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);
*av = *sig = 0.0;
for(i = 0; i < SAMPLE2; ++i)
{
cy = (double)read_tsc();
f_cfb_dec(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
cy = (double)read_tsc() - cy;
if(cy > av1 - sig1 && cy < av1 + sig1)
{
*av += cy;
*sig += cy * cy;
sam_cnt++;
}
#ifdef VALIDATE_IN_TIMING
CFBdec(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -