Skip to content

Commit

Permalink
Fix over-zeroing of destination buffer with PaddingMode.Zeros
Browse files Browse the repository at this point in the history
This addresses two issues with how zero padding is handled.

The first is performance. When applying zero padding, we were clearing
the entire destination, then copying the plaintext over it. We were
zeroing more data than required, the only data that needed to be zeroed
is where the zero padding is applied.

The second is in the case of overlapping buffers for the plaintext
and ciphertext. However, this cannot happen currently since we always
ensure the destination buffer does not overlap the input buffer.
If overlapping is permitted in a future change, this would clear the
plaintext, not just where padding is required.
  • Loading branch information
vcsjones authored May 7, 2021
1 parent 28cff50 commit 74cafe7
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ private int PadBlock(ReadOnlySpan<byte> block, Span<byte> destination)
throw new ArgumentException(SR.Argument_DestinationTooShort, nameof(destination));
}

destination.Slice(0, zeroSize).Clear();
block.CopyTo(destination);
destination.Slice(count, padBytes).Clear();
return zeroSize;

default:
Expand Down

0 comments on commit 74cafe7

Please sign in to comment.