Skip to content

Commit 5106814

Browse files
committed
Work on unit tests
1 parent 91d6e66 commit 5106814

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

Source/DECHash.pas

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,9 @@ THash_SHA3Base = class(TDECHashBit)
470470
/// Value used for padding if the length of the message to be hashed
471471
/// is not a multiple of 8 bit bytes.
472472
/// </param>
473-
/// <param name="BitLen">
474-
/// Number of needed padding bits?
473+
/// <param name="Bitlen">
474+
/// Length of the final byte in bit. Required for supporting message
475+
/// lengths which are not a multiple of 8 bits.
475476
/// </param>
476477
/// <param name="HashValue">
477478
/// The hash value which shall be updated by this method

Unit Tests/Tests/TestDECCipherModesGCM.pas

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ TestTDECGCM = class(TTestCase)
235235
procedure TestDecodeAuthenticationFailure;
236236
procedure TestEncodeStream;
237237
procedure TestEncodeLargeStream;
238+
procedure TestEncodeStreamChunked;
238239
procedure TestSetGetDataToAuthenticate;
239240
procedure TestSetGetAuthenticationBitLength;
240241
procedure TestGetStandardAuthenticationTagBitLengths;
@@ -733,6 +734,13 @@ procedure TestTDECGCM.TestEncodeStream;
733734
DoTestEncodeStream_LoadAndTestCAVSData(-1);
734735
end;
735736

737+
procedure TestTDECGCM.TestEncodeStreamChunked;
738+
begin
739+
// Use cipher block size as max chunk size
740+
DoTestEncodeStream_LoadAndTestCAVSData(
741+
Max(FCipherAES.Context.BlockSize, FCipherAES.Context.BufferSize));
742+
end;
743+
736744
procedure TestTDECGCM.DoTestEncodeStream_LoadAndTestCAVSData(const
737745
aMaxChunkSize: Int64);
738746
var
@@ -761,7 +769,9 @@ procedure TestTDECGCM.TestEncodeLargeStream;
761769
FTestDataLoader.LoadFile('..\..\Unit Tests\Data\gcmEncryptExtIV256_large.rsp',
762770
FTestDataList, True);
763771
Status('Encode large stream using chunking');
764-
Assert(StreamBufferSize = 8192, 'Might need to update data set to have enough data!');
772+
CheckEquals(8192, StreamBufferSize, 'Might need to update data set to have enough data!');
773+
{ TODO : Auskommentierten Code entfernen }
774+
// Assert(StreamBufferSize = 8192, 'Might need to update data set to have enough data!');
765775
DoTestEncodeStream_TestSingleSet(0, 0, StreamBufferSize);
766776
Status('Encode large stream without chunking');
767777
DoTestEncodeStream_TestSingleSet(0, 0, -1);

Unit Tests/Tests/TestDECHashSHA3.pas

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{*****************************************************************************
1+
{*****************************************************************************
22
The DEC team (see file NOTICE.txt) licenses this file
33
to you under the Apache License, Version 2.0 (the
44
"License"); you may not use this file except in compliance
@@ -295,7 +295,6 @@ TestTHash_Keccak_Base = class(TestTHash_SHA3_Base)
295295
/// <summary>
296296
/// Adds the SHA3 padding sheme to an input vector so that calculating
297297
/// the hash using Keccak instead of SHA3 provides the same result.
298-
/// Deliberately empty here as only implemented in class TestTHash_Keccak_Base.
299298
/// </summary>
300299
/// <param name="SHA3InputVector">
301300
/// The SHA3 input vector in bytes (not hex encoded!) which shall get
@@ -507,23 +506,15 @@ procedure TestTHash_SHA3_Base.LoadTestDataFile(FileName : string;
507506
FinalByteLen : UInt8;
508507
HashLength : Int16;
509508
lDataRow : IHashTestDataRowSetup;
510-
511-
//NewContents: TStringList;
512-
U : RawByteString;
513509
begin
514510
Len := 0;
515511
Contents := TStringList.Create;
516-
//NewContents := TStringList.Create;
512+
517513
try
518514
Contents.LoadFromFile(FileName);
519515

520516
for FileRow in Contents do
521517
begin
522-
//if FileRow.StartsWith('MDuni') then
523-
// Continue
524-
//else
525-
// NewContents.Add(FileRow);
526-
527518
FileRowTrim := LowerCase(Trim(FileRow));
528519

529520
// # denotes comments
@@ -571,6 +562,9 @@ procedure TestTHash_SHA3_Base.LoadTestDataFile(FileName : string;
571562
else
572563
begin
573564
FinalByteLen := 0;
565+
{ TODO :
566+
Problem: here the method from the base class is called instead the
567+
overwritten one from Keccack... }
574568
MsgWithFixup := AddLastByteForKeccakTest('', FinalByteLen);
575569
lDataRow.AddInputVector(MsgWithFixup);
576570
lDataRow.FinalBitLength := FinalByteLen;
@@ -675,7 +669,7 @@ procedure TestTHash_SHA3_224.SetUp;
675669
FTestFileNames.Add('..\..\Unit Tests\Data\SHA3_224LongMsg.rsp');
676670
// SourceEnd
677671

678-
// Für Unittests für CalcStream verschoben Start
672+
// Für Unittests für CalcStream verschoben Start
679673
// Source https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-
680674
// and-Guidelines/documents/examples/SHA3-224_Msg5.pdf
681675
lDataRow := FTestData.AddRow;
@@ -695,7 +689,7 @@ procedure TestTHash_SHA3_224.SetUp;
695689
'14ce743c5641cebe';
696690
lDataRow.AddInputVector(#$53#$58#$7B#$19);
697691
lDataRow.FinalBitLength := 6;
698-
// Für Unittests für CalcStream verschoben Ende
692+
// Für Unittests für CalcStream verschoben Ende
699693

700694
// Source: https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-
701695
// and-Guidelines/documents/examples/SHA3-224_1600.pdf
@@ -1749,11 +1743,9 @@ procedure TestTHash_Keccak_Base.AddLastByteForCodeTest(var lDataRow : IHashTe
17491743
SHA3InputVector : RawByteString;
17501744
LastByteLength : UInt8);
17511745
var
1752-
LastByteLen : UInt8;
1753-
MsgWithFixup : RawByteString;
1746+
LastByteLen : UInt8;
1747+
MsgWithFixup : RawByteString;
17541748
begin
1755-
lDataRow.FinalBitLength := LastByteLength;
1756-
LastByteLen := LastByteLength;
17571749
MsgWithFixup := AddLastByteForKeccakTest(SHA3InputVector, LastByteLen);
17581750
lDataRow.AddInputVector(MsgWithFixup);
17591751
lDataRow.FinalBitLength := LastByteLen;
@@ -1777,9 +1769,9 @@ function TestTHash_Keccak_Base.AddLastByteForKeccakTest(SHA3InputVector : Raw
17771769
1..6 :
17781770
begin
17791771
lastbyte := UInt8(SHA3InputVector[High(SHA3InputVector)]);
1780-
// in lastbyte 0 an stelle fblSHA3 einfügen:
1772+
// in lastbyte 0 an stelle fblSHA3 einfügen:
17811773
lastbyte := lastbyte and (( 1 shl LastByteLength ) xor $FF);
1782-
// in lastbyte 1 an stelle fblSHA3+1 einfügen:
1774+
// in lastbyte 1 an stelle fblSHA3+1 einfügen:
17831775
lastbyte := lastbyte or BYTE( 1 shl (LastByteLength + 1));
17841776
SHA3InputVector[High(SHA3InputVector)] := Ansichar(lastbyte);
17851777
if LastByteLength < 6 then
@@ -1788,11 +1780,11 @@ function TestTHash_Keccak_Base.AddLastByteForKeccakTest(SHA3InputVector : Raw
17881780
LastByteLength := 0;
17891781
end;
17901782
7 : begin // ist ok
1791-
// 0 anhängen - es könnte sein, dass in mSHA3 eine 1 steht
1783+
// 0 anhängen - es könnte sein, dass in mSHA3 eine 1 steht
17921784
// wenn man sicher ist, dass dies nie der Fall ist, dann kann
17931785
// man auf die vier Zeilen verzichten
17941786
lastbyte := UInt8(SHA3InputVector[High(SHA3InputVector)]);
1795-
lastbyte := lastbyte and $7F; // evt vorhandene 1 an vorderster Stelle löschen
1787+
lastbyte := lastbyte and $7F; // evt vorhandene 1 an vorderster Stelle löschen
17961788
SHA3InputVector[High(SHA3InputVector)] := Ansichar(lastbyte);
17971789

17981790
SHA3InputVector := SHA3InputVector + chr($01);
@@ -1826,15 +1818,16 @@ procedure TestTHash_Keccak_224.SetUp;
18261818
//Source https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-
18271819
// Validation-Program/documents/sha3/sha-3bittestvectors.zip
18281820
FTestFileNames.Add('..\..\Unit Tests\Data\SHA3_224ShortMsg.rsp');
1829-
FTestFileNames.Add('..\..\Unit Tests\Data\SHA3_224LongMsg.rsp');
1821+
// FTestFileNames.Add('..\..\Unit Tests\Data\SHA3_224LongMsg.rsp');
18301822
// SourceEnd
18311823

18321824
// Source https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-
18331825
// and-Guidelines/documents/examples/SHA3-224_Msg5.pdf
18341826
lDataRow := FTestData.AddRow;
18351827
lDataRow.ExpectedOutput := 'ffbad5da96bad71789330206dc6768ecaeb1b32d' +
18361828
'ca6b3301489674ab';
1837-
AddLastByteForCodeTest(lDataRow, #$13, 5);
1829+
AddLastByteForCodeTest(lDataRow, #$19, 5);
1830+
18381831

18391832
// Source https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-
18401833
// and-Guidelines/documents/examples/SHA3-224_Msg30.pdf

0 commit comments

Comments
 (0)