Improved Performance of SMFREE3's RawEncrypt

Jonathan Schulze-Hewett <[email protected]>
Newsgroups gmane.ietf.sfl
Organization Information Security Corporation
Message-ID <[email protected]>
The following RawEncrypt for the cyrpto++ ctil improves performance
significantly. Before, 90% of the time to encrypt was spent allocating
and copying memory (every 8 bytes written to the buffer required an
allocation and memory copy). In my tests the changes have reduced my
encryption time from 15 minutes to less than 1 minute on a 1.7MB file.
Changes are noted by // JCSH - 7/12/2000. I suspect there is a better
way to achieve the same thing (allocate the amount of memory you need in
advance rather than when needed) but this works for now.

-Jonathan
==============================================================================
Jonathan C. Schulze-Hewett        Email: [email protected]
Software Engineer                 
Information Security Corporation  Voice: 708-445-1704       
1011 W. Lake Street, Suite 212    Fax:   708-455-9705
Oak Park, IL  60301               WWW:   http://www.infoseccorp.com


void CSM_Free3::RawEncrypt(CSM_Buffer *pbufInput, CSM_Buffer
*pbufOutput,
         Filter *pCBCEncryption, int iINBlockLen)
{
   long lBlockLen;
   char *achIn=(char *)calloc(1,iINBlockLen + 1);
   char *achOut=(char *)calloc(1,iINBlockLen + 1);
   // achBuf is used for padding at end of encryption
   int getLength;

   int nBytesWriten = 0; // JCSH - 7/12/2000.
   CSM_Buffer *pTempBuf = new CSM_Buffer(pbufInput->Length() * 2); //
JCSH - 7/12/2000.


   SME_SETUP("CSM_Free3::RawEncrypt");

   // open input for reading
   SME(pbufInput->Open(SM_FOPEN_READ));

   // open output for writing
   SME(pTempBuf ->Open(SM_FOPEN_WRITE)); // JCSH - 7/12/2000.


   // read input, cbc encrypt it, and write encrypted
   // result to output WHILE we have full blocks
   while ((lBlockLen = pbufInput->cRead(&achIn[0], iINBlockLen)) ==
iINBlockLen)
   {
      // do cbc process
      pCBCEncryption->Put((const unsigned char *) &achIn[0],
iINBlockLen);
      //RWC; "->Put(...) calls
ProcessBuf(...);pCBCEncryption->ProcessBuf();
      getLength = pCBCEncryption->Get((unsigned char *) &achOut[0],
iINBlockLen);
      if (getLength)
      {
        // now, write the block to pEncryptedData

	nBytesWriten += getLength; // JCSH - 7/12/2000.
        SME(pTempBuf->Write(&achOut[0], getLength)); // JCSH -
7/12/2000.
      }
   }
      pCBCEncryption->Put((const unsigned char *) &achIn[0], lBlockLen
);
      pCBCEncryption->InputFinished();      // auto-Pad results.
      getLength = pCBCEncryption->Get((unsigned char *) &achOut[0], 
          iINBlockLen);
      if (getLength)
      {
         // write padded block to pEncryptedData
        nBytesWriten += getLength; // JCSH - 7/12/2000.

        SME(pTempBuf->Write(&achOut[0], getLength)); // JCSH -
7/12/2000.
      }

   pbufInput->Close();

   pTempBuf->Close(); // JCSH - 7/12/2000.

   pbufOutput->Open(SM_FOPEN_WRITE); // JCSH - 7/12/2000.

   pbufOutput->Write(pTempBuf->Get(), nBytesWriten); // JCSH -
7/12/2000.

   pbufOutput->Close(); // JCSH - 7/12/2000.
   delete pTempBuf; // JCSH - 7/12/2000.
   free(achIn);
   free(achOut);

   SME_FINISH_CATCH
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.