Debian Codegraph @

components/kascrypt/Hashes/Private/kperm_64.inc

raw
1 {KeccakF[1600] state permutation for 32+ bit compilers with RotL as function }
2
3 {Note: This will compile with all compilers which do support int64, but it is}
4 {better than the 32 bit code only if HAS_INLINE is available, 64-bit code is }
5 {produced and executed on 64-bit OS. Pascal translation based on the C++ code}
6 {keccak.cpp from the Botan library available from http://botan.randombit.net/}
7
8 {$define USE_LOCALA} {With FPC64/WIN64 about 20% faster}
9
10 {$ifdef HAS_UINT64}
11 type u64bit = uint64;
12 {$else}
13 type u64bit = int64;
14 {$endif}
15
16 type
17 pu64bit = ^u64bit;
18
19 const
20 RC: array[0..23] of u64bit = (
21 u64bit($0000000000000001), u64bit($0000000000008082),
22 u64bit($800000000000808A), u64bit($8000000080008000),
23 u64bit($000000000000808B), u64bit($0000000080000001),
24 u64bit($8000000080008081), u64bit($8000000000008009),
25 u64bit($000000000000008A), u64bit($0000000000000088),
26 u64bit($0000000080008009), u64bit($000000008000000A),
27 u64bit($000000008000808B), u64bit($800000000000008B),
28 u64bit($8000000000008089), u64bit($8000000000008003),
29 u64bit($8000000000008002), u64bit($8000000000000080),
30 u64bit($000000000000800A), u64bit($800000008000000A),
31 u64bit($8000000080008081), u64bit($8000000000008080),
32 u64bit($0000000080000001), u64bit($8000000080008008));
33
34
35 {---------------------------------------------------------------------------}
36 {$IFDEF FPC}
37 {$MACRO ON} {$DEFINE RotL:= RolQWord}
38 {$ELSE}
39 function RotL(x: u64bit; c: integer): u64bit; {$ifdef HAS_INLINE} inline; {$endif}
40 begin
41 RotL := (x shl c) or (x shr (64-c));
42 end;
43 {$ENDIF}
44
45
46 {---------------------------------------------------------------------------}
47 procedure KeccakPermutation(var state: TState_L);
48 {$ifdef USE_LOCALA}
49 var A: array[0..24] of u64bit;
50 {$else}
51 var A: packed array[0..24] of u64bit absolute state;
52 {$endif}
53 var
54 B: array[0..24] of u64bit;
55 C0, C1, C2, C3, C4, D0, D1, D2, D3, D4: u64bit;
56 i: integer;
57 begin
58 {$ifdef USE_LOCALA}
59 TState_L(A) := state;
60 {$endif}
61 for i:=0 to 23 do begin
62 C0 := A[00] xor A[05] xor A[10] xor A[15] xor A[20];
63 C1 := A[01] xor A[06] xor A[11] xor A[16] xor A[21];
64 C2 := A[02] xor A[07] xor A[12] xor A[17] xor A[22];
65 C3 := A[03] xor A[08] xor A[13] xor A[18] xor A[23];
66 C4 := A[04] xor A[09] xor A[14] xor A[19] xor A[24];
67
68 D0 := RotL(C0, 1) xor C3;
69 D1 := RotL(C1, 1) xor C4;
70 D2 := RotL(C2, 1) xor C0;
71 D3 := RotL(C3, 1) xor C1;
72 D4 := RotL(C4, 1) xor C2;
73
74 B[00] := A[00] xor D1;
75 B[01] := RotL(A[06] xor D2, 44);
76 B[02] := RotL(A[12] xor D3, 43);
77 B[03] := RotL(A[18] xor D4, 21);
78 B[04] := RotL(A[24] xor D0, 14);
79 B[05] := RotL(A[03] xor D4, 28);
80 B[06] := RotL(A[09] xor D0, 20);
81 B[07] := RotL(A[10] xor D1, 3);
82 B[08] := RotL(A[16] xor D2, 45);
83 B[09] := RotL(A[22] xor D3, 61);
84 B[10] := RotL(A[01] xor D2, 1);
85 B[11] := RotL(A[07] xor D3, 6);
86 B[12] := RotL(A[13] xor D4, 25);
87 B[13] := RotL(A[19] xor D0, 8);
88 B[14] := RotL(A[20] xor D1, 18);
89 B[15] := RotL(A[04] xor D0, 27);
90 B[16] := RotL(A[05] xor D1, 36);
91 B[17] := RotL(A[11] xor D2, 10);
92 B[18] := RotL(A[17] xor D3, 15);
93 B[19] := RotL(A[23] xor D4, 56);
94 B[20] := RotL(A[02] xor D3, 62);
95 B[21] := RotL(A[08] xor D4, 55);
96 B[22] := RotL(A[14] xor D0, 39);
97 B[23] := RotL(A[15] xor D1, 41);
98 B[24] := RotL(A[21] xor D2, 2);
99
100 A[00] := B[00] xor ((not B[01]) and B[02]);
101 A[01] := B[01] xor ((not B[02]) and B[03]);
102 A[02] := B[02] xor ((not B[03]) and B[04]);
103 A[03] := B[03] xor ((not B[04]) and B[00]);
104 A[04] := B[04] xor ((not B[00]) and B[01]);
105 A[05] := B[05] xor ((not B[06]) and B[07]);
106 A[06] := B[06] xor ((not B[07]) and B[08]);
107 A[07] := B[07] xor ((not B[08]) and B[09]);
108 A[08] := B[08] xor ((not B[09]) and B[05]);
109 A[09] := B[09] xor ((not B[05]) and B[06]);
110 A[10] := B[10] xor ((not B[11]) and B[12]);
111 A[11] := B[11] xor ((not B[12]) and B[13]);
112 A[12] := B[12] xor ((not B[13]) and B[14]);
113 A[13] := B[13] xor ((not B[14]) and B[10]);
114 A[14] := B[14] xor ((not B[10]) and B[11]);
115 A[15] := B[15] xor ((not B[16]) and B[17]);
116 A[16] := B[16] xor ((not B[17]) and B[18]);
117 A[17] := B[17] xor ((not B[18]) and B[19]);
118 A[18] := B[18] xor ((not B[19]) and B[15]);
119 A[19] := B[19] xor ((not B[15]) and B[16]);
120 A[20] := B[20] xor ((not B[21]) and B[22]);
121 A[21] := B[21] xor ((not B[22]) and B[23]);
122 A[22] := B[22] xor ((not B[23]) and B[24]);
123 A[23] := B[23] xor ((not B[24]) and B[20]);
124 A[24] := B[24] xor ((not B[20]) and B[21]);
125 A[00] := A[00] xor RC[i];
126 end;
127 {$ifdef USE_LOCALA}
128 state := TState_L(A);
129 {$endif}
130 end;
131
132
133 {---------------------------------------------------------------------------}
134 procedure extractFromState(outp: pointer; const state: TState_L; laneCount: integer);
135 var
136 pI, pS: pu64bit;
137 i: integer;
138 begin
139 pI := outp;
140 pS := @state[0];
141 for i:=laneCount-1 downto 0 do begin
142 pI^ := pS^;
143 inc(pI);
144 inc(pS);
145 end;
146 end;
147
148
149 {---------------------------------------------------------------------------}
150 procedure xorIntoState(var state: TState_L; inp: PLongint; laneCount: integer);
151 {-Include input message data bits into the sponge state}
152 var
153 pI, pS: pu64bit;
154 i: integer;
155 begin
156 pI := pu64bit(inp);
157 pS := @state[0];
158 for i:=laneCount-1 downto 0 do begin
159 pS^ := pS^ xor pI^;
160 inc(pI);
161 inc(pS);
162 end;
163 end;
164