digest-kangarootwelve 0.0.1

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.
@@ -0,0 +1,45 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #ifndef _KeccakP_1600_times2_SnP_h_
17
+ #define _KeccakP_1600_times2_SnP_h_
18
+
19
+ /** For the documentation, see PlSnP-documentation.h.
20
+ */
21
+
22
+ #include "KeccakP-1600-SnP.h"
23
+
24
+ #define KeccakP1600times2_implementation "fallback on serial implementation (" KeccakP1600_implementation ")"
25
+ #define KeccakP1600times2_statesSizeInBytes (((KeccakP1600_stateSizeInBytes+(KeccakP1600_stateAlignment-1))/KeccakP1600_stateAlignment)*KeccakP1600_stateAlignment*2)
26
+ #define KeccakP1600times2_statesAlignment KeccakP1600_stateAlignment
27
+
28
+ void KeccakP1600times2_StaticInitialize( void );
29
+ void KeccakP1600times2_InitializeAll(void *states);
30
+ void KeccakP1600times2_AddByte(void *states, unsigned int instanceIndex, unsigned char data, unsigned int offset);
31
+ void KeccakP1600times2_AddBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length);
32
+ void KeccakP1600times2_AddLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
33
+ void KeccakP1600times2_OverwriteBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length);
34
+ void KeccakP1600times2_OverwriteLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
35
+ void KeccakP1600times2_OverwriteWithZeroes(void *states, unsigned int instanceIndex, unsigned int byteCount);
36
+ void KeccakP1600times2_PermuteAll_4rounds(void *states);
37
+ void KeccakP1600times2_PermuteAll_6rounds(void *states);
38
+ void KeccakP1600times2_PermuteAll_12rounds(void *states);
39
+ void KeccakP1600times2_PermuteAll_24rounds(void *states);
40
+ void KeccakP1600times2_ExtractBytes(const void *states, unsigned int instanceIndex, unsigned char *data, unsigned int offset, unsigned int length);
41
+ void KeccakP1600times2_ExtractLanesAll(const void *states, unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
42
+ void KeccakP1600times2_ExtractAndAddBytes(const void *states, unsigned int instanceIndex, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length);
43
+ void KeccakP1600times2_ExtractAndAddLanesAll(const void *states, const unsigned char *input, unsigned char *output, unsigned int laneCount, unsigned int laneOffset);
44
+
45
+ #endif
@@ -0,0 +1,31 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #include "KeccakP-1600-SnP.h"
17
+
18
+ #define prefix KeccakP1600times2
19
+ #define PlSnP_baseParallelism 1
20
+ #define PlSnP_targetParallelism 2
21
+ #define SnP_laneLengthInBytes 8
22
+ #define SnP KeccakP1600
23
+ #define SnP_Permute KeccakP1600_Permute_24rounds
24
+ #define SnP_Permute_12rounds KeccakP1600_Permute_12rounds
25
+ #define SnP_Permute_Nrounds KeccakP1600_Permute_Nrounds
26
+ #define PlSnP_PermuteAll KeccakP1600times2_PermuteAll_24rounds
27
+ #define PlSnP_PermuteAll_12rounds KeccakP1600times2_PermuteAll_12rounds
28
+ #define PlSnP_PermuteAll_6rounds KeccakP1600times2_PermuteAll_6rounds
29
+ #define PlSnP_PermuteAll_4rounds KeccakP1600times2_PermuteAll_4rounds
30
+
31
+ #include "PlSnP-Fallback.inc"
@@ -0,0 +1,45 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #ifndef _KeccakP_1600_times4_SnP_h_
17
+ #define _KeccakP_1600_times4_SnP_h_
18
+
19
+ /** For the documentation, see PlSnP-documentation.h.
20
+ */
21
+
22
+ #include "KeccakP-1600-SnP.h"
23
+
24
+ #define KeccakP1600times4_implementation "fallback on serial implementation (" KeccakP1600_implementation ")"
25
+ #define KeccakP1600times4_statesSizeInBytes (((KeccakP1600_stateSizeInBytes+(KeccakP1600_stateAlignment-1))/KeccakP1600_stateAlignment)*KeccakP1600_stateAlignment*4)
26
+ #define KeccakP1600times4_statesAlignment KeccakP1600_stateAlignment
27
+
28
+ void KeccakP1600times4_StaticInitialize( void );
29
+ void KeccakP1600times4_InitializeAll(void *states);
30
+ void KeccakP1600times4_AddByte(void *states, unsigned int instanceIndex, unsigned char data, unsigned int offset);
31
+ void KeccakP1600times4_AddBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length);
32
+ void KeccakP1600times4_AddLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
33
+ void KeccakP1600times4_OverwriteBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length);
34
+ void KeccakP1600times4_OverwriteLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
35
+ void KeccakP1600times4_OverwriteWithZeroes(void *states, unsigned int instanceIndex, unsigned int byteCount);
36
+ void KeccakP1600times4_PermuteAll_4rounds(void *states);
37
+ void KeccakP1600times4_PermuteAll_6rounds(void *states);
38
+ void KeccakP1600times4_PermuteAll_12rounds(void *states);
39
+ void KeccakP1600times4_PermuteAll_24rounds(void *states);
40
+ void KeccakP1600times4_ExtractBytes(const void *states, unsigned int instanceIndex, unsigned char *data, unsigned int offset, unsigned int length);
41
+ void KeccakP1600times4_ExtractLanesAll(const void *states, unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
42
+ void KeccakP1600times4_ExtractAndAddBytes(const void *states, unsigned int instanceIndex, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length);
43
+ void KeccakP1600times4_ExtractAndAddLanesAll(const void *states, const unsigned char *input, unsigned char *output, unsigned int laneCount, unsigned int laneOffset);
44
+
45
+ #endif
@@ -0,0 +1,31 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #include "KeccakP-1600-SnP.h"
17
+
18
+ #define prefix KeccakP1600times4
19
+ #define PlSnP_baseParallelism 1
20
+ #define PlSnP_targetParallelism 4
21
+ #define SnP_laneLengthInBytes 8
22
+ #define SnP KeccakP1600
23
+ #define SnP_Permute KeccakP1600_Permute_24rounds
24
+ #define SnP_Permute_12rounds KeccakP1600_Permute_12rounds
25
+ #define SnP_Permute_Nrounds KeccakP1600_Permute_Nrounds
26
+ #define PlSnP_PermuteAll KeccakP1600times4_PermuteAll_24rounds
27
+ #define PlSnP_PermuteAll_12rounds KeccakP1600times4_PermuteAll_12rounds
28
+ #define PlSnP_PermuteAll_6rounds KeccakP1600times4_PermuteAll_6rounds
29
+ #define PlSnP_PermuteAll_4rounds KeccakP1600times4_PermuteAll_4rounds
30
+
31
+ #include "PlSnP-Fallback.inc"
@@ -0,0 +1,45 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #ifndef _KeccakP_1600_times8_SnP_h_
17
+ #define _KeccakP_1600_times8_SnP_h_
18
+
19
+ /** For the documentation, see PlSnP-documentation.h.
20
+ */
21
+
22
+ #include "KeccakP-1600-SnP.h"
23
+
24
+ #define KeccakP1600times8_implementation "fallback on serial implementation (" KeccakP1600_implementation ")"
25
+ #define KeccakP1600times8_statesSizeInBytes (((KeccakP1600_stateSizeInBytes+(KeccakP1600_stateAlignment-1))/KeccakP1600_stateAlignment)*KeccakP1600_stateAlignment*8)
26
+ #define KeccakP1600times8_statesAlignment KeccakP1600_stateAlignment
27
+
28
+ void KeccakP1600times8_StaticInitialize( void );
29
+ void KeccakP1600times8_InitializeAll(void *states);
30
+ void KeccakP1600times8_AddByte(void *states, unsigned int instanceIndex, unsigned char data, unsigned int offset);
31
+ void KeccakP1600times8_AddBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length);
32
+ void KeccakP1600times8_AddLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
33
+ void KeccakP1600times8_OverwriteBytes(void *states, unsigned int instanceIndex, const unsigned char *data, unsigned int offset, unsigned int length);
34
+ void KeccakP1600times8_OverwriteLanesAll(void *states, const unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
35
+ void KeccakP1600times8_OverwriteWithZeroes(void *states, unsigned int instanceIndex, unsigned int byteCount);
36
+ void KeccakP1600times8_PermuteAll_4rounds(void *states);
37
+ void KeccakP1600times8_PermuteAll_6rounds(void *states);
38
+ void KeccakP1600times8_PermuteAll_12rounds(void *states);
39
+ void KeccakP1600times8_PermuteAll_24rounds(void *states);
40
+ void KeccakP1600times8_ExtractBytes(const void *states, unsigned int instanceIndex, unsigned char *data, unsigned int offset, unsigned int length);
41
+ void KeccakP1600times8_ExtractLanesAll(const void *states, unsigned char *data, unsigned int laneCount, unsigned int laneOffset);
42
+ void KeccakP1600times8_ExtractAndAddBytes(const void *states, unsigned int instanceIndex, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length);
43
+ void KeccakP1600times8_ExtractAndAddLanesAll(const void *states, const unsigned char *input, unsigned char *output, unsigned int laneCount, unsigned int laneOffset);
44
+
45
+ #endif
@@ -0,0 +1,31 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #include "KeccakP-1600-SnP.h"
17
+
18
+ #define prefix KeccakP1600times8
19
+ #define PlSnP_baseParallelism 1
20
+ #define PlSnP_targetParallelism 8
21
+ #define SnP_laneLengthInBytes 8
22
+ #define SnP KeccakP1600
23
+ #define SnP_Permute KeccakP1600_Permute_24rounds
24
+ #define SnP_Permute_12rounds KeccakP1600_Permute_12rounds
25
+ #define SnP_Permute_Nrounds KeccakP1600_Permute_Nrounds
26
+ #define PlSnP_PermuteAll KeccakP1600times8_PermuteAll_24rounds
27
+ #define PlSnP_PermuteAll_12rounds KeccakP1600times8_PermuteAll_12rounds
28
+ #define PlSnP_PermuteAll_6rounds KeccakP1600times8_PermuteAll_6rounds
29
+ #define PlSnP_PermuteAll_4rounds KeccakP1600times8_PermuteAll_4rounds
30
+
31
+ #include "PlSnP-Fallback.inc"
@@ -0,0 +1,37 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #ifndef _KeccakSpongeCommon_h_
17
+ #define _KeccakSpongeCommon_h_
18
+
19
+ #include <string.h>
20
+ #include "align.h"
21
+
22
+ #define KCP_DeclareSpongeStructure(prefix, size, alignment) \
23
+ ALIGN(alignment) typedef struct prefix##_SpongeInstanceStruct { \
24
+ unsigned char state[size]; \
25
+ unsigned int rate; \
26
+ unsigned int byteIOIndex; \
27
+ int squeezing; \
28
+ } prefix##_SpongeInstance;
29
+
30
+ #define KCP_DeclareSpongeFunctions(prefix) \
31
+ 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); \
32
+ int prefix##_SpongeInitialize(prefix##_SpongeInstance *spongeInstance, unsigned int rate, unsigned int capacity); \
33
+ int prefix##_SpongeAbsorb(prefix##_SpongeInstance *spongeInstance, const unsigned char *data, size_t dataByteLen); \
34
+ int prefix##_SpongeAbsorbLastFewBits(prefix##_SpongeInstance *spongeInstance, unsigned char delimitedData); \
35
+ int prefix##_SpongeSqueeze(prefix##_SpongeInstance *spongeInstance, unsigned char *data, size_t dataByteLen);
36
+
37
+ #endif
@@ -0,0 +1,313 @@
1
+ /*
2
+ Implementation by the Keccak, Keyak and Ketje Teams, namely, Guido Bertoni,
3
+ Joan Daemen, Michaël Peeters, Gilles Van Assche and Ronny Van Keer, hereby
4
+ denoted as "the implementer".
5
+
6
+ For more information, feedback or questions, please refer to our websites:
7
+ http://keccak.noekeon.org/
8
+ http://keyak.noekeon.org/
9
+ http://ketje.noekeon.org/
10
+
11
+ To the extent possible under law, the implementer has waived all copyright
12
+ and related or neighboring rights to the source code in this file.
13
+ http://creativecommons.org/publicdomain/zero/1.0/
14
+ */
15
+
16
+ #define JOIN0(a, b) a ## b
17
+ #define JOIN(a, b) JOIN0(a, b)
18
+
19
+ #define Sponge JOIN(prefix, _Sponge)
20
+ #define SpongeInstance JOIN(prefix, _SpongeInstance)
21
+ #define SpongeInitialize JOIN(prefix, _SpongeInitialize)
22
+ #define SpongeAbsorb JOIN(prefix, _SpongeAbsorb)
23
+ #define SpongeAbsorbLastFewBits JOIN(prefix, _SpongeAbsorbLastFewBits)
24
+ #define SpongeSqueeze JOIN(prefix, _SpongeSqueeze)
25
+
26
+ #define SnP_stateSizeInBytes JOIN(SnP, _stateSizeInBytes)
27
+ #define SnP_stateAlignment JOIN(SnP, _stateAlignment)
28
+ #define SnP_StaticInitialize JOIN(SnP, _StaticInitialize)
29
+ #define SnP_Initialize JOIN(SnP, _Initialize)
30
+ #define SnP_AddByte JOIN(SnP, _AddByte)
31
+ #define SnP_AddBytes JOIN(SnP, _AddBytes)
32
+ #define SnP_ExtractBytes JOIN(SnP, _ExtractBytes)
33
+
34
+ int Sponge(unsigned int rate, unsigned int capacity, const unsigned char *input, size_t inputByteLen, unsigned char suffix, unsigned char *output, size_t outputByteLen)
35
+ {
36
+ ALIGN(SnP_stateAlignment) unsigned char state[SnP_stateSizeInBytes];
37
+ unsigned int partialBlock;
38
+ const unsigned char *curInput = input;
39
+ unsigned char *curOutput = output;
40
+ unsigned int rateInBytes = rate/8;
41
+
42
+ if (rate+capacity != SnP_width)
43
+ return 1;
44
+ if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0))
45
+ return 1;
46
+ if (suffix == 0)
47
+ return 1;
48
+
49
+ /* Initialize the state */
50
+ SnP_StaticInitialize();
51
+ SnP_Initialize(state);
52
+
53
+ /* First, absorb whole blocks */
54
+ #ifdef SnP_FastLoop_Absorb
55
+ if (((rateInBytes % (SnP_width/200)) == 0) && (inputByteLen >= rateInBytes)) {
56
+ /* fast lane: whole lane rate */
57
+ size_t j;
58
+ j = SnP_FastLoop_Absorb(state, rateInBytes/(SnP_width/200), curInput, inputByteLen);
59
+ curInput += j;
60
+ inputByteLen -= j;
61
+ }
62
+ #endif
63
+ while(inputByteLen >= (size_t)rateInBytes) {
64
+ #ifdef KeccakReference
65
+ displayBytes(1, "Block to be absorbed", curInput, rateInBytes);
66
+ #endif
67
+ SnP_AddBytes(state, curInput, 0, rateInBytes);
68
+ SnP_Permute(state);
69
+ curInput += rateInBytes;
70
+ inputByteLen -= rateInBytes;
71
+ }
72
+
73
+ /* Then, absorb what remains */
74
+ partialBlock = (unsigned int)inputByteLen;
75
+ #ifdef KeccakReference
76
+ displayBytes(1, "Block to be absorbed (part)", curInput, partialBlock);
77
+ #endif
78
+ SnP_AddBytes(state, curInput, 0, partialBlock);
79
+
80
+ /* Finally, absorb the suffix */
81
+ #ifdef KeccakReference
82
+ {
83
+ unsigned char delimitedData1[1];
84
+ delimitedData1[0] = suffix;
85
+ displayBytes(1, "Block to be absorbed (last few bits + first bit of padding)", delimitedData1, 1);
86
+ }
87
+ #endif
88
+ /* Last few bits, whose delimiter coincides with first bit of padding */
89
+ SnP_AddByte(state, suffix, partialBlock);
90
+ /* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */
91
+ if ((suffix >= 0x80) && (partialBlock == (rateInBytes-1)))
92
+ SnP_Permute(state);
93
+ /* Second bit of padding */
94
+ SnP_AddByte(state, 0x80, rateInBytes-1);
95
+ #ifdef KeccakReference
96
+ {
97
+ unsigned char block[SnP_width/8];
98
+ memset(block, 0, SnP_width/8);
99
+ block[rateInBytes-1] = 0x80;
100
+ displayBytes(1, "Second bit of padding", block, rateInBytes);
101
+ }
102
+ #endif
103
+ SnP_Permute(state);
104
+ #ifdef KeccakReference
105
+ displayText(1, "--- Switching to squeezing phase ---");
106
+ #endif
107
+
108
+ /* First, output whole blocks */
109
+ while(outputByteLen > (size_t)rateInBytes) {
110
+ SnP_ExtractBytes(state, curOutput, 0, rateInBytes);
111
+ SnP_Permute(state);
112
+ #ifdef KeccakReference
113
+ displayBytes(1, "Squeezed block", curOutput, rateInBytes);
114
+ #endif
115
+ curOutput += rateInBytes;
116
+ outputByteLen -= rateInBytes;
117
+ }
118
+
119
+ /* Finally, output what remains */
120
+ partialBlock = (unsigned int)outputByteLen;
121
+ SnP_ExtractBytes(state, curOutput, 0, partialBlock);
122
+ #ifdef KeccakReference
123
+ displayBytes(1, "Squeezed block (part)", curOutput, partialBlock);
124
+ #endif
125
+
126
+ return 0;
127
+ }
128
+
129
+ /* ---------------------------------------------------------------- */
130
+ /* ---------------------------------------------------------------- */
131
+ /* ---------------------------------------------------------------- */
132
+
133
+ int SpongeInitialize(SpongeInstance *instance, unsigned int rate, unsigned int capacity)
134
+ {
135
+ if (rate+capacity != SnP_width)
136
+ return 1;
137
+ if ((rate <= 0) || (rate > SnP_width) || ((rate % 8) != 0))
138
+ return 1;
139
+ SnP_StaticInitialize();
140
+ SnP_Initialize(instance->state);
141
+ instance->rate = rate;
142
+ instance->byteIOIndex = 0;
143
+ instance->squeezing = 0;
144
+
145
+ return 0;
146
+ }
147
+
148
+ /* ---------------------------------------------------------------- */
149
+
150
+ int SpongeAbsorb(SpongeInstance *instance, const unsigned char *data, size_t dataByteLen)
151
+ {
152
+ size_t i, j;
153
+ unsigned int partialBlock;
154
+ const unsigned char *curData;
155
+ unsigned int rateInBytes = instance->rate/8;
156
+
157
+ if (instance->squeezing)
158
+ return 1; /* Too late for additional input */
159
+
160
+ i = 0;
161
+ curData = data;
162
+ while(i < dataByteLen) {
163
+ if ((instance->byteIOIndex == 0) && (dataByteLen >= (i + rateInBytes))) {
164
+ #ifdef SnP_FastLoop_Absorb
165
+ /* processing full blocks first */
166
+ if ((rateInBytes % (SnP_width/200)) == 0) {
167
+ /* fast lane: whole lane rate */
168
+ j = SnP_FastLoop_Absorb(instance->state, rateInBytes/(SnP_width/200), curData, dataByteLen - i);
169
+ i += j;
170
+ curData += j;
171
+ }
172
+ else {
173
+ #endif
174
+ for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
175
+ #ifdef KeccakReference
176
+ displayBytes(1, "Block to be absorbed", curData, rateInBytes);
177
+ #endif
178
+ SnP_AddBytes(instance->state, curData, 0, rateInBytes);
179
+ SnP_Permute(instance->state);
180
+ curData+=rateInBytes;
181
+ }
182
+ i = dataByteLen - j;
183
+ #ifdef SnP_FastLoop_Absorb
184
+ }
185
+ #endif
186
+ }
187
+ else {
188
+ /* normal lane: using the message queue */
189
+ partialBlock = (unsigned int)(dataByteLen - i);
190
+ if (partialBlock+instance->byteIOIndex > rateInBytes)
191
+ partialBlock = rateInBytes-instance->byteIOIndex;
192
+ #ifdef KeccakReference
193
+ displayBytes(1, "Block to be absorbed (part)", curData, partialBlock);
194
+ #endif
195
+ i += partialBlock;
196
+
197
+ SnP_AddBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
198
+ curData += partialBlock;
199
+ instance->byteIOIndex += partialBlock;
200
+ if (instance->byteIOIndex == rateInBytes) {
201
+ SnP_Permute(instance->state);
202
+ instance->byteIOIndex = 0;
203
+ }
204
+ }
205
+ }
206
+ return 0;
207
+ }
208
+
209
+ /* ---------------------------------------------------------------- */
210
+
211
+ int SpongeAbsorbLastFewBits(SpongeInstance *instance, unsigned char delimitedData)
212
+ {
213
+ unsigned int rateInBytes = instance->rate/8;
214
+
215
+ if (delimitedData == 0)
216
+ return 1;
217
+ if (instance->squeezing)
218
+ return 1; /* Too late for additional input */
219
+
220
+ #ifdef KeccakReference
221
+ {
222
+ unsigned char delimitedData1[1];
223
+ delimitedData1[0] = delimitedData;
224
+ displayBytes(1, "Block to be absorbed (last few bits + first bit of padding)", delimitedData1, 1);
225
+ }
226
+ #endif
227
+ /* Last few bits, whose delimiter coincides with first bit of padding */
228
+ SnP_AddByte(instance->state, delimitedData, instance->byteIOIndex);
229
+ /* If the first bit of padding is at position rate-1, we need a whole new block for the second bit of padding */
230
+ if ((delimitedData >= 0x80) && (instance->byteIOIndex == (rateInBytes-1)))
231
+ SnP_Permute(instance->state);
232
+ /* Second bit of padding */
233
+ SnP_AddByte(instance->state, 0x80, rateInBytes-1);
234
+ #ifdef KeccakReference
235
+ {
236
+ unsigned char block[SnP_width/8];
237
+ memset(block, 0, SnP_width/8);
238
+ block[rateInBytes-1] = 0x80;
239
+ displayBytes(1, "Second bit of padding", block, rateInBytes);
240
+ }
241
+ #endif
242
+ SnP_Permute(instance->state);
243
+ instance->byteIOIndex = 0;
244
+ instance->squeezing = 1;
245
+ #ifdef KeccakReference
246
+ displayText(1, "--- Switching to squeezing phase ---");
247
+ #endif
248
+ return 0;
249
+ }
250
+
251
+ /* ---------------------------------------------------------------- */
252
+
253
+ int SpongeSqueeze(SpongeInstance *instance, unsigned char *data, size_t dataByteLen)
254
+ {
255
+ size_t i, j;
256
+ unsigned int partialBlock;
257
+ unsigned int rateInBytes = instance->rate/8;
258
+ unsigned char *curData;
259
+
260
+ if (!instance->squeezing)
261
+ SpongeAbsorbLastFewBits(instance, 0x01);
262
+
263
+ i = 0;
264
+ curData = data;
265
+ while(i < dataByteLen) {
266
+ if ((instance->byteIOIndex == rateInBytes) && (dataByteLen >= (i + rateInBytes))) {
267
+ for(j=dataByteLen-i; j>=rateInBytes; j-=rateInBytes) {
268
+ SnP_Permute(instance->state);
269
+ SnP_ExtractBytes(instance->state, curData, 0, rateInBytes);
270
+ #ifdef KeccakReference
271
+ displayBytes(1, "Squeezed block", curData, rateInBytes);
272
+ #endif
273
+ curData+=rateInBytes;
274
+ }
275
+ i = dataByteLen - j;
276
+ }
277
+ else {
278
+ /* normal lane: using the message queue */
279
+ if (instance->byteIOIndex == rateInBytes) {
280
+ SnP_Permute(instance->state);
281
+ instance->byteIOIndex = 0;
282
+ }
283
+ partialBlock = (unsigned int)(dataByteLen - i);
284
+ if (partialBlock+instance->byteIOIndex > rateInBytes)
285
+ partialBlock = rateInBytes-instance->byteIOIndex;
286
+ i += partialBlock;
287
+
288
+ SnP_ExtractBytes(instance->state, curData, instance->byteIOIndex, partialBlock);
289
+ #ifdef KeccakReference
290
+ displayBytes(1, "Squeezed block (part)", curData, partialBlock);
291
+ #endif
292
+ curData += partialBlock;
293
+ instance->byteIOIndex += partialBlock;
294
+ }
295
+ }
296
+ return 0;
297
+ }
298
+
299
+ /* ---------------------------------------------------------------- */
300
+
301
+ #undef Sponge
302
+ #undef SpongeInstance
303
+ #undef SpongeInitialize
304
+ #undef SpongeAbsorb
305
+ #undef SpongeAbsorbLastFewBits
306
+ #undef SpongeSqueeze
307
+ #undef SnP_stateSizeInBytes
308
+ #undef SnP_stateAlignment
309
+ #undef SnP_StaticInitialize
310
+ #undef SnP_Initialize
311
+ #undef SnP_AddByte
312
+ #undef SnP_AddBytes
313
+ #undef SnP_ExtractBytes