📄 modetest.c
字号:
i = ptx_rpt;
while(i--)
{
memcpy(buf, ctx, ptx_len);
f->decrypt(buf, ptx_len, contx);
}
f->compute_tag(tbuf, tag_len, contx);
f->end(contx);
if(ptx_rpt == 1 && memcmp(ptx, buf, ptx_len))
printf("\n\tdecrypt plaintext error on test number %i", vec_no), err++;
if(memcmp(tag, tbuf, tag_len))
printf("\n\tdecrypt tag error on test number %i", vec_no), err++;
}
if(!err)
printf("matched");
if(inf) fclose(inf);
if( gen_flag && outf )
fclose(outf);
}
return;
}
const int loops = 100; // number of timing loops
unsigned int rand32(void)
{ static unsigned int r4,r_cnt = -1,w = 521288629,z = 362436069;
z = 36969 * (z & 65535) + (z >> 16);
w = 18000 * (w & 65535) + (w >> 16);
r_cnt = 0; r4 = (z << 16) + w; return r4;
}
unsigned char rand8(void)
{ static unsigned int r4,r_cnt = 4;
if(r_cnt == 4)
{
r4 = rand32(); r_cnt = 0;
}
return (char)(r4 >> (8 * r_cnt++));
}
// fill a block with random charactrers
void block_rndfill(unsigned char l[], unsigned int len)
{ unsigned int i;
for(i = 0; i < len; ++i)
l[i] = rand8();
}
double ccm_time(int key_len, int iv_len, int hdr_len, int txt_len)
{ int i, c1 = INT_MAX, c2 = INT_MAX, cy1, cy2, err;
unsigned volatile long long tval;
unsigned char t1[BLOCK_SIZE], t2[BLOCK_SIZE], t3[BLOCK_SIZE];
ccm_ctx ctx[1];
unsigned char *kp = malloc(key_len);
unsigned char *ip = malloc(iv_len);
unsigned char *hp = malloc(hdr_len);
unsigned char *tp = malloc(txt_len);
unsigned char *bp1 = malloc(txt_len);
unsigned char *bp2 = malloc(txt_len);
unsigned char *bp3 = malloc(txt_len);
block_rndfill(kp, key_len);
block_rndfill(ip, iv_len);
block_rndfill(hp, hdr_len);
block_rndfill(tp, txt_len);
ccm_init_and_key(kp, key_len, ctx);
for(i = 0; i < loops; ++i)
{
memcpy(bp1, tp, txt_len);
memcpy(bp2, tp, txt_len);
memcpy(bp3, tp, txt_len);
err = 0;
tval = read_tsc();
ccm_encrypt_message(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx);
cy1 = (int)(read_tsc() - tval);
tval = read_tsc();
ccm_encrypt_message(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx);
ccm_encrypt_message(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx);
cy2 = (int)(read_tsc() - tval);
err |= ccm_decrypt_message
(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx) == EXIT_FAILURE
|| memcmp(bp1, tp, txt_len);
err |= ccm_decrypt_message
(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx) == EXIT_FAILURE
|| memcmp(bp2, tp, txt_len);
err |= ccm_decrypt_message
(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx) == EXIT_FAILURE
|| memcmp(bp3, tp, txt_len);
if(err) printf("\n error");
c1 = (unsigned int)(c1 > cy1 ? cy1 : c1);
c2 = (unsigned int)(c2 > cy2 ? cy2 : c2);
}
ccm_end(ctx);
free(kp); free(ip);
free(hp); free(tp);
free(bp1); free(bp2); free(bp3);
return ((c2 - c1) + 0.5);
}
double cwc_time(int key_len, int iv_len, int hdr_len, int txt_len)
{ int i, c1 = INT_MAX, c2 = INT_MAX, cy1, cy2, err;
unsigned volatile long long tval;
unsigned char t1[BLOCK_SIZE], t2[BLOCK_SIZE], t3[BLOCK_SIZE];
cwc_ctx ctx[1];
unsigned char *kp = malloc(key_len);
unsigned char *ip = malloc(iv_len);
unsigned char *hp = malloc(hdr_len);
unsigned char *tp = malloc(txt_len);
unsigned char *bp1 = malloc(txt_len);
unsigned char *bp2 = malloc(txt_len);
unsigned char *bp3 = malloc(txt_len);
block_rndfill(kp, key_len);
block_rndfill(ip, iv_len);
block_rndfill(hp, hdr_len);
block_rndfill(tp, txt_len);
cwc_init_and_key(kp, key_len, ctx);
for(i = 0; i < loops; ++i)
{
memcpy(bp1, tp, txt_len);
memcpy(bp2, tp, txt_len);
memcpy(bp3, tp, txt_len);
err = 0;
tval = read_tsc();
cwc_encrypt_message(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx);
cy1 = (int)(read_tsc() - tval);
tval = read_tsc();
cwc_encrypt_message(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx);
cwc_encrypt_message(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx);
cy2 = (int)(read_tsc() - tval);
err |= cwc_decrypt_message
(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx) == EXIT_FAILURE
|| memcmp(bp1, tp, txt_len);
err |= cwc_decrypt_message
(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx) == EXIT_FAILURE
|| memcmp(bp2, tp, txt_len);
err |= cwc_decrypt_message
(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx) == EXIT_FAILURE
|| memcmp(bp3, tp, txt_len);
if(err) printf("\n error");
c1 = (unsigned int)(c1 > cy1 ? cy1 : c1);
c2 = (unsigned int)(c2 > cy2 ? cy2 : c2);
}
cwc_end(ctx);
free(kp); free(ip);
free(hp); free(tp);
free(bp1); free(bp2); free(bp3);
return ((c2 - c1) + 0.5);
}
double eax_time(int key_len, int iv_len, int hdr_len, int txt_len)
{ int i, c1 = INT_MAX, c2 = INT_MAX, cy1, cy2, err;
unsigned volatile long long tval;
unsigned char t1[BLOCK_SIZE], t2[BLOCK_SIZE], t3[BLOCK_SIZE];
eax_ctx ctx[1];
unsigned char *kp = malloc(key_len);
unsigned char *ip = malloc(iv_len);
unsigned char *hp = malloc(hdr_len);
unsigned char *tp = malloc(txt_len);
unsigned char *bp1 = malloc(txt_len);
unsigned char *bp2 = malloc(txt_len);
unsigned char *bp3 = malloc(txt_len);
block_rndfill(kp, key_len);
block_rndfill(ip, iv_len);
block_rndfill(hp, hdr_len);
block_rndfill(tp, txt_len);
eax_init_and_key(kp, key_len, ctx);
for(i = 0; i < loops; ++i)
{
memcpy(bp1, tp, txt_len);
memcpy(bp2, tp, txt_len);
memcpy(bp3, tp, txt_len);
err = 0;
tval = read_tsc();
eax_encrypt_message(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx);
cy1 = (int)(read_tsc() - tval);
tval = read_tsc();
eax_encrypt_message(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx);
eax_encrypt_message(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx);
cy2 = (int)(read_tsc() - tval);
err |= eax_decrypt_message
(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx) == EXIT_FAILURE
|| memcmp(bp1, tp, txt_len);
err |= eax_decrypt_message
(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx) == EXIT_FAILURE
|| memcmp(bp2, tp, txt_len);
err |= eax_decrypt_message
(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx) == EXIT_FAILURE
|| memcmp(bp3, tp, txt_len);
if(err) printf("\n error");
c1 = (unsigned int)(c1 > cy1 ? cy1 : c1);
c2 = (unsigned int)(c2 > cy2 ? cy2 : c2);
}
eax_end(ctx);
free(kp); free(ip);
free(hp); free(tp);
free(bp1); free(bp2); free(bp3);
return ((c2 - c1) + 0.5);
}
double gcm_time(int key_len, int iv_len, int hdr_len, int txt_len)
{ int i, c1 = INT_MAX, c2 = INT_MAX, cy1, cy2, err;
unsigned volatile long long tval;
unsigned char t1[BLOCK_SIZE], t2[BLOCK_SIZE], t3[BLOCK_SIZE];
gcm_ctx ctx[1];
unsigned char *kp = malloc(key_len);
unsigned char *ip = malloc(iv_len);
unsigned char *hp = malloc(hdr_len);
unsigned char *tp = malloc(txt_len);
unsigned char *bp1 = malloc(txt_len);
unsigned char *bp2 = malloc(txt_len);
unsigned char *bp3 = malloc(txt_len);
block_rndfill(kp, key_len);
block_rndfill(ip, iv_len);
block_rndfill(hp, hdr_len);
block_rndfill(tp, txt_len);
gcm_init_and_key(kp, key_len, ctx);
for(i = 0; i < loops; ++i)
{
memcpy(bp1, tp, txt_len);
memcpy(bp2, tp, txt_len);
memcpy(bp3, tp, txt_len);
err = 0;
tval = read_tsc();
gcm_encrypt_message(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx);
cy1 = (int)(read_tsc() - tval);
tval = read_tsc();
gcm_encrypt_message(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx);
gcm_encrypt_message(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx);
cy2 = (int)(read_tsc() - tval);
err |= gcm_decrypt_message
(ip, iv_len, hp, hdr_len, bp1, txt_len, t1, 16, ctx) == EXIT_FAILURE
|| memcmp(bp1, tp, txt_len);
err |= gcm_decrypt_message
(ip, iv_len, hp, hdr_len, bp2, txt_len, t2, 16, ctx) == EXIT_FAILURE
|| memcmp(bp2, tp, txt_len);
err |= gcm_decrypt_message
(ip, iv_len, hp, hdr_len, bp3, txt_len, t3, 16, ctx) == EXIT_FAILURE
|| memcmp(bp3, tp, txt_len);
if(err) printf("\n error");
c1 = (unsigned int)(c1 > cy1 ? cy1 : c1);
c2 = (unsigned int)(c2 > cy2 ? cy2 : c2);
}
gcm_end(ctx);
free(kp); free(ip);
free(hp); free(tp);
free(bp1); free(bp2); free(bp3);
return ((c2 - c1) + 0.5);
}
static int tlen[12] = { 16, 20, 40, 44, 64, 128, 256, 552, 576, 1024, 1500, 8192 };
void ccm_tests(unsigned long key_len, unsigned long iv_len,
unsigned long hdr_len, double tval[], int flag)
{ ccm_ctx ctx[1];
mode_fns f[1];
int i;
ccm_functions(f);
do_test(f, ctx, flag);
for(i = 0; i < 12; ++i)
tval[i] = ccm_time(key_len, iv_len, hdr_len, tlen[i]);
}
void cwc_tests(unsigned long key_len, unsigned long iv_len,
unsigned long hdr_len, double tval[], int flag)
{ cwc_ctx ctx[1];
mode_fns f[1];
int i;
cwc_functions(f);
do_test(f, ctx, flag);
for(i = 0; i < 12; ++i)
tval[i] = cwc_time(key_len, iv_len, hdr_len, tlen[i]);
}
void eax_tests(unsigned long key_len, unsigned long iv_len,
unsigned long hdr_len, double tval[], int flag)
{ eax_ctx ctx[1];
mode_fns f[1];
int i;
eax_functions(f);
do_test(f, ctx, flag);
for(i = 0; i < 12; ++i)
tval[i] = eax_time(key_len, iv_len, hdr_len, tlen[i]);
}
void gcm_tests(unsigned long key_len, unsigned long iv_len,
unsigned long hdr_len, double tval[], int flag)
{ gcm_ctx ctx[1];
mode_fns f[1];
int i;
gcm_functions(f);
do_test(f, ctx, flag);
for(i = 0; i < 12; ++i)
tval[i] = gcm_time(key_len, iv_len, hdr_len, tlen[i]);
}
int main(void)
{
double tval[4][13];
unsigned long hdr_len = 0;
int i, flag = 1;
#if defined( DUAL_CORE ) && defined( _WIN32 )
// we need to constrain the process to one core in order to
// obtain meaningful timing data
HANDLE ph;
DWORD_PTR afp;
DWORD_PTR afs;
ph = GetCurrentProcess();
if(GetProcessAffinityMask(ph, &afp, &afs))
{
afp &= (GetCurrentProcessorNumber() + 1);
if(!SetProcessAffinityMask(ph, afp))
{
printf("Couldn't set Process Affinity Mask\n\n"); return -1;
}
}
else
{
printf("Couldn't get Process Affinity Mask\n\n"); return -1;
}
#endif
ccm_tests(16, 12, hdr_len, tval[0], flag);
cwc_tests(16, 12, hdr_len, tval[1], flag);
eax_tests(16, 12, hdr_len, tval[2], flag);
gcm_tests(16, 12, hdr_len, tval[3], flag);
for(i = 0; i < 4; ++i)
{ double av;
av = 0.05 * tval[i][3] + 0.15 * tval[i][7]
+ 0.2 * tval[i][8] + 0.6 * tval[i][10];
av /= (0.05 * 44 + 0.15 * 552 + 0.2 * 576 + 0.6 * 1500 + hdr_len);
tval[i][12] = av;
}
printf("\n\n Length CCM CWC EAX GCM");
for(i = 0; i < 12; ++i)
{
printf("\n%8i %8.2f %8.2f %8.2f %8.2f", tlen[i],
tval[0][i] / (tlen[i] + hdr_len),
tval[1][i] / (tlen[i] + hdr_len),
tval[2][i] / (tlen[i] + hdr_len),
tval[3][i] / (tlen[i] + hdr_len));
}
printf("\naverage: %8.2f %8.2f %8.2f %8.2f",
tval[0][12], tval[1][12], tval[2][12], tval[3][12]);
printf("\n\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -