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