Merge pull request #505 from roji/shutdown_flow
[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                 protected int FeedbackSizeValue;
46                 protected CipherMode ModeValue;
47                 protected PaddingMode PaddingValue;
48                 private bool m_disposed;
49
50                 protected SymmetricAlgorithm ()
51                 {
52                         ModeValue = CipherMode.CBC;
53                         PaddingValue = PaddingMode.PKCS7;
54                 }
55
56 #if NET_4_0
57                 public void Dispose ()
58 #else
59                 void IDisposable.Dispose () 
60 #endif
61                 {
62                         Dispose (true);
63                         GC.SuppressFinalize (this);  // Finalization is now unnecessary
64                 }
65
66                 public void Clear() 
67                 {
68                         Dispose (true);
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                                 // re-setting the same BlockSize *doesn't* regenerate the IV
96                                 if (BlockSizeValue != value) {
97                                         BlockSizeValue = value;
98                                         IVValue = null;
99                                 }
100                         }
101                 }
102
103                 public virtual int FeedbackSize {
104                         get { return this.FeedbackSizeValue; }
105                         set {
106                                 if ((value <= 0) || (value > this.BlockSizeValue)) {
107                                         throw new CryptographicException (
108                                                 Locale.GetText ("feedback size larger than block size"));
109                                 }
110                                 if ((value & 3) != 0) {
111                                         throw new CryptographicException (
112                                                 Locale.GetText ("feedback size must be a multiple of 8 (bits)"));
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                                 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
129                                 if ((value.Length << 3) != this.BlockSizeValue) {
130                                         throw new CryptographicException (
131                                                 Locale.GetText ("IV length is different than block size"));
132                                 }
133                                 this.IVValue = (byte[]) value.Clone ();
134                         }
135                 }
136
137                 public virtual byte[] Key {
138                         get {
139                                 if (this.KeyValue == null)
140                                         GenerateKey();
141
142                                 return (byte[]) this.KeyValue.Clone ();
143                         }
144                         set {
145                                 if (value == null)
146                                         throw new ArgumentNullException ("Key");
147
148                                 int length = (value.Length << 3);
149                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
150                                         throw new CryptographicException (
151                                                 Locale.GetText ("Key size not supported by algorithm"));
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                                 // re-setting the same KeySize *does* regenerate the key
166                                 KeySizeValue = value;
167                                 KeyValue = null;
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 #if FULL_AOT_RUNTIME
230                         return new System.Security.Cryptography.RijndaelManaged ();
231 #else
232                         return Create ("System.Security.Cryptography.SymmetricAlgorithm");
233 #endif
234                 }
235
236                 public static SymmetricAlgorithm Create (string algName) 
237                 {
238                         return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
239                 }
240         }
241 }