Add license and copyright to all source files in corlib
[mono.git] / mcs / class / corlib / System.Security.Cryptography / HMAC.cs
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 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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.
31 //
32
33 #if NET_2_0
34
35 using System;
36
37 using Mono.Security.Cryptography;
38
39 namespace System.Security.Cryptography {
40
41         // Mostly copied from (internal) Mono.Security.Cryptography.HMACAlgorithm
42
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
55
56         public abstract class HMAC : KeyedHashAlgorithm {
57
58                 private bool _disposed;
59                 private string _hashName;
60                 private HashAlgorithm _algo;
61                 private BlockProcessor _block;
62                 protected Int32 BlockSizeValue; 
63
64                 // constructors
65
66                 protected HMAC () 
67                 {
68                         _disposed = false;
69                 }
70
71                 // properties
72
73                 public string HashName {
74                         get { return _hashName; }
75                         set { 
76                                 _hashName = value; 
77                                 _algo = HashAlgorithm.Create (_hashName);
78                                 _block = new BlockProcessor (_algo, 8);
79                         }
80                 }
81
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();
89                         }
90                 }
91
92                 // methods
93
94                 private byte[] KeySetup (byte[] key, byte padding) 
95                 {
96                         byte[] buf = new byte [64];
97         
98                         for (int i = 0; i < key.Length; ++i)
99                                 buf [i] = (byte) ((byte) key [i] ^ padding);
100         
101                         for (int i = key.Length; i < 64; ++i)
102                                 buf [i] = padding;
103                         
104                         return buf;
105                 }
106
107                 protected override void Dispose (bool disposing) 
108                 {
109                         if (!_disposed) {
110                                 base.Dispose (disposing);
111                         }
112                 }
113
114                 protected override void HashCore (byte[] rgb, int ib, int cb) 
115                 {
116                         if (_disposed)
117                                 throw new ObjectDisposedException ("HMACSHA1");
118
119                         if (State == 0) {
120                                 Initialize ();
121                                 State = 1;
122                         }
123                         _block.Core (rgb, ib, cb);
124                 }
125
126                 protected override byte[] HashFinal () 
127                 {
128                         if (_disposed)
129                                 throw new ObjectDisposedException ("HMAC");
130                         State = 0;
131
132                         _block.Final ();
133                         byte[] intermediate = _algo.Hash;
134         
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;
145                 }
146
147                 public override void Initialize () 
148                 {
149                         if (_disposed)
150                                 throw new ObjectDisposedException ("HMAC");
151
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);
159                 }
160
161                 // static methods
162
163                 public static new HMAC Create () 
164                 {
165                         return Create ("System.Security.Cryptography.HMAC");
166                 }
167
168                 public static new HMAC Create (string algName) 
169                 {
170                         return (HMAC) CryptoConfig.CreateFromName (algName);
171                 }
172         }
173 }
174
175 #endif