summaryrefslogtreecommitdiff
path: root/modes.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2017-09-05 16:28:00 -0400
committerJeffrey Walton <noloader@gmail.com>2017-09-05 16:28:00 -0400
commit37e02f9e0e2ee627f0f95b7bc0a09f4ba1ce562e (patch)
treec8b8cc1e9b0b56998eed479f29aa6f3da33f1755 /modes.cpp
parent23b939c62b7f497d6f99bfe97ad639b35287ac61 (diff)
downloadcryptopp-git-37e02f9e0e2ee627f0f95b7bc0a09f4ba1ce562e.tar.gz
Revert AltiVec and Power8 commits
The strategy of "cleanup under-aligned buffers" is not scaling well. Corner cases are still turing up. The library has some corner-case breaks, like old 32-bit Intels. And it still has not solved the AltiVec and Power8 alignment problems. For now we are backing out the changes and investigating other strategies
Diffstat (limited to 'modes.cpp')
-rw-r--r--modes.cpp177
1 files changed, 31 insertions, 146 deletions
diff --git a/modes.cpp b/modes.cpp
index f92d4ade..0d9849ce 100644
--- a/modes.cpp
+++ b/modes.cpp
@@ -34,8 +34,7 @@ void CFB_ModePolicy::Iterate(byte *output, const byte *input, CipherDir dir, siz
{
CRYPTOPP_ASSERT(input);
CRYPTOPP_ASSERT(output);
- // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
- CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation());
+ CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation()); // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
CRYPTOPP_ASSERT(m_feedbackSize == BlockSize());
const unsigned int s = BlockSize();
@@ -44,23 +43,21 @@ void CFB_ModePolicy::Iterate(byte *output, const byte *input, CipherDir dir, siz
m_cipher->ProcessAndXorBlock(m_register, input, output);
if (iterationCount > 1)
m_cipher->AdvancedProcessBlocks(output, input+s, output+s, (iterationCount-1)*s, 0);
- std::memcpy(m_register, output+(iterationCount-1)*s, s);
+ memcpy(m_register, output+(iterationCount-1)*s, s);
}
else
{
- std::memcpy(m_temp, input+(iterationCount-1)*s, s); // make copy first in case of in-place decryption
+ memcpy(m_temp, input+(iterationCount-1)*s, s); // make copy first in case of in-place decryption
if (iterationCount > 1)
m_cipher->AdvancedProcessBlocks(input, input+s, output+s, (iterationCount-1)*s, BlockTransformation::BT_ReverseDirection);
m_cipher->ProcessAndXorBlock(m_register, input, output);
- std::memcpy(m_register, m_temp, s);
+ memcpy(m_register, m_temp, s);
}
}
void CFB_ModePolicy::TransformRegister()
{
- // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
- CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation());
-
+ CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation()); // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
m_cipher->ProcessBlock(m_register, m_temp);
unsigned int updateSize = BlockSize()-m_feedbackSize;
memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize);
@@ -89,16 +86,12 @@ void CFB_ModePolicy::ResizeBuffers()
void OFB_ModePolicy::WriteKeystream(byte *keystreamBuffer, size_t iterationCount)
{
- CRYPTOPP_ASSERT(keystreamBuffer);
- CRYPTOPP_ASSERT(iterationCount);
- // OFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
- CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation());
-
- const unsigned int s = BlockSize();
+ CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation()); // OFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
+ unsigned int s = BlockSize();
m_cipher->ProcessBlock(m_register, keystreamBuffer);
if (iterationCount > 1)
m_cipher->AdvancedProcessBlocks(keystreamBuffer, NULLPTR, keystreamBuffer+s, s*(iterationCount-1), 0);
- std::memcpy(m_register, keystreamBuffer+s*(iterationCount-1), s);
+ memcpy(m_register, keystreamBuffer+s*(iterationCount-1), s);
}
void OFB_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
@@ -128,44 +121,18 @@ void CTR_ModePolicy::IncrementCounterBy256()
void CTR_ModePolicy::OperateKeystream(KeystreamOperation /*operation*/, byte *output, const byte *input, size_t iterationCount)
{
- CRYPTOPP_ASSERT(output);
- CRYPTOPP_ASSERT(iterationCount);
- // CTR mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
- CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation());
-
- const unsigned int s = BlockSize();
- const unsigned int inputIncrement = input ? s : 0;
- const unsigned int alignment = m_cipher->OptimalDataAlignment();
- const byte* is = input; byte* os = output;
+ CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation()); // CTR mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
+ unsigned int s = BlockSize();
+ unsigned int inputIncrement = input ? s : 0;
- AlignedSecByteBlock i, o;
while (iterationCount)
{
byte lsb = m_counterArray[s-1];
- const size_t blocks = UnsignedMin(iterationCount, 256U-lsb);
-
- is = input; os = output;
- if(!IsAlignedOn(input, alignment))
- {
- i.Assign(input, blocks*inputIncrement);
- is = i.begin();
- }
- if(!IsAlignedOn(output, alignment))
- {
- o.Assign(output, blocks*s);
- os = o.begin();
- }
-
- m_cipher->AdvancedProcessBlocks(m_counterArray, is, os, blocks*s, BlockTransformation::BT_InBlockIsCounter|BlockTransformation::BT_AllowParallel);
-
+ size_t blocks = UnsignedMin(iterationCount, 256U-lsb);
+ m_cipher->AdvancedProcessBlocks(m_counterArray, input, output, blocks*s, BlockTransformation::BT_InBlockIsCounter|BlockTransformation::BT_AllowParallel);
if ((m_counterArray[s-1] = lsb + (byte)blocks) == 0)
IncrementCounterBy256();
- if(!IsAlignedOn(output, alignment))
- {
- std::memcpy(output, os, blocks*s);
- }
-
output += blocks*s;
input += blocks*inputIncrement;
iterationCount -= blocks;
@@ -183,9 +150,6 @@ void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer, const byte *iv,
void BlockOrientedCipherModeBase::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
{
- CRYPTOPP_ASSERT(key);
- CRYPTOPP_ASSERT(length);
-
m_cipher->SetKey(key, length, params);
ResizeBuffers();
if (IsResynchronizable())
@@ -204,85 +168,32 @@ void BlockOrientedCipherModeBase::ResizeBuffers()
void ECB_OneWay::ProcessData(byte *outString, const byte *inString, size_t length)
{
- CRYPTOPP_ASSERT(outString);
- CRYPTOPP_ASSERT(inString);
- CRYPTOPP_ASSERT(length);
CRYPTOPP_ASSERT(length%BlockSize()==0);
-
- const unsigned int blockSize = BlockSize();
- const unsigned int alignment = m_cipher->OptimalDataAlignment();
- const byte* is = inString; byte* os = outString;
-
- AlignedSecByteBlock i, o;
- if(!IsAlignedOn(inString, alignment))
- {
- i.Assign(inString, length);
- is = i.begin();
- }
- if(!IsAlignedOn(outString, alignment))
- {
- o.New(length);
- os = o.begin();
- }
-
- m_cipher->AdvancedProcessBlocks(is, NULLPTR, os, length, BlockTransformation::BT_AllowParallel);
-
- if(!IsAlignedOn(outString, alignment))
- {
- std::memcpy(outString, os, length);
- }
+ m_cipher->AdvancedProcessBlocks(inString, NULLPTR, outString, length, BlockTransformation::BT_AllowParallel);
}
void CBC_Encryption::ProcessData(byte *outString, const byte *inString, size_t length)
{
- CRYPTOPP_ASSERT(outString);
- CRYPTOPP_ASSERT(inString);
- CRYPTOPP_ASSERT(length);
- CRYPTOPP_ASSERT(length%BlockSize()==0);
-
if (!length)
return;
+ CRYPTOPP_ASSERT(length%BlockSize()==0);
- const unsigned int blockSize = BlockSize();
- const unsigned int alignment = m_cipher->OptimalDataAlignment();
- const byte* is = inString; byte* os = outString;
-
- AlignedSecByteBlock i, o;
- if(!IsAlignedOn(inString, alignment))
- {
- i.Assign(inString, length);
- is = i.begin();
- }
- if(!IsAlignedOn(outString, alignment))
- {
- o.New(length);
- os = o.begin();
- }
-
+ unsigned int blockSize = BlockSize();
m_cipher->AdvancedProcessBlocks(inString, m_register, outString, blockSize, BlockTransformation::BT_XorInput);
if (length > blockSize)
m_cipher->AdvancedProcessBlocks(inString+blockSize, outString, outString+blockSize, length-blockSize, BlockTransformation::BT_XorInput);
- std::memcpy(m_register, outString + length - blockSize, blockSize);
-
- if(!IsAlignedOn(outString, alignment))
- {
- std::memcpy(outString, os, length);
- }
+ memcpy(m_register, outString + length - blockSize, blockSize);
}
void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
{
- CRYPTOPP_ASSERT(outString);
- CRYPTOPP_ASSERT(inString);
- CRYPTOPP_ASSERT(length);
-
if (length <= BlockSize())
{
if (!m_stolenIV)
throw InvalidArgument("CBC_Encryption: message is too short for ciphertext stealing");
// steal from IV
- std::memcpy(outString, m_register, length);
+ memcpy(outString, m_register, length);
outString = m_stolenIV;
}
else
@@ -292,13 +203,13 @@ void CBC_CTS_Encryption::ProcessLastBlock(byte *outString, const byte *inString,
m_cipher->ProcessBlock(m_register);
inString += BlockSize();
length -= BlockSize();
- std::memcpy(outString+BlockSize(), m_register, length);
+ memcpy(outString+BlockSize(), m_register, length);
}
// output last full ciphertext block
xorbuf(m_register, inString, length);
m_cipher->ProcessBlock(m_register);
- std::memcpy(outString, m_register, BlockSize());
+ memcpy(outString, m_register, BlockSize());
}
void CBC_Decryption::ResizeBuffers()
@@ -309,46 +220,20 @@ void CBC_Decryption::ResizeBuffers()
void CBC_Decryption::ProcessData(byte *outString, const byte *inString, size_t length)
{
- CRYPTOPP_ASSERT(outString);
- CRYPTOPP_ASSERT(inString);
- CRYPTOPP_ASSERT(length && length%BlockSize()==0);
-
if (!length)
return;
+ CRYPTOPP_ASSERT(length%BlockSize()==0);
- const unsigned int blockSize = BlockSize();
- const unsigned int alignment = m_cipher->OptimalDataAlignment();
- bool align = !IsAlignedOn(inString, alignment) || !IsAlignedOn(outString, alignment);
-
- if (align && false)
- {
- AlignedSecByteBlock i(length), o(length);
- std::memcpy(i, inString, length);
- std::memcpy(o, outString+length-blockSize, blockSize); // copy tail
-
- std::memcpy(m_temp, i+length-blockSize, blockSize); // save copy now in case of in-place decryption
- if (length > blockSize)
- m_cipher->AdvancedProcessBlocks(i+blockSize, i, o+blockSize, length-blockSize, BlockTransformation::BT_ReverseDirection|BlockTransformation::BT_AllowParallel);
- m_cipher->ProcessAndXorBlock(i, m_register, o);
- m_register.swap(m_temp);
- std::memcpy(outString, o, length);
- }
- else
- {
- std::memcpy(m_temp, inString+length-blockSize, blockSize); // save copy now in case of in-place decryption
- if (length > blockSize)
- m_cipher->AdvancedProcessBlocks(inString+blockSize, inString, outString+blockSize, length-blockSize, BlockTransformation::BT_ReverseDirection|BlockTransformation::BT_AllowParallel);
- m_cipher->ProcessAndXorBlock(inString, m_register, outString);
- m_register.swap(m_temp);
- }
+ unsigned int blockSize = BlockSize();
+ memcpy(m_temp, inString+length-blockSize, blockSize); // save copy now in case of in-place decryption
+ if (length > blockSize)
+ m_cipher->AdvancedProcessBlocks(inString+blockSize, inString, outString+blockSize, length-blockSize, BlockTransformation::BT_ReverseDirection|BlockTransformation::BT_AllowParallel);
+ m_cipher->ProcessAndXorBlock(inString, m_register, outString);
+ m_register.swap(m_temp);
}
void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
{
- CRYPTOPP_ASSERT(outString);
- CRYPTOPP_ASSERT(inString);
- CRYPTOPP_ASSERT(length);
-
const byte *pn, *pn1;
bool stealIV = length <= BlockSize();
@@ -365,17 +250,17 @@ void CBC_CTS_Decryption::ProcessLastBlock(byte *outString, const byte *inString,
}
// decrypt last partial plaintext block
- std::memcpy(m_temp, pn1, BlockSize());
+ memcpy(m_temp, pn1, BlockSize());
m_cipher->ProcessBlock(m_temp);
xorbuf(m_temp, pn, length);
if (stealIV)
- std::memcpy(outString, m_temp, length);
+ memcpy(outString, m_temp, length);
else
{
- std::memcpy(outString+BlockSize(), m_temp, length);
+ memcpy(outString+BlockSize(), m_temp, length);
// decrypt next to last plaintext block
- std::memcpy(m_temp, pn, length);
+ memcpy(m_temp, pn, length);
m_cipher->ProcessBlock(m_temp);
xorbuf(outString, m_temp, m_register, BlockSize());
}