Exemplo n.º 1
0
void AdditiveCipherTemplate<S>::GenerateBlock(byte *outString, size_t length)
{
	if (m_leftOver > 0)
	{
		const size_t len = STDMIN(m_leftOver, length);
		memcpy(outString, PtrSub(KeystreamBufferEnd(), m_leftOver), len);

		length -= len; m_leftOver -= len;
		outString = PtrAdd(outString, len);
		if (!length) {return;}
	}

	PolicyInterface &policy = this->AccessPolicy();
	unsigned int bytesPerIteration = policy.GetBytesPerIteration();

	if (length >= bytesPerIteration)
	{
		const size_t iterations = length / bytesPerIteration;
		policy.WriteKeystream(outString, iterations);
		length -= iterations * bytesPerIteration;
		outString = PtrAdd(outString, iterations * bytesPerIteration);
	}

	if (length > 0)
	{
		size_t bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
		size_t bufferIterations = bufferByteSize / bytesPerIteration;

		policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
		memcpy(outString, PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
		m_leftOver = bufferByteSize - length;
	}
}
Exemplo n.º 2
0
void AdditiveCipherTemplate<S>::ProcessData(byte *outString, const byte *inString, size_t length)
{
	if (m_leftOver > 0)
	{
		size_t len = STDMIN(m_leftOver, length);
		xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
		length -= len;
		m_leftOver -= len;
		inString += len;
		outString += len;

		if (!length)
			return;
	}
	CRYPTOPP_ASSERT(m_leftOver == 0);

	PolicyInterface &policy = this->AccessPolicy();
	unsigned int bytesPerIteration = policy.GetBytesPerIteration();

	if (policy.CanOperateKeystream() && length >= bytesPerIteration)
	{
		size_t iterations = length / bytesPerIteration;
		unsigned int alignment = policy.GetAlignment();
		KeystreamOperation operation = KeystreamOperation((IsAlignedOn(inString, alignment) * 2) | (int)IsAlignedOn(outString, alignment));

		policy.OperateKeystream(operation, outString, inString, iterations);

		inString += iterations * bytesPerIteration;
		outString += iterations * bytesPerIteration;
		length -= iterations * bytesPerIteration;

		if (!length)
			return;
	}

	size_t bufferByteSize = m_buffer.size();
	size_t bufferIterations = bufferByteSize / bytesPerIteration;

	while (length >= bufferByteSize)
	{
		policy.WriteKeystream(m_buffer, bufferIterations);
		xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
		length -= bufferByteSize;
		inString += bufferByteSize;
		outString += bufferByteSize;
	}

	if (length > 0)
	{
		bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
		bufferIterations = bufferByteSize / bytesPerIteration;

		policy.WriteKeystream(KeystreamBufferEnd()-bufferByteSize, bufferIterations);
		xorbuf(outString, inString, KeystreamBufferEnd()-bufferByteSize, length);
		m_leftOver = bufferByteSize - length;
	}
}
Exemplo n.º 3
0
inline void AdditiveCipherTemplate<S>::ProcessData(byte *outString, const byte *inString, unsigned int length)
{
	if (m_leftOver > 0)
	{
		unsigned int len = STDMIN(m_leftOver, length);
		xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
		length -= len;
		m_leftOver -= len;
		inString += len;
		outString += len;
	}

	if (!length)
		return;

	assert(m_leftOver == 0);

	PolicyInterface &policy = this->AccessPolicy();
	unsigned int bytesPerIteration = policy.GetBytesPerIteration();
	unsigned int alignment = policy.GetAlignment();

	if (policy.CanOperateKeystream() && length >= bytesPerIteration && IsAlignedOn(outString, alignment))
	{
		if (IsAlignedOn(inString, alignment))
			policy.OperateKeystream(XOR_KEYSTREAM, outString, inString, length / bytesPerIteration);
		else
		{
			memcpy(outString, inString, length);
			policy.OperateKeystream(XOR_KEYSTREAM_INPLACE, outString, outString, length / bytesPerIteration);
		}
		inString += length - length % bytesPerIteration;
		outString += length - length % bytesPerIteration;
		length %= bytesPerIteration;

		if (!length)
			return;
	}

	unsigned int bufferByteSize = GetBufferByteSize(policy);
	unsigned int bufferIterations = policy.GetIterationsToBuffer();

	while (length >= bufferByteSize)
	{
		policy.WriteKeystream(m_buffer, bufferIterations);
		xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
		length -= bufferByteSize;
		inString += bufferByteSize;
		outString += bufferByteSize;
	}

	if (length > 0)
	{
		policy.WriteKeystream(m_buffer, bufferIterations);
		xorbuf(outString, inString, KeystreamBufferBegin(), length);
		m_leftOver = bytesPerIteration - length;
	}
}
Exemplo n.º 4
0
byte AdditiveCipherTemplate<S>::GenerateByte()
{
	PolicyInterface &policy = this->AccessPolicy();

	if (m_leftOver == 0)
	{
		policy.WriteKeystream(m_buffer, policy.GetIterationsToBuffer());
		m_leftOver = policy.GetBytesPerIteration();
	}

	return *(KeystreamBufferEnd()-m_leftOver--);
}
Exemplo n.º 5
0
void AdditiveCipherTemplate<S>::GenerateBlock(byte *outString, size_t length)
{
	if (m_leftOver > 0)
	{
		size_t len = STDMIN(m_leftOver, length);
		memcpy(outString, KeystreamBufferEnd()-m_leftOver, len);
		length -= len;
		m_leftOver -= len;
		outString += len;

		if (!length)
			return;
	}
	CRYPTOPP_ASSERT(m_leftOver == 0);

	PolicyInterface &policy = this->AccessPolicy();
	unsigned int bytesPerIteration = policy.GetBytesPerIteration();

	if (length >= bytesPerIteration)
	{
		size_t iterations = length / bytesPerIteration;
		policy.WriteKeystream(outString, iterations);
		outString += iterations * bytesPerIteration;
		length -= iterations * bytesPerIteration;
	}

	if (length > 0)
	{
		size_t bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
		size_t bufferIterations = bufferByteSize / bytesPerIteration;

		policy.WriteKeystream(KeystreamBufferEnd()-bufferByteSize, bufferIterations);
		memcpy(outString, KeystreamBufferEnd()-bufferByteSize, length);
		m_leftOver = bufferByteSize - length;
	}
}
Exemplo n.º 6
0
void AdditiveCipherTemplate<BASE>::Seek(lword position)
{
	PolicyInterface &policy = this->AccessPolicy();
	unsigned int bytesPerIteration = policy.GetBytesPerIteration();

	policy.SeekToIteration(position / bytesPerIteration);
	position %= bytesPerIteration;

	if (position > 0)
	{
		policy.WriteKeystream(KeystreamBufferEnd()-bytesPerIteration, 1);
		m_leftOver = bytesPerIteration - (unsigned int)position;
	}
	else
		m_leftOver = 0;
}
Exemplo n.º 7
0
void AdditiveCipherTemplate<BASE>::Seek(lword position)
{
	PolicyInterface &policy = this->AccessPolicy();
	word32 bytesPerIteration = policy.GetBytesPerIteration();

	policy.SeekToIteration(position / bytesPerIteration);
	position %= bytesPerIteration;

	if (position > 0)
	{
		policy.WriteKeystream(PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
		m_leftOver = bytesPerIteration - static_cast<word32>(position);
	}
	else
		m_leftOver = 0;
}
Exemplo n.º 8
0
void AdditiveCipherTemplate<S>::GenerateBlock(byte *outString, size_t length)
{
	if (m_leftOver > 0)
	{
		size_t len = STDMIN(m_leftOver, length);
		memcpy(outString, KeystreamBufferEnd()-m_leftOver, len);
		length -= len;
		m_leftOver -= len;
		outString += len;

		if (!length)
			return;
	}
	assert(m_leftOver == 0);

	PolicyInterface &policy = this->AccessPolicy();
	unsigned int bytesPerIteration = policy.GetBytesPerIteration();

	if (length >= bytesPerIteration)
	{
		size_t iterations = length / bytesPerIteration;
		policy.WriteKeystream(outString, iterations);
		outString += iterations * bytesPerIteration;
		length -= iterations * bytesPerIteration;

		if (!length)
			return;
	}

	unsigned int bufferByteSize = GetBufferByteSize(policy);
	unsigned int bufferIterations = policy.GetIterationsToBuffer();

	while (length >= bufferByteSize)
	{
		policy.WriteKeystream(m_buffer, bufferIterations);
		memcpy(outString, KeystreamBufferBegin(), bufferByteSize);
		length -= bufferByteSize;
		outString += bufferByteSize;
	}

	if (length > 0)
	{
		policy.WriteKeystream(m_buffer, bufferIterations);
		memcpy(outString, KeystreamBufferBegin(), length);
		m_leftOver = bytesPerIteration - length;
	}
}