sha3 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/README.md +8 -5
  4. data/certs/johanns.pem +23 -22
  5. data/ext/sha3/config.h +26 -0
  6. data/ext/sha3/digest.c +6 -6
  7. data/ext/sha3/extconf.rb +40 -10
  8. data/ext/sha3/lib/common/align.h +33 -0
  9. data/ext/sha3/{brg_endian.h → lib/common/brg_endian.h} +8 -7
  10. data/ext/sha3/{KeccakHash.c → lib/high/Keccak/FIPS202/KeccakHash.c} +26 -25
  11. data/ext/sha3/{KeccakHash.h → lib/high/Keccak/FIPS202/KeccakHash.h} +34 -19
  12. data/ext/sha3/lib/high/Keccak/KeccakSponge.c +111 -0
  13. data/ext/sha3/lib/high/Keccak/KeccakSponge.h +76 -0
  14. data/ext/sha3/lib/high/Keccak/KeccakSponge.inc +316 -0
  15. data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-64.macros +748 -0
  16. data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-unrolling.macros +305 -0
  17. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-SnP.h +44 -0
  18. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference.h +23 -0
  19. data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference32BI.c +625 -0
  20. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-SnP.h +44 -0
  21. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c +444 -0
  22. data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.h +23 -0
  23. data/lib/sha3/version.rb +1 -1
  24. data/sha3.gemspec +6 -5
  25. data/tests.sh +8 -7
  26. data.tar.gz.sig +0 -0
  27. metadata +50 -66
  28. metadata.gz.sig +0 -0
  29. data/ext/sha3/KeccakF-1600-interface.h +0 -40
  30. data/ext/sha3/KeccakSponge.c +0 -192
  31. data/ext/sha3/KeccakSponge.h +0 -113
  32. data/ext/sha3/Optimized64/KeccakF-1600-64.macros +0 -2199
  33. data/ext/sha3/Optimized64/KeccakF-1600-opt64-settings.h +0 -3
  34. data/ext/sha3/Optimized64/KeccakF-1600-opt64.c +0 -508
  35. data/ext/sha3/Optimized64/KeccakF-1600-unrolling.macros +0 -126
  36. data/ext/sha3/Optimized64/SnP-interface.h +0 -47
  37. data/ext/sha3/Reference/KeccakF-1600-reference.c +0 -311
  38. data/ext/sha3/Reference/KeccakF-reference.h +0 -26
  39. data/ext/sha3/Reference/SnP-FBWL-default.c +0 -96
  40. data/ext/sha3/Reference/SnP-FBWL-default.h +0 -26
  41. data/ext/sha3/Reference/SnP-interface.h +0 -42
  42. data/ext/sha3/Reference/displayIntermediateValues.c +0 -158
  43. data/ext/sha3/Reference/displayIntermediateValues.h +0 -34
  44. data/ext/sha3/SnP-Relaned.h +0 -249
@@ -0,0 +1,76 @@
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 the designers, 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 _KeccakSponge_h_
18
+ #define _KeccakSponge_h_
19
+
20
+ /* For the documentation, please follow the link: */
21
+ /* #include "KeccakSponge-documentation.h" */
22
+
23
+ #include <string.h>
24
+ #include "align.h"
25
+ #include "config.h"
26
+
27
+ #define XKCP_DeclareSpongeStructure(prefix, size, alignment) \
28
+ ALIGN(alignment) typedef struct prefix##_SpongeInstanceStruct { \
29
+ unsigned char state[size]; \
30
+ unsigned int rate; \
31
+ unsigned int byteIOIndex; \
32
+ int squeezing; \
33
+ } prefix##_SpongeInstance;
34
+
35
+ #define XKCP_DeclareSpongeFunctions(prefix) \
36
+ int prefix##_Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input, size_t inputByteLen, unsigned char suffix, unsigned char *output, size_t outputByteLen); \
37
+ int prefix##_SpongeInitialize(prefix##_SpongeInstance *spongeInstance, unsigned int rate, unsigned int capacity); \
38
+ int prefix##_SpongeAbsorb(prefix##_SpongeInstance *spongeInstance, const unsigned char *data, size_t dataByteLen); \
39
+ int prefix##_SpongeAbsorbLastFewBits(prefix##_SpongeInstance *spongeInstance, unsigned char delimitedData); \
40
+ int prefix##_SpongeSqueeze(prefix##_SpongeInstance *spongeInstance, unsigned char *data, size_t dataByteLen);
41
+
42
+ #ifdef XKCP_has_KeccakP200
43
+ #include "KeccakP-200-SnP.h"
44
+ XKCP_DeclareSpongeStructure(KeccakWidth200, KeccakP200_stateSizeInBytes, KeccakP200_stateAlignment)
45
+ XKCP_DeclareSpongeFunctions(KeccakWidth200)
46
+ #define XKCP_has_Sponge_Keccak_width200
47
+ #endif
48
+
49
+ #ifdef XKCP_has_KeccakP400
50
+ #include "KeccakP-400-SnP.h"
51
+ XKCP_DeclareSpongeStructure(KeccakWidth400, KeccakP400_stateSizeInBytes, KeccakP400_stateAlignment)
52
+ XKCP_DeclareSpongeFunctions(KeccakWidth400)
53
+ #define XKCP_has_Sponge_Keccak_width400
54
+ #endif
55
+
56
+ #ifdef XKCP_has_KeccakP800
57
+ #include "KeccakP-800-SnP.h"
58
+ XKCP_DeclareSpongeStructure(KeccakWidth800, KeccakP800_stateSizeInBytes, KeccakP800_stateAlignment)
59
+ XKCP_DeclareSpongeFunctions(KeccakWidth800)
60
+ #define XKCP_has_Sponge_Keccak_width800
61
+ #endif
62
+
63
+ #ifdef XKCP_has_KeccakP1600
64
+ #include "KeccakP-1600-SnP.h"
65
+ XKCP_DeclareSpongeStructure(KeccakWidth1600, KeccakP1600_stateSizeInBytes, KeccakP1600_stateAlignment)
66
+ XKCP_DeclareSpongeFunctions(KeccakWidth1600)
67
+ #define XKCP_has_Sponge_Keccak_width1600
68
+ #endif
69
+
70
+ #ifdef XKCP_has_KeccakP1600
71
+ #include "KeccakP-1600-SnP.h"
72
+ XKCP_DeclareSpongeStructure(KeccakWidth1600_12rounds, KeccakP1600_stateSizeInBytes, KeccakP1600_stateAlignment)
73
+ XKCP_DeclareSpongeFunctions(KeccakWidth1600_12rounds)
74
+ #endif
75
+
76
+ #endif
@@ -0,0 +1,316 @@
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 the designers, 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 Sponge JOIN(prefix, _Sponge)
21
+ #define SpongeInstance JOIN(prefix, _SpongeInstance)
22
+ #define SpongeInitialize JOIN(prefix, _SpongeInitialize)
23
+ #define SpongeAbsorb JOIN(prefix, _SpongeAbsorb)
24
+ #define SpongeAbsorbLastFewBits JOIN(prefix, _SpongeAbsorbLastFewBits)
25
+ #define SpongeSqueeze JOIN(prefix, _SpongeSqueeze)
26
+
27
+ #define SnP_stateSizeInBytes JOIN(SnP, _stateSizeInBytes)
28
+ #define SnP_stateAlignment JOIN(SnP, _stateAlignment)
29
+ #define SnP_StaticInitialize JOIN(SnP, _StaticInitialize)
30
+ #define SnP_Initialize JOIN(SnP, _Initialize)
31
+ #define SnP_AddByte JOIN(SnP, _AddByte)
32
+ #define SnP_AddBytes JOIN(SnP, _AddBytes)
33
+ #define SnP_ExtractBytes JOIN(SnP, _ExtractBytes)
34
+
35
+ 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
+ {
37
+ ALIGN(SnP_stateAlignment) unsigned char state[SnP_stateSizeInBytes];
38
+ unsigned int partialBlock;
39
+ const unsigned char *curInput = input;
40
+ unsigned char *curOutput = output;
41
+ unsigned int rateInBytes = rate/8;
42
+
43
+ if (rate+capacity != SnP_width)
44
+ return 1;
45
+ if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0))
46
+ return 1;
47
+ if (suffix == 0)
48
+ return 1;
49
+
50
+ /* Initialize the state */
51
+ SnP_StaticInitialize();
52
+ SnP_Initialize(state);
53
+
54
+ /* First, absorb whole blocks */
55
+ #ifdef SnP_FastLoop_Absorb
56
+ if (((rateInBytes % (SnP_width/200)) == 0) && (inputByteLen >= rateInBytes)) {
57
+ /* fast lane: whole lane rate */
58
+ size_t j;
59
+ j = SnP_FastLoop_Absorb(state, rateInBytes/(SnP_width/200), curInput, inputByteLen);
60
+ curInput += j;
61
+ inputByteLen -= j;
62
+ }
63
+ #endif
64
+ while(inputByteLen >= (size_t)rateInBytes) {
65
+ #ifdef KeccakReference
66
+ displayBytes(1, "Block to be absorbed", curInput, rateInBytes);
67
+ #endif
68
+ SnP_AddBytes(state, curInput, 0, rateInBytes);
69
+ SnP_Permute(state);
70
+ curInput += rateInBytes;
71
+ inputByteLen -= rateInBytes;
72
+ }
73
+
74
+ /* Then, absorb what remains */
75
+ partialBlock = (unsigned int)inputByteLen;
76
+ #ifdef KeccakReference
77
+ displayBytes(1, "Block to be absorbed (part)", curInput, partialBlock);
78
+ #endif
79
+ SnP_AddBytes(state, curInput, 0, partialBlock);
80
+
81
+ /* Finally, absorb the suffix */
82
+ #ifdef KeccakReference
83
+ {
84
+ unsigned char delimitedData1[1];
85
+ delimitedData1[0] = suffix;
86
+ displayBytes(1, "Block to be absorbed (last few bits + first bit of padding)", delimitedData1, 1);
87
+ }
88
+ #endif
89
+ /* Last few bits, whose delimiter coincides with first bit of padding */
90
+ SnP_AddByte(state, suffix, partialBlock);
91
+ /* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */
92
+ if ((suffix >= 0x80) && (partialBlock == (rateInBytes-1)))
93
+ SnP_Permute(state);
94
+ /* Second bit of padding */
95
+ SnP_AddByte(state, 0x80, rateInBytes-1);
96
+ #ifdef KeccakReference
97
+ {
98
+ unsigned char block[SnP_width/8];
99
+ memset(block, 0, SnP_width/8);
100
+ block[rateInBytes-1] = 0x80;
101
+ displayBytes(1, "Second bit of padding", block, rateInBytes);
102
+ }
103
+ #endif
104
+ SnP_Permute(state);
105
+ #ifdef KeccakReference
106
+ displayText(1, "--- Switching to squeezing phase ---");
107
+ #endif
108
+
109
+ /* First, output whole blocks */
110
+ while(outputByteLen > (size_t)rateInBytes) {
111
+ SnP_ExtractBytes(state, curOutput, 0, rateInBytes);
112
+ SnP_Permute(state);
113
+ #ifdef KeccakReference
114
+ displayBytes(1, "Squeezed block", curOutput, rateInBytes);
115
+ #endif
116
+ curOutput += rateInBytes;
117
+ outputByteLen -= rateInBytes;
118
+ }
119
+
120
+ /* Finally, output what remains */
121
+ partialBlock = (unsigned int)outputByteLen;
122
+ SnP_ExtractBytes(state, curOutput, 0, partialBlock);
123
+ #ifdef KeccakReference
124
+ displayBytes(1, "Squeezed block (part)", curOutput, partialBlock);
125
+ #endif
126
+
127
+ return 0;
128
+ }
129
+
130
+ /* ---------------------------------------------------------------- */
131
+ /* ---------------------------------------------------------------- */
132
+ /* ---------------------------------------------------------------- */
133
+
134
+ int SpongeInitialize(SpongeInstance *instance, unsigned int rate, unsigned int capacity)
135
+ {
136
+ if (rate+capacity != SnP_width)
137
+ return 1;
138
+ if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0))
139
+ return 1;
140
+ SnP_StaticInitialize();
141
+ SnP_Initialize(instance->state);
142
+ instance->rate = rate;
143
+ instance->byteIOIndex = 0;
144
+ instance->squeezing = 0;
145
+
146
+ return 0;
147
+ }
148
+
149
+ /* ---------------------------------------------------------------- */
150
+
151
+ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dataByteLen)
152
+ {
153
+ size_t i, j;
154
+ unsigned int partialBlock;
155
+ const unsigned char *curData;
156
+ unsigned int rateInBytes = instance->rate/8;
157
+
158
+ if (instance->squeezing)
159
+ return 1; /* Too late for additional input */
160
+
161
+ i = 0;
162
+ curData = data;
163
+ while(i < dataByteLen) {
164
+ if ((instance->byteIOIndex == 0) && (dataByteLen-i >= rateInBytes)) {
165
+ #ifdef SnP_FastLoop_Absorb
166
+ /* processing full blocks first */
167
+ if ((rateInBytes % (SnP_width/200)) == 0) {
168
+ /* fast lane: whole lane rate */
169
+ j = SnP_FastLoop_Absorb(instance->state, rateInBytes/(SnP_width/200), curData, dataByteLen - i);
170
+ i += j;
171
+ curData += j;
172
+ }
173
+ else {
174
+ #endif
175
+ for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
176
+ #ifdef KeccakReference
177
+ displayBytes(1, "Block to be absorbed", curData, rateInBytes);
178
+ #endif
179
+ SnP_AddBytes(instance->state, curData, 0, rateInBytes);
180
+ SnP_Permute(instance->state);
181
+ curData+=rateInBytes;
182
+ }
183
+ i = dataByteLen - j;
184
+ #ifdef SnP_FastLoop_Absorb
185
+ }
186
+ #endif
187
+ }
188
+ else {
189
+ /* normal lane: using the message queue */
190
+ if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
191
+ partialBlock = rateInBytes-instance->byteIOIndex;
192
+ else
193
+ partialBlock = (unsigned int)(dataByteLen - i);
194
+ #ifdef KeccakReference
195
+ displayBytes(1, "Block to be absorbed (part)", curData, partialBlock);
196
+ #endif
197
+ i += partialBlock;
198
+
199
+ SnP_AddBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
200
+ curData += partialBlock;
201
+ instance->byteIOIndex += partialBlock;
202
+ if (instance->byteIOIndex == rateInBytes) {
203
+ SnP_Permute(instance->state);
204
+ instance->byteIOIndex = 0;
205
+ }
206
+ }
207
+ }
208
+ return 0;
209
+ }
210
+
211
+ /* ---------------------------------------------------------------- */
212
+
213
+ int SpongeAbsorbLastFewBits(SpongeInstance *instance, unsigned char delimitedData)
214
+ {
215
+ unsigned int rateInBytes = instance->rate/8;
216
+
217
+ if (delimitedData == 0)
218
+ return 1;
219
+ if (instance->squeezing)
220
+ return 1; /* Too late for additional input */
221
+
222
+ #ifdef KeccakReference
223
+ {
224
+ unsigned char delimitedData1[1];
225
+ delimitedData1[0] = delimitedData;
226
+ displayBytes(1, "Block to be absorbed (last few bits + first bit of padding)", delimitedData1, 1);
227
+ }
228
+ #endif
229
+ /* Last few bits, whose delimiter coincides with first bit of padding */
230
+ SnP_AddByte(instance->state, delimitedData, instance->byteIOIndex);
231
+ /* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */
232
+ if ((delimitedData >= 0x80) && (instance->byteIOIndex == (rateInBytes-1)))
233
+ SnP_Permute(instance->state);
234
+ /* Second bit of padding */
235
+ SnP_AddByte(instance->state, 0x80, rateInBytes-1);
236
+ #ifdef KeccakReference
237
+ {
238
+ unsigned char block[SnP_width/8];
239
+ memset(block, 0, SnP_width/8);
240
+ block[rateInBytes-1] = 0x80;
241
+ displayBytes(1, "Second bit of padding", block, rateInBytes);
242
+ }
243
+ #endif
244
+ SnP_Permute(instance->state);
245
+ instance->byteIOIndex = 0;
246
+ instance->squeezing = 1;
247
+ #ifdef KeccakReference
248
+ displayText(1, "--- Switching to squeezing phase ---");
249
+ #endif
250
+ return 0;
251
+ }
252
+
253
+ /* ---------------------------------------------------------------- */
254
+
255
+ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByteLen)
256
+ {
257
+ size_t i, j;
258
+ unsigned int partialBlock;
259
+ unsigned int rateInBytes = instance->rate/8;
260
+ unsigned char *curData;
261
+
262
+ if (!instance->squeezing)
263
+ SpongeAbsorbLastFewBits(instance, 0x01);
264
+
265
+ i = 0;
266
+ curData = data;
267
+ while(i < dataByteLen) {
268
+ if ((instance->byteIOIndex == rateInBytes) && (dataByteLen-i >= rateInBytes)) {
269
+ for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
270
+ SnP_Permute(instance->state);
271
+ SnP_ExtractBytes(instance->state, curData, 0, rateInBytes);
272
+ #ifdef KeccakReference
273
+ displayBytes(1, "Squeezed block", curData, rateInBytes);
274
+ #endif
275
+ curData+=rateInBytes;
276
+ }
277
+ i = dataByteLen - j;
278
+ }
279
+ else {
280
+ /* normal lane: using the message queue */
281
+ if (instance->byteIOIndex == rateInBytes) {
282
+ SnP_Permute(instance->state);
283
+ instance->byteIOIndex = 0;
284
+ }
285
+ if (dataByteLen-i > rateInBytes-instance->byteIOIndex)
286
+ partialBlock = rateInBytes-instance->byteIOIndex;
287
+ else
288
+ partialBlock = (unsigned int)(dataByteLen - i);
289
+ i += partialBlock;
290
+
291
+ SnP_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
292
+ #ifdef KeccakReference
293
+ displayBytes(1, "Squeezed block (part)", curData, partialBlock);
294
+ #endif
295
+ curData += partialBlock;
296
+ instance->byteIOIndex += partialBlock;
297
+ }
298
+ }
299
+ return 0;
300
+ }
301
+
302
+ /* ---------------------------------------------------------------- */
303
+
304
+ #undef Sponge
305
+ #undef SpongeInstance
306
+ #undef SpongeInitialize
307
+ #undef SpongeAbsorb
308
+ #undef SpongeAbsorbLastFewBits
309
+ #undef SpongeSqueeze
310
+ #undef SnP_stateSizeInBytes
311
+ #undef SnP_stateAlignment
312
+ #undef SnP_StaticInitialize
313
+ #undef SnP_Initialize
314
+ #undef SnP_AddByte
315
+ #undef SnP_AddBytes
316
+ #undef SnP_ExtractBytes