bring Mono Security to monotouch
[mcs.git] / class / corlib / System.Security.Cryptography / SymmetricAlgorithm.cs
blobb6bb62d4562eb788899cbb15a5469122ef0d2468
1 //
2 // System.Security.Cryptography SymmetricAlgorithm Class implementation
3 //
4 // Authors:
5 // Thomas Neidhart (tome@sbox.tugraz.at)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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 {
37 #if NET_2_0
38 [ComVisible (true)]
39 #endif
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;
52 #else
53 protected int FeedbackSizeValue;
54 protected CipherMode ModeValue;
55 protected PaddingMode PaddingValue;
56 #endif
57 private bool m_disposed;
59 #if NET_2_0
60 protected SymmetricAlgorithm ()
61 #else
62 public SymmetricAlgorithm ()
63 #endif
65 ModeValue = CipherMode.CBC;
66 PaddingValue = PaddingMode.PKCS7;
67 m_disposed = false;
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
73 #else
74 ~SymmetricAlgorithm ()
76 Dispose (false);
78 #endif
79 void IDisposable.Dispose ()
81 Dispose (true);
82 GC.SuppressFinalize (this); // Finalization is now unnecessary
85 public void Clear()
87 Dispose (true);
90 protected virtual void Dispose (bool disposing)
92 if (!m_disposed) {
93 // always zeroize keys
94 if (KeyValue != null) {
95 // Zeroize the secret key and free
96 Array.Clear (KeyValue, 0, KeyValue.Length);
97 KeyValue = null;
99 // dispose unmanaged managed objects
100 if (disposing) {
101 // dispose managed objects
103 m_disposed = true;
107 public virtual int BlockSize {
108 get { return this.BlockSizeValue; }
109 set {
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;
117 IVValue = null;
122 public virtual int FeedbackSize {
123 get { return this.FeedbackSizeValue; }
124 set {
125 #if NET_2_0
126 if ((value <= 0) || (value > this.BlockSizeValue)) {
127 #else
128 if (value > this.BlockSizeValue) {
129 #endif
130 throw new CryptographicException (
131 Locale.GetText ("feedback size larger than block size"));
133 this.FeedbackSizeValue = value;
137 public virtual byte[] IV {
138 get {
139 if (this.IVValue == null)
140 GenerateIV();
142 return (byte[]) this.IVValue.Clone ();
144 set {
145 if (value == null)
146 throw new ArgumentNullException ("IV");
147 #if NET_2_0
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"));
153 #else
154 if ((value.Length << 3) > this.BlockSizeValue) {
155 throw new CryptographicException (
156 Locale.GetText ("IV length cannot be larger than block size"));
158 #endif
159 this.IVValue = (byte[]) value.Clone ();
163 public virtual byte[] Key {
164 get {
165 if (this.KeyValue == null)
166 GenerateKey();
168 return (byte[]) this.KeyValue.Clone ();
170 set {
171 if (value == null)
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; }
186 set {
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;
193 KeyValue = null;
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; }
207 set {
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; }
219 set {
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);