Crypto++ 7.0 Free C++ class library of cryptographic schemes

keccakc.cpp
1 // keccakc.cpp - Keccak core functions shared between SHA3 and Keccak.
2 // written and placed in the public domain by JW.
3
4 /*
5 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
6 Michael Peeters and Gilles Van Assche. For more information, feedback or
7 questions, please refer to our website: http://keccak.noekeon.org/
8
9 Implementation by Ronny Van Keer, hereby denoted as "the implementer".
10
11 To the extent possible under law, the implementer has waived all copyright
12 and related or neighboring rights to the source code in this file.
13 http://creativecommons.org/publicdomain/zero/1.0/
14 */
15
16 #include "pch.h"
17 #include "keccak.h"
18 #include "keccakc.h"
19
20 ANONYMOUS_NAMESPACE_BEGIN
21
22 using CryptoPP::word64;
23
24 const word64 KeccakF_RoundConstants[24] =
25 {
26 W64LIT(0x0000000000000001), W64LIT(0x0000000000008082), W64LIT(0x800000000000808a),
27 W64LIT(0x8000000080008000), W64LIT(0x000000000000808b), W64LIT(0x0000000080000001),
28 W64LIT(0x8000000080008081), W64LIT(0x8000000000008009), W64LIT(0x000000000000008a),
29 W64LIT(0x0000000000000088), W64LIT(0x0000000080008009), W64LIT(0x000000008000000a),
30 W64LIT(0x000000008000808b), W64LIT(0x800000000000008b), W64LIT(0x8000000000008089),
31 W64LIT(0x8000000000008003), W64LIT(0x8000000000008002), W64LIT(0x8000000000000080),
32 W64LIT(0x000000000000800a), W64LIT(0x800000008000000a), W64LIT(0x8000000080008081),
33 W64LIT(0x8000000000008080), W64LIT(0x0000000080000001), W64LIT(0x8000000080008008)
34 };
35
36 ANONYMOUS_NAMESPACE_END
37
38 NAMESPACE_BEGIN(CryptoPP)
39
40 void KeccakF1600(word64 *state)
41 {
42 {
43 word64 Aba, Abe, Abi, Abo, Abu;
44 word64 Aga, Age, Agi, Ago, Agu;
45 word64 Aka, Ake, Aki, Ako, Aku;
46 word64 Ama, Ame, Ami, Amo, Amu;
47 word64 Asa, Ase, Asi, Aso, Asu;
48 word64 BCa, BCe, BCi, BCo, BCu;
49 word64 Da, De, Di, Do, Du;
50 word64 Eba, Ebe, Ebi, Ebo, Ebu;
51 word64 Ega, Ege, Egi, Ego, Egu;
52 word64 Eka, Eke, Eki, Eko, Eku;
53 word64 Ema, Eme, Emi, Emo, Emu;
54 word64 Esa, Ese, Esi, Eso, Esu;
55
56 //copyFromState(A, state)
57 typedef BlockGetAndPut<word64, LittleEndian, true, true> Block;
58 Block::Get(state)(Aba)(Abe)(Abi)(Abo)(Abu)(Aga)(Age)(Agi)(Ago)(Agu)(Aka)(Ake)(Aki)(Ako)(Aku)(Ama)(Ame)(Ami)(Amo)(Amu)(Asa)(Ase)(Asi)(Aso)(Asu);
59
60 for( unsigned int round = 0; round < 24; round += 2 )
61 {
62 // prepareTheta
63 BCa = Aba^Aga^Aka^Ama^Asa;
64 BCe = Abe^Age^Ake^Ame^Ase;
65 BCi = Abi^Agi^Aki^Ami^Asi;
66 BCo = Abo^Ago^Ako^Amo^Aso;
67 BCu = Abu^Agu^Aku^Amu^Asu;
68
69 //thetaRhoPiChiIotaPrepareTheta(round , A, E)
70 Da = BCu^rotlConstant<1>(BCe);
71 De = BCa^rotlConstant<1>(BCi);
72 Di = BCe^rotlConstant<1>(BCo);
73 Do = BCi^rotlConstant<1>(BCu);
74 Du = BCo^rotlConstant<1>(BCa);
75
76 Aba ^= Da;
77 BCa = Aba;
78 Age ^= De;
79 BCe = rotlConstant<44>(Age);
80 Aki ^= Di;
81 BCi = rotlConstant<43>(Aki);
82 Amo ^= Do;
83 BCo = rotlConstant<21>(Amo);
84 Asu ^= Du;
85 BCu = rotlConstant<14>(Asu);
86 Eba = BCa ^((~BCe)& BCi );
87 Eba ^= (word64)KeccakF_RoundConstants[round];
88 Ebe = BCe ^((~BCi)& BCo );
89 Ebi = BCi ^((~BCo)& BCu );
90 Ebo = BCo ^((~BCu)& BCa );
91 Ebu = BCu ^((~BCa)& BCe );
92
93 Abo ^= Do;
94 BCa = rotlConstant<28>(Abo);
95 Agu ^= Du;
96 BCe = rotlConstant<20>(Agu);
97 Aka ^= Da;
98 BCi = rotlConstant<3>(Aka);
99 Ame ^= De;
100 BCo = rotlConstant<45>(Ame);
101 Asi ^= Di;
102 BCu = rotlConstant<61>(Asi);
103 Ega = BCa ^((~BCe)& BCi );
104 Ege = BCe ^((~BCi)& BCo );
105 Egi = BCi ^((~BCo)& BCu );
106 Ego = BCo ^((~BCu)& BCa );
107 Egu = BCu ^((~BCa)& BCe );
108
109 Abe ^= De;
110 BCa = rotlConstant<1>(Abe);
111 Agi ^= Di;
112 BCe = rotlConstant<6>(Agi);
113 Ako ^= Do;
114 BCi = rotlConstant<25>(Ako);
115 Amu ^= Du;
116 BCo = rotlConstant<8>(Amu);
117 Asa ^= Da;
118 BCu = rotlConstant<18>(Asa);
119 Eka = BCa ^((~BCe)& BCi );
120 Eke = BCe ^((~BCi)& BCo );
121 Eki = BCi ^((~BCo)& BCu );
122 Eko = BCo ^((~BCu)& BCa );
123 Eku = BCu ^((~BCa)& BCe );
124
125 Abu ^= Du;
126 BCa = rotlConstant<27>(Abu);
127 Aga ^= Da;
128 BCe = rotlConstant<36>(Aga);
129 Ake ^= De;
130 BCi = rotlConstant<10>(Ake);
131 Ami ^= Di;
132 BCo = rotlConstant<15>(Ami);
133 Aso ^= Do;
134 BCu = rotlConstant<56>(Aso);
135 Ema = BCa ^((~BCe)& BCi );
136 Eme = BCe ^((~BCi)& BCo );
137 Emi = BCi ^((~BCo)& BCu );
138 Emo = BCo ^((~BCu)& BCa );
139 Emu = BCu ^((~BCa)& BCe );
140
141 Abi ^= Di;
142 BCa = rotlConstant<62>(Abi);
143 Ago ^= Do;
144 BCe = rotlConstant<55>(Ago);
145 Aku ^= Du;
146 BCi = rotlConstant<39>(Aku);
147 Ama ^= Da;
148 BCo = rotlConstant<41>(Ama);
149 Ase ^= De;
150 BCu = rotlConstant<2>(Ase);
151 Esa = BCa ^((~BCe)& BCi );
152 Ese = BCe ^((~BCi)& BCo );
153 Esi = BCi ^((~BCo)& BCu );
154 Eso = BCo ^((~BCu)& BCa );
155 Esu = BCu ^((~BCa)& BCe );
156
157 // prepareTheta
158 BCa = Eba^Ega^Eka^Ema^Esa;
159 BCe = Ebe^Ege^Eke^Eme^Ese;
160 BCi = Ebi^Egi^Eki^Emi^Esi;
161 BCo = Ebo^Ego^Eko^Emo^Eso;
162 BCu = Ebu^Egu^Eku^Emu^Esu;
163
164 //thetaRhoPiChiIotaPrepareTheta(round+1, E, A)
165 Da = BCu^rotlConstant<1>(BCe);
166 De = BCa^rotlConstant<1>(BCi);
167 Di = BCe^rotlConstant<1>(BCo);
168 Do = BCi^rotlConstant<1>(BCu);
169 Du = BCo^rotlConstant<1>(BCa);
170
171 Eba ^= Da;
172 BCa = Eba;
173 Ege ^= De;
174 BCe = rotlConstant<44>(Ege);
175 Eki ^= Di;
176 BCi = rotlConstant<43>(Eki);
177 Emo ^= Do;
178 BCo = rotlConstant<21>(Emo);
179 Esu ^= Du;
180 BCu = rotlConstant<14>(Esu);
181 Aba = BCa ^((~BCe)& BCi );
182 Aba ^= (word64)KeccakF_RoundConstants[round+1];
183 Abe = BCe ^((~BCi)& BCo );
184 Abi = BCi ^((~BCo)& BCu );
185 Abo = BCo ^((~BCu)& BCa );
186 Abu = BCu ^((~BCa)& BCe );
187
188 Ebo ^= Do;
189 BCa = rotlConstant<28>(Ebo);
190 Egu ^= Du;
191 BCe = rotlConstant<20>(Egu);
192 Eka ^= Da;
193 BCi = rotlConstant<3>(Eka);
194 Eme ^= De;
195 BCo = rotlConstant<45>(Eme);
196 Esi ^= Di;
197 BCu = rotlConstant<61>(Esi);
198 Aga = BCa ^((~BCe)& BCi );
199 Age = BCe ^((~BCi)& BCo );
200 Agi = BCi ^((~BCo)& BCu );
201 Ago = BCo ^((~BCu)& BCa );
202 Agu = BCu ^((~BCa)& BCe );
203
204 Ebe ^= De;
205 BCa = rotlConstant<1>(Ebe);
206 Egi ^= Di;
207 BCe = rotlConstant<6>(Egi);
208 Eko ^= Do;
209 BCi = rotlConstant<25>(Eko);
210 Emu ^= Du;
211 BCo = rotlConstant<8>(Emu);
212 Esa ^= Da;
213 BCu = rotlConstant<18>(Esa);
214 Aka = BCa ^((~BCe)& BCi );
215 Ake = BCe ^((~BCi)& BCo );
216 Aki = BCi ^((~BCo)& BCu );
217 Ako = BCo ^((~BCu)& BCa );
218 Aku = BCu ^((~BCa)& BCe );
219
220 Ebu ^= Du;
221 BCa = rotlConstant<27>(Ebu);
222 Ega ^= Da;
223 BCe = rotlConstant<36>(Ega);
224 Eke ^= De;
225 BCi = rotlConstant<10>(Eke);
226 Emi ^= Di;
227 BCo = rotlConstant<15>(Emi);
228 Eso ^= Do;
229 BCu = rotlConstant<56>(Eso);
230 Ama = BCa ^((~BCe)& BCi );
231 Ame = BCe ^((~BCi)& BCo );
232 Ami = BCi ^((~BCo)& BCu );
233 Amo = BCo ^((~BCu)& BCa );
234 Amu = BCu ^((~BCa)& BCe );
235
236 Ebi ^= Di;
237 BCa = rotlConstant<62>(Ebi);
238 Ego ^= Do;
239 BCe = rotlConstant<55>(Ego);
240 Eku ^= Du;
241 BCi = rotlConstant<39>(Eku);
242 Ema ^= Da;
243 BCo = rotlConstant<41>(Ema);
244 Ese ^= De;
245 BCu = rotlConstant<2>(Ese);
246 Asa = BCa ^((~BCe)& BCi );
247 Ase = BCe ^((~BCi)& BCo );
248 Asi = BCi ^((~BCo)& BCu );
249 Aso = BCo ^((~BCu)& BCa );
250 Asu = BCu ^((~BCa)& BCe );
251 }
252
253 //copyToState(state, A)
254 Block::Put(NULLPTR, state)(Aba)(Abe)(Abi)(Abo)(Abu)(Aga)(Age)(Agi)(Ago)(Agu)(Aka)(Ake)(Aki)(Ako)(Aku)(Ama)(Ame)(Ami)(Amo)(Amu)(Asa)(Ase)(Asi)(Aso)(Asu);
255 }
256 }
257
258 NAMESPACE_END
keccak.h
Classes for Keccak message digests.
BlockGetAndPut
Access a block of memory.
Definition: misc.h:2466
pch.h
Precompiled header file.
PutBlock
Access a block of memory.
Definition: misc.h:2429
CryptoPP
Crypto++ library namespace.