1 /* Transport Security Layer (TLS)
\r
2 * Copyright (c) 2003 Carlos Guzmán Álvarez
\r
4 * Permission is hereby granted, free of charge, to any person
\r
5 * obtaining a copy of this software and associated documentation
\r
6 * files (the "Software"), to deal in the Software without restriction,
\r
7 * including without limitation the rights to use, copy, modify, merge,
\r
8 * publish, distribute, sublicense, and/or sell copies of the Software,
\r
9 * and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included
\r
13 * in all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
\r
17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
\r
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
\r
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
22 * DEALINGS IN THE SOFTWARE.
\r
26 using System.Security.Cryptography;
\r
28 namespace Mono.Security.Cryptography
\r
32 * RFC 2104 (http://www.ietf.org/rfc/rfc2104.txt)
\r
33 * RFC 2202 (http://www.ietf.org/rfc/rfc2202.txt)
\r
36 * Extending the KeyedHashAlgorithm Class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconextendingkeyedhashalgorithmclass.asp)
\r
38 internal class HMAC : System.Security.Cryptography.KeyedHashAlgorithm
\r
42 private HashAlgorithm hash;
\r
43 private bool hashing;
\r
45 private byte[] innerPad;
\r
46 private byte[] outerPad;
\r
52 public override byte[] Key
\r
54 get { return (byte[])KeyValue.Clone(); }
\r
59 throw new Exception("Cannot change key during hash operation.");
\r
62 /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */
\r
63 if (value.Length > 64)
\r
65 KeyValue = hash.ComputeHash(value);
\r
69 KeyValue = (byte[])value.Clone();
\r
78 #region CONSTRUCTORS
\r
83 hash = HashAlgorithm.Create("MD5");
\r
84 // Set HashSizeValue
\r
85 HashSizeValue = hash.HashSize;
\r
87 // Generate a radom key
\r
88 byte[] rgbKey = new byte[64];
\r
89 RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
\r
90 rng.GetNonZeroBytes(rgbKey);
\r
92 KeyValue = (byte[])rgbKey.Clone();
\r
97 public HMAC(string hashName, byte[] rgbKey)
\r
100 if (hashName == null || hashName.Length == 0)
\r
104 hash = HashAlgorithm.Create(hashName);
\r
105 // Set HashSizeValue
\r
106 HashSizeValue = hash.HashSize;
\r
108 /* if key is longer than 64 bytes reset it to rgbKey = Hash(rgbKey) */
\r
109 if (rgbKey.Length > 64)
\r
111 KeyValue = hash.ComputeHash(rgbKey);
\r
115 KeyValue = (byte[])rgbKey.Clone();
\r
125 public override void Initialize()
\r
132 protected override byte[] HashFinal()
\r
136 hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0);
\r
139 // Finalize the original hash
\r
140 hash.TransformFinalBlock(new byte[0], 0, 0);
\r
142 byte[] firstResult = hash.Hash;
\r
145 hash.TransformBlock(outerPad, 0, outerPad.Length, outerPad, 0);
\r
146 hash.TransformFinalBlock(firstResult, 0, firstResult.Length);
\r
153 protected override void HashCore(
\r
160 hash.TransformBlock(innerPad, 0, innerPad.Length, innerPad, 0);
\r
163 hash.TransformBlock(array, ibStart, cbSize, array, ibStart);
\r
168 #region PRIVATE_METHODS
\r
170 private void initializePad()
\r
173 innerPad = new byte[64];
\r
174 outerPad = new byte[64];
\r
176 /* Pad the key for inner and outer digest */
\r
177 for (int i = 0 ; i < KeyValue.Length; ++i)
\r
179 innerPad[i] = (byte)(KeyValue[i] ^ 0x36);
\r
180 outerPad[i] = (byte)(KeyValue[i] ^ 0x5C);
\r
182 for (int i = KeyValue.Length; i < 64; ++i)
\r
184 innerPad[i] = 0x36;
\r
185 outerPad[i] = 0x5C;
\r