/////////////////////////////////////////////////////////////////
// Sample: testing Crypton API functions
// (c) Ancud 1998
// --------------------------------------------------------------
// The sample shows using all the Crypton API functions
/////////////////////////////////////////////////////////////////
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "..\..\INC\CryptLib.h"
CRHANDLE hCR; // Handle to a crypton service
#define TestBufSize 2048 // The size of the buffers for encryption/decryption and random tests
char TestBuf1[TestBufSize]; // Buffers for encryption/decryption and random tests
char TestBuf2[TestBufSize];
char TestBuf3[TestBufSize];
crSYNCHRO crSync;
crKEY crKey1,crKey2; // Buffers for keys
crIP crIP1,crIP2; // Buffers for imitoprefixes
void // Almost obvious function ;)
WriteRes(BOOL res)
{
if (res)
printf(" \tOK\n");
else
printf(" \tFALSE\n");
}
void // Making equal buffers
SetEqualBufs(char* Buf1,char* Buf2,int Size)
{
char j=35;
int i;
for (i=0; i<Size; i++)
{
*(Buf1+i)=j;
*(Buf2+i)=j;
j++;
}
}
void // Making different buffers
SetBufs(char* Buf1,char* Buf2,int Size)
{
char j=32;
int i;
for (i=0; i<Size; i++)
{
*(Buf1+i)=j;
*(Buf2+i)=255-j;
j++;
}
}
void
main()
{
printf("\nTest of Crypton API functions (c) Ancud 1998\n");
if (crInitCryptonLib()) // Don't forget to call this function
// before using Crypton API functions
{
hCR=crInitService(); // Get a handle to the hardware service
if (hCR)
{
BOOL res;
if(crTestHardware(hCR)){
printf("Using DK with Crypton Hardware!\n");
}else
printf("Using DK with Crypton Emulator!\n");
// crTestHardware
printf("crTestHardware\t"); WriteRes(crTestHardware(hCR));
// crGetCryptonNumber
printf("crGetCryptonNumber\t%i\n",crGetCryptonNumber(hCR));
// crRandom
printf("crRandom\t");
SetEqualBufs(TestBuf1,TestBuf2,TestBufSize);
res=crRandom(hCR,TestBufSize,TestBuf1);
res&=(memcmp(TestBuf1,TestBuf2,TestBufSize)!=0);
WriteRes(res);
// crControlledRandom
{
crCTRLRANDOM TestBuf1,TestBuf2;
printf("crControlledRandom");
SetEqualBufs(TestBuf1,TestBuf2,CTRLRANDOM_LEN);
res=crControlledRandom(hCR,TestBuf1);
res&=(memcmp(TestBuf1,TestBuf2,CTRLRANDOM_LEN)!=0);
WriteRes(res);
}
// crSetK1
printf("crSetK1\t\t");
crRandom(hCR,CRYPTON_KEY_LEN,crKey1);
WriteRes(crSetK1(hCR,crKey1));
// crSetK2
printf("crSetK2\t\t");
crRandom(hCR,CRYPTON_KEY_LEN,crKey1);
WriteRes(crSetK2(hCR,crKey1));
// crSetK1onK2
printf("crSetK1onK2\t");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
res=crSetK1onK2(hCR,crKey1);
res&=crGetK1onK2(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)==0);
WriteRes(res);
// crGetK1onK2
printf("crGetK1onK2\t");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
crSetK1onK2(hCR,crKey1);
res=crGetK1onK2(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)==0);
WriteRes(res);
// crGetK2onK1
printf("crGetK2onK1\t");
crRandom(hCR,CRYPTON_KEY_LEN,crKey1);
crSetK1(hCR,crKey1);
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
res=crGetK2onK1(hCR,crKey1);
res&=crGetK2onK1(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)==0);
WriteRes(res);
// crSetK2onK3
printf("crSetK2onK3\t");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
res=crSetK2onK3(hCR,crKey1);
res&=crGetK2onK3(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)==0);
WriteRes(res);
// crGetK2onK3
printf("crGetK2onK3\t");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
crSetK2onK3(hCR,crKey1);
res=crGetK2onK3(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)==0);
WriteRes(res);
// crSetK1onK3
printf("crSetK1onK3\t");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
res=crSetK1onK3(hCR,crKey1);
res&=crGetK1onK3(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)==0);
WriteRes(res);
// crGetK1onK3
printf("crGetK1onK3\t");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
crSetK1onK3(hCR,crKey1);
res=crGetK1onK3(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)==0);
WriteRes(res);
// crSetRandomK1onK3
printf("crSetRandomK1onK3");
SetEqualBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
res=crGetK1onK3(hCR,crKey1);
res&=crSetRandomK1onK3(hCR);
res&=crGetK1onK3(hCR,crKey2);
res&=(memcmp(crKey1,crKey2,CRYPTON_KEY_LEN)!=0);
WriteRes(res);
// crGetIPforDataonK1
printf("crGetIPforDataonK1");
SetBufs(TestBuf1,TestBuf2,TestBufSize);
SetBufs(crIP1,crIP2,CRYPTON_IP_LEN);
res=crGetIPforDataonK1(hCR,TestBufSize,TestBuf1,crIP1);
res&=crGetIPforDataonK1(hCR,TestBufSize,TestBuf1,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)==0);
res&=crGetIPforDataonK1(hCR,TestBufSize,TestBuf2,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)!=0);
WriteRes(res);
// crGetIPforK1onK2
printf("crGetIPforK1onK2");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
SetBufs(crIP1,crIP2,CRYPTON_IP_LEN);
crSetK1(hCR,crKey1);
res=crGetIPforK1onK2(hCR,crIP1);
res&=crGetIPforK1onK2(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)==0);
crSetK1(hCR,crKey2);
res&=crGetIPforK1onK2(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)!=0);
WriteRes(res);
// crGetIPforK2onK3
printf("crGetIPforK2onK3");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
SetBufs(crIP1,crIP2,CRYPTON_IP_LEN);
crSetK2(hCR,crKey1);
res=crGetIPforK2onK3(hCR,crIP1);
res&=crGetIPforK2onK3(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)==0);
crSetK2(hCR,crKey2);
res&=crGetIPforK2onK3(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)!=0);
WriteRes(res);
// crGetIPforK2onK1
printf("crGetIPforK2onK1");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
SetBufs(crIP1,crIP2,CRYPTON_IP_LEN);
crSetK2(hCR,crKey1);
res=crGetIPforK2onK1(hCR,crIP1);
res&=crGetIPforK2onK1(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)==0);
crSetK2(hCR,crKey2);
res&=crGetIPforK2onK1(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)!=0);
WriteRes(res);
// crGetIPforK1onK3
printf("crGetIPforK1onK3");
SetBufs(crKey1,crKey2,CRYPTON_KEY_LEN);
SetBufs(crIP1,crIP2,CRYPTON_IP_LEN);
crSetK1(hCR,crKey1);
res=crGetIPforK1onK3(hCR,crIP1);
res&=crGetIPforK1onK3(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)==0);
crSetK1(hCR,crKey2);
res&=crGetIPforK1onK3(hCR,crIP2);
res&=(memcmp(crIP1,crIP2,CRYPTON_IP_LEN)!=0);
WriteRes(res);
// crGammaCodeonK1Ex
printf("crGammaCodeonK1Ex");
SetEqualBufs(TestBuf1,TestBuf2,TestBufSize);
crRandom(hCR,CRYPTON_SYNCHRO_LEN,crSync);
res=crGammaCodeonK1Ex(hCR,TestBufSize,crSync,TestBuf1,TestBuf2);
res&=(memcmp(TestBuf1,TestBuf2,TestBufSize)!=0);
SetBufs(TestBuf1,TestBuf3,TestBufSize);
res&=crGammaCodeonK1Ex(hCR,TestBufSize,crSync,TestBuf1,TestBuf2);
res&=crGammaCodeonK1Ex(hCR,TestBufSize,crSync,TestBuf2,TestBuf3);
res&=(memcmp(TestBuf1,TestBuf3,TestBufSize)==0);
WriteRes(res);
// crGammaRecoveryCodeonK1Ex
printf("crGammaRecoveryCodeonK1Ex");
SetEqualBufs(TestBuf1,TestBuf2,TestBufSize);
crRandom(hCR,CRYPTON_SYNCHRO_LEN,crSync);
res=crGammaRecoveryCodeonK1Ex(hCR,TestBufSize,crSync,TestBuf1,TestBuf2);
res&=(memcmp(TestBuf1,TestBuf2,TestBufSize)!=0);
SetBufs(TestBuf1,TestBuf3,TestBufSize);
res&=crGammaRecoveryCodeonK1Ex(hCR,TestBufSize,crSync,TestBuf1,TestBuf2);
res&=crGammaRecoveryDecodeonK1Ex(hCR,TestBufSize,crSync,TestBuf2,TestBuf3);
res&=(memcmp(TestBuf1,TestBuf3,TestBufSize)==0);
WriteRes(res);
crDoneService(hCR); // We don't need this handle any more
}
else
printf("Error starting the service.\n");
crDoneCryptonLib(); // We aren't going to use Crypton API any more
}
else
printf("Error initialization of the Crypton library.\n");
printf("\nPress any key...\n"); _getch();
}
во, интересное в свежем
Когда я вижу как ты шифруешь, малыш, ты меня волнуешь
а почему было реализовано именно аппаратно, если сам алгоритм не являлся закрытым? для софтовой реализации производительности не хватало или архитектура не позволяла?
Необходимо для ускорения шифрования (создавалось это ещё в эпоху ЕС ПЭВМ), а также для большей защищённости (ключи хранились в памяти платы и не могли быть извлечены).