2 // System.Security.Cryptography SymmetricAlgorithm Class implementation
5 // Thomas Neidhart (tome@sbox.tugraz.at)
6 // Sebastien Pouliot <sebastien@ximian.com>
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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 {
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;
46 // Silverlight 2.0 only supports CBC
47 internal int FeedbackSizeValue;
48 internal CipherMode ModeValue;
49 internal PaddingMode PaddingValue;
51 protected int FeedbackSizeValue;
52 protected CipherMode ModeValue;
53 protected PaddingMode PaddingValue;
55 private bool m_disposed;
57 protected SymmetricAlgorithm ()
59 ModeValue = CipherMode.CBC;
60 PaddingValue = PaddingMode.PKCS7;
64 public void Dispose ()
66 void IDisposable.Dispose ()
70 GC.SuppressFinalize (this); // Finalization is now unnecessary
78 protected virtual void Dispose (bool disposing)
81 // always zeroize keys
82 if (KeyValue != null) {
83 // Zeroize the secret key and free
84 Array.Clear (KeyValue, 0, KeyValue.Length);
87 // dispose unmanaged managed objects
89 // dispose managed objects
95 public virtual int BlockSize {
96 get { return this.BlockSizeValue; }
98 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
99 throw new CryptographicException (
100 Locale.GetText ("block size not supported by algorithm"));
102 // re-setting the same BlockSize *doesn't* regenerate the IV
103 if (BlockSizeValue != value) {
104 BlockSizeValue = value;
110 public virtual int FeedbackSize {
111 get { return this.FeedbackSizeValue; }
113 if ((value <= 0) || (value > this.BlockSizeValue)) {
114 throw new CryptographicException (
115 Locale.GetText ("feedback size larger than block size"));
117 if ((value & 3) != 0) {
118 throw new CryptographicException (
119 Locale.GetText ("feedback size must be a multiple of 8 (bits)"));
121 this.FeedbackSizeValue = value;
125 public virtual byte[] IV {
127 if (this.IVValue == null)
130 return (byte[]) this.IVValue.Clone ();
134 throw new ArgumentNullException ("IV");
135 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
136 if ((value.Length << 3) != this.BlockSizeValue) {
137 throw new CryptographicException (
138 Locale.GetText ("IV length is different than block size"));
140 this.IVValue = (byte[]) value.Clone ();
144 public virtual byte[] Key {
146 if (this.KeyValue == null)
149 return (byte[]) this.KeyValue.Clone ();
153 throw new ArgumentNullException ("Key");
155 int length = (value.Length << 3);
156 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
157 throw new CryptographicException (
158 Locale.GetText ("Key size not supported by algorithm"));
160 this.KeySizeValue = length;
161 this.KeyValue = (byte[]) value.Clone ();
165 public virtual int KeySize {
166 get { return this.KeySizeValue; }
168 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
169 throw new CryptographicException (
170 Locale.GetText ("Key size not supported by algorithm"));
172 // re-setting the same KeySize *does* regenerate the key
173 KeySizeValue = value;
178 public virtual KeySizes[] LegalBlockSizes {
179 get { return this.LegalBlockSizesValue; }
182 public virtual KeySizes[] LegalKeySizes {
183 get { return this.LegalKeySizesValue; }
186 public virtual CipherMode Mode {
187 get { return this.ModeValue; }
189 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
190 throw new CryptographicException (
191 Locale.GetText ("Cipher mode not available"));
194 this.ModeValue = value;
198 public virtual PaddingMode Padding {
199 get { return this.PaddingValue; }
201 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
202 throw new CryptographicException (
203 Locale.GetText ("Padding mode not available"));
206 this.PaddingValue = value;
210 public virtual ICryptoTransform CreateDecryptor ()
212 return CreateDecryptor (Key, IV);
215 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
217 public virtual ICryptoTransform CreateEncryptor()
219 return CreateEncryptor (Key, IV);
222 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
224 public abstract void GenerateIV ();
226 public abstract void GenerateKey ();
228 public bool ValidKeySize (int bitLength)
230 return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
233 // LAMESPEC: Default is Rijndael - not TripleDES
234 public static SymmetricAlgorithm Create ()
237 return new System.Security.Cryptography.RijndaelManaged ();
239 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
243 public static SymmetricAlgorithm Create (string algName)
245 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);