Add this for backwards compatibility
[mono.git] / mcs / class / corlib / System.Security.Cryptography / SymmetricAlgorithm.cs
1 //
2 // System.Security.Cryptography SymmetricAlgorithm Class implementation
3 //
4 // Authors:
5 //   Thomas Neidhart (tome@sbox.tugraz.at)
6 //   Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33 using Mono.Security.Cryptography;
34
35 namespace System.Security.Cryptography {
36
37 #if NET_2_0
38         [ComVisible (true)]
39 #endif
40         public abstract class SymmetricAlgorithm : IDisposable {
41                 protected int BlockSizeValue; 
42                 protected int FeedbackSizeValue; 
43                 protected byte[] IVValue; 
44                 protected int KeySizeValue; 
45                 protected byte[] KeyValue; 
46                 protected KeySizes[] LegalBlockSizesValue; 
47                 protected KeySizes[] LegalKeySizesValue; 
48                 protected CipherMode ModeValue; 
49                 protected PaddingMode PaddingValue; 
50                 private bool m_disposed;
51
52                 public SymmetricAlgorithm () 
53                 {
54                         ModeValue = CipherMode.CBC;
55                         PaddingValue = PaddingMode.PKCS7;
56                         m_disposed = false;
57                 }
58                 
59                 ~SymmetricAlgorithm () 
60                 {
61                         Dispose (false);
62                 }
63
64                 public void Clear() 
65                 {
66                         Dispose (true);
67                 }
68
69                 void IDisposable.Dispose () 
70                 {
71                         Dispose (true);
72                         GC.SuppressFinalize (this);  // Finalization is now unnecessary
73                 }
74
75                 protected virtual void Dispose (bool disposing) 
76                 {
77                         if (!m_disposed) {
78                                 // always zeroize keys
79                                 if (KeyValue != null) {
80                                         // Zeroize the secret key and free
81                                         Array.Clear (KeyValue, 0, KeyValue.Length);
82                                         KeyValue = null;
83                                 }
84                                 // dispose unmanaged managed objects
85                                 if (disposing) {
86                                         // dispose managed objects
87                                 }
88                                 m_disposed = true;
89                         }
90                 }
91
92                 public virtual int BlockSize {
93                         get { return this.BlockSizeValue; }
94                         set {
95                                 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
96                                         throw new CryptographicException (
97                                                 Locale.GetText ("block size not supported by algorithm"));
98                                 }
99                                 // re-setting the same BlockSize *doesn't* regenerate the IV
100                                 if (BlockSizeValue != value) {
101                                         BlockSizeValue = value;
102                                         IVValue = null;
103                                 }
104                         }
105                 }
106
107                 public virtual int FeedbackSize {
108                         get { return this.FeedbackSizeValue; }
109                         set {
110 #if NET_2_0
111                                 if ((value <= 0) || (value > this.BlockSizeValue)) {
112 #else
113                                 if (value > this.BlockSizeValue) {
114 #endif
115                                         throw new CryptographicException (
116                                                 Locale.GetText ("feedback size larger than block size"));
117                                 }
118                                 this.FeedbackSizeValue = value;
119                         }
120                 }
121                 
122                 public virtual byte[] IV {
123                         get {
124                                 if (this.IVValue == null)
125                                         GenerateIV();
126
127                                 return (byte[]) this.IVValue.Clone ();
128                         }
129                         set {
130                                 if (value == null)
131                                         throw new ArgumentNullException ("IV");
132 #if NET_2_0
133                                 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
134                                 if ((value.Length << 3) != this.BlockSizeValue) {
135                                         throw new CryptographicException (
136                                                 Locale.GetText ("IV length is different than block size"));
137                                 }
138 #else                                   
139                                 if ((value.Length << 3) > this.BlockSizeValue) {
140                                         throw new CryptographicException (
141                                                 Locale.GetText ("IV length cannot be larger than block size"));
142                                 }
143 #endif
144                                 this.IVValue = (byte[]) value.Clone ();
145                         }
146                 }
147
148                 public virtual byte[] Key {
149                         get {
150                                 if (this.KeyValue == null)
151                                         GenerateKey();
152
153                                 return (byte[]) this.KeyValue.Clone ();
154                         }
155                         set {
156                                 if (value == null)
157                                         throw new ArgumentNullException ("Key");
158
159                                 int length = (value.Length << 3);
160                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
161                                         throw new CryptographicException (
162                                                 Locale.GetText ("Key size not supported by algorithm"));
163                                 }
164                                 this.KeySizeValue = length;
165                                 this.KeyValue = (byte[]) value.Clone ();
166                         }
167                 }
168                 
169                 public virtual int KeySize {
170                         get { return this.KeySizeValue; }
171                         set {
172                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
173                                         throw new CryptographicException (
174                                                 Locale.GetText ("Key size not supported by algorithm"));
175                                 }
176                                 // re-setting the same KeySize *does* regenerate the key
177                                 KeySizeValue = value;
178                                 KeyValue = null;
179                         }
180                 }
181
182                 public virtual KeySizes[] LegalBlockSizes {
183                         get { return this.LegalBlockSizesValue; }
184                 }
185
186                 public virtual KeySizes[] LegalKeySizes {
187                         get { return this.LegalKeySizesValue; }
188                 }
189
190                 public virtual CipherMode Mode {
191                         get { return this.ModeValue; }
192                         set {
193                                 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
194                                         throw new CryptographicException (
195                                                 Locale.GetText ("Cipher mode not available"));
196                                 }
197                                 
198                                 this.ModeValue = value;
199                         }
200                 }
201
202                 public virtual PaddingMode Padding {
203                         get { return this.PaddingValue; }
204                         set {
205                                 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
206                                         throw new CryptographicException (
207                                                 Locale.GetText ("Padding mode not available"));
208                                 }
209                                 
210                                 this.PaddingValue = value;
211                         }
212                 }
213
214                 public virtual ICryptoTransform CreateDecryptor () 
215                 {
216                         return CreateDecryptor (Key, IV);
217                 }
218
219                 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
220
221                 public virtual ICryptoTransform CreateEncryptor() 
222                 {
223                         return CreateEncryptor (Key, IV);
224                 }
225
226                 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
227
228                 public abstract void GenerateIV ();
229
230                 public abstract void GenerateKey ();
231
232                 public bool ValidKeySize (int bitLength) 
233                 {
234                         return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
235                 }
236                 
237                 // LAMESPEC: Default is Rijndael - not TripleDES
238                 public static SymmetricAlgorithm Create () 
239                 {
240                         return Create ("System.Security.Cryptography.SymmetricAlgorithm");
241                 }
242
243                 public static SymmetricAlgorithm Create (string algName) 
244                 {
245                         return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
246                 }
247         }
248 }