(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Security.Cryptography / HMAC.cs
bloba72fb7b4ee36fdc097974a71d198b7c27accb134
1 //
2 // HMAC.cs: Generic HMAC inplementation
3 //
4 // Author:
5 // Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #if NET_2_0
35 using System;
37 using Mono.Security.Cryptography;
39 namespace System.Security.Cryptography {
41 // Mostly copied from (internal) Mono.Security.Cryptography.HMACAlgorithm
43 // References:
44 // a. FIPS PUB 198: The Keyed-Hash Message Authentication Code (HMAC), 2002 March.
45 // http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf
46 // b. Internet RFC 2104, HMAC, Keyed-Hashing for Message Authentication
47 // (include C source for HMAC-MD5)
48 // http://www.ietf.org/rfc/rfc2104.txt
49 // c. IETF RFC2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
50 // (include C source for HMAC-MD5 and HAMAC-SHA1)
51 // http://www.ietf.org/rfc/rfc2202.txt
52 // d. ANSI X9.71, Keyed Hash Message Authentication Code.
53 // not free :-(
54 // http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E71%2D2000
56 public abstract class HMAC : KeyedHashAlgorithm {
58 private bool _disposed;
59 private string _hashName;
60 private HashAlgorithm _algo;
61 private BlockProcessor _block;
62 protected Int32 BlockSizeValue;
64 // constructors
66 protected HMAC ()
68 _disposed = false;
71 // properties
73 public string HashName {
74 get { return _hashName; }
75 set {
76 _hashName = value;
77 _algo = HashAlgorithm.Create (_hashName);
78 _block = new BlockProcessor (_algo, 8);
82 public override byte[] Key {
83 get { return (byte[]) base.Key.Clone (); }
84 set {
85 if ((value != null) && (value.Length > 64))
86 base.Key = _algo.ComputeHash (value);
87 else
88 base.Key = (byte[]) value.Clone();
92 // methods
94 private byte[] KeySetup (byte[] key, byte padding)
96 byte[] buf = new byte [64];
98 for (int i = 0; i < key.Length; ++i)
99 buf [i] = (byte) ((byte) key [i] ^ padding);
101 for (int i = key.Length; i < 64; ++i)
102 buf [i] = padding;
104 return buf;
107 protected override void Dispose (bool disposing)
109 if (!_disposed) {
110 base.Dispose (disposing);
114 protected override void HashCore (byte[] rgb, int ib, int cb)
116 if (_disposed)
117 throw new ObjectDisposedException ("HMACSHA1");
119 if (State == 0) {
120 Initialize ();
121 State = 1;
123 _block.Core (rgb, ib, cb);
126 protected override byte[] HashFinal ()
128 if (_disposed)
129 throw new ObjectDisposedException ("HMAC");
130 State = 0;
132 _block.Final ();
133 byte[] intermediate = _algo.Hash;
135 byte[] buf = KeySetup (Key, 0x5C);
136 _algo.Initialize ();
137 _algo.TransformBlock (buf, 0, buf.Length, buf, 0);
138 _algo.TransformFinalBlock (intermediate, 0, intermediate.Length);
139 byte[] hash = _algo.Hash;
140 _algo.Clear ();
141 // zeroize sensitive data
142 Array.Clear (buf, 0, buf.Length);
143 Array.Clear (intermediate, 0, intermediate.Length);
144 return hash;
147 public override void Initialize ()
149 if (_disposed)
150 throw new ObjectDisposedException ("HMAC");
152 State = 0;
153 _block.Initialize ();
154 byte[] buf = KeySetup (Key, 0x36);
155 _algo.Initialize ();
156 _block.Core (buf);
157 // zeroize key
158 Array.Clear (buf, 0, buf.Length);
161 // static methods
163 public static new HMAC Create ()
165 return Create ("System.Security.Cryptography.HMAC");
168 public static new HMAC Create (string algName)
170 return (HMAC) CryptoConfig.CreateFromName (algName);
175 #endif