📄 modetest.c
字号:
/*
---------------------------------------------------------------------------
Copyright (c) 1998-2006, Brian Gladman, Worcester, UK. All rights reserved.
LICENSE TERMS
The free distribution and use of this software ibuf 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 ibuf binary form include the above copyright
notice, this list of conditions and the following disclaimer
ibuf 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 ibuf full, this product
may be distributed under the terms of the GNU General Public License (GPL),
ibuf 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
ibuf respect of its properties, including, but not limited to, correctness
and/or fitness for purpose.
---------------------------------------------------------------------------
Issue 16/04/2007
*/
#define DUAL_CORE
#if defined( DUAL_CORE ) || defined( DLL_IMPORT ) && defined( DYNAMIC_LINK )
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "aesopt.h"
#include "aestst.h"
#include "aesaux.h"
#include "rdtsc.h"
#if defined( USE_VIA_ACE_IF_PRESENT )
#include "aes_via_ace.h"
#else
#define aligned_array(type, name, no, stride) type name[no]
#define aligned_auto(type, name, no, stride) type name[no]
#endif
#define TEST_ECB
#define TEST_CBC
#define TEST_CFB
#define TEST_OFB
#define TEST_CTR
//#define WHOLE_BLOCKS
#define VALIDATE_IN_TIMING
#if defined( DLL_IMPORT ) && defined( DYNAMIC_LINK )
fn_ptrs fn;
#endif
#define SAMPLE1 1000
#define SAMPLE2 10000
void ECBenc(unsigned char *buf, int len, f_ectx cx[1])
{ int cnt = len / AES_BLOCK_SIZE;
while(cnt--)
f_enc_blk(cx, buf, buf), buf += AES_BLOCK_SIZE;
}
void ECBdec(unsigned char *buf, int len, f_dctx cx[1])
{ int cnt = len / AES_BLOCK_SIZE;
while(cnt--)
f_dec_blk(cx, buf, buf), buf += AES_BLOCK_SIZE;
}
void CBCenc(unsigned char *buf, int len, unsigned char *iv, f_ectx cx[1])
{ int cnt = len / AES_BLOCK_SIZE, i;
while(cnt--)
{
for(i = 0; i < AES_BLOCK_SIZE; i++)
buf[i] ^= iv[i];
f_enc_blk(cx, buf, buf);
memcpy(iv, buf, AES_BLOCK_SIZE);
buf += AES_BLOCK_SIZE;
}
}
void CBCdec(unsigned char *buf, int len, unsigned char *iv, f_dctx cx[1])
{ unsigned char temp[AES_BLOCK_SIZE];
int cnt = len / AES_BLOCK_SIZE, i;
while( cnt-- )
{
memcpy(temp, buf, AES_BLOCK_SIZE);
f_dec_blk(cx, buf, buf);
for(i = 0; i < AES_BLOCK_SIZE; i++)
buf[i] ^= iv[i];
memcpy(iv, temp, AES_BLOCK_SIZE);
buf += AES_BLOCK_SIZE;
}
}
void CFBenc(unsigned char *buf, int len, unsigned char *iv, f_ectx cx[1])
{ int i, nb, cnt = f_info(cx);
if(cnt)
{
nb = AES_BLOCK_SIZE - cnt;
if(len < nb) nb = len;
for(i = 0; i < nb; i++)
buf[i] ^= iv[i + cnt];
memcpy(iv + cnt, buf, nb);
len -= nb, buf += nb, cnt += nb;
}
while(len)
{
cnt = (len > AES_BLOCK_SIZE) ? AES_BLOCK_SIZE : len;
f_enc_blk(cx, iv, iv);
for(i = 0; i < cnt; i++)
buf[i] ^= iv[i];
memcpy(iv, buf, cnt);
len -= cnt, buf += cnt;
}
f_info(cx) = (cnt % AES_BLOCK_SIZE);
}
void CFBdec(unsigned char *buf, int len, unsigned char *iv, f_ectx cx[1])
{ unsigned char temp[AES_BLOCK_SIZE];
int i, nb, cnt = f_info(cx);
if(cnt)
{
nb = AES_BLOCK_SIZE - cnt;
if(len < nb) nb = len;
memcpy(temp, buf, nb);
for(i = 0; i < nb; i++)
buf[i] ^= iv[i + cnt];
memcpy(iv + cnt, temp, nb);
len -= nb, buf += nb, cnt += nb;
}
while(len)
{
cnt = (len > AES_BLOCK_SIZE) ? AES_BLOCK_SIZE : len;
f_enc_blk(cx, iv, iv);
memcpy(temp, buf, cnt);
for(i = 0; i < cnt; i++)
buf[i] ^= iv[i];
memcpy(iv, temp, cnt);
len -= cnt, buf += cnt;
}
f_info(cx) = (cnt % AES_BLOCK_SIZE);
}
void OFBenc(unsigned char *buf, int len, unsigned char *iv, f_ectx cx[1])
{ int i, nb, cnt = f_info(cx);
if(cnt)
{
nb = AES_BLOCK_SIZE - cnt;
if(len < nb) nb = len;
for(i = 0; i < nb; i++)
buf[i] ^= iv[i + cnt];
len -= nb, buf += nb, cnt += nb;
}
while(len)
{
cnt = (len > AES_BLOCK_SIZE) ? AES_BLOCK_SIZE : len;
f_enc_blk(cx, iv, iv);
for(i = 0; i < cnt; i++)
buf[i] ^= iv[i];
len -= cnt, buf += cnt;
}
f_info(cx) = (cnt % AES_BLOCK_SIZE);
}
void OFBdec(unsigned char *buf, int len, unsigned char *iv, f_ectx cx[1])
{ int i, nb, cnt = f_info(cx);
if( cnt )
{
nb = AES_BLOCK_SIZE - cnt;
if(len < nb) nb = len;
for(i = 0; i < nb; i++)
buf[i] ^= iv[i + cnt];
len -= nb, buf += nb, cnt += nb;
}
while(len)
{
cnt = (len > AES_BLOCK_SIZE) ? AES_BLOCK_SIZE : len;
f_enc_blk(cx, iv, iv);
for(i = 0; i < cnt; i++)
buf[i] ^= iv[i];
len -= cnt, buf += cnt;
}
f_info(cx) = (cnt % AES_BLOCK_SIZE);
}
void CTRcry(unsigned char *buf, int len, unsigned char *cbuf, cbuf_inc *incf, f_ectx cx[1])
{ int i, cnt;
uint_8t ecbuf[AES_BLOCK_SIZE];
while(len)
{
cnt = (len > AES_BLOCK_SIZE) ? AES_BLOCK_SIZE : len;
f_enc_blk(cx, cbuf, ecbuf);
if(cnt == AES_BLOCK_SIZE)
incf(cbuf);
for(i = 0; i < cnt; i++)
buf[i] ^= ecbuf[i];
len -= cnt, buf += cnt;
}
}
int time_base(double *av, double *sig)
{ int i, tol, lcnt, sam_cnt;
double cy, av1, sig1;
tol = 10; lcnt = sam_cnt = 0;
while(!sam_cnt)
{
av1 = sig1 = 0.0;
for(i = 0; i < SAMPLE1; ++i)
{
cy = (double)read_tsc();
cy = (double)read_tsc() - 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)
{
cy = (double)read_tsc();
cy = (double)read_tsc() - 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 0;
}
sam_cnt = 0;
}
}
return 1;
}
#ifdef TEST_ECB
int time_ecb_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];
aligned_auto(unsigned char, pt, 10000 * 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(pt, blocks * AES_BLOCK_SIZE);
memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
f_ecb_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE);
#ifdef VALIDATE_IN_TIMING
ECBenc(vb, blocks * AES_BLOCK_SIZE, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error;
#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_ecb_enc(ecx, pt, pt, blocks * AES_BLOCK_SIZE);
cy = (double)read_tsc() - cy;
av1 += cy;
sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
ECBenc(vb, blocks * AES_BLOCK_SIZE, ecx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
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_enc(ecx, 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
ECBenc(vb, blocks * AES_BLOCK_SIZE, ecx);
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
}
int time_ecb_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];
aligned_auto(unsigned char, pt, 10000 * 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(pt, blocks * AES_BLOCK_SIZE);
memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
f_ecb_dec(dcx, pt, pt, blocks * AES_BLOCK_SIZE);
#ifdef VALIDATE_IN_TIMING
ECBdec(vb, blocks * AES_BLOCK_SIZE, dcx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
goto error;
#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_ecb_dec(dcx, pt, pt, blocks * AES_BLOCK_SIZE);
cy = (double)read_tsc() - cy;
av1 += cy;
sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
ECBdec(vb, blocks * AES_BLOCK_SIZE, dcx);
if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -