예제 #1
0
void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
	AssertValidKeyLength(length);

	static const word32 offset[ROUNDS] = {
	0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
	0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL,
	};

	GetUserKey(BIG_ENDIAN_ORDER, roundkeys[0], KEYLENGTH/4, userKey, KEYLENGTH);

	/* apply the key evolution function */
	for (int i = 1; i < ROUNDS+1; i++)
	{
		roundkeys[i][0] = roundkeys[i-1][0] ^ rotlFixed(roundkeys[i-1][3], 8U) ^ offset[i-1];
		roundkeys[i][1] = roundkeys[i-1][1] ^ roundkeys[i][0];
		roundkeys[i][2] = roundkeys[i-1][2] ^ roundkeys[i][1];
		roundkeys[i][3] = roundkeys[i-1][3] ^ roundkeys[i][2];
	}  

	/* produce the round keys */
	if (IsForwardTransformation())
	{
		for (int i = 0; i < ROUNDS; i++)
			SquareTransform (roundkeys[i], roundkeys[i]);
	}
	else
	{
		for (int i = 0; i < ROUNDS/2; i++)
			for (int j = 0; j < 4; j++)
				std::swap(roundkeys[i][j], roundkeys[ROUNDS-i][j]);
		SquareTransform (roundkeys[ROUNDS], roundkeys[ROUNDS]);
	}
}
void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
{
	AssertValidKeyLength(length);

	static const word32 offset[ROUNDS] = {
	0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
	0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL,
	};

	GetUserKey(BIG_ENDIAN_ORDER, m_roundkeys.data(), KEYLENGTH/4, userKey, KEYLENGTH);

	/* apply the key evolution function */
	for (int i = 1; i < ROUNDS+1; i++)
	{
		roundkeys(i, 0) = roundkeys(i-1, 0) ^ rotlFixed(roundkeys(i-1, 3), 8U) ^ offset[i-1];
		roundkeys(i, 1) = roundkeys(i-1, 1) ^ roundkeys(i, 0);
		roundkeys(i, 2) = roundkeys(i-1, 2) ^ roundkeys(i, 1);
		roundkeys(i, 3) = roundkeys(i-1, 3) ^ roundkeys(i, 2);
	}  

	/* produce the round keys */
	if (IsForwardTransformation())
	{
		for (int i = 0; i < ROUNDS; i++)
			SquareTransform (roundkeys4(i), roundkeys4(i));
	}
	else
	{
		for (int i = 0; i < ROUNDS/2; i++)
			for (int j = 0; j < 4; j++)
				std::swap(roundkeys(i, j), roundkeys(ROUNDS-i, j));
		SquareTransform (roundkeys4(ROUNDS), roundkeys4(ROUNDS));
	}
}