summaryrefslogtreecommitdiff
path: root/modes.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2018-07-09 06:31:17 -0400
committerJeffrey Walton <noloader@gmail.com>2018-07-09 06:31:17 -0400
commit4c5487b0e45ec65503c85703895a4f8b6339a92f (patch)
treead0ede6c31d8589acb03a3ea066d8ae3d3bfaa31 /modes.cpp
parent86773e942c8449a08674f06c67db3014f8fa05a5 (diff)
downloadcryptopp-git-4c5487b0e45ec65503c85703895a4f8b6339a92f.tar.gz
Increase use of ptrdiff_t when performing pointer math
Increase use of ptrdiff_t when performing pointer math Reduce AlgorithmProvider overrides Fix CPU_ProbeARMv7 on Aarch64
Diffstat (limited to 'modes.cpp')
-rw-r--r--modes.cpp53
1 files changed, 31 insertions, 22 deletions
diff --git a/modes.cpp b/modes.cpp
index c82cbe6d..2a4f0cf9 100644
--- a/modes.cpp
+++ b/modes.cpp
@@ -37,7 +37,7 @@ void CFB_ModePolicy::Iterate(byte *output, const byte *input, CipherDir dir, siz
CRYPTOPP_ASSERT(m_register.size() == BlockSize());
CRYPTOPP_ASSERT(m_temp.size() == BlockSize());
- const unsigned int s = BlockSize();
+ const ptrdiff_t s = BlockSize();
if (dir == ENCRYPTION)
{
m_cipher->ProcessAndXorBlock(m_register, input, output);
@@ -63,7 +63,7 @@ void CFB_ModePolicy::TransformRegister()
CRYPTOPP_ASSERT(m_temp.size() == BlockSize());
m_cipher->ProcessBlock(m_register, m_temp);
- const unsigned int updateSize = BlockSize()-m_feedbackSize;
+ const ptrdiff_t updateSize = BlockSize()-m_feedbackSize;
memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize);
memcpy_s(m_register+updateSize, m_register.size()-updateSize, m_temp, m_feedbackSize);
}
@@ -90,12 +90,19 @@ void CFB_ModePolicy::ResizeBuffers()
m_temp.New(BlockSize());
}
+byte* CFB_ModePolicy::GetRegisterBegin()
+{
+ CRYPTOPP_ASSERT(!m_register.empty());
+ CRYPTOPP_ASSERT(BlockSize() >= m_feedbackSize);
+ return m_register + static_cast<ptrdiff_t>(BlockSize() - m_feedbackSize);
+}
+
void OFB_ModePolicy::WriteKeystream(byte *keystreamBuffer, size_t iterationCount)
{
CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation());
CRYPTOPP_ASSERT(m_register.size() == BlockSize());
- const unsigned int s = BlockSize();
+ const ptrdiff_t s = BlockSize();
m_cipher->ProcessBlock(m_register, keystreamBuffer);
if (iterationCount > 1)
m_cipher->AdvancedProcessBlocks(keystreamBuffer, NULLPTR, keystreamBuffer+s, s*(iterationCount-1), 0);
@@ -117,7 +124,7 @@ void CTR_ModePolicy::SeekToIteration(lword iterationCount)
for (int i=BlockSize()-1; i>=0; i--)
{
unsigned int sum = m_register[i] + byte(iterationCount) + carry;
- m_counterArray[i] = (byte) sum;
+ m_counterArray[i] = static_cast<byte>(sum);
carry = sum >> 8;
iterationCount >>= 8;
}
@@ -133,8 +140,8 @@ void CTR_ModePolicy::OperateKeystream(KeystreamOperation /*operation*/, byte *ou
CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation());
CRYPTOPP_ASSERT(m_counterArray.size() == BlockSize());
- const unsigned int s = BlockSize();
- const unsigned int inputIncrement = input ? s : 0;
+ const ptrdiff_t s = BlockSize();
+ const ptrdiff_t inputIncrement = input ? s : 0;
while (iterationCount)
{
@@ -190,7 +197,7 @@ void CBC_Encryption::ProcessData(byte *outString, const byte *inString, size_t l
CRYPTOPP_ASSERT(m_register.size() == BlockSize());
if (!length) return;
- const unsigned int blockSize = BlockSize();
+ const ptrdiff_t 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);
@@ -200,7 +207,9 @@ void CBC_Encryption::ProcessData(byte *outString, const byte *inString, size_t l
size_t CBC_CTS_Encryption::ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
{
CRYPTOPP_UNUSED(outLength);
- size_t used = inLength;
+ const size_t used = inLength;
+ const ptrdiff_t blockSize = static_cast<ptrdiff_t>(BlockSize());
+
if (inLength <= BlockSize())
{
if (!m_stolenIV)
@@ -215,9 +224,8 @@ size_t CBC_CTS_Encryption::ProcessLastBlock(byte *outString, size_t outLength, c
// steal from next to last block
xorbuf(m_register, inString, BlockSize());
m_cipher->ProcessBlock(m_register);
- inString += BlockSize();
- inLength -= BlockSize();
- memcpy(outString+BlockSize(), m_register, inLength);
+ inString += blockSize; inLength -= blockSize;
+ memcpy(outString+blockSize, m_register, inLength);
}
// output last full ciphertext block
@@ -236,12 +244,12 @@ void CBC_Decryption::ResizeBuffers()
void CBC_Decryption::ProcessData(byte *outString, const byte *inString, size_t length)
{
- if (!length)
- return;
CRYPTOPP_ASSERT(length%BlockSize()==0);
+ if (!length) {return;}
- const unsigned int blockSize = BlockSize();
- memcpy(m_temp, inString+length-blockSize, blockSize); // save copy now in case of in-place decryption
+ // save copy now in case of in-place decryption
+ const ptrdiff_t blockSize = BlockSize();
+ memcpy(m_temp, inString+length-blockSize, blockSize);
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);
@@ -252,8 +260,9 @@ size_t CBC_CTS_Decryption::ProcessLastBlock(byte *outString, size_t outLength, c
{
CRYPTOPP_UNUSED(outLength);
const byte *pn1, *pn2;
- bool stealIV = inLength <= BlockSize();
- size_t used = inLength;
+ const size_t used = inLength;
+ const bool stealIV = inLength <= BlockSize();
+ const ptrdiff_t blockSize = static_cast<ptrdiff_t>(BlockSize());
if (stealIV)
{
@@ -262,13 +271,13 @@ size_t CBC_CTS_Decryption::ProcessLastBlock(byte *outString, size_t outLength, c
}
else
{
- pn1 = inString + BlockSize();
+ pn1 = inString + blockSize;
pn2 = inString;
- inLength -= BlockSize();
+ inLength -= blockSize;
}
// decrypt last partial plaintext block
- memcpy(m_temp, pn2, BlockSize());
+ memcpy(m_temp, pn2, blockSize);
m_cipher->ProcessBlock(m_temp);
xorbuf(m_temp, pn1, inLength);
@@ -278,11 +287,11 @@ size_t CBC_CTS_Decryption::ProcessLastBlock(byte *outString, size_t outLength, c
}
else
{
- memcpy(outString+BlockSize(), m_temp, inLength);
+ memcpy(outString+blockSize, m_temp, inLength);
// decrypt next to last plaintext block
memcpy(m_temp, pn1, inLength);
m_cipher->ProcessBlock(m_temp);
- xorbuf(outString, m_temp, m_register, BlockSize());
+ xorbuf(outString, m_temp, m_register, blockSize);
}
return used;