Add licensing info
[mono.git] / mcs / class / Mono.Security / Mono.Security.Cryptography / TlsHMAC.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 /* Transport Security Layer (TLS)
23  * Copyright (c) 2003-2004 Carlos Guzman Alvarez
24  * 
25  * Permission is hereby granted, free of charge, to any person 
26  * obtaining a copy of this software and associated documentation 
27  * files (the "Software"), to deal in the Software without restriction, 
28  * including without limitation the rights to use, copy, modify, merge, 
29  * publish, distribute, sublicense, and/or sell copies of the Software, 
30  * and to permit persons to whom the Software is furnished to do so, 
31  * subject to the following conditions:
32  * 
33  * The above copyright notice and this permission notice shall be included 
34  * in all copies or substantial portions of the Software.
35  * 
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
37  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
38  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
39  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
40  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
41  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
42  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
43  * DEALINGS IN THE SOFTWARE.
44  */
45
46 using System;
47 using System.Security.Cryptography;
48
49 namespace Mono.Security.Cryptography
50 {
51    /*
52         * References:
53         *               RFC 2104 (http://www.ietf.org/rfc/rfc2104.txt)
54         *               RFC 2202 (http://www.ietf.org/rfc/rfc2202.txt)
55         * MSDN:
56         * 
57         *               Extending the KeyedHashAlgorithm Class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconextendingkeyedhashalgorithmclass.asp)
58         */
59         internal class HMAC : System.Security.Cryptography.KeyedHashAlgorithm
60         {
61                 #region Fields
62
63                 private HashAlgorithm   hash;
64                 private bool                    hashing;
65
66                 private byte[]                  innerPad;
67                 private byte[]                  outerPad;
68
69                 #endregion
70
71                 #region Properties
72         
73                 public override byte[] Key
74                 {
75                         get { return (byte[])KeyValue.Clone(); }
76                         set
77                         {
78                                 if (hashing)
79                                 {
80                                         throw new Exception("Cannot change key during hash operation.");
81                                 }
82
83                                 /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */
84                                 if (value.Length > 64)
85                                 {
86                                         KeyValue = hash.ComputeHash(value);
87                                 }
88                                 else
89                                 {
90                                         KeyValue = (byte[])value.Clone();
91                                 }
92
93                                 initializePad();
94                         }
95                 }
96
97                 #endregion
98
99                 #region Constructors
100
101                 public HMAC()
102                 {
103                         // Create the hash
104                         hash = MD5.Create();
105                         // Set HashSizeValue
106                         HashSizeValue = hash.HashSize;
107
108                         // Generate a radom key
109                         byte[] rgbKey = new byte[64];
110                         RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
111                         rng.GetNonZeroBytes(rgbKey);
112
113                         KeyValue = (byte[])rgbKey.Clone();
114
115                         this.Initialize();
116                 }
117
118                 public HMAC(string hashName, byte[] rgbKey)
119                 {
120                         // Create the hash
121                         if (hashName == null || hashName.Length == 0)
122                         {
123                                 hashName = "MD5";
124                         }
125                         hash = HashAlgorithm.Create(hashName);
126                         // Set HashSizeValue
127                         HashSizeValue = hash.HashSize;
128
129                         /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */
130                         if (rgbKey.Length > 64)
131                         {
132                                 KeyValue = hash.ComputeHash(rgbKey);
133                         }
134                         else
135                         {
136                                 KeyValue = (byte[])rgbKey.Clone();
137                         }
138
139                         this.Initialize();
140                 }
141
142                 #endregion
143
144                 #region Methods
145
146                 public override void Initialize()
147                 {
148                         hash.Initialize();
149                         initializePad();
150                         hashing = false;
151                 }
152
153                 protected override byte[] HashFinal()
154                 {
155                         if (!hashing)
156                         {
157                                 hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0);
158                                 hashing = true;
159                         }
160                         // Finalize the original hash
161                         hash.TransformFinalBlock(new byte[0], 0, 0);
162
163                         byte[] firstResult = hash.Hash;
164
165                         hash.Initialize();
166                         hash.TransformBlock(outerPad, 0, outerPad.Length, outerPad, 0);
167                         hash.TransformFinalBlock(firstResult, 0, firstResult.Length);
168                         
169                         Initialize();
170
171                         return hash.Hash;
172                 }
173
174                 protected override void HashCore(
175                         byte[] array,
176                         int ibStart,
177                         int cbSize)
178                 {
179                         if (!hashing)
180                         {
181                                 hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0);
182                                 hashing = true;
183                         }
184                         hash.TransformBlock(array, ibStart, cbSize, array, ibStart);
185                 }
186
187                 #endregion
188
189                 #region Private Methods
190
191                 private void initializePad()
192                 {
193                         // Fill pad arrays
194                         innerPad = new byte[64];
195                         outerPad = new byte[64];
196
197                         /* Pad the key for inner and outer digest */
198                         for (int i = 0 ; i < KeyValue.Length; ++i)
199                         {
200                                 innerPad[i] = (byte)(KeyValue[i] ^ 0x36);
201                                 outerPad[i] = (byte)(KeyValue[i] ^ 0x5C);
202                         }
203                         for (int i = KeyValue.Length; i < 64; ++i) 
204                         {
205                                 innerPad[i] = 0x36;
206                                 outerPad[i] = 0x5C;
207                         }
208                 }
209
210                 #endregion
211         }
212 }