summary refs log tree commit diff
path: root/crypto/src/crypto/digests/SparkleDigest.cs
blob: bd03fdf0dbdde2dee621a8c9862c4ab3e9b263d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.IO;

using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Crypto.Digests
{
    /// <summary>Sparkle v1.2, based on the current round 3 submission, https://sparkle-lwc.github.io/ .</summary>
    /// <remarks>
    /// Reference C implementation: https://github.com/cryptolu/sparkle.<br/>
    /// Specification:
    /// https://csrc.nist.gov/CSRC/media/Projects/lightweight-cryptography/documents/finalist-round/updated-spec-doc/sparkle-spec-final.pdf .
    /// </remarks>
    public sealed class SparkleDigest
        : IDigest
    {
        public enum SparkleParameters
        {
            ESCH256,
            ESCH384
        }

        private static readonly uint[] RCON = { 0xB7E15162U, 0xBF715880U, 0x38B4DA56U, 0x324E7738U, 0xBB1185EBU,
            0x4F7C7B57U, 0xCFBFA1C8U, 0xC2B3293DU };

        private string algorithmName;
        private readonly uint[] state;
        private readonly MemoryStream message = new MemoryStream();
        private readonly int DIGEST_BYTES;
        private readonly int SPARKLE_STEPS_SLIM;
        private readonly int SPARKLE_STEPS_BIG;
        private readonly int STATE_BRANS;
        private readonly int STATE_WORDS;
        private readonly int RATE_WORDS;
        private readonly int RATE_BYTES;

        public SparkleDigest(SparkleParameters sparkleParameters)
        {
            int ESCH_DIGEST_LEN;
            int SPARKLE_STATE;
            int SPARKLE_RATE = 128;
            switch (sparkleParameters)
            {
            case SparkleParameters.ESCH256:
                ESCH_DIGEST_LEN = 256;
                SPARKLE_STATE = 384;
                SPARKLE_STEPS_SLIM = 7;
                SPARKLE_STEPS_BIG = 11;
                algorithmName = "ESCH-256";
                break;
            case SparkleParameters.ESCH384:
                ESCH_DIGEST_LEN = 384;
                SPARKLE_STATE = 512;
                SPARKLE_STEPS_SLIM = 8;
                SPARKLE_STEPS_BIG = 12;
                algorithmName = "ESCH-384";
                break;
            default:
                throw new ArgumentException("Invalid definition of SCHWAEMM instance");
            }
            STATE_BRANS = SPARKLE_STATE >> 6;
            STATE_WORDS = SPARKLE_STATE >> 5;
            RATE_WORDS = SPARKLE_RATE >> 5;
            RATE_BYTES = SPARKLE_RATE >> 3;
            DIGEST_BYTES = ESCH_DIGEST_LEN >> 3;
            state = new uint[STATE_WORDS];
        }

        public string AlgorithmName => algorithmName;

        public int GetDigestSize() => DIGEST_BYTES;

        public int GetByteLength() => RATE_BYTES;

        public void Update(byte input)
        {
            message.WriteByte(input);
        }

        public void BlockUpdate(byte[] input, int inOff, int inLen)
        {
            Check.DataLength(input, inOff, inLen, "input buffer too short");

            message.Write(input, inOff, inLen);
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public void BlockUpdate(ReadOnlySpan<byte> input)
        {
            message.Write(input);
        }
#endif

        public int DoFinal(byte[] output, int outOff)
        {
            Check.OutputLength(output, outOff, DIGEST_BYTES, "output buffer too short");

            byte[] input = message.GetBuffer();
            int inlen = (int)message.Length, i, inOff = 0;
            uint tmpx, tmpy;
            // Main Hashing Loop
            uint[] in32 = Pack.LE_To_UInt32(input, 0, inlen >> 2);
            while (inlen > RATE_BYTES)
            {
                // addition of a buffer block to the state
                tmpx = 0;
                tmpy = 0;
                for (i = 0; i < RATE_WORDS; i += 2)
                {
                    tmpx ^= in32[i + (inOff >> 2)];
                    tmpy ^= in32[i + 1 + (inOff >> 2)];
                }
                tmpx = ELL(tmpx);
                tmpy = ELL(tmpy);
                for (i = 0; i < RATE_WORDS; i += 2)
                {
                    state[i] ^= (in32[i + (inOff >> 2)] ^ tmpy);
                    state[i + 1] ^= (in32[i + 1 + (inOff >> 2)] ^ tmpx);
                }
                for (i = RATE_WORDS; i < (STATE_WORDS / 2); i += 2)
                {
                    state[i] ^= tmpy;
                    state[i + 1] ^= tmpx;
                }
                // execute SPARKLE with slim number of steps
                sparkle_opt(state, STATE_BRANS, SPARKLE_STEPS_SLIM);
                inlen -= RATE_BYTES;
                inOff += RATE_BYTES;
            }
            // Hashing of Last Block
            // addition of constant M1 or M2 to the state
            state[STATE_BRANS - 1] ^= ((inlen < RATE_BYTES) ? (1u << 24) : (1u << 25));
            // addition of last msg block (incl. padding)
            uint[] buffer = new uint[RATE_WORDS];
            for (i = 0; i < inlen; ++i)
            {
                buffer[i >> 2] |= (input[inOff++] & 0xffu) << ((i & 3) << 3);
            }
            if (inlen < RATE_BYTES)
            {  // padding
                buffer[i >> 2] |= 0x80u << ((i & 3) << 3);
            }
            tmpx = 0;
            tmpy = 0;
            for (i = 0; i < RATE_WORDS; i += 2)
            {
                tmpx ^= buffer[i];
                tmpy ^= buffer[i + 1];
            }
            tmpx = ELL(tmpx);
            tmpy = ELL(tmpy);
            for (i = 0; i < RATE_WORDS; i += 2)
            {
                state[i] ^= (buffer[i] ^ tmpy);
                state[i + 1] ^= (buffer[i + 1] ^ tmpx);
            }
            for (i = RATE_WORDS; i < (STATE_WORDS / 2); i += 2)
            {
                state[i] ^= tmpy;
                state[i + 1] ^= tmpx;
            }
            // execute SPARKLE with big number of steps
            sparkle_opt(state, STATE_BRANS, SPARKLE_STEPS_BIG);
            Pack.UInt32_To_LE(state, 0, RATE_WORDS, output, outOff);
            int outlen = RATE_BYTES;
            outOff += RATE_BYTES;
            while (outlen < DIGEST_BYTES)
            {
                sparkle_opt(state, STATE_BRANS, SPARKLE_STEPS_SLIM);
                Pack.UInt32_To_LE(state, 0, RATE_WORDS, output, outOff);
                outlen += RATE_BYTES;
                outOff += RATE_BYTES;
            }
            return DIGEST_BYTES;

            // TODO Reset?
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public int DoFinal(Span<byte> output)
        {
            byte[] rv = new byte[DIGEST_BYTES];
            DoFinal(rv, 0);
            // TODO Remove duplicate if added in other DoFinal
            Reset();
            rv.AsSpan(0, rv.Length).CopyTo(output);
            return DIGEST_BYTES;
        }
#endif

        public void Reset()
        {
            message.SetLength(0);
            Arrays.Fill(state, 0U);
        }

        private void sparkle_opt(uint[] state, int brans, int steps)
        {
            uint i, j, rc, tmpx, tmpy, x0, y0;
            for (i = 0; i < steps; i++)
            {
                // Add round ant
                state[1] ^= RCON[i & 7];
                state[3] ^= i;
                // ARXBOX layer
                for (j = 0; j < 2 * brans; j += 2)
                {
                    rc = RCON[j >> 1];
                    state[j] += Integers.RotateRight(state[j + 1], 31);
                    state[j + 1] ^= Integers.RotateRight(state[j], 24);
                    state[j] ^= rc;
                    state[j] += Integers.RotateRight(state[j + 1], 17);
                    state[j + 1] ^= Integers.RotateRight(state[j], 17);
                    state[j] ^= rc;
                    state[j] += state[j + 1];
                    state[j + 1] ^= Integers.RotateRight(state[j], 31);
                    state[j] ^= rc;
                    state[j] += Integers.RotateRight(state[j + 1], 24);
                    state[j + 1] ^= Integers.RotateRight(state[j], 16);
                    state[j] ^= rc;
                }
                // Linear layer
                tmpx = x0 = state[0];
                tmpy = y0 = state[1];
                for (j = 2; j < brans; j += 2)
                {
                    tmpx ^= state[j];
                    tmpy ^= state[j + 1];
                }
                tmpx = ELL(tmpx);
                tmpy = ELL(tmpy);
                for (j = 2; j < brans; j += 2)
                {
                    state[j - 2] = state[j + brans] ^ state[j] ^ tmpy;
                    state[j + brans] = state[j];
                    state[j - 1] = state[j + brans + 1] ^ state[j + 1] ^ tmpx;
                    state[j + brans + 1] = state[j + 1];
                }
                state[brans - 2] = state[brans] ^ x0 ^ tmpy;
                state[brans] = x0;
                state[brans - 1] = state[brans + 1] ^ y0 ^ tmpx;
                state[brans + 1] = y0;
            }
        }

        private static uint ELL(uint x)
        {
            return Integers.RotateRight(x ^ (x << 16), 16);
        }
    }
}