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 System.Runtime.InteropServices;
33 using Mono.Security.Cryptography;
35 namespace System.Security.Cryptography {
40 public abstract class SymmetricAlgorithm : IDisposable {
41 protected int BlockSizeValue;
42 protected int FeedbackSizeValue;
43 protected byte[] IVValue;
44 protected int KeySizeValue;
45 protected byte[] KeyValue;
46 protected KeySizes[] LegalBlockSizesValue;
47 protected KeySizes[] LegalKeySizesValue;
48 protected CipherMode ModeValue;
49 protected PaddingMode PaddingValue;
50 private bool m_disposed;
52 public SymmetricAlgorithm ()
54 ModeValue = CipherMode.CBC;
55 PaddingValue = PaddingMode.PKCS7;
59 ~SymmetricAlgorithm ()
69 void IDisposable.Dispose ()
72 GC.SuppressFinalize (this); // Finalization is now unnecessary
75 protected virtual void Dispose (bool disposing)
78 // always zeroize keys
79 if (KeyValue != null) {
80 // Zeroize the secret key and free
81 Array.Clear (KeyValue, 0, KeyValue.Length);
84 // dispose unmanaged managed objects
86 // dispose managed objects
92 public virtual int BlockSize {
93 get { return this.BlockSizeValue; }
95 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
96 throw new CryptographicException (
97 Locale.GetText ("block size not supported by algorithm"));
99 this.BlockSizeValue = value;
103 public virtual int FeedbackSize {
104 get { return this.FeedbackSizeValue; }
107 if ((value <= 0) || (value > this.BlockSizeValue)) {
109 if (value > this.BlockSizeValue) {
111 throw new CryptographicException (
112 Locale.GetText ("feedback size larger than block size"));
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");
129 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
130 if ((value.Length << 3) != this.BlockSizeValue) {
131 throw new CryptographicException (
132 Locale.GetText ("IV length is different than block size"));
135 if ((value.Length << 3) > this.BlockSizeValue) {
136 throw new CryptographicException (
137 Locale.GetText ("IV length cannot be larger 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"));
161 this.KeySizeValue = length;
162 this.KeyValue = (byte[]) value.Clone ();
166 public virtual int KeySize {
167 get { return this.KeySizeValue; }
169 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
170 throw new CryptographicException (
171 Locale.GetText ("Key size not supported by algorithm"));
174 this.KeyValue = null;
175 this.KeySizeValue = value;
179 public virtual KeySizes[] LegalBlockSizes {
180 get { return this.LegalBlockSizesValue; }
183 public virtual KeySizes[] LegalKeySizes {
184 get { return this.LegalKeySizesValue; }
187 public virtual CipherMode Mode {
188 get { return this.ModeValue; }
190 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
191 throw new CryptographicException (
192 Locale.GetText ("Cipher mode not available"));
195 this.ModeValue = value;
199 public virtual PaddingMode Padding {
200 get { return this.PaddingValue; }
202 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
203 throw new CryptographicException (
204 Locale.GetText ("Padding mode not available"));
207 this.PaddingValue = value;
211 public virtual ICryptoTransform CreateDecryptor ()
213 return CreateDecryptor (Key, IV);
216 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
218 public virtual ICryptoTransform CreateEncryptor()
220 return CreateEncryptor (Key, IV);
223 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
225 public abstract void GenerateIV ();
227 public abstract void GenerateKey ();
229 public bool ValidKeySize (int bitLength)
231 return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
234 // LAMESPEC: Default is Rijndael - not TripleDES
235 public static SymmetricAlgorithm Create ()
237 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
240 public static SymmetricAlgorithm Create (string algName)
242 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);