-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeccakSpongetimes4.inc
211 lines (186 loc) · 8.07 KB
/
KeccakSpongetimes4.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
denoted as "the implementer".
For more information, feedback or questions, please refer to our websites:
http://keccak.noekeon.org/
http://keyak.noekeon.org/
http://ketje.noekeon.org/
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#define JOIN0(a, b) a ## b
#define JOIN(a, b) JOIN0(a, b)
#define Sponge JOIN(prefix, _Sponge)
#define SpongeInstance JOIN(prefix, _SpongeInstance)
#define SpongeInitialize JOIN(prefix, _SpongeInitialize)
#define SpongeAbsorb JOIN(prefix, _SpongeAbsorb)
#define SpongeAbsorbLastFewBits JOIN(prefix, _SpongeAbsorbLastFewBits)
#define SpongeSqueeze JOIN(prefix, _SpongeSqueeze)
#define PlSnP_statesSizeInBytes JOIN(PlSnP, _statesSizeInBytes)
#define PlSnP_statesAlignment JOIN(PlSnP, _statesAlignment)
#define PlSnP_StaticInitialize JOIN(PlSnP, _StaticInitialize)
#define PlSnP_InitializeAll JOIN(PlSnP, _InitializeAll)
#define PlSnP_AddByte JOIN(PlSnP, _AddByte)
#define PlSnP_AddBytes JOIN(PlSnP, _AddBytes)
#define PlSnP_ExtractBytes JOIN(PlSnP, _ExtractBytes)
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
int SpongeInitialize(SpongeInstance *instance, unsigned int rate, unsigned int capacity)
{
if (rate+capacity != PlSnP_width)
return 1;
if ((rate <= 0) || (rate > PlSnP_width) || ((rate % 8) != 0))
return 1;
PlSnP_StaticInitialize();
PlSnP_InitializeAll(instance->state);
instance->rate = rate;
instance->byteIOIndex = 0;
instance->squeezing = 0;
return 0;
}
/* ---------------------------------------------------------------- */
int SpongeAbsorb(SpongeInstance *instance, const unsigned char **data, size_t dataByteLen)
{
size_t i, j;
unsigned int partialBlock;
const unsigned char *curData[4];
unsigned int rateInBytes = instance->rate/8;
if (instance->squeezing)
return 1; /* Too late for additional input */
i = 0;
if(dataByteLen > 0) {
for (unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
curData[instanceIndex] = data[instanceIndex];
}
}
while(i < dataByteLen) {
if ((instance->byteIOIndex == 0) && (dataByteLen >= (i + rateInBytes))) {
#ifdef PlSnP_FastLoop_Absorb
/* processing full blocks first */
if ((rateInBytes % (PlSnP_width/200)) == 0) {
/* fast lane: whole lane rate */
for(unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
j = PlSnP_FastLoop_Absorb(instance->state, rateInBytes/(PlSnP_width/200), 0, 0, curData[instanceIndex], dataByteLen - i);
curData[instanceIndex] += j;
}
i += j;
}
else {
#endif
for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
for(unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
PlSnP_AddBytes(instance->state, instanceIndex, curData[instanceIndex], 0, rateInBytes);
curData[instanceIndex]+=rateInBytes;
}
PlSnP_Permute(instance->state);
}
i = dataByteLen - j;
#ifdef PlSnP_FastLoop_Absorb
}
#endif
}
else {
/* normal lane: using the message queue */
partialBlock = (unsigned int)(dataByteLen - i);
if (partialBlock+instance->byteIOIndex > rateInBytes)
partialBlock = rateInBytes-instance->byteIOIndex;
i += partialBlock;
for(unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
PlSnP_AddBytes(instance->state, instanceIndex, curData[instanceIndex], instance->byteIOIndex, partialBlock);
curData[instanceIndex] += partialBlock;
}
instance->byteIOIndex += partialBlock;
if (instance->byteIOIndex == rateInBytes) {
PlSnP_Permute(instance->state);
instance->byteIOIndex = 0;
}
}
}
return 0;
}
/* ---------------------------------------------------------------- */
int SpongeAbsorbLastFewBits(SpongeInstance *instance, unsigned char delimitedData)
{
unsigned int rateInBytes = instance->rate/8;
if (delimitedData == 0)
return 1;
if (instance->squeezing)
return 1; /* Too late for additional input */
/* Last few bits, whose delimiter coincides with first bit of padding */
for(unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
PlSnP_AddByte(instance->state, instanceIndex, delimitedData, instance->byteIOIndex);
}
/* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */
if ((delimitedData >= 0x80) && (instance->byteIOIndex == (rateInBytes-1)))
PlSnP_Permute(instance->state);
/* Second bit of padding */
for(unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
PlSnP_AddByte(instance->state, instanceIndex, 0x80, rateInBytes - 1);
}
PlSnP_Permute(instance->state);
instance->byteIOIndex = 0;
instance->squeezing = 1;
return 0;
}
/* ---------------------------------------------------------------- */
int SpongeSqueeze(SpongeInstance *instance, unsigned char **data, size_t dataByteLen)
{
size_t i, j;
unsigned int partialBlock;
unsigned int rateInBytes = instance->rate/8;
unsigned char *curData[4] = { NULL, NULL, NULL, NULL};
if (!instance->squeezing)
SpongeAbsorbLastFewBits(instance, 0x01);
i = 0;
if(dataByteLen > 0) {
for (unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
curData[instanceIndex] = data[instanceIndex];
}
}
while(i < dataByteLen) {
if ((instance->byteIOIndex == rateInBytes) && (dataByteLen >= (i + rateInBytes))) {
for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
PlSnP_Permute(instance->state);
for(unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
PlSnP_ExtractBytes(instance->state, instanceIndex, curData[instanceIndex], 0, rateInBytes);
curData[instanceIndex]+=rateInBytes;
}
}
i = dataByteLen - j;
}
else {
/* normal lane: using the message queue */
if (instance->byteIOIndex == rateInBytes) {
PlSnP_Permute(instance->state);
instance->byteIOIndex = 0;
}
partialBlock = (unsigned int)(dataByteLen - i);
if (partialBlock+instance->byteIOIndex > rateInBytes)
partialBlock = rateInBytes-instance->byteIOIndex;
i += partialBlock;
for(unsigned int instanceIndex = 0; instanceIndex < 4; instanceIndex++) {
PlSnP_ExtractBytes(instance->state, instanceIndex, curData[instanceIndex], instance->byteIOIndex, partialBlock);
curData[instanceIndex] += partialBlock;
}
instance->byteIOIndex += partialBlock;
}
}
return 0;
}
/* ---------------------------------------------------------------- */
#undef Sponge
#undef SpongeInstance
#undef SpongeInitialize
#undef SpongeAbsorb
#undef SpongeAbsorbLastFewBits
#undef SpongeSqueeze
#undef PlSnP_statesSizeInBytes
#undef PlSnP_statesAlignment
#undef PlSnP_StaticInitialize
#undef PlSnP_InitializeAll
#undef PlSnP_AddByte
#undef PlSnP_AddBytes
#undef PlSnP_ExtractBytes