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 byte[] IVValue
;
43 protected int KeySizeValue
;
44 protected byte[] KeyValue
;
45 protected KeySizes
[] LegalBlockSizesValue
;
46 protected KeySizes
[] LegalKeySizesValue
;
47 #if NET_2_1 && !MONOTOUCH
48 // Silverlight 2.0 only supports CBC
49 internal int FeedbackSizeValue
;
50 internal CipherMode ModeValue
;
51 internal PaddingMode PaddingValue
;
53 protected int FeedbackSizeValue
;
54 protected CipherMode ModeValue
;
55 protected PaddingMode PaddingValue
;
57 private bool m_disposed
;
60 protected SymmetricAlgorithm ()
62 public SymmetricAlgorithm ()
65 ModeValue
= CipherMode
.CBC
;
66 PaddingValue
= PaddingMode
.PKCS7
;
70 #if NET_2_1 && !MONOTOUCH
71 // No Finalizer or IDisposable.Dispose in Silverlight 2.0
72 // Documentation makes it "clear" that Clear MUST BE CALLED to zero out sensitive information
74 ~
SymmetricAlgorithm ()
79 void IDisposable
.Dispose ()
82 GC
.SuppressFinalize (this); // Finalization is now unnecessary
90 protected virtual void Dispose (bool disposing
)
93 // always zeroize keys
94 if (KeyValue
!= null) {
95 // Zeroize the secret key and free
96 Array
.Clear (KeyValue
, 0, KeyValue
.Length
);
99 // dispose unmanaged managed objects
101 // dispose managed objects
107 public virtual int BlockSize
{
108 get { return this.BlockSizeValue; }
110 if (!KeySizes
.IsLegalKeySize (this.LegalBlockSizesValue
, value)) {
111 throw new CryptographicException (
112 Locale
.GetText ("block size not supported by algorithm"));
114 // re-setting the same BlockSize *doesn't* regenerate the IV
115 if (BlockSizeValue
!= value) {
116 BlockSizeValue
= value;
122 public virtual int FeedbackSize
{
123 get { return this.FeedbackSizeValue; }
126 if ((value <= 0) || (value > this.BlockSizeValue
)) {
128 if (value > this.BlockSizeValue
) {
130 throw new CryptographicException (
131 Locale
.GetText ("feedback size larger than block size"));
133 this.FeedbackSizeValue
= value;
137 public virtual byte[] IV
{
139 if (this.IVValue
== null)
142 return (byte[]) this.IVValue
.Clone ();
146 throw new ArgumentNullException ("IV");
148 // 2.0 is stricter for IV length - which is bad for IV-less stream ciphers like RC4
149 if ((value.Length
<< 3) != this.BlockSizeValue
) {
150 throw new CryptographicException (
151 Locale
.GetText ("IV length is different than block size"));
154 if ((value.Length
<< 3) > this.BlockSizeValue
) {
155 throw new CryptographicException (
156 Locale
.GetText ("IV length cannot be larger than block size"));
159 this.IVValue
= (byte[]) value.Clone ();
163 public virtual byte[] Key
{
165 if (this.KeyValue
== null)
168 return (byte[]) this.KeyValue
.Clone ();
172 throw new ArgumentNullException ("Key");
174 int length
= (value.Length
<< 3);
175 if (!KeySizes
.IsLegalKeySize (this.LegalKeySizesValue
, length
)) {
176 throw new CryptographicException (
177 Locale
.GetText ("Key size not supported by algorithm"));
179 this.KeySizeValue
= length
;
180 this.KeyValue
= (byte[]) value.Clone ();
184 public virtual int KeySize
{
185 get { return this.KeySizeValue; }
187 if (!KeySizes
.IsLegalKeySize (this.LegalKeySizesValue
, value)) {
188 throw new CryptographicException (
189 Locale
.GetText ("Key size not supported by algorithm"));
191 // re-setting the same KeySize *does* regenerate the key
192 KeySizeValue
= value;
197 public virtual KeySizes
[] LegalBlockSizes
{
198 get { return this.LegalBlockSizesValue; }
201 public virtual KeySizes
[] LegalKeySizes
{
202 get { return this.LegalKeySizesValue; }
205 public virtual CipherMode Mode
{
206 get { return this.ModeValue; }
208 if (!Enum
.IsDefined (ModeValue
.GetType (), value)) {
209 throw new CryptographicException (
210 Locale
.GetText ("Cipher mode not available"));
213 this.ModeValue
= value;
217 public virtual PaddingMode Padding
{
218 get { return this.PaddingValue; }
220 if (!Enum
.IsDefined (PaddingValue
.GetType (), value)) {
221 throw new CryptographicException (
222 Locale
.GetText ("Padding mode not available"));
225 this.PaddingValue
= value;
229 public virtual ICryptoTransform
CreateDecryptor ()
231 return CreateDecryptor (Key
, IV
);
234 public abstract ICryptoTransform
CreateDecryptor (byte[] rgbKey
, byte[] rgbIV
);
236 public virtual ICryptoTransform
CreateEncryptor()
238 return CreateEncryptor (Key
, IV
);
241 public abstract ICryptoTransform
CreateEncryptor (byte[] rgbKey
, byte[] rgbIV
);
243 public abstract void GenerateIV ();
245 public abstract void GenerateKey ();
247 public bool ValidKeySize (int bitLength
)
249 return KeySizes
.IsLegalKeySize (LegalKeySizesValue
, bitLength
);
252 // LAMESPEC: Default is Rijndael - not TripleDES
253 public static SymmetricAlgorithm
Create ()
255 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
258 public static SymmetricAlgorithm
Create (string algName
)
260 return (SymmetricAlgorithm
) CryptoConfig
.CreateFromName (algName
);