Защита информации в системах дистанционного обучения с монопольным доступом
SetValue(p, *vars, *regs, *data, 0);
p = (&operations) - dayTmp;
break;
}
}
}
}
//-----------------------------------------------------------------------------
void CryptDecryptData(BYTE &dataArray, size_t size, BYTE &algorithmDataArray,
size_t algorithmDataSize, bool crypt)
{
_ASSERT(dataSizeSmoothing >= sizeof(int) * maxNumTransformation);
if (!size || !algorithmDataSize || size % dataSizeSmoothing != 0)
CkgExcept(ERROR_INVALID_ARGUMENT);
AlgorithmDataStruct
&alg = reinterpret_cast
(algorithmDataArray);
if (alg.idSupportAlgorithm != idSupportAlgorithm ||
(crypt && alg.idType != idCryptAlgorithm) ||
(!crypt && alg.idType != idDecryptAlgorithm))
CkgExcept(_T("unsupported crypt algorithm type"));
if (alg.sizeAll != algorithmDataSize)
CkgExcept(_T("error crypt algorithm size"));
alg.variables[alg.numVarForSetSize] = size / sizeof(int);
BYTE *p = &algorithmDataArray;
p += sizeof AlgorithmDataStruct;
// _ASSERT(DWORD(p) & 0x03 == 0x0);
_ASSERT(DWORD(p) % sizeof(int) == 0x0);
TranslateOperations(*reinterpret_cast
dataArray);
}
//-----------------------------------------------------------------------------
void CryptData(BYTE &dataArray, size_t size, BYTE &algorithmDataArray,
size_t algorithmDataSize)
{
CryptDecryptData(dataArray, size, algorithmDataArray,
algorithmDataSize, true);
}
//-----------------------------------------------------------------------------
void DecryptData(BYTE &dataArray, size_t size, BYTE &algorithmDataArray,
size_t algorithmDataSize)
{
CryptDecryptData(dataArray, size, algorithmDataArray,
algorithmDataSize, false);
}
//-----------------------------------------------------------------------------
void CryptDecryptData(BYTE &dataArray, size_t size, const TCHAR &fileName,
bool crypt)
{
FILE *f = _tfopen(&fileName, _T("rb"));
if (!f)
CkgExcept(ERROR_OPEN_FILE);
try
{
if (fseek(f, 0, SEEK_END))
CkgExcept(ERROR_READ_FILE);
long algorithmDataSize = ftell(f);
cpp_arr_ptr
TestPtr(algorithmDataArray);
if (fseek(f, 0, SEEK_SET))
CkgExcept(ERROR_READ_FILE);
if (fread(algorithmDataArray, sizeof(BYTE), algorithmDataSize, f) !=
static_cast
CkgExcept(ERROR_READ_FILE);
if (fclose(f))
CkgExcept(ERROR_CLOSE_FILE);
f = 0;
CryptDecryptData(dataArray, size, *algorithmDataArray,
algorithmDataSize, crypt);
}
catch (...)
{
if (f)
fclose(f);
throw;
}
}
//-----------------------------------------------------------------------------
void CryptData(BYTE &dataArray, size_t size, const TCHAR &fileName)
{
CryptDecryptData(dataArray, size, fileName, true);
}
//-----------------------------------------------------------------------------
void DecryptData(BYTE &dataArray, size_t size, const TCHAR &fileName)
{
CryptDecryptData(dataArray, size, fileName, false);
}
//-----------------------------------------------------------------------------
unsigned GetRandomNonUsedRegister(const AlgorithmParameters ¶meters)
{
unsigned i;
unsigned numberNonUsedRegisters = 0;
for (i = 0; i < registersNum; i++)
numberNonUsedRegisters += !parameters.usedRegisters[i];
if (!numberNonUsedRegisters)
CkgExcept();
unsigned randomNum = CoolRand(numberNonUsedRegisters) + 1;
for (i = 0; i < registersNum; i++)
{
randomNum -= !parameters.usedRegisters[i];
if (!randomNum)
break;
}
_ASSERT(!randomNum && !parameters.usedRegisters[i]);
return i;
}
//-----------------------------------------------------------------------------
unsigned GetRandomNonUsedVariable(const AlgorithmParameters ¶meters)
{
unsigned i;
unsigned numberNonUsedVariables = 0;
for (i = 0; i < variablesNum; i++)
numberNonUsedVariables += !parameters.usedVariables[i];
if (!numberNonUsedVariables)
CkgExcept();
unsigned randomNum = CoolRand(numberNonUsedVariables) + 1;
for (i = 0; i < variablesNum; i++)
{
randomNum -= !parameters.usedVariables[i];
if (!randomNum)
break;
}
_ASSERT(!randomNum && !parameters.usedVariables[i]);
return i;
}
//-----------------------------------------------------------------------------
unsigned ReserveRandomNonUsedRegister(AlgorithmParameters ¶meters)
{
unsigned num = GetRandomNonUsedRegister(parameters);
parameters.usedRegisters[num] = true;
return num;
}
//-----------------------------------------------------------------------------
unsigned ReserveRandomNonUsedVariables(AlgorithmParameters ¶meters)
{
unsigned num = GetRandomNonUsedVariable(parameters);
parameters.usedVariables[num] = true;
return num;
}
//-----------------------------------------------------------------------------
void RandomInitAlgorithmParameters(AlgorithmParameters ¶meters,
unsigned numTransformation)
{
unsigned i;
for (i = 0; i < registersNum; i++)
parameters.usedRegisters[i] = false;
for (i = 0; i < variablesNum; i++)
parameters.usedVariables[i] = false;
parameters.sizeAlgorithm =
CoolRand(maxSizeAlgorithm - minSizeAlgorithm) + minSizeAlgorithm;
parameters.numTransformation = numTransformation;
parameters.addressIsReg = CoolRand(2) == 1;
if (parameters.addressIsReg)
parameters.addressNumber = ReserveRandomNonUsedRegister(parameters);
else
parameters.addressNumber = ReserveRandomNonUsedVariables(parameters);
for (i = 0; i < parameters.numTransformation; i++)
{
bool isReg = CoolRand(2) == 1;
parameters.modificatoryIsReg[i] = isReg;
if (isReg)
parameters.modificatorsNumbers[i] = ReserveRandomNonUsedRegister(parameters);
else
parameters.modificatorsNumbers[i] = ReserveRandomNonUsedVariables(parameters);
}
parameters.counterIsReg = CoolRand(2);
if (parameters.counterIsReg)
parameters.counterNumber = ReserveRandomNonUsedRegister(parameters);
else
parameters.counterNumber = ReserveRandomNonUsedVariables(parameters);
parameters.initCounterNumber = ReserveRandomNonUsedVariables(parameters);
parameters.totalNumTrueBlocks =
numBlocks + parameters.numTransformation * 2 - 2;
}
//-----------------------------------------------------------------------------
void RandomInitAlgorithmParameters(AlgorithmParameters (¶meters)[2])
{
SYSTEMTIME lpSystemTime;
GetSystemTime(&lpSystemTime);
srand(unsigned(lpSystemTime.wSecond) * 1000 + lpSystemTime.wMilliseconds);
unsigned numTransformation = (maxNumTransformation > minNumTransformation) ?
CoolRand(maxNumTransformation - minNumTransformation) + minNumTransformation :
1;
RandomInitAlgorithmParameters(parameters[0], numTransformation);
RandomInitAlgorithmParameters(parameters[1], numTransformation);
}
//-----------------------------------------------------------------------------
bool SearchSubstitution(const Block &block, unsigned &position)
{
unsigned blockSize = block.GetSize();
_ASSERT(position <= blockSize);
for (; position < blockSize; position++)
{
int a = block[position];
for (unsigned i = 0; i < AllSubstitutionSize; i++)
if (AllSubstitution[i] == a)
return true;
}
return false;
}
//-----------------------------------------------------------------------------
struct SAppropriate
{
bool testSize;
unsigned maxSize;
bool testContain;
const int *shouldContain;
unsigned numShouldContain;
bool testNotContain;
unsigned numNotShouldContain;
const int *notShouldContain;
SAppropriate() : testSize(false), maxSize(0),
testContain(false), shouldContain(NULL), numShouldContain(0),
testNotContain(false), notShouldContain(NULL), numNotShouldContain(0)
{ }
~SAppropriate() { }
};
//-----------------------------------------------------------------------------
bool Search(const int &block, unsigned blockSize, int needFind)
{
_ASSERT(blockSize);
unsigned j;
for (j = 0; j < blockSize; j++)
if ((&block)[j] == needFind)
break;
return !(j == blockSize);
}
//-----------------------------------------------------------------------------
bool SearchRandomAppropriateBlock(unsigned numberBlockForTest,
const unsigned &blocksSize,
const int * const &addrBlocks,
const SAppropriate &appropriate,
unsigned &retAppropriateBlock)
{
_ASSERT(numberBlockForTest);
CkgSimpleContainerArray
for (unsigned i = 0; i < numberBlockForTest; i++)
{
unsigned blockSize = (&blocksSize)[i];
_ASSERT(blockSize);
if (appropriate.testSize && blockSize > appropriate.maxSize)
continue;
const int *block = (&addrBlocks)[i];
_ASSERT(block);
if (appropriate.testContain)
{
_ASSERT(appropriate.shouldContain && appropriate.numShouldContain);
bool found = true;
for (unsigned j = 0; j < appropriate.numShouldContain && found; j++)
{
int needFind = appropriate.shouldContain[j];
found = Search(*block, blockSize, needFind);
}
if (!found)
continue;
}
if (appropriate.testNotContain)
{
_ASSERT(appropriate.notShouldContain && appropriate.numNotShouldContain);
bool found = false;
for (unsigned j = 0; j < appropriate.numNotShouldContain && !found; j++)
{
int needFind = appropriate.notShouldContain[j];
found = Search(*block, blockSize, needFind);
}
if (found)
continue;
}
convenianceSample.Add(i);
}
unsigned numConvenianceSample = convenianceSample.GetSize();
if (!numConvenianceSample)
return false;
unsigned select = numConvenianceSample > 1 ?
CoolRand(numConvenianceSample) :
0;
retAppropriateBlock = convenianceSample[select];
return true;
}
//-----------------------------------------------------------------------------
bool ReplacemenRandomEntity(int &entity, const AlgorithmParameters ¶meters)
{
switch (entity)
{
case ES_RANDOM_NOP: {
unsigned NumNops = LAST_DEFINED_NOP - EO_NOP_0;
entity = CoolRand(NumNops);
entity += EO_NOP_0;
_ASSERT(entity >= EO_NOP_0 && entity <= LAST_DEFINED_NOP);
break;
}
case ES_RANDOM_CONST: entity = CoolRand(); break;
case ES_RANDOM_REG: entity = GetRandomNonUsedRegister(parameters); break;
case ES_RANDOM_VAR: entity = GetRandomNonUsedVariable(parameters); break;
case ES_RANDOM_USE_REG: entity = CoolRand(registersNum); break;
case ES_RANDOM_USE_VAR: entity = CoolRand(variablesNum); break;
default: return false;
}
return true;
}
//-----------------------------------------------------------------------------
void
CreateNopBlock(Block &block, AlgorithmParameters ¶meters,
cpp_arr_ptr
unsigned maxSize)
{
_ASSERT(block.IsEmpty());
if (!maxSize)
CkgExcept();
SAppropriate appropriate;
appropriate.testSize = true;
appropriate.maxSize = maxSize;
unsigned index;
if (!SearchRandomAppropriateBlock(sizeBlocksArray_Nops, *sizeBlocks_Nops,
*blocks_Nops, appropriate, index))
CkgExcept();
for (unsigned i = 0; i < sizeBlocks_Nops[index]; i++)
block.Add(blocks_Nops[index][i]);
unsigned position = 0;
while (SearchSubstitution(block, position))
{
if (!ReplacemenRandomEntity(block.GetRef(position), parameters))
CkgExcept();
DEBUG_TEST_BLOCK(block.Get(position));
position++;
}
}
//-----------------------------------------------------------------------------
void
CreateBlock0_(Block &block, AlgorithmParameters ¶meters,
cpp_arr_ptr
{
_ASSERT(block.IsEmpty());
SAppropriate appropriate;
appropriate.testContain = true;
appropriate.numShouldContain = 1;
int whatSearch = parameters.addressIsReg ? ES_REG_0 : ES_VARIABLE_0;
appropriate.shouldContain = &whatSearch;
unsigned index;
if (!SearchRandomAppropriateBlock(sizeBlocksArray_00, *sizeBlocks_00,
*blocks_00, appropriate, index))
CkgExcept();
for (unsigned i = 0; i < sizeBlocks_00[index]; i++)
block.Add(blocks_00[index][i]);
unsigned position = 0;
while (SearchSubstitution(block, position))
{
if (!ReplacemenRandomEntity(block.GetRef(position), parameters))
{
int newValue = EO_ERROR;
int b = block[position];
switch (b)
{
case ES_REG_0: _ASSERT(parameters.addressIsReg);
newValue = parameters.addressNumber;
break;
case ES_VARIABLE_0: _ASSERT(!parameters.addressIsReg);
newValue = parameters.addressNumber;
break;
default: CkgExcept();
}
block.GetRef(position) = newValue;
}
DEBUG_TEST_BLOCK(block.Get(position));
position++;
}
}
//-----------------------------------------------------------------------------
void
CreateBlock0(Block block[], AlgorithmParameters parameters[],
cpp_arr_ptr
{
CreateBlock0_(block[0], parameters[0], vars[0]);
CreateBlock0_(block[1], parameters[1], vars[1]);
}
//-----------------------------------------------------------------------------
void
CreateBlock1_(Block &block, AlgorithmParameters ¶meters,
cpp_arr_ptr
{
_ASSERT(block.IsEmpty());
SAppropriate appropriate;
appropriate.testContain = true;
appropriate.numShouldContain = 1;
int whatSearch = parameters.counterIsReg ? ES_REG_0 : ES_VARIABLE_0;
appropriate.shouldContain = &whatSearch;
unsigned index;
if (!SearchRandomAppropriateBlock(sizeBlocksArray_01, *sizeBlocks_01,
*blocks_01, appropriate, index))
CkgExcept();
for (unsigned i = 0; i < sizeBlocks_01[index]; i++)
block.Add(blocks_01[index][i]);
unsigned position = 0;
while (SearchSubstitution(block, position))
{
if (!ReplacemenRandomEntity(block.GetRef(position), parameters))
{
int newValue = EO_ERROR;
int b = block[position];
switch (b)
{
case ES_REG_0: _ASSERT(parameters.counterIsReg);
newValue = parameters.counterNumber;