Skip to content

Commit

Permalink
Guard increment of inBlock in CTR_ModePolicy::OperateKeystream
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Sep 29, 2023
1 parent c6a16ea commit b08f72c
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef CRYPTOPP_IMPORTS

#include "modes.h"
#include "strciphr.h"
#include "misc.h"

#if defined(CRYPTOPP_DEBUG)
Expand Down Expand Up @@ -137,25 +138,29 @@ void CTR_ModePolicy::IncrementCounterBy256()
IncrementCounterByOne(m_counterArray, BlockSize()-1);
}

void CTR_ModePolicy::OperateKeystream(KeystreamOperation /*operation*/, byte *output, const byte *input, size_t iterationCount)
void CTR_ModePolicy::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
{
CRYPTOPP_ASSERT(output);
// CRYPTOPP_ASSERT(input); // input is sometimes NULL
CRYPTOPP_ASSERT(m_cipher->IsForwardTransformation());
CRYPTOPP_ASSERT(m_counterArray.size() == BlockSize());
CRYPTOPP_UNUSED(operation);

const unsigned int s = BlockSize();
const unsigned int inputIncrement = input ? s : 0;
const size_t s = BlockSize();

while (iterationCount)
{
const byte lsb = m_counterArray[s-1];
const size_t blocks = UnsignedMin(iterationCount, 256U-lsb);
const size_t blocks = UnsignedMin(iterationCount, 256u-lsb);
const size_t outIncrement = output ? blocks*s : 0;

This comment has been minimized.

Copy link
@irwir

irwir Sep 29, 2023

Contributor

Might be better to remove lines 155-156 and replace lines 162-163 with:

if (output)
    output = PtrAdd(output, blocks*s);
if (input)
    input = PtrAdd(input, blocks*s);

This comment has been minimized.

Copy link
@irwir

irwir Sep 29, 2023

Contributor

Had to edit the previsious comment because Github markup made one line invisible .

const size_t inIncrement = input ? blocks*s : 0;

m_cipher->AdvancedProcessBlocks(m_counterArray, input, output, blocks*s, BlockTransformation::BT_InBlockIsCounter|BlockTransformation::BT_AllowParallel);
if ((m_counterArray[s-1] = byte(lsb + blocks)) == 0)
if ((m_counterArray[s-1] = static_cast<byte>(lsb + blocks)) == 0)
IncrementCounterBy256();

output = PtrAdd(output, blocks*s);
input = PtrAdd(input, blocks*inputIncrement);
output = PtrAdd(output, outIncrement);
input = PtrAdd(input, inIncrement);
iterationCount -= blocks;
}
}
Expand Down

0 comments on commit b08f72c

Please sign in to comment.