Skip to content

Commit 408b806

Browse files
Add test cases for streaming Decode/Encode into DEC's GCM test suite.
1 parent f2a7a2d commit 408b806

File tree

1 file changed

+160
-1
lines changed

1 file changed

+160
-1
lines changed

Unit Tests/Tests/TestDECCipherModesGCM.pas

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ TestTDECGCM = class(TTestCase)
223223
published
224224
procedure TestEncode;
225225
procedure TestDecode;
226+
procedure TestDecodeStream;
226227
procedure TestDecodeAuthenticationFailure;
228+
procedure TestEncodeStream;
227229
procedure TestSetGetDataToAuthenticate;
228230
procedure TestSetGetAuthenticationBitLength;
229231
procedure TestGetStandardAuthenticationTagBitLengths;
@@ -472,7 +474,9 @@ procedure TestTDECGCM.TestDecode;
472474
BytesOf(TestDataSet.TestData[i].CT)));
473475
FCipherAES.Done;
474476
except
475-
self.Status('CryptKey ' + string(TestDataSet.TestData[i].CryptKey));
477+
on E: Exception do
478+
Status('CryptKey ' + string(TestDataSet.TestData[i].CryptKey) +
479+
' ' + E.ClassName + ': ' + E.Message);
476480
end;
477481

478482
CheckEquals(string(TestDataSet.TestData[i].PT),
@@ -631,6 +635,161 @@ function TestTDECGCM.IsEqual(const a, b : TBytes):Boolean;
631635
Result := true;
632636
end;
633637

638+
procedure TestTDECGCM.TestDecodeStream;
639+
var
640+
ctbStream: TBytesStream;
641+
ctBytes: TBytes;
642+
TestDataSet : TGCMTestSetEntry;
643+
i : Integer;
644+
DecryptData : TBytes;
645+
ptbStream: TBytesStream;
646+
begin
647+
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV128.rsp', FTestDataList);
648+
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV192.rsp', FTestDataList);
649+
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV256.rsp', FTestDataList);
650+
651+
for TestDataSet in FTestDataList do
652+
begin
653+
for i := Low(TestDataSet.TestData) to High(TestDataSet.TestData) do
654+
begin
655+
ctBytes := TFormat_HexL.Decode(BytesOf(TestDataSet.TestData[i].CT));
656+
657+
// FIXME: Auth tag does not generate for empty CT
658+
if Length(ctBytes) = 0 then
659+
continue;
660+
661+
try
662+
663+
664+
FCipherAES.Init(BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[i].CryptKey)),
665+
BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[i].InitVector)),
666+
$FF);
667+
668+
FCipherAES.AuthenticationResultBitLength := TestDataSet.Taglen;
669+
FCipherAES.DataToAuthenticate := TFormat_HexL.Decode(
670+
BytesOf(
671+
TestDataSet.TestData[i].AAD));
672+
673+
FCipherAES.ExpectedAuthenticationResult :=
674+
TFormat_HexL.Decode(BytesOf(TestDataSet.TestData[i].TagResult));
675+
676+
ctbStream := TBytesStream.Create(ctBytes);
677+
ptbStream := TBytesStream.Create;
678+
679+
FCipherAES.DecodeStream(ctbStream, ptbStream, ctbStream.Size);
680+
681+
FCipherAES.Done;
682+
683+
DecryptData := ptbStream.Bytes;
684+
SetLength(DecryptData, ptbStream.Size);
685+
686+
except
687+
on E: Exception do
688+
Status('CryptKey ' + string(TestDataSet.TestData[i].CryptKey) +
689+
' ' + E.ClassName + ': ' + E.Message);
690+
end;
691+
FreeAndNil(ptbStream);
692+
FreeAndNil(ctbStream);
693+
694+
CheckEquals(string(TestDataSet.TestData[i].PT),
695+
StringOf(TFormat_HexL.Encode(DecryptData)),
696+
'Plaintext wrong for key ' +
697+
string(TestDataSet.TestData[i].CryptKey) + ' IV ' +
698+
string(TestDataSet.TestData[i].InitVector) + ' PT ' +
699+
string(TestDataSet.TestData[i].PT) + ' AAD ' +
700+
string(TestDataSet.TestData[i].AAD) + ' Exp.: ' +
701+
string(TestDataSet.TestData[i].CT) + ' Act.: ' +
702+
StringOf(TFormat_HexL.Encode(DecryptData)));
703+
704+
// Additional Authentication Data prüfen
705+
CheckEquals(string(TestDataSet.TestData[i].TagResult),
706+
StringOf(TFormat_HexL.Encode(FCipherAES.CalculatedAuthenticationResult)),
707+
'Authentication tag wrong for key ' +
708+
string(TestDataSet.TestData[i].CryptKey) + ' IV ' +
709+
string(TestDataSet.TestData[i].InitVector) + ' PT ' +
710+
string(TestDataSet.TestData[i].PT) + ' AAD ' +
711+
string(TestDataSet.TestData[i].AAD) + ' Exp.: ' +
712+
string(TestDataSet.TestData[i].TagResult) + ' Act.: ' +
713+
StringOf(TFormat_HexL.Encode(FCipherAES.DataToAuthenticate)));
714+
715+
end;
716+
end;
717+
end;
718+
719+
procedure TestTDECGCM.TestEncodeStream;
720+
var
721+
ctbStream: TBytesStream;
722+
ptBytes: TBytes;
723+
TestDataSet : TGCMTestSetEntry;
724+
i : Integer;
725+
EncryptData : TBytes;
726+
ptbStream: TBytesStream;
727+
begin
728+
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV128.rsp', FTestDataList);
729+
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV192.rsp', FTestDataList);
730+
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV256.rsp', FTestDataList);
731+
732+
for TestDataSet in FTestDataList do
733+
begin
734+
for i := Low(TestDataSet.TestData) to High(TestDataSet.TestData) do
735+
begin
736+
ptBytes := TFormat_HexL.Decode(BytesOf(TestDataSet.TestData[i].PT));
737+
738+
// FIXME: Auth tag does not generate for empty PT
739+
if Length(ptBytes) = 0 then
740+
continue;
741+
742+
FCipherAES.Init(BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[i].CryptKey)),
743+
BytesOf(TFormat_HexL.Decode(TestDataSet.TestData[i].InitVector)),
744+
$FF);
745+
746+
FCipherAES.AuthenticationResultBitLength := TestDataSet.Taglen;
747+
FCipherAES.DataToAuthenticate := TFormat_HexL.Decode(
748+
BytesOf(
749+
TestDataSet.TestData[i].AAD));
750+
751+
ptbStream := TBytesStream.Create(ptBytes);
752+
ctbStream := TBytesStream.Create;
753+
try
754+
FCipherAES.EncodeStream(ptbStream, ctbStream, ptbStream.Size);
755+
756+
FCipherAES.Done;
757+
758+
EncryptData := ctbStream.Bytes;
759+
SetLength(EncryptData, ctbStream.Size);
760+
except
761+
on E: Exception do
762+
Status('CryptKey ' + string(TestDataSet.TestData[i].CryptKey) +
763+
' ' + E.ClassName + ': ' + E.Message);
764+
end;
765+
766+
FreeAndNil(ptbStream);
767+
FreeAndNil(ctbStream);
768+
769+
CheckEquals(string(TestDataSet.TestData[i].CT),
770+
StringOf(TFormat_HexL.Encode(EncryptData)),
771+
'Cipher text wrong for Key ' +
772+
string(TestDataSet.TestData[i].CryptKey) + ' IV ' +
773+
string(TestDataSet.TestData[i].InitVector) + ' PT ' +
774+
string(TestDataSet.TestData[i].PT) + ' AAD ' +
775+
string(TestDataSet.TestData[i].AAD) + ' Exp.: ' +
776+
string(TestDataSet.TestData[i].CT) + ' Act.: ' +
777+
StringOf(TFormat_HexL.Encode(EncryptData)));
778+
779+
// Additional Authentication Data prüfen
780+
CheckEquals(string(TestDataSet.TestData[i].TagResult),
781+
StringOf(TFormat_HexL.Encode(FCipherAES.CalculatedAuthenticationResult)),
782+
'Authentication tag wrong for Key ' +
783+
string(TestDataSet.TestData[i].CryptKey) + ' IV ' +
784+
string(TestDataSet.TestData[i].InitVector) + ' PT ' +
785+
string(TestDataSet.TestData[i].PT) + ' AAD ' +
786+
string(TestDataSet.TestData[i].AAD) + ' Exp.: ' +
787+
string(TestDataSet.TestData[i].TagResult) + ' Act.: ' +
788+
StringOf(TFormat_HexL.Encode(FCipherAES.DataToAuthenticate)));
789+
end;
790+
end;
791+
end;
792+
634793
procedure TestTDECGCM.TestGetStandardAuthenticationTagBitLengths;
635794
var
636795
BitLengths: TStandardBitLengths;

0 commit comments

Comments
 (0)