sha3 0.2.6 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sha3 might be problematic. Click here for more details.

Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.travis.yml +9 -1
  4. data/Gemfile.ci +4 -4
  5. data/LICENSE.txt +1 -1
  6. data/README.md +145 -0
  7. data/Rakefile +4 -5
  8. data/ext/sha3/KeccakF-1600-interface.h +28 -34
  9. data/ext/sha3/KeccakHash.c +80 -0
  10. data/ext/sha3/KeccakHash.h +110 -0
  11. data/ext/sha3/KeccakSponge.c +127 -201
  12. data/ext/sha3/KeccakSponge.h +74 -37
  13. data/ext/sha3/Optimized64/KeccakF-1600-64.macros +2199 -0
  14. data/ext/sha3/Optimized64/KeccakF-1600-opt64-settings.h +3 -0
  15. data/ext/sha3/Optimized64/KeccakF-1600-opt64.c +508 -0
  16. data/ext/sha3/{KeccakF-1600-unrolling.macros → Optimized64/KeccakF-1600-unrolling.macros} +16 -14
  17. data/ext/sha3/Optimized64/SnP-interface.h +47 -0
  18. data/ext/sha3/Reference/KeccakF-1600-reference.c +311 -0
  19. data/ext/sha3/Reference/KeccakF-reference.h +26 -0
  20. data/ext/sha3/Reference/SnP-FBWL-default.c +96 -0
  21. data/ext/sha3/Reference/SnP-FBWL-default.h +26 -0
  22. data/ext/sha3/Reference/SnP-interface.h +42 -0
  23. data/ext/sha3/{displayIntermediateValues.c → Reference/displayIntermediateValues.c} +52 -11
  24. data/ext/sha3/{displayIntermediateValues.h → Reference/displayIntermediateValues.h} +11 -6
  25. data/ext/sha3/SnP-Relaned.h +249 -0
  26. data/ext/sha3/brg_endian.h +0 -0
  27. data/ext/sha3/digest.c +67 -70
  28. data/ext/sha3/digest.h +2 -2
  29. data/ext/sha3/extconf.rb +7 -12
  30. data/ext/sha3/sha3.h +2 -2
  31. data/lib/sha3/doc.rb +26 -39
  32. data/lib/sha3/version.rb +2 -2
  33. data/sha3.gemspec +6 -6
  34. data/spec/generate_tests.rb +6 -41
  35. data/spec/sha3_core_spec.rb +111 -133
  36. data/spec/spec_helper.rb +2 -2
  37. data/tests.sh +9 -7
  38. metadata +33 -36
  39. data/README.rdoc +0 -131
  40. data/ext/sha3/KeccakF-1600-32-rvk.macros +0 -555
  41. data/ext/sha3/KeccakF-1600-32-s1.macros +0 -1187
  42. data/ext/sha3/KeccakF-1600-32-s2.macros +0 -1187
  43. data/ext/sha3/KeccakF-1600-32.macros +0 -26
  44. data/ext/sha3/KeccakF-1600-64.macros +0 -728
  45. data/ext/sha3/KeccakF-1600-int-set.h +0 -6
  46. data/ext/sha3/KeccakF-1600-opt.c +0 -504
  47. data/ext/sha3/KeccakF-1600-opt32-settings.h +0 -4
  48. data/ext/sha3/KeccakF-1600-opt32.c-arch +0 -524
  49. data/ext/sha3/KeccakF-1600-opt64-settings.h +0 -7
  50. data/ext/sha3/KeccakF-1600-opt64.c-arch +0 -504
  51. data/ext/sha3/KeccakF-1600-reference.c-arch +0 -300
  52. data/ext/sha3/KeccakF-1600-x86-64-gas.s +0 -766
  53. data/ext/sha3/KeccakF-1600-x86-64-shld-gas.s +0 -766
  54. data/ext/sha3/KeccakNISTInterface.c +0 -81
  55. data/ext/sha3/KeccakNISTInterface.h +0 -70
@@ -1,10 +1,12 @@
1
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/
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
5
 
6
- Implementation by the designers,
7
- hereby denoted as "the implementer".
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/
8
10
 
9
11
  To the extent possible under law, the implementer has waived all copyright
10
12
  and related or neighboring rights to the source code in this file.
@@ -13,254 +15,178 @@ http://creativecommons.org/publicdomain/zero/1.0/
13
15
 
14
16
  #include <string.h>
15
17
  #include "KeccakSponge.h"
16
- #include "KeccakF-1600-interface.h"
18
+ #include "SnP-interface.h"
17
19
  #ifdef KeccakReference
18
20
  #include "displayIntermediateValues.h"
19
21
  #endif
20
22
 
21
- int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity)
23
+ /* ---------------------------------------------------------------- */
24
+
25
+ int Keccak_SpongeInitialize(Keccak_SpongeInstance *instance, unsigned int rate, unsigned int capacity)
22
26
  {
23
- if (rate+capacity != 1600)
27
+ if (rate+capacity != SnP_width)
24
28
  return 1;
25
- if ((rate <= 0) || (rate >= 1600) || ((rate % 64) != 0))
29
+ if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0))
26
30
  return 1;
27
- KeccakInitialize();
28
- state->rate = rate;
29
- state->capacity = capacity;
30
- state->fixedOutputLength = 0;
31
- KeccakInitializeState(state->state);
32
- memset(state->dataQueue, 0, KeccakMaximumRateInBytes);
33
- state->bitsInQueue = 0;
34
- state->squeezing = 0;
35
- state->bitsAvailableForSqueezing = 0;
31
+ SnP_StaticInitialize();
32
+ SnP_Initialize(instance->state);
33
+ instance->rate = rate;
34
+ instance->byteIOIndex = 0;
35
+ instance->squeezing = 0;
36
36
 
37
37
  return 0;
38
38
  }
39
39
 
40
- void AbsorbQueue(spongeState *state)
41
- {
42
- // state->bitsInQueue is assumed to be equal to state->rate
43
- #ifdef KeccakReference
44
- displayBytes(1, "Block to be absorbed", state->dataQueue, state->rate/8);
45
- #endif
46
- #ifdef ProvideFast576
47
- if (state->rate == 576)
48
- KeccakAbsorb576bits(state->state, state->dataQueue);
49
- else
50
- #endif
51
- #ifdef ProvideFast832
52
- if (state->rate == 832)
53
- KeccakAbsorb832bits(state->state, state->dataQueue);
54
- else
55
- #endif
56
- #ifdef ProvideFast1024
57
- if (state->rate == 1024)
58
- KeccakAbsorb1024bits(state->state, state->dataQueue);
59
- else
60
- #endif
61
- #ifdef ProvideFast1088
62
- if (state->rate == 1088)
63
- KeccakAbsorb1088bits(state->state, state->dataQueue);
64
- else
65
- #endif
66
- #ifdef ProvideFast1152
67
- if (state->rate == 1152)
68
- KeccakAbsorb1152bits(state->state, state->dataQueue);
69
- else
70
- #endif
71
- #ifdef ProvideFast1344
72
- if (state->rate == 1344)
73
- KeccakAbsorb1344bits(state->state, state->dataQueue);
74
- else
75
- #endif
76
- KeccakAbsorb(state->state, state->dataQueue, state->rate/64);
77
- state->bitsInQueue = 0;
78
- }
40
+ /* ---------------------------------------------------------------- */
79
41
 
80
- int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen)
42
+ int Keccak_SpongeAbsorb(Keccak_SpongeInstance *instance, const unsigned char *data, size_t dataByteLen)
81
43
  {
82
- unsigned long long i, j, wholeBlocks;
83
- unsigned int partialBlock, partialByte;
44
+ size_t i, j;
45
+ unsigned int partialBlock;
84
46
  const unsigned char *curData;
47
+ unsigned int rateInBytes = instance->rate/8;
85
48
 
86
- if ((state->bitsInQueue % 8) != 0)
87
- return 1; // Only the last call may contain a partial byte
88
- if (state->squeezing)
49
+ if (instance->squeezing)
89
50
  return 1; // Too late for additional input
90
51
 
91
52
  i = 0;
92
- while(i < databitlen) {
93
- if ((state->bitsInQueue == 0) && (databitlen >= state->rate) && (i <= (databitlen-state->rate))) {
94
- wholeBlocks = (databitlen-i)/state->rate;
95
- curData = data+i/8;
96
- #ifdef ProvideFast576
97
- if (state->rate == 576) {
98
- for(j=0; j<wholeBlocks; j++, curData+=576/8) {
99
- #ifdef KeccakReference
100
- displayBytes(1, "Block to be absorbed", curData, state->rate/8);
101
- #endif
102
- KeccakAbsorb576bits(state->state, curData);
103
- }
104
- }
105
- else
106
- #endif
107
- #ifdef ProvideFast832
108
- if (state->rate == 832) {
109
- for(j=0; j<wholeBlocks; j++, curData+=832/8) {
110
- #ifdef KeccakReference
111
- displayBytes(1, "Block to be absorbed", curData, state->rate/8);
112
- #endif
113
- KeccakAbsorb832bits(state->state, curData);
114
- }
53
+ curData = data;
54
+ while(i < dataByteLen) {
55
+ if ((instance->byteIOIndex == 0) && (dataByteLen >= (i + rateInBytes))) {
56
+ // processing full blocks first
57
+ if ((rateInBytes % SnP_laneLengthInBytes) == 0) {
58
+ // fast lane: whole lane rate
59
+ j = SnP_FBWL_Absorb(instance->state, rateInBytes/SnP_laneLengthInBytes, curData, dataByteLen - i, 0);
60
+ i += j;
61
+ curData += j;
115
62
  }
116
- else
117
- #endif
118
- #ifdef ProvideFast1024
119
- if (state->rate == 1024) {
120
- for(j=0; j<wholeBlocks; j++, curData+=1024/8) {
121
- #ifdef KeccakReference
122
- displayBytes(1, "Block to be absorbed", curData, state->rate/8);
123
- #endif
124
- KeccakAbsorb1024bits(state->state, curData);
125
- }
126
- }
127
- else
128
- #endif
129
- #ifdef ProvideFast1088
130
- if (state->rate == 1088) {
131
- for(j=0; j<wholeBlocks; j++, curData+=1088/8) {
63
+ else {
64
+ for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
132
65
  #ifdef KeccakReference
133
- displayBytes(1, "Block to be absorbed", curData, state->rate/8);
66
+ displayBytes(1, "Block to be absorbed", curData, rateInBytes);
134
67
  #endif
135
- KeccakAbsorb1088bits(state->state, curData);
68
+ SnP_XORBytes(instance->state, curData, 0, rateInBytes);
69
+ SnP_Permute(instance->state);
70
+ curData+=rateInBytes;
136
71
  }
72
+ i = dataByteLen - j;
137
73
  }
138
- else
139
- #endif
140
- #ifdef ProvideFast1152
141
- if (state->rate == 1152) {
142
- for(j=0; j<wholeBlocks; j++, curData+=1152/8) {
143
- #ifdef KeccakReference
144
- displayBytes(1, "Block to be absorbed", curData, state->rate/8);
145
- #endif
146
- KeccakAbsorb1152bits(state->state, curData);
147
- }
148
- }
149
- else
150
- #endif
151
- #ifdef ProvideFast1344
152
- if (state->rate == 1344) {
153
- for(j=0; j<wholeBlocks; j++, curData+=1344/8) {
154
- #ifdef KeccakReference
155
- displayBytes(1, "Block to be absorbed", curData, state->rate/8);
156
- #endif
157
- KeccakAbsorb1344bits(state->state, curData);
158
- }
159
- }
160
- else
161
- #endif
162
- {
163
- for(j=0; j<wholeBlocks; j++, curData+=state->rate/8) {
164
- #ifdef KeccakReference
165
- displayBytes(1, "Block to be absorbed", curData, state->rate/8);
166
- #endif
167
- KeccakAbsorb(state->state, curData, state->rate/64);
168
- }
169
- }
170
- i += wholeBlocks*state->rate;
171
74
  }
172
75
  else {
173
- partialBlock = (unsigned int)(databitlen - i);
174
- if (partialBlock+state->bitsInQueue > state->rate)
175
- partialBlock = state->rate-state->bitsInQueue;
176
- partialByte = partialBlock % 8;
177
- partialBlock -= partialByte;
178
- memcpy(state->dataQueue+state->bitsInQueue/8, data+i/8, partialBlock/8);
179
- state->bitsInQueue += partialBlock;
76
+ // normal lane: using the message queue
77
+ partialBlock = (unsigned int)(dataByteLen - i);
78
+ if (partialBlock+instance->byteIOIndex > rateInBytes)
79
+ partialBlock = rateInBytes-instance->byteIOIndex;
80
+ #ifdef KeccakReference
81
+ displayBytes(1, "Block to be absorbed (part)", curData, partialBlock);
82
+ #endif
180
83
  i += partialBlock;
181
- if (state->bitsInQueue == state->rate)
182
- AbsorbQueue(state);
183
- if (partialByte > 0) {
184
- unsigned char mask = (1 << partialByte)-1;
185
- state->dataQueue[state->bitsInQueue/8] = data[i/8] & mask;
186
- state->bitsInQueue += partialByte;
187
- i += partialByte;
84
+
85
+ SnP_XORBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
86
+ curData += partialBlock;
87
+ instance->byteIOIndex += partialBlock;
88
+ if (instance->byteIOIndex == rateInBytes) {
89
+ SnP_Permute(instance->state);
90
+ instance->byteIOIndex = 0;
188
91
  }
189
92
  }
190
93
  }
191
94
  return 0;
192
95
  }
193
96
 
194
- void PadAndSwitchToSqueezingPhase(spongeState *state)
97
+ /* ---------------------------------------------------------------- */
98
+
99
+ int Keccak_SpongeAbsorbLastFewBits(Keccak_SpongeInstance *instance, unsigned char delimitedData)
195
100
  {
196
- // Note: the bits are numbered from 0=LSB to 7=MSB
197
- if (state->bitsInQueue + 1 == state->rate) {
198
- state->dataQueue[state->bitsInQueue/8 ] |= 1 << (state->bitsInQueue % 8);
199
- AbsorbQueue(state);
200
- memset(state->dataQueue, 0, state->rate/8);
201
- }
202
- else {
203
- memset(state->dataQueue + (state->bitsInQueue+7)/8, 0, state->rate/8 - (state->bitsInQueue+7)/8);
204
- state->dataQueue[state->bitsInQueue/8 ] |= 1 << (state->bitsInQueue % 8);
205
- }
206
- state->dataQueue[(state->rate-1)/8] |= 1 << ((state->rate-1) % 8);
207
- AbsorbQueue(state);
101
+ unsigned char delimitedData1[1];
102
+ unsigned int rateInBytes = instance->rate/8;
208
103
 
104
+ if (delimitedData == 0)
105
+ return 1;
106
+ if (instance->squeezing)
107
+ return 1; // Too late for additional input
108
+
109
+ delimitedData1[0] = delimitedData;
209
110
  #ifdef KeccakReference
210
- displayText(1, "--- Switching to squeezing phase ---");
111
+ displayBytes(1, "Block to be absorbed (last few bits + first bit of padding)", delimitedData1, 1);
211
112
  #endif
212
- #ifdef ProvideFast1024
213
- if (state->rate == 1024) {
214
- KeccakExtract1024bits(state->state, state->dataQueue);
215
- state->bitsAvailableForSqueezing = 1024;
216
- }
217
- else
218
- #endif
113
+ // Last few bits, whose delimiter coincides with first bit of padding
114
+ SnP_XORBytes(instance->state, delimitedData1, instance->byteIOIndex, 1);
115
+ // If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding
116
+ if ((delimitedData >= 0x80) && (instance->byteIOIndex == (rateInBytes-1)))
117
+ SnP_Permute(instance->state);
118
+ // Second bit of padding
119
+ SnP_ComplementBit(instance->state, rateInBytes*8-1);
120
+ #ifdef KeccakReference
219
121
  {
220
- KeccakExtract(state->state, state->dataQueue, state->rate/64);
221
- state->bitsAvailableForSqueezing = state->rate;
122
+ unsigned char block[SnP_width/8];
123
+ memset(block, 0, SnP_width/8);
124
+ block[rateInBytes-1] = 0x80;
125
+ displayBytes(1, "Second bit of padding", block, rateInBytes);
222
126
  }
127
+ #endif
128
+ SnP_Permute(instance->state);
129
+ instance->byteIOIndex = 0;
130
+ instance->squeezing = 1;
223
131
  #ifdef KeccakReference
224
- displayBytes(1, "Block available for squeezing", state->dataQueue, state->bitsAvailableForSqueezing/8);
132
+ displayText(1, "--- Switching to squeezing phase ---");
225
133
  #endif
226
- state->squeezing = 1;
134
+ return 0;
227
135
  }
228
136
 
229
- int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength)
137
+ /* ---------------------------------------------------------------- */
138
+
139
+ int Keccak_SpongeSqueeze(Keccak_SpongeInstance *instance, unsigned char *data, size_t dataByteLen)
230
140
  {
231
- unsigned long long i;
141
+ size_t i, j;
232
142
  unsigned int partialBlock;
143
+ unsigned int rateInBytes = instance->rate/8;
144
+ unsigned char *curData;
233
145
 
234
- if (!state->squeezing)
235
- PadAndSwitchToSqueezingPhase(state);
236
- if ((outputLength % 8) != 0)
237
- return 1; // Only multiple of 8 bits are allowed, truncation can be done at user level
146
+ if (!instance->squeezing)
147
+ Keccak_SpongeAbsorbLastFewBits(instance, 0x01);
238
148
 
239
149
  i = 0;
240
- while(i < outputLength) {
241
- if (state->bitsAvailableForSqueezing == 0) {
242
- KeccakPermutation(state->state);
243
- #ifdef ProvideFast1024
244
- if (state->rate == 1024) {
245
- KeccakExtract1024bits(state->state, state->dataQueue);
246
- state->bitsAvailableForSqueezing = 1024;
150
+ curData = data;
151
+ while(i < dataByteLen) {
152
+ if ((instance->byteIOIndex == rateInBytes) && (dataByteLen >= (i + rateInBytes))) {
153
+ // processing full blocks first
154
+ if ((rateInBytes % SnP_laneLengthInBytes) == 0) {
155
+ // fast lane: whole lane rate
156
+ j = SnP_FBWL_Squeeze(instance->state, rateInBytes/SnP_laneLengthInBytes, curData, dataByteLen - i);
157
+ i += j;
158
+ curData += j;
247
159
  }
248
- else
249
- #endif
250
- {
251
- KeccakExtract(state->state, state->dataQueue, state->rate/64);
252
- state->bitsAvailableForSqueezing = state->rate;
160
+ else {
161
+ for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
162
+ SnP_Permute(instance->state);
163
+ SnP_ExtractBytes(instance->state, curData, 0, rateInBytes);
164
+ #ifdef KeccakReference
165
+ displayBytes(1, "Squeezed block", curData, rateInBytes);
166
+ #endif
167
+ curData+=rateInBytes;
168
+ }
169
+ i = dataByteLen - j;
170
+ }
171
+ }
172
+ else {
173
+ // normal lane: using the message queue
174
+ if (instance->byteIOIndex == rateInBytes) {
175
+ SnP_Permute(instance->state);
176
+ instance->byteIOIndex = 0;
253
177
  }
178
+ partialBlock = (unsigned int)(dataByteLen - i);
179
+ if (partialBlock+instance->byteIOIndex > rateInBytes)
180
+ partialBlock = rateInBytes-instance->byteIOIndex;
181
+ i += partialBlock;
182
+
183
+ SnP_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
254
184
  #ifdef KeccakReference
255
- displayBytes(1, "Block available for squeezing", state->dataQueue, state->bitsAvailableForSqueezing/8);
185
+ displayBytes(1, "Squeezed block (part)", curData, partialBlock);
256
186
  #endif
187
+ curData += partialBlock;
188
+ instance->byteIOIndex += partialBlock;
257
189
  }
258
- partialBlock = state->bitsAvailableForSqueezing;
259
- if ((unsigned long long)partialBlock > outputLength - i)
260
- partialBlock = (unsigned int)(outputLength - i);
261
- memcpy(output+i/8, state->dataQueue+(state->rate-state->bitsAvailableForSqueezing)/8, partialBlock/8);
262
- state->bitsAvailableForSqueezing -= partialBlock;
263
- i += partialBlock;
264
190
  }
265
191
  return 0;
266
192
  }
@@ -1,10 +1,12 @@
1
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/
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
5
 
6
- Implementation by the designers,
7
- hereby denoted as "the implementer".
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/
8
10
 
9
11
  To the extent possible under law, the implementer has waived all copyright
10
12
  and related or neighboring rights to the source code in this file.
@@ -14,10 +16,13 @@ http://creativecommons.org/publicdomain/zero/1.0/
14
16
  #ifndef _KeccakSponge_h_
15
17
  #define _KeccakSponge_h_
16
18
 
17
- #define KeccakPermutationSize 1600
18
- #define KeccakPermutationSizeInBytes (KeccakPermutationSize/8)
19
- #define KeccakMaximumRate 1536
20
- #define KeccakMaximumRateInBytes (KeccakMaximumRate/8)
19
+ #include "SnP-interface.h"
20
+ #include <string.h>
21
+
22
+ // on Mac OS-X and possibly others, ALIGN(x) is defined in param.h, and -Werror chokes on the redef.
23
+ #ifdef ALIGN
24
+ #undef ALIGN
25
+ #endif
21
26
 
22
27
  #if defined(__GNUC__)
23
28
  #define ALIGN __attribute__ ((aligned(32)))
@@ -27,50 +32,82 @@ http://creativecommons.org/publicdomain/zero/1.0/
27
32
  #define ALIGN
28
33
  #endif
29
34
 
30
- ALIGN typedef struct spongeStateStruct {
31
- ALIGN unsigned char state[KeccakPermutationSizeInBytes];
32
- ALIGN unsigned char dataQueue[KeccakMaximumRateInBytes];
35
+ /**
36
+ * Structure that contains the sponge instance attributes for use with the
37
+ * Keccak_Sponge* functions.
38
+ * It gathers the state processed by the permutation as well as the rate,
39
+ * the position of input/output bytes in the state and the phase
40
+ * (absorbing or squeezing).
41
+ */
42
+ ALIGN typedef struct Keccak_SpongeInstanceStruct {
43
+ /** The state processed by the permutation. */
44
+ ALIGN unsigned char state[SnP_stateSizeInBytes];
45
+ /** The value of the rate in bits.*/
33
46
  unsigned int rate;
34
- unsigned int capacity;
35
- unsigned int bitsInQueue;
36
- unsigned int fixedOutputLength;
47
+ /** The position in the state of the next byte to be input (when absorbing) or output (when squeezing). */
48
+ unsigned int byteIOIndex;
49
+ /** If set to 0, in the absorbing phase; otherwise, in the squeezing phase. */
37
50
  int squeezing;
38
- unsigned int bitsAvailableForSqueezing;
39
- } spongeState;
51
+ } Keccak_SpongeInstance;
40
52
 
41
53
  /**
42
54
  * Function to initialize the state of the Keccak[r, c] sponge function.
43
- * The sponge function is set to the absorbing phase.
44
- * @param state Pointer to the state of the sponge function to be initialized.
55
+ * The phase of the sponge function is set to absorbing.
56
+ * @param spongeInstance Pointer to the sponge instance to be initialized.
45
57
  * @param rate The value of the rate r.
46
58
  * @param capacity The value of the capacity c.
47
- * @pre One must have r+c=1600 and the rate a multiple of 64 bits in this implementation.
59
+ * @pre One must have r+c equal to the supported width of this implementation
60
+ * and the rate a multiple of 8 bits (one byte) in this implementation.
48
61
  * @return Zero if successful, 1 otherwise.
49
62
  */
50
- int InitSponge(spongeState *state, unsigned int rate, unsigned int capacity);
63
+ int Keccak_SpongeInitialize(Keccak_SpongeInstance *spongeInstance, unsigned int rate, unsigned int capacity);
64
+
51
65
  /**
52
- * Function to give input data for the sponge function to absorb.
53
- * @param state Pointer to the state of the sponge function initialized by InitSponge().
54
- * @param data Pointer to the input data.
55
- * When @a databitLen is not a multiple of 8, the last bits of data must be
56
- * in the least significant bits of the last byte.
57
- * @param databitLen The number of input bits provided in the input data.
58
- * @pre In the previous call to Absorb(), databitLen was a multiple of 8.
66
+ * Function to give input data bytes for the sponge function to absorb.
67
+ * @param spongeInstance Pointer to the sponge instance initialized by Keccak_SpongeInitialize().
68
+ * @param data Pointer to the input data.
69
+ * @param dataByteLen The number of input bytes provided in the input data.
59
70
  * @pre The sponge function must be in the absorbing phase,
60
- * i.e., Squeeze() must not have been called before.
71
+ * i.e., Keccak_SpongeSqueeze() or Keccak_SpongeAbsorbLastFewBits()
72
+ * must not have been called before.
61
73
  * @return Zero if successful, 1 otherwise.
62
74
  */
63
- int Absorb(spongeState *state, const unsigned char *data, unsigned long long databitlen);
75
+ int Keccak_SpongeAbsorb(Keccak_SpongeInstance *spongeInstance, const unsigned char *data, size_t dataByteLen);
76
+
77
+ /**
78
+ * Function to give input data bits for the sponge function to absorb
79
+ * and then to switch to the squeezing phase.
80
+ * @param spongeInstance Pointer to the sponge instance initialized by Keccak_SpongeInitialize().
81
+ * @param delimitedData Byte containing from 0 to 7 trailing bits
82
+ * that must be absorbed.
83
+ * These <i>n</i> bits must be in the least significant bit positions.
84
+ * These bits must be delimited with a bit 1 at position <i>n</i>
85
+ * (counting from 0=LSB to 7=MSB) and followed by bits 0
86
+ * from position <i>n</i>+1 to position 7.
87
+ * Some examples:
88
+ * - If no bits are to be absorbed, then @a delimitedData must be 0x01.
89
+ * - If the 2-bit sequence 0,0 is to be absorbed, @a delimitedData must be 0x04.
90
+ * - If the 5-bit sequence 0,1,0,0,1 is to be absorbed, @a delimitedData must be 0x32.
91
+ * - If the 7-bit sequence 1,1,0,1,0,0,0 is to be absorbed, @a delimitedData must be 0x8B.
92
+ * .
93
+ * @pre The sponge function must be in the absorbing phase,
94
+ * i.e., Keccak_SpongeSqueeze() or Keccak_SpongeAbsorbLastFewBits()
95
+ * must not have been called before.
96
+ * @pre @a delimitedData ≠ 0x00
97
+ * @return Zero if successful, 1 otherwise.
98
+ */
99
+ int Keccak_SpongeAbsorbLastFewBits(Keccak_SpongeInstance *spongeInstance, unsigned char delimitedData);
100
+
64
101
  /**
65
102
  * Function to squeeze output data from the sponge function.
66
- * If the sponge function was in the absorbing phase, this function
67
- * switches it to the squeezing phase.
68
- * @param state Pointer to the state of the sponge function initialized by InitSponge().
69
- * @param output Pointer to the buffer where to store the output data.
70
- * @param outputLength The number of output bits desired.
71
- * It must be a multiple of 8.
103
+ * If the sponge function was in the absorbing phase, this function
104
+ * switches it to the squeezing phase
105
+ * as if Keccak_SpongeAbsorbLastFewBits(spongeInstance, 0x01) was called.
106
+ * @param spongeInstance Pointer to the sponge instance initialized by Keccak_SpongeInitialize().
107
+ * @param data Pointer to the buffer where to store the output data.
108
+ * @param dataByteLen The number of output bytes desired.
72
109
  * @return Zero if successful, 1 otherwise.
73
110
  */
74
- int Squeeze(spongeState *state, unsigned char *output, unsigned long long outputLength);
111
+ int Keccak_SpongeSqueeze(Keccak_SpongeInstance *spongeInstance, unsigned char *data, size_t dataByteLen);
75
112
 
76
113
  #endif