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