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 this.FeedbackSizeValue = value;
121 public virtual byte[] IV {
123 if (this.IVValue == null)
126 return (byte[]) this.IVValue.Clone ();
130 throw new ArgumentNullException ("IV");
131 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
132 if ((value.Length << 3) != this.BlockSizeValue) {
133 throw new CryptographicException (
134 Locale.GetText ("IV length is different 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"));
156 this.KeySizeValue = length;
157 this.KeyValue = (byte[]) value.Clone ();
161 public virtual int KeySize {
162 get { return this.KeySizeValue; }
164 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
165 throw new CryptographicException (
166 Locale.GetText ("Key size not supported by algorithm"));
168 // re-setting the same KeySize *does* regenerate the key
169 KeySizeValue = value;
174 public virtual KeySizes[] LegalBlockSizes {
175 get { return this.LegalBlockSizesValue; }
178 public virtual KeySizes[] LegalKeySizes {
179 get { return this.LegalKeySizesValue; }
182 public virtual CipherMode Mode {
183 get { return this.ModeValue; }
185 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
186 throw new CryptographicException (
187 Locale.GetText ("Cipher mode not available"));
190 this.ModeValue = value;
194 public virtual PaddingMode Padding {
195 get { return this.PaddingValue; }
197 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
198 throw new CryptographicException (
199 Locale.GetText ("Padding mode not available"));
202 this.PaddingValue = value;
206 public virtual ICryptoTransform CreateDecryptor ()
208 return CreateDecryptor (Key, IV);
211 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
213 public virtual ICryptoTransform CreateEncryptor()
215 return CreateEncryptor (Key, IV);
218 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
220 public abstract void GenerateIV ();
222 public abstract void GenerateKey ();
224 public bool ValidKeySize (int bitLength)
226 return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
229 // LAMESPEC: Default is Rijndael - not TripleDES
230 public static SymmetricAlgorithm Create ()
233 return new System.Security.Cryptography.RijndaelManaged ();
235 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
239 public static SymmetricAlgorithm Create (string algName)
241 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);