Skip to content

Commit cc3af2f

Browse files
Address issue in TDECFormattedCipher.DoEncodeDecodeStream where GCM based cipher produces incorrect tag when reusing buffers. Solution is duplicating logic in existing DecodeBytes method.
1 parent 408b806 commit cc3af2f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

Source/DECCipherFormats.pas

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
726726
const OnProgress: TDECProgressEvent);
727727
var
728728
Buffer: TBytes;
729+
outBuffer: TBytes;
729730
BufferSize, Bytes: Integer;
730731
Max, StartPos, Pos: Int64;
731732
begin
@@ -752,6 +753,11 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
752753
SetLength(Buffer, BufferSize)
753754
else
754755
SetLength(Buffer, DataSize);
756+
757+
outBuffer := Buffer;
758+
if (FMode = cmGCM) then
759+
SetLength(outBuffer, Length(Buffer));
760+
755761
while DataSize > 0 do
756762
begin
757763
Bytes := BufferSize;
@@ -760,8 +766,8 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
760766
Source.ReadBuffer(Buffer[0], Bytes);
761767

762768
// The real encryption or decryption routine
763-
CipherProc(Buffer[0], Buffer[0], Bytes);
764-
Dest.WriteBuffer(Buffer[0], Bytes);
769+
CipherProc(Buffer[0], outBuffer[0], Bytes);
770+
Dest.WriteBuffer(outBuffer[0], Bytes);
765771
Dec(DataSize, Bytes);
766772
Inc(Pos, Bytes);
767773

@@ -770,6 +776,8 @@ procedure TDECFormattedCipher.DoEncodeDecodeStream(const Source, Dest: TStream;
770776
end;
771777
finally
772778
ProtectBytes(Buffer);
779+
if (FMode = cmGCM) then
780+
ProtectBytes(outBuffer);
773781
if Assigned(OnProgress) then
774782
OnProgress(Max, Max, Finished);
775783
end;

0 commit comments

Comments
 (0)