PandA-2024.02
Keccak.c
Go to the documentation of this file.
1 /*
2  * The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
3  * MichaĆ«l Peeters and Gilles Van Assche. For more information, feedback or
4  * questions, please refer to our website: http://keccak.noekeon.org/
5  * Implementation by the designers,
6  * hereby denoted as "the implementer".
7  * To the extent possible under law, the implementer has waived all copyright
8  * and related or neighboring rights to the source code in this file.
9  * http://creativecommons.org/publicdomain/zero/1.0/
10  *
11  */
12 typedef unsigned char UINT8;
13 typedef unsigned long long int UINT64;
14 #define nrRounds 24
15 
16 #define GET_KRC_VAL(index) (KeccakRoundConstants[index])
17 
19  0x0000000000000001ULL,
20  0x0000000000008082ULL,
21  0x800000000000808aULL,
22  0x8000000080008000ULL,
23  0x000000000000808bULL,
24  0x0000000080000001ULL,
25  0x8000000080008081ULL,
26  0x8000000000008009ULL,
27  0x000000000000008aULL,
28  0x0000000000000088ULL,
29  0x0000000080008009ULL,
30  0x000000008000000aULL,
31  0x000000008000808bULL,
32  0x800000000000008bULL,
33  0x8000000000008089ULL,
34  0x8000000000008003ULL,
35  0x8000000000008002ULL,
36  0x8000000000000080ULL,
37  0x000000000000800aULL,
38  0x800000008000000aULL,
39  0x8000000080008081ULL,
40  0x8000000000008080ULL,
41  0x0000000080000001ULL,
42  0x8000000080008008ULL
43 };
44 
45 #define nrLanes 25
46 static unsigned char KeccakRhoOffsets[nrLanes] = {
47  0,
48  1,
49  62,
50  28,
51  27,
52  36,
53  44,
54  6,
55  55,
56  20,
57  3,
58  10,
59  43,
60  25,
61  39,
62  41,
63  45,
64  15,
65  21,
66  8,
67  18,
68  2,
69  61,
70  56,
71  14
72 };
73 
74 #define index(x, y) (((x)%5)+5*((y)%5))
75 #define ROL64(a, offset) ((offset != 0) ? ((((UINT64)a) << offset) ^ (((UINT64)a) >> (64-offset))) : a)
76 
77 void theta(UINT64 *A)
78 {
79  unsigned int x, y;
80  UINT64 C[5], D[5];
81 
82  for(x=0; x<5; x++) {
83  C[x] = 0;
84  for(y=0; y<5; y++)
85  C[x] ^= A[index(x, y)];
86  }
87  for(x=0; x<5; x++)
88  D[x] = ROL64(C[(x+1)%5], 1) ^ C[(x+4)%5];
89  for(x=0; x<5; x++)
90  for(y=0; y<5; y++)
91  A[index(x, y)] ^= D[x];
92 }
93 
94 void rho(UINT64 *A)
95 {
96  unsigned int x, y;
97 
98  for(x=0; x<5; x++) for(y=0; y<5; y++)
99  A[index(x, y)] = ROL64(A[index(x, y)], KeccakRhoOffsets[index(x, y)]);
100 }
101 
102 void pi(UINT64 *A)
103 {
104  unsigned int x, y;
105  UINT64 tempA[25];
106 
107  for(x=0; x<5; x++) for(y=0; y<5; y++)
108  tempA[index(x, y)] = A[index(x, y)];
109  for(x=0; x<5; x++) for(y=0; y<5; y++)
110  A[index(0*x+1*y, 2*x+3*y)] = tempA[index(x, y)];
111 }
112 
113 void chi(UINT64 *A)
114 {
115  unsigned int x, y;
116  UINT64 C[5];
117 
118  for(y=0; y<5; y++) {
119  for(x=0; x<5; x++)
120  C[x] = A[index(x, y)] ^ ((~A[index(x+1, y)]) & A[index(x+2, y)]);
121  for(x=0; x<5; x++)
122  A[index(x, y)] = C[x];
123  }
124 }
125 
126 void iota(UINT64 *A, unsigned int indexRound)
127 {
128  A[index(0, 0)] ^= GET_KRC_VAL(indexRound);
129 }
130 
131 
133 {
134  unsigned int i;
135  for(i=0;i<nrRounds;i++) {
136  theta(A);
137  rho(A);
138  pi(A);
139  chi(A);
140  iota(A,i);
141  }
142 }
void pi(UINT64 *A)
Definition: Keccak.c:102
static UINT64 KeccakRoundConstants[nrRounds]
Definition: Keccak.c:18
void kekka_coproc(UINT64 A[25])
Definition: Keccak.c:132
#define C
Definition: generate.c:15
void rho(UINT64 *A)
Definition: Keccak.c:94
#define ROL64(a, offset)
Definition: Keccak.c:75
#define A
Definition: generate.c:13
unsigned char UINT8
Definition: Keccak.c:12
#define index(x, y)
Definition: Keccak.c:74
#define D
Definition: generate.c:16
unsigned long long int UINT64
Definition: Keccak.c:13
#define nrRounds
Definition: Keccak.c:14
void theta(UINT64 *A)
Definition: Keccak.c:77
#define nrLanes
Definition: Keccak.c:45
void iota(UINT64 *A, unsigned int indexRound)
Definition: Keccak.c:126
static unsigned char KeccakRhoOffsets[nrLanes]
Definition: Keccak.c:46
x
Return the smallest n such that 2^n >= _x.
#define GET_KRC_VAL(index)
Definition: Keccak.c:16
void chi(UINT64 *A)
Definition: Keccak.c:113

Generated on Mon Feb 12 2024 13:02:47 for PandA-2024.02 by doxygen 1.8.13