sha3 1.0.5 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.clang-format +54 -0
  4. data/.document +4 -3
  5. data/.rdoc_options +10 -0
  6. data/.rspec +2 -2
  7. data/.rubocop.yml +5 -1
  8. data/CHANGELOG.md +23 -0
  9. data/Gemfile +11 -0
  10. data/LICENSE.txt +1 -1
  11. data/README.md +154 -67
  12. data/Rakefile +9 -3
  13. data/certs/io+sha3@jsg.io.pem +26 -0
  14. data/doc/sha3.rb +81 -0
  15. data/ext/sha3/digest.c +635 -163
  16. data/ext/sha3/digest.h +71 -35
  17. data/ext/sha3/extconf.rb +42 -38
  18. data/ext/sha3/lib/high/Keccak/KeccakDuplex.c +81 -0
  19. data/ext/sha3/lib/high/Keccak/KeccakDuplex.h +73 -0
  20. data/ext/sha3/lib/high/Keccak/KeccakDuplex.inc +201 -0
  21. data/ext/sha3/lib/high/Keccak/KeccakSponge.c +2 -18
  22. data/ext/sha3/lib/high/Keccak/KeccakSponge.h +4 -10
  23. data/ext/sha3/lib/high/Keccak/KeccakSponge.inc +27 -31
  24. data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.c +61 -0
  25. data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.h +67 -0
  26. data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.inc +128 -0
  27. data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.c +93 -0
  28. data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.h +599 -0
  29. data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.inc +573 -0
  30. data/ext/sha3/lib/high/common/Phases.h +25 -0
  31. data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-64.macros +19 -9
  32. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-SnP.h +18 -12
  33. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference32BI.c +28 -36
  34. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-SnP.h +18 -12
  35. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c +28 -59
  36. data/ext/sha3/lib/low/common/PlSnP-Fallback.inc +291 -0
  37. data/ext/sha3/lib/low/common/SnP-Relaned.h +145 -0
  38. data/lib/sha3.rb +25 -28
  39. data.tar.gz.sig +0 -0
  40. metadata +55 -115
  41. metadata.gz.sig +0 -0
  42. data/.yardopts +0 -1
  43. data/ChangeLog.rdoc +0 -27
  44. data/certs/johanns.pem +0 -25
  45. data/ext/sha3/sha3.c +0 -62
  46. data/ext/sha3/sha3.h +0 -26
  47. data/lib/sha3/doc.rb +0 -121
  48. data/lib/sha3/version.rb +0 -9
  49. data/sha3.gemspec +0 -54
  50. data/tests.sh +0 -29
@@ -24,8 +24,6 @@ http://creativecommons.org/publicdomain/zero/1.0/
24
24
  #define SpongeAbsorbLastFewBits JOIN(prefix, _SpongeAbsorbLastFewBits)
25
25
  #define SpongeSqueeze JOIN(prefix, _SpongeSqueeze)
26
26
 
27
- #define SnP_stateSizeInBytes JOIN(SnP, _stateSizeInBytes)
28
- #define SnP_stateAlignment JOIN(SnP, _stateAlignment)
29
27
  #define SnP_StaticInitialize JOIN(SnP, _StaticInitialize)
30
28
  #define SnP_Initialize JOIN(SnP, _Initialize)
31
29
  #define SnP_AddByte JOIN(SnP, _AddByte)
@@ -34,7 +32,7 @@ http://creativecommons.org/publicdomain/zero/1.0/
34
32
 
35
33
  int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input, size_t inputByteLen, unsigned char suffix, unsigned char *output, size_t outputByteLen)
36
34
  {
37
- ALIGN(SnP_stateAlignment) unsigned char state[SnP_stateSizeInBytes];
35
+ SnP_state state;
38
36
  unsigned int partialBlock;
39
37
  const unsigned char *curInput = input;
40
38
  unsigned char *curOutput = output;
@@ -49,14 +47,14 @@ int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input,
49
47
 
50
48
  /* Initialize the state */
51
49
  SnP_StaticInitialize();
52
- SnP_Initialize(state);
50
+ SnP_Initialize(&state);
53
51
 
54
52
  /* First, absorb whole blocks */
55
53
  #ifdef SnP_FastLoop_Absorb
56
54
  if (((rateInBytes % (SnP_width/200)) == 0) && (inputByteLen >= rateInBytes)) {
57
55
  /* fast lane: whole lane rate */
58
56
  size_t j;
59
- j = SnP_FastLoop_Absorb(state, rateInBytes/(SnP_width/200), curInput, inputByteLen);
57
+ j = SnP_FastLoop_Absorb(&state, rateInBytes/(SnP_width/200), curInput, inputByteLen);
60
58
  curInput += j;
61
59
  inputByteLen -= j;
62
60
  }
@@ -65,8 +63,8 @@ int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input,
65
63
  #ifdef KeccakReference
66
64
  displayBytes(1, "Block to be absorbed", curInput, rateInBytes);
67
65
  #endif
68
- SnP_AddBytes(state, curInput, 0, rateInBytes);
69
- SnP_Permute(state);
66
+ SnP_AddBytes(&state, curInput, 0, rateInBytes);
67
+ SnP_Permute(&state);
70
68
  curInput += rateInBytes;
71
69
  inputByteLen -= rateInBytes;
72
70
  }
@@ -76,7 +74,7 @@ int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input,
76
74
  #ifdef KeccakReference
77
75
  displayBytes(1, "Block to be absorbed (part)", curInput, partialBlock);
78
76
  #endif
79
- SnP_AddBytes(state, curInput, 0, partialBlock);
77
+ SnP_AddBytes(&state, curInput, 0, partialBlock);
80
78
 
81
79
  /* Finally, absorb the suffix */
82
80
  #ifdef KeccakReference
@@ -87,12 +85,12 @@ int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input,
87
85
  }
88
86
  #endif
89
87
  /* Last few bits, whose delimiter coincides with first bit of padding */
90
- SnP_AddByte(state, suffix, partialBlock);
88
+ SnP_AddByte(&state, suffix, partialBlock);
91
89
  /* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */
92
90
  if ((suffix >= 0x80) && (partialBlock == (rateInBytes-1)))
93
- SnP_Permute(state);
91
+ SnP_Permute(&state);
94
92
  /* Second bit of padding */
95
- SnP_AddByte(state, 0x80, rateInBytes-1);
93
+ SnP_AddByte(&state, 0x80, rateInBytes-1);
96
94
  #ifdef KeccakReference
97
95
  {
98
96
  unsigned char block[SnP_width/8];
@@ -101,15 +99,15 @@ int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input,
101
99
  displayBytes(1, "Second bit of padding", block, rateInBytes);
102
100
  }
103
101
  #endif
104
- SnP_Permute(state);
102
+ SnP_Permute(&state);
105
103
  #ifdef KeccakReference
106
104
  displayText(1, "--- Switching to squeezing phase ---");
107
105
  #endif
108
106
 
109
107
  /* First, output whole blocks */
110
108
  while(outputByteLen > (size_t)rateInBytes) {
111
- SnP_ExtractBytes(state, curOutput, 0, rateInBytes);
112
- SnP_Permute(state);
109
+ SnP_ExtractBytes(&state, curOutput, 0, rateInBytes);
110
+ SnP_Permute(&state);
113
111
  #ifdef KeccakReference
114
112
  displayBytes(1, "Squeezed block", curOutput, rateInBytes);
115
113
  #endif
@@ -119,7 +117,7 @@ int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input,
119
117
 
120
118
  /* Finally, output what remains */
121
119
  partialBlock = (unsigned int)outputByteLen;
122
- SnP_ExtractBytes(state, curOutput, 0, partialBlock);
120
+ SnP_ExtractBytes(&state, curOutput, 0, partialBlock);
123
121
  #ifdef KeccakReference
124
122
  displayBytes(1, "Squeezed block (part)", curOutput, partialBlock);
125
123
  #endif
@@ -138,7 +136,7 @@ int SpongeInitialize(SpongeInstance *instance, unsigned int rate, unsigned int c
138
136
  if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0))
139
137
  return 1;
140
138
  SnP_StaticInitialize();
141
- SnP_Initialize(instance->state);
139
+ SnP_Initialize(&instance->state);
142
140
  instance->rate = rate;
143
141
  instance->byteIOIndex = 0;
144
142
  instance->squeezing = 0;
@@ -166,7 +164,7 @@ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dat
166
164
  /* processing full blocks first */
167
165
  if ((rateInBytes % (SnP_width/200)) == 0) {
168
166
  /* fast lane: whole lane rate */
169
- j = SnP_FastLoop_Absorb(instance->state, rateInBytes/(SnP_width/200), curData, dataByteLen - i);
167
+ j = SnP_FastLoop_Absorb(&instance->state, rateInBytes/(SnP_width/200), curData, dataByteLen - i);
170
168
  i += j;
171
169
  curData += j;
172
170
  }
@@ -176,8 +174,8 @@ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dat
176
174
  #ifdef KeccakReference
177
175
  displayBytes(1, "Block to be absorbed", curData, rateInBytes);
178
176
  #endif
179
- SnP_AddBytes(instance->state, curData, 0, rateInBytes);
180
- SnP_Permute(instance->state);
177
+ SnP_AddBytes(&instance->state, curData, 0, rateInBytes);
178
+ SnP_Permute(&instance->state);
181
179
  curData+=rateInBytes;
182
180
  }
183
181
  i = dataByteLen - j;
@@ -196,11 +194,11 @@ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dat
196
194
  #endif
197
195
  i += partialBlock;
198
196
 
199
- SnP_AddBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
197
+ SnP_AddBytes(&instance->state, curData, instance->byteIOIndex, partialBlock);
200
198
  curData += partialBlock;
201
199
  instance->byteIOIndex += partialBlock;
202
200
  if (instance->byteIOIndex == rateInBytes) {
203
- SnP_Permute(instance->state);
201
+ SnP_Permute(&instance->state);
204
202
  instance->byteIOIndex = 0;
205
203
  }
206
204
  }
@@ -227,12 +225,12 @@ int SpongeAbsorbLastFewBits(SpongeInstance *instance, unsigned char delimitedDat
227
225
  }
228
226
  #endif
229
227
  /* Last few bits, whose delimiter coincides with first bit of padding */
230
- SnP_AddByte(instance->state, delimitedData, instance->byteIOIndex);
228
+ SnP_AddByte(&instance->state, delimitedData, instance->byteIOIndex);
231
229
  /* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */
232
230
  if ((delimitedData >= 0x80) && (instance->byteIOIndex == (rateInBytes-1)))
233
- SnP_Permute(instance->state);
231
+ SnP_Permute(&instance->state);
234
232
  /* Second bit of padding */
235
- SnP_AddByte(instance->state, 0x80, rateInBytes-1);
233
+ SnP_AddByte(&instance->state, 0x80, rateInBytes-1);
236
234
  #ifdef KeccakReference
237
235
  {
238
236
  unsigned char block[SnP_width/8];
@@ -241,7 +239,7 @@ int SpongeAbsorbLastFewBits(SpongeInstance *instance, unsigned char delimitedDat
241
239
  displayBytes(1, "Second bit of padding", block, rateInBytes);
242
240
  }
243
241
  #endif
244
- SnP_Permute(instance->state);
242
+ SnP_Permute(&instance->state);
245
243
  instance->byteIOIndex = 0;
246
244
  instance->squeezing = 1;
247
245
  #ifdef KeccakReference
@@ -267,8 +265,8 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
267
265
  while(i < dataByteLen) {
268
266
  if ((instance->byteIOIndex == rateInBytes) && (dataByteLen-i >= rateInBytes)) {
269
267
  for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
270
- SnP_Permute(instance->state);
271
- SnP_ExtractBytes(instance->state, curData, 0, rateInBytes);
268
+ SnP_Permute(&instance->state);
269
+ SnP_ExtractBytes(&instance->state, curData, 0, rateInBytes);
272
270
  #ifdef KeccakReference
273
271
  displayBytes(1, "Squeezed block", curData, rateInBytes);
274
272
  #endif
@@ -279,7 +277,7 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
279
277
  else {
280
278
  /* normal lane: using the message queue */
281
279
  if (instance->byteIOIndex == rateInBytes) {
282
- SnP_Permute(instance->state);
280
+ SnP_Permute(&instance->state);
283
281
  instance->byteIOIndex = 0;
284
282
  }
285
283
  if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
@@ -288,7 +286,7 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
288
286
  partialBlock = (unsigned int)(dataByteLen - i);
289
287
  i += partialBlock;
290
288
 
291
- SnP_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
289
+ SnP_ExtractBytes(&instance->state, curData, instance->byteIOIndex, partialBlock);
292
290
  #ifdef KeccakReference
293
291
  displayBytes(1, "Squeezed block (part)", curData, partialBlock);
294
292
  #endif
@@ -307,8 +305,6 @@ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByte
307
305
  #undef SpongeAbsorb
308
306
  #undef SpongeAbsorbLastFewBits
309
307
  #undef SpongeSqueeze
310
- #undef SnP_stateSizeInBytes
311
- #undef SnP_stateAlignment
312
308
  #undef SnP_StaticInitialize
313
309
  #undef SnP_Initialize
314
310
  #undef SnP_AddByte
@@ -0,0 +1,61 @@
1
+ /*
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
4
+
5
+ Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
6
+
7
+ Implementation by Gilles Van Assche, hereby denoted as "the implementer".
8
+
9
+ For more information, feedback or questions, please refer to the Keccak Team website:
10
+ https://keccak.team/
11
+
12
+ To the extent possible under law, the implementer has waived all copyright
13
+ and related or neighboring rights to the source code in this file.
14
+ http://creativecommons.org/publicdomain/zero/1.0/
15
+ */
16
+
17
+ #include "KeccakPRG.h"
18
+
19
+ #ifdef KeccakReference
20
+ #include "displayIntermediateValues.h"
21
+ #endif
22
+
23
+ #ifdef XKCP_has_KeccakP200
24
+ #include "KeccakP-200-SnP.h"
25
+
26
+ #define prefix KeccakWidth200
27
+ #define SnP_width 200
28
+ #include "KeccakPRG.inc"
29
+ #undef prefix
30
+ #undef SnP_width
31
+ #endif
32
+
33
+ #ifdef XKCP_has_KeccakP400
34
+ #include "KeccakP-400-SnP.h"
35
+
36
+ #define prefix KeccakWidth400
37
+ #define SnP_width 400
38
+ #include "KeccakPRG.inc"
39
+ #undef prefix
40
+ #undef SnP_width
41
+ #endif
42
+
43
+ #ifdef XKCP_has_KeccakP800
44
+ #include "KeccakP-800-SnP.h"
45
+
46
+ #define prefix KeccakWidth800
47
+ #define SnP_width 800
48
+ #include "KeccakPRG.inc"
49
+ #undef prefix
50
+ #undef SnP_width
51
+ #endif
52
+
53
+ #ifdef XKCP_has_KeccakP1600
54
+ #include "KeccakP-1600-SnP.h"
55
+
56
+ #define prefix KeccakWidth1600
57
+ #define SnP_width 1600
58
+ #include "KeccakPRG.inc"
59
+ #undef prefix
60
+ #undef SnP_width
61
+ #endif
@@ -0,0 +1,67 @@
1
+ /*
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
4
+
5
+ Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
6
+
7
+ Implementation by Gilles Van Assche, hereby denoted as "the implementer".
8
+
9
+ For more information, feedback or questions, please refer to the Keccak Team website:
10
+ https://keccak.team/
11
+
12
+ To the extent possible under law, the implementer has waived all copyright
13
+ and related or neighboring rights to the source code in this file.
14
+ http://creativecommons.org/publicdomain/zero/1.0/
15
+ */
16
+
17
+ #ifndef _KeccakPRG_h_
18
+ #define _KeccakPRG_h_
19
+
20
+ /* For the documentation, please follow the link: */
21
+ /* #include "KeccakPRG-documentation.h" */
22
+
23
+ #include <string.h>
24
+ #include "align.h"
25
+ #include "config.h"
26
+ #include "KeccakDuplex.h"
27
+
28
+ #define XKCP_DeclareSpongePRG_Structure(prefix) \
29
+ typedef struct prefix##_SpongePRG_InstanceStruct { \
30
+ prefix##_DuplexInstance duplex; \
31
+ } prefix##_SpongePRG_Instance;
32
+
33
+ #define XKCP_DeclareSpongePRG_Functions(prefix) \
34
+ int prefix##_SpongePRG_Initialize(prefix##_SpongePRG_Instance *instance, unsigned int capacity); \
35
+ int prefix##_SpongePRG_Feed(prefix##_SpongePRG_Instance *instance, const unsigned char *input, unsigned int inputByteLen); \
36
+ int prefix##_SpongePRG_Fetch(prefix##_SpongePRG_Instance *Instance, unsigned char *out, unsigned int outByteLen); \
37
+ int prefix##_SpongePRG_Forget(prefix##_SpongePRG_Instance *instance);
38
+
39
+ #ifdef XKCP_has_KeccakP200
40
+ #include "KeccakP-200-SnP.h"
41
+ XKCP_DeclareSpongePRG_Structure(KeccakWidth200, KeccakP200_stateSizeInBytes, KeccakP200_stateAlignment)
42
+ XKCP_DeclareSpongePRG_Functions(KeccakWidth200)
43
+ #define XKCP_has_PRG_Keccak_width200
44
+ #endif
45
+
46
+ #ifdef XKCP_has_KeccakP400
47
+ #include "KeccakP-400-SnP.h"
48
+ XKCP_DeclareSpongePRG_Structure(KeccakWidth400, KeccakP400_stateSizeInBytes, KeccakP400_stateAlignment)
49
+ XKCP_DeclareSpongePRG_Functions(KeccakWidth400)
50
+ #define XKCP_has_PRG_Keccak_width400
51
+ #endif
52
+
53
+ #ifdef XKCP_has_KeccakP800
54
+ #include "KeccakP-800-SnP.h"
55
+ XKCP_DeclareSpongePRG_Structure(KeccakWidth800, KeccakP800_stateSizeInBytes, KeccakP800_stateAlignment)
56
+ XKCP_DeclareSpongePRG_Functions(KeccakWidth800)
57
+ #define XKCP_has_PRG_Keccak_width800
58
+ #endif
59
+
60
+ #ifdef XKCP_has_KeccakP1600
61
+ #include "KeccakP-1600-SnP.h"
62
+ XKCP_DeclareSpongePRG_Structure(KeccakWidth1600)
63
+ XKCP_DeclareSpongePRG_Functions(KeccakWidth1600)
64
+ #define XKCP_has_PRG_Keccak_width1600
65
+ #endif
66
+
67
+ #endif
@@ -0,0 +1,128 @@
1
+ /*
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
4
+
5
+ Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
6
+
7
+ Implementation by Gilles Van Assche, hereby denoted as "the implementer".
8
+
9
+ For more information, feedback or questions, please refer to the Keccak Team website:
10
+ https://keccak.team/
11
+
12
+ To the extent possible under law, the implementer has waived all copyright
13
+ and related or neighboring rights to the source code in this file.
14
+ http://creativecommons.org/publicdomain/zero/1.0/
15
+ */
16
+
17
+ #define JOIN0(a, b) a ## b
18
+ #define JOIN(a, b) JOIN0(a, b)
19
+
20
+ #define SpongePRG_Instance JOIN(prefix, _SpongePRG_Instance)
21
+ #define SpongePRG_Initialize JOIN(prefix, _SpongePRG_Initialize)
22
+ #define SpongePRG_Feed JOIN(prefix, _SpongePRG_Feed)
23
+ #define SpongePRG_Fetch JOIN(prefix, _SpongePRG_Fetch)
24
+ #define SpongePRG_Forget JOIN(prefix, _SpongePRG_Forget)
25
+
26
+ #define DuplexInstance JOIN(prefix, _DuplexInstance)
27
+ #define DuplexInitialize JOIN(prefix, _DuplexInitialize)
28
+ #define Duplexing JOIN(prefix, _Duplexing)
29
+ #define DuplexingFeedPartialInput JOIN(prefix, _DuplexingFeedPartialInput)
30
+ #define DuplexingOverwriteWithZeroes JOIN(prefix, _DuplexingOverwriteWithZeroes)
31
+ #define DuplexingGetFurtherOutput JOIN(prefix, _DuplexingGetFurtherOutput)
32
+ #define DuplexGetInputIndex(duplex) (duplex)->byteInputIndex
33
+ #define DuplexGetOutputIndex(duplex) (duplex)->byteOutputIndex
34
+ #define DuplexSetOutputIndex(duplex, i) (duplex)->byteOutputIndex = (i)
35
+
36
+ int SpongePRG_Initialize(SpongePRG_Instance *instance, unsigned int capacity)
37
+ {
38
+ unsigned int rate;
39
+ unsigned int rhoInBytes;
40
+
41
+ if (capacity > (SnP_width-10))
42
+ return 1;
43
+
44
+ rate = SnP_width - capacity;
45
+ rhoInBytes = (rate-2)/8;
46
+
47
+ if ( (rhoInBytes == 0) || (rhoInBytes >= SnP_width/8) )
48
+ return 1;
49
+ return DuplexInitialize(&instance->duplex, rate, capacity);
50
+ }
51
+
52
+ int SpongePRG_Feed(SpongePRG_Instance *instance, const unsigned char *input, unsigned int inputByteLen)
53
+ {
54
+ unsigned int rhoInBytes = (instance->duplex.rate-2)/8;
55
+ int error = 0;
56
+
57
+ while( !error && (inputByteLen >= rhoInBytes - DuplexGetInputIndex(&instance->duplex))) {
58
+ unsigned int localSize = rhoInBytes - DuplexGetInputIndex(&instance->duplex);
59
+ error |= DuplexingFeedPartialInput(&instance->duplex, input, localSize);
60
+ error |= Duplexing(&instance->duplex, 0, 0, 0, 0, 0x01);
61
+ input += localSize;
62
+ inputByteLen -= localSize;
63
+ }
64
+ if (!error)
65
+ error = DuplexingFeedPartialInput(&instance->duplex, input, inputByteLen);
66
+ DuplexSetOutputIndex(&instance->duplex, rhoInBytes);
67
+ return error;
68
+ }
69
+
70
+ int SpongePRG_Fetch(SpongePRG_Instance *instance, unsigned char *output, unsigned int outputByteLen)
71
+ {
72
+ unsigned int rhoInBytes = (instance->duplex.rate-2)/8;
73
+ int error = 0;
74
+
75
+ if (DuplexGetOutputIndex(&instance->duplex) < rhoInBytes) {
76
+ unsigned int localSize = rhoInBytes - DuplexGetOutputIndex(&instance->duplex);
77
+ localSize = (localSize <= outputByteLen) ? localSize : outputByteLen;
78
+ error = DuplexingGetFurtherOutput(&instance->duplex, output, localSize);
79
+ output += localSize;
80
+ outputByteLen -= localSize;
81
+ }
82
+
83
+ while( !error && (outputByteLen > 0) ) {
84
+ error = Duplexing(&instance->duplex, 0, 0, 0, 0, 0x01);
85
+ if (!error) {
86
+ unsigned int localSize = (rhoInBytes <= outputByteLen) ? rhoInBytes : outputByteLen;
87
+ error = DuplexingGetFurtherOutput(&instance->duplex, output, localSize);
88
+ output += localSize;
89
+ outputByteLen -= localSize;
90
+ }
91
+ }
92
+ return error;
93
+ }
94
+
95
+ int SpongePRG_Forget(SpongePRG_Instance *instance)
96
+ {
97
+ unsigned int rhoInBytes = (instance->duplex.rate-2)/8;
98
+ unsigned int capacity = SnP_width - instance->duplex.rate;
99
+ int error;
100
+
101
+ if ((rhoInBytes*8) < capacity)
102
+ return 1;
103
+
104
+ error = Duplexing(&instance->duplex, 0, 0, 0, 0, 0x01);
105
+ if ( !error ) {
106
+ error = DuplexingOverwriteWithZeroes(&instance->duplex, rhoInBytes);
107
+ if ( !error )
108
+ error = Duplexing(&instance->duplex, 0, 0, 0, 0, 0x01);
109
+ }
110
+ DuplexSetOutputIndex(&instance->duplex, rhoInBytes);
111
+ return error;
112
+ }
113
+
114
+ #undef SpongePRG_Instance
115
+ #undef SpongePRG_Initialize
116
+ #undef SpongePRG_Feed
117
+ #undef SpongePRG_Fetch
118
+ #undef SpongePRG_Forget
119
+
120
+ #undef DuplexInstance
121
+ #undef DuplexInitialize
122
+ #undef Duplexing
123
+ #undef DuplexingFeedPartialInput
124
+ #undef DuplexingOverwriteWithZeroes
125
+ #undef DuplexingGetFurtherOutput
126
+ #undef DuplexGetInputIndex
127
+ #undef DuplexGetOutputIndex
128
+ #undef DuplexSetOutputIndex
@@ -0,0 +1,93 @@
1
+ /*
2
+ The eXtended Keccak Code Package (XKCP)
3
+ https://github.com/XKCP/XKCP
4
+
5
+ Keccak, designed by Guido Bertoni, Joan Daemen, Michaël Peeters and Gilles Van Assche.
6
+
7
+ Implementation by Ronny Van Keer, hereby denoted as "the implementer".
8
+
9
+ For more information, feedback or questions, please refer to the Keccak Team website:
10
+ https://keccak.team/
11
+
12
+ To the extent possible under law, the implementer has waived all copyright
13
+ and related or neighboring rights to the source code in this file.
14
+ http://creativecommons.org/publicdomain/zero/1.0/
15
+ */
16
+
17
+ #include <string.h>
18
+ #include "SP800-185.h"
19
+
20
+ #ifdef XKCP_has_KeccakP1600times2
21
+ #include "KeccakP-1600-times2-SnP.h"
22
+ #endif
23
+
24
+ #ifdef XKCP_has_KeccakP1600times4
25
+ #include "KeccakP-1600-times4-SnP.h"
26
+ #endif
27
+
28
+ #ifdef XKCP_has_KeccakP1600times8
29
+ #include "KeccakP-1600-times8-SnP.h"
30
+ #endif
31
+
32
+ /* #define DEBUG_DUMP */
33
+
34
+ #if defined(DEBUG_DUMP)
35
+
36
+ #include <stdio.h>
37
+
38
+ static void DUMP( const unsigned char * pText, const unsigned char * pData, unsigned int size )
39
+ {
40
+ unsigned int i;
41
+ printf("%s (%u bytes):", pText, size);
42
+ for(i=0; i<size; i++)
43
+ printf(" %02x", (int)pData[i]);
44
+ printf("\n");
45
+ }
46
+ #else
47
+ #define DUMP(pText, pData, size )
48
+ #endif
49
+
50
+ static unsigned int left_encode( unsigned char * encbuf, size_t value )
51
+ {
52
+ unsigned int n, i;
53
+ size_t v;
54
+
55
+ for ( v = value, n = 0; v && (n < sizeof(size_t)); ++n, v >>= 8 )
56
+ ; /* empty */
57
+ if (n == 0)
58
+ n = 1;
59
+ for ( i = 1; i <= n; ++i )
60
+ {
61
+ encbuf[i] = (unsigned char)(value >> (8 * (n-i)));
62
+ }
63
+ encbuf[0] = (unsigned char)n;
64
+ return n + 1;
65
+ }
66
+
67
+ static unsigned int right_encode( unsigned char * encbuf, size_t value )
68
+ {
69
+ unsigned int n, i;
70
+ size_t v;
71
+
72
+ for ( v = value, n = 0; v && (n < sizeof(size_t)); ++n, v >>= 8 )
73
+ ; /* empty */
74
+ if (n == 0)
75
+ n = 1;
76
+ for ( i = 1; i <= n; ++i )
77
+ {
78
+ encbuf[i-1] = (unsigned char)(value >> (8 * (n-i)));
79
+ }
80
+ encbuf[n] = (unsigned char)n;
81
+ return n + 1;
82
+ }
83
+
84
+ #define laneSize 8
85
+ #define suffix 0x1F
86
+
87
+ #define security 128
88
+ #include "SP800-185.inc"
89
+ #undef security
90
+
91
+ #define security 256
92
+ #include "SP800-185.inc"
93
+ #undef security