📄 testcrypt.c
字号:
{ 0x05, 0x9B, 0x5E, 0x08, 0x51, 0xCF, 0x14, 0x3A },
{ 0x86, 0xA5, 0x60, 0xF1, 0x0E, 0xC6, 0xD8, 0x5B } },
{ { 0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6 },
{ 0x07, 0x56, 0xD8, 0xE0, 0x77, 0x47, 0x61, 0xD2 },
{ 0x0C, 0xD3, 0xDA, 0x02, 0x00, 0x21, 0xDC, 0x09 } },
{ { 0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE },
{ 0x76, 0x25, 0x14, 0xB8, 0x29, 0xBF, 0x48, 0x6A },
{ 0xEA, 0x67, 0x6B, 0x2C, 0xB7, 0xDB, 0x2B, 0x7A } },
{ { 0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16 },
{ 0x3B, 0xDD, 0x11, 0x90, 0x49, 0x37, 0x28, 0x02 },
{ 0xDF, 0xD6, 0x4A, 0x81, 0x5C, 0xAF, 0x1A, 0x0F } },
{ { 0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F },
{ 0x26, 0x95, 0x5F, 0x68, 0x35, 0xAF, 0x60, 0x9A },
{ 0x5C, 0x51, 0x3C, 0x9C, 0x48, 0x86, 0xC0, 0x88 } },
{ { 0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46 },
{ 0x16, 0x4D, 0x5E, 0x40, 0x4F, 0x27, 0x52, 0x32 },
{ 0x0A, 0x2A, 0xEE, 0xAE, 0x3F, 0xF4, 0xAB, 0x77 } },
{ { 0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E },
{ 0x6B, 0x05, 0x6E, 0x18, 0x75, 0x9F, 0x5C, 0xCA },
{ 0xEF, 0x1B, 0xF0, 0x3E, 0x5D, 0xFA, 0x57, 0x5A } },
{ { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 },
{ 0x00, 0x4B, 0xD6, 0xEF, 0x09, 0x17, 0x60, 0x62 },
{ 0x88, 0xBF, 0x0D, 0xB6, 0xD7, 0x0D, 0xEE, 0x56 } },
{ { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07 },
{ 0x48, 0x0D, 0x39, 0x00, 0x6E, 0xE7, 0x62, 0xF2 },
{ 0xA1, 0xF9, 0x91, 0x55, 0x41, 0x02, 0x0B, 0x56 } },
{ { 0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F },
{ 0x43, 0x75, 0x40, 0xC8, 0x69, 0x8F, 0x3C, 0xFA },
{ 0x6F, 0xBF, 0x1C, 0xAF, 0xCF, 0xFD, 0x05, 0x56 } },
{ { 0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7 },
{ 0x07, 0x2D, 0x43, 0xA0, 0x77, 0x07, 0x52, 0x92 },
{ 0x2F, 0x22, 0xE4, 0x9B, 0xAB, 0x7C, 0xA1, 0xAC } },
{ { 0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF },
{ 0x02, 0xFE, 0x55, 0x77, 0x81, 0x17, 0xF1, 0x2A },
{ 0x5A, 0x6B, 0x61, 0x2C, 0xC2, 0x6C, 0xCE, 0x4A } },
{ { 0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6 },
{ 0x1D, 0x9D, 0x5C, 0x50, 0x18, 0xF7, 0x28, 0xC2 },
{ 0x5F, 0x4C, 0x03, 0x8E, 0xD1, 0x2B, 0x2E, 0x41 } },
{ { 0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF },
{ 0x30, 0x55, 0x32, 0x28, 0x6D, 0x6F, 0x29, 0x5A },
{ 0x63, 0xFA, 0xC0, 0xD0, 0x34, 0xD9, 0xF7, 0x93 } }
};
/* CAST-128 Test Vectors from RFC2144 */
#define CAST_TEST_COUNT 1
typedef struct {
unsigned char key[16];
unsigned char plaintext[8];
unsigned char ciphertext[8];
} CAST_TEST;
CAST_TEST cast_ecb_vectors[CAST_TEST_COUNT] = {
"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A",
"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
"\x23\x8B\x4F\xE5\x84\x7E\x44\xB2"
};
unsigned char ks_tmp[MAX_EXPANDED_KEY];
void
init_cipher2(int cipher, void* key, void* ks, int key_len)
{
/* This routine uses init_cipher for everything except the variable key length ciphers */
switch (cipher)
{
case BLOWFISH:
BF_set_key (ks, key_len, key);
break;
case IDEA:
init_cipher(cipher,key,ks);
break;
case DES56:
init_cipher(cipher,key,ks);
break;
case CAST:
init_cipher(cipher,key,ks);
break;
case TRIPLEDES:
init_cipher(cipher,key,ks);
break;
}
}
void
ResetCipherTest(HWND hwndDlg, int nCipherChoice)
{
int ndx;
ShowWindow(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), SW_HIDE);
ShowWindow(GetDlgItem(hwndDlg, IDC_REDTICK), SW_HIDE);
/* Only allow user to pick key size if this is possible */
if (nCipherChoice == BLOWFISH)
EnableWindow(GetDlgItem(hwndDlg,IDC_KEY_SIZE), TRUE);
else
EnableWindow(GetDlgItem(hwndDlg,IDC_KEY_SIZE), FALSE);
/* Setup the keysize and plaintext sizes for the selected cipher */
SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_RESETCONTENT, 0,0);
SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_RESETCONTENT, 0,0);
ndx = SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_ADDSTRING, 0,(LPARAM) "8");
SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 8);
SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETCURSEL, ndx,0);
if (nCipherChoice == BLOWFISH)
{
ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "8");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 8);
ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "16");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "32");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 32);
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, 0,0);
SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000");
}
if (nCipherChoice == IDEA || nCipherChoice == CAST)
{
ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "16");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "00000000000000000000000000000000");
}
if (nCipherChoice == TRIPLEDES)
{
ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "24");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 24);
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "000000000000000000000000000000000000000000000000");
}
if (nCipherChoice == DES56)
{
ndx = SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) "8");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 8);
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, 0,0);
SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), "0000000000000000");
}
SetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), "0000000000000000");
SetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), "0000000000000000");
}
BOOL Des56TestLoop(void *test_vectors, int nVectorCount, int enc)
{
char key[32];
long tmp[2];
int i, cipher;
cipher = DES56;
for (i=0;i<nVectorCount;i++)
{
memcpy(key, ((DES_TEST*)test_vectors)->key, 8);
memcpy(tmp, ((DES_TEST*)test_vectors)->plaintext, 8);
memset(ks_tmp,0,sizeof(ks_tmp));
init_cipher2(cipher, key, ks_tmp, 8);
if (enc)
{
encipher_block(cipher, tmp, ks_tmp);
}
else
{
decipher_block(cipher, tmp, ks_tmp);
}
if (memcmp(((DES_TEST*)test_vectors)->ciphertext, tmp,8)!=0)
return FALSE;
}
return TRUE;
}
/* Except in response to the WM_INITDIALOG message, the dialog box procedure
should return nonzero if it processes the message, and zero if it does
not. - see DialogProc */
BOOL CALLBACK
CipherTestDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WORD lw = LOWORD (wParam);
static int nCipherChoice;
if (lParam); /* Remove unused parameter warning */
switch (uMsg)
{
case WM_INITDIALOG:
{
char szTmp[256];
SetDefaultUserFont (hwndDlg);
SendMessage(GetDlgItem(hwndDlg, IDC_TESTS_MESSAGE), WM_SETFONT, (WPARAM)hSmallBoldFont, MAKELPARAM(TRUE,0));
SendMessage(GetDlgItem(hwndDlg, IDC_KEY), EM_LIMITTEXT, 80,0);
SendMessage(GetDlgItem(hwndDlg, IDC_KEY), WM_SETFONT, (WPARAM)hSmallFont, MAKELPARAM(1,0));
SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT), EM_LIMITTEXT,80,0);
SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT), WM_SETFONT, (WPARAM)hSmallFont, MAKELPARAM(1,0));
SendMessage(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), EM_LIMITTEXT,80,0);
SendMessage(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), WM_SETFONT, (WPARAM)hSmallFont, MAKELPARAM(1,0));
nCipherChoice = (int) lParam;
ResetCipherTest(hwndDlg, nCipherChoice);
sprintf(szTmp, getstr(IDS_CIPHER_TEST), get_cipher_name(nCipherChoice));
SetWindowText(hwndDlg, szTmp);
return 1;
}
case WM_COMMAND:
if (lw == IDC_RESET)
{
ResetCipherTest(hwndDlg, nCipherChoice);
return 1;
}
if (lw == IDC_AUTO)
{
char key[32];
unsigned char tmp[8];
BOOL bFailed = FALSE;
int i;
for (i=0;i<BF_TEST_COUNT;i++)
{
memcpy(key, bf_ecb_vectors[i].key, 8);
memcpy(tmp, bf_ecb_vectors[i].plaintext, 8);
init_cipher2(BLOWFISH, key, ks_tmp, 8);
BF_ecb_encrypt(tmp,tmp,(struct bf_key_st *)ks_tmp,1);
if (memcmp(bf_ecb_vectors[i].ciphertext,tmp,8)!=0)
break;
}
if (i==BF_TEST_COUNT)
{
/* Test the Blowfish code against Bruce Schneiers test vectors (1 & 2) and
Mike Morgans test vector (3) */
unsigned char *plain1 = ( unsigned char * ) "BLOWFISH";
unsigned char *key1 = ( unsigned char * ) "abcdefghijk
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -