* Mono.Posix.dll.sources: Rename Mono.Posix to Mono.Unix.
[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 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Globalization;
37 using Mono.Security.Cryptography;
38
39 namespace System.Security.Cryptography {
40
41         public abstract class SymmetricAlgorithm : IDisposable {
42                 protected int BlockSizeValue; 
43                 protected int FeedbackSizeValue; 
44                 protected byte[] IVValue; 
45                 protected int KeySizeValue; 
46                 protected byte[] KeyValue; 
47                 protected KeySizes[] LegalBlockSizesValue; 
48                 protected KeySizes[] LegalKeySizesValue; 
49                 protected CipherMode ModeValue; 
50                 protected PaddingMode PaddingValue; 
51                 private bool m_disposed;
52
53                 public SymmetricAlgorithm () 
54                 {
55                         ModeValue = CipherMode.CBC;
56                         PaddingValue = PaddingMode.PKCS7;
57                         m_disposed = false;
58                 }
59                 
60                 ~SymmetricAlgorithm () 
61                 {
62                         Dispose (false);
63                 }
64
65                 public void Clear() 
66                 {
67                         Dispose (true);
68                 }
69
70                 void IDisposable.Dispose () 
71                 {
72                         Dispose (true);
73                         GC.SuppressFinalize (this);  // Finalization is now unnecessary
74                 }
75
76                 protected virtual void Dispose (bool disposing) 
77                 {
78                         if (!m_disposed) {
79                                 // always zeroize keys
80                                 if (KeyValue != null) {
81                                         // Zeroize the secret key and free
82                                         Array.Clear (KeyValue, 0, KeyValue.Length);
83                                         KeyValue = null;
84                                 }
85                                 // dispose unmanaged managed objects
86                                 if (disposing) {
87                                         // dispose managed objects
88                                 }
89                                 m_disposed = true;
90                         }
91                 }
92
93                 public virtual int BlockSize {
94                         get { return this.BlockSizeValue; }
95                         set {
96                                 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
97                                         throw new CryptographicException (
98                                                 Locale.GetText ("block size not supported by algorithm"));
99                                 }
100                                 this.BlockSizeValue = value;
101                         }
102                 }
103
104                 public virtual int FeedbackSize {
105                         get { return this.FeedbackSizeValue; }
106                         set {
107                                 if (value > this.BlockSizeValue) {
108                                         throw new CryptographicException (
109                                                 Locale.GetText ("feedback size larger than block size"));
110                                 }
111                                 this.FeedbackSizeValue = value;
112                         }
113                 }
114                 
115                 public virtual byte[] IV {
116                         get {
117                                 if (this.IVValue == null)
118                                         GenerateIV();
119
120                                 return (byte[]) this.IVValue.Clone ();
121                         }
122                         set {
123                                 if (value == null)
124                                         throw new ArgumentNullException ("IV");
125                                         
126                                 if ((value.Length << 3) > this.BlockSizeValue) {
127                                         throw new CryptographicException (
128                                                 Locale.GetText ("IV length cannot be larger than block size"));
129                                 }
130
131                                 this.IVValue = (byte[]) value.Clone ();
132                         }
133                 }
134
135                 public virtual byte[] Key {
136                         get {
137                                 if (this.KeyValue == null)
138                                         GenerateKey();
139
140                                 return (byte[]) this.KeyValue.Clone ();
141                         }
142                         set {
143                                 if (value == null)
144                                         throw new ArgumentNullException ("Key");
145
146                                 int length = (value.Length << 3);
147                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
148                                         throw new CryptographicException (
149                                                 Locale.GetText ("Key size not supported by algorithm"));
150                                 }
151
152                                 this.KeySizeValue = length;
153                                 this.KeyValue = (byte[]) value.Clone ();
154                         }
155                 }
156                 
157                 public virtual int KeySize {
158                         get { return this.KeySizeValue; }
159                         set {
160                                 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
161                                         throw new CryptographicException (
162                                                 Locale.GetText ("Key size not supported by algorithm"));
163                                 }
164                                 
165                                 this.KeyValue = null;
166                                 this.KeySizeValue = value;
167                         }
168                 }
169
170                 public virtual KeySizes[] LegalBlockSizes {
171                         get { return this.LegalBlockSizesValue; }
172                 }
173
174                 public virtual KeySizes[] LegalKeySizes {
175                         get { return this.LegalKeySizesValue; }
176                 }
177
178                 public virtual CipherMode Mode {
179                         get { return this.ModeValue; }
180                         set {
181                                 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
182                                         throw new CryptographicException (
183                                                 Locale.GetText ("Cipher mode not available"));
184                                 }
185                                 
186                                 this.ModeValue = value;
187                         }
188                 }
189
190                 public virtual PaddingMode Padding {
191                         get { return this.PaddingValue; }
192                         set {
193                                 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
194                                         throw new CryptographicException (
195                                                 Locale.GetText ("Padding mode not available"));
196                                 }
197                                 
198                                 this.PaddingValue = value;
199                         }
200                 }
201
202                 public virtual ICryptoTransform CreateDecryptor () 
203                 {
204                         return CreateDecryptor (Key, IV);
205                 }
206
207                 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
208
209                 public virtual ICryptoTransform CreateEncryptor() 
210                 {
211                         return CreateEncryptor (Key, IV);
212                 }
213
214                 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
215
216                 public abstract void GenerateIV ();
217
218                 public abstract void GenerateKey ();
219
220                 public bool ValidKeySize (int bitLength) 
221                 {
222                         return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
223                 }
224                 
225                 // LAMESPEC: Default is Rijndael - not TripleDES
226                 public static SymmetricAlgorithm Create () 
227                 {
228                         return Create ("System.Security.Cryptography.SymmetricAlgorithm");
229                 }
230
231                 public static SymmetricAlgorithm Create (string algName) 
232                 {
233                         return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
234                 }
235         }
236 }