Skip to content

Commit 9527ee0

Browse files
committed
Refactor PackTauOrSigma and ChaCha callers
- Simplify TSalsa20Engine.PackTauOrSigma to write state[0..3] directly - Drop unused stateOffset parameter from the helper signature - Use keyLength div 4 - 4 for tau/sigma index selection (equivalent for 128/256-bit keys) - Update ChaChaEngine and ChaCha7539Engine call sites; use literal 32 in ChaCha7539 SetKey Behavior-preserving refactor; no API or test changes required.
1 parent bf945ef commit 9527ee0

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

CryptoLib/src/Crypto/Engines/ClpChaCha7539Engine.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ procedure TChaCha7539Engine.SetKey(const AKeyBytes,
164164
[AlgorithmName]);
165165
end;
166166

167-
PackTauOrSigma(System.Length(AKeyBytes), FEngineState, 0);
167+
PackTauOrSigma(32, FEngineState);
168168

169169
// Key
170170
TPack.LE_To_UInt32(AKeyBytes, 0, FEngineState, 4, 8);

CryptoLib/src/Crypto/Engines/ClpChaChaEngine.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ procedure TChaChaEngine.SetKey(const AKeyBytes, AIvBytes: TCryptoLibByteArray);
296296
[AlgorithmName]);
297297
end;
298298

299-
PackTauOrSigma(System.Length(AKeyBytes), FEngineState, 0);
299+
PackTauOrSigma(System.Length(AKeyBytes), FEngineState);
300300

301301
// Key
302302
TPack.LE_To_UInt32(AKeyBytes, 0, FEngineState, 4, 4);
@@ -339,7 +339,7 @@ class procedure TChaChaEngine.HChaCha20(const AKey256, ANonce128: TCryptoLibByte
339339
end;
340340

341341
System.SetLength(LState, 16);
342-
PackTauOrSigma(32, LState, 0);
342+
PackTauOrSigma(32, LState);
343343
TPack.LE_To_UInt32(AKey256, 0, LState, 4, 8);
344344
TPack.LE_To_UInt32(ANonce128, 0, LState, 12, 4);
345345

CryptoLib/src/Crypto/Engines/ClpSalsa20Engine.pas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ TSalsa20Engine = class(TInterfacedObject, ISalsa20Engine, IStreamCipher)
113113
/// </returns>
114114
class function R(AX: UInt32; AY: Int32): UInt32; static; inline;
115115
class procedure PackTauOrSigma(AKeyLength: Int32;
116-
const AState: TCryptoLibUInt32Array; AStateOffset: Int32); static;
116+
const AState: TCryptoLibUInt32Array); static;
117117
class procedure SalsaCore(ARounds: Int32;
118118
const AInput, AX: TCryptoLibUInt32Array); static;
119119

@@ -364,15 +364,15 @@ procedure TSalsa20Engine.ProcessBlocks2(
364364
end;
365365

366366
class procedure TSalsa20Engine.PackTauOrSigma(AKeyLength: Int32;
367-
const AState: TCryptoLibUInt32Array; AStateOffset: Int32);
367+
const AState: TCryptoLibUInt32Array);
368368
var
369369
LTsOff: Int32;
370370
begin
371-
LTsOff := (AKeyLength - 16) div 4;
372-
AState[AStateOffset] := TAU_SIGMA[LTsOff];
373-
AState[AStateOffset + 1] := TAU_SIGMA[LTsOff + 1];
374-
AState[AStateOffset + 2] := TAU_SIGMA[LTsOff + 2];
375-
AState[AStateOffset + 3] := TAU_SIGMA[LTsOff + 3];
371+
LTsOff := AKeyLength div 4 - 4;
372+
AState[0] := TAU_SIGMA[LTsOff];
373+
AState[1] := TAU_SIGMA[LTsOff + 1];
374+
AState[2] := TAU_SIGMA[LTsOff + 2];
375+
AState[3] := TAU_SIGMA[LTsOff + 3];
376376
end;
377377

378378
procedure TSalsa20Engine.ProcessBytes(const AInBytes: TCryptoLibByteArray;

0 commit comments

Comments
 (0)