Skip to content

Commit b79a688

Browse files
committed
Refactor Blake2b/2s to inherit from TBlockHash
1 parent 2f66a75 commit b79a688

4 files changed

Lines changed: 116 additions & 142 deletions

File tree

HashLib/src/Crypto/HlpBlake2B.pas

Lines changed: 57 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface
77
uses
88
SysUtils,
99
HlpHash,
10+
HlpHashCryptoNotBuildIn,
1011
HlpHashResult,
1112
HlpIHashResult,
1213
HlpIBlake2BParams,
@@ -28,7 +29,7 @@ interface
2829
SWritetoXofAfterReadError = '"%s" Write to Xof after Read not Allowed';
2930

3031
type
31-
TBlake2B = class(THash, ICryptoNotBuildIn, ITransformBlock)
32+
TBlake2B = class(TBlockHash, ICryptoNotBuildIn, ITransformBlock)
3233
strict private
3334

3435
const
@@ -55,11 +56,10 @@ TBlake2B = class(THash, ICryptoNotBuildIn, ITransformBlock)
5556
var
5657
FM: array [0 .. 15] of UInt64;
5758
FState: THashLibUInt64Array;
58-
FBuffer: THashLibByteArray;
59-
FFilledBufferCount: Int32;
6059
FCounter0, FCounter1, FFinalizationFlag0, FFinalizationFlag1: UInt64;
6160

62-
procedure Finish();
61+
procedure Finish(); override;
62+
function GetResult(): THashLibByteArray; override;
6363
function GetName: String; override;
6464

6565
public
@@ -69,10 +69,10 @@ TBlake2B = class(THash, ICryptoNotBuildIn, ITransformBlock)
6969
const ATreeConfig: IBlake2BTreeConfig;
7070
ADoTransformKeyBlock: Boolean = True); overload;
7171
procedure Initialize; override;
72-
procedure TransformBlock(ABlock: PByte);
72+
procedure TransformBlock(AData: PByte; ADataLength: Int32;
73+
AIndex: Int32); override;
7374
procedure TransformBytes(const AData: THashLibByteArray;
7475
AIndex, ADataLength: Int32); override;
75-
function TransformFinal: IHashResult; override;
7676
function CloneInternal(): TBlake2B;
7777
function Clone(): IHash; override;
7878

@@ -136,7 +136,7 @@ TBlake2XB = class sealed(TBlake2B, IXOF)
136136

137137
function ComputeStepLength(): Int32; inline;
138138

139-
function GetResult(): THashLibByteArray;
139+
function GetResult(): THashLibByteArray; reintroduce;
140140

141141
constructor CreateInternal(const AConfig: IBlake2BConfig;
142142
const ATreeConfig: IBlake2BTreeConfig);
@@ -236,12 +236,12 @@ function TBlake2B.CloneInternal(): TBlake2B;
236236
Result := TBlake2B.Create(FConfig.Clone(), LTreeConfig, FDoTransformKeyBlock);
237237
System.Move(FM, Result.FM, System.SizeOf(FM));
238238
Result.FState := System.Copy(FState);
239-
Result.FBuffer := System.Copy(FBuffer);
240-
Result.FFilledBufferCount := FFilledBufferCount;
239+
Result.FBuffer := FBuffer.Clone();
241240
Result.FCounter0 := FCounter0;
242241
Result.FCounter1 := FCounter1;
243242
Result.FFinalizationFlag0 := FFinalizationFlag0;
244243
Result.FFinalizationFlag1 := FFinalizationFlag1;
244+
Result.FProcessedBytesCount := FProcessedBytesCount;
245245
Result.BufferSize := BufferSize;
246246
end;
247247

@@ -282,18 +282,12 @@ constructor TBlake2B.Create(const AConfig: IBlake2BConfig;
282282

283283
System.SetLength(FState, 8);
284284

285-
System.SetLength(FBuffer, BlockSizeInBytes);
286-
287285
inherited Create(FConfig.HashSize, BlockSizeInBytes);
288286
end;
289287

290288
procedure TBlake2B.Finish;
291-
var
292-
LCount: Int32;
293-
LPtrBuffer: PByte;
294289
begin
295-
// Last compression
296-
Blake2BIncrementCounter(UInt64(FFilledBufferCount));
290+
Blake2BIncrementCounter(UInt64(FBuffer.Position));
297291

298292
FFinalizationFlag0 := System.High(UInt64);
299293

@@ -302,15 +296,14 @@ procedure TBlake2B.Finish;
302296
FFinalizationFlag1 := System.High(UInt64);
303297
end;
304298

305-
LCount := System.Length(FBuffer) - FFilledBufferCount;
299+
Compress(PByte(FBuffer.GetBytesZeroPadded()), 0);
300+
end;
306301

307-
if LCount > 0 then
308-
begin
309-
TArrayUtils.Fill(FBuffer, FFilledBufferCount,
310-
LCount + FFilledBufferCount, Byte(0));
311-
end;
312-
LPtrBuffer := PByte(FBuffer);
313-
Compress(LPtrBuffer, 0);
302+
function TBlake2B.GetResult: THashLibByteArray;
303+
begin
304+
System.SetLength(Result, HashSize);
305+
TConverters.le64_copy(PUInt64(FState), 0, PByte(Result), 0,
306+
System.Length(Result));
314307
end;
315308

316309
procedure TBlake2B.Initialize;
@@ -319,6 +312,8 @@ procedure TBlake2B.Initialize;
319312
LBlock: THashLibByteArray;
320313
LRawConfig: THashLibUInt64Array;
321314
begin
315+
inherited Initialize();
316+
322317
LRawConfig := TBlake2BIvBuilder.ConfigB(FConfig, FTreeConfig);
323318
LBlock := nil;
324319

@@ -355,10 +350,6 @@ procedure TBlake2B.Initialize;
355350
FFinalizationFlag0 := 0;
356351
FFinalizationFlag1 := 0;
357352

358-
FFilledBufferCount := 0;
359-
360-
TArrayUtils.ZeroFill(FBuffer);
361-
362353
System.FillChar(FM, System.SizeOf(FM), UInt64(0));
363354

364355
for LIdx := 0 to 7 do
@@ -371,76 +362,72 @@ procedure TBlake2B.Initialize;
371362
if (LBlock <> nil) then
372363
begin
373364
TransformBytes(LBlock, 0, System.Length(LBlock));
374-
TArrayUtils.ZeroFill(LBlock); // burn key from memory
365+
TArrayUtils.ZeroFill(LBlock);
375366
end;
376367
end;
377368
end;
378369

379-
procedure TBlake2B.TransformBlock(ABlock: PByte);
370+
procedure TBlake2B.TransformBlock(AData: PByte; ADataLength: Int32;
371+
AIndex: Int32);
380372
begin
381-
if FFilledBufferCount = BlockSizeInBytes then
373+
if FBuffer.IsFull then
382374
begin
383375
Blake2BIncrementCounter(UInt64(BlockSizeInBytes));
384-
Compress(PByte(FBuffer), 0);
385-
FFilledBufferCount := 0;
376+
Compress(PByte(FBuffer.GetBytes()), 0);
386377
end;
387378
Blake2BIncrementCounter(UInt64(BlockSizeInBytes));
388-
Compress(ABlock, 0);
379+
Compress(AData, AIndex);
389380
end;
390381

391382
procedure TBlake2B.TransformBytes(const AData: THashLibByteArray;
392383
AIndex, ADataLength: Int32);
393384
var
394-
LOffset, LBufferRemaining: Int32;
395-
LPtrData, LPtrBuffer: PByte;
385+
LPtrData: PByte;
396386
begin
397-
LOffset := AIndex;
398-
LBufferRemaining := BlockSizeInBytes - FFilledBufferCount;
387+
{$IFDEF DEBUG}
388+
System.Assert(AIndex >= 0);
389+
System.Assert(ADataLength >= 0);
390+
System.Assert(AIndex + ADataLength <= System.Length(AData));
391+
{$ENDIF DEBUG}
392+
if ADataLength <= 0 then
393+
Exit;
394+
395+
LPtrData := PByte(AData);
399396

400-
if ((FFilledBufferCount > 0) and (ADataLength > LBufferRemaining)) then
397+
if FBuffer.IsFull then
401398
begin
402-
if LBufferRemaining > 0 then
403-
begin
404-
System.Move(AData[LOffset], FBuffer[FFilledBufferCount],
405-
LBufferRemaining);
406-
end;
407399
Blake2BIncrementCounter(UInt64(BlockSizeInBytes));
408-
LPtrBuffer := PByte(FBuffer);
409-
Compress(LPtrBuffer, 0);
410-
LOffset := LOffset + LBufferRemaining;
411-
ADataLength := ADataLength - LBufferRemaining;
412-
FFilledBufferCount := 0;
400+
Compress(PByte(FBuffer.GetBytes()), 0);
413401
end;
414402

415-
LPtrData := PByte(AData);
403+
if (not FBuffer.IsEmpty) then
404+
begin
405+
if FBuffer.Feed(LPtrData, System.Length(AData), AIndex, ADataLength,
406+
FProcessedBytesCount) then
407+
begin
408+
if ADataLength > 0 then
409+
begin
410+
Blake2BIncrementCounter(UInt64(BlockSizeInBytes));
411+
Compress(PByte(FBuffer.GetBytes()), 0);
412+
end;
413+
end;
414+
end;
416415

417-
while (ADataLength > BlockSizeInBytes) do
416+
while (ADataLength > FBuffer.Length) do
418417
begin
419418
Blake2BIncrementCounter(UInt64(BlockSizeInBytes));
420-
Compress(LPtrData, LOffset);
421-
LOffset := LOffset + BlockSizeInBytes;
422-
ADataLength := ADataLength - BlockSizeInBytes;
419+
Compress(LPtrData, AIndex);
420+
AIndex := AIndex + FBuffer.Length;
421+
ADataLength := ADataLength - FBuffer.Length;
423422
end;
424423

425424
if (ADataLength > 0) then
426425
begin
427-
System.Move(AData[LOffset], FBuffer[FFilledBufferCount], ADataLength);
428-
FFilledBufferCount := FFilledBufferCount + ADataLength;
426+
FBuffer.Feed(LPtrData, System.Length(AData), AIndex, ADataLength,
427+
FProcessedBytesCount);
429428
end;
430429
end;
431430

432-
function TBlake2B.TransformFinal: IHashResult;
433-
var
434-
LBuffer: THashLibByteArray;
435-
begin
436-
Finish();
437-
System.SetLength(LBuffer, HashSize);
438-
TConverters.le64_copy(PUInt64(FState), 0, PByte(LBuffer), 0,
439-
System.Length(LBuffer));
440-
Result := THashResult.Create(LBuffer);
441-
Initialize();
442-
end;
443-
444431
function TBlake2B.GetName: String;
445432
begin
446433
Result := Format('%s_%u', [Self.ClassName, Self.HashSize * 8]);
@@ -574,12 +561,12 @@ function TBlake2XB.Clone(): IHash;
574561
// Internal Blake2B Cloning
575562
System.Move(FM, LHashInstance.FM, System.SizeOf(FM));
576563
LHashInstance.FState := System.Copy(FState);
577-
LHashInstance.FBuffer := System.Copy(FBuffer);
578-
LHashInstance.FFilledBufferCount := FFilledBufferCount;
564+
LHashInstance.FBuffer := FBuffer.Clone();
579565
LHashInstance.FCounter0 := FCounter0;
580566
LHashInstance.FCounter1 := FCounter1;
581567
LHashInstance.FFinalizationFlag0 := FFinalizationFlag0;
582568
LHashInstance.FFinalizationFlag1 := FFinalizationFlag1;
569+
LHashInstance.FProcessedBytesCount := FProcessedBytesCount;
583570

584571
Result := LHashInstance;
585572
Result.BufferSize := BufferSize;

HashLib/src/Crypto/HlpBlake2BP.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ procedure TBlake2BP.ProcessLeafLane(AIdx: Int32; APtrData: PByte;
239239

240240
while (LCounter >= StripeSize) do
241241
begin
242-
FLeafHashes[AIdx].TransformBlock(LPtrData);
242+
FLeafHashes[AIdx].TransformBlock(LPtrData, BlockSizeInBytes, 0);
243243
System.Inc(LPtrData, StripeSize);
244244
LCounter := LCounter - StripeSize;
245245
end;

0 commit comments

Comments
 (0)