Пример #1
0
void GOSTTests::testHash()
{
	// Get an RNG and GOST R 34.11-94 hash instance
	CPPUNIT_ASSERT((rng = CryptoFactory::i()->getRNG()) != NULL);
	CPPUNIT_ASSERT((hash = CryptoFactory::i()->getHashAlgorithm("gost")) != NULL);

	// Generate some random input data
	ByteString b;
	ByteString osslHash, gostHash;

	CPPUNIT_ASSERT(rng->generateRandom(b, 53287));

	// Write it to file
	writeTmpFile(b);

	// Use OpenSSL externally to hash it
#ifndef _WIN32
	CPPUNIT_ASSERT(system("openssl dgst -engine gost -md_gost94 -binary < gost-hashtest.tmp > gost-hashtest-out.tmp 2> /dev/null") == 0);
#else
	CPPUNIT_ASSERT(system("openssl dgst -engine gost -md_gost94 -binary < gost-hashtest.tmp > gost-hashtest-out.tmp 2> nul") == 0);
#endif

	// Read the hash from file
	readTmpFile(osslHash);

	// Now recreate the hash using our implementation in a single operation
	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b));
	CPPUNIT_ASSERT(hash->hashFinal(gostHash));

	CPPUNIT_ASSERT(osslHash == gostHash);

	// Now recreate the hash in a single part operation
	gostHash.wipe();

	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(0, 567)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567, 989)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567 + 989)));
	CPPUNIT_ASSERT(hash->hashFinal(gostHash));

	CPPUNIT_ASSERT(osslHash == gostHash);

	CryptoFactory::i()->recycleHashAlgorithm(hash);

	hash = NULL;
	rng = NULL;
}
Пример #2
0
void HashTests::testMD5()
{
	// Get an RNG and MD5 hash instance
	CPPUNIT_ASSERT((rng = CryptoFactory::i()->getRNG()) != NULL);
	CPPUNIT_ASSERT((hash = CryptoFactory::i()->getHashAlgorithm("md5")) != NULL);

	// Generate some random input data
	ByteString b;
	ByteString osslHash, shsmHash;

	CPPUNIT_ASSERT(rng->generateRandom(b, 52237));

	// Write it to file
	writeTmpFile(b);

	// Use OpenSSL externally to hash it
	CPPUNIT_ASSERT(system("cat shsmv2-hashtest.tmp | openssl md5 -binary > shsmv2-hashtest-out.tmp") == 0);

	// Read the hash from file
	readTmpFile(osslHash);

	// Now recreate the hash using our implementation in a single operation
	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b));
	CPPUNIT_ASSERT(hash->hashFinal(shsmHash));

	CPPUNIT_ASSERT(osslHash == shsmHash);

	// Now recreate the hash in a multiple part operation
	shsmHash.wipe();

	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(0, 567)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567, 989)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567 + 989)));
	CPPUNIT_ASSERT(hash->hashFinal(shsmHash));

	CPPUNIT_ASSERT(osslHash == shsmHash);

	CryptoFactory::i()->recycleHashAlgorithm(hash);

	hash = NULL;
	rng = NULL;
}
Пример #3
0
void Game::play()
{
	long maxp = gameSettings->getMaxp();
	long maxl = gameSettings->getMaxl();
	long maxg = gameSettings->getMaxg();
	int level = gameSettings->getLevel();
	int preview = gameSettings->getPreview();
	int stat = gameSettings->getStat();
	int size = gameSettings->getSize();
	int sliding = gameSettings->getSliding();
	int delay = gameSettings->getDelay();
	int animate = gameSettings->getAnimate();
	int exit = gameSettings->getExit();
	int pieceStartX = gameSettings->getPieceStartX();
	int frameRateDelay = gameSettings->getFrameRateDelay();
	int nextPiece = 0;
	int viewBoard = gameSettings->getViewBoard();
	int stepMode = gameSettings->getStepMode();
	milisec1 = GetTickCount();
	milisec2 = GetTickCount();
	seconds = time(NULL);

	if (strlen(gameSettings->getWgameFilename()) != 0)
	{
		if (wGameFile.open(gameSettings->getWgameFilename()))
		{
			cout << "Could not open the file  \"" << gameSettings->getWgameFilename() << "\"" << endl;

			return;
		}
	}

	if (strlen(gameSettings->getRgameFilename()) != 0)
	{
		if (rGameFile.open(gameSettings->getRgameFilename()))
		{
			cout << "Could not open the file  \"" << gameSettings->getRgameFilename() << "\"" << endl;

			return;
		}
	}

	if (strlen(gameSettings->getWhtmlFilename()) != 0)
	{
		wHtml.open(gameSettings->getWhtmlFilename());

		if (!wHtml.good())
		{
			cout << "Could not open the file  \"" << gameSettings->getWhtmlFilename() << "\"" << endl;

			return;
		}
	}

  board = new Board(gameSettings->getWidth(), gameSettings->getHeight(), sliding, pieceStartX);

	Piece setStaticInitializersPiece(board, 1, sliding, 1);

	Player *player = new Player(board);

	Random random = Random(gameSettings->getSeed());
	GameInfo gameInfo(maxCntblockstat);

  writeTmpFile(board);

	double equity = player->evaluate();
	double maxDiff = 0;

	int pieces[10];

	if (preview > 1)
		pieces[0] = random.getRandomP();

	pieces[1] = 0;
	pieces[2] = 0;
	pieces[3] = 0;
	pieces[4] = 0;
	pieces[5] = 0;
	pieces[6] = 0;
	pieces[7] = 0;
	pieces[8] = 0;
	pieces[9] = 0;

	if (wGameFile.isOpen())
	{
		// 1. Version of the file
		wGameFile.writeShort(1);													// # Version (short)
		
		// 2. Header
		wGameFile.writeShort(DATA_HEADER);								// # Type of block
		wGameFile.writeShort(1);													// # Version
		wGameFile.writeShort(gameSettings->getWidth());		// # Width (short)
		wGameFile.writeShort(gameSettings->getHeight());	// # Height (short)
		wGameFile.writeInt(gameSettings->getSeed());			// # Seed (int)
		wGameFile.writeByte(gameSettings->getLevel());		// # Level (byte)
		wGameFile.writeByte(gameSettings->getPreview());	// # Preview (byte)
		wGameFile.writeByte(pieces[0]);      							// # Piece (byte)
	}

	while ((maxg==0 && maxl==0 && maxp==0) || 
		     ((maxg > 0 && games < maxg) ||
		      (maxl > 0 && totalLines < maxl) ||
				  (maxp > 0 && totalPieces < maxp)))
	{
		if (exit == 1 || gameSettings->getExit() == 1)
			break;

		//-------------------------------------------
		if (stepMode != gameSettings->getStepMode()) {
			stepMode = gameSettings->getStepMode();
			initTime();
		}
		
		if (level != gameSettings->getLevel()) {
			level = gameSettings->getLevel();
			initTime();
		}

		if (sliding != gameSettings->getSliding())
		{
			sliding = gameSettings->getSliding();
			board->setSliding(sliding);
			initTime();
		}

		if (preview != gameSettings->getPreview())
		{
			preview = gameSettings->getPreview();

			if (preview == 1)
				pieces[1] = 0;
			else
			{
				if (nextPiece == 0)
					nextPiece = pieces[1] = random.getRandomP();
				else
					pieces[1] = nextPiece;
			}

			initTime();
		}

		if (animate != gameSettings->getAnimate()) {
			animate = gameSettings->getAnimate();
			initTime();
		}

		if (delay != gameSettings->getDelay()) {
			delay = gameSettings->getDelay();
			initTime();
		}

		if (frameRateDelay != gameSettings->getFrameRateDelay()) {
			frameRateDelay = gameSettings->getFrameRateDelay();
			initTime();
		}

		if (viewBoard != gameSettings->getViewBoard()) {
			viewBoard = gameSettings->getViewBoard();
			initTime();
		}

		while (gameSettings->isPause())
			::Sleep(10);

		if (board->getWidth() != gameSettings->getWidth())
		{
			delete board;
			pieceStartX = gameSettings->getPieceStartX();
			board = new Board(gameSettings->getWidth(), gameSettings->getHeight(), sliding, pieceStartX);
			Piece setStaticInitializersPiece(board, 1, sliding, 1);			
		}
		//-------------------------------------------

		if (preview > 1)
			nextPiece = pieces[1] = random.getRandomP();
		else
			pieces[0] = random.getRandomP();

		if (wGameFile.isOpen())
			wGameFile.writeByte(pieces[1]);									// # Piece

		Piece bestPiece(board, pieces[0]);


		gameObserverList.notify(board, 
														&gameInfo, 
														pieces[0], 
														pieces[1], 
														1, 
														milisec1,
														milisec2,
														cntblockstat, 
														games,
														cntPieces,
														cntLines,
														totalMoves,
														totalSlided,
														totalLines,
														totalLines2,
														totalPieces,
														totalPieces2,
														minLines,
														maxLines,
														linesPerGame);


		if (!animate)
			::Sleep(delay*10);

		int playAgain;
		Move *bestMove;
		MoveList *moveList;

		do
		{
			playAgain = 0;
			gameSettings->setIsThinking();

			if (animate != gameSettings->getAnimate()) {
				animate = gameSettings->getAnimate();
				initTime();
			}

			bestMove = player->play(pieces, level, preview, animate, &gameObserverList);

			if (bestMove > 0)
				bestPiece.set(bestMove->getV(), bestMove->getX(), bestMove->getY(), bestMove->getClearedLines());

			gameSettings->setIsNotThinking();

			moveList = board->getMoveList(level);

			gameObserverList.notify(moveList);

			while (gameSettings->isStepModeOn() && gameSettings->isStepWait())
			{
				if (level != gameSettings->getLevel()) {
					level = gameSettings->getLevel();
					initTime();
					playAgain = 1;
					break;
				}

				if (preview != gameSettings->getPreview())
				{
					preview = gameSettings->getPreview();

					if (preview == 1)
						pieces[1] = 0;
					else
					{
						if (nextPiece == 0)
							nextPiece = pieces[1] = random.getRandomP();
						else
							pieces[1] = nextPiece;
					}

					initTime();
					playAgain = 1;

					gameObserverList.notify(board, 
																	&gameInfo, 
																	pieces[0], 
																	pieces[1], 
																	1, 
																	milisec1,
																	milisec2,
																	cntblockstat, 
																	games,
																	cntPieces,
																	cntLines,
																	totalMoves,
																	totalSlided,
																	totalLines,
																	totalLines2,
																	totalPieces,
																	totalPieces2,
																	minLines,
																	maxLines,
																	linesPerGame);



					break;
				}

				if (sliding != gameSettings->getSliding() && bestMove > 0)
				{
					sliding = gameSettings->getSliding();
					board->setSliding(sliding);

					initTime();
					playAgain = 1;
					break;
				}

				::Sleep(10);
			}
		} while (playAgain);

		if (gameSettings->isStepModeOn())
			gameSettings->setStepWait();

		if (!animate && bestMove > 0)
		{
			bestPiece.setPiece();

			if (bestPiece.getClearedLines() > 0)
				board->clearLines(bestPiece.getY(), bestPiece.getHeight());
		}

		Move *move;
		int index = 0;

		int length = moveList->getLength();

		totalMoves += length;

		if (animate && bestMove > 0)
		{
			// Animate the moves.
			MovePath *movePath = board->getMovePath(level);
			MovePathStep *s = movePath->getPath(bestMove->getV(), bestMove->getX(), bestMove->getY());

			int skipFirst = 0;

			int prevy = -1;

			while (s > 0)
			{
				//----
				// Calculate the number of steps for the current line (y value).
				MovePathStep *s1 = s;
				int y1 = s1->y;
				int cnt;

				if (prevy != y1)
				{
					cnt = 0;

					while (s1 > 0 && s1->y == y1) {
						cnt++;
						s1 = s1->next;
					}
					prevy = y1;
				}
				//----
				
				if (animate != gameSettings->getAnimate())
					break;

				if (delay != gameSettings->getDelay())
					delay = gameSettings->getDelay();

				int v = s->v;
				int x = s->x;
				int y = s->y;

				Piece piece(board, bestPiece.getP());
				piece.setPiece(v, x, y);

				if (skipFirst)
					gameObserverList.notify(board, 
																	&gameInfo, 
																	pieces[0], 
																	pieces[1], 
																	0, 
																	milisec1,
																	milisec2,
																	cntblockstat, 
																	games,
																	cntPieces,
																	cntLines,
																	totalMoves,
																	totalSlided,
																	totalLines,
																	totalLines2,
																	totalPieces,
																	totalPieces2,
																	minLines,
																	maxLines,
																	linesPerGame);

				if (gameSettings->isSlidingOn())
					::Sleep((double)delay / cnt);
				else
					::Sleep((double)delay);

				piece.clearPiece();
				skipFirst = 1;
				s = s->next;

				while (gameSettings->isPause())
					::Sleep(10);
			}

			bestPiece.setPiece();

			if (bestPiece.getClearedLines() > 0)
			{
				int clearedLinesBits = board->getClearedLines(bestPiece.getY(), bestPiece.getHeight());

				for (int n=0; n<4; n++)
				{
					board->flashLines(bestPiece.getY(), bestPiece.getHeight(), clearedLinesBits);
					
					if (n<3)
					{
						gameObserverList.notify(board, 
																		&gameInfo, 
																		pieces[0], 
																		pieces[1], 
																		0, 
																		milisec1,
																		milisec2,
																		cntblockstat, 
																		games,
																		cntPieces,
																		cntLines,
																		totalMoves,
																		totalSlided,
																		totalLines,
																		totalLines2,
																		totalPieces,
																		totalPieces2,
																		minLines,
																		maxLines,
																		linesPerGame);

						if (delay > 0)
						{
							if (delay >= 50)
								::Sleep(150);
							else
								::Sleep(150-(50-delay)*2);
						}
					}
				}

				board->clearLines(bestPiece.getY(), bestPiece.getHeight());
			}
		}

		cntPieces++;
		totalPieces++;
		
		if (bestMove > 0)
			totalSlided += bestMove->isSlided();

		// Log the moves
		if (wGameFile.isOpen())
		{
			wGameFile.writeShort(length);											// # Number of legal moves.
			wGameFile.writeByte(bestPiece.getV());						// # V
			wGameFile.writeShort(bestPiece.getX());						// # X
			wGameFile.writeShort(bestPiece.getY());						// # Y

			move = moveList->getFirstMove();

			while (move != 0)
			{																									// Move:
				wGameFile.writeByte(move->getV());							// # V
				wGameFile.writeShort(move->getX());							// # X
				wGameFile.writeShort(move->getY());							// # Y

				for (int i=preview; i<=level; i++)							// Equity (e.g):
					wGameFile.writeFloat(move->getEquity(i));			// L2, L3, L4

				move = moveList->getNextMove();
			}
		}

		if (preview > 1)
			pieces[0] = pieces[1];

  	int clearedLines = bestPiece.getClearedLines();

  	if (bestMove == 0) 
		{
  		games++;
  		
			linesPerGame = totalLines/games;
		}
		else
		{
			cntLines += clearedLines;
			totalLines += clearedLines;

			cntBlock += 4 - clearedLines * board->getWidth();

			if (DEBUG == 0)
			{
				int xxx = cntBlock/4;
				if (xxx < maxCntblockstat)
					cntblockstat[xxx]++;
			}
		}

  	if (cntLines > maxLines)
  		maxLines = cntLines;

		if (bestMove == 0)
		{
  		if (cntLines < minLines || minLines==-1)
  			minLines = cntLines;
			
			cntBlock = 0;
			cntPieces = 0;
  		cntLines = 0;
		  board->clear();
			pieces[0] = random.getRandomP();
		}
	}

	delete board;
	delete player;

	if (wGameFile.isOpen())
	{
		wGameFile.writeByte(END_PIECE);				// # Piece
		wGameFile.close();
	}

	if (rGameFile.isOpen() && strlen(gameSettings->getWhtmlFilename()) > 0)
	{
		readGame(maxp, size);
		rGameFile.close();
	}

	gameSettings->setExit(2);
}
Пример #4
0
void DESTests::testCBC()
{
	char testKeys56[][17] =
	{
		"0000000000000000",
		"0102030405060708",
		"4041424344454647",
		"4698436794236871",
		"0940278947239572"
	};

	char testKeys112[][33] =
	{
		"00000000000000000000000000000000",
		"0102030405060708090A0B0C0D0E0F10",
		"404142434445464748494A4B4C4D4E4F",
		"64398647034486943598534703463870",
		"87406984068406984607412103517413"
	};
	
	char testKeys168[][49] =
	{
		"000000000000000000000000000000000000000000000000",
		"0102030405060708090A0B0C0D0E0F101112131415161718",
		"404142434445464748494A4B4C4D4E4F5051525354555657",
		"643906874509874309687459084769847562436043696747",
		"430135460496813044639085714376487549490586439575"
	};

	char testData[][256] =
	{
		"4938673409687134684698438657403986439058740935874395813968496846",
		"549813644389670948567490687546098245665626527788",
		"64398769586792586795867965624526",
		"468376458463264536"
	};

	char testIV[][33] =
	{
		"0000000000000000",
		"0102030405060708",
		"4041424344454647",
		"4693867334098764",
		"6209876098547207"
	};

	for (int i = 0; i < 5; i++)
	{
		char commandLine[2048];

		ByteString keyData56(testKeys56[i]);
		CPPUNIT_ASSERT(keyData56.size() == 8);
		ByteString keyData112(testKeys112[i]);
		CPPUNIT_ASSERT(keyData112.size() == 16);
		ByteString keyData168(testKeys168[i]);
		CPPUNIT_ASSERT(keyData168.size() == 24);

		DESKey desKey56(56);
		CPPUNIT_ASSERT(desKey56.setKeyBits(keyData56));
		DESKey desKey112(112);
		CPPUNIT_ASSERT(desKey112.setKeyBits(keyData112));
		DESKey desKey168(168);
		CPPUNIT_ASSERT(desKey168.setKeyBits(keyData168));

		ByteString IV(testIV[i]);

		for (int j = 0; j < 4; j++)
		{
			ByteString plainText(testData[j]), shsmPlainText;
			ByteString cipherText;
			ByteString shsmCipherText, OB;

			// Test 56-bit key

			// First, use the OpenSSL command line tool to encrypt the test data
			writeTmpFile(plainText);

			sprintf(commandLine, "openssl des-cbc -in shsmv2-destest.tmp -out shsmv2-destest-out.tmp -K %s -iv %s",
				testKeys56[i], testIV[i]);

			CPPUNIT_ASSERT(!system(commandLine));

			readTmpFile(cipherText);

			// Now, do the same thing using our DES implementation
			shsmCipherText.wipe();
			CPPUNIT_ASSERT(des->encryptInit(&desKey56, "cbc", IV));

			CPPUNIT_ASSERT(des->encryptUpdate(plainText, OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(des->encryptFinal(OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(shsmCipherText == cipherText);

			// Check that we can get the plain text
			shsmPlainText.wipe();
			CPPUNIT_ASSERT(des->decryptInit(&desKey56, "cbc", IV));

			CPPUNIT_ASSERT(des->decryptUpdate(shsmCipherText, OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(des->decryptFinal(OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(shsmPlainText == plainText);

			// Test 112-bit key

			// First, use the OpenSSL command line tool to encrypt the test data
			writeTmpFile(plainText);

			sprintf(commandLine, "openssl des-ede-cbc -in shsmv2-destest.tmp -out shsmv2-destest-out.tmp -K %s -iv %s",
				testKeys112[i], testIV[i]);

			CPPUNIT_ASSERT(!system(commandLine));

			readTmpFile(cipherText);

			// Now, do the same thing using our DES implementation
			shsmCipherText.wipe();
			CPPUNIT_ASSERT(des->encryptInit(&desKey112, "cbc", IV));

			CPPUNIT_ASSERT(des->encryptUpdate(plainText, OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(des->encryptFinal(OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(shsmCipherText == cipherText);

			// Check that we can get the plain text
			shsmPlainText.wipe();
			CPPUNIT_ASSERT(des->decryptInit(&desKey112, "cbc", IV));

			CPPUNIT_ASSERT(des->decryptUpdate(shsmCipherText, OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(des->decryptFinal(OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(shsmPlainText == plainText);

			// Test 168-bit key

			// First, use the OpenSSL command line tool to encrypt the test data
			writeTmpFile(plainText);

			sprintf(commandLine, "openssl des-ede3-cbc -in shsmv2-destest.tmp -out shsmv2-destest-out.tmp -K %s -iv %s",
				testKeys168[i], testIV[i]);

			CPPUNIT_ASSERT(!system(commandLine));

			readTmpFile(cipherText);

			// Now, do the same thing using our DES implementation
			shsmCipherText.wipe();
			CPPUNIT_ASSERT(des->encryptInit(&desKey168, "cbc", IV));

			CPPUNIT_ASSERT(des->encryptUpdate(plainText, OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(des->encryptFinal(OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(shsmCipherText == cipherText);

			// Check that we can get the plain text
			shsmPlainText.wipe();
			CPPUNIT_ASSERT(des->decryptInit(&desKey168, "cbc", IV));

			CPPUNIT_ASSERT(des->decryptUpdate(shsmCipherText, OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(des->decryptFinal(OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(shsmPlainText == plainText);
		}
	}
}