summary refs log tree commit diff
path: root/crypto/src/tls/crypto/impl/TlsSuiteHmac.cs
blob: 3e4b6f0f289d7694a581b5aa06618adf630b5f54 (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
using System;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Tls.Crypto.Impl
{
    /// <summary>A generic TLS MAC implementation, acting as an HMAC based on some underlying Digest.</summary>
    // TODO[api] sealed
    public class TlsSuiteHmac
        : TlsSuiteMac
    {
        private const long SequenceNumberPlaceholder = -1L;

        protected static int GetMacSize(TlsCryptoParameters cryptoParams, TlsMac mac)
        {
            int macSize = mac.MacLength;
            if (cryptoParams.SecurityParameters.IsTruncatedHmac)
            {
                macSize = System.Math.Min(macSize, 10);
            }
            return macSize;
        }

        protected readonly TlsCryptoParameters m_cryptoParams;
        protected readonly TlsHmac m_mac;
        protected readonly int m_digestBlockSize;
        protected readonly int m_digestOverhead;
        protected readonly int m_macSize;

        /// <summary>Generate a new instance of a TlsMac.</summary>
        /// <param name="cryptoParams">the TLS client context specific crypto parameters.</param>
        /// <param name="mac">The MAC to use.</param>
        public TlsSuiteHmac(TlsCryptoParameters cryptoParams, TlsHmac mac)
        {
            this.m_cryptoParams = cryptoParams;
            this.m_mac = mac;
            this.m_macSize = GetMacSize(cryptoParams, mac);
            this.m_digestBlockSize = mac.InternalBlockSize;

            // TODO This should check the actual algorithm, not assume based on the digest size
            if (TlsImplUtilities.IsSsl(cryptoParams) && mac.MacLength == 20)
            {
                /*
                 * NOTE: For the SSL 3.0 MAC with SHA-1, the secret + input pad is not block-aligned.
                 */
                this.m_digestOverhead = 4;
            }
            else
            {
                this.m_digestOverhead = m_digestBlockSize / 8;
            }
        }

        public virtual int Size
        {
            get { return m_macSize; }
        }

        public virtual byte[] CalculateMac(long seqNo, short type, byte[] msg, int msgOff, int msgLen)
        {
            return CalculateMac(seqNo, type, null, msg, msgOff, msgLen);
        }

        // TODO[api] Replace TlsSuiteMac.CalculateMac (non-span version)
        public virtual byte[] CalculateMac(long seqNo, short type, byte[] connectionID, byte[] msg, int msgOff,
            int msgLen)
        {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
            var connIDSpan = connectionID == null ? Span<byte>.Empty : connectionID.AsSpan();

            return CalculateMac(seqNo, type, connIDSpan, msg.AsSpan(msgOff, msgLen));
#else
            ProtocolVersion serverVersion = m_cryptoParams.ServerVersion;

            if (serverVersion.IsSsl)
            {
                byte[] macHeader = new byte[11];
                TlsUtilities.WriteUint64(seqNo, macHeader, 0);
                TlsUtilities.WriteUint8(type, macHeader, 8);
                TlsUtilities.WriteUint16(msgLen, macHeader, 9);

                m_mac.Update(macHeader, 0, macHeader.Length);
            }
            else if (!Arrays.IsNullOrEmpty(connectionID))
            {
                int cidLength = connectionID.Length;
                byte[] macHeader = new byte[23 + cidLength];
                TlsUtilities.WriteUint64(SequenceNumberPlaceholder, macHeader, 0);
                TlsUtilities.WriteUint8(ContentType.tls12_cid, macHeader, 8);
                TlsUtilities.WriteUint8(cidLength, macHeader, 9);
                TlsUtilities.WriteUint8(ContentType.tls12_cid, macHeader, 10);
                TlsUtilities.WriteVersion(serverVersion, macHeader, 11);
                TlsUtilities.WriteUint64(seqNo, macHeader, 13);
                Array.Copy(connectionID, 0, macHeader, 21, cidLength);
                TlsUtilities.WriteUint16(msgLen, macHeader, 21 + cidLength);

                m_mac.Update(macHeader, 0, macHeader.Length);
            }
            else
            {
                byte[] macHeader = new byte[13];
                TlsUtilities.WriteUint64(seqNo, macHeader, 0);
                TlsUtilities.WriteUint8(type, macHeader, 8);
                TlsUtilities.WriteVersion(serverVersion, macHeader, 9);
                TlsUtilities.WriteUint16(msgLen, macHeader, 11);

                m_mac.Update(macHeader, 0, macHeader.Length);
            }

            m_mac.Update(msg, msgOff, msgLen);

            return Truncate(m_mac.CalculateMac());
#endif
        }

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
        public virtual byte[] CalculateMac(long seqNo, short type, ReadOnlySpan<byte> message)
        {
            return CalculateMac(seqNo, type, ReadOnlySpan<byte>.Empty, message);
        }

        // TODO[api] Replace TlsSuiteMac.CalculateMac (span version)
        public virtual byte[] CalculateMac(long seqNo, short type, ReadOnlySpan<byte> connectionID,
            ReadOnlySpan<byte> message)
        {
            ProtocolVersion serverVersion = m_cryptoParams.ServerVersion;

            if (serverVersion.IsSsl)
            {
                byte[] macHeader = new byte[11];
                TlsUtilities.WriteUint64(seqNo, macHeader, 0);
                TlsUtilities.WriteUint8(type, macHeader, 8);
                TlsUtilities.WriteUint16(message.Length, macHeader, 9);

                m_mac.Update(macHeader);
            }
            else if (!connectionID.IsEmpty)
            {
                int cidLength = connectionID.Length;
                byte[] macHeader = new byte[23 + cidLength];
                TlsUtilities.WriteUint64(SequenceNumberPlaceholder, macHeader, 0);
                TlsUtilities.WriteUint8(ContentType.tls12_cid, macHeader, 8);
                TlsUtilities.WriteUint8(cidLength, macHeader, 9);
                TlsUtilities.WriteUint8(ContentType.tls12_cid, macHeader, 10);
                TlsUtilities.WriteVersion(serverVersion, macHeader, 11);
                TlsUtilities.WriteUint64(seqNo, macHeader, 13);
                connectionID.CopyTo(macHeader.AsSpan(21));
                TlsUtilities.WriteUint16(message.Length, macHeader, 21 + cidLength);

                m_mac.Update(macHeader);
            }
            else
            {
                byte[] macHeader = new byte[13];
                TlsUtilities.WriteUint64(seqNo, macHeader, 0);
                TlsUtilities.WriteUint8(type, macHeader, 8);
                TlsUtilities.WriteVersion(serverVersion, macHeader, 9);
                TlsUtilities.WriteUint16(message.Length, macHeader, 11);

                m_mac.Update(macHeader);
            }

            m_mac.Update(message);

            return Truncate(m_mac.CalculateMac());
        }
#endif

        public virtual byte[] CalculateMacConstantTime(long seqNo, short type, byte[] msg, int msgOff, int msgLen,
            int fullLength, byte[] dummyData)
        {
            return CalculateMacConstantTime(seqNo, type, null, msg, msgOff, msgLen, fullLength, dummyData);
        }

        // TODO[api] Replace TlsSuiteMac.CalculateMacConstantTime
        public virtual byte[] CalculateMacConstantTime(long seqNo, short type, byte[] connectionID, byte[] msg,
            int msgOff, int msgLen, int fullLength, byte[] dummyData)
        {
            /*
             * Actual MAC only calculated on 'length' bytes...
             */
            byte[] result = CalculateMac(seqNo, type, connectionID, msg, msgOff, msgLen);

            /*
             * ...but ensure a constant number of complete digest blocks are processed (as many as would
             * be needed for 'fullLength' bytes of input).
             */
            int headerLength = GetHeaderLength(connectionID);

            // How many extra full blocks do we need to calculate?
            int extra = GetDigestBlockCount(headerLength + fullLength) - GetDigestBlockCount(headerLength + msgLen);

            while (--extra >= 0)
            {
                m_mac.Update(dummyData, 0, m_digestBlockSize);
            }

            // One more byte in case the implementation is "lazy" about processing blocks
            m_mac.Update(dummyData, 0, 1);
            m_mac.Reset();

            return result;
        }

        protected virtual int GetDigestBlockCount(int inputLength)
        {
            // NOTE: The input pad for HMAC is always a full digest block

            // NOTE: This calculation assumes a minimum of 1 pad byte
            return (inputLength + m_digestOverhead) / m_digestBlockSize;
        }

        protected virtual int GetHeaderLength(byte[] connectionID)
        {
            if (TlsImplUtilities.IsSsl(m_cryptoParams))
            {
                return 11;
            }
            else if (!Arrays.IsNullOrEmpty(connectionID))
            {
                return 23 + connectionID.Length;
            }
            else
            {
                return 13;
            }
        }

        protected virtual byte[] Truncate(byte[] bs)
        {
            if (bs.Length <= m_macSize)
                return bs;

            return Arrays.CopyOf(bs, m_macSize);
        }
    }
}