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 {
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;
53 protected SymmetricAlgorithm ()
55 public SymmetricAlgorithm ()
58 ModeValue = CipherMode.CBC;
59 PaddingValue = PaddingMode.PKCS7;
63 ~SymmetricAlgorithm ()
73 void IDisposable.Dispose ()
76 GC.SuppressFinalize (this); // Finalization is now unnecessary
79 protected virtual void Dispose (bool disposing)
82 // always zeroize keys
83 if (KeyValue != null) {
84 // Zeroize the secret key and free
85 Array.Clear (KeyValue, 0, KeyValue.Length);
88 // dispose unmanaged managed objects
90 // dispose managed objects
96 public virtual int BlockSize {
97 get { return this.BlockSizeValue; }
99 if (!KeySizes.IsLegalKeySize (this.LegalBlockSizesValue, value)) {
100 throw new CryptographicException (
101 Locale.GetText ("block size not supported by algorithm"));
103 // re-setting the same BlockSize *doesn't* regenerate the IV
104 if (BlockSizeValue != value) {
105 BlockSizeValue = value;
111 public virtual int FeedbackSize {
112 get { return this.FeedbackSizeValue; }
115 if ((value <= 0) || (value > this.BlockSizeValue)) {
117 if (value > this.BlockSizeValue) {
119 throw new CryptographicException (
120 Locale.GetText ("feedback size larger than block size"));
122 this.FeedbackSizeValue = value;
126 public virtual byte[] IV {
128 if (this.IVValue == null)
131 return (byte[]) this.IVValue.Clone ();
135 throw new ArgumentNullException ("IV");
137 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
138 if ((value.Length << 3) != this.BlockSizeValue) {
139 throw new CryptographicException (
140 Locale.GetText ("IV length is different than block size"));
143 if ((value.Length << 3) > this.BlockSizeValue) {
144 throw new CryptographicException (
145 Locale.GetText ("IV length cannot be larger than block size"));
148 this.IVValue = (byte[]) value.Clone ();
152 public virtual byte[] Key {
154 if (this.KeyValue == null)
157 return (byte[]) this.KeyValue.Clone ();
161 throw new ArgumentNullException ("Key");
163 int length = (value.Length << 3);
164 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, length)) {
165 throw new CryptographicException (
166 Locale.GetText ("Key size not supported by algorithm"));
168 this.KeySizeValue = length;
169 this.KeyValue = (byte[]) value.Clone ();
173 public virtual int KeySize {
174 get { return this.KeySizeValue; }
176 if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value)) {
177 throw new CryptographicException (
178 Locale.GetText ("Key size not supported by algorithm"));
180 // re-setting the same KeySize *does* regenerate the key
181 KeySizeValue = value;
186 public virtual KeySizes[] LegalBlockSizes {
187 get { return this.LegalBlockSizesValue; }
190 public virtual KeySizes[] LegalKeySizes {
191 get { return this.LegalKeySizesValue; }
194 public virtual CipherMode Mode {
195 get { return this.ModeValue; }
197 if (!Enum.IsDefined (ModeValue.GetType (), value)) {
198 throw new CryptographicException (
199 Locale.GetText ("Cipher mode not available"));
202 this.ModeValue = value;
206 public virtual PaddingMode Padding {
207 get { return this.PaddingValue; }
209 if (!Enum.IsDefined (PaddingValue.GetType (), value)) {
210 throw new CryptographicException (
211 Locale.GetText ("Padding mode not available"));
214 this.PaddingValue = value;
218 public virtual ICryptoTransform CreateDecryptor ()
220 return CreateDecryptor (Key, IV);
223 public abstract ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV);
225 public virtual ICryptoTransform CreateEncryptor()
227 return CreateEncryptor (Key, IV);
230 public abstract ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
232 public abstract void GenerateIV ();
234 public abstract void GenerateKey ();
236 public bool ValidKeySize (int bitLength)
238 return KeySizes.IsLegalKeySize (LegalKeySizesValue, bitLength);
241 // LAMESPEC: Default is Rijndael - not TripleDES
242 public static SymmetricAlgorithm Create ()
244 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
247 public static SymmetricAlgorithm Create (string algName)
249 return (SymmetricAlgorithm) CryptoConfig.CreateFromName (algName);