Skip to content

Commit af94b8b

Browse files
Extract test logic for a single data set from TestTDECGCM.TestEncodeStream as DoTestEncodeStream_TestSingleSet. Move data set related knowledge into DoTestEncodeStream_LoadAndTestCAVSData, which will then test each set using DoTestEncodeStream_TestSingleSet. DoTestEncodeStream_TestSingleSet now allows to optionally use data chunking instead of processing whole stream at once. Also addressed issue where failed test would mislabeled printed data in error message.
1 parent 0da3dbf commit af94b8b

File tree

1 file changed

+81
-51
lines changed

1 file changed

+81
-51
lines changed

Unit Tests/Tests/TestDECCipherModesGCM.pas

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface
2929
{$ELSE}
3030
TestFramework,
3131
{$ENDIF}
32-
System.SysUtils, Generics.Collections,
32+
System.SysUtils, Generics.Collections, System.Math,
3333
DECCipherBase, DECCipherModes, DECCipherFormats, DECCiphers;
3434

3535
type
@@ -217,6 +217,9 @@ TestTDECGCM = class(TTestCase)
217217
private
218218
function IsEqual(const a, b: TBytes): Boolean;
219219
procedure DoTestDecodeFailure;
220+
procedure DoTestEncodeStream_LoadAndTestCAVSData(const aMaxChunkSize: Int64);
221+
procedure DoTestEncodeStream_TestSingleSet(const aSetIndex, aDataIndex:
222+
Integer; const aMaxChunkSize: Int64 = -1);
220223
public
221224
procedure SetUp; override;
222225
procedure TearDown; override;
@@ -713,73 +716,100 @@ procedure TestTDECGCM.TestDecodeStream;
713716
end;
714717

715718
procedure TestTDECGCM.TestEncodeStream;
716-
var
717-
ctbStream: TBytesStream;
718-
ptBytes: TBytes;
719-
TestDataSet : TGCMTestSetEntry;
719+
begin
720+
// -1 to disable chunking
721+
DoTestEncodeStream_LoadAndTestCAVSData(-1);
722+
end;
723+
724+
procedure TestTDECGCM.DoTestEncodeStream_LoadAndTestCAVSData(const
725+
aMaxChunkSize: Int64);
726+
var
720727
i : Integer;
721-
EncryptData : TBytes;
722-
ptbStream: TBytesStream;
728+
TestDataSet : TGCMTestSetEntry;
729+
curSetIndex: Integer;
723730
begin
724731
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV128.rsp', FTestDataList);
725732
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV192.rsp', FTestDataList);
726733
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV256.rsp', FTestDataList);
727734

728-
for TestDataSet in FTestDataList do
735+
for curSetIndex := 0 to FTestDataList.Count - 1 do
729736
begin
737+
TestDataSet := FTestDataList[curSetIndex];
730738
for i := Low(TestDataSet.TestData) to High(TestDataSet.TestData) do
731739
begin
732-
ptBytes := TFormat_HexL.Decode(BytesOf(TestDataSet.TestData[i].PT));
740+
DoTestEncodeStream_TestSingleSet(curSetIndex, i, aMaxChunkSize);
741+
end;
742+
end;
743+
end;
744+
745+
procedure TestTDECGCM.DoTestEncodeStream_TestSingleSet(const aSetIndex,
746+
aDataIndex: Integer; const aMaxChunkSize: Int64 = -1);
747+
var
748+
ctbStream: TBytesStream;
749+
curChunkSize: Int64;
750+
dataLeftToEncode: Int64;
751+
ptBytes: TBytes;
752+
TestDataSet : TGCMTestSetEntry;
753+
EncryptData : TBytes;
754+
ptbStream: TBytesStream;
755+
begin
756+
TestDataSet := FTestDataList[aSetIndex];
757+
758+
ptBytes := TFormat_HexL.Decode(BytesOf(TestDataSet.TestData[aDataIndex].PT));
733759

734-
FCipherAES.Init(BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[i].CryptKey)),
735-
BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[i].InitVector)),
736-
$FF);
760+
FCipherAES.Init(BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[aDataIndex].CryptKey)),
761+
BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[aDataIndex].InitVector)),
762+
$FF);
737763

738-
FCipherAES.AuthenticationResultBitLength := TestDataSet.Taglen;
739-
FCipherAES.DataToAuthenticate := TFormat_HexL.Decode(
740-
BytesOf(
741-
TestDataSet.TestData[i].AAD));
764+
FCipherAES.AuthenticationResultBitLength := TestDataSet.Taglen;
765+
FCipherAES.DataToAuthenticate := TFormat_HexL.Decode(
766+
BytesOf(
767+
TestDataSet.TestData[aDataIndex].AAD));
742768

743-
ptbStream := TBytesStream.Create(ptBytes);
744-
ctbStream := TBytesStream.Create;
745-
try
746-
FCipherAES.EncodeStream(ptbStream, ctbStream, ptbStream.Size);
769+
ptbStream := TBytesStream.Create(ptBytes);
770+
ctbStream := TBytesStream.Create;
771+
try
772+
dataLeftToEncode := ptbStream.Size;
773+
curChunkSize := dataLeftToEncode;
774+
repeat
775+
// Apply chunking if needed
776+
if aMaxChunkSize > 0 then
777+
curChunkSize := Min(dataLeftToEncode, aMaxChunkSize);
778+
FCipherAES.EncodeStream(ptbStream, ctbStream, curChunkSize);
779+
Dec(dataLeftToEncode, curChunkSize);
780+
until (dataLeftToEncode = 0);
747781

748-
FCipherAES.Done;
782+
FCipherAES.Done;
749783

750-
EncryptData := ctbStream.Bytes;
751-
SetLength(EncryptData, ctbStream.Size);
752-
except
753-
on E: Exception do
754-
Status('CryptKey ' + string(TestDataSet.TestData[i].CryptKey) +
755-
' ' + E.ClassName + ': ' + E.Message);
756-
end;
784+
EncryptData := ctbStream.Bytes;
785+
SetLength(EncryptData, ctbStream.Size);
786+
except
787+
on E: Exception do
788+
Status('CryptKey ' + string(TestDataSet.TestData[aDataIndex].CryptKey) +
789+
' ' + E.ClassName + ': ' + E.Message);
790+
end;
757791

758-
FreeAndNil(ptbStream);
759-
FreeAndNil(ctbStream);
792+
FreeAndNil(ptbStream);
793+
FreeAndNil(ctbStream);
760794

761-
CheckEquals(string(TestDataSet.TestData[i].CT),
762-
StringOf(TFormat_HexL.Encode(EncryptData)),
763-
'Cipher text wrong for Key ' +
764-
string(TestDataSet.TestData[i].CryptKey) + ' IV ' +
765-
string(TestDataSet.TestData[i].InitVector) + ' PT ' +
766-
string(TestDataSet.TestData[i].PT) + ' AAD ' +
767-
string(TestDataSet.TestData[i].AAD) + ' Exp.: ' +
768-
string(TestDataSet.TestData[i].CT) + ' Act.: ' +
769-
StringOf(TFormat_HexL.Encode(EncryptData)));
795+
CheckEquals(string(TestDataSet.TestData[aDataIndex].CT),
796+
StringOf(TFormat_HexL.Encode(EncryptData)),
797+
'Cipher text wrong for Key ' +
798+
string(TestDataSet.TestData[aDataIndex].CryptKey) + ' IV ' +
799+
string(TestDataSet.TestData[aDataIndex].InitVector) + ' PT ' +
800+
string(TestDataSet.TestData[aDataIndex].PT) + ' AAD Exp.: ' +
801+
string(TestDataSet.TestData[aDataIndex].AAD) + ' Act.: ' +
802+
StringOf(TFormat_HexL.Encode(FCipherAES.DataToAuthenticate)));
770803

771-
// Additional Authentication Data prüfen
772-
CheckEquals(string(TestDataSet.TestData[i].TagResult),
773-
StringOf(TFormat_HexL.Encode(FCipherAES.CalculatedAuthenticationResult)),
774-
'Authentication tag wrong for Key ' +
775-
string(TestDataSet.TestData[i].CryptKey) + ' IV ' +
776-
string(TestDataSet.TestData[i].InitVector) + ' PT ' +
777-
string(TestDataSet.TestData[i].PT) + ' AAD ' +
778-
string(TestDataSet.TestData[i].AAD) + ' Exp.: ' +
779-
string(TestDataSet.TestData[i].TagResult) + ' Act.: ' +
780-
StringOf(TFormat_HexL.Encode(FCipherAES.DataToAuthenticate)));
781-
end;
782-
end;
804+
// Additional Authentication Data prüfen
805+
CheckEquals(string(TestDataSet.TestData[aDataIndex].TagResult),
806+
StringOf(TFormat_HexL.Encode(FCipherAES.CalculatedAuthenticationResult)),
807+
'Authentication tag wrong for Key ' +
808+
string(TestDataSet.TestData[aDataIndex].CryptKey) + ' IV ' +
809+
string(TestDataSet.TestData[aDataIndex].InitVector) + ' PT ' +
810+
string(TestDataSet.TestData[aDataIndex].PT) + ' AAD Exp.: ' +
811+
string(TestDataSet.TestData[aDataIndex].AAD) + ' Act.: ' +
812+
StringOf(TFormat_HexL.Encode(FCipherAES.DataToAuthenticate)));
783813
end;
784814

785815
procedure TestTDECGCM.TestGetStandardAuthenticationTagBitLengths;

0 commit comments

Comments
 (0)