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;
45 protected int FeedbackSizeValue;
46 protected CipherMode ModeValue;
47 protected PaddingMode PaddingValue;
48 private bool m_disposed;
50 protected SymmetricAlgorithm ()
52 ModeValue = CipherMode.CBC;
53 PaddingValue = PaddingMode.PKCS7;
57 public void Dispose ()
59 void IDisposable.Dispose ()
63 GC.SuppressFinalize (this); // Finalization is now unnecessary
71 protected virtual void Dispose (bool disposing)
74 // always zeroize keys
75 if (KeyValue != null) {
76 // Zeroize the secret key and free
77 Array.Clear (KeyValue, 0, KeyValue.Length);
80 // dispose unmanaged managed objects
82 // dispose managed objects
88 public virtual int BlockSize {
89 get { return this.BlockSizeValue; }
91 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
92 throw new CryptographicException (
93 Locale.GetText ("block size not supported by algorithm"));
95 // re-setting the same BlockSize *doesn't* regenerate the IV
96 if (BlockSizeValue != value) {
97 BlockSizeValue = value;
103 public virtual int FeedbackSize {
104 get { return this.FeedbackSizeValue; }
106 if ((value <= 0) || (value > this.BlockSizeValue)) {
107 throw new CryptographicException (
108 Locale.GetText ("feedback size larger than block size"));
110 if ((value & 3) != 0) {
111 throw new CryptographicException (
112 Locale.GetText ("feedback size must be a multiple of 8 (bits)"));
114 this.FeedbackSizeValue = value;
118 public virtual byte[] IV {
120 if (this.IVValue == null)
123 return (byte[]) this.IVValue.Clone ();
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"));
133 this.IVValue = (byte[]) value.Clone ();
137 public virtual byte[] Key {
139 if (this.KeyValue == null)
142 return (byte[]) this.KeyValue.Clone ();
146 throw new ArgumentNullException ("Key");
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"));
153 this.KeySizeValue = length;
154 this.KeyValue = (byte[]) value.Clone ();
158 public virtual int KeySize {
159 get { return this.KeySizeValue; }
161 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
162 throw new CryptographicException (
163 Locale.GetText ("Key size not supported by algorithm"));
165 // re-setting the same KeySize *does* regenerate the key
166 KeySizeValue = value;
171 public virtual KeySizes[] LegalBlockSizes {
172 get { return this.LegalBlockSizesValue; }
175 public virtual KeySizes[] LegalKeySizes {
176 get { return this.LegalKeySizesValue; }
179 public virtual CipherMode Mode {
180 get { return this.ModeValue; }
182 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
183 throw new CryptographicException (
184 Locale.GetText ("Cipher mode not available"));
187 this.ModeValue = value;
191 public virtual PaddingMode Padding {
192 get { return this.PaddingValue; }
194 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
195 throw new CryptographicException (
196 Locale.GetText ("Padding mode not available"));
199 this.PaddingValue = value;
203 public virtual ICryptoTransform CreateDecryptor ()
205 return CreateDecryptor (Key, IV);
208 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
210 public virtual ICryptoTransform CreateEncryptor()
212 return CreateEncryptor (Key, IV);
215 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
217 public abstract void GenerateIV ();
219 public abstract void GenerateKey ();
221 public bool ValidKeySize (int bitLength)
223 return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
226 // LAMESPEC: Default is Rijndael - not TripleDES
227 public static SymmetricAlgorithm Create ()
230 return new System.Security.Cryptography.RijndaelManaged ();
232 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
236 public static SymmetricAlgorithm Create (string algName)
238 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);