do not check order sequence if option /order was not used
[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-2006 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         [ComVisible (true)]
38         public abstract class SymmetricAlgorithm : IDisposable {
39                 protected int BlockSizeValue; 
40                 protected byte[] IVValue; 
41                 protected int KeySizeValue; 
42                 protected byte[] KeyValue; 
43                 protected KeySizes[] LegalBlockSizesValue; 
44                 protected KeySizes[] LegalKeySizesValue; 
45 #if MOONLIGHT
46                 // Silverlight 2.0 only supports CBC
47                 internal int FeedbackSizeValue;
48                 internal CipherMode ModeValue;
49                 internal PaddingMode PaddingValue;
50 #else
51                 protected int FeedbackSizeValue;
52                 protected CipherMode ModeValue;
53                 protected PaddingMode PaddingValue;
54 #endif
55                 private bool m_disposed;
56
57                 protected SymmetricAlgorithm ()
58                 {
59                         ModeValue = CipherMode.CBC;
60                         PaddingValue = PaddingMode.PKCS7;
61                 }
62
63 #if NET_4_0
64                 public void Dispose ()
65 #else
66                 void IDisposable.Dispose () 
67 #endif
68                 {
69                         Dispose (true);
70                         GC.SuppressFinalize (this);  // Finalization is now unnecessary
71                 }
72
73                 public void Clear() 
74                 {
75                         Dispose (true);
76                 }
77
78                 protected virtual void Dispose (bool disposing) 
79                 {
80                         if (!m_disposed) {
81                                 // always zeroize keys
82                                 if (KeyValue != null) {
83                                         // Zeroize the secret key and free
84                                         Array.Clear (KeyValue, 0, KeyValue.Length);
85                                         KeyValue = null;
86                                 }
87                                 // dispose unmanaged managed objects
88                                 if (disposing) {
89                                         // dispose managed objects
90                                 }
91                                 m_disposed = true;
92                         }
93                 }
94
95                 public virtual int BlockSize {
96                         get { return this.BlockSizeValue; }
97                         set {
98                                 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
99                                         throw new CryptographicException (
100                                                 Locale.GetText ("block size not supported by algorithm"));
101                                 }
102                                 // re-setting the same BlockSize *doesn't* regenerate the IV
103                                 if (BlockSizeValue != value) {
104                                         BlockSizeValue = value;
105                                         IVValue = null;
106                                 }
107                         }
108                 }
109
110                 public virtual int FeedbackSize {
111                         get { return this.FeedbackSizeValue; }
112                         set {
113                                 if ((value <= 0) || (value > this.BlockSizeValue)) {
114                                         throw new CryptographicException (
115                                                 Locale.GetText ("feedback size larger than block size"));
116                                 }
117                                 this.FeedbackSizeValue = value;
118                         }
119                 }
120                 
121                 public virtual byte[] IV {
122                         get {
123                                 if (this.IVValue == null)
124                                         GenerateIV();
125
126                                 return (byte[]) this.IVValue.Clone ();
127                         }
128                         set {
129                                 if (value == null)
130                                         throw new ArgumentNullException ("IV");
131                                 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
132                                 if ((value.Length << 3) != this.BlockSizeValue) {
133                                         throw new CryptographicException (
134                                                 Locale.GetText ("IV length is different than block size"));
135                                 }
136                                 this.IVValue = (byte[]) value.Clone ();
137                         }
138                 }
139
140                 public virtual byte[] Key {
141                         get {
142                                 if (this.KeyValue == null)
143                                         GenerateKey();
144
145                                 return (byte[]) this.KeyValue.Clone ();
146                         }
147                         set {
148                                 if (value == null)
149                                         throw new ArgumentNullException ("Key");
150
151                                 int length = (value.Length << 3);
152                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
153                                         throw new CryptographicException (
154                                                 Locale.GetText ("Key size not supported by algorithm"));
155                                 }
156                                 this.KeySizeValue = length;
157                                 this.KeyValue = (byte[]) value.Clone ();
158                         }
159                 }
160                 
161                 public virtual int KeySize {
162                         get { return this.KeySizeValue; }
163                         set {
164                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
165                                         throw new CryptographicException (
166                                                 Locale.GetText ("Key size not supported by algorithm"));
167                                 }
168                                 // re-setting the same KeySize *does* regenerate the key
169                                 KeySizeValue = value;
170                                 KeyValue = null;
171                         }
172                 }
173
174                 public virtual KeySizes[] LegalBlockSizes {
175                         get { return this.LegalBlockSizesValue; }
176                 }
177
178                 public virtual KeySizes[] LegalKeySizes {
179                         get { return this.LegalKeySizesValue; }
180                 }
181
182                 public virtual CipherMode Mode {
183                         get { return this.ModeValue; }
184                         set {
185                                 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
186                                         throw new CryptographicException (
187                                                 Locale.GetText ("Cipher mode not available"));
188                                 }
189                                 
190                                 this.ModeValue = value;
191                         }
192                 }
193
194                 public virtual PaddingMode Padding {
195                         get { return this.PaddingValue; }
196                         set {
197                                 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
198                                         throw new CryptographicException (
199                                                 Locale.GetText ("Padding mode not available"));
200                                 }
201                                 
202                                 this.PaddingValue = value;
203                         }
204                 }
205
206                 public virtual ICryptoTransform CreateDecryptor () 
207                 {
208                         return CreateDecryptor (Key, IV);
209                 }
210
211                 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
212
213                 public virtual ICryptoTransform CreateEncryptor() 
214                 {
215                         return CreateEncryptor (Key, IV);
216                 }
217
218                 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
219
220                 public abstract void GenerateIV ();
221
222                 public abstract void GenerateKey ();
223
224                 public bool ValidKeySize (int bitLength) 
225                 {
226                         return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
227                 }
228                 
229                 // LAMESPEC: Default is Rijndael - not TripleDES
230                 public static SymmetricAlgorithm Create () 
231                 {
232                         return Create ("System.Security.Cryptography.SymmetricAlgorithm");
233                 }
234
235                 public static SymmetricAlgorithm Create (string algName) 
236                 {
237                         return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
238                 }
239         }
240 }