summary refs log tree commit diff
path: root/crypto/src/crypto/macs/KMac.cs
blob: 38697a9a9b5cba45fa995f152b3cc46b9703c408 (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
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Org.BouncyCastle.Crypto.Macs
{
    public class KMac : IMac, IXof
    {

        private static readonly byte[] padding = new byte[100];

        private readonly CSHAKEDigest cshake;
        private readonly int bitLength;
        private readonly int outputLength;

        private byte[] key;
        private bool initialised;
        private bool firstOutput;


        public KMac(int bitLength, byte[] S)
        {
            this.cshake = new CSHAKEDigest(bitLength, Encoding.ASCII.GetBytes("KMAC"),S);
            this.bitLength = bitLength;
            this.outputLength = bitLength * 2 / 8;
        }


        public string AlgorithmName => "KMAC" + cshake.AlgorithmName.Substring(6);

        public void BlockUpdate(byte[] input, int inOff, int len)
        {
            if (!initialised)
            {
                throw new InvalidOperationException("KMAC not initialized");
            }

            cshake.BlockUpdate(input, inOff, len);
        }

        public int DoFinal(byte[] output, int outOff)
        {
            if (firstOutput)
            {
                if (!initialised)
                {
                    throw new InvalidOperationException("KMAC not initialized");
                }

                byte[] encOut = XofUtils.rightEncode(GetMacSize() * 8);

                cshake.BlockUpdate(encOut, 0, encOut.Length);
            }

            int rv = cshake.DoFinal(output, outOff, GetMacSize());

            Reset();

            return rv;
        }

        public int DoFinal(byte[] output, int outOff, int outLen)
        {
            if (firstOutput)
            {
                if (!initialised)
                {
                    throw new InvalidOperationException("KMAC not initialized");
                }

                byte[] encOut = XofUtils.rightEncode(outLen * 8);

                cshake.BlockUpdate(encOut, 0, encOut.Length);
            }

            int rv = cshake.DoFinal(output, outOff, outLen);

            Reset();

            return rv;
        }

        public int DoOutput(byte[] output, int outOff, int outLen)
        {
            if (firstOutput)
            {
                if (!initialised)
                {
                    throw new InvalidOperationException("KMAC not initialized");
                }

                byte[] encOut = XofUtils.rightEncode(0);

                cshake.BlockUpdate(encOut, 0, encOut.Length);

                firstOutput = false;
            }

            return cshake.DoOutput(output, outOff, outLen);
        }

        public int GetByteLength()
        {
            return cshake.GetByteLength();
        }

        public int GetDigestSize()
        {
            return outputLength;
        }

        public int GetMacSize()
        {
            return outputLength;
        }

        public void Init(ICipherParameters parameters)
        {
            KeyParameter kParam = (KeyParameter)parameters;
            this.key = Arrays.Clone(kParam.GetKey());
            this.initialised = true;
            Reset();
        }

        public void Reset()
        {
            cshake.Reset();

            if (key != null)
            {
                if (bitLength == 128)
                {
                    bytePad(key, 168);
                }
                else
                {
                    bytePad(key, 136);
                }
            }

            firstOutput = true;
        }

        private void bytePad(byte[] X, int w)
        {
            byte[] bytes = XofUtils.leftEncode(w);
            BlockUpdate(bytes, 0, bytes.Length);
            byte[] encX = encode(X);
            BlockUpdate(encX, 0, encX.Length);

            int required = w - ((bytes.Length + encX.Length) % w);

            if (required > 0 && required != w)
            {
                while (required > padding.Length)
                {
                    BlockUpdate(padding, 0, padding.Length);
                    required -= padding.Length;
                }

                BlockUpdate(padding, 0, required);
            }
        }

        private static byte[] encode(byte[] X)
        {
            return Arrays.Concatenate(XofUtils.leftEncode(X.Length * 8), X);
        }

        public void Update(byte input)
        {
            if (!initialised)
            {
                throw new InvalidOperationException("KMAC not initialized");
            }

            cshake.Update(input);
        }
    }
}