Skip to content

Commit 9142e51

Browse files
committed
Fix remaining issues from my battle with Git
Pull request #82 should be completely applied now
1 parent 5226550 commit 9142e51

File tree

2 files changed

+86
-70
lines changed

2 files changed

+86
-70
lines changed

Source/DECCipherPaddings.pas

Lines changed: 84 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ TPadding = class abstract
7575
// </para>
7676
/// </remarks>
7777
class function AddPadding(const Data : string;
78-
BlockSize : Integer): string; overload; virtual;
78+
BlockSize : Integer): string; overload; virtual; abstract;
7979
// <summary>
8080
/// Adds padding to a raw byte string.
8181
/// </summary>
@@ -95,7 +95,7 @@ TPadding = class abstract
9595
/// </para>
9696
/// </remarks>
9797
class function AddPadding(const Data : RawByteString;
98-
BlockSize : Integer): RawByteString; overload; virtual;
98+
BlockSize : Integer): RawByteString; overload; virtual; abstract;
9999
/// <summary>
100100
/// Checks if the specified data contains valid padding.
101101
/// </summary>
@@ -154,7 +154,7 @@ TPadding = class abstract
154154
/// </para>
155155
/// </remarks>
156156
class function RemovePadding(const Data : RawByteString;
157-
BlockSize : Integer): RawByteString; overload; virtual;
157+
BlockSize : Integer): RawByteString; overload; virtual; abstract;
158158
// <summary>
159159
/// Removes padding from a string.
160160
/// </summary>
@@ -179,7 +179,7 @@ TPadding = class abstract
179179
/// </para>
180180
/// </remarks>
181181
class function RemovePadding(const Data : string;
182-
BlockSize : Integer): string; overload; virtual;
182+
BlockSize : Integer): string; overload; virtual; abstract;
183183
end;
184184

185185
/// <summary>
@@ -318,6 +318,60 @@ TFixedBytePadding = class abstract(TPadding)
318318
/// </returns>
319319
class function RemovePadding(const Data : TBytes;
320320
BlockSize : Integer): TBytes; override;
321+
// <summary>
322+
/// Removes a fixed byte padding from a raw byte string.
323+
/// </summary>
324+
/// <param name="data">
325+
/// The padded byte raw byte string.
326+
/// </param>
327+
/// <param name="BlockSize">
328+
/// The block size in bytes used for padding.
329+
/// </param>
330+
/// <returns>
331+
/// A new raw byte string with the padding removed. Raises an exception
332+
/// if the padding is invalid.
333+
/// </returns>
334+
/// <exception cref="EDECCipherException">
335+
/// Raised if the padding is invalid or missing.
336+
/// </exception>
337+
/// <remarks>
338+
/// This function checks for valid a fixed byte padding (depending on the
339+
/// derrived class) and raises an `EDECCipherException` exception if the
340+
/// padding is incorrect. This includes cases where the final bytes do not
341+
/// match the pad count or if the pad count is greater than the block size.
342+
/// <para>
343+
/// Call this method after decryption.
344+
/// </para>
345+
/// </remarks>
346+
class function RemovePadding(const Data : RawByteString;
347+
BlockSize : Integer): RawByteString; override;
348+
// <summary>
349+
/// Removes a fixed byte padding from a string.
350+
/// </summary>
351+
/// <param name="data">
352+
/// The padded byte raw byte string.
353+
/// </param>
354+
/// <param name="BlockSize">
355+
/// The block size in bytes used for padding.
356+
/// </param>
357+
/// <returns>
358+
/// A new raw byte string with the padding removed. Raises an exception
359+
/// if the padding is invalid.
360+
/// </returns>
361+
/// <exception cref="EDECCipherException">
362+
/// Raised if the padding is invalid or missing.
363+
/// </exception>
364+
/// <remarks>
365+
/// This function checks for valid a fixed byte padding (depending on the
366+
/// derrived class) and raises an `EDECCipherException` exception if the
367+
/// padding is incorrect. This includes cases where the final bytes do not
368+
/// match the pad count or if the pad count is greater than the block size.
369+
/// <para>
370+
/// Call this method after decryption.
371+
/// </para>
372+
/// </remarks>
373+
class function RemovePadding(const Data : string;
374+
BlockSize : Integer): string; override;
321375
end;
322376

323377
/// <summary>
@@ -397,8 +451,7 @@ TPKCS5Padding = class(TPKCS7Padding)
397451
end;
398452

399453
/// <summary>
400-
/// Implementation of the ANSI X9.23 padding algorithm. This is the more
401-
/// commonly used variant of this algorithm.
454+
/// Implementation of the ANSI X9.23 padding algorithm.
402455
/// </summary>
403456
/// <remarks>
404457
/// ANSI X9.23 padding is a standard algorithm used in symmetric cryptosystems
@@ -588,6 +641,26 @@ class function TFixedBytePadding.AddPadding(const Data : TBytes;
588641
Result[I] := GetPaddingByte(PadLength, I = High(Result));
589642
end;
590643

644+
class function TFixedBytePadding.AddPadding(const Data : string;
645+
BlockSize : Integer): string;
646+
var
647+
Buf: TBytes;
648+
begin
649+
Buf := AddPadding(StringToBytes(Data), BlockSize);
650+
Result := BytesToString(Buf);
651+
ProtectBytes(Buf);
652+
end;
653+
654+
class function TFixedBytePadding.AddPadding(const Data : RawByteString;
655+
BlockSize : Integer): RawByteString;
656+
var
657+
Buf: TBytes;
658+
begin
659+
Buf := AddPadding(RawStringToBytes(Data), BlockSize);
660+
Result := BytesToRawString(Buf);
661+
ProtectBytes(Buf);
662+
end;
663+
591664
class function TFixedBytePadding.HasValidPadding(const Data : TBytes;
592665
BlockSize : Integer): Boolean;
593666
var
@@ -625,75 +698,18 @@ class function TFixedBytePadding.RemovePadding(const Data : TBytes;
625698
Move(Data[0], Result[0], Length(Result));
626699
end;
627700

628-
{ TANSI_X9_23_Padding }
629-
630-
class function TANSI_X9_23_Padding.GetPaddingByte(PaddingLength: Integer): UInt8;
631-
begin
632-
Result := 0;
633-
end;
634-
635-
{ TANSI_X9_23_Padding_Legacy }
636-
637-
class function TANSI_X9_23_Padding_Legacy.AddPadding(const Data : TBytes;
638-
BlockSize : Integer): TBytes;
639-
var
640-
PadLength: Integer;
641-
I: Integer;
642-
begin
643-
PadLength := BlockSize - (Length(Data) mod BlockSize);
644-
SetLength(Result, Length(Data) + PadLength);
645-
if Length(Data) > 0 then
646-
Move(Data[0], Result[0], Length(Data));
647-
648-
for I := Length(Data) to High(Result) do
649-
Result[I] := GetPaddingByte(PadLength);
650-
end;
651-
652-
class function TANSI_X9_23_Padding_Legacy.GetPaddingByte(PaddingLength: Integer): UInt8;
653-
begin
654-
Result := Random(256);
655-
end;
656-
657-
class function TANSI_X9_23_Padding_Legacy.HasValidPadding(const Data : TBytes;
658-
BlockSize : Integer): Boolean;
659-
var
660-
PadLength : Integer;
661-
begin
662-
if Length(Data) = 0 then
663-
exit(false);
664-
665-
PadLength := Data[High(Data)];
666-
if (PadLength <= 0) or (PadLength > BlockSize) then
667-
exit(false);
668-
669-
// do not check padding byte, as that one is random for this variant
670-
Result := true;
671-
end;
672-
673-
{ TPadding }
674-
675-
class function TPadding.AddPadding(const Data : string;
676-
BlockSize : Integer): string;
677-
var
678-
Buf: TBytes;
679-
begin
680-
Buf := AddPadding(StringToBytes(Data), BlockSize);
681-
Result := BytesToString(Buf);
682-
ProtectBytes(Buf);
683-
end;
684-
685-
class function TPadding.AddPadding(const Data : RawByteString;
686-
BlockSize : Integer): RawByteString;
701+
class function TFixedBytePadding.RemovePadding(const Data : RawByteString;
702+
BlockSize : Integer): RawByteString;
687703
var
688704
Buf: TBytes;
689705
begin
690-
Buf := AddPadding(RawStringToBytes(Data), BlockSize);
706+
Buf := RemovePadding(RawStringToBytes(Data), BlockSize);
691707
Result := BytesToRawString(Buf);
692708
ProtectBytes(Buf);
693709
end;
694710

695-
class function TPadding.RemovePadding(const Data : string;
696-
BlockSize : Integer): string;
711+
class function TFixedBytePadding.RemovePadding(const Data : string;
712+
BlockSize : Integer): string;
697713
var
698714
Buf: TBytes;
699715
begin

Unit Tests/Tests/TestDECCipherPaddings.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ initialization
247247
{$IFDEF DUnitX}
248248
TDUnitX.RegisterTestFixture(TestTDECPKCS7Padding);
249249
TDUnitX.RegisterTestFixture(TestTANSI_X9_23Padding);
250-
TDUnitX.RegisterTestFixture(TestTANSI_X9_23Padding_Legacy);
251250
{$ELSE}
252-
RegisterTests('DECCipherPaddings', [TestTPKCS7Padding.Suite, TestTANSI_X9_23Padding.Suite]);
251+
RegisterTests('DECCipherPaddings', [TestTPKCS7Padding.Suite,
252+
TestTANSI_X9_23Padding.Suite]);
253253
{$ENDIF}
254254
end.

0 commit comments

Comments
 (0)