From caf9e032e6b4ccb114a74a3936c916bcfaba262d Mon Sep 17 00:00:00 2001 From: weidai Date: Mon, 2 Mar 2009 02:39:17 +0000 Subject: changes for 5.6: - added AuthenticatedSymmetricCipher interface class and Filter wrappers - added CCM, GCM (with SSE2 assembly), CMAC, and SEED - improved AES speed on x86 and x64 - removed WORD64_AVAILABLE; compiler 64-bit int support is now required git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@433 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- strciphr.cpp | 66 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 35 deletions(-) (limited to 'strciphr.cpp') diff --git a/strciphr.cpp b/strciphr.cpp index 45dd4de..53e0073 100644 --- a/strciphr.cpp +++ b/strciphr.cpp @@ -2,9 +2,6 @@ #include "pch.h" -// prevent Sun's CC compiler from including this file automatically -#if !defined(__SUNPRO_CC) || defined(CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES) - #ifndef CRYPTOPP_IMPORTS #include "strciphr.h" @@ -17,10 +14,15 @@ void AdditiveCipherTemplate::UncheckedSetKey(const byte *key, unsigned int le PolicyInterface &policy = this->AccessPolicy(); policy.CipherSetKey(params, key, length); m_leftOver = 0; - m_buffer.New(GetBufferByteSize(policy)); + unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) : RoundUpToMultipleOf(1024U, GetBufferByteSize(policy)); + m_buffer.New(bufferByteSize); if (this->IsResynchronizable()) - policy.CipherResynchronize(m_buffer, this->GetIVAndThrowIfInvalid(params)); + { + size_t ivLength; + const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength); + policy.CipherResynchronize(m_buffer, iv, ivLength); + } } template @@ -48,27 +50,16 @@ void AdditiveCipherTemplate::GenerateBlock(byte *outString, size_t length) 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; + 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; } } @@ -108,8 +99,8 @@ void AdditiveCipherTemplate::ProcessData(byte *outString, const byte *inStrin return; } - unsigned int bufferByteSize = GetBufferByteSize(policy); - unsigned int bufferIterations = policy.GetIterationsToBuffer(); + size_t bufferByteSize = m_buffer.size(); + size_t bufferIterations = bufferByteSize / bytesPerIteration; while (length >= bufferByteSize) { @@ -122,19 +113,22 @@ void AdditiveCipherTemplate::ProcessData(byte *outString, const byte *inStrin if (length > 0) { - policy.WriteKeystream(m_buffer, bufferIterations); - xorbuf(outString, inString, KeystreamBufferBegin(), length); - m_leftOver = bytesPerIteration - length; + bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration); + bufferIterations = bufferByteSize / bytesPerIteration; + + policy.WriteKeystream(KeystreamBufferEnd()-bufferByteSize, bufferIterations); + xorbuf(outString, inString, KeystreamBufferEnd()-bufferByteSize, length); + m_leftOver = bufferByteSize - length; } } template -void AdditiveCipherTemplate::Resynchronize(const byte *iv) +void AdditiveCipherTemplate::Resynchronize(const byte *iv, int length) { PolicyInterface &policy = this->AccessPolicy(); m_leftOver = 0; m_buffer.New(GetBufferByteSize(policy)); - policy.CipherResynchronize(m_buffer, iv); + policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length)); } template @@ -148,7 +142,7 @@ void AdditiveCipherTemplate::Seek(lword position) if (position > 0) { - policy.WriteKeystream(m_buffer, 1); + policy.WriteKeystream(KeystreamBufferEnd()-bytesPerIteration, 1); m_leftOver = bytesPerIteration - (unsigned int)position; } else @@ -162,16 +156,20 @@ void CFB_CipherTemplate::UncheckedSetKey(const byte *key, unsigned int len policy.CipherSetKey(params, key, length); if (this->IsResynchronizable()) - policy.CipherResynchronize(this->GetIVAndThrowIfInvalid(params)); + { + size_t ivLength; + const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength); + policy.CipherResynchronize(iv, ivLength); + } m_leftOver = policy.GetBytesPerIteration(); } template -void CFB_CipherTemplate::Resynchronize(const byte *iv) +void CFB_CipherTemplate::Resynchronize(const byte *iv, int length) { PolicyInterface &policy = this->AccessPolicy(); - policy.CipherResynchronize(iv); + policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length)); m_leftOver = policy.GetBytesPerIteration(); } @@ -252,5 +250,3 @@ void CFB_DecryptionTemplate::CombineMessageAndShiftRegister(byte *output, NAMESPACE_END #endif - -#endif -- cgit v1.2.1