optimize ECB/CBC modes
parent
3dfcdf8c44
commit
3bc56fe4c4
47
modes.cpp
47
modes.cpp
|
|
@ -94,51 +94,40 @@ void BlockOrientedCipherModeBase::UncheckedSetKey(const byte *key, unsigned int
|
||||||
|
|
||||||
void BlockOrientedCipherModeBase::ProcessData(byte *outString, const byte *inString, size_t length)
|
void BlockOrientedCipherModeBase::ProcessData(byte *outString, const byte *inString, size_t length)
|
||||||
{
|
{
|
||||||
|
if (!length)
|
||||||
|
return;
|
||||||
|
|
||||||
unsigned int s = BlockSize();
|
unsigned int s = BlockSize();
|
||||||
assert(length % s == 0);
|
assert(length % s == 0);
|
||||||
unsigned int alignment = m_cipher->BlockAlignment();
|
|
||||||
bool inputAlignmentOk = !RequireAlignedInput() || IsAlignedOn(inString, alignment);
|
|
||||||
|
|
||||||
if (IsAlignedOn(outString, alignment))
|
if (!RequireAlignedInput() || IsAlignedOn(inString, m_cipher->BlockAlignment()))
|
||||||
{
|
ProcessBlocks(outString, inString, length / s);
|
||||||
if (inputAlignmentOk)
|
|
||||||
ProcessBlocks(outString, inString, length / s);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
memcpy(outString, inString, length);
|
|
||||||
ProcessBlocks(outString, outString, length / s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (length)
|
do
|
||||||
{
|
{
|
||||||
if (inputAlignmentOk)
|
memcpy(m_buffer, inString, s);
|
||||||
ProcessBlocks(m_buffer, inString, 1);
|
ProcessBlocks(outString, m_buffer, 1);
|
||||||
else
|
|
||||||
{
|
|
||||||
memcpy(m_buffer, inString, s);
|
|
||||||
ProcessBlocks(m_buffer, m_buffer, 1);
|
|
||||||
}
|
|
||||||
memcpy(outString, m_buffer, s);
|
|
||||||
inString += s;
|
inString += s;
|
||||||
outString += s;
|
outString += s;
|
||||||
length -= s;
|
length -= s;
|
||||||
}
|
} while (length > 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CBC_Encryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
|
void CBC_Encryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
|
||||||
{
|
{
|
||||||
unsigned int blockSize = BlockSize();
|
unsigned int blockSize = BlockSize();
|
||||||
while (numberOfBlocks--)
|
xorbuf(m_register, inString, blockSize);
|
||||||
|
while (--numberOfBlocks)
|
||||||
{
|
{
|
||||||
xorbuf(m_register, inString, blockSize);
|
m_cipher->ProcessBlock(m_register, outString);
|
||||||
m_cipher->ProcessBlock(m_register);
|
|
||||||
memcpy(outString, m_register, blockSize);
|
|
||||||
inString += blockSize;
|
inString += blockSize;
|
||||||
|
xorbuf(m_register, inString, outString, blockSize);
|
||||||
outString += blockSize;
|
outString += blockSize;
|
||||||
}
|
}
|
||||||
|
m_cipher->ProcessBlock(m_register);
|
||||||
|
memcpy(outString, m_register, blockSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
|
void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
|
||||||
|
|
@ -171,14 +160,14 @@ void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString,
|
||||||
void CBC_Decryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
|
void CBC_Decryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
|
||||||
{
|
{
|
||||||
unsigned int blockSize = BlockSize();
|
unsigned int blockSize = BlockSize();
|
||||||
while (numberOfBlocks--)
|
do
|
||||||
{
|
{
|
||||||
memcpy(m_temp, inString, blockSize);
|
memcpy(m_temp, inString, blockSize); // make copy in case we're doing in place decryption
|
||||||
m_cipher->ProcessAndXorBlock(m_temp, m_register, outString);
|
m_cipher->ProcessAndXorBlock(m_temp, m_register, outString);
|
||||||
m_register.swap(m_temp);
|
m_register.swap(m_temp);
|
||||||
inString += blockSize;
|
inString += blockSize;
|
||||||
outString += blockSize;
|
outString += blockSize;
|
||||||
}
|
} while (--numberOfBlocks);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
|
void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue