📄 aes_tst.cpp
字号:
// I retain copyright in this code but I encourage its free use provided
// that I don't carry any responsibility for the results. I am especially
// happy to see it used in free and open source software. If you do use
// it I would appreciate an acknowledgement of its origin in the code or
// the product that results and I would also appreciate knowing a liitle
// about the use to which it is being put.
//
// Dr B. R. Gladman <brg@gladman.uk.net> 1st June 2001.
// Correct Output (for variable block size - BLOCK_SIZE undefined):
// key = 2b7e151628aed2a6abf7158809cf4f3c
// input = 3243f6a8885a308d313198a2e0370734
// enc = 3925841d02dc09fbdc118597196a0b32
// dec = 3243f6a8885a308d313198a2e0370734
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input = 3243f6a8885a308d313198a2e0370734
// enc = 231d844639b31b412211cfe93712b880
// dec = 3243f6a8885a308d313198a2e0370734
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input = 3243f6a8885a308d313198a2e0370734
// enc = f9fb29aefc384a250340d833b87ebc00
// dec = 3243f6a8885a308d313198a2e0370734
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input = 3243f6a8885a308d313198a2e0370734
// enc = 8faa8fe4dee9eb17caa4797502fc9d3f
// dec = 3243f6a8885a308d313198a2e0370734
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input = 3243f6a8885a308d313198a2e0370734
// enc = 1a6e6c2c662e7da6501ffb62bc9e93f3
// dec = 3243f6a8885a308d313198a2e0370734
// key = 2b7e151628aed2a6abf7158809cf4f3c
// input = 3243f6a8885a308d313198a2e03707344a409382
// enc = 16e73aec921314c29df905432bc8968ab64b1f51
// dec = 3243f6a8885a308d313198a2e03707344a409382
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input = 3243f6a8885a308d313198a2e03707344a409382
// enc = 0553eb691670dd8a5a5b5addf1aa7450f7a0e587
// dec = 3243f6a8885a308d313198a2e03707344a409382
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input = 3243f6a8885a308d313198a2e03707344a409382
// enc = 73cd6f3423036790463aa9e19cfcde894ea16623
// dec = 3243f6a8885a308d313198a2e03707344a409382
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input = 3243f6a8885a308d313198a2e03707344a409382
// enc = 601b5dcd1cf4ece954c740445340bf0afdc048df
// dec = 3243f6a8885a308d313198a2e03707344a409382
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input = 3243f6a8885a308d313198a2e03707344a409382
// enc = 579e930b36c1529aa3e86628bacfe146942882cf
// dec = 3243f6a8885a308d313198a2e03707344a409382
// key = 2b7e151628aed2a6abf7158809cf4f3c
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// enc = b24d275489e82bb8f7375e0d5fcdb1f481757c538b65148a
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// enc = 738dae25620d3d3beff4a037a04290d73eb33521a63ea568
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// enc = 725ae43b5f3161de806a7c93e0bca93c967ec1ae1b71e1cf
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// enc = bbfc14180afbf6a36382a061843f0b63e769acdc98769130
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// enc = 0ebacf199e3315c2e34b24fcc7c46ef4388aa475d66c194c
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d
// key = 2b7e151628aed2a6abf7158809cf4f3c
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// enc = b0a8f78f6b3c66213f792ffd2a61631f79331407a5e5c8d3793aceb1
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// enc = 08b99944edfce33a2acb131183ab0168446b2d15e958480010f545e3
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// enc = be4c597d8f7efe22a2f7e5b1938e2564d452a5bfe72399c7af1101e2
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// enc = ef529598ecbce297811b49bbed2c33bbe1241d6e1a833dbe119569e8
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// enc = 02fafc200176ed05deb8edb82a3555b0b10d47a388dfd59cab2f6c11
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa9
// key = 2b7e151628aed2a6abf7158809cf4f3c
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// enc = 7d15479076b69a46ffb3b3beae97ad8313f622f67fedb487de9f06b9ed9c8f19
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// enc = 514f93fb296b5ad16aa7df8b577abcbd484decacccc7fb1f18dc567309ceeffd
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da5
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// enc = 5d7101727bb25781bf6715b0e6955282b9610e23a43c2eb062699f0ebf5887b2
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d90
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// enc = d56c5a63627432579e1dd308b2c8f157b40a4bfb56fea1377b25d3ed3d6dbf80
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
// input = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
// enc = a49406115dfb30a40418aafa4869b7c6a886ff31602a7dd19c889dc64f7e4e7a
// dec = 3243f6a8885a308d313198a2e03707344a4093822299f31d0082efa98ec4e6c8
#include <iostream>
#include <iomanip>
#include <memory.h>
#include "aes.h"
#include "aes_aux.h"
#include "aes_tst.h"
// Use this define if testing aes++.c
#define AESX
#if defined(AES_DLL)
fn_ptrs fn;
#endif
void oblk(char m[], byte v[], word n = 16)
{
std::cout << std::hex << std::setfill('0') << '\n' << m;
for(word i = 0; i < n; ++i)
std::cout << std::setw(2) << static_cast<word>(v[i]);
}
void message(char *s) { std::cout << s; }
#if defined(AES_DLL)
#include "windows.h"
HINSTANCE init_dll(fn_ptrs *fn)
{ HINSTANCE h_dll;
if(!(h_dll = LoadLibrary(dll_path)))
{
message("\n\nDynamic link Library AES_DLL not found\n\n"); return 0;
}
fn->fn_set_key = (f_ky*)GetProcAddress(h_dll, "_set_key@16");
fn->fn_encrypt = (f_ed*)GetProcAddress(h_dll, "_encrypt@12");
fn->fn_decrypt = (f_ed*)GetProcAddress(h_dll, "_decrypt@12");
fn->fn_set_blk = (f_bl*)GetProcAddress(h_dll, "_set_blk@8");
#if !defined(BLOCK_SIZE)
if(!fn->fn_set_key || !fn->fn_encrypt || !fn->fn_decrypt || !fn->fn_set_blk)
#else
if(!fn->fn_set_key || !fn->fn_encrypt || !fn->fn_decrypt)
#endif
{
message("\n\nRequired DLL Entry Point(s) not found\n\n");
FreeLibrary(h_dll);
return 0;
}
return h_dll;
}
#endif // AES_DLL
byte pih[32] = // hex digits of pi
{
0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
0x4a, 0x40, 0x93, 0x82, 0x22, 0x99, 0xf3, 0x1d,
0x00, 0x82, 0xef, 0xa9, 0x8e, 0xc4, 0xe6, 0xc8
};
byte exh[32] = // hex digits of e
{
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
0x76, 0x2e, 0x71, 0x60, 0xf3, 0x8b, 0x4d, 0xa5,
0x6a, 0x78, 0x4d, 0x90, 0x45, 0x19, 0x0c, 0xfe
};
byte res[5][5][32] =
{
{
{ 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32
},
{
0x23, 0x1d, 0x84, 0x46, 0x39, 0xb3, 0x1b, 0x41,
0x22, 0x11, 0xcf, 0xe9, 0x37, 0x12, 0xb8, 0x80
},
{ 0xf9, 0xfb, 0x29, 0xae, 0xfc, 0x38, 0x4a, 0x25,
0x03, 0x40, 0xd8, 0x33, 0xb8, 0x7e, 0xbc, 0x00
},
{
0x8f, 0xaa, 0x8f, 0xe4, 0xde, 0xe9, 0xeb, 0x17,
0xca, 0xa4, 0x79, 0x75, 0x02, 0xfc, 0x9d, 0x3f
},
{ 0x1a, 0x6e, 0x6c,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x35, 0x55, 0xb0,
0xb1, 0x0d, 0x47, 0xa3, 0x88, 0xdf, 0xd5, 0x9c,
0xab, 0x2f, 0x6c, 0x11
}
},
{
{ 0x7d, 0x15, 0x47, 0x90, 0x76, 0xb6, 0x9a, 0x46,
0xff, 0xb3, 0xb3, 0xbe, 0xae, 0x97, 0xad, 0x83,
0x13, 0xf6, 0x22, 0xf6, 0x7f, 0xed, 0xb4, 0x87,
0xde, 0x9f, 0x06, 0xb9, 0xed, 0x9c, 0x8f, 0x19
},
{
0x51, 0x4f, 0x93, 0xfb, 0x29, 0x6b, 0x5a, 0xd1,
0x6a, 0xa7, 0xdf, 0x8b, 0x57, 0x7a, 0xbc, 0xbd,
0x48, 0x4d, 0xec, 0xac, 0xcc, 0xc7, 0xfb, 0x1f,
0x18, 0xdc, 0x56, 0x73, 0x09, 0xce, 0xef, 0xfd
},
{ 0x5d, 0x71, 0x01, 0x72, 0x7b, 0xb2, 0x57, 0x81,
0xbf, 0x67, 0x15, 0xb0, 0xe6, 0x95, 0x52, 0x82,
0xb9, 0x61, 0x0e, 0x23, 0xa4, 0x3c, 0x2e, 0xb0,
0x62, 0x69, 0x9f, 0x0e, 0xbf, 0x58, 0x87, 0xb2
},
{
0xd5, 0x6c, 0x5a, 0x63, 0x62, 0x74, 0x32, 0x57,
0x9e, 0x1d, 0xd3, 0x08, 0xb2, 0xc8, 0xf1, 0x57,
0xb4, 0x0a, 0x4b, 0xfb, 0x56, 0xfe, 0xa1, 0x37,
0x7b, 0x25, 0xd3, 0xed, 0x3d, 0x6d, 0xbf, 0x80
},
{ 0xa4, 0x94, 0x06, 0x11, 0x5d, 0xfb, 0x30, 0xa4,
0x04, 0x18, 0xaa, 0xfa, 0x48, 0x69, 0xb7, 0xc6,
0xa8, 0x86, 0xff, 0x31, 0x60, 0x2a, 0x7d, 0xd1,
0x9c, 0x88, 0x9d, 0xc6, 0x4f, 0x7e, 0x4e, 0x7a
}
}
};
#if defined(AESX)
#define INC 1
#else
#define INC 2
#endif
#if BLOCK_SIZE == 16
#define STR 0
#define CNT 1
#elif BLOCK_SIZE == 20
#define STR 1
#define CNT 2
#elif BLOCK_SIZE == 24
#define STR 2
#define CNT 3
#elif BLOCK_SIZE == 28
#define STR 3
#define CNT 4
#elif BLOCK_SIZE == 32
#define STR 4
#define CNT 5
#elif !defined(BLOCK_SIZE)
#define STR 0
#define CNT 5
#else
#error Illegal block size
#endif
int main(void)
{ byte out[32], ret[32], err = 0, i, j;
aes alg;
#if defined(AES_DLL)
HINSTANCE h_dll;
fn_ptrs fn;
if(!(h_dll = init_dll(&fn))) return -1;
#endif
#if !defined(AES_IN_CPP)
alg.mode = 0;
#endif
message("\nRun tests for the AES algorithm");
#if defined(AES_DLL)
message(" (DLL Version)");
#endif
for(i = STR; i < CNT; i += INC)
{
#if !defined(BLOCK_SIZE)
f_set_blk(&alg, 16 + 4 * i);
#elif defined(AES_DLL)
if(fn.fn_set_blk) f_set_blk(&alg, BLOCK_SIZE);
#endif
for(j = 0; j < 5; j += INC)
{
f_set_key(&alg, exh, 16 + 4 * j, both);
oblk("\n// key = ", exh, 16 + 4 * j);
oblk("// input = ", pih, 16 + 4 * i);
f_encrypt(&alg, pih, out);
oblk("// enc = ", out, 16 + 4 * i);
if(memcmp(out, res[i][j], 16 + 4 * i)) err++;
f_decrypt(&alg, out, ret);
oblk("// dec = ", ret, 16 + 4 * i);
if(memcmp(ret, pih, 16 + 4 * i)) err++;
}
}
message("\n\nThese values are ");
message(err ? "in error\n\n" : "correct\n\n");
#if defined(AES_DLL)
if(h_dll) FreeLibrary(h_dll);
#endif
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -