minor fix for bug 9520:
[mono.git] / mcs / class / corlib / System.Security.Cryptography / MACTripleDES.cs
1 //
2 // MACTripleDES.cs: Handles MAC with TripleDES
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Runtime.InteropServices;
31
32 using Mono.Security.Cryptography;
33
34 namespace System.Security.Cryptography {
35
36         // References:
37         // a.   FIPS PUB 81: DES MODES OF OPERATION 
38         //      MAC: Appendix F (MACDES not MACTripleDES but close enough ;-)
39         //      http://www.itl.nist.gov/fipspubs/fip81.htm
40         
41         // LAMESPEC: MACTripleDES == MAC-CBC using TripleDES (not MAC-CFB).
42         [ComVisible (true)]
43         public class MACTripleDES: KeyedHashAlgorithm {
44         
45                 private TripleDES tdes;
46                 private MACAlgorithm mac;
47                 private bool m_disposed;
48         
49                 public MACTripleDES ()
50                 {
51                         Setup ("TripleDES", null);
52                 }
53         
54                 public MACTripleDES (byte[] rgbKey)
55                 {
56                         if (rgbKey == null)
57                                 throw new ArgumentNullException ("rgbKey");
58                         Setup ("TripleDES", rgbKey);
59                 }
60         
61                 public MACTripleDES (string strTripleDES, byte[] rgbKey) 
62                 {
63                         if (rgbKey == null)
64                                 throw new ArgumentNullException ("rgbKey");
65                         if (strTripleDES == null)
66                                 Setup ("TripleDES", rgbKey);
67                         else
68                                 Setup (strTripleDES, rgbKey);
69                 }
70         
71                 private void Setup (string strTripleDES, byte[] rgbKey) 
72                 {
73                         tdes = TripleDES.Create (strTripleDES);
74                         // default padding (as using in Fx 1.0 and 1.1)
75                         tdes.Padding = PaddingMode.Zeros;
76                         // if rgbKey is null we keep the randomly generated key
77                         if (rgbKey != null) {
78                                 // this way we get the TripleDES key validation (like weak
79                                 // and semi-weak keys)
80                                 tdes.Key = rgbKey;
81                         }
82                         HashSizeValue = tdes.BlockSize;
83                         // we use Key property to get the additional validations 
84                         // (from KeyedHashAlgorithm ancestor)
85                         Key = tdes.Key;
86                         mac = new MACAlgorithm (tdes);
87                         m_disposed = false;
88                 }
89
90                 [ComVisible (false)]
91                 public PaddingMode Padding {
92                         get { return tdes.Padding; }
93                         set { tdes.Padding = value; }
94                 }
95
96                 protected override void Dispose (bool disposing) 
97                 {
98                         if (!m_disposed) {
99                                 // note: we ALWAYS zeroize keys (disposing or not)
100         
101                                 // clear our copy of the secret key
102                                 if (KeyValue != null)
103                                         Array.Clear (KeyValue, 0, KeyValue.Length);
104                                 // clear the secret key (inside TripleDES)
105                                 if (tdes != null)
106                                         tdes.Clear ();
107         
108                                 if (disposing) {
109                                         // disposed managed stuff
110                                         KeyValue = null;
111                                         tdes = null;
112                                 }
113                                 // ancestor
114                                 base.Dispose (disposing);
115                                 m_disposed = true;
116                         }
117                 }
118         
119                 public override void Initialize () 
120                 {
121                         if (m_disposed)
122                                 throw new ObjectDisposedException ("MACTripleDES");
123                         State = 0;
124                         mac.Initialize (KeyValue);
125                 }
126         
127                 protected override void HashCore (byte[] rgbData, int ibStart, int cbSize) 
128                 {
129                         if (m_disposed)
130                                 throw new ObjectDisposedException ("MACTripleDES");
131                         if (State == 0) {
132                                 Initialize ();
133                                 State = 1;
134                         }
135                         mac.Core (rgbData, ibStart, cbSize);
136                 }
137         
138                 protected override byte[] HashFinal () 
139                 {
140                         if (m_disposed)
141                                 throw new ObjectDisposedException ("MACTripleDES");
142                         State = 0;
143                         return mac.Final ();
144                 }
145         }
146 }