📄 aestmr.cpp
字号:
/*
---------------------------------------------------------------------------
Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
LICENSE TERMS
The free distribution and use of this software in both source and binary
form is allowed (with or without changes) provided that:
1. distributions of this source code include the above copyright
notice, this list of conditions and the following disclaimer;
2. distributions in binary form include the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other associated materials;
3. the copyright holder's name is not used to endorse products
built using this software without specific written permission.
ALTERNATIVELY, provided that this notice is retained in full, this product
may be distributed under the terms of the GNU General Public License (GPL),
in which case the provisions of the GPL apply INSTEAD OF those given above.
DISCLAIMER
This software is provided 'as is' with no explicit or implied warranties
in respect of its properties, including, but not limited to, correctness
and/or fitness for purpose.
---------------------------------------------------------------------------
Issue 28/01/2004
*/
/* Measure the Encryption, Decryption and Key Setup Times for AES using
the Pentium Time Stamp Counter */
#include <iostream>
#include <fstream>
#include <memory.h>
#include <cmath>
#if defined( AES_CPP )
#include "aescpp.h"
#else
#include "aes.h"
#endif
#include "aesaux.h"
#include "aestst.h"
// Use this define if testing aespp.c
//#define AESX
#if defined(AES_DLL)
fn_ptrs fn;
#endif
#define t_pro(hi,lo) \
__asm { __asm cpuid \
__asm rdtsc \
__asm mov lo,eax \
__asm mov hi,edx \
}
#define t_epi(hi,lo,cy) \
__asm { __asm rdtsc \
__asm sub eax,lo \
__asm sbb edx,hi \
__asm mov lo,eax \
__asm mov hi,edx \
} \
cy = ((unsigned long long)hi << 32) | lo
#define t_epif(hi,lo,cy) \
__asm { __asm rdtsc \
__asm sub eax,lo \
__asm sbb edx,hi \
__asm mov lo,eax \
__asm mov hi,edx \
} \
cy = (double)(((unsigned long long)hi << 32) | lo)
#define PROCESSOR "P4" // Processor
const unsigned int loops = 100; // number of timing loops
word rand32(void)
{ static word 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;
}
byte rand8(void)
{ static word 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(byte l[], word len)
{ word i;
for(i = 0; i < len; ++i)
l[i] = rand8();
}
#define SAMPLE1 1000
#define SAMPLE2 10000
bool time_base(double& av, double& sig)
{ int i, tol, lcnt, sam_cnt;
unsigned int yl, yh;
double cy, av1, sig1;
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
t_pro(yh, yl);
t_epif(yh, yl, cy);
av1 += cy;
sig1 += cy * cy;
}
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)
{
t_pro(yh, yl);
t_epif(yh, yl, cy);
if(cy > av1 - sig1 && cy < av1 + sig1)
{
av += cy;
sig += cy * cy;
sam_cnt++;
}
}
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 false;
}
sam_cnt = 0;
}
}
return true;
}
typedef unsigned long long (*aes_tmr)(void);
bool time_eks8(unsigned int k_len, double& av, double& sig)
{ int i, tol, lcnt, sam_cnt;
unsigned int yl, yh;
double cy, av1, sig1;
unsigned char key[8][2 * AES_BLOCK_SIZE];
f_ectx ec[1];
block_rndfill(key[0], 16 * AES_BLOCK_SIZE);
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
t_pro(yh, yl);
f_enc_key(ec, key[0], k_len);
f_enc_key(ec, key[1], k_len);
f_enc_key(ec, key[2], k_len);
f_enc_key(ec, key[3], k_len);
f_enc_key(ec, key[4], k_len);
f_enc_key(ec, key[5], k_len);
f_enc_key(ec, key[6], k_len);
f_enc_key(ec, key[7], k_len);
t_epif(yh, yl, cy);
av1 += cy;
sig1 += cy * cy;
}
av1 /= SAMPLE1;
sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);
f_enc_key(ec, key[0], k_len);
av = sig = 0.0;
for(i = 0; i < SAMPLE2; ++i)
{
t_pro(yh, yl);
f_enc_key(ec, key[0], k_len);
f_enc_key(ec, key[1], k_len);
f_enc_key(ec, key[2], k_len);
f_enc_key(ec, key[3], k_len);
f_enc_key(ec, key[4], k_len);
f_enc_key(ec, key[5], k_len);
f_enc_key(ec, key[6], k_len);
f_enc_key(ec, key[7], k_len);
t_epif(yh, yl, cy);
if(cy > av1 - sig1 && cy < av1 + sig1)
{
av += cy;
sig += cy * cy;
sam_cnt++;
}
}
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 false;
}
sam_cnt = 0;
}
}
return true;
}
bool time_dks8(unsigned int k_len, double& av, double& sig)
{ int i, tol, lcnt, sam_cnt;
unsigned int yl, yh;
double cy, av1, sig1;
unsigned char key[8][2 * AES_BLOCK_SIZE];
f_dctx dc[1];
block_rndfill(key[0], 16 * AES_BLOCK_SIZE);
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
t_pro(yh, yl);
f_dec_key(dc, key[ 0], k_len);
f_dec_key(dc, key[ 1], k_len);
f_dec_key(dc, key[ 2], k_len);
f_dec_key(dc, key[ 3], k_len);
f_dec_key(dc, key[ 4], k_len);
f_dec_key(dc, key[ 5], k_len);
f_dec_key(dc, key[ 6], k_len);
f_dec_key(dc, key[ 7], k_len);
t_epif(yh, yl, cy);
av1 += cy;
sig1 += cy * cy;
}
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)
{
t_pro(yh, yl);
f_dec_key(dc, key[ 0], k_len);
f_dec_key(dc, key[ 1], k_len);
f_dec_key(dc, key[ 2], k_len);
f_dec_key(dc, key[ 3], k_len);
f_dec_key(dc, key[ 4], k_len);
f_dec_key(dc, key[ 5], k_len);
f_dec_key(dc, key[ 6], k_len);
f_dec_key(dc, key[ 7], k_len);
t_epif(yh, yl, cy);
if(cy > av1 - sig1 && cy < av1 + sig1)
{
av += cy;
sig += cy * cy;
sam_cnt++;
}
}
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 false;
}
sam_cnt = 0;
}
}
return true;
}
bool time_enc16(unsigned int k_len, double& av, double& sig)
{ int i, tol, lcnt, sam_cnt;
unsigned int yl, yh;
double cy, av1, sig1;
unsigned char pt[2 * AES_BLOCK_SIZE];
f_ectx ec[1];
block_rndfill(pt, 2 * AES_BLOCK_SIZE);
f_enc_key(ec, pt, k_len);
block_rndfill(pt, AES_BLOCK_SIZE);
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
t_pro(yh, yl);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
t_epif(yh, yl, cy);
av1 += cy;
sig1 += cy * cy;
}
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)
{
t_pro(yh, yl);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
f_enc_blk(ec, pt, pt);
t_epif(yh, yl, cy);
if(cy > av1 - sig1 && cy < av1 + sig1)
{
av += cy;
sig += cy * cy;
sam_cnt++;
}
}
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 false;
}
sam_cnt = 0;
}
}
return true;
}
bool time_dec16(unsigned int k_len, double& av, double& sig)
{ int i, tol, lcnt, sam_cnt;
unsigned int yl, yh;
double cy, av1, sig1;
unsigned char pt[2 * AES_BLOCK_SIZE];
f_dctx dc[1];
block_rndfill(pt, 32);
f_dec_key(dc, pt, k_len);
block_rndfill(pt, 16);
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
t_pro(yh, yl);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
t_epif(yh, yl, cy);
av1 += cy;
sig1 += cy * cy;
}
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)
{
t_pro(yh, yl);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
f_dec_blk(dc, pt, pt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -