summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/sphincsplus/HarakaS_X86.cs
blob: 3975f02ff052d92e6233b3bed15c1459f2677d4d (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
#if NETCOREAPP3_0_OR_GREATER
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;

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

namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus
{
    internal class HarakaS_X86
        : IXof
    {
        public static bool IsSupported => Haraka512_X86.IsSupported;

        private enum State { Absorbing, Squeezing };

        private readonly Vector128<byte>[] m_roundConstants = new Vector128<byte>[40];

        private readonly byte[] m_buf = new byte[64];
        private int m_bufPos = 0;
        private State m_state = State.Absorbing;

        internal HarakaS_X86(ReadOnlySpan<byte> pkSeed)
        {
            if (!IsSupported)
                throw new PlatformNotSupportedException(nameof(HarakaS_X86));

            // Absorb PKSeed
            Span<byte> buf = stackalloc byte[64];
            while (pkSeed.Length >= 32)
            {
                XorWith(pkSeed[..32], buf);
                Haraka512_X86.Permute(buf, buf);
                pkSeed = pkSeed[32..];
            }
            XorWith(pkSeed, buf);
            buf[pkSeed.Length] ^= 0x1F;
            buf[           31] ^= 0x80;

            // Squeeze round constants
            int rc = 0;
            while (rc < 40)
            {
                Haraka512_X86.Permute(buf, buf);
                m_roundConstants[rc++] = Load128(buf[  ..16]);
                m_roundConstants[rc++] = Load128(buf[16..32]);
            }
        }

        internal ReadOnlySpan<Vector128<byte>> RoundConstants => m_roundConstants;

        public string AlgorithmName => "HarakaS";

        public int GetDigestSize() => 32;

        public int GetByteLength() => 32;

        public void Update(byte input)
        {
            if (m_state != State.Absorbing)
                throw new InvalidOperationException();

            m_buf[m_bufPos++] ^= input;
            if (m_bufPos == 32)
            {
                Haraka512_X86.Permute(m_buf, m_buf, m_roundConstants);
                m_bufPos = 0;
            }
        }

        public void BlockUpdate(byte[] input, int inOff, int inLen)
        {
            BlockUpdate(input.AsSpan(inOff, inLen));
        }

        public void BlockUpdate(ReadOnlySpan<byte> input)
        {
            if (m_state != State.Absorbing)
                throw new InvalidOperationException();

            int available = 32 - m_bufPos;
            if (input.Length < available)
            {
                XorWith(input, m_buf.AsSpan(m_bufPos));
                m_bufPos += input.Length;
                return;
            }

            XorWith(input[..available], m_buf.AsSpan(m_bufPos));
            input = input[available..];
            Haraka512_X86.Permute(m_buf, m_buf, m_roundConstants);

            while (input.Length >= 32)
            {
                XorWith(input[..32], m_buf);
                input = input[32..];
                Haraka512_X86.Permute(m_buf, m_buf, m_roundConstants);
            }

            XorWith(input, m_buf);
            m_bufPos = input.Length;
        }

        public int DoFinal(byte[] output, int outOff)
        {
            return OutputFinal(output.AsSpan(outOff, 32));
        }

        public int DoFinal(Span<byte> output)
        {
            return OutputFinal(output[..32]);
        }

        public int Output(byte[] output, int outOff, int outLen)
        {
            return Output(output.AsSpan(outOff, outLen));
        }

        public int Output(Span<byte> output)
        {
            int result = output.Length;

            if (m_state != State.Squeezing)
            {
                m_buf[m_bufPos] ^= 0x1F;
                m_buf[31] ^= 0x80;
                m_bufPos = 32;
                m_state = State.Squeezing;

                if (output.IsEmpty)
                    return result;
            }
            else
            {
                int available = 32 - m_bufPos;
                if (output.Length <= available)
                {
                    output.CopyFrom(m_buf.AsSpan(m_bufPos));
                    m_bufPos += available;
                    return result;
                }

                output[..available].CopyFrom(m_buf.AsSpan(m_bufPos));
                output = output[available..];
            }

            Debug.Assert(!output.IsEmpty);

            while (output.Length > 32)
            {
                Haraka512_X86.Permute(m_buf, m_buf, m_roundConstants);
                output[..32].CopyFrom(m_buf);
                output = output[32..];
            }

            Haraka512_X86.Permute(m_buf, m_buf, m_roundConstants);
            output.CopyFrom(m_buf);
            m_bufPos = output.Length;

            return result;
        }

        public int OutputFinal(byte[] output, int outOff, int outLen)
        {
            return OutputFinal(output.AsSpan(outOff, outLen));
        }

        public int OutputFinal(Span<byte> output)
        {
            int result = Output(output);
            Reset();
            return result;
        }

        public void Reset()
        {
            Array.Clear(m_buf);
            m_bufPos = 0;
            m_state = State.Absorbing;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static Vector128<byte> Load128(ReadOnlySpan<byte> t)
        {
#if NET7_0_OR_GREATER
            return Vector128.Create<byte>(t);
#else
            if (BitConverter.IsLittleEndian && Unsafe.SizeOf<Vector128<byte>>() == 16)
                return Unsafe.ReadUnaligned<Vector128<byte>>(ref Unsafe.AsRef(t[0]));

            return Vector128.Create(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8], t[9], t[10], t[11], t[12],
                t[13], t[14], t[15]);
#endif
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static void XorWith(ReadOnlySpan<byte> x, Span<byte> z)
        {
            for (int i = 0; i < x.Length; i++)
            {
                z[i] ^= x[i];
            }
        }
    }
}
#endif