sha3 1.0.5 → 2.1.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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.clang-format +54 -0
- data/.document +3 -3
- data/.rdoc_options +11 -0
- data/.rspec +2 -2
- data/.rubocop.yml +8 -1
- data/CHANGELOG.md +23 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +1 -1
- data/README.md +185 -65
- data/Rakefile +12 -4
- data/certs/io+sha3@jsg.io.pem +26 -0
- data/doc/sha3.rb +83 -0
- data/ext/sha3/config.h +2 -2
- data/ext/sha3/digest.c +726 -169
- data/ext/sha3/digest.h +6 -35
- data/ext/sha3/extconf.rb +42 -38
- data/ext/sha3/kmac.c +504 -0
- data/ext/sha3/kmac.h +14 -0
- data/ext/sha3/lib/high/Keccak/KeccakDuplex.c +81 -0
- data/ext/sha3/lib/high/Keccak/KeccakDuplex.h +73 -0
- data/ext/sha3/lib/high/Keccak/KeccakDuplex.inc +201 -0
- data/ext/sha3/lib/high/Keccak/KeccakSponge.c +2 -18
- data/ext/sha3/lib/high/Keccak/KeccakSponge.h +4 -10
- data/ext/sha3/lib/high/Keccak/KeccakSponge.inc +27 -31
- data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.c +61 -0
- data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.h +67 -0
- data/ext/sha3/lib/high/Keccak/PRG/KeccakPRG.inc +128 -0
- data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.c +93 -0
- data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.h +599 -0
- data/ext/sha3/lib/high/Keccak/SP800-185/SP800-185.inc +573 -0
- data/ext/sha3/lib/high/common/Phases.h +25 -0
- data/ext/sha3/lib/low/KeccakP-1600/common/KeccakP-1600-64.macros +19 -9
- data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-SnP.h +18 -12
- data/ext/sha3/lib/low/KeccakP-1600/ref-32bits/KeccakP-1600-reference32BI.c +28 -36
- data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-SnP.h +18 -12
- data/ext/sha3/lib/low/KeccakP-1600/ref-64bits/KeccakP-1600-reference.c +28 -59
- data/ext/sha3/lib/low/common/PlSnP-Fallback.inc +291 -0
- data/ext/sha3/lib/low/common/SnP-Relaned.h +145 -0
- data/ext/sha3/sha3.c +28 -59
- data/ext/sha3/sha3.h +4 -13
- data/lib/constants.rb +5 -0
- data/lib/sha3.rb +25 -24
- data.tar.gz.sig +0 -0
- metadata +61 -127
- metadata.gz.sig +0 -0
- data/.yardopts +0 -1
- data/ChangeLog.rdoc +0 -27
- data/certs/johanns.pem +0 -25
- data/lib/sha3/doc.rb +0 -121
- data/lib/sha3/version.rb +0 -9
- data/sha3.gemspec +0 -54
- data/tests.sh +0 -29
@@ -0,0 +1,81 @@
|
|
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
|
+
#include "KeccakDuplex.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 KeccakP200
|
28
|
+
#define SnP_width 200
|
29
|
+
#define SnP_Permute KeccakP200_Permute_18rounds
|
30
|
+
#include "KeccakDuplex.inc"
|
31
|
+
#undef prefix
|
32
|
+
#undef SnP
|
33
|
+
#undef SnP_width
|
34
|
+
#undef SnP_Permute
|
35
|
+
#undef SnP_FastLoop_Absorb
|
36
|
+
#endif
|
37
|
+
|
38
|
+
#ifdef XKCP_has_KeccakP400
|
39
|
+
#include "KeccakP-400-SnP.h"
|
40
|
+
|
41
|
+
#define prefix KeccakWidth400
|
42
|
+
#define SnP KeccakP400
|
43
|
+
#define SnP_width 400
|
44
|
+
#define SnP_Permute KeccakP400_Permute_20rounds
|
45
|
+
#include "KeccakDuplex.inc"
|
46
|
+
#undef prefix
|
47
|
+
#undef SnP
|
48
|
+
#undef SnP_width
|
49
|
+
#undef SnP_Permute
|
50
|
+
#undef SnP_FastLoop_Absorb
|
51
|
+
#endif
|
52
|
+
|
53
|
+
#ifdef XKCP_has_KeccakP800
|
54
|
+
#include "KeccakP-800-SnP.h"
|
55
|
+
|
56
|
+
#define prefix KeccakWidth800
|
57
|
+
#define SnP KeccakP800
|
58
|
+
#define SnP_width 800
|
59
|
+
#define SnP_Permute KeccakP800_Permute_22rounds
|
60
|
+
#include "KeccakDuplex.inc"
|
61
|
+
#undef prefix
|
62
|
+
#undef SnP
|
63
|
+
#undef SnP_width
|
64
|
+
#undef SnP_Permute
|
65
|
+
#undef SnP_FastLoop_Absorb
|
66
|
+
#endif
|
67
|
+
|
68
|
+
#ifdef XKCP_has_KeccakP1600
|
69
|
+
#include "KeccakP-1600-SnP.h"
|
70
|
+
|
71
|
+
#define prefix KeccakWidth1600
|
72
|
+
#define SnP KeccakP1600
|
73
|
+
#define SnP_width 1600
|
74
|
+
#define SnP_Permute KeccakP1600_Permute_24rounds
|
75
|
+
#include "KeccakDuplex.inc"
|
76
|
+
#undef prefix
|
77
|
+
#undef SnP
|
78
|
+
#undef SnP_width
|
79
|
+
#undef SnP_Permute
|
80
|
+
#undef SnP_FastLoop_Absorb
|
81
|
+
#endif
|
@@ -0,0 +1,73 @@
|
|
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 _KeccakDuplex_h_
|
18
|
+
#define _KeccakDuplex_h_
|
19
|
+
|
20
|
+
/* For the documentation, please follow the link: */
|
21
|
+
/* #include "KeccakDuplex-documentation.h" */
|
22
|
+
|
23
|
+
#include <string.h>
|
24
|
+
#include "align.h"
|
25
|
+
#include "config.h"
|
26
|
+
|
27
|
+
#define XKCP_DeclareDuplexStructure(prefix, state_t) \
|
28
|
+
typedef struct prefix##_DuplexInstanceStruct { \
|
29
|
+
state_t state; \
|
30
|
+
unsigned int rate; \
|
31
|
+
unsigned int byteInputIndex; \
|
32
|
+
unsigned int byteOutputIndex; \
|
33
|
+
} prefix##_DuplexInstance;
|
34
|
+
|
35
|
+
#define XKCP_DeclareDuplexFunctions(prefix) \
|
36
|
+
int prefix##_DuplexInitialize(prefix##_DuplexInstance *duplexInstance, unsigned int rate, unsigned int capacity); \
|
37
|
+
int prefix##_Duplexing(prefix##_DuplexInstance *duplexInstance, const unsigned char *sigmaBegin, unsigned int sigmaBeginByteLen, unsigned char *Z, unsigned int ZByteLen, unsigned char delimitedSigmaEnd); \
|
38
|
+
int prefix##_DuplexingFeedPartialInput(prefix##_DuplexInstance *duplexInstance, const unsigned char *input, unsigned int inputByteLen); \
|
39
|
+
int prefix##_DuplexingFeedZeroes(prefix##_DuplexInstance *duplexInstance, unsigned int inputByteLen); \
|
40
|
+
int prefix##_DuplexingOverwritePartialInput(prefix##_DuplexInstance *duplexInstance, const unsigned char *input, unsigned int inputByteLen); \
|
41
|
+
int prefix##_DuplexingOverwriteWithZeroes(prefix##_DuplexInstance *duplexInstance, unsigned int inputByteLen); \
|
42
|
+
int prefix##_DuplexingGetFurtherOutput(prefix##_DuplexInstance *duplexInstance, unsigned char *out, unsigned int outByteLen); \
|
43
|
+
int prefix##_DuplexingGetFurtherOutputAndAdd(prefix##_DuplexInstance *duplexInstance, const unsigned char *input, unsigned char *output, unsigned int outputByteLen);
|
44
|
+
|
45
|
+
#ifdef XKCP_has_KeccakP200
|
46
|
+
#include "KeccakP-200-SnP.h"
|
47
|
+
XKCP_DeclareDuplexStructure(KeccakWidth200, KeccakP200_stateSizeInBytes, KeccakP200_stateAlignment)
|
48
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth200)
|
49
|
+
#define XKCP_has_Duplex_Keccak_width200
|
50
|
+
#endif
|
51
|
+
|
52
|
+
#ifdef XKCP_has_KeccakP400
|
53
|
+
#include "KeccakP-400-SnP.h"
|
54
|
+
XKCP_DeclareDuplexStructure(KeccakWidth400, KeccakP400_stateSizeInBytes, KeccakP400_stateAlignment)
|
55
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth400)
|
56
|
+
#define XKCP_has_Duplex_Keccak_width400
|
57
|
+
#endif
|
58
|
+
|
59
|
+
#ifdef XKCP_has_KeccakP800
|
60
|
+
#include "KeccakP-800-SnP.h"
|
61
|
+
XKCP_DeclareDuplexStructure(KeccakWidth800, KeccakP800_stateSizeInBytes, KeccakP800_stateAlignment)
|
62
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth800)
|
63
|
+
#define XKCP_has_Duplex_Keccak_width800
|
64
|
+
#endif
|
65
|
+
|
66
|
+
#ifdef XKCP_has_KeccakP1600
|
67
|
+
#include "KeccakP-1600-SnP.h"
|
68
|
+
XKCP_DeclareDuplexStructure(KeccakWidth1600, KeccakP1600_state)
|
69
|
+
XKCP_DeclareDuplexFunctions(KeccakWidth1600)
|
70
|
+
#define XKCP_has_Duplex_Keccak_width1600
|
71
|
+
#endif
|
72
|
+
|
73
|
+
#endif
|
@@ -0,0 +1,201 @@
|
|
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 DuplexInstance JOIN(prefix, _DuplexInstance)
|
21
|
+
#define DuplexInitialize JOIN(prefix, _DuplexInitialize)
|
22
|
+
#define Duplexing JOIN(prefix, _Duplexing)
|
23
|
+
#define DuplexingFeedPartialInput JOIN(prefix, _DuplexingFeedPartialInput)
|
24
|
+
#define DuplexingFeedZeroes JOIN(prefix, _DuplexingFeedZeroes)
|
25
|
+
#define DuplexingOverwritePartialInput JOIN(prefix, _DuplexingOverwritePartialInput)
|
26
|
+
#define DuplexingOverwriteWithZeroes JOIN(prefix, _DuplexingOverwriteWithZeroes)
|
27
|
+
#define DuplexingGetFurtherOutput JOIN(prefix, _DuplexingGetFurtherOutput)
|
28
|
+
#define DuplexingGetFurtherOutputAndAdd JOIN(prefix, _DuplexingGetFurtherOutputAndAdd)
|
29
|
+
|
30
|
+
#define SnP_StaticInitialize JOIN(SnP, _StaticInitialize)
|
31
|
+
#define SnP_Initialize JOIN(SnP, _Initialize)
|
32
|
+
#define SnP_AddByte JOIN(SnP, _AddByte)
|
33
|
+
#define SnP_AddBytes JOIN(SnP, _AddBytes)
|
34
|
+
#define SnP_OverwriteBytes JOIN(SnP, _OverwriteBytes)
|
35
|
+
#define SnP_OverwriteWithZeroes JOIN(SnP, _OverwriteWithZeroes)
|
36
|
+
#define SnP_ExtractBytes JOIN(SnP, _ExtractBytes)
|
37
|
+
#define SnP_ExtractAndAddBytes JOIN(SnP, _ExtractAndAddBytes)
|
38
|
+
|
39
|
+
int DuplexInitialize(DuplexInstance *instance, unsigned int rate, unsigned int capacity)
|
40
|
+
{
|
41
|
+
if (rate+capacity != SnP_width)
|
42
|
+
return 1;
|
43
|
+
if ((rate <= 2) || (rate > SnP_width))
|
44
|
+
return 1;
|
45
|
+
SnP_StaticInitialize();
|
46
|
+
instance->rate = rate;
|
47
|
+
SnP_Initialize(&instance->state);
|
48
|
+
instance->byteInputIndex = 0;
|
49
|
+
instance->byteOutputIndex = (instance->rate+7)/8;
|
50
|
+
return 0;
|
51
|
+
}
|
52
|
+
|
53
|
+
int Duplexing(DuplexInstance *instance, const unsigned char *sigmaBegin, unsigned int sigmaBeginByteLen, unsigned char *Z, unsigned int ZByteLen, unsigned char delimitedSigmaEnd)
|
54
|
+
{
|
55
|
+
const unsigned int rho_max = instance->rate - 2;
|
56
|
+
|
57
|
+
if (delimitedSigmaEnd == 0)
|
58
|
+
return 1;
|
59
|
+
if (sigmaBeginByteLen > rho_max/8)
|
60
|
+
return 1;
|
61
|
+
if ((instance->byteInputIndex+sigmaBeginByteLen)*8 > rho_max)
|
62
|
+
return 1;
|
63
|
+
if (rho_max - sigmaBeginByteLen*8 < 7) {
|
64
|
+
unsigned int maxBitsInDelimitedSigmaEnd = rho_max - sigmaBeginByteLen*8;
|
65
|
+
if (delimitedSigmaEnd >= (1 << (maxBitsInDelimitedSigmaEnd+1)))
|
66
|
+
return 1;
|
67
|
+
}
|
68
|
+
if (ZByteLen > (instance->rate+7)/8)
|
69
|
+
return 1; /* The output length must not be greater than the rate (rounded up to a byte) */
|
70
|
+
|
71
|
+
SnP_AddBytes(&instance->state, sigmaBegin, instance->byteInputIndex, sigmaBeginByteLen);
|
72
|
+
#ifdef KeccakReference
|
73
|
+
{
|
74
|
+
unsigned char block[SnP_width/8];
|
75
|
+
memcpy(block, sigmaBegin, sigmaBeginByteLen);
|
76
|
+
block[sigmaBeginByteLen] = delimitedSigmaEnd;
|
77
|
+
memset(block+sigmaBeginByteLen+1, 0, sizeof(block)-sigmaBeginByteLen-1);
|
78
|
+
block[(instance->rate-1)/8] |= 1 << ((instance->rate-1) % 8);
|
79
|
+
displayBytes(1, "Block to be absorbed (after padding)", block, (instance->rate+7)/8);
|
80
|
+
}
|
81
|
+
#endif
|
82
|
+
|
83
|
+
/* Last few bits, whose delimiter coincides with first bit of padding */
|
84
|
+
SnP_AddByte(&instance->state, delimitedSigmaEnd, instance->byteInputIndex+sigmaBeginByteLen);
|
85
|
+
/* Second bit of padding */
|
86
|
+
SnP_AddByte(&instance->state, (unsigned char)1 << ((instance->rate - 1)%8), (instance->rate - 1)/8);
|
87
|
+
SnP_Permute(&instance->state);
|
88
|
+
SnP_ExtractBytes(&instance->state, Z, 0, ZByteLen);
|
89
|
+
|
90
|
+
if (ZByteLen*8 > instance->rate) {
|
91
|
+
unsigned char mask = (unsigned char)(1 << (instance->rate % 8)) - 1;
|
92
|
+
Z[ZByteLen-1] &= mask;
|
93
|
+
}
|
94
|
+
|
95
|
+
instance->byteInputIndex = 0;
|
96
|
+
instance->byteOutputIndex = ZByteLen;
|
97
|
+
|
98
|
+
return 0;
|
99
|
+
}
|
100
|
+
|
101
|
+
int DuplexingFeedPartialInput(DuplexInstance *instance, const unsigned char *input, unsigned int inputByteLen)
|
102
|
+
{
|
103
|
+
const unsigned int rho_max = instance->rate - 2;
|
104
|
+
|
105
|
+
if (inputByteLen > rho_max/8)
|
106
|
+
return 1;
|
107
|
+
if ((instance->byteInputIndex+inputByteLen)*8 > rho_max)
|
108
|
+
return 1;
|
109
|
+
|
110
|
+
SnP_AddBytes(&instance->state, input, instance->byteInputIndex, inputByteLen);
|
111
|
+
instance->byteInputIndex += inputByteLen;
|
112
|
+
return 0;
|
113
|
+
}
|
114
|
+
|
115
|
+
int DuplexingFeedZeroes(DuplexInstance *instance, unsigned int inputByteLen)
|
116
|
+
{
|
117
|
+
const unsigned int rho_max = instance->rate - 2;
|
118
|
+
|
119
|
+
if (inputByteLen > rho_max/8)
|
120
|
+
return 1;
|
121
|
+
if ((instance->byteInputIndex+inputByteLen)*8 > rho_max)
|
122
|
+
return 1;
|
123
|
+
|
124
|
+
instance->byteInputIndex += inputByteLen;
|
125
|
+
return 0;
|
126
|
+
}
|
127
|
+
|
128
|
+
int DuplexingOverwritePartialInput(DuplexInstance *instance, const unsigned char *input, unsigned int inputByteLen)
|
129
|
+
{
|
130
|
+
const unsigned int rho_max = instance->rate - 2;
|
131
|
+
|
132
|
+
if (inputByteLen > rho_max/8)
|
133
|
+
return 1;
|
134
|
+
if ((instance->byteInputIndex+inputByteLen)*8 > rho_max)
|
135
|
+
return 1;
|
136
|
+
|
137
|
+
SnP_OverwriteBytes(&instance->state, input, instance->byteInputIndex, inputByteLen);
|
138
|
+
instance->byteInputIndex += inputByteLen;
|
139
|
+
return 0;
|
140
|
+
}
|
141
|
+
|
142
|
+
int DuplexingOverwriteWithZeroes(DuplexInstance *instance, unsigned int inputByteLen)
|
143
|
+
{
|
144
|
+
const unsigned int rho_max = instance->rate - 2;
|
145
|
+
|
146
|
+
if (inputByteLen > rho_max/8)
|
147
|
+
return 1;
|
148
|
+
if ((instance->byteInputIndex != 0) || (inputByteLen*8 > rho_max))
|
149
|
+
return 1;
|
150
|
+
|
151
|
+
SnP_OverwriteWithZeroes(&instance->state, inputByteLen);
|
152
|
+
instance->byteInputIndex = inputByteLen;
|
153
|
+
|
154
|
+
return 0;
|
155
|
+
}
|
156
|
+
|
157
|
+
int DuplexingGetFurtherOutput(DuplexInstance *instance, unsigned char *output, unsigned int outputByteLen)
|
158
|
+
{
|
159
|
+
if (outputByteLen > (instance->rate+7)/8 - instance->byteOutputIndex)
|
160
|
+
return 1; /* The output length must not be greater than the rate (rounded up to a byte) */
|
161
|
+
|
162
|
+
SnP_ExtractBytes(&instance->state, output, instance->byteOutputIndex, outputByteLen);
|
163
|
+
instance->byteOutputIndex += outputByteLen;
|
164
|
+
if (instance->byteOutputIndex*8 > instance->rate) {
|
165
|
+
unsigned char mask = (1 << (instance->rate % 8)) - 1;
|
166
|
+
output[outputByteLen-1] &= mask;
|
167
|
+
}
|
168
|
+
return 0;
|
169
|
+
}
|
170
|
+
|
171
|
+
int DuplexingGetFurtherOutputAndAdd(DuplexInstance *instance, const unsigned char *input, unsigned char *output, unsigned int outputByteLen)
|
172
|
+
{
|
173
|
+
if (outputByteLen > (instance->rate+7)/8 - instance->byteOutputIndex)
|
174
|
+
return 1; /* The output length must not be greater than the rate (rounded up to a byte) */
|
175
|
+
|
176
|
+
SnP_ExtractAndAddBytes(&instance->state, input, output, instance->byteOutputIndex, outputByteLen);
|
177
|
+
instance->byteOutputIndex += outputByteLen;
|
178
|
+
if (instance->byteOutputIndex*8 > instance->rate) {
|
179
|
+
unsigned char mask = (1 << (instance->rate % 8)) - 1;
|
180
|
+
output[outputByteLen-1] &= mask;
|
181
|
+
}
|
182
|
+
return 0;
|
183
|
+
}
|
184
|
+
|
185
|
+
#undef DuplexInstance
|
186
|
+
#undef DuplexInitialize
|
187
|
+
#undef Duplexing
|
188
|
+
#undef DuplexingFeedPartialInput
|
189
|
+
#undef DuplexingFeedZeroes
|
190
|
+
#undef DuplexingOverwritePartialInput
|
191
|
+
#undef DuplexingOverwriteWithZeroes
|
192
|
+
#undef DuplexingGetFurtherOutput
|
193
|
+
#undef DuplexingGetFurtherOutputAndAdd
|
194
|
+
#undef SnP_StaticInitialize
|
195
|
+
#undef SnP_Initialize
|
196
|
+
#undef SnP_AddByte
|
197
|
+
#undef SnP_AddBytes
|
198
|
+
#undef SnP_OverwriteBytes
|
199
|
+
#undef SnP_OverwriteWithZeroes
|
200
|
+
#undef SnP_ExtractBytes
|
201
|
+
#undef SnP_ExtractAndAddBytes
|
@@ -79,6 +79,7 @@ http://creativecommons.org/publicdomain/zero/1.0/
|
|
79
79
|
|
80
80
|
#define prefix KeccakWidth1600
|
81
81
|
#define SnP KeccakP1600
|
82
|
+
#define SnP_state KeccakP1600_state
|
82
83
|
#define SnP_width 1600
|
83
84
|
#define SnP_Permute KeccakP1600_Permute_24rounds
|
84
85
|
#if defined(KeccakF1600_FastLoop_supported)
|
@@ -87,24 +88,7 @@ http://creativecommons.org/publicdomain/zero/1.0/
|
|
87
88
|
#include "KeccakSponge.inc"
|
88
89
|
#undef prefix
|
89
90
|
#undef SnP
|
90
|
-
#undef
|
91
|
-
#undef SnP_Permute
|
92
|
-
#undef SnP_FastLoop_Absorb
|
93
|
-
#endif
|
94
|
-
|
95
|
-
#ifdef XKCP_has_KeccakP1600
|
96
|
-
#include "KeccakP-1600-SnP.h"
|
97
|
-
|
98
|
-
#define prefix KeccakWidth1600_12rounds
|
99
|
-
#define SnP KeccakP1600
|
100
|
-
#define SnP_width 1600
|
101
|
-
#define SnP_Permute KeccakP1600_Permute_12rounds
|
102
|
-
#if defined(KeccakP1600_12rounds_FastLoop_supported)
|
103
|
-
#define SnP_FastLoop_Absorb KeccakP1600_12rounds_FastLoop_Absorb
|
104
|
-
#endif
|
105
|
-
#include "KeccakSponge.inc"
|
106
|
-
#undef prefix
|
107
|
-
#undef SnP
|
91
|
+
#undef SnP_state
|
108
92
|
#undef SnP_width
|
109
93
|
#undef SnP_Permute
|
110
94
|
#undef SnP_FastLoop_Absorb
|
@@ -24,9 +24,9 @@ http://creativecommons.org/publicdomain/zero/1.0/
|
|
24
24
|
#include "align.h"
|
25
25
|
#include "config.h"
|
26
26
|
|
27
|
-
#define XKCP_DeclareSpongeStructure(prefix,
|
28
|
-
|
29
|
-
|
27
|
+
#define XKCP_DeclareSpongeStructure(prefix, state_t) \
|
28
|
+
typedef struct prefix##_SpongeInstanceStruct { \
|
29
|
+
state_t state; \
|
30
30
|
unsigned int rate; \
|
31
31
|
unsigned int byteIOIndex; \
|
32
32
|
int squeezing; \
|
@@ -62,15 +62,9 @@ http://creativecommons.org/publicdomain/zero/1.0/
|
|
62
62
|
|
63
63
|
#ifdef XKCP_has_KeccakP1600
|
64
64
|
#include "KeccakP-1600-SnP.h"
|
65
|
-
XKCP_DeclareSpongeStructure(KeccakWidth1600,
|
65
|
+
XKCP_DeclareSpongeStructure(KeccakWidth1600, KeccakP1600_state)
|
66
66
|
XKCP_DeclareSpongeFunctions(KeccakWidth1600)
|
67
67
|
#define XKCP_has_Sponge_Keccak_width1600
|
68
68
|
#endif
|
69
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
70
|
#endif
|
@@ -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
|
-
|
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
|