Skip to content

Commit 0da30da

Browse files
committed
Replace PByteArray with PUInt8Array
In order to avoid overflows etc. with larger data
1 parent 144ec14 commit 0da30da

File tree

10 files changed

+92
-71
lines changed

10 files changed

+92
-71
lines changed

Source/DECCipherBase.pas

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface
2626
{$ELSE}
2727
System.SysUtils, System.Classes, Generics.Collections,
2828
{$ENDIF}
29-
DECBaseClass, DECFormatBase;
29+
DECBaseClass, DECFormatBase, DECTypes;
3030

3131
type
3232
/// <summary>
@@ -221,7 +221,7 @@ TDECCipher = class(TDECObject)
221221
/// This is the complete memory block containing FInitializationVector,
222222
/// FFeedback, FBuffer and FAdditionalBuffer
223223
/// </summary>
224-
FData : PByteArray;
224+
FData : PUInt8Array;
225225
/// <summary>
226226
/// This is the size of FData in byte
227227
/// </summary>
@@ -253,7 +253,7 @@ TDECCipher = class(TDECObject)
253253
/// to work with. Some other methods like Done or Valid cipher need to pass
254254
/// a buffer as parameter as that is ecpected by the called method.
255255
/// </summary>
256-
FBuffer: PByteArray;
256+
FBuffer: PUInt8Array;
257257

258258
/// <summary>
259259
/// Initialization vector. When using cipher modes to derive a stream
@@ -262,7 +262,7 @@ TDECCipher = class(TDECObject)
262262
/// is no such encrypted data yet, so this initialization vector fills this
263263
/// "gap".
264264
/// </summary>
265-
FInitializationVector: PByteArray;
265+
FInitializationVector: PUInt8Array;
266266

267267
/// <summary>
268268
/// Size of the initialization vector in byte. Required for algorithms
@@ -277,7 +277,7 @@ TDECCipher = class(TDECObject)
277277
/// block. It may be XORed with the next block cipher text for isntance.
278278
/// That data "going into the next block encryption" is this feedback array
279279
/// </summary>
280-
FFeedback: PByteArray;
280+
FFeedback: PUInt8Array;
281281

282282
/// <summary>
283283
/// Size of FAdditionalBuffer in Byte
@@ -720,7 +720,7 @@ TDECCipher = class(TDECObject)
720720
/// <summary>
721721
/// Provides access to the contents of the initialization vector
722722
/// </summary>
723-
property InitVector: PByteArray
723+
property InitVector: PUInt8Array
724724
read FInitializationVector;
725725

726726
/// <summary>
@@ -732,7 +732,7 @@ TDECCipher = class(TDECObject)
732732
/// feedback array. The size usually depends on the block size of the
733733
/// cipher algorithm.
734734
/// </summary>
735-
property Feedback: PByteArray
735+
property Feedback: PUInt8Array
736736
read FFeedback;
737737
/// <summary>
738738
/// Allows to query the current internal processing state
@@ -799,7 +799,7 @@ implementation
799799
{$ELSE}
800800
System.TypInfo,
801801
{$ENDIF}
802-
DECTypes, DECUtil;
802+
DECUtil;
803803

804804
{$IFOPT Q+}{$DEFINE RESTORE_OVERFLOWCHECKS}{$Q-}{$ENDIF}
805805
{$IFOPT R+}{$DEFINE RESTORE_RANGECHECKS}{$R-}{$ENDIF}
@@ -873,7 +873,7 @@ constructor TDECCipher.Create;
873873

874874
if MustAdditionalBufferSave then
875875
// buffer contents: FData, then FFeedback, then FBuffer then FAdditionalBuffer
876-
FAdditionalBufferBackup := @PByteArray(FAdditionalBuffer)[FAdditionalBufferSize]
876+
FAdditionalBufferBackup := @PUInt8Array(FAdditionalBuffer)[FAdditionalBufferSize]
877877
else
878878
FAdditionalBufferBackup := nil;
879879

Source/DECCipherModes.pas

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ procedure TDECCipherModes.EncodeCFBx(Source, Dest: PByteArray; Size: Integer);
610610
// CFB-BlockSize
611611
var
612612
I: Integer;
613-
F: PByteArray;
613+
F: PUInt8Array;
614614
begin
615615
FState := csEncode;
616616
if FBufferIndex > 0 then
@@ -790,7 +790,7 @@ procedure TDECCipherModes.EncodeCFSx(Source, Dest: PByteArray; Size: Integer);
790790

791791
procedure TDECCipherModes.EncodeCBCx(Source, Dest: PByteArray; Size: Integer);
792792
var
793-
F: PByteArray;
793+
F: PUInt8Array;
794794
I: Integer;
795795
begin
796796
Dec(Size, FBufferSize);
@@ -1011,7 +1011,7 @@ procedure TDECCipherModes.DecodeCFBx(Source, Dest: PByteArray; Size: Integer);
10111011
// CFB-BlockSize
10121012
var
10131013
I: Integer;
1014-
F: PByteArray;
1014+
F: PUInt8Array;
10151015
begin
10161016
FState := csDecode;
10171017
if FBufferIndex > 0 then
@@ -1171,7 +1171,7 @@ procedure TDECCipherModes.DecodeCFSx(Source, Dest: PByteArray; Size: Integer);
11711171
procedure TDECCipherModes.DecodeCBCx(Source, Dest: PByteArray; Size: Integer);
11721172
var
11731173
I: Integer;
1174-
F, B, T: PByteArray;
1174+
F, B, T: PUInt8Array;
11751175
begin
11761176
Dec(Size, FBufferSize);
11771177
F := FFeedback;
@@ -1215,7 +1215,7 @@ procedure TDECCipherModes.DecodeCBCx(Source, Dest: PByteArray; Size: Integer);
12151215
procedure TDECCipherModes.DecodeCTSx(Source, Dest: PByteArray; Size: Integer);
12161216
var
12171217
I: Integer;
1218-
F, B, T: PByteArray;
1218+
F, B, T: PUInt8Array;
12191219
begin
12201220
Dec(Size, FBufferSize);
12211221
F := FFeedback;

Source/DECFormat.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,9 +1867,9 @@ class procedure TFormat_Base32.DoEncode(const Source;
18671867
Size: Integer);
18681868
var
18691869
i, n, c, b : Integer;
1870-
pIn : PByteArray;
1871-
pOut : PByte;
1872-
PadChars : UInt8;
1870+
pIn : PUInt8Array;
1871+
pOut : PByte;
1872+
PadChars : UInt8;
18731873
begin
18741874
if (Size = 0) or (Pointer(Source) = nil) then
18751875
begin

Source/DECHash.pas

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ THash_Square = class;
7979
THash_Snefru128 = class; // derived from the Xerox Secure Hash Function
8080
THash_Snefru256 = class; // " - "
8181
THash_Sapphire = class;
82+
THash_BCrypt = class;
8283

8384
/// <summary>
8485
/// Implementation of the MD2 hash algorithm. Considered to be broken,
@@ -92,7 +93,7 @@ THash_MD2 = class(TDECHashExtended)
9293
procedure DoTransform(Buffer: PUInt32Array); override;
9394
procedure DoDone; override;
9495
public
95-
function Digest: PByteArray; override;
96+
function Digest: PUInt8Array; override;
9697
class function DigestSize: UInt32; override;
9798
class function BlockSize: UInt32; override;
9899
end;
@@ -108,7 +109,7 @@ THashBaseMD4 = class(TDECHashExtended)
108109
procedure DoInit; override;
109110
procedure DoDone; override;
110111
public
111-
function Digest: PByteArray; override;
112+
function Digest: PUInt8Array; override;
112113
class function DigestSize: UInt32; override;
113114
class function BlockSize: UInt32; override;
114115
end;
@@ -239,7 +240,7 @@ THash_SHA384 = class(TDECHashExtended)
239240
procedure DoTransform(Buffer: PUInt32Array); override;
240241
procedure DoDone; override;
241242
public
242-
function Digest: PByteArray; override;
243+
function Digest: PUInt8Array; override;
243244
class function DigestSize: UInt32; override;
244245
class function BlockSize: UInt32; override;
245246
end;
@@ -565,7 +566,7 @@ THash_SHA3Base = class(TDECHashBit)
565566
/// <returns>
566567
/// Hash value calculated
567568
/// </returns>
568-
function Digest: PByteArray; override;
569+
function Digest: PUInt8Array; override;
569570
public
570571
/// <summary>
571572
/// Dimension hash result buffer
@@ -755,7 +756,7 @@ THashBaseHaval = class(TDECHashExtended, IDECHashRounds)
755756
procedure DoTransform5(Buffer: PUInt32Array);
756757
procedure DoDone; override;
757758
public
758-
function Digest: PByteArray; override;
759+
function Digest: PUInt8Array; override;
759760
class function BlockSize: UInt32; override;
760761
/// <summary>
761762
/// Returns the minimum possible number for the rounds parameter.
@@ -880,7 +881,7 @@ THash_Panama = class(TDECHashExtended)
880881
procedure DoDone; override;
881882
procedure DoPull;
882883
public
883-
function Digest: PByteArray; override;
884+
function Digest: PUInt8Array; override;
884885
class function DigestSize: UInt32; override;
885886
class function BlockSize: UInt32; override; // 32
886887
end;
@@ -894,7 +895,7 @@ THashBaseWhirlpool = class(TDECHashExtended)
894895
procedure DoTransform(Buffer: PUInt32Array); override;
895896
procedure DoDone; override;
896897
public
897-
function Digest: PByteArray; override;
898+
function Digest: PUInt8Array; override;
898899
class function DigestSize: UInt32; override;
899900
class function BlockSize: UInt32; override;
900901
end;
@@ -975,7 +976,7 @@ THash_Square = class(TDECHashExtended)
975976
procedure DoTransform(Buffer: PUInt32Array); override;
976977
procedure DoDone; override;
977978
public
978-
function Digest: PByteArray; override;
979+
function Digest: PUInt8Array; override;
979980
class function DigestSize: UInt32; override;
980981
class function BlockSize: UInt32; override;
981982
end;
@@ -1001,7 +1002,7 @@ THashBaseSnefru = class(TDECHashExtended, IDECHashRounds)
10011002
procedure DoInit; override;
10021003
procedure DoDone; override;
10031004
public
1004-
function Digest: PByteArray; override;
1005+
function Digest: PUInt8Array; override;
10051006
/// Returns the minimum possible number for the rounds parameter.
10061007
/// Value depends on Digest size which depends on concrete implementation
10071008
/// </summary>
@@ -1073,7 +1074,7 @@ THash_Sapphire = class(TDECHashExtended)
10731074
procedure DoDone; override;
10741075
procedure DoTransform(Buffer: PUInt32Array); override;
10751076
public
1076-
function Digest: PByteArray; override;
1077+
function Digest: PUInt8Array; override;
10771078
function DigestAsBytes: TBytes; override;
10781079
/// <summary>
10791080
/// Returns the default digest/hash size in bit. If RequestedDigestSize is
@@ -1408,7 +1409,7 @@ TBCryptBSDData = record
14081409
/// </exception>
14091410
procedure Calc(const Data; DataSize: Integer); override;
14101411

1411-
function Digest: PByteArray; override;
1412+
function Digest: PUInt8Array; override;
14121413
class function DigestSize: UInt32; override;
14131414
class function BlockSize: UInt32; override;
14141415

@@ -1554,7 +1555,7 @@ procedure THash_MD2.DoDone;
15541555
DoTransform(Pointer(FBuffer));
15551556
end;
15561557

1557-
function THash_MD2.Digest: PByteArray;
1558+
function THash_MD2.Digest: PUInt8Array;
15581559
begin
15591560
Result := @FDigest;
15601561
end;
@@ -1604,7 +1605,7 @@ procedure THashBaseMD4.DoDone;
16041605
DoTransform(Pointer(FBuffer));
16051606
end;
16061607

1607-
function THashBaseMD4.Digest: PByteArray;
1608+
function THashBaseMD4.Digest: PUInt8Array;
16081609
begin
16091610
Result := @FDigest;
16101611
end;
@@ -2982,7 +2983,7 @@ procedure THash_SHA384.DoDone;
29822983
SwapInt64Buffer(FDigest, FDigest, SizeOf(FDigest) div 8);
29832984
end;
29842985

2985-
function THash_SHA384.Digest: PByteArray;
2986+
function THash_SHA384.Digest: PUInt8Array;
29862987
begin
29872988
Result := @FDigest;
29882989
end;
@@ -3368,7 +3369,7 @@ procedure THashBaseHaval.DoDone;
33683369
end;
33693370
end;
33703371

3371-
function THashBaseHaval.Digest: PByteArray;
3372+
function THashBaseHaval.Digest: PUInt8Array;
33723373
begin
33733374
Result := @FDigest;
33743375
end;
@@ -3905,7 +3906,7 @@ procedure THash_Panama.DoPull;
39053906
end;
39063907
{$ENDIF !THash_Panama_asm}
39073908

3908-
function THash_Panama.Digest: PByteArray;
3909+
function THash_Panama.Digest: PUInt8Array;
39093910
begin
39103911
Result := @FDigest;
39113912
end;
@@ -4112,7 +4113,7 @@ procedure THashBaseWhirlpool.DoDone;
41124113
DoTransform(Pointer(FBuffer));
41134114
end;
41144115

4115-
function THashBaseWhirlpool.Digest: PByteArray;
4116+
function THashBaseWhirlpool.Digest: PUInt8Array;
41164117
begin
41174118
Result := @FDigest;
41184119
end;
@@ -4271,7 +4272,7 @@ procedure THash_Square.DoDone;
42714272
DoTransform(Pointer(FBuffer));
42724273
end;
42734274

4274-
function THash_Square.Digest: PByteArray;
4275+
function THash_Square.Digest: PUInt8Array;
42754276
begin
42764277
Result := @FDigest;
42774278
end;
@@ -4331,7 +4332,7 @@ procedure THashBaseSnefru.DoDone;
43314332
SwapUInt32Buffer(FDigest, FDigest, 8);
43324333
end;
43334334

4334-
function THashBaseSnefru.Digest: PByteArray;
4335+
function THashBaseSnefru.Digest: PUInt8Array;
43354336
begin
43364337
Result := @FDigest;
43374338
end;
@@ -4537,7 +4538,7 @@ procedure THash_Sapphire.Calc(const Data; DataSize: Integer);
45374538
end;
45384539
{$ENDIF !THash_Sapphire_asm}
45394540

4540-
function THash_Sapphire.Digest: PByteArray;
4541+
function THash_Sapphire.Digest: PUInt8Array;
45414542
begin
45424543
Result := @FDigest;
45434544
end;
@@ -5113,7 +5114,7 @@ constructor THash_SHA3Base.Create;
51135114
SetLength(FDigest, 64);
51145115
end;
51155116

5116-
function THash_SHA3Base.Digest: PByteArray;
5117+
function THash_SHA3Base.Digest: PUInt8Array;
51175118
begin
51185119
Result := @FDigest[0];
51195120
end;
@@ -5545,7 +5546,7 @@ constructor THash_BCrypt.Create;
55455546
// fixed value instead of no initialization at all.
55465547
end;
55475548

5548-
function THash_BCrypt.Digest: PByteArray;
5549+
function THash_BCrypt.Digest: PUInt8Array;
55495550
begin
55505551
Result := @FDigest;
55515552
end;

Source/DECHashAuthentication.pas

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,6 @@ class function TDECHashAuthentication.KDFInternal(const Data; DataSize: Integer;
965965
var
966966
I, n,
967967
Rounds, DigestBytes : Integer;
968-
Dest : PByteArray;
969968
Count : UInt32;
970969
HashInstance : TDECHashAuthentication;
971970
begin
@@ -980,8 +979,6 @@ class function TDECHashAuthentication.KDFInternal(const Data; DataSize: Integer;
980979
try
981980
Rounds := (MaskSize + DigestBytes - 1) div DigestBytes;
982981
SetLength(Result, Rounds * DigestBytes);
983-
Dest := @Result[0];
984-
985982

986983
if (KDFType = ktKDF2) then
987984
n := 1
@@ -1006,7 +1003,7 @@ class function TDECHashAuthentication.KDFInternal(const Data; DataSize: Integer;
10061003

10071004
HashInstance.Calc(Seed, SeedSize);
10081005
HashInstance.Done;
1009-
Move(HashInstance.Digest[0], Dest[(I) * DigestBytes], DigestBytes);
1006+
Move(HashInstance.Digest[0], Result[(I) * DigestBytes], DigestBytes);
10101007

10111008
inc(n);
10121009
end;

Source/DECHashBase.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ TDECHash = class(TDECObject, IDECHash)
8383
/// <summary>
8484
/// Internal processing buffer
8585
/// </summary>
86-
FBuffer : PByteArray;
86+
FBuffer : PUInt8Array;
8787
/// <summary>
8888
/// Size of the internal processing buffer in byte
8989
/// </summary>
@@ -154,7 +154,7 @@ TDECHash = class(TDECObject, IDECHash)
154154
/// <summary>
155155
/// Returns the calculated hash value
156156
/// </summary>
157-
function Digest: PByteArray; virtual; abstract;
157+
function Digest: PUInt8Array; virtual; abstract;
158158
public
159159
/// <summary>
160160
/// Initialize internal fields

0 commit comments

Comments
 (0)