OSDN Git Service

84bd6bcf31107bfc21f9a089a6109cd7d0f7d6c0
[mutilities/MUtilities.git] / src / 3rd_party / keccak / include / keccak_impl.h
1 /***************************************************************************
2 **                                                                        **
3 **  Keccak (http://keccak.noekeon.org/) implementation file               **
4 **                                                                        **
5 **  This file implements the Keccak hash algorithm as provided by the     **
6 **  inventors, and submitted to the third round of the NIST SHA-3         **
7 **  competition.                                                          **
8 **  For the QKeccakHash wrapper, the originally multiple files were       **
9 **  combined in this one. Slight modifications have been made to          **
10 **  allow use in C++ as an independently included .cpp file.              **
11 **  It is not meant to be compiled into an object, hence, there is no     **
12 **  header.                                                               **
13 **                                                                        **
14 **  The code here is not part of the actual QKeccakHash implementation    **
15 **  and thus not licensed under the GPLv3 but under its respecitve        **
16 **  license given by the inventors. Typically, this means it is in the    **
17 **  public domain, if not noted otherwise.                                **
18 **                                                                        **
19 ***************************************************************************/
20
21 namespace MUtils {
22 namespace Internal {
23 namespace KeccakImpl {
24
25 typedef unsigned char UINT8;
26 typedef unsigned short UINT16;
27 typedef unsigned int UINT32;
28 typedef unsigned long long int UINT64;
29
30 // ================================================================================
31 // =================== brg_endian.h
32 // ================================================================================
33
34
35 /*
36  ---------------------------------------------------------------------------
37  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
38
39  LICENSE TERMS
40
41  The redistribution and use of this software (with or without changes)
42  is allowed without the payment of fees or royalties provided that:
43
44   1. source code distributions include the above copyright notice, this
45      list of conditions and the following disclaimer;
46
47   2. binary distributions include the above copyright notice, this list
48      of conditions and the following disclaimer in their documentation;
49
50   3. the name of the copyright holder is not used to endorse products
51      built using this software without specific written permission.
52
53  DISCLAIMER
54
55  This software is provided 'as is' with no explicit or implied warranties
56  in respect of its properties, including, but not limited to, correctness
57  and/or fitness for purpose.
58  ---------------------------------------------------------------------------
59  Issue Date: 20/12/2007
60  Changes for ARM 9/9/2010
61 */
62
63 #ifndef _BRG_ENDIAN_H
64 #define _BRG_ENDIAN_H
65
66 #define IS_BIG_ENDIAN      4321 /* byte 0 is most significant (mc68k) */
67 #define IS_LITTLE_ENDIAN   1234 /* byte 0 is least significant (i386) */
68
69 #if 0
70 /* Include files where endian defines and byteswap functions may reside */
71 #if defined( __sun )
72 #  include <sys/isa_defs.h>
73 #elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ )
74 #  include <sys/endian.h>
75 #elif defined( BSD ) && ( BSD >= 199103 ) || defined( __APPLE__ ) || \
76       defined( __CYGWIN32__ ) || defined( __DJGPP__ ) || defined( __osf__ )
77 #  include <machine/endian.h>
78 #elif defined( __linux__ ) || defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
79 #  if !defined( __MINGW32__ ) && !defined( _AIX )
80 #    include <endian.h>
81 #    if !defined( __BEOS__ )
82 #      include <byteswap.h>
83 #    endif
84 #  endif
85 #endif
86 #endif
87
88 /* Now attempt to set the define for platform byte order using any  */
89 /* of the four forms SYMBOL, _SYMBOL, __SYMBOL & __SYMBOL__, which  */
90 /* seem to encompass most endian symbol definitions                 */
91
92 #if defined( BIG_ENDIAN ) && defined( LITTLE_ENDIAN )
93 #  if defined( BYTE_ORDER ) && BYTE_ORDER == BIG_ENDIAN
94 #    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
95 #  elif defined( BYTE_ORDER ) && BYTE_ORDER == LITTLE_ENDIAN
96 #    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
97 #  endif
98 #elif defined( BIG_ENDIAN )
99 #  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
100 #elif defined( LITTLE_ENDIAN )
101 #  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
102 #endif
103
104 #if defined( _BIG_ENDIAN ) && defined( _LITTLE_ENDIAN )
105 #  if defined( _BYTE_ORDER ) && _BYTE_ORDER == _BIG_ENDIAN
106 #    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
107 #  elif defined( _BYTE_ORDER ) && _BYTE_ORDER == _LITTLE_ENDIAN
108 #    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
109 #  endif
110 #elif defined( _BIG_ENDIAN )
111 #  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
112 #elif defined( _LITTLE_ENDIAN )
113 #  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
114 #endif
115
116 #if defined( __BIG_ENDIAN ) && defined( __LITTLE_ENDIAN )
117 #  if defined( __BYTE_ORDER ) && __BYTE_ORDER == __BIG_ENDIAN
118 #    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
119 #  elif defined( __BYTE_ORDER ) && __BYTE_ORDER == __LITTLE_ENDIAN
120 #    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
121 #  endif
122 #elif defined( __BIG_ENDIAN )
123 #  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
124 #elif defined( __LITTLE_ENDIAN )
125 #  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
126 #endif
127
128 #if defined( __BIG_ENDIAN__ ) && defined( __LITTLE_ENDIAN__ )
129 #  if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __BIG_ENDIAN__
130 #    define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
131 #  elif defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __LITTLE_ENDIAN__
132 #    define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
133 #  endif
134 #elif defined( __BIG_ENDIAN__ )
135 #  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
136 #elif defined( __LITTLE_ENDIAN__ )
137 #  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
138 #endif
139
140 /*  if the platform byte order could not be determined, then try to */
141 /*  set this define using common machine defines                    */
142 #if !defined(PLATFORM_BYTE_ORDER)
143
144 #if   defined( __alpha__ ) || defined( __alpha ) || defined( i386 )       || \
145       defined( __i386__ )  || defined( _M_I86 )  || defined( _M_IX86 )    || \
146       defined( __OS2__ )   || defined( sun386 )  || defined( __TURBOC__ ) || \
147       defined( vax )       || defined( vms )     || defined( VMS )        || \
148       defined( __VMS )     || defined( _M_X64 )
149 #  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
150
151 #elif defined( AMIGA )   || defined( applec )    || defined( __AS400__ )  || \
152       defined( _CRAY )   || defined( __hppa )    || defined( __hp9000 )   || \
153       defined( ibm370 )  || defined( mc68000 )   || defined( m68k )       || \
154       defined( __MRC__ ) || defined( __MVS__ )   || defined( __MWERKS__ ) || \
155       defined( sparc )   || defined( __sparc)    || defined( SYMANTEC_C ) || \
156       defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM )   || \
157       defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX )
158 #  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
159
160 #elif defined(__arm__)
161 # ifdef __BIG_ENDIAN
162 #  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
163 # else
164 #  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
165 # endif
166 #elif 1     /* **** EDIT HERE IF NECESSARY **** */
167 #  define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
168 #elif 0     /* **** EDIT HERE IF NECESSARY **** */
169 #  define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
170 #else
171 #  error Please edit lines 132 or 134 in brg_endian.h to set the platform byte order
172 #endif
173
174 #endif
175
176 #endif
177
178 // ================================================================================
179 // =================== KeccakSponge.h
180 // ================================================================================
181
182 /*
183 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
184 Michaël Peeters and Gilles Van Assche. For more information, feedback or
185 questions, please refer to our website: http://keccak.noekeon.org/
186
187 Implementation by the designers,
188 hereby denoted as "the implementer".
189
190 To the extent possible under law, the implementer has waived all copyright
191 and related or neighboring rights to the source code in this file.
192 http://creativecommons.org/publicdomain/zero/1.0/
193 */
194
195 #ifndef _KeccakSponge_h_
196 #define _KeccakSponge_h_
197
198 /**
199   * Function to initialize the state of the Keccak[r, c] sponge function.
200   * The sponge function is set to the absorbing phase.
201   * @param  state       Pointer to the state of the sponge function to be initialized.
202   * @param  rate        The value of the rate r.
203   * @param  capacity    The value of the capacity c.
204   * @pre    One must have r+c=1600 and the rate a multiple of 64 bits in this implementation.
205   * @return Zero if successful, 1 otherwise.
206   */
207 int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity);
208 /**
209   * Function to give input data for the sponge function to absorb.
210   * @param  state       Pointer to the state of the sponge function initialized by InitSponge().
211   * @param  data        Pointer to the input data. 
212   *                     When @a databitLen is not a multiple of 8, the last bits of data must be
213   *                     in the least significant bits of the last byte.
214   * @param  databitLen  The number of input bits provided in the input data.
215   * @pre    In the previous call to Absorb(), databitLen was a multiple of 8.
216   * @pre    The sponge function must be in the absorbing phase,
217   *         i.e., Squeeze() must not have been called before.
218   * @return Zero if successful, 1 otherwise.
219   */
220 int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen);
221 /**
222   * Function to squeeze output data from the sponge function.
223   * If the sponge function was in the absorbing phase, this function 
224   * switches it to the squeezing phase.
225   * @param  state       Pointer to the state of the sponge function initialized by InitSponge().
226   * @param  output      Pointer to the buffer where to store the output data.
227   * @param  outputLength    The number of output bits desired.
228   *                     It must be a multiple of 8.
229   * @return Zero if successful, 1 otherwise.
230   */
231 int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength);
232
233 #endif
234
235 // ================================================================================
236 // =================== KeccakF-1600-int-set.h
237 // ================================================================================
238
239
240 #define ProvideFast576
241 #define ProvideFast832
242 #define ProvideFast1024
243 #define ProvideFast1088
244 #define ProvideFast1152
245 #define ProvideFast1344
246
247
248 // ================================================================================
249 // =================== KeccakF-1600-interface.h
250 // ================================================================================
251
252
253 /*
254 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
255 Michaël Peeters and Gilles Van Assche. For more information, feedback or
256 questions, please refer to our website: http://keccak.noekeon.org/
257
258 Implementation by the designers,
259 hereby denoted as "the implementer".
260
261 To the extent possible under law, the implementer has waived all copyright
262 and related or neighboring rights to the source code in this file.
263 http://creativecommons.org/publicdomain/zero/1.0/
264 */
265
266 #ifndef _KeccakPermutationInterface_h_
267 #define _KeccakPermutationInterface_h_
268
269 //#include "KeccakF-1600-int-set.h"
270
271 void KeccakInitialize( void );
272 void KeccakInitializeState(unsigned char *state);
273 void KeccakPermutation(unsigned char *state);
274 #ifdef ProvideFast576
275 void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data);
276 #endif
277 #ifdef ProvideFast832
278 void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data);
279 #endif
280 #ifdef ProvideFast1024
281 void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data);
282 #endif
283 #ifdef ProvideFast1088
284 void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data);
285 #endif
286 #ifdef ProvideFast1152
287 void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data);
288 #endif
289 #ifdef ProvideFast1344
290 void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data);
291 #endif
292 void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount);
293 #ifdef ProvideFast1024
294 void KeccakExtract1024bits(const unsigned char *state, unsigned char *data);
295 #endif
296 void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount);
297
298 #endif
299
300
301 // ================================================================================
302 // =================== KeccakF-1600-opt32-settings.h
303 // ================================================================================
304
305
306 #define Unrolling 2
307 //#define UseBebigokimisa
308 //#define UseInterleaveTables
309 #define UseSchedule 3
310
311
312 // ================================================================================
313 // =================== KeccakF-1600-unrolling.macros
314 // ================================================================================
315
316 /*
317 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
318 Michaël Peeters and Gilles Van Assche. For more information, feedback or
319 questions, please refer to our website: http://keccak.noekeon.org/
320
321 Implementation by the designers,
322 hereby denoted as "the implementer".
323
324 To the extent possible under law, the implementer has waived all copyright
325 and related or neighboring rights to the source code in this file.
326 http://creativecommons.org/publicdomain/zero/1.0/
327 */
328
329 #if (Unrolling == 24)
330 #define rounds \
331     prepareTheta \
332     thetaRhoPiChiIotaPrepareTheta( 0, A, E) \
333     thetaRhoPiChiIotaPrepareTheta( 1, E, A) \
334     thetaRhoPiChiIotaPrepareTheta( 2, A, E) \
335     thetaRhoPiChiIotaPrepareTheta( 3, E, A) \
336     thetaRhoPiChiIotaPrepareTheta( 4, A, E) \
337     thetaRhoPiChiIotaPrepareTheta( 5, E, A) \
338     thetaRhoPiChiIotaPrepareTheta( 6, A, E) \
339     thetaRhoPiChiIotaPrepareTheta( 7, E, A) \
340     thetaRhoPiChiIotaPrepareTheta( 8, A, E) \
341     thetaRhoPiChiIotaPrepareTheta( 9, E, A) \
342     thetaRhoPiChiIotaPrepareTheta(10, A, E) \
343     thetaRhoPiChiIotaPrepareTheta(11, E, A) \
344     thetaRhoPiChiIotaPrepareTheta(12, A, E) \
345     thetaRhoPiChiIotaPrepareTheta(13, E, A) \
346     thetaRhoPiChiIotaPrepareTheta(14, A, E) \
347     thetaRhoPiChiIotaPrepareTheta(15, E, A) \
348     thetaRhoPiChiIotaPrepareTheta(16, A, E) \
349     thetaRhoPiChiIotaPrepareTheta(17, E, A) \
350     thetaRhoPiChiIotaPrepareTheta(18, A, E) \
351     thetaRhoPiChiIotaPrepareTheta(19, E, A) \
352     thetaRhoPiChiIotaPrepareTheta(20, A, E) \
353     thetaRhoPiChiIotaPrepareTheta(21, E, A) \
354     thetaRhoPiChiIotaPrepareTheta(22, A, E) \
355     thetaRhoPiChiIota(23, E, A) \
356     copyToState(state, A)
357 #elif (Unrolling == 12)
358 #define rounds \
359     prepareTheta \
360     for(i=0; i<24; i+=12) { \
361         thetaRhoPiChiIotaPrepareTheta(i   , A, E) \
362         thetaRhoPiChiIotaPrepareTheta(i+ 1, E, A) \
363         thetaRhoPiChiIotaPrepareTheta(i+ 2, A, E) \
364         thetaRhoPiChiIotaPrepareTheta(i+ 3, E, A) \
365         thetaRhoPiChiIotaPrepareTheta(i+ 4, A, E) \
366         thetaRhoPiChiIotaPrepareTheta(i+ 5, E, A) \
367         thetaRhoPiChiIotaPrepareTheta(i+ 6, A, E) \
368         thetaRhoPiChiIotaPrepareTheta(i+ 7, E, A) \
369         thetaRhoPiChiIotaPrepareTheta(i+ 8, A, E) \
370         thetaRhoPiChiIotaPrepareTheta(i+ 9, E, A) \
371         thetaRhoPiChiIotaPrepareTheta(i+10, A, E) \
372         thetaRhoPiChiIotaPrepareTheta(i+11, E, A) \
373     } \
374     copyToState(state, A)
375 #elif (Unrolling == 8)
376 #define rounds \
377     prepareTheta \
378     for(i=0; i<24; i+=8) { \
379         thetaRhoPiChiIotaPrepareTheta(i  , A, E) \
380         thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
381         thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
382         thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \
383         thetaRhoPiChiIotaPrepareTheta(i+4, A, E) \
384         thetaRhoPiChiIotaPrepareTheta(i+5, E, A) \
385         thetaRhoPiChiIotaPrepareTheta(i+6, A, E) \
386         thetaRhoPiChiIotaPrepareTheta(i+7, E, A) \
387     } \
388     copyToState(state, A)
389 #elif (Unrolling == 6)
390 #define rounds \
391     prepareTheta \
392     for(i=0; i<24; i+=6) { \
393         thetaRhoPiChiIotaPrepareTheta(i  , A, E) \
394         thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
395         thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
396         thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \
397         thetaRhoPiChiIotaPrepareTheta(i+4, A, E) \
398         thetaRhoPiChiIotaPrepareTheta(i+5, E, A) \
399     } \
400     copyToState(state, A)
401 #elif (Unrolling == 4)
402 #define rounds \
403     prepareTheta \
404     for(i=0; i<24; i+=4) { \
405         thetaRhoPiChiIotaPrepareTheta(i  , A, E) \
406         thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
407         thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
408         thetaRhoPiChiIotaPrepareTheta(i+3, E, A) \
409     } \
410     copyToState(state, A)
411 #elif (Unrolling == 3)
412 #define rounds \
413     prepareTheta \
414     for(i=0; i<24; i+=3) { \
415         thetaRhoPiChiIotaPrepareTheta(i  , A, E) \
416         thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
417         thetaRhoPiChiIotaPrepareTheta(i+2, A, E) \
418         copyStateVariables(A, E) \
419     } \
420     copyToState(state, A)
421 #elif (Unrolling == 2)
422 #define rounds \
423     prepareTheta \
424     for(i=0; i<24; i+=2) { \
425         thetaRhoPiChiIotaPrepareTheta(i  , A, E) \
426         thetaRhoPiChiIotaPrepareTheta(i+1, E, A) \
427     } \
428     copyToState(state, A)
429 #elif (Unrolling == 1)
430 #define rounds \
431     prepareTheta \
432     for(i=0; i<24; i++) { \
433         thetaRhoPiChiIotaPrepareTheta(i  , A, E) \
434         copyStateVariables(A, E) \
435     } \
436     copyToState(state, A)
437 #else
438 #error "Unrolling is not correctly specified!"
439 #endif
440
441
442 // ================================================================================
443 // =================== KeccakF-1600-32-rvk.macros
444 // ================================================================================
445
446 /*
447 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
448 Michaël Peeters and Gilles Van Assche. For more information, feedback or
449 questions, please refer to our website: http://keccak.noekeon.org/
450
451 Implementation by Ronny Van Keer,
452 hereby denoted as "the implementer".
453
454 To the extent possible under law, the implementer has waived all copyright
455 and related or neighboring rights to the source code in this file.
456 http://creativecommons.org/publicdomain/zero/1.0/
457 */
458
459 static const UINT32 KeccakF1600RoundConstants_int2[2*24] =
460 {
461     0x00000001UL,    0x00000000UL,
462     0x00000000UL,    0x00000089UL,
463     0x00000000UL,    0x8000008bUL,
464     0x00000000UL,    0x80008080UL,
465     0x00000001UL,    0x0000008bUL,
466     0x00000001UL,    0x00008000UL,
467     0x00000001UL,    0x80008088UL,
468     0x00000001UL,    0x80000082UL,
469     0x00000000UL,    0x0000000bUL,
470     0x00000000UL,    0x0000000aUL,
471     0x00000001UL,    0x00008082UL,
472     0x00000000UL,    0x00008003UL,
473     0x00000001UL,    0x0000808bUL,
474     0x00000001UL,    0x8000000bUL,
475     0x00000001UL,    0x8000008aUL,
476     0x00000001UL,    0x80000081UL,
477     0x00000000UL,    0x80000081UL,
478     0x00000000UL,    0x80000008UL,
479     0x00000000UL,    0x00000083UL,
480     0x00000000UL,    0x80008003UL,
481     0x00000001UL,    0x80008088UL,
482     0x00000000UL,    0x80000088UL,
483     0x00000001UL,    0x00008000UL,
484     0x00000000UL,    0x80008082UL
485 };
486
487 #undef rounds
488
489 #define rounds \
490 { \
491     UINT32 Da0, De0, Di0, Do0, Du0; \
492     UINT32 Da1, De1, Di1, Do1, Du1; \
493     UINT32 Ba, Be, Bi, Bo, Bu; \
494     UINT32 Aba0, Abe0, Abi0, Abo0, Abu0; \
495     UINT32 Aba1, Abe1, Abi1, Abo1, Abu1; \
496     UINT32 Aga0, Age0, Agi0, Ago0, Agu0; \
497     UINT32 Aga1, Age1, Agi1, Ago1, Agu1; \
498     UINT32 Aka0, Ake0, Aki0, Ako0, Aku0; \
499     UINT32 Aka1, Ake1, Aki1, Ako1, Aku1; \
500     UINT32 Ama0, Ame0, Ami0, Amo0, Amu0; \
501     UINT32 Ama1, Ame1, Ami1, Amo1, Amu1; \
502     UINT32 Asa0, Ase0, Asi0, Aso0, Asu0; \
503     UINT32 Asa1, Ase1, Asi1, Aso1, Asu1; \
504     UINT32 Cw, Cx, Cy, Cz; \
505     UINT32 Eba0, Ebe0, Ebi0, Ebo0, Ebu0; \
506     UINT32 Eba1, Ebe1, Ebi1, Ebo1, Ebu1; \
507     UINT32 Ega0, Ege0, Egi0, Ego0, Egu0; \
508     UINT32 Ega1, Ege1, Egi1, Ego1, Egu1; \
509     UINT32 Eka0, Eke0, Eki0, Eko0, Eku0; \
510     UINT32 Eka1, Eke1, Eki1, Eko1, Eku1; \
511     UINT32 Ema0, Eme0, Emi0, Emo0, Emu0; \
512     UINT32 Ema1, Eme1, Emi1, Emo1, Emu1; \
513     UINT32 Esa0, Ese0, Esi0, Eso0, Esu0; \
514     UINT32 Esa1, Ese1, Esi1, Eso1, Esu1; \
515         const UINT32 * pRoundConstants = KeccakF1600RoundConstants_int2; \
516     UINT32 i; \
517 \
518     copyFromState(A, state) \
519 \
520     for( i = 12; i != 0; --i ) { \
521             Cx = Abu0^Agu0^Aku0^Amu0^Asu0; \
522             Du1 = Abe1^Age1^Ake1^Ame1^Ase1; \
523             Da0 = Cx^ROL32(Du1, 1); \
524             Cz = Abu1^Agu1^Aku1^Amu1^Asu1; \
525             Du0 = Abe0^Age0^Ake0^Ame0^Ase0; \
526             Da1 = Cz^Du0; \
527 \
528             Cw = Abi0^Agi0^Aki0^Ami0^Asi0; \
529             Do0 = Cw^ROL32(Cz, 1); \
530             Cy = Abi1^Agi1^Aki1^Ami1^Asi1; \
531             Do1 = Cy^Cx; \
532 \
533             Cx = Aba0^Aga0^Aka0^Ama0^Asa0; \
534             De0 = Cx^ROL32(Cy, 1); \
535             Cz = Aba1^Aga1^Aka1^Ama1^Asa1; \
536             De1 = Cz^Cw; \
537 \
538             Cy = Abo1^Ago1^Ako1^Amo1^Aso1; \
539             Di0 = Du0^ROL32(Cy, 1); \
540             Cw = Abo0^Ago0^Ako0^Amo0^Aso0; \
541             Di1 = Du1^Cw; \
542 \
543             Du0 = Cw^ROL32(Cz, 1); \
544             Du1 = Cy^Cx; \
545 \
546             Aba0 ^= Da0; \
547             Ba = Aba0; \
548             Age0 ^= De0; \
549             Be = ROL32(Age0, 22); \
550             Aki1 ^= Di1; \
551             Bi = ROL32(Aki1, 22); \
552             Amo1 ^= Do1; \
553             Bo = ROL32(Amo1, 11); \
554             Asu0 ^= Du0; \
555             Bu = ROL32(Asu0, 7); \
556             Eba0 =   Ba ^((~Be)&  Bi ) ^ *(pRoundConstants++); \
557             Ebe0 =   Be ^((~Bi)&  Bo ); \
558             Ebi0 =   Bi ^((~Bo)&  Bu ); \
559             Ebo0 =   Bo ^((~Bu)&  Ba ); \
560             Ebu0 =   Bu ^((~Ba)&  Be ); \
561 \
562             Abo0 ^= Do0; \
563             Ba = ROL32(Abo0, 14); \
564             Agu0 ^= Du0; \
565             Be = ROL32(Agu0, 10); \
566             Aka1 ^= Da1; \
567             Bi = ROL32(Aka1, 2); \
568             Ame1 ^= De1; \
569             Bo = ROL32(Ame1, 23); \
570             Asi1 ^= Di1; \
571             Bu = ROL32(Asi1, 31); \
572             Ega0 =   Ba ^((~Be)&  Bi ); \
573             Ege0 =   Be ^((~Bi)&  Bo ); \
574             Egi0 =   Bi ^((~Bo)&  Bu ); \
575             Ego0 =   Bo ^((~Bu)&  Ba ); \
576             Egu0 =   Bu ^((~Ba)&  Be ); \
577 \
578             Abe1 ^= De1; \
579             Ba = ROL32(Abe1, 1); \
580             Agi0 ^= Di0; \
581             Be = ROL32(Agi0, 3); \
582             Ako1 ^= Do1; \
583             Bi = ROL32(Ako1, 13); \
584             Amu0 ^= Du0; \
585             Bo = ROL32(Amu0, 4); \
586             Asa0 ^= Da0; \
587             Bu = ROL32(Asa0, 9); \
588             Eka0 =   Ba ^((~Be)&  Bi ); \
589             Eke0 =   Be ^((~Bi)&  Bo ); \
590             Eki0 =   Bi ^((~Bo)&  Bu ); \
591             Eko0 =   Bo ^((~Bu)&  Ba ); \
592             Eku0 =   Bu ^((~Ba)&  Be ); \
593 \
594             Abu1 ^= Du1; \
595             Ba = ROL32(Abu1, 14); \
596             Aga0 ^= Da0; \
597             Be = ROL32(Aga0, 18); \
598             Ake0 ^= De0; \
599             Bi = ROL32(Ake0, 5); \
600             Ami1 ^= Di1; \
601             Bo = ROL32(Ami1, 8); \
602             Aso0 ^= Do0; \
603             Bu = ROL32(Aso0, 28); \
604             Ema0 =   Ba ^((~Be)&  Bi ); \
605             Eme0 =   Be ^((~Bi)&  Bo ); \
606             Emi0 =   Bi ^((~Bo)&  Bu ); \
607             Emo0 =   Bo ^((~Bu)&  Ba ); \
608             Emu0 =   Bu ^((~Ba)&  Be ); \
609 \
610             Abi0 ^= Di0; \
611             Ba = ROL32(Abi0, 31); \
612             Ago1 ^= Do1; \
613             Be = ROL32(Ago1, 28); \
614             Aku1 ^= Du1; \
615             Bi = ROL32(Aku1, 20); \
616             Ama1 ^= Da1; \
617             Bo = ROL32(Ama1, 21); \
618             Ase0 ^= De0; \
619             Bu = ROL32(Ase0, 1); \
620             Esa0 =   Ba ^((~Be)&  Bi ); \
621             Ese0 =   Be ^((~Bi)&  Bo ); \
622             Esi0 =   Bi ^((~Bo)&  Bu ); \
623             Eso0 =   Bo ^((~Bu)&  Ba ); \
624             Esu0 =   Bu ^((~Ba)&  Be ); \
625 \
626             Aba1 ^= Da1; \
627             Ba = Aba1; \
628             Age1 ^= De1; \
629             Be = ROL32(Age1, 22); \
630             Aki0 ^= Di0; \
631             Bi = ROL32(Aki0, 21); \
632             Amo0 ^= Do0; \
633             Bo = ROL32(Amo0, 10); \
634             Asu1 ^= Du1; \
635             Bu = ROL32(Asu1, 7); \
636             Eba1 =   Ba ^((~Be)&  Bi ); \
637             Eba1 ^= *(pRoundConstants++); \
638             Ebe1 =   Be ^((~Bi)&  Bo ); \
639             Ebi1 =   Bi ^((~Bo)&  Bu ); \
640             Ebo1 =   Bo ^((~Bu)&  Ba ); \
641             Ebu1 =   Bu ^((~Ba)&  Be ); \
642 \
643             Abo1 ^= Do1; \
644             Ba = ROL32(Abo1, 14); \
645             Agu1 ^= Du1; \
646             Be = ROL32(Agu1, 10); \
647             Aka0 ^= Da0; \
648             Bi = ROL32(Aka0, 1); \
649             Ame0 ^= De0; \
650             Bo = ROL32(Ame0, 22); \
651             Asi0 ^= Di0; \
652             Bu = ROL32(Asi0, 30); \
653             Ega1 =   Ba ^((~Be)&  Bi ); \
654             Ege1 =   Be ^((~Bi)&  Bo ); \
655             Egi1 =   Bi ^((~Bo)&  Bu ); \
656             Ego1 =   Bo ^((~Bu)&  Ba ); \
657             Egu1 =   Bu ^((~Ba)&  Be ); \
658 \
659             Abe0 ^= De0; \
660             Ba = Abe0; \
661             Agi1 ^= Di1; \
662             Be = ROL32(Agi1, 3); \
663             Ako0 ^= Do0; \
664             Bi = ROL32(Ako0, 12); \
665             Amu1 ^= Du1; \
666             Bo = ROL32(Amu1, 4); \
667             Asa1 ^= Da1; \
668             Bu = ROL32(Asa1, 9); \
669             Eka1 =   Ba ^((~Be)&  Bi ); \
670             Eke1 =   Be ^((~Bi)&  Bo ); \
671             Eki1 =   Bi ^((~Bo)&  Bu ); \
672             Eko1 =   Bo ^((~Bu)&  Ba ); \
673             Eku1 =   Bu ^((~Ba)&  Be ); \
674 \
675             Abu0 ^= Du0; \
676             Ba = ROL32(Abu0, 13); \
677             Aga1 ^= Da1; \
678             Be = ROL32(Aga1, 18); \
679             Ake1 ^= De1; \
680             Bi = ROL32(Ake1, 5); \
681             Ami0 ^= Di0; \
682             Bo = ROL32(Ami0, 7); \
683             Aso1 ^= Do1; \
684             Bu = ROL32(Aso1, 28); \
685             Ema1 =   Ba ^((~Be)&  Bi ); \
686             Eme1 =   Be ^((~Bi)&  Bo ); \
687             Emi1 =   Bi ^((~Bo)&  Bu ); \
688             Emo1 =   Bo ^((~Bu)&  Ba ); \
689             Emu1 =   Bu ^((~Ba)&  Be ); \
690 \
691             Abi1 ^= Di1; \
692             Ba = ROL32(Abi1, 31); \
693             Ago0 ^= Do0; \
694             Be = ROL32(Ago0, 27); \
695             Aku0 ^= Du0; \
696             Bi = ROL32(Aku0, 19); \
697             Ama0 ^= Da0; \
698             Bo = ROL32(Ama0, 20); \
699             Ase1 ^= De1; \
700             Bu = ROL32(Ase1, 1); \
701             Esa1 =   Ba ^((~Be)&  Bi ); \
702             Ese1 =   Be ^((~Bi)&  Bo ); \
703             Esi1 =   Bi ^((~Bo)&  Bu ); \
704             Eso1 =   Bo ^((~Bu)&  Ba ); \
705             Esu1 =   Bu ^((~Ba)&  Be ); \
706 \
707             Cx = Ebu0^Egu0^Eku0^Emu0^Esu0; \
708             Du1 = Ebe1^Ege1^Eke1^Eme1^Ese1; \
709             Da0 = Cx^ROL32(Du1, 1); \
710             Cz = Ebu1^Egu1^Eku1^Emu1^Esu1; \
711             Du0 = Ebe0^Ege0^Eke0^Eme0^Ese0; \
712             Da1 = Cz^Du0; \
713 \
714             Cw = Ebi0^Egi0^Eki0^Emi0^Esi0; \
715             Do0 = Cw^ROL32(Cz, 1); \
716             Cy = Ebi1^Egi1^Eki1^Emi1^Esi1; \
717             Do1 = Cy^Cx; \
718 \
719             Cx = Eba0^Ega0^Eka0^Ema0^Esa0; \
720             De0 = Cx^ROL32(Cy, 1); \
721             Cz = Eba1^Ega1^Eka1^Ema1^Esa1; \
722             De1 = Cz^Cw; \
723 \
724             Cy = Ebo1^Ego1^Eko1^Emo1^Eso1; \
725             Di0 = Du0^ROL32(Cy, 1); \
726             Cw = Ebo0^Ego0^Eko0^Emo0^Eso0; \
727             Di1 = Du1^Cw; \
728 \
729             Du0 = Cw^ROL32(Cz, 1); \
730             Du1 = Cy^Cx; \
731 \
732             Eba0 ^= Da0; \
733             Ba = Eba0; \
734             Ege0 ^= De0; \
735             Be = ROL32(Ege0, 22); \
736             Eki1 ^= Di1; \
737             Bi = ROL32(Eki1, 22); \
738             Emo1 ^= Do1; \
739             Bo = ROL32(Emo1, 11); \
740             Esu0 ^= Du0; \
741             Bu = ROL32(Esu0, 7); \
742             Aba0 =   Ba ^((~Be)&  Bi ); \
743             Aba0 ^= *(pRoundConstants++); \
744             Abe0 =   Be ^((~Bi)&  Bo ); \
745             Abi0 =   Bi ^((~Bo)&  Bu ); \
746             Abo0 =   Bo ^((~Bu)&  Ba ); \
747             Abu0 =   Bu ^((~Ba)&  Be ); \
748 \
749             Ebo0 ^= Do0; \
750             Ba = ROL32(Ebo0, 14); \
751             Egu0 ^= Du0; \
752             Be = ROL32(Egu0, 10); \
753             Eka1 ^= Da1; \
754             Bi = ROL32(Eka1, 2); \
755             Eme1 ^= De1; \
756             Bo = ROL32(Eme1, 23); \
757             Esi1 ^= Di1; \
758             Bu = ROL32(Esi1, 31); \
759             Aga0 =   Ba ^((~Be)&  Bi ); \
760             Age0 =   Be ^((~Bi)&  Bo ); \
761             Agi0 =   Bi ^((~Bo)&  Bu ); \
762             Ago0 =   Bo ^((~Bu)&  Ba ); \
763             Agu0 =   Bu ^((~Ba)&  Be ); \
764 \
765             Ebe1 ^= De1; \
766             Ba = ROL32(Ebe1, 1); \
767             Egi0 ^= Di0; \
768             Be = ROL32(Egi0, 3); \
769             Eko1 ^= Do1; \
770             Bi = ROL32(Eko1, 13); \
771             Emu0 ^= Du0; \
772             Bo = ROL32(Emu0, 4); \
773             Esa0 ^= Da0; \
774             Bu = ROL32(Esa0, 9); \
775             Aka0 =   Ba ^((~Be)&  Bi ); \
776             Ake0 =   Be ^((~Bi)&  Bo ); \
777             Aki0 =   Bi ^((~Bo)&  Bu ); \
778             Ako0 =   Bo ^((~Bu)&  Ba ); \
779             Aku0 =   Bu ^((~Ba)&  Be ); \
780 \
781             Ebu1 ^= Du1; \
782             Ba = ROL32(Ebu1, 14); \
783             Ega0 ^= Da0; \
784             Be = ROL32(Ega0, 18); \
785             Eke0 ^= De0; \
786             Bi = ROL32(Eke0, 5); \
787             Emi1 ^= Di1; \
788             Bo = ROL32(Emi1, 8); \
789             Eso0 ^= Do0; \
790             Bu = ROL32(Eso0, 28); \
791             Ama0 =   Ba ^((~Be)&  Bi ); \
792             Ame0 =   Be ^((~Bi)&  Bo ); \
793             Ami0 =   Bi ^((~Bo)&  Bu ); \
794             Amo0 =   Bo ^((~Bu)&  Ba ); \
795             Amu0 =   Bu ^((~Ba)&  Be ); \
796 \
797             Ebi0 ^= Di0; \
798             Ba = ROL32(Ebi0, 31); \
799             Ego1 ^= Do1; \
800             Be = ROL32(Ego1, 28); \
801             Eku1 ^= Du1; \
802             Bi = ROL32(Eku1, 20); \
803             Ema1 ^= Da1; \
804             Bo = ROL32(Ema1, 21); \
805             Ese0 ^= De0; \
806             Bu = ROL32(Ese0, 1); \
807             Asa0 =   Ba ^((~Be)&  Bi ); \
808             Ase0 =   Be ^((~Bi)&  Bo ); \
809             Asi0 =   Bi ^((~Bo)&  Bu ); \
810             Aso0 =   Bo ^((~Bu)&  Ba ); \
811             Asu0 =   Bu ^((~Ba)&  Be ); \
812 \
813             Eba1 ^= Da1; \
814             Ba = Eba1; \
815             Ege1 ^= De1; \
816             Be = ROL32(Ege1, 22); \
817             Eki0 ^= Di0; \
818             Bi = ROL32(Eki0, 21); \
819             Emo0 ^= Do0; \
820             Bo = ROL32(Emo0, 10); \
821             Esu1 ^= Du1; \
822             Bu = ROL32(Esu1, 7); \
823             Aba1 =   Ba ^((~Be)&  Bi ); \
824             Aba1 ^= *(pRoundConstants++); \
825             Abe1 =   Be ^((~Bi)&  Bo ); \
826             Abi1 =   Bi ^((~Bo)&  Bu ); \
827             Abo1 =   Bo ^((~Bu)&  Ba ); \
828             Abu1 =   Bu ^((~Ba)&  Be ); \
829 \
830             Ebo1 ^= Do1; \
831             Ba = ROL32(Ebo1, 14); \
832             Egu1 ^= Du1; \
833             Be = ROL32(Egu1, 10); \
834             Eka0 ^= Da0; \
835             Bi = ROL32(Eka0, 1); \
836             Eme0 ^= De0; \
837             Bo = ROL32(Eme0, 22); \
838             Esi0 ^= Di0; \
839             Bu = ROL32(Esi0, 30); \
840             Aga1 =   Ba ^((~Be)&  Bi ); \
841             Age1 =   Be ^((~Bi)&  Bo ); \
842             Agi1 =   Bi ^((~Bo)&  Bu ); \
843             Ago1 =   Bo ^((~Bu)&  Ba ); \
844             Agu1 =   Bu ^((~Ba)&  Be ); \
845 \
846             Ebe0 ^= De0; \
847             Ba = Ebe0; \
848             Egi1 ^= Di1; \
849             Be = ROL32(Egi1, 3); \
850             Eko0 ^= Do0; \
851             Bi = ROL32(Eko0, 12); \
852             Emu1 ^= Du1; \
853             Bo = ROL32(Emu1, 4); \
854             Esa1 ^= Da1; \
855             Bu = ROL32(Esa1, 9); \
856             Aka1 =   Ba ^((~Be)&  Bi ); \
857             Ake1 =   Be ^((~Bi)&  Bo ); \
858             Aki1 =   Bi ^((~Bo)&  Bu ); \
859             Ako1 =   Bo ^((~Bu)&  Ba ); \
860             Aku1 =   Bu ^((~Ba)&  Be ); \
861 \
862             Ebu0 ^= Du0; \
863             Ba = ROL32(Ebu0, 13); \
864             Ega1 ^= Da1; \
865             Be = ROL32(Ega1, 18); \
866             Eke1 ^= De1; \
867             Bi = ROL32(Eke1, 5); \
868             Emi0 ^= Di0; \
869             Bo = ROL32(Emi0, 7); \
870             Eso1 ^= Do1; \
871             Bu = ROL32(Eso1, 28); \
872             Ama1 =   Ba ^((~Be)&  Bi ); \
873             Ame1 =   Be ^((~Bi)&  Bo ); \
874             Ami1 =   Bi ^((~Bo)&  Bu ); \
875             Amo1 =   Bo ^((~Bu)&  Ba ); \
876             Amu1 =   Bu ^((~Ba)&  Be ); \
877 \
878             Ebi1 ^= Di1; \
879             Ba = ROL32(Ebi1, 31); \
880             Ego0 ^= Do0; \
881             Be = ROL32(Ego0, 27); \
882             Eku0 ^= Du0; \
883             Bi = ROL32(Eku0, 19); \
884             Ema0 ^= Da0; \
885             Bo = ROL32(Ema0, 20); \
886             Ese1 ^= De1; \
887             Bu = ROL32(Ese1, 1); \
888             Asa1 =   Ba ^((~Be)&  Bi ); \
889             Ase1 =   Be ^((~Bi)&  Bo ); \
890             Asi1 =   Bi ^((~Bo)&  Bu ); \
891             Aso1 =   Bo ^((~Bu)&  Ba ); \
892             Asu1 =   Bu ^((~Ba)&  Be ); \
893     } \
894     copyToState(state, A) \
895 }
896
897 #define copyFromState(X, state) \
898     X##ba0 = state[ 0]; \
899     X##ba1 = state[ 1]; \
900     X##be0 = state[ 2]; \
901     X##be1 = state[ 3]; \
902     X##bi0 = state[ 4]; \
903     X##bi1 = state[ 5]; \
904     X##bo0 = state[ 6]; \
905     X##bo1 = state[ 7]; \
906     X##bu0 = state[ 8]; \
907     X##bu1 = state[ 9]; \
908     X##ga0 = state[10]; \
909     X##ga1 = state[11]; \
910     X##ge0 = state[12]; \
911     X##ge1 = state[13]; \
912     X##gi0 = state[14]; \
913     X##gi1 = state[15]; \
914     X##go0 = state[16]; \
915     X##go1 = state[17]; \
916     X##gu0 = state[18]; \
917     X##gu1 = state[19]; \
918     X##ka0 = state[20]; \
919     X##ka1 = state[21]; \
920     X##ke0 = state[22]; \
921     X##ke1 = state[23]; \
922     X##ki0 = state[24]; \
923     X##ki1 = state[25]; \
924     X##ko0 = state[26]; \
925     X##ko1 = state[27]; \
926     X##ku0 = state[28]; \
927     X##ku1 = state[29]; \
928     X##ma0 = state[30]; \
929     X##ma1 = state[31]; \
930     X##me0 = state[32]; \
931     X##me1 = state[33]; \
932     X##mi0 = state[34]; \
933     X##mi1 = state[35]; \
934     X##mo0 = state[36]; \
935     X##mo1 = state[37]; \
936     X##mu0 = state[38]; \
937     X##mu1 = state[39]; \
938     X##sa0 = state[40]; \
939     X##sa1 = state[41]; \
940     X##se0 = state[42]; \
941     X##se1 = state[43]; \
942     X##si0 = state[44]; \
943     X##si1 = state[45]; \
944     X##so0 = state[46]; \
945     X##so1 = state[47]; \
946     X##su0 = state[48]; \
947     X##su1 = state[49]; \
948
949 #define copyToState(state, X) \
950     state[ 0] = X##ba0; \
951     state[ 1] = X##ba1; \
952     state[ 2] = X##be0; \
953     state[ 3] = X##be1; \
954     state[ 4] = X##bi0; \
955     state[ 5] = X##bi1; \
956     state[ 6] = X##bo0; \
957     state[ 7] = X##bo1; \
958     state[ 8] = X##bu0; \
959     state[ 9] = X##bu1; \
960     state[10] = X##ga0; \
961     state[11] = X##ga1; \
962     state[12] = X##ge0; \
963     state[13] = X##ge1; \
964     state[14] = X##gi0; \
965     state[15] = X##gi1; \
966     state[16] = X##go0; \
967     state[17] = X##go1; \
968     state[18] = X##gu0; \
969     state[19] = X##gu1; \
970     state[20] = X##ka0; \
971     state[21] = X##ka1; \
972     state[22] = X##ke0; \
973     state[23] = X##ke1; \
974     state[24] = X##ki0; \
975     state[25] = X##ki1; \
976     state[26] = X##ko0; \
977     state[27] = X##ko1; \
978     state[28] = X##ku0; \
979     state[29] = X##ku1; \
980     state[30] = X##ma0; \
981     state[31] = X##ma1; \
982     state[32] = X##me0; \
983     state[33] = X##me1; \
984     state[34] = X##mi0; \
985     state[35] = X##mi1; \
986     state[36] = X##mo0; \
987     state[37] = X##mo1; \
988     state[38] = X##mu0; \
989     state[39] = X##mu1; \
990     state[40] = X##sa0; \
991     state[41] = X##sa1; \
992     state[42] = X##se0; \
993     state[43] = X##se1; \
994     state[44] = X##si0; \
995     state[45] = X##si1; \
996     state[46] = X##so0; \
997     state[47] = X##so1; \
998     state[48] = X##su0; \
999     state[49] = X##su1; \
1000
1001
1002 // ================================================================================
1003 // =================== KeccakF-1600-32.macros
1004 // ================================================================================
1005
1006 /*
1007 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
1008 Michaël Peeters and Gilles Van Assche. For more information, feedback or
1009 questions, please refer to our website: http://keccak.noekeon.org/
1010
1011 Implementation by the designers,
1012 hereby denoted as "the implementer".
1013
1014 To the extent possible under law, the implementer has waived all copyright
1015 and related or neighboring rights to the source code in this file.
1016 http://creativecommons.org/publicdomain/zero/1.0/
1017 */
1018
1019 #ifdef UseSchedule
1020     #if (UseSchedule == 1)
1021         #include "KeccakF-1600-32-s1.macros"
1022     #elif (UseSchedule == 2)
1023         #include "KeccakF-1600-32-s2.macros"
1024     #elif (UseSchedule == 3)
1025         //#include "KeccakF-1600-32-rvk.macros"
1026     #else
1027         #error "This schedule is not supported."
1028     #endif
1029 #else
1030     #include "KeccakF-1600-32-s1.macros"
1031 #endif
1032
1033 // ================================================================================
1034 // =================== KeccakF-1600-opt32.c
1035 // ================================================================================
1036
1037
1038 /*
1039 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
1040 Michaël Peeters and Gilles Van Assche. For more information, feedback or
1041 questions, please refer to our website: http://keccak.noekeon.org/
1042
1043 Implementation by the designers,
1044 hereby denoted as "the implementer".
1045
1046 To the extent possible under law, the implementer has waived all copyright
1047 and related or neighboring rights to the source code in this file.
1048 http://creativecommons.org/publicdomain/zero/1.0/
1049 */
1050
1051 #include <string.h>
1052 //#include "brg_endian.h"
1053 //#include "KeccakF-1600-opt32-settings.h"
1054 //#include "KeccakF-1600-interface.h"
1055
1056
1057
1058 #ifdef UseInterleaveTables
1059 int interleaveTablesBuilt = 0;
1060 UINT16 interleaveTable[65536];
1061 UINT16 deinterleaveTable[65536];
1062
1063 void buildInterleaveTables()
1064 {
1065     UINT32 i, j;
1066     UINT16 x;
1067
1068     if (!interleaveTablesBuilt) {
1069         for(i=0; i<65536; i++) {
1070             x = 0;
1071             for(j=0; j<16; j++) {
1072                 if (i & (1 << j))
1073                     x |= (1 << (j/2 + 8*(j%2)));
1074             }
1075             interleaveTable[i] = x;
1076             deinterleaveTable[x] = (UINT16)i;
1077         }
1078         interleaveTablesBuilt = 1;
1079     }
1080 }
1081
1082 #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
1083
1084 #define xor2bytesIntoInterleavedWords(even, odd, source, j) \
1085     i##j = interleaveTable[((const UINT16*)source)[j]]; \
1086     ((UINT8*)even)[j] ^= i##j & 0xFF; \
1087     ((UINT8*)odd)[j] ^= i##j >> 8;
1088
1089 #define setInterleavedWordsInto2bytes(dest, even, odd, j) \
1090     d##j = deinterleaveTable[((even >> (j*8)) & 0xFF) ^ (((odd >> (j*8)) & 0xFF) << 8)]; \
1091     ((UINT16*)dest)[j] = d##j;
1092
1093 #else // (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
1094
1095 #define xor2bytesIntoInterleavedWords(even, odd, source, j) \
1096     i##j = interleaveTable[source[2*j] ^ ((UINT16)source[2*j+1] << 8)]; \
1097     *even ^= (i##j & 0xFF) << (j*8); \
1098     *odd ^= ((i##j >> 8) & 0xFF) << (j*8);
1099
1100 #define setInterleavedWordsInto2bytes(dest, even, odd, j) \
1101     d##j = deinterleaveTable[((even >> (j*8)) & 0xFF) ^ (((odd >> (j*8)) & 0xFF) << 8)]; \
1102     dest[2*j] = d##j & 0xFF; \
1103     dest[2*j+1] = d##j >> 8;
1104
1105 #endif // Endianness
1106
1107 void xor8bytesIntoInterleavedWords(UINT32 *even, UINT32 *odd, const UINT8* source)
1108 {
1109     UINT16 i0, i1, i2, i3;
1110
1111     xor2bytesIntoInterleavedWords(even, odd, source, 0)
1112     xor2bytesIntoInterleavedWords(even, odd, source, 1)
1113     xor2bytesIntoInterleavedWords(even, odd, source, 2)
1114     xor2bytesIntoInterleavedWords(even, odd, source, 3)
1115 }
1116
1117 #define xorLanesIntoState(laneCount, state, input) \
1118     { \
1119         int i; \
1120         for(i=0; i<(laneCount); i++) \
1121             xor8bytesIntoInterleavedWords(state+i*2, state+i*2+1, input+i*8); \
1122     }
1123
1124 void setInterleavedWordsInto8bytes(UINT8* dest, UINT32 even, UINT32 odd)
1125 {
1126     UINT16 d0, d1, d2, d3;
1127
1128     setInterleavedWordsInto2bytes(dest, even, odd, 0)
1129     setInterleavedWordsInto2bytes(dest, even, odd, 1)
1130     setInterleavedWordsInto2bytes(dest, even, odd, 2)
1131     setInterleavedWordsInto2bytes(dest, even, odd, 3)
1132 }
1133
1134 #define extractLanes(laneCount, state, data) \
1135     { \
1136         int i; \
1137         for(i=0; i<(laneCount); i++) \
1138             setInterleavedWordsInto8bytes(data+i*8, ((UINT32*)state)[i*2], ((UINT32*)state)[i*2+1]); \
1139     }
1140
1141 #else // No interleaving tables
1142
1143 #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
1144
1145 // Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002
1146 #define xorInterleavedLE(rateInLanes, state, input) \
1147         { \
1148                 const UINT32 * pI = (const UINT32 *)input; \
1149                 UINT32 * pS = state; \
1150                 UINT32 t, x0, x1; \
1151             int i; \
1152             for (i = (rateInLanes)-1; i >= 0; --i) \
1153                 { \
1154                         x0 = *(pI++); \
1155                         t = (x0 ^ (x0 >>  1)) & 0x22222222UL;  x0 = x0 ^ t ^ (t <<  1); \
1156                         t = (x0 ^ (x0 >>  2)) & 0x0C0C0C0CUL;  x0 = x0 ^ t ^ (t <<  2); \
1157                         t = (x0 ^ (x0 >>  4)) & 0x00F000F0UL;  x0 = x0 ^ t ^ (t <<  4); \
1158                         t = (x0 ^ (x0 >>  8)) & 0x0000FF00UL;  x0 = x0 ^ t ^ (t <<  8); \
1159                         x1 = *(pI++); \
1160                         t = (x1 ^ (x1 >>  1)) & 0x22222222UL;  x1 = x1 ^ t ^ (t <<  1); \
1161                         t = (x1 ^ (x1 >>  2)) & 0x0C0C0C0CUL;  x1 = x1 ^ t ^ (t <<  2); \
1162                         t = (x1 ^ (x1 >>  4)) & 0x00F000F0UL;  x1 = x1 ^ t ^ (t <<  4); \
1163                         t = (x1 ^ (x1 >>  8)) & 0x0000FF00UL;  x1 = x1 ^ t ^ (t <<  8); \
1164                         *(pS++) ^= (UINT16)x0 | (x1 << 16); \
1165                         *(pS++) ^= (x0 >> 16) | (x1 & 0xFFFF0000); \
1166                 } \
1167         }
1168
1169 #define xorLanesIntoState(laneCount, state, input) \
1170     xorInterleavedLE(laneCount, state, input)
1171
1172 #else // (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
1173
1174 // Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002
1175 UINT64 toInterleaving(UINT64 x) 
1176 {
1177    UINT64 t;
1178
1179    t = (x ^ (x >>  1)) & 0x2222222222222222ULL;  x = x ^ t ^ (t <<  1);
1180    t = (x ^ (x >>  2)) & 0x0C0C0C0C0C0C0C0CULL;  x = x ^ t ^ (t <<  2);
1181    t = (x ^ (x >>  4)) & 0x00F000F000F000F0ULL;  x = x ^ t ^ (t <<  4);
1182    t = (x ^ (x >>  8)) & 0x0000FF000000FF00ULL;  x = x ^ t ^ (t <<  8);
1183    t = (x ^ (x >> 16)) & 0x00000000FFFF0000ULL;  x = x ^ t ^ (t << 16);
1184
1185    return x;
1186 }
1187
1188 void xor8bytesIntoInterleavedWords(UINT32* evenAndOdd, const UINT8* source)
1189 {
1190     // This can be optimized
1191     UINT64 sourceWord =
1192         (UINT64)source[0]
1193         ^ (((UINT64)source[1]) <<  8)
1194         ^ (((UINT64)source[2]) << 16)
1195         ^ (((UINT64)source[3]) << 24)
1196         ^ (((UINT64)source[4]) << 32)
1197         ^ (((UINT64)source[5]) << 40)
1198         ^ (((UINT64)source[6]) << 48)
1199         ^ (((UINT64)source[7]) << 56);
1200     UINT64 evenAndOddWord = toInterleaving(sourceWord);
1201     evenAndOdd[0] ^= (UINT32)evenAndOddWord;
1202     evenAndOdd[1] ^= (UINT32)(evenAndOddWord >> 32);
1203 }
1204
1205 #define xorLanesIntoState(laneCount, state, input) \
1206     { \
1207         int i; \
1208         for(i=0; i<(laneCount); i++) \
1209             xor8bytesIntoInterleavedWords(state+i*2, input+i*8); \
1210     }
1211
1212 #endif // Endianness
1213
1214 // Credit: Henry S. Warren, Hacker's Delight, Addison-Wesley, 2002
1215 UINT64 fromInterleaving(UINT64 x)
1216 {
1217    UINT64 t;
1218
1219    t = (x ^ (x >> 16)) & 0x00000000FFFF0000ULL;  x = x ^ t ^ (t << 16);
1220    t = (x ^ (x >>  8)) & 0x0000FF000000FF00ULL;  x = x ^ t ^ (t <<  8);
1221    t = (x ^ (x >>  4)) & 0x00F000F000F000F0ULL;  x = x ^ t ^ (t <<  4);
1222    t = (x ^ (x >>  2)) & 0x0C0C0C0C0C0C0C0CULL;  x = x ^ t ^ (t <<  2);
1223    t = (x ^ (x >>  1)) & 0x2222222222222222ULL;  x = x ^ t ^ (t <<  1);
1224
1225    return x;
1226 }
1227
1228 void setInterleavedWordsInto8bytes(UINT8* dest, UINT32* evenAndOdd)
1229 {
1230 #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
1231     ((UINT64*)dest)[0] = fromInterleaving(*(UINT64*)evenAndOdd);
1232 #else // (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
1233     // This can be optimized
1234     UINT64 evenAndOddWord = (UINT64)evenAndOdd[0] ^ ((UINT64)evenAndOdd[1] << 32);
1235     UINT64 destWord = fromInterleaving(evenAndOddWord);
1236     dest[0] = destWord & 0xFF;
1237     dest[1] = (destWord >> 8) & 0xFF;
1238     dest[2] = (destWord >> 16) & 0xFF;
1239     dest[3] = (destWord >> 24) & 0xFF;
1240     dest[4] = (destWord >> 32) & 0xFF;
1241     dest[5] = (destWord >> 40) & 0xFF;
1242     dest[6] = (destWord >> 48) & 0xFF;
1243     dest[7] = (destWord >> 56) & 0xFF;
1244 #endif // Endianness
1245 }
1246
1247 #define extractLanes(laneCount, state, data) \
1248     { \
1249         int i; \
1250         for(i=0; i<(laneCount); i++) \
1251             setInterleavedWordsInto8bytes(data+i*8, (UINT32*)state+i*2); \
1252     }
1253
1254 #endif // With or without interleaving tables
1255
1256 #if defined(_MSC_VER)
1257 #define ROL32(a, offset) _rotl(a, offset)
1258 #elif (defined (__arm__) && defined(__ARMCC_VERSION))
1259 #define ROL32(a, offset) __ror(a, 32-(offset))
1260 #else
1261 #define ROL32(a, offset) ((((UINT32)a) << (offset)) ^ (((UINT32)a) >> (32-(offset))))
1262 #endif
1263
1264 //#include "KeccakF-1600-unrolling.macros"
1265 //#include "KeccakF-1600-32.macros"
1266
1267 #if (UseSchedule == 3)
1268
1269 #ifdef UseBebigokimisa
1270 #error "No lane complementing with schedule 3."
1271 #endif
1272
1273 #if (Unrolling != 2)
1274 #error "Only unrolling 2 is supported by schedule 3."
1275 #endif
1276
1277 void KeccakPermutationOnWords(UINT32 *state)
1278 {
1279     rounds
1280 }
1281
1282 void KeccakPermutationOnWordsAfterXoring(UINT32 *state, const UINT8 *input, unsigned int laneCount)
1283 {
1284     xorLanesIntoState(laneCount, state, input)
1285     rounds
1286 }
1287
1288 #ifdef ProvideFast576
1289 void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *input)
1290 {
1291     xorLanesIntoState(9, state, input)
1292     rounds
1293 }
1294 #endif
1295
1296 #ifdef ProvideFast832
1297 void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *input)
1298 {
1299     xorLanesIntoState(13, state, input)
1300     rounds
1301 }
1302 #endif
1303
1304 #ifdef ProvideFast1024
1305 void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *input)
1306 {
1307     xorLanesIntoState(16, state, input)
1308     rounds
1309 }
1310 #endif
1311
1312 #ifdef ProvideFast1088
1313 void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *input)
1314 {
1315     xorLanesIntoState(17, state, input)
1316     rounds
1317 }
1318 #endif
1319
1320 #ifdef ProvideFast1152
1321 void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *input)
1322 {
1323     xorLanesIntoState(18, state, input)
1324     rounds
1325 }
1326 #endif
1327
1328 #ifdef ProvideFast1344
1329 void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *input)
1330 {
1331     xorLanesIntoState(21, state, input)
1332     rounds
1333 }
1334 #endif
1335
1336 #else // (Schedule != 3)
1337
1338 void KeccakPermutationOnWords(UINT32 *state)
1339 {
1340     declareABCDE
1341 #if (Unrolling != 24)
1342     unsigned int i;
1343 #endif
1344
1345     copyFromState(A, state)
1346     rounds
1347 }
1348
1349 void KeccakPermutationOnWordsAfterXoring(UINT32 *state, const UINT8 *input, unsigned int laneCount)
1350 {
1351     declareABCDE
1352     unsigned int i;
1353
1354     xorLanesIntoState(laneCount, state, input)
1355     copyFromState(A, state)
1356     rounds
1357 }
1358
1359 #ifdef ProvideFast576
1360 void KeccakPermutationOnWordsAfterXoring576bits(UINT32 *state, const UINT8 *input)
1361 {
1362     declareABCDE
1363     unsigned int i;
1364
1365     xorLanesIntoState(9, state, input)
1366     copyFromState(A, state)
1367     rounds
1368 }
1369 #endif
1370
1371 #ifdef ProvideFast832
1372 void KeccakPermutationOnWordsAfterXoring832bits(UINT32 *state, const UINT8 *input)
1373 {
1374     declareABCDE
1375     unsigned int i;
1376
1377     xorLanesIntoState(13, state, input)
1378     copyFromState(A, state)
1379     rounds
1380 }
1381 #endif
1382
1383 #ifdef ProvideFast1024
1384 void KeccakPermutationOnWordsAfterXoring1024bits(UINT32 *state, const UINT8 *input)
1385 {
1386     declareABCDE
1387     unsigned int i;
1388
1389     xorLanesIntoState(16, state, input)
1390     copyFromState(A, state)
1391     rounds
1392 }
1393 #endif
1394
1395 #ifdef ProvideFast1088
1396 void KeccakPermutationOnWordsAfterXoring1088bits(UINT32 *state, const UINT8 *input)
1397 {
1398     declareABCDE
1399     unsigned int i;
1400
1401     xorLanesIntoState(17, state, input)
1402     copyFromState(A, state)
1403     rounds
1404 }
1405 #endif
1406
1407 #ifdef ProvideFast1152
1408 void KeccakPermutationOnWordsAfterXoring1152bits(UINT32 *state, const UINT8 *input)
1409 {
1410     declareABCDE
1411     unsigned int i;
1412
1413     xorLanesIntoState(18, state, input)
1414     copyFromState(A, state)
1415     rounds
1416 }
1417 #endif
1418
1419 #ifdef ProvideFast1344
1420 void KeccakPermutationOnWordsAfterXoring1344bits(UINT32 *state, const UINT8 *input)
1421 {
1422     declareABCDE
1423     unsigned int i;
1424
1425     xorLanesIntoState(21, state, input)
1426     copyFromState(A, state)
1427     rounds
1428 }
1429 #endif
1430
1431 #endif
1432
1433 void KeccakInitialize()
1434 {
1435 #ifdef UseInterleaveTables
1436     buildInterleaveTables();
1437 #endif
1438 }
1439
1440 void KeccakInitializeState(unsigned char *state)
1441 {
1442     memset(state, 0, 200);
1443 #ifdef UseBebigokimisa
1444     ((UINT32*)state)[ 2] = ~(UINT32)0;
1445     ((UINT32*)state)[ 3] = ~(UINT32)0;
1446     ((UINT32*)state)[ 4] = ~(UINT32)0;
1447     ((UINT32*)state)[ 5] = ~(UINT32)0;
1448     ((UINT32*)state)[16] = ~(UINT32)0;
1449     ((UINT32*)state)[17] = ~(UINT32)0;
1450     ((UINT32*)state)[24] = ~(UINT32)0;
1451     ((UINT32*)state)[25] = ~(UINT32)0;
1452     ((UINT32*)state)[34] = ~(UINT32)0;
1453     ((UINT32*)state)[35] = ~(UINT32)0;
1454     ((UINT32*)state)[40] = ~(UINT32)0;
1455     ((UINT32*)state)[41] = ~(UINT32)0;
1456 #endif
1457 }
1458
1459 void KeccakPermutation(unsigned char *state)
1460 {
1461     // We assume the state is always stored as interleaved 32-bit words
1462     KeccakPermutationOnWords((UINT32*)state);
1463 }
1464
1465 #ifdef ProvideFast576
1466 void KeccakAbsorb576bits(unsigned char *state, const unsigned char *data)
1467 {
1468     KeccakPermutationOnWordsAfterXoring576bits((UINT32*)state, data);
1469 }
1470 #endif
1471
1472 #ifdef ProvideFast832
1473 void KeccakAbsorb832bits(unsigned char *state, const unsigned char *data)
1474 {
1475     KeccakPermutationOnWordsAfterXoring832bits((UINT32*)state, data);
1476 }
1477 #endif
1478
1479 #ifdef ProvideFast1024
1480 void KeccakAbsorb1024bits(unsigned char *state, const unsigned char *data)
1481 {
1482     KeccakPermutationOnWordsAfterXoring1024bits((UINT32*)state, data);
1483 }
1484 #endif
1485
1486 #ifdef ProvideFast1088
1487 void KeccakAbsorb1088bits(unsigned char *state, const unsigned char *data)
1488 {
1489     KeccakPermutationOnWordsAfterXoring1088bits((UINT32*)state, data);
1490 }
1491 #endif
1492
1493 #ifdef ProvideFast1152
1494 void KeccakAbsorb1152bits(unsigned char *state, const unsigned char *data)
1495 {
1496     KeccakPermutationOnWordsAfterXoring1152bits((UINT32*)state, data);
1497 }
1498 #endif
1499
1500 #ifdef ProvideFast1344
1501 void KeccakAbsorb1344bits(unsigned char *state, const unsigned char *data)
1502 {
1503     KeccakPermutationOnWordsAfterXoring1344bits((UINT32*)state, data);
1504 }
1505 #endif
1506
1507 void KeccakAbsorb(unsigned char *state, const unsigned char *data, unsigned int laneCount)
1508 {
1509     KeccakPermutationOnWordsAfterXoring((UINT32*)state, data, laneCount);
1510 }
1511
1512 #ifdef ProvideFast1024
1513 void KeccakExtract1024bits(const unsigned char *state, unsigned char *data)
1514 {
1515     extractLanes(16, state, data)
1516 #ifdef UseBebigokimisa
1517     ((UINT32*)data)[ 2] = ~((UINT32*)data)[ 2];
1518     ((UINT32*)data)[ 3] = ~((UINT32*)data)[ 3];
1519     ((UINT32*)data)[ 4] = ~((UINT32*)data)[ 4];
1520     ((UINT32*)data)[ 5] = ~((UINT32*)data)[ 5];
1521     ((UINT32*)data)[16] = ~((UINT32*)data)[16];
1522     ((UINT32*)data)[17] = ~((UINT32*)data)[17];
1523     ((UINT32*)data)[24] = ~((UINT32*)data)[24];
1524     ((UINT32*)data)[25] = ~((UINT32*)data)[25];
1525 #endif
1526 }
1527 #endif
1528
1529 void KeccakExtract(const unsigned char *state, unsigned char *data, unsigned int laneCount)
1530 {
1531     extractLanes((int)laneCount, state, data)
1532 #ifdef UseBebigokimisa
1533     if (laneCount > 1) {
1534         ((UINT32*)data)[ 2] = ~((UINT32*)data)[ 2];
1535         ((UINT32*)data)[ 3] = ~((UINT32*)data)[ 3];
1536         if (laneCount > 2) {
1537             ((UINT32*)data)[ 4] = ~((UINT32*)data)[ 4];
1538             ((UINT32*)data)[ 5] = ~((UINT32*)data)[ 5];
1539             if (laneCount > 8) {
1540                 ((UINT32*)data)[16] = ~((UINT32*)data)[16];
1541                 ((UINT32*)data)[17] = ~((UINT32*)data)[17];
1542                 if (laneCount > 12) {
1543                     ((UINT32*)data)[24] = ~((UINT32*)data)[24];
1544                     ((UINT32*)data)[25] = ~((UINT32*)data)[25];
1545                     if (laneCount > 17) {
1546                         ((UINT32*)data)[34] = ~((UINT32*)data)[34];
1547                         ((UINT32*)data)[35] = ~((UINT32*)data)[35];
1548                         if (laneCount > 20) {
1549                             ((UINT32*)data)[40] = ~((UINT32*)data)[40];
1550                             ((UINT32*)data)[41] = ~((UINT32*)data)[41];
1551                         }
1552                     }
1553                 }
1554             }
1555         }
1556     }
1557 #endif
1558 }
1559
1560
1561 // ================================================================================
1562 // =================== KeccakSponge.c
1563 // ================================================================================
1564
1565
1566 /*
1567 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
1568 Michaël Peeters and Gilles Van Assche. For more information, feedback or
1569 questions, please refer to our website: http://keccak.noekeon.org/
1570
1571 Implementation by the designers,
1572 hereby denoted as "the implementer".
1573
1574 To the extent possible under law, the implementer has waived all copyright
1575 and related or neighboring rights to the source code in this file.
1576 http://creativecommons.org/publicdomain/zero/1.0/
1577 */
1578
1579 #include <string.h>
1580 //#include "KeccakSponge.h"
1581 //#include "KeccakF-1600-interface.h"
1582 #ifdef KeccakReference
1583 //#include "displayIntermediateValues.h"
1584 #endif
1585
1586 int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity)
1587 {
1588     if (rate+capacity != 1600)
1589         return 1;
1590     if ((rate <= 0) || (rate >= 1600) || ((rate % 64) != 0))
1591         return 1;
1592     KeccakInitialize();
1593     state->rate = rate;
1594     state->capacity = capacity;
1595     state->fixedOutputLength = 0;
1596     KeccakInitializeState(state->state);
1597     memset(state->dataQueue, 0, KeccakMaximumRateInBytes);
1598     state->bitsInQueue = 0;
1599     state->squeezing = 0;
1600     state->bitsAvailableForSqueezing = 0;
1601
1602     return 0;
1603 }
1604
1605 void AbsorbQueue(spongeState *state)
1606 {
1607     // state->bitsInQueue is assumed to be equal to state->rate
1608     #ifdef KeccakReference
1609     displayBytes(1, "Block to be absorbed", state->dataQueue, state->rate/8);
1610     #endif
1611 #ifdef ProvideFast576
1612     if (state->rate == 576)
1613         KeccakAbsorb576bits(state->state, state->dataQueue);
1614     else 
1615 #endif
1616 #ifdef ProvideFast832
1617     if (state->rate == 832)
1618         KeccakAbsorb832bits(state->state, state->dataQueue);
1619     else 
1620 #endif
1621 #ifdef ProvideFast1024
1622     if (state->rate == 1024)
1623         KeccakAbsorb1024bits(state->state, state->dataQueue);
1624     else 
1625 #endif
1626 #ifdef ProvideFast1088
1627     if (state->rate == 1088)
1628         KeccakAbsorb1088bits(state->state, state->dataQueue);
1629     else
1630 #endif
1631 #ifdef ProvideFast1152
1632     if (state->rate == 1152)
1633         KeccakAbsorb1152bits(state->state, state->dataQueue);
1634     else 
1635 #endif
1636 #ifdef ProvideFast1344
1637     if (state->rate == 1344)
1638         KeccakAbsorb1344bits(state->state, state->dataQueue);
1639     else 
1640 #endif
1641         KeccakAbsorb(state->state, state->dataQueue, state->rate/64);
1642     state->bitsInQueue = 0;
1643 }
1644
1645 int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen)
1646 {
1647     unsigned long long i, j, wholeBlocks;
1648     unsigned int partialBlock, partialByte;
1649     const unsigned char *curData;
1650
1651     if ((state->bitsInQueue % 8) != 0)
1652         return 1; // Only the last call may contain a partial byte
1653     if (state->squeezing)
1654         return 1; // Too late for additional input
1655
1656     i = 0;
1657     while(i < databitlen) {
1658         if ((state->bitsInQueue == 0) && (databitlen >= state->rate) && (i <= (databitlen-state->rate))) {
1659             wholeBlocks = (databitlen-i)/state->rate;
1660             curData = data+i/8;
1661 #ifdef ProvideFast576
1662             if (state->rate == 576) {
1663                 for(j=0; j<wholeBlocks; j++, curData+=576/8) {
1664                     #ifdef KeccakReference
1665                     displayBytes(1, "Block to be absorbed", curData, state->rate/8);
1666                     #endif
1667                     KeccakAbsorb576bits(state->state, curData);
1668                 }
1669             }
1670             else
1671 #endif
1672 #ifdef ProvideFast832
1673             if (state->rate == 832) {
1674                 for(j=0; j<wholeBlocks; j++, curData+=832/8) {
1675                     #ifdef KeccakReference
1676                     displayBytes(1, "Block to be absorbed", curData, state->rate/8);
1677                     #endif
1678                     KeccakAbsorb832bits(state->state, curData);
1679                 }
1680             }
1681             else
1682 #endif
1683 #ifdef ProvideFast1024
1684             if (state->rate == 1024) {
1685                 for(j=0; j<wholeBlocks; j++, curData+=1024/8) {
1686                     #ifdef KeccakReference
1687                     displayBytes(1, "Block to be absorbed", curData, state->rate/8);
1688                     #endif
1689                     KeccakAbsorb1024bits(state->state, curData);
1690                 }
1691             }
1692             else
1693 #endif
1694 #ifdef ProvideFast1088
1695             if (state->rate == 1088) {
1696                 for(j=0; j<wholeBlocks; j++, curData+=1088/8) {
1697                     #ifdef KeccakReference
1698                     displayBytes(1, "Block to be absorbed", curData, state->rate/8);
1699                     #endif
1700                     KeccakAbsorb1088bits(state->state, curData);
1701                 }
1702             }
1703             else
1704 #endif
1705 #ifdef ProvideFast1152
1706             if (state->rate == 1152) {
1707                 for(j=0; j<wholeBlocks; j++, curData+=1152/8) {
1708                     #ifdef KeccakReference
1709                     displayBytes(1, "Block to be absorbed", curData, state->rate/8);
1710                     #endif
1711                     KeccakAbsorb1152bits(state->state, curData);
1712                 }
1713             }
1714             else
1715 #endif
1716 #ifdef ProvideFast1344
1717             if (state->rate == 1344) {
1718                 for(j=0; j<wholeBlocks; j++, curData+=1344/8) {
1719                     #ifdef KeccakReference
1720                     displayBytes(1, "Block to be absorbed", curData, state->rate/8);
1721                     #endif
1722                     KeccakAbsorb1344bits(state->state, curData);
1723                 }
1724             }
1725             else
1726 #endif
1727             {
1728                 for(j=0; j<wholeBlocks; j++, curData+=state->rate/8) {
1729                     #ifdef KeccakReference
1730                     displayBytes(1, "Block to be absorbed", curData, state->rate/8);
1731                     #endif
1732                     KeccakAbsorb(state->state, curData, state->rate/64);
1733                 }
1734             }
1735             i += wholeBlocks*state->rate;
1736         }
1737         else {
1738             partialBlock = (unsigned int)(databitlen - i);
1739             if (partialBlock+state->bitsInQueue > state->rate)
1740                 partialBlock = state->rate-state->bitsInQueue;
1741             partialByte = partialBlock % 8;
1742             partialBlock -= partialByte;
1743             memcpy(state->dataQueue+state->bitsInQueue/8, data+i/8, partialBlock/8);
1744             state->bitsInQueue += partialBlock;
1745             i += partialBlock;
1746             if (state->bitsInQueue == state->rate)
1747                 AbsorbQueue(state);
1748             if (partialByte > 0) {
1749                 unsigned char mask = (1 << partialByte)-1;
1750                 state->dataQueue[state->bitsInQueue/8] = data[i/8] & mask;
1751                 state->bitsInQueue += partialByte;
1752                 i += partialByte;
1753             }
1754         }
1755     }
1756     return 0;
1757 }
1758
1759 void PadAndSwitchToSqueezingPhase(spongeState *state)
1760 {
1761     // Note: the bits are numbered from 0=LSB to 7=MSB
1762     if (state->bitsInQueue + 1 == state->rate) {
1763         state->dataQueue[state->bitsInQueue/8 ] |= 1 << (state->bitsInQueue % 8);
1764         AbsorbQueue(state);
1765         memset(state->dataQueue, 0, state->rate/8);
1766     }
1767     else {
1768         memset(state->dataQueue + (state->bitsInQueue+7)/8, 0, state->rate/8 - (state->bitsInQueue+7)/8);
1769         state->dataQueue[state->bitsInQueue/8 ] |= 1 << (state->bitsInQueue % 8);
1770     }
1771     state->dataQueue[(state->rate-1)/8] |= 1 << ((state->rate-1) % 8);
1772     AbsorbQueue(state);
1773
1774     #ifdef KeccakReference
1775     displayText(1, "--- Switching to squeezing phase ---");
1776     #endif
1777 #ifdef ProvideFast1024
1778     if (state->rate == 1024) {
1779         KeccakExtract1024bits(state->state, state->dataQueue);
1780         state->bitsAvailableForSqueezing = 1024;
1781     }
1782     else
1783 #endif
1784     {
1785         KeccakExtract(state->state, state->dataQueue, state->rate/64);
1786         state->bitsAvailableForSqueezing = state->rate;
1787     }
1788     #ifdef KeccakReference
1789     displayBytes(1, "Block available for squeezing", state->dataQueue, state->bitsAvailableForSqueezing/8);
1790     #endif
1791     state->squeezing = 1;
1792 }
1793
1794 int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength)
1795 {
1796     unsigned long long i;
1797     unsigned int partialBlock;
1798
1799     if (!state->squeezing)
1800         PadAndSwitchToSqueezingPhase(state);
1801     if ((outputLength % 8) != 0)
1802         return 1; // Only multiple of 8 bits are allowed, truncation can be done at user level
1803
1804     i = 0;
1805     while(i < outputLength) {
1806         if (state->bitsAvailableForSqueezing == 0) {
1807             KeccakPermutation(state->state);
1808 #ifdef ProvideFast1024
1809             if (state->rate == 1024) {
1810                 KeccakExtract1024bits(state->state, state->dataQueue);
1811                 state->bitsAvailableForSqueezing = 1024;
1812             }
1813             else
1814 #endif
1815             {
1816                 KeccakExtract(state->state, state->dataQueue, state->rate/64);
1817                 state->bitsAvailableForSqueezing = state->rate;
1818             }
1819             #ifdef KeccakReference
1820             displayBytes(1, "Block available for squeezing", state->dataQueue, state->bitsAvailableForSqueezing/8);
1821             #endif
1822         }
1823         partialBlock = state->bitsAvailableForSqueezing;
1824         if ((unsigned long long)partialBlock > outputLength - i)
1825             partialBlock = (unsigned int)(outputLength - i);
1826         memcpy(output+i/8, state->dataQueue+(state->rate-state->bitsAvailableForSqueezing)/8, partialBlock/8);
1827         state->bitsAvailableForSqueezing -= partialBlock;
1828         i += partialBlock;
1829     }
1830     return 0;
1831 }
1832
1833
1834
1835 // ================================================================================
1836 // =================== KeccakNISTInterface.h
1837 // ================================================================================
1838
1839
1840 /*
1841 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
1842 Michaël Peeters and Gilles Van Assche. For more information, feedback or
1843 questions, please refer to our website: http://keccak.noekeon.org/
1844
1845 Implementation by the designers,
1846 hereby denoted as "the implementer".
1847
1848 To the extent possible under law, the implementer has waived all copyright
1849 and related or neighboring rights to the source code in this file.
1850 http://creativecommons.org/publicdomain/zero/1.0/
1851 */
1852
1853 #ifndef _KeccakNISTInterface_h_
1854 #define _KeccakNISTInterface_h_
1855
1856 //#include "KeccakSponge.h"
1857
1858 typedef unsigned char BitSequence;
1859 typedef unsigned long long DataLength;
1860 typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn;
1861
1862 typedef spongeState hashState;
1863
1864 /**
1865   * Function to initialize the state of the Keccak[r, c] sponge function.
1866   * The rate r and capacity c values are determined from @a hashbitlen.
1867   * @param  state       Pointer to the state of the sponge function to be initialized.
1868   * @param  hashbitlen  The desired number of output bits, 
1869   *                     or 0 for Keccak[] with default parameters
1870   *                     and arbitrarily-long output.
1871   * @pre    The value of hashbitlen must be one of 0, 224, 256, 384 and 512.
1872   * @return SUCCESS if successful, BAD_HASHLEN if the value of hashbitlen is incorrect.
1873   */
1874 HashReturn Init(hashState *state, int hashbitlen);
1875 /**
1876   * Function to give input data for the sponge function to absorb.
1877   * @param  state       Pointer to the state of the sponge function initialized by Init().
1878   * @param  data        Pointer to the input data. 
1879   *                     When @a databitLen is not a multiple of 8, the last bits of data must be
1880   *                     in the most significant bits of the last byte.
1881   * @param  databitLen  The number of input bits provided in the input data.
1882   * @pre    In the previous call to Absorb(), databitLen was a multiple of 8.
1883   * @return SUCCESS if successful, FAIL otherwise.
1884   */
1885 HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen);
1886 /**
1887   * Function to squeeze output data from the sponge function.
1888   * If @a hashbitlen was not 0 in the call to Init(), the number of output bits is equal to @a hashbitlen.
1889   * If @a hashbitlen was 0 in the call to Init(), the output bits must be extracted using the Squeeze() function.
1890   * @param  state       Pointer to the state of the sponge function initialized by Init().
1891   * @param  hashval     Pointer to the buffer where to store the output data.
1892   * @return SUCCESS if successful, FAIL otherwise.
1893   */
1894 HashReturn Final(hashState *state, BitSequence *hashval);
1895 /**
1896   * Function to compute a hash using the Keccak[r, c] sponge function.
1897   * The rate r and capacity c values are determined from @a hashbitlen.
1898   * @param  hashbitlen  The desired number of output bits.
1899   * @param  data        Pointer to the input data. 
1900   *                     When @a databitLen is not a multiple of 8, the last bits of data must be
1901   *                     in the most significant bits of the last byte.
1902   * @param  databitLen  The number of input bits provided in the input data.
1903   * @param  hashval     Pointer to the buffer where to store the output data.
1904   * @pre    The value of hashbitlen must be one of 224, 256, 384 and 512.
1905   * @return SUCCESS if successful, BAD_HASHLEN if the value of hashbitlen is incorrect.
1906   */
1907 HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval);
1908
1909 #endif
1910
1911
1912 // ================================================================================
1913 // =================== KeccakNISTInterface.c
1914 // ================================================================================
1915
1916
1917
1918 /*
1919 The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
1920 Michaël Peeters and Gilles Van Assche. For more information, feedback or
1921 questions, please refer to our website: http://keccak.noekeon.org/
1922
1923 Implementation by the designers,
1924 hereby denoted as "the implementer".
1925
1926 To the extent possible under law, the implementer has waived all copyright
1927 and related or neighboring rights to the source code in this file.
1928 http://creativecommons.org/publicdomain/zero/1.0/
1929 */
1930
1931 #include <string.h>
1932 //#include "KeccakNISTInterface.h"
1933 //#include "KeccakF-1600-interface.h"
1934
1935 HashReturn Init(hashState *state, int hashbitlen)
1936 {
1937     switch(hashbitlen) {
1938         case 0: // Default parameters, arbitrary length output
1939             InitSponge((spongeState*)state, 1024, 576);
1940             break;
1941         case 224:
1942             InitSponge((spongeState*)state, 1152, 448);
1943             break;
1944         case 256:
1945             InitSponge((spongeState*)state, 1088, 512);
1946             break;
1947         case 384:
1948             InitSponge((spongeState*)state, 832, 768);
1949             break;
1950         case 512:
1951             InitSponge((spongeState*)state, 576, 1024);
1952             break;
1953         default:
1954             return BAD_HASHLEN;
1955     }
1956     state->fixedOutputLength = hashbitlen;
1957     return SUCCESS;
1958 }
1959
1960 HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen)
1961 {
1962     if ((databitlen % 8) == 0)
1963         return (HashReturn)Absorb((spongeState*)state, data, databitlen);
1964     else {
1965         HashReturn ret = (HashReturn)Absorb((spongeState*)state, data, databitlen - (databitlen % 8));
1966         if (ret == SUCCESS) {
1967             unsigned char lastByte; 
1968             // Align the last partial byte to the least significant bits
1969             lastByte = data[databitlen/8] >> (8 - (databitlen % 8));
1970             return (HashReturn)Absorb((spongeState*)state, &lastByte, databitlen % 8);
1971         }
1972         else
1973             return ret;
1974     }
1975 }
1976
1977 HashReturn Final(hashState *state, BitSequence *hashval)
1978 {
1979     return (HashReturn)Squeeze(state, hashval, state->fixedOutputLength);
1980 }
1981
1982 HashReturn Hash(int hashbitlen, const BitSequence *data, DataLength databitlen, BitSequence *hashval)
1983 {
1984     hashState state;
1985     HashReturn result;
1986
1987     if ((hashbitlen != 224) && (hashbitlen != 256) && (hashbitlen != 384) && (hashbitlen != 512))
1988         return BAD_HASHLEN; // Only the four fixed output lengths available through this API
1989     result = Init(&state, hashbitlen);
1990     if (result != SUCCESS)
1991         return result;
1992     result = Update(&state, data, databitlen);
1993     if (result != SUCCESS)
1994         return result;
1995     result = Final(&state, hashval);
1996     return result;
1997 }
1998
1999
2000 } // end of namespace KeccakImpl
2001 } // end of namespace Internal
2002 } // end of namespace MUtils
2003