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-2005 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 Mono.Security.Cryptography;
34 namespace System.Security.Cryptography {
36 public abstract class SymmetricAlgorithm : IDisposable {
37 protected int BlockSizeValue;
38 protected int FeedbackSizeValue;
39 protected byte[] IVValue;
40 protected int KeySizeValue;
41 protected byte[] KeyValue;
42 protected KeySizes[] LegalBlockSizesValue;
43 protected KeySizes[] LegalKeySizesValue;
44 protected CipherMode ModeValue;
45 protected PaddingMode PaddingValue;
46 private bool m_disposed;
48 public SymmetricAlgorithm ()
50 ModeValue = CipherMode.CBC;
51 PaddingValue = PaddingMode.PKCS7;
55 ~SymmetricAlgorithm ()
65 void IDisposable.Dispose ()
68 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 this.BlockSizeValue = value;
99 public virtual int FeedbackSize {
100 get { return this.FeedbackSizeValue; }
102 if (value > this.BlockSizeValue) {
103 throw new CryptographicException (
104 Locale.GetText ("feedback size larger than block size"));
106 this.FeedbackSizeValue = value;
110 public virtual byte[] IV {
112 if (this.IVValue == null)
115 return (byte[]) this.IVValue.Clone ();
119 throw new ArgumentNullException ("IV");
121 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
122 if ((value.Length << 3) != this.BlockSizeValue) {
123 throw new CryptographicException (
124 Locale.GetText ("IV length is different than block size"));
127 if ((value.Length << 3) > this.BlockSizeValue) {
128 throw new CryptographicException (
129 Locale.GetText ("IV length cannot be larger than block size"));
132 this.IVValue = (byte[]) value.Clone ();
136 public virtual byte[] Key {
138 if (this.KeyValue == null)
141 return (byte[]) this.KeyValue.Clone ();
145 throw new ArgumentNullException ("Key");
147 int length = (value.Length << 3);
148 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
149 throw new CryptographicException (
150 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"));
166 this.KeyValue = null;
167 this.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 ()
229 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
232 public static SymmetricAlgorithm Create (string algName)
234 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);