From e92eb316906c41b349f6c86a8186fd18b0b3b580 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Fri, 29 Sep 2017 22:34:33 -0400 Subject: Update StreamTransformation and ProcessLastBlock Some authenticated encryption modes have needs that are not expressed well with MandatoryBlockSize() and MinLastBlockSize(). When IsLastBlockSpecial() returns true three things happen. First, standard block cipher padding is not applied. Second, the ProcessLastBlock() is used that provides inString and outString lengths. Third, outString is larger than inString by 2*MandatoryBlockSize(). That is, there's a reserve available when processing the last block. The return value of ProcessLastBlock() indicates how many bytes were written to outString. A filter driving data will send outString and returned length to an AttachedTransformation() for additional processing. --- modes.cpp | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'modes.cpp') diff --git a/modes.cpp b/modes.cpp index 0d9849ce..5738b403 100644 --- a/modes.cpp +++ b/modes.cpp @@ -174,8 +174,7 @@ void ECB_OneWay::ProcessData(byte *outString, const byte *inString, size_t lengt void CBC_Encryption::ProcessData(byte *outString, const byte *inString, size_t length) { - if (!length) - return; + if (!length) return; CRYPTOPP_ASSERT(length%BlockSize()==0); unsigned int blockSize = BlockSize(); @@ -185,15 +184,17 @@ void CBC_Encryption::ProcessData(byte *outString, const byte *inString, size_t l memcpy(m_register, outString + length - blockSize, blockSize); } -void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length) +size_t CBC_CTS_Encryption::ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength) { - if (length <= BlockSize()) + CRYPTOPP_UNUSED(outLength); + size_t used = inLength; + if (inLength <= BlockSize()) { if (!m_stolenIV) throw InvalidArgument("CBC_Encryption: message is too short for ciphertext stealing"); // steal from IV - memcpy(outString, m_register, length); + memcpy(outString, m_register, inLength); outString = m_stolenIV; } else @@ -202,14 +203,16 @@ void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString, xorbuf(m_register, inString, BlockSize()); m_cipher->ProcessBlock(m_register); inString += BlockSize(); - length -= BlockSize(); - memcpy(outString+BlockSize(), m_register, length); + inLength -= BlockSize(); + memcpy(outString+BlockSize(), m_register, inLength); } // output last full ciphertext block - xorbuf(m_register, inString, length); + xorbuf(m_register, inString, inLength); m_cipher->ProcessBlock(m_register); memcpy(outString, m_register, BlockSize()); + + return used; } void CBC_Decryption::ResizeBuffers() @@ -232,38 +235,44 @@ void CBC_Decryption::ProcessData(byte *outString, const byte *inString, size_t l m_register.swap(m_temp); } -void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length) +size_t CBC_CTS_Decryption::ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength) { - const byte *pn, *pn1; - bool stealIV = length <= BlockSize(); + CRYPTOPP_UNUSED(outLength); + const byte *pn1, *pn2; + bool stealIV = inLength <= BlockSize(); + size_t used = inLength; if (stealIV) { - pn = inString; - pn1 = m_register; + pn1 = inString; + pn2 = m_register; } else { - pn = inString + BlockSize(); - pn1 = inString; - length -= BlockSize(); + pn1 = inString + BlockSize(); + pn2 = inString; + inLength -= BlockSize(); } // decrypt last partial plaintext block - memcpy(m_temp, pn1, BlockSize()); + memcpy(m_temp, pn2, BlockSize()); m_cipher->ProcessBlock(m_temp); - xorbuf(m_temp, pn, length); + xorbuf(m_temp, pn1, inLength); if (stealIV) - memcpy(outString, m_temp, length); + { + memcpy(outString, m_temp, inLength); + } else { - memcpy(outString+BlockSize(), m_temp, length); + memcpy(outString+BlockSize(), m_temp, inLength); // decrypt next to last plaintext block - memcpy(m_temp, pn, length); + memcpy(m_temp, pn1, inLength); m_cipher->ProcessBlock(m_temp); xorbuf(outString, m_temp, m_register, BlockSize()); } + + return used; } NAMESPACE_END -- cgit v1.2.1