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
;
65 // No Finalizer or IDisposable.Dispose in Silverlight 2.0
66 // Documentation makes it "clear" that Clear MUST BE CALLED to zero out sensitive information
68 ~
SymmetricAlgorithm ()
73 void IDisposable
.Dispose ()
76 GC
.SuppressFinalize (this); // Finalization is now unnecessary
84 protected virtual void Dispose (bool disposing
)
87 // always zeroize keys
88 if (KeyValue
!= null) {
89 // Zeroize the secret key and free
90 Array
.Clear (KeyValue
, 0, KeyValue
.Length
);
93 // dispose unmanaged managed objects
95 // dispose managed objects
101 public virtual int BlockSize
{
102 get { return this.BlockSizeValue; }
104 if (!KeySizes
.IsLegalKeySize (this.LegalBlockSizesValue
, value)) {
105 throw new CryptographicException (
106 Locale
.GetText ("block size not supported by algorithm"));
108 // re-setting the same BlockSize *doesn't* regenerate the IV
109 if (BlockSizeValue
!= value) {
110 BlockSizeValue
= value;
116 public virtual int FeedbackSize
{
117 get { return this.FeedbackSizeValue; }
119 if ((value <= 0) || (value > this.BlockSizeValue
)) {
120 throw new CryptographicException (
121 Locale
.GetText ("feedback size larger than block size"));
123 this.FeedbackSizeValue
= value;
127 public virtual byte[] IV
{
129 if (this.IVValue
== null)
132 return (byte[]) this.IVValue
.Clone ();
136 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"));
142 this.IVValue
= (byte[]) value.Clone ();
146 public virtual byte[] Key
{
148 if (this.KeyValue
== null)
151 return (byte[]) this.KeyValue
.Clone ();
155 throw new ArgumentNullException ("Key");
157 int length
= (value.Length
<< 3);
158 if (!KeySizes
.IsLegalKeySize (this.LegalKeySizesValue
, length
)) {
159 throw new CryptographicException (
160 Locale
.GetText ("Key size not supported by algorithm"));
162 this.KeySizeValue
= length
;
163 this.KeyValue
= (byte[]) value.Clone ();
167 public virtual int KeySize
{
168 get { return this.KeySizeValue; }
170 if (!KeySizes
.IsLegalKeySize (this.LegalKeySizesValue
, value)) {
171 throw new CryptographicException (
172 Locale
.GetText ("Key size not supported by algorithm"));
174 // re-setting the same KeySize *does* regenerate the key
175 KeySizeValue
= value;
180 public virtual KeySizes
[] LegalBlockSizes
{
181 get { return this.LegalBlockSizesValue; }
184 public virtual KeySizes
[] LegalKeySizes
{
185 get { return this.LegalKeySizesValue; }
188 public virtual CipherMode Mode
{
189 get { return this.ModeValue; }
191 if (!Enum
.IsDefined (ModeValue
.GetType (), value)) {
192 throw new CryptographicException (
193 Locale
.GetText ("Cipher mode not available"));
196 this.ModeValue
= value;
200 public virtual PaddingMode Padding
{
201 get { return this.PaddingValue; }
203 if (!Enum
.IsDefined (PaddingValue
.GetType (), value)) {
204 throw new CryptographicException (
205 Locale
.GetText ("Padding mode not available"));
208 this.PaddingValue
= value;
212 public virtual ICryptoTransform
CreateDecryptor ()
214 return CreateDecryptor (Key
, IV
);
217 public abstract ICryptoTransform
CreateDecryptor (byte[] rgbKey
, byte[] rgbIV
);
219 public virtual ICryptoTransform
CreateEncryptor()
221 return CreateEncryptor (Key
, IV
);
224 public abstract ICryptoTransform
CreateEncryptor (byte[] rgbKey
, byte[] rgbIV
);
226 public abstract void GenerateIV ();
228 public abstract void GenerateKey ();
230 public bool ValidKeySize (int bitLength
)
232 return KeySizes
.IsLegalKeySize (LegalKeySizesValue
, bitLength
);
235 // LAMESPEC: Default is Rijndael - not TripleDES
236 public static SymmetricAlgorithm
Create ()
238 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
241 public static SymmetricAlgorithm
Create (string algName
)
243 return (SymmetricAlgorithm
) CryptoConfig
.CreateFromName (algName
);