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; }
103 if ((value <= 0) || (value > this.BlockSizeValue)) {
105 if (value > this.BlockSizeValue) {
107 throw new CryptographicException (
108 Locale.GetText ("feedback size larger than block size"));
110 this.FeedbackSizeValue = value;
114 public virtual byte[] IV {
116 if (this.IVValue == null)
119 return (byte[]) this.IVValue.Clone ();
123 throw new ArgumentNullException ("IV");
125 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
126 if ((value.Length << 3) != this.BlockSizeValue) {
127 throw new CryptographicException (
128 Locale.GetText ("IV length is different than block size"));
131 if ((value.Length << 3) > this.BlockSizeValue) {
132 throw new CryptographicException (
133 Locale.GetText ("IV length cannot be larger than block size"));
136 this.IVValue = (byte[]) value.Clone ();
140 public virtual byte[] Key {
142 if (this.KeyValue == null)
145 return (byte[]) this.KeyValue.Clone ();
149 throw new ArgumentNullException ("Key");
151 int length = (value.Length << 3);
152 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
153 throw new CryptographicException (
154 Locale.GetText ("Key size not supported by algorithm"));
157 this.KeySizeValue = length;
158 this.KeyValue = (byte[]) value.Clone ();
162 public virtual int KeySize {
163 get { return this.KeySizeValue; }
165 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
166 throw new CryptographicException (
167 Locale.GetText ("Key size not supported by algorithm"));
170 this.KeyValue = null;
171 this.KeySizeValue = value;
175 public virtual KeySizes[] LegalBlockSizes {
176 get { return this.LegalBlockSizesValue; }
179 public virtual KeySizes[] LegalKeySizes {
180 get { return this.LegalKeySizesValue; }
183 public virtual CipherMode Mode {
184 get { return this.ModeValue; }
186 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
187 throw new CryptographicException (
188 Locale.GetText ("Cipher mode not available"));
191 this.ModeValue = value;
195 public virtual PaddingMode Padding {
196 get { return this.PaddingValue; }
198 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
199 throw new CryptographicException (
200 Locale.GetText ("Padding mode not available"));
203 this.PaddingValue = value;
207 public virtual ICryptoTransform CreateDecryptor ()
209 return CreateDecryptor (Key, IV);
212 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
214 public virtual ICryptoTransform CreateEncryptor()
216 return CreateEncryptor (Key, IV);
219 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
221 public abstract void GenerateIV ();
223 public abstract void GenerateKey ();
225 public bool ValidKeySize (int bitLength)
227 return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
230 // LAMESPEC: Default is Rijndael - not TripleDES
231 public static SymmetricAlgorithm Create ()
233 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
236 public static SymmetricAlgorithm Create (string algName)
238 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);