| 1 |
/* |
| 2 |
Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni, |
| 3 |
Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby |
| 4 |
denoted as "the implementer". |
| 5 |
|
| 6 |
For more information, feedback or questions, please refer to our websites: |
| 7 |
http://keccak.noekeon.org/ |
| 8 |
http://keyak.noekeon.org/ |
| 9 |
http://ketje.noekeon.org/ |
| 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 |
#define JOIN0(a, b) a ## b |
| 17 |
#define JOIN(a, b) JOIN0(a, b) |
| 18 |
|
| 19 |
#define Sponge JOIN(prefix, _Sponge) |
| 20 |
#define SpongeInstance JOIN(prefix, _SpongeInstance) |
| 21 |
#define SpongeInitialize JOIN(prefix, _SpongeInitialize) |
| 22 |
#define SpongeAbsorb JOIN(prefix, _SpongeAbsorb) |
| 23 |
#define SpongeAbsorbLastFewBits JOIN(prefix, _SpongeAbsorbLastFewBits) |
| 24 |
#define SpongeSqueeze JOIN(prefix, _SpongeSqueeze) |
| 25 |
|
| 26 |
#define SnP_stateSizeInBytes JOIN(SnP, _stateSizeInBytes) |
| 27 |
#define SnP_stateAlignment JOIN(SnP, _stateAlignment) |
| 28 |
#define SnP_StaticInitialize JOIN(SnP, _StaticInitialize) |
| 29 |
#define SnP_Initialize JOIN(SnP, _Initialize) |
| 30 |
#define SnP_AddByte JOIN(SnP, _AddByte) |
| 31 |
#define SnP_AddBytes JOIN(SnP, _AddBytes) |
| 32 |
#define SnP_ExtractBytes JOIN(SnP, _ExtractBytes) |
| 33 |
|
| 34 |
int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input, size_t inputByteLen, unsigned char suffix, unsigned char *output, size_t outputByteLen) |
| 35 |
{ |
| 36 |
ALIGN(SnP_stateAlignment) unsigned char state[SnP_stateSizeInBytes]; |
| 37 |
unsigned int partialBlock; |
| 38 |
const unsigned char *curInput = input; |
| 39 |
unsigned char *curOutput = output; |
| 40 |
unsigned int rateInBytes = rate/8; |
| 41 |
|
| 42 |
if (rate+capacity != SnP_width) |
| 43 |
return 1; |
| 44 |
if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0)) |
| 45 |
return 1; |
| 46 |
if (suffix == 0) |
| 47 |
return 1; |
| 48 |
|
| 49 |
/* Initialize the state */ |
| 50 |
|
| 51 |
SnP_StaticInitialize(); |
| 52 |
SnP_Initialize(state); |
| 53 |
|
| 54 |
/* First, absorb whole blocks */ |
| 55 |
|
| 56 |
#ifdef SnP_FastLoop_Absorb |
| 57 |
if (((rateInBytes % (SnP_width/200)) == 0) && (inputByteLen >= rateInBytes)) { |
| 58 |
/* fast lane: whole lane rate */ |
| 59 |
|
| 60 |
size_t j; |
| 61 |
j = SnP_FastLoop_Absorb(state, rateInBytes/(SnP_width/200), curInput, inputByteLen); |
| 62 |
curInput += j; |
| 63 |
inputByteLen -= j; |
| 64 |
} |
| 65 |
#endif |
| 66 |
while(inputByteLen >= (size_t)rateInBytes) { |
| 67 |
#ifdef KeccakReference |
| 68 |
displayBytes(1, "Block to be absorbed", curInput, rateInBytes); |
| 69 |
#endif |
| 70 |
SnP_AddBytes(state, curInput, 0, rateInBytes); |
| 71 |
SnP_Permute(state); |
| 72 |
curInput += rateInBytes; |
| 73 |
inputByteLen -= rateInBytes; |
| 74 |
} |
| 75 |
|
| 76 |
/* Then, absorb what remains */ |
| 77 |
|
| 78 |
partialBlock = (unsigned int)inputByteLen; |
| 79 |
#ifdef KeccakReference |
| 80 |
displayBytes(1, "Block to be absorbed (part)", curInput, partialBlock); |
| 81 |
#endif |
| 82 |
SnP_AddBytes(state, curInput, 0, partialBlock); |
| 83 |
|
| 84 |
/* Finally, absorb the suffix */ |
| 85 |
|
| 86 |
#ifdef KeccakReference |
| 87 |
{ |
| 88 |
unsigned char delimitedData1[1]; |
| 89 |
delimitedData1[0] = suffix; |
| 90 |
displayBytes(1, "Block to be absorbed (last few bits + first bit of padding)", delimitedData1, 1); |
| 91 |
} |
| 92 |
#endif |
| 93 |
/* Last few bits, whose delimiter coincides with first bit of padding */ |
| 94 |
|
| 95 |
SnP_AddByte(state, suffix, partialBlock); |
| 96 |
/* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */ |
| 97 |
|
| 98 |
if ((suffix >= 0x80) && (partialBlock == (rateInBytes-1))) |
| 99 |
SnP_Permute(state); |
| 100 |
/* Second bit of padding */ |
| 101 |
|
| 102 |
SnP_AddByte(state, 0x80, rateInBytes-1); |
| 103 |
#ifdef KeccakReference |
| 104 |
{ |
| 105 |
unsigned char block[SnP_width/8]; |
| 106 |
memset(block, 0, SnP_width/8); |
| 107 |
block[rateInBytes-1] = 0x80; |
| 108 |
displayBytes(1, "Second bit of padding", block, rateInBytes); |
| 109 |
} |
| 110 |
#endif |
| 111 |
SnP_Permute(state); |
| 112 |
#ifdef KeccakReference |
| 113 |
displayText(1, "--- Switching to squeezing phase ---"); |
| 114 |
#endif |
| 115 |
|
| 116 |
/* First, output whole blocks */ |
| 117 |
|
| 118 |
while(outputByteLen > (size_t)rateInBytes) { |
| 119 |
SnP_ExtractBytes(state, curOutput, 0, rateInBytes); |
| 120 |
SnP_Permute(state); |
| 121 |
#ifdef KeccakReference |
| 122 |
displayBytes(1, "Squeezed block", curOutput, rateInBytes); |
| 123 |
#endif |
| 124 |
curOutput += rateInBytes; |
| 125 |
outputByteLen -= rateInBytes; |
| 126 |
} |
| 127 |
|
| 128 |
/* Finally, output what remains */ |
| 129 |
|
| 130 |
partialBlock = (unsigned int)outputByteLen; |
| 131 |
SnP_ExtractBytes(state, curOutput, 0, partialBlock); |
| 132 |
#ifdef KeccakReference |
| 133 |
displayBytes(1, "Squeezed block (part)", curOutput, partialBlock); |
| 134 |
#endif |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
/* ---------------------------------------------------------------- */ |
| 140 |
/* ---------------------------------------------------------------- */ |
| 141 |
/* ---------------------------------------------------------------- */ |
| 142 |
|
| 143 |
int SpongeInitialize(SpongeInstance *instance, unsigned int rate, unsigned int capacity) |
| 144 |
{ |
| 145 |
if (rate+capacity != SnP_width) |
| 146 |
return 1; |
| 147 |
if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0)) |
| 148 |
return 1; |
| 149 |
SnP_StaticInitialize(); |
| 150 |
SnP_Initialize(instance->state); |
| 151 |
instance->rate = rate; |
| 152 |
instance->byteIOIndex = 0; |
| 153 |
instance->squeezing = 0; |
| 154 |
|
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
/* ---------------------------------------------------------------- */ |
| 159 |
|
| 160 |
int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dataByteLen) |
| 161 |
{ |
| 162 |
size_t i, j; |
| 163 |
unsigned int partialBlock; |
| 164 |
const unsigned char *curData; |
| 165 |
unsigned int rateInBytes = instance->rate/8; |
| 166 |
|
| 167 |
if (instance->squeezing) |
| 168 |
return 1; /* Too late for additional input */ |
| 169 |
|
| 170 |
|
| 171 |
i = 0; |
| 172 |
curData = data; |
| 173 |
while(i < dataByteLen) { |
| 174 |
if ((instance->byteIOIndex == 0) && (dataByteLen-i >= rateInBytes)) { |
| 175 |
#ifdef SnP_FastLoop_Absorb |
| 176 |
/* processing full blocks first */ |
| 177 |
|
| 178 |
if ((rateInBytes % (SnP_width/200)) == 0) { |
| 179 |
/* fast lane: whole lane rate */ |
| 180 |
|
| 181 |
j = SnP_FastLoop_Absorb(instance->state, rateInBytes/(SnP_width/200), curData, dataByteLen - i); |
| 182 |
i += j; |
| 183 |
curData += j; |
| 184 |
} |
| 185 |
else { |
| 186 |
#endif |
| 187 |
for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) { |
| 188 |
#ifdef KeccakReference |
| 189 |
displayBytes(1, "Block to be absorbed", curData, rateInBytes); |
| 190 |
#endif |
| 191 |
SnP_AddBytes(instance->state, curData, 0, rateInBytes); |
| 192 |
SnP_Permute(instance->state); |
| 193 |
curData+=rateInBytes; |
| 194 |
} |
| 195 |
i = dataByteLen - j; |
| 196 |
#ifdef SnP_FastLoop_Absorb |
| 197 |
} |
| 198 |
#endif |
| 199 |
} |
| 200 |
else { |
| 201 |
/* normal lane: using the message queue */ |
| 202 |
|
| 203 |
if (dataByteLen-i > rateInBytes-instance->byteIOIndex) |
| 204 |
partialBlock = rateInBytes-instance->byteIOIndex; |
| 205 |
else |
| 206 |
partialBlock = (unsigned int)(dataByteLen - i); |
| 207 |
#ifdef KeccakReference |
| 208 |
displayBytes(1, "Block to be absorbed (part)", curData, partialBlock); |
| 209 |
#endif |
| 210 |
i += partialBlock; |
| 211 |
|
| 212 |
SnP_AddBytes(instance->state, curData, instance->byteIOIndex, partialBlock); |
| 213 |
curData += partialBlock; |
| 214 |
instance->byteIOIndex += partialBlock; |
| 215 |
if (instance->byteIOIndex == rateInBytes) { |
| 216 |
SnP_Permute(instance->state); |
| 217 |
instance->byteIOIndex = 0; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
/* ---------------------------------------------------------------- */ |
| 225 |
|
| 226 |
int SpongeAbsorbLastFewBits(SpongeInstance *instance, unsigned char delimitedData) |
| 227 |
{ |
| 228 |
unsigned int rateInBytes = instance->rate/8; |
| 229 |
|
| 230 |
if (delimitedData == 0) |
| 231 |
return 1; |
| 232 |
if (instance->squeezing) |
| 233 |
return 1; /* Too late for additional input */ |
| 234 |
|
| 235 |
|
| 236 |
#ifdef KeccakReference |
| 237 |
{ |
| 238 |
unsigned char delimitedData1[1]; |
| 239 |
delimitedData1[0] = delimitedData; |
| 240 |
displayBytes(1, "Block to be absorbed (last few bits + first bit of padding)", delimitedData1, 1); |
| 241 |
} |
| 242 |
#endif |
| 243 |
/* Last few bits, whose delimiter coincides with first bit of padding */ |
| 244 |
|
| 245 |
SnP_AddByte(instance->state, delimitedData, instance->byteIOIndex); |
| 246 |
/* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */ |
| 247 |
|
| 248 |
if ((delimitedData >= 0x80) && (instance->byteIOIndex == (rateInBytes-1))) |
| 249 |
SnP_Permute(instance->state); |
| 250 |
/* Second bit of padding */ |
| 251 |
|
| 252 |
SnP_AddByte(instance->state, 0x80, rateInBytes-1); |
| 253 |
#ifdef KeccakReference |
| 254 |
{ |
| 255 |
unsigned char block[SnP_width/8]; |
| 256 |
memset(block, 0, SnP_width/8); |
| 257 |
block[rateInBytes-1] = 0x80; |
| 258 |
displayBytes(1, "Second bit of padding", block, rateInBytes); |
| 259 |
} |
| 260 |
#endif |
| 261 |
SnP_Permute(instance->state); |
| 262 |
instance->byteIOIndex = 0; |
| 263 |
instance->squeezing = 1; |
| 264 |
#ifdef KeccakReference |
| 265 |
displayText(1, "--- Switching to squeezing phase ---"); |
| 266 |
#endif |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
/* ---------------------------------------------------------------- */ |
| 271 |
|
| 272 |
int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByteLen) |
| 273 |
{ |
| 274 |
size_t i, j; |
| 275 |
unsigned int partialBlock; |
| 276 |
unsigned int rateInBytes = instance->rate/8; |
| 277 |
unsigned char *curData; |
| 278 |
|
| 279 |
if (!instance->squeezing) |
| 280 |
SpongeAbsorbLastFewBits(instance, 0x01); |
| 281 |
|
| 282 |
i = 0; |
| 283 |
curData = data; |
| 284 |
while(i < dataByteLen) { |
| 285 |
if ((instance->byteIOIndex == rateInBytes) && (dataByteLen-i >= rateInBytes)) { |
| 286 |
for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) { |
| 287 |
SnP_Permute(instance->state); |
| 288 |
SnP_ExtractBytes(instance->state, curData, 0, rateInBytes); |
| 289 |
#ifdef KeccakReference |
| 290 |
displayBytes(1, "Squeezed block", curData, rateInBytes); |
| 291 |
#endif |
| 292 |
curData+=rateInBytes; |
| 293 |
} |
| 294 |
i = dataByteLen - j; |
| 295 |
} |
| 296 |
else { |
| 297 |
/* normal lane: using the message queue */ |
| 298 |
|
| 299 |
if (instance->byteIOIndex == rateInBytes) { |
| 300 |
SnP_Permute(instance->state); |
| 301 |
instance->byteIOIndex = 0; |
| 302 |
} |
| 303 |
if (dataByteLen-i > rateInBytes-instance->byteIOIndex) |
| 304 |
partialBlock = rateInBytes-instance->byteIOIndex; |
| 305 |
else |
| 306 |
partialBlock = (unsigned int)(dataByteLen - i); |
| 307 |
i += partialBlock; |
| 308 |
|
| 309 |
SnP_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock); |
| 310 |
#ifdef KeccakReference |
| 311 |
displayBytes(1, "Squeezed block (part)", curData, partialBlock); |
| 312 |
#endif |
| 313 |
curData += partialBlock; |
| 314 |
instance->byteIOIndex += partialBlock; |
| 315 |
} |
| 316 |
} |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
/* ---------------------------------------------------------------- */ |
| 321 |
|
| 322 |
#undef Sponge |
| 323 |
#undef SpongeInstance |
| 324 |
#undef SpongeInitialize |
| 325 |
#undef SpongeAbsorb |
| 326 |
#undef SpongeAbsorbLastFewBits |
| 327 |
#undef SpongeSqueeze |
| 328 |
#undef SnP_stateSizeInBytes |
| 329 |
#undef SnP_stateAlignment |
| 330 |
#undef SnP_StaticInitialize |
| 331 |
#undef SnP_Initialize |
| 332 |
#undef SnP_AddByte |
| 333 |
#undef SnP_AddBytes |
| 334 |
#undef SnP_ExtractBytes |