Commits

Snake Doctor  committed 4c48325

Small improvements, refactoring and cleanup (removed unnecessary junk)
Added flag/define to allow access to Keccak internal functions

  • Participants
  • Parent commits 7f140cf

Comments (0)

Files changed (1)

File unitKeccak.pas

 unit unitKeccak;
 interface
 uses
-  windows, sysutils, classes;
+  windows;
+
+{ $DEFINE KECCAK_EXPOSE_INTERNAL_FUNCTIONS}
 
 {$REGION 'Generic Types'}
 type
-  size_t = NativeUInt;
-
-  PInt8  = ^Int8;
-  PInt16 = ^Int16;
-  PInt32 = ^Int32;
-  PInt64 = ^Int64;
-
   PUInt8  = ^UInt8;
-  PUInt16 = ^UInt16;
   PUInt32 = ^UInt32;
   PUInt64 = ^UInt64;
 
-  TUInt8Array  = array[0..(MAXDWORD div 4 )-1] of UInt8;
-  TUInt16Array = array[0..(MAXDWORD div 8 )-1] of UInt16;
-  TUInt32Array = array[0..(MAXDWORD div 16 )-1] of UInt32;
-  TUInt64Array = array[0..(MAXDWORD div 32)-1] of UInt64;
-
-  TDynUInt8 = record
-  	case Integer of
-		0: (Val: UInt8);
-        1: (Arr: TUInt8Array);
-  end;
-
-  TDynUInt16 = record
-  	case Integer of
-		0: (Val: UInt16);
-        1: (Arr: TUInt16Array);
-  end;
-
-  TDynUInt32 = record
-  	case Integer of
-		0: (Val: UInt32);
-        1: (Arr: TUInt32Array);
-  end;
-
-  TDynUInt64 = record
-  	case Integer of
-		0: (Val: UInt64);
-        1: (ArrUInt64: TUInt64Array);
-  end;
-
-  TDynUInt64_ = record
-  	case Integer of
-        0: (ValUInt8:  UInt8);
-        1: (ValUInt16: UInt16);
-        2: (ValUInt32: UInt32);
-        3: (ValUInt64: UInt64);
-        4: (ArrUInt8:  TUInt8Array);
-        5: (ArrUInt16: TUInt16Array);
-        6: (ArrUInt32: TUInt32Array);
-        7: (ArrUInt64: TUInt64Array);
-  end;
+  TUInt8Array  = array[0..(MAXDWORD div 2 )-1] of UInt8;
+  TUInt32Array = array[0..(MAXDWORD div 8 )-1] of UInt32;
+  TUInt64Array = array[0..(MAXDWORD div 16)-1] of UInt64;
 
   PUInt8Array  = ^TUInt8Array;
-  PUInt16Array = ^TUInt16Array;
   PUInt32Array = ^TUInt32Array;
   PUInt64Array = ^TUInt64Array;
-
-  PDynUInt8  = ^TDynUInt8;
-  PDynUInt16 = ^TDynUInt16;
-  PDynUInt32 = ^TDynUInt32;
-  PDynUInt64 = ^TDynUInt64;
 {$ENDREGION}
 
 {$REGION 'Keccak Types'}
 	end;
     PSpongeState = ^TSpongeState;
 {$ENDREGION}
-{$REGION 'Keccak Internal Variables'}
-var
-	KeccakRoundConstants: array[0..nrRounds-1] of UInt64;
-	KeccakRhoOffsets: array[0..nrLanes-1] of UInt32;
-{$ENDREGION}
 
+{$REGION 'Keccak Internal Functions'}
+{$IFDEF KECCAK_EXPOSE_INTERNAL_FUNCTIONS}
 function InitSponge(ctx: PSpongeState; rate: UInt32; capacity: UInt32): integer;
 procedure AbsorbQueue(ctx: PSpongeState);
 function Absorb(ctx: PSpongeState; const data: PUInt8Array; databitlen: UInt32): integer;
 procedure KeccakInitializeDataQueue(state: PUInt8Array);
 procedure KeccakAbsorb(state: PUInt8Array; const data: PUInt8Array; laneCount: UInt32);
 procedure KeccakExtract(state: PUInt8Array; data: PUInt8Array; laneCount: UInt32);
-
+{$ENDIF}
+{$ENDREGION}
+
 function Keccak_Init(state: PSpongeState; hashbitlen: integer): EHashReturn;
 function Keccak_Update(state: PSpongeState; const data: Pointer; databitlen: UInt64): EHashReturn;
 function Keccak_Final(state: PSpongeState; hashval: Pointer): EHashReturn;
 
 implementation
 
+{$REGION 'Keccak Internal Variables'}
+var
+	KeccakRoundConstants: array[0..nrRounds-1] of UInt64;
+	KeccakRhoOffsets: array[0..nrLanes-1] of UInt32;
+{$ENDREGION}
+
+{$REGION 'Keccak Internal Function Forwardsd'}
+{$IFNDEF KECCAK_EXPOSE_INTERNAL_FUNCTIONS}
+function InitSponge(ctx: PSpongeState; rate: UInt32; capacity: UInt32): integer; forward;
+procedure AbsorbQueue(ctx: PSpongeState); forward;
+function Absorb(ctx: PSpongeState; const data: PUInt8Array; databitlen: UInt32): integer; forward;
+procedure PadAndSwitchToSqueezingPhase(ctx: PSpongeState); forward;
+function Squeeze(ctx: PSpongeState; output: PUInt8Array; outputLength: UInt32): integer; forward;
+
+procedure KeccakPermutation( state: PUInt8Array ); forward;
+procedure KeccakPermutationAfterXor( state: PUInt8Array; const data: PUInt8Array; dataLengthInBytes: UInt32 ); forward;
+procedure KeccakPermutationOnWords( state: PUInt64Array ); forward;
+procedure KeccakInitializeRoundConstants; forward;
+procedure KeccakInitializeRhoOffsets; forward;
+procedure KeccakInitialize; forward;
+procedure KeccakInitializeState(state: PUInt8Array); forward;
+procedure KeccakInitializeDataQueue(state: PUInt8Array); forward;
+procedure KeccakAbsorb(state: PUInt8Array; const data: PUInt8Array; laneCount: UInt32); forward;
+procedure KeccakExtract(state: PUInt8Array; data: PUInt8Array; laneCount: UInt32); forward;
+{$ENDIF}
+{$ENDREGION}
+
+{$REGION 'Keccak Helper Functions'}
 function index(x, y: UInt32): UInt32; inline;
 begin
     Result := (x mod 5) + 5*(y mod 5);
 begin
 	Result := a xor b;
 end;
+{$ENDREGION}
 
 {$REGION 'Sponge Functions'}
 function InitSponge;