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