Permalink
Browse files

Fix bottleneck (loops) in TransformFinalBlock when the buffers are la…

…rger than the block size. Issue found by Ajay Matai
  • Loading branch information...
1 parent 1d814df commit 2051b9f0cbee709bd826ccddee42e092bfe9bb04 @spouliot spouliot committed Sep 2, 2012
Showing with 4 additions and 15 deletions.
  1. +4 −15 class/Crimson.CryptoDev/Crimson.CryptoDev/CryptoDevTransform.cs
@@ -326,15 +326,10 @@ private byte[] FinalEncrypt (byte[] inputBuffer, int inputOffset, int inputCount
}
byte[] res = new byte [total];
- int outputOffset = 0;
- // process all blocks except the last (final) block
- while (total > BlockSizeByte) {
- InternalTransformBlock (inputBuffer, inputOffset, BlockSizeByte, res, outputOffset);
- inputOffset += BlockSizeByte;
- outputOffset += BlockSizeByte;
- total -= BlockSizeByte;
- }
+ // process all blocks except the last (final) block
+ int outputOffset = InternalTransformBlock (inputBuffer, inputOffset, total - BlockSizeByte, res, 0);
+ inputOffset += outputOffset;
// now we only have a single last block to encrypt
byte pad = (byte) (BlockSizeByte - rem);
@@ -379,14 +374,8 @@ private byte[] FinalDecrypt (byte[] inputBuffer, int inputOffset, int inputCount
total += BlockSizeByte;
byte[] res = new byte [total];
- int outputOffset = 0;
- while (inputCount > 0) {
- int len = InternalTransformBlock (inputBuffer, inputOffset, BlockSizeByte, res, outputOffset);
- inputOffset += BlockSizeByte;
- outputOffset += len;
- inputCount -= BlockSizeByte;
- }
+ int outputOffset = InternalTransformBlock (inputBuffer, inputOffset, inputCount, res, 0);
if (lastBlock) {
Transform (workBuff, 0, res, outputOffset, BlockSizeByte);

0 comments on commit 2051b9f

Please sign in to comment.