diff options
author | weidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0> | 2007-05-05 15:26:05 +0000 |
---|---|---|
committer | weidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0> | 2007-05-05 15:26:05 +0000 |
commit | d467bfa9fc99d7cedaf195673403956f1ed5ae71 (patch) | |
tree | a74b36953700ba1aaee667e0d03212af1ebc7556 /modes.cpp | |
parent | 621a2887f9ae17c5bd3a6e120031b4b0b8362381 (diff) | |
download | cryptopp-d467bfa9fc99d7cedaf195673403956f1ed5ae71.tar.gz |
optimize ECB/CBC modes
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@362 57ff6487-cd31-0410-9ec3-f628ee90f5f0
Diffstat (limited to 'modes.cpp')
-rw-r--r-- | modes.cpp | 47 |
1 files changed, 18 insertions, 29 deletions
@@ -94,51 +94,40 @@ void BlockOrientedCipherModeBase::UncheckedSetKey(const byte *key, unsigned int void BlockOrientedCipherModeBase::ProcessData(byte *outString, const byte *inString, size_t length) { + if (!length) + return; + unsigned int s = BlockSize(); assert(length % s == 0); - unsigned int alignment = m_cipher->BlockAlignment(); - bool inputAlignmentOk = !RequireAlignedInput() || IsAlignedOn(inString, alignment); - if (IsAlignedOn(outString, alignment)) - { - if (inputAlignmentOk) - ProcessBlocks(outString, inString, length / s); - else - { - memcpy(outString, inString, length); - ProcessBlocks(outString, outString, length / s); - } - } + if (!RequireAlignedInput() || IsAlignedOn(inString, m_cipher->BlockAlignment())) + ProcessBlocks(outString, inString, length / s); else { - while (length) + do { - if (inputAlignmentOk) - ProcessBlocks(m_buffer, inString, 1); - else - { - memcpy(m_buffer, inString, s); - ProcessBlocks(m_buffer, m_buffer, 1); - } - memcpy(outString, m_buffer, s); + memcpy(m_buffer, inString, s); + ProcessBlocks(outString, m_buffer, 1); inString += s; outString += s; length -= s; - } + } while (length > 0); } } void CBC_Encryption::ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks) { unsigned int blockSize = BlockSize(); - while (numberOfBlocks--) + xorbuf(m_register, inString, blockSize); + while (--numberOfBlocks) { - xorbuf(m_register, inString, blockSize); - m_cipher->ProcessBlock(m_register); - memcpy(outString, m_register, blockSize); + m_cipher->ProcessBlock(m_register, outString); inString += blockSize; + xorbuf(m_register, inString, 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) @@ -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) { 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_register.swap(m_temp); inString += blockSize; outString += blockSize; - } + } while (--numberOfBlocks); } void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length) |