Martijn Laan пре 4 месеци
родитељ
комит
2cc5577bee
2 измењених фајлова са 21 додато и 4 уклоњено
  1. 5 4
      Components/ChaCha20.pas
  2. 16 0
      Components/UnsignedFunc.pas

+ 5 - 4
Components/ChaCha20.pas

@@ -37,7 +37,8 @@ procedure XChaCha20Crypt(var Context: TChaCha20Context; const InBuffer;
 implementation
 
 uses
-  System.SysUtils;
+  System.SysUtils,
+  UnsignedFunc;
 
 {$C+}
 
@@ -54,7 +55,7 @@ begin
   ctx[1] := $3320646e;
   ctx[2] := $79622d32;
   ctx[3] := $6b206574;
-  Move(Key, ctx[4], KeyLength);
+  UMove(Key, ctx[4], KeyLength);
   ctx[12] := Count;
   if NonceLength = 12 then
     Move(Nonce, ctx[13], 12)
@@ -134,7 +135,7 @@ begin
 
   var InBuf: PByte := @InBuffer;
   var OutBuf: PByte := @OutBuffer;
-  var KeyStream: PByte := @Context.keystream;
+  var KeyStream := PByte(@Context.keystream);
 
   for var I := 0 to Length-1 do begin
     if Context.position >= 64 then begin
@@ -168,7 +169,7 @@ begin
   var SubKey: TBytes;
   HChaCha20(Key, KeyLength, Nonce, 16, SubKey);
   var NonceBytes: PByte := @Nonce;
-  ChaCha20Init(Context, SubKey[0], Length(SubKey), NonceBytes[16], 8, Count);
+  ChaCha20Init(Context, SubKey[0], ULength(SubKey), NonceBytes[16], 8, Count);
 end;
 
 procedure XChaCha20Crypt(var Context: TChaCha20Context; const InBuffer;

+ 16 - 0
Components/UnsignedFunc.pas

@@ -18,6 +18,7 @@ uses
 function ULength(const S: String): Cardinal; overload;
 function ULength(const S: AnsiString): Cardinal; overload;
 function ULength(const S: TBytes): Cardinal; overload;
+procedure UMove(const Source; var Dest; Count: NativeUInt);
 
 implementation
 
@@ -36,4 +37,19 @@ begin
   Result := Cardinal(Length(S));
 end;
 
+procedure UMove(const Source; var Dest; Count: NativeUInt);
+begin
+  var SourceBuf: PByte := @Source;
+  var DestBuf: PByte := @Dest;
+  while Count > 0 do begin
+    var MoveCount := High(NativeInt);
+    if Count < NativeUInt(MoveCount) then
+      MoveCount := NativeInt(Count);
+    Move(SourceBuf^, DestBuf^, MoveCount);
+    Dec(Count, MoveCount);
+    Inc(SourceBuf, MoveCount);
+    Inc(DestBuf, MoveCount);
+  end;
+end;
+
 end.