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