summary refs log tree commit diff
path: root/crypto/src/crypto/macs/DSTU7624Mac.cs
blob: 01c1f869c7662a14ac801cc869732b0406490813 (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
using System;

using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Crypto.Macs
{
    /**
     * implementation of DSTU 7624 MAC
     */
    public class Dstu7624Mac : IMac
    {
        private int macSize;

        private Dstu7624Engine engine;
        private int blockSize;

        private byte[] c, cTemp, kDelta;
        private byte[] buf;
        private int bufOff;

        public Dstu7624Mac(int blockSizeBits, int q)
        {
            engine = new Dstu7624Engine(blockSizeBits);

            blockSize = blockSizeBits / 8;

            macSize = q / 8;

            c = new byte[blockSize];

            cTemp = new byte[blockSize];

            kDelta = new byte[blockSize];
            buf = new byte[blockSize];
        }

        public void Init(ICipherParameters parameters)
        {
            if (parameters is KeyParameter)
            {
                engine.Init(true, (KeyParameter)parameters);

                engine.ProcessBlock(kDelta, 0, kDelta, 0);
            }
            else
            {
                throw new ArgumentException("invalid parameter passed to Dstu7624Mac init - "
                + Platform.GetTypeName(parameters));
            }
        }

        public string AlgorithmName
        {
            get { return "Dstu7624Mac"; }
        }

        public int GetMacSize()
        {
            return macSize;
        }

        public void Update(byte input)
        {
            if (bufOff == buf.Length)
            {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
                ProcessBlock(buf);
#else
                ProcessBlock(buf, 0);
#endif
                bufOff = 0;
            }

            buf[bufOff++] = input;
        }

        public void BlockUpdate(byte[] input, int inOff, int len)
        {
            if (len < 0)
                throw new ArgumentException("Can't have a negative input length!");

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
            BlockUpdate(input.AsSpan(inOff, len));
#else
            int blockSize = engine.GetBlockSize();
            int gapLen = blockSize - bufOff;

            if (len > gapLen)
            {
                Array.Copy(input, inOff, buf, bufOff, gapLen);

                ProcessBlock(buf, 0);

                bufOff = 0;
                len -= gapLen;
                inOff += gapLen;

                while (len > blockSize)
                {
                    ProcessBlock(input, inOff);

                    len -= blockSize;
                    inOff += blockSize;
                }
            }

            Array.Copy(input, inOff, buf, bufOff, len);

            bufOff += len;
#endif
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public void BlockUpdate(ReadOnlySpan<byte> input)
        {
            int blockSize = engine.GetBlockSize();
            int gapLen = blockSize - bufOff;

            if (input.Length > gapLen)
            {
                input[..gapLen].CopyTo(buf.AsSpan(bufOff));

                ProcessBlock(buf);

                bufOff = 0;
                input = input[gapLen..];

                while (input.Length > blockSize)
                {
                    ProcessBlock(input);
                    input = input[blockSize..];
                }
            }

            input.CopyTo(buf.AsSpan(bufOff));

            bufOff += input.Length;
        }
#endif

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        private void ProcessBlock(ReadOnlySpan<byte> input)
        {
            Xor(c, input, cTemp);

            engine.ProcessBlock(cTemp, c);
        }

        private void Xor(ReadOnlySpan<byte> c, ReadOnlySpan<byte> input, Span<byte> xorResult)
        {
            for (int byteIndex = 0; byteIndex < blockSize; byteIndex++)
            {
                xorResult[byteIndex] = (byte)(c[byteIndex] ^ input[byteIndex]);
            }
        }
#else
        private void ProcessBlock(byte[] input, int inOff)
        {
            Xor(c, 0, input, inOff, cTemp);

            engine.ProcessBlock(cTemp, 0, c, 0);
        }
#endif

        private void Xor(byte[] c, int cOff, byte[] input, int inOff, byte[] xorResult)
        {
            for (int byteIndex = 0; byteIndex < blockSize; byteIndex++)
            {
                xorResult[byteIndex] = (byte)(c[byteIndex + cOff] ^ input[byteIndex + inOff]);
            }
        }

        public int DoFinal(byte[] output, int outOff)
        {
            if (bufOff % buf.Length != 0)
                throw new DataLengthException("Input must be a multiple of blocksize");

            //Last block
            Xor(c, 0, buf, 0, cTemp);
            Xor(cTemp, 0, kDelta, 0, c);
            engine.ProcessBlock(c, 0, c, 0);

            if (macSize + outOff > output.Length)
                throw new DataLengthException("Output buffer too short");

            Array.Copy(c, 0, output, outOff, macSize);

            return macSize;
        }

        public void Reset()
        {
            Arrays.Fill(c, (byte)0x00);
            Arrays.Fill(cTemp, (byte)0x00);
            Arrays.Fill(kDelta, (byte)0x00);
            Arrays.Fill(buf, (byte)0x00);
            engine.Reset();
            engine.ProcessBlock(kDelta, 0, kDelta, 0);
            bufOff = 0;
        }
    }
}