2005-06-10 Sebastien Pouliot <sebastien@ximian.com>
[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                                 this.BlockSizeValue = value;
100                         }
101                 }
102
103                 public virtual int FeedbackSize {
104                         get { return this.FeedbackSizeValue; }
105                         set {
106 #if NET_2_0
107                                 if ((value <= 0) || (value > this.BlockSizeValue)) {
108 #else
109                                 if (value > this.BlockSizeValue) {
110 #endif
111                                         throw new CryptographicException (
112                                                 Locale.GetText ("feedback size larger than block size"));
113                                 }
114                                 this.FeedbackSizeValue = value;
115                         }
116                 }
117                 
118                 public virtual byte[] IV {
119                         get {
120                                 if (this.IVValue == null)
121                                         GenerateIV();
122
123                                 return (byte[]) this.IVValue.Clone ();
124                         }
125                         set {
126                                 if (value == null)
127                                         throw new ArgumentNullException ("IV");
128 #if NET_2_0
129                                 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
130                                 if ((value.Length << 3) != this.BlockSizeValue) {
131                                         throw new CryptographicException (
132                                                 Locale.GetText ("IV length is different than block size"));
133                                 }
134 #else                                   
135                                 if ((value.Length << 3) > this.BlockSizeValue) {
136                                         throw new CryptographicException (
137                                                 Locale.GetText ("IV length cannot be larger than block size"));
138                                 }
139 #endif
140                                 this.IVValue = (byte[]) value.Clone ();
141                         }
142                 }
143
144                 public virtual byte[] Key {
145                         get {
146                                 if (this.KeyValue == null)
147                                         GenerateKey();
148
149                                 return (byte[]) this.KeyValue.Clone ();
150                         }
151                         set {
152                                 if (value == null)
153                                         throw new ArgumentNullException ("Key");
154
155                                 int length = (value.Length << 3);
156                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
157                                         throw new CryptographicException (
158                                                 Locale.GetText ("Key size not supported by algorithm"));
159                                 }
160
161                                 this.KeySizeValue = length;
162                                 this.KeyValue = (byte[]) value.Clone ();
163                         }
164                 }
165                 
166                 public virtual int KeySize {
167                         get { return this.KeySizeValue; }
168                         set {
169                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
170                                         throw new CryptographicException (
171                                                 Locale.GetText ("Key size not supported by algorithm"));
172                                 }
173                                 
174                                 this.KeyValue = null;
175                                 this.KeySizeValue = value;
176                         }
177                 }
178
179                 public virtual KeySizes[] LegalBlockSizes {
180                         get { return this.LegalBlockSizesValue; }
181                 }
182
183                 public virtual KeySizes[] LegalKeySizes {
184                         get { return this.LegalKeySizesValue; }
185                 }
186
187                 public virtual CipherMode Mode {
188                         get { return this.ModeValue; }
189                         set {
190                                 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
191                                         throw new CryptographicException (
192                                                 Locale.GetText ("Cipher mode not available"));
193                                 }
194                                 
195                                 this.ModeValue = value;
196                         }
197                 }
198
199                 public virtual PaddingMode Padding {
200                         get { return this.PaddingValue; }
201                         set {
202                                 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
203                                         throw new CryptographicException (
204                                                 Locale.GetText ("Padding mode not available"));
205                                 }
206                                 
207                                 this.PaddingValue = value;
208                         }
209                 }
210
211                 public virtual ICryptoTransform CreateDecryptor () 
212                 {
213                         return CreateDecryptor (Key, IV);
214                 }
215
216                 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
217
218                 public virtual ICryptoTransform CreateEncryptor() 
219                 {
220                         return CreateEncryptor (Key, IV);
221                 }
222
223                 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
224
225                 public abstract void GenerateIV ();
226
227                 public abstract void GenerateKey ();
228
229                 public bool ValidKeySize (int bitLength) 
230                 {
231                         return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
232                 }
233                 
234                 // LAMESPEC: Default is Rijndael - not TripleDES
235                 public static SymmetricAlgorithm Create () 
236                 {
237                         return Create ("System.Security.Cryptography.SymmetricAlgorithm");
238                 }
239
240                 public static SymmetricAlgorithm Create (string algName) 
241                 {
242                         return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
243                 }
244         }
245 }