2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Security.Cryptography / SymmetricAlgorithm.cs
blob2ee27a7fabcbdea3b57f808622b05abb2c89b935
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)
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.
31 using System.Globalization;
32 using System.Runtime.InteropServices;
33 using Mono.Security.Cryptography;
35 namespace System.Security.Cryptography {
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 #if MOONLIGHT
46 // Silverlight 2.0 only supports CBC
47 internal int FeedbackSizeValue;
48 internal CipherMode ModeValue;
49 internal PaddingMode PaddingValue;
50 #else
51 protected int FeedbackSizeValue;
52 protected CipherMode ModeValue;
53 protected PaddingMode PaddingValue;
54 #endif
55 private bool m_disposed;
57 protected SymmetricAlgorithm ()
59 ModeValue = CipherMode.CBC;
60 PaddingValue = PaddingMode.PKCS7;
61 m_disposed = false;
64 #if MOONLIGHT
65 // No Finalizer or IDisposable.Dispose in Silverlight 2.0
66 // Documentation makes it "clear" that Clear MUST BE CALLED to zero out sensitive information
67 #else
68 ~SymmetricAlgorithm ()
70 Dispose (false);
72 #endif
73 void IDisposable.Dispose ()
75 Dispose (true);
76 GC.SuppressFinalize (this); // Finalization is now unnecessary
79 public void Clear()
81 Dispose (true);
84 protected virtual void Dispose (bool disposing)
86 if (!m_disposed) {
87 // always zeroize keys
88 if (KeyValue != null) {
89 // Zeroize the secret key and free
90 Array.Clear (KeyValue, 0, KeyValue.Length);
91 KeyValue = null;
93 // dispose unmanaged managed objects
94 if (disposing) {
95 // dispose managed objects
97 m_disposed = true;
101 public virtual int BlockSize {
102 get { return this.BlockSizeValue; }
103 set {
104 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
105 throw new CryptographicException (
106 Locale.GetText ("block size not supported by algorithm"));
108 // re-setting the same BlockSize *doesn't* regenerate the IV
109 if (BlockSizeValue != value) {
110 BlockSizeValue = value;
111 IVValue = null;
116 public virtual int FeedbackSize {
117 get { return this.FeedbackSizeValue; }
118 set {
119 if ((value <= 0) || (value > this.BlockSizeValue)) {
120 throw new CryptographicException (
121 Locale.GetText ("feedback size larger than block size"));
123 this.FeedbackSizeValue = value;
127 public virtual byte[] IV {
128 get {
129 if (this.IVValue == null)
130 GenerateIV();
132 return (byte[]) this.IVValue.Clone ();
134 set {
135 if (value == null)
136 throw new ArgumentNullException ("IV");
137 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
138 if ((value.Length << 3) != this.BlockSizeValue) {
139 throw new CryptographicException (
140 Locale.GetText ("IV length is different than block size"));
142 this.IVValue = (byte[]) value.Clone ();
146 public virtual byte[] Key {
147 get {
148 if (this.KeyValue == null)
149 GenerateKey();
151 return (byte[]) this.KeyValue.Clone ();
153 set {
154 if (value == null)
155 throw new ArgumentNullException ("Key");
157 int length = (value.Length << 3);
158 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
159 throw new CryptographicException (
160 Locale.GetText ("Key size not supported by algorithm"));
162 this.KeySizeValue = length;
163 this.KeyValue = (byte[]) value.Clone ();
167 public virtual int KeySize {
168 get { return this.KeySizeValue; }
169 set {
170 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
171 throw new CryptographicException (
172 Locale.GetText ("Key size not supported by algorithm"));
174 // re-setting the same KeySize *does* regenerate the key
175 KeySizeValue = value;
176 KeyValue = null;
180 public virtual KeySizes[] LegalBlockSizes {
181 get { return this.LegalBlockSizesValue; }
184 public virtual KeySizes[] LegalKeySizes {
185 get { return this.LegalKeySizesValue; }
188 public virtual CipherMode Mode {
189 get { return this.ModeValue; }
190 set {
191 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
192 throw new CryptographicException (
193 Locale.GetText ("Cipher mode not available"));
196 this.ModeValue = value;
200 public virtual PaddingMode Padding {
201 get { return this.PaddingValue; }
202 set {
203 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
204 throw new CryptographicException (
205 Locale.GetText ("Padding mode not available"));
208 this.PaddingValue = value;
212 public virtual ICryptoTransform CreateDecryptor ()
214 return CreateDecryptor (Key, IV);
217 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
219 public virtual ICryptoTransform CreateEncryptor()
221 return CreateEncryptor (Key, IV);
224 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
226 public abstract void GenerateIV ();
228 public abstract void GenerateKey ();
230 public bool ValidKeySize (int bitLength)
232 return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
235 // LAMESPEC: Default is Rijndael - not TripleDES
236 public static SymmetricAlgorithm Create ()
238 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
241 public static SymmetricAlgorithm Create (string algName)
243 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);