summary refs log tree commit diff
path: root/crypto/src/crypto/digests/Haraka256Digest.cs
blob: efbaa81e2c91b80d5ee7145a84e6a7bd113046f7 (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
using System;

namespace Org.BouncyCastle.Crypto.Digests
{
    public sealed class Haraka256Digest
        : HarakaBase
    {
        private readonly byte[] m_buf;
        private int m_bufPos;

        public Haraka256Digest()
        {
            m_buf = new byte[32];
            m_bufPos = 0;
        }

        public override string AlgorithmName => "Haraka-256";

        public override int GetByteLength() => 32;

        public override void Update(byte input)
        {
            if (m_bufPos > 32 - 1)
                throw new ArgumentException("total input cannot be more than 32 bytes");

            m_buf[m_bufPos++] = input;
        }

        public override void BlockUpdate(byte[] input, int inOff, int len)
        {
            if (m_bufPos > 32 - len)
                throw new ArgumentException("total input cannot be more than 32 bytes");

            Array.Copy(input, inOff, m_buf, m_bufPos, len);
            m_bufPos += len;
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public override void BlockUpdate(ReadOnlySpan<byte> input)
        {
            if (m_bufPos > 32 - input.Length)
                throw new ArgumentException("total input cannot be more than 32 bytes");

            input.CopyTo(m_buf.AsSpan(m_bufPos));
            m_bufPos += input.Length;
        }
#endif

        public override int DoFinal(byte[] output, int outOff)
        {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
            return DoFinal(output.AsSpan(outOff));
#else
            if (m_bufPos != 32)
                throw new ArgumentException("input must be exactly 32 bytes");

            if (output.Length - outOff < 32)
                throw new ArgumentException("output too short to receive digest");

            int rv = Haraka256256(m_buf, output, outOff);

            Reset();

            return rv;
#endif
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public override int DoFinal(Span<byte> output)
        {
            if (m_bufPos != 32)
                throw new ArgumentException("input must be exactly 32 bytes");

            if (output.Length < 32)
                throw new ArgumentException("output too short to receive digest");

#if NETCOREAPP3_0_OR_GREATER
            if (Haraka256_X86.IsSupported)
            {
                Haraka256_X86.Hash(m_buf, output);
                Reset();
                return 32;
            }
#endif

            int rv = Haraka256256(m_buf, output);

            Reset();

            return rv;
        }
#endif

        public override void Reset()
        {
            m_bufPos = 0;
            Array.Clear(m_buf, 0, 32);
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        private static int Haraka256256(ReadOnlySpan<byte> msg, Span<byte> output)
        {
            byte[][] s1 = new byte[2][];
            s1[0] = new byte[16];
            s1[1] = new byte[16];
            byte[][] s2 = new byte[2][];
            s2[0] = new byte[16];
            s2[1] = new byte[16];

            msg[  ..16].CopyTo(s1[0]);
            msg[16..32].CopyTo(s1[1]);

            s1[0] = AesEnc(s1[0], RC[0]);
            s1[1] = AesEnc(s1[1], RC[1]);
            s1[0] = AesEnc(s1[0], RC[2]);
            s1[1] = AesEnc(s1[1], RC[3]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[4]);
            s1[1] = AesEnc(s2[1], RC[5]);
            s1[0] = AesEnc(s1[0], RC[6]);
            s1[1] = AesEnc(s1[1], RC[7]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[8]);
            s1[1] = AesEnc(s2[1], RC[9]);
            s1[0] = AesEnc(s1[0], RC[10]);
            s1[1] = AesEnc(s1[1], RC[11]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[12]);
            s1[1] = AesEnc(s2[1], RC[13]);
            s1[0] = AesEnc(s1[0], RC[14]);
            s1[1] = AesEnc(s1[1], RC[15]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[16]);
            s1[1] = AesEnc(s2[1], RC[17]);
            s1[0] = AesEnc(s1[0], RC[18]);
            s1[1] = AesEnc(s1[1], RC[19]);
            Mix256(s1, s2);

            Xor(s2[0], msg      , output[  ..16]);
            Xor(s2[1], msg[16..], output[16..32]);

            return DIGEST_SIZE;
        }
#else
        private static int Haraka256256(byte[] msg, byte[] output, int outOff)
        {
            byte[][] s1 = new byte[2][];
            s1[0] = new byte[16];
            s1[1] = new byte[16];
            byte[][] s2 = new byte[2][];
            s2[0] = new byte[16];
            s2[1] = new byte[16];

            Array.Copy(msg,  0, s1[0], 0, 16);
            Array.Copy(msg, 16, s1[1], 0, 16);

            s1[0] = AesEnc(s1[0], RC[0]);
            s1[1] = AesEnc(s1[1], RC[1]);
            s1[0] = AesEnc(s1[0], RC[2]);
            s1[1] = AesEnc(s1[1], RC[3]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[4]);
            s1[1] = AesEnc(s2[1], RC[5]);
            s1[0] = AesEnc(s1[0], RC[6]);
            s1[1] = AesEnc(s1[1], RC[7]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[8]);
            s1[1] = AesEnc(s2[1], RC[9]);
            s1[0] = AesEnc(s1[0], RC[10]);
            s1[1] = AesEnc(s1[1], RC[11]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[12]);
            s1[1] = AesEnc(s2[1], RC[13]);
            s1[0] = AesEnc(s1[0], RC[14]);
            s1[1] = AesEnc(s1[1], RC[15]);
            Mix256(s1, s2);

            s1[0] = AesEnc(s2[0], RC[16]);
            s1[1] = AesEnc(s2[1], RC[17]);
            s1[0] = AesEnc(s1[0], RC[18]);
            s1[1] = AesEnc(s1[1], RC[19]);
            Mix256(s1, s2);

            s1[0] = Xor(s2[0], msg,  0);
            s1[1] = Xor(s2[1], msg, 16);

            Array.Copy(s1[0], 0, output, outOff     , 16);
            Array.Copy(s1[1], 0, output, outOff + 16, 16);

            return DIGEST_SIZE;
        }
#endif

        private static void Mix256(byte[][] s1, byte[][] s2)
        {
            Array.Copy(s1[0], 0, s2[0], 0, 4);
            Array.Copy(s1[1], 0, s2[0], 4, 4);
            Array.Copy(s1[0], 4, s2[0], 8, 4);
            Array.Copy(s1[1], 4, s2[0], 12, 4);

            Array.Copy(s1[0], 8, s2[1], 0, 4);
            Array.Copy(s1[1], 8, s2[1], 4, 4);
            Array.Copy(s1[0], 12, s2[1], 8, 4);
            Array.Copy(s1[1], 12, s2[1], 12, 4);
        }
    }
}