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 // (C) 2004 Novell (http://www.novell.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System
.Globalization
;
37 using Mono
.Security
.Cryptography
;
39 namespace System
.Security
.Cryptography
{
41 public abstract class SymmetricAlgorithm
: IDisposable
{
42 protected int BlockSizeValue
;
43 protected int FeedbackSizeValue
;
44 protected byte[] IVValue
;
45 protected int KeySizeValue
;
46 protected byte[] KeyValue
;
47 protected KeySizes
[] LegalBlockSizesValue
;
48 protected KeySizes
[] LegalKeySizesValue
;
49 protected CipherMode ModeValue
;
50 protected PaddingMode PaddingValue
;
51 private bool m_disposed
;
53 public SymmetricAlgorithm ()
55 ModeValue
= CipherMode
.CBC
;
56 PaddingValue
= PaddingMode
.PKCS7
;
60 ~
SymmetricAlgorithm ()
70 void IDisposable
.Dispose ()
73 GC
.SuppressFinalize (this); // Finalization is now unnecessary
76 protected virtual void Dispose (bool disposing
)
79 // always zeroize keys
80 if (KeyValue
!= null) {
81 // Zeroize the secret key and free
82 Array
.Clear (KeyValue
, 0, KeyValue
.Length
);
85 // dispose unmanaged managed objects
87 // dispose managed objects
93 public virtual int BlockSize
{
94 get { return this.BlockSizeValue; }
96 if (!KeySizes
.IsLegalKeySize (this.LegalBlockSizesValue
, value)) {
97 throw new CryptographicException (
98 Locale
.GetText ("block size not supported by algorithm"));
100 this.BlockSizeValue
= value;
104 public virtual int FeedbackSize
{
105 get { return this.FeedbackSizeValue; }
107 if (value > this.BlockSizeValue
) {
108 throw new CryptographicException (
109 Locale
.GetText ("feedback size larger than block size"));
111 this.FeedbackSizeValue
= value;
115 public virtual byte[] IV
{
117 if (this.IVValue
== null)
120 return (byte[]) this.IVValue
.Clone ();
124 throw new ArgumentNullException ("IV");
126 if ((value.Length
<< 3) > this.BlockSizeValue
) {
127 throw new CryptographicException (
128 Locale
.GetText ("IV length cannot be larger than block size"));
131 this.IVValue
= (byte[]) value.Clone ();
135 public virtual byte[] Key
{
137 if (this.KeyValue
== null)
140 return (byte[]) this.KeyValue
.Clone ();
144 throw new ArgumentNullException ("Key");
146 int length
= (value.Length
<< 3);
147 if (!KeySizes
.IsLegalKeySize (this.LegalKeySizesValue
, length
)) {
148 throw new CryptographicException (
149 Locale
.GetText ("Key size not supported by algorithm"));
152 this.KeySizeValue
= length
;
153 this.KeyValue
= (byte[]) value.Clone ();
157 public virtual int KeySize
{
158 get { return this.KeySizeValue; }
160 if (!KeySizes
.IsLegalKeySize (this.LegalKeySizesValue
, value)) {
161 throw new CryptographicException (
162 Locale
.GetText ("Key size not supported by algorithm"));
165 this.KeyValue
= null;
166 this.KeySizeValue
= value;
170 public virtual KeySizes
[] LegalBlockSizes
{
171 get { return this.LegalBlockSizesValue; }
174 public virtual KeySizes
[] LegalKeySizes
{
175 get { return this.LegalKeySizesValue; }
178 public virtual CipherMode Mode
{
179 get { return this.ModeValue; }
181 if (!Enum
.IsDefined (ModeValue
.GetType (), value)) {
182 throw new CryptographicException (
183 Locale
.GetText ("Cipher mode not available"));
186 this.ModeValue
= value;
190 public virtual PaddingMode Padding
{
191 get { return this.PaddingValue; }
193 if (!Enum
.IsDefined (PaddingValue
.GetType (), value)) {
194 throw new CryptographicException (
195 Locale
.GetText ("Padding mode not available"));
198 this.PaddingValue
= value;
202 public virtual ICryptoTransform
CreateDecryptor ()
204 return CreateDecryptor (Key
, IV
);
207 public abstract ICryptoTransform
CreateDecryptor (byte[] rgbKey
, byte[] rgbIV
);
209 public virtual ICryptoTransform
CreateEncryptor()
211 return CreateEncryptor (Key
, IV
);
214 public abstract ICryptoTransform
CreateEncryptor (byte[] rgbKey
, byte[] rgbIV
);
216 public abstract void GenerateIV ();
218 public abstract void GenerateKey ();
220 public bool ValidKeySize (int bitLength
)
222 return KeySizes
.IsLegalKeySize (LegalKeySizesValue
, bitLength
);
225 // LAMESPEC: Default is Rijndael - not TripleDES
226 public static SymmetricAlgorithm
Create ()
228 return Create ("System.Security.Cryptography.SymmetricAlgorithm");
231 public static SymmetricAlgorithm
Create (string algName
)
233 return (SymmetricAlgorithm
) CryptoConfig
.CreateFromName (algName
);