inline void CTR_ModePolicy::ProcessMultipleBlocks(byte *output, const byte *input, size_t n) { unsigned int s = BlockSize(), j = 0; for (unsigned int i=1; i<n; i++, j+=s) IncrementCounterByOne(m_counterArray + j + s, m_counterArray + j, s); m_cipher->ProcessAndXorMultipleBlocks(m_counterArray, input, output, n); IncrementCounterByOne(m_counterArray, m_counterArray + s*(n-1), s); }
void CTR_ModePolicy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) { assert(m_cipher->IsForwardTransformation()); // CTR mode needs the "encrypt" direction of the underlying block cipher, even to decrypt unsigned int maxBlocks = m_cipher->OptimalNumberOfParallelBlocks(); if (maxBlocks == 1) { unsigned int sizeIncrement = BlockSize(); while (iterationCount) { m_cipher->ProcessAndXorBlock(m_counterArray, input, output); IncrementCounterByOne(m_counterArray, sizeIncrement); output += sizeIncrement; input += sizeIncrement; iterationCount -= 1; } } else { unsigned int sizeIncrement = maxBlocks * BlockSize(); while (iterationCount >= maxBlocks) { ProcessMultipleBlocks(output, input, maxBlocks); output += sizeIncrement; input += sizeIncrement; iterationCount -= maxBlocks; } if (iterationCount > 0) ProcessMultipleBlocks(output, input, iterationCount); } }
void CTR_ModePolicy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) { unsigned int maxBlocks = m_cipher->OptimalNumberOfParallelBlocks(); if (maxBlocks == 1) { unsigned int sizeIncrement = BlockSize(); while (iterationCount) { m_cipher->ProcessAndXorBlock(m_counterArray, input, output); IncrementCounterByOne(m_counterArray, sizeIncrement); output += sizeIncrement; input += sizeIncrement; iterationCount -= 1; } } else { unsigned int sizeIncrement = maxBlocks * BlockSize(); while (iterationCount >= maxBlocks) { ProcessMultipleBlocks(output, input, maxBlocks); output += sizeIncrement; input += sizeIncrement; iterationCount -= maxBlocks; } if (iterationCount > 0) ProcessMultipleBlocks(output, input, iterationCount); } }
void CTR_ModePolicy::IncrementCounterBy256() { IncrementCounterByOne(m_counterArray, BlockSize()-1); }
void CTR_ModePolicy::GetNextIV(byte *IV) { IncrementCounterByOne(IV, m_counterArray, BlockSize()); }