aes_gtst.cpp
来自「文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2」· C++ 代码 · 共 672 行 · 第 1/2 页
CPP
672 行
// Generate and Compare AES Test Files For KAT and Monte Carlo Tests
// Directory for AES test vector files and locally generated copies
char *aes_path = "i:\\testvals\\";
char *dir_path = "d:\\cpp\\aes\\";
// Frog, Safer and Serpent hack to get Monte Carlo test agreement
// because the test vectors for these algorithms assemble the key
// vectors in the reverse direction in memory
bool do_fss_hack = true;
#include "../std_defs.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <string>
using std::ios;
using std::ostream;
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
using std::setw;
using std::ios_base;
using std::string;
using std::getline;
string fstr[6] = { "KEYSIZE=", "I=", "IV=", "KEY=", "PT=", "CT=" };
char *hxx = "0123456789abcdef";
void v_out(ostream &outf, u1byte ty, u4byte len, void *v = 0)
{
if(ty == 1)
outf << endl << endl << fstr[ty] << len;
else
{ u1byte *p = reinterpret_cast<u1byte*>(v), buf[80], *bp = buf;
for(int i = 0; i < len; ++i)
{
*bp++ = hxx[(*(p + i) >> 4) & 15];
*bp++ = hxx[*(p + i) & 15];
}
*bp = '\0'; outf << endl << fstr[ty] << buf;
}
};
bool on_screen = false;
inline void clear_count(void)
{
if(on_screen)
cout << "\b\b\b\b \b\b\b\b";
on_screen = false;
};
void put_count(int x)
{
clear_count();
cout << (x /1000) % 10 << (x /100) % 10 << (x /10) % 10 << x % 10;
on_screen = true;
};
void header(ostream &outf, int type)
{ string str0("=======================================================");
string str1("Author: Dr B R Gladman ");
string str2("Test: ");
string str3(string("Algorithm: ") + cipher_name()[0] + " (" + cipher_name()[1] + ")");
string str4("Filename: ");
str2 += (type < 4 ? "ECB " : "CBC ");
str4 += (type < 4 ? "ecb_" : "cbc_");
switch(type)
{
case 0: str2 += "Variable Key Known Answer Tests";
str4 += "vk.txt"; break;
case 1: str2 += "Variable Text Known Answer Tests";
str4 += "vt.txt"; break;
case 2:
case 4: str2 += "Monte Carlo (Encryption) Tests";
str4 += "me.txt"; break;
case 3:
case 5: str2 += "Monte Carlo (Decryption) Tests";
str4 += "md.txt"; break;
}
outf << str0 << endl << str1 << endl << str2 << endl << str3
<< endl << str4 << endl << str0;
}
void ecb_vt(ostream &outf)
{ u4byte i, j, pt[4], ct[4], key[8];
key[0] = key[1] = key[2] = key[3] = 0;
key[4] = key[5] = key[6] = key[7] = 0;
for(i = 0; i < 3; ++i)
{
set_key(key, 128 + 64 * i);
outf << endl << endl << fstr[0] << setw(3) << 128 + 64 * i << endl;
v_out(outf, 3, 16 + 8 * i, key);
for(j = 0; j <= 128; ++j)
{
v_out(outf, 1, j);
pt[0] = pt[1] = pt[2] = pt[3] = 0;
if(j)
*((u1byte*)pt + (j - 1) / 8) = 0x80 >> (j - 1) % 8;
encrypt(pt , ct);
v_out(outf, 4, 16, pt);
v_out(outf, 5, 16, ct);
}
}
};
void ecb_vk(ostream &outf)
{ u4byte i, j, pt[4], ct[4], key[8];
pt[0] = pt[1] = pt[2] = pt[3] = 0;
for(i = 0; i < 3; ++i)
{
outf << endl << endl << fstr[0] << setw(3) << 128 + 64 * i << endl;
v_out(outf, 4, 16, pt);
for(j = 0; j <= 128 + 64 * i; ++j)
{
v_out(outf, 1, j);
key[0] = key[1] = key[2] = key[3] = 0;
key[4] = key[5] = key[6] = key[7] = 0;
if(j)
*((u1byte*)key + (j - 1) / 8) = 0x80 >> (j - 1) % 8;
set_key(key, 128 + 64 * i);
v_out(outf, 3, 16 + 8 * i, key);
encrypt(pt , ct);
v_out(outf, 5, 16, ct);
}
}
};
void ecb_me(ostream &outf)
{ u4byte i, j, k, xo, pt[4], key[8], ct[12];
for(i = 0; i < 3; ++i)
{
outf << endl << endl << fstr[0] << setw(3) << 128 + 64 * i;
pt[0] = pt[1] = pt[2] = pt[3] = 0;
key[0] = key[1] = key[2] = key[3] = 0;
key[4] = key[5] = key[6] = key[7] = 0;
ct[4] = pt[0]; ct[5] = pt[1]; ct[6] = pt[2]; ct[7] = pt[3];
for(j = 0; j < 400; j++)
{
if(j % 40 == 0)
put_count(j);
set_key(key, 128 + 64 * i);
v_out(outf, 1, j);
v_out(outf, 3, 16 + 8 * i, key);
v_out(outf, 4, 16, pt);
for(k = 0; k < 5000; ++k)
{
encrypt(ct + 4, ct); encrypt(ct, ct + 4);
}
v_out(outf, 5, 16, ct + 4);
pt[0] = ct[4]; pt[1] = ct[5]; pt[2] = ct[6]; pt[3] = ct[7];
xo = 4 - 2 * i;
if(do_fss_hack && (**cipher_name() == 'f' || **cipher_name() == 's'))
{
ct[8] = ct[0]; ct[9] = ct[1]; ct[10] = ct[2]; ct[11] = ct[3]; xo = 4;
}
for(k = 0; k < 4 + 2 * i; ++k)
key[k] ^= ct[k + xo];
}
}
clear_count();
};
void ecb_md(ostream &outf)
{ u4byte i, j, k, xo, pt[4], key[8], ct[12];
for(i = 0; i < 3; ++i)
{
outf << endl << endl << fstr[0] << setw(3) << 128 + 64 * i;
pt[0] = pt[1] = pt[2] = pt[3] = 0;
key[0] = key[1] = key[2] = key[3] = 0;
key[4] = key[5] = key[6] = key[7] = 0;
ct[4] = pt[0]; ct[5] = pt[1]; ct[6] = pt[2]; ct[7] = pt[3];
for(j = 0; j < 400; j++)
{
if(j % 40 == 0)
put_count(j);
set_key(key, 128 + 64 * i);
v_out(outf, 1, j);
v_out(outf, 3, 16 + 8 * i, key);
v_out(outf, 5, 16, pt);
for(k = 0; k < 5000; ++k)
{
decrypt(ct + 4, ct); decrypt(ct, ct + 4);
}
v_out(outf, 4, 16, ct + 4);
pt[0] = ct[4]; pt[1] = ct[5]; pt[2] = ct[6]; pt[3] = ct[7];
xo = 4 - 2 * i;
if(do_fss_hack && (**cipher_name() == 'f' || **cipher_name() == 's'))
{
ct[8] = ct[0]; ct[9] = ct[1]; ct[10] = ct[2]; ct[11] = ct[3]; xo = 4;
}
for(k = 0; k < 4 + 2 * i; ++k)
key[k] ^= ct[k + xo];
}
}
clear_count();
};
void cbc_me(ostream &outf)
{ u4byte i, j, k, xo, key[8], ct[12];
for(i = 0; i < 3; ++i)
{
outf << endl << endl << fstr[0] << setw(3) << 128 + 64 * i;
key[0] = key[1] = key[2] = key[3] = 0;
key[4] = key[5] = key[6] = key[7] = 0; // KEY[0]
ct[4] = ct[5] = ct[6] = ct[7] = 0; // IV[0]
ct[0] = ct[1] = ct[2] = ct[3] = 0; // PT[0]
for(j = 0; j < 400; j++)
{
if(j % 40 == 0)
put_count(j);
set_key(key, 128 + 64 * i);
v_out(outf, 1, j);
v_out(outf, 3, 16 + 8 * i, key);
v_out(outf, 2, 16, ct + 4);
v_out(outf, 4, 16, ct);
for(k = 0; k < 5000; ++k)
{
ct[0] ^= ct[4]; ct[1] ^= ct[5]; ct[2] ^= ct[6]; ct[3] ^= ct[7];
encrypt(ct, ct);
ct[4] ^= ct[0]; ct[5] ^= ct[1]; ct[6] ^= ct[2]; ct[7] ^= ct[3];
encrypt(ct + 4, ct + 4);
}
v_out(outf, 5, 16, ct + 4);
xo = 4 - 2 * i;
if(do_fss_hack && (**cipher_name() == 'f' || **cipher_name() == 's'))
{
ct[8] = ct[0]; ct[9] = ct[1]; ct[10] = ct[2]; ct[11] = ct[3]; xo = 4;
}
for(k = 0; k < 4 + 2 * i; ++k)
key[k] ^= ct[k + xo];
}
}
clear_count();
};
void cbc_md(ostream &outf)
{ u4byte i, j, k, xo, pt[4], key[8], ct[12];
for(i = 0; i < 3; ++i)
{
outf << endl << endl << fstr[0] << setw(3) << 128 + 64 * i;
key[0] = key[1] = key[2] = key[3] = 0;
key[4] = key[5] = key[6] = key[7] = 0; // KEY[0]
ct[0] = ct[1] = ct[2] = ct[3] = 0; // IV[0]
ct[4] = ct[5] = ct[6] = ct[7] = 0; // CT[0]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?