Thanks for the bug report. Apparently I forgot to test doing Seek() after
already encrypting something. As a workaround you can do a Resynchronize()
with the original IV before doing Seek(), or apply the attached patch.
On Sun, Feb 02, 2003 at 06:36:54AM -0000, gl wrote:
>
> Forgot my IV_SIZE const:
>
> const IV_SIZE = Blowfish::BLOCKSIZE;
> --
> gl
>
> ----- Original Message -----
> From: "gl" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Sunday, February 02, 2003 6:32 AM
> Subject: Streamcipher Seek() bugged?
>
>
> >
> > I've just updated my working 4.2 code to use 5.0 (downloaded from the
> > website, not from CVS). Under 4.2, Seek()'ing worked correctly for me,
> but
> > it no longer seems to work under 5.
> >
> > Here's some test code that demonstrates the failure:
> >
> > AutoSeededRandomPool rnd;
> >
> > SecByteBlock pass;
> > const char* passphrase = "test passphrase-";
> > pass.Assign((unsigned char*)passphrase, strlen(passphrase));
> >
> > unsigned char plain[100], buffer[100];
> > rnd.GenerateBlock(plain, 100);
> > memcpy(buffer, plain, 100);
> >
> > SecByteBlock IV;
> > IV .New(IV_SIZE);
> > rnd.GenerateBlock(IV, IV_SIZE);
> >
> > CTR_Mode<Blowfish>::Encryption Enc(pass, pass.size(), IV);
> > CTR_Mode<Blowfish>::Decryption Dec(pass, pass.size(), IV);
> >
> > // encode, then decode the buffer
> > Enc.ProcessString(buffer, 100);
> > _ASSERT(memcmp(plain, buffer, 100) != 0); // passes
> > Dec.ProcessString(buffer, 100);
> > // does it match the plaintext?
> > _ASSERT(memcmp(plain, buffer, 100) == 0); // passes
> >
> > // encrypt the buffer again
> > Enc.Seek(0);
> > Enc.ProcessString(buffer, 100);
> > // copy the first plain byte into the first encrypted byte,
> > // seek the streamcipher and process it.
> > buffer[0] = plain[0];
> > Enc.Seek(0);
> > Enc.ProcessString(buffer, 1);
> > // now try to decode the buffer again
> > Dec.Seek(0);
> > Dec.ProcessString(buffer, 100);
> > // do they still match?
> > _ASSERTE(memcmp(plain, buffer, 100) == 0); // FAILS!
> >
> > Does anyone see anything wrong with this? If no, Wei, any idea what's
> > happening here?
> > --
> > gl
> >
Index: modes.cpp
===================================================================
RCS file: /cvsroot/cryptopp/c5/modes.cpp,v
retrieving revision 1.3
diff -c -r1.3 modes.cpp
*** modes.cpp 17 Oct 2002 16:32:28 -0000 1.3
--- modes.cpp 4 Feb 2003 00:35:10 -0000
***************
*** 63,71 ****
void CTR_ModePolicy::SeekToIteration(dword iterationCount)
{
int carry=0;
! for (int i=BlockSize()-1; i>=0 && (iterationCount || carry); i--)
{
! unsigned int sum = m_counterArray[i] + byte(iterationCount) + carry;
m_counterArray[i] = (byte) sum;
carry = sum >> 8;
iterationCount >>= 8;
--- 63,71 ----
void CTR_ModePolicy::SeekToIteration(dword iterationCount)
{
int carry=0;
! for (int i=BlockSize()-1; i>=0; i--)
{
! unsigned int sum = m_register[i] + byte(iterationCount) + carry;
m_counterArray[i] = (byte) sum;
carry = sum >> 8;
iterationCount >>= 8;