Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / TripleDESCryptoServiceProviderTest.cs
1 //
2 // TripleDESCryptoServiceProviderTest.cs - Unit tests for 
3 //      System.Security.Cryptography.TripleDESCryptoServiceProvider
4 //
5 // Author:
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Security.Cryptography;
32
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Security.Cryptography {
36
37         [TestFixture]
38         public class TripleDESCryptoServiceProviderTest {
39
40                 private TripleDESCryptoServiceProvider tdes;
41
42                 [SetUp]
43                 public void SetUp () 
44                 {
45                         tdes = new TripleDESCryptoServiceProvider ();
46                 }
47
48                 [Test]
49 #if NET_2_0
50                 [ExpectedException (typeof (CryptographicException))]
51 #else
52                 [ExpectedException (typeof (NullReferenceException))]
53 #endif
54                 public void CreateEncryptor_KeyNull ()
55                 {
56                         ICryptoTransform encryptor = tdes.CreateEncryptor (null, tdes.IV);
57                         byte[] data = new byte[encryptor.InputBlockSize];
58                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
59
60                         ICryptoTransform decryptor = tdes.CreateDecryptor (tdes.Key, tdes.IV);
61                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
62                         // null key != SymmetricAlgorithm.Key
63
64                         // in about 1 out of 256 runs the exception will not be thrown because the padding will be
65                         // the same as expected - however this still won't produce the right results (next check)
66                         Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
67                         // so this case is ok too, throw the expected exception to make the unit test succeed
68                         throw new CryptographicException ("1/256");
69                 }
70
71                 [Test]
72                 public void CreateEncryptor_IvNull ()
73                 {
74                         ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, null);
75                         byte[] data = new byte[encryptor.InputBlockSize];
76                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
77
78                         ICryptoTransform decryptor = tdes.CreateDecryptor (tdes.Key, tdes.IV);
79                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
80                         Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
81                         // null iv != SymmetricAlgorithm.IV
82                 }
83
84                 [Test]
85                 public void CreateEncryptor_KeyIv ()
86                 {
87                         byte[] originalKey = tdes.Key;
88                         byte[] originalIV = tdes.IV;
89
90                         byte[] key = (byte[]) tdes.Key.Clone ();
91                         Array.Reverse (key);
92                         byte[] iv = (byte[]) tdes.IV.Clone ();
93                         Array.Reverse (iv);
94
95                         Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateEncryptor");
96
97                         Assert.AreEqual (originalKey, tdes.Key, "Key");
98                         Assert.AreEqual (originalIV, tdes.IV, "IV");
99                         // SymmetricAlgorithm Key and IV not changed by CreateEncryptor
100                 }
101
102                 [Test]
103 #if NET_2_0
104                 [ExpectedException (typeof (CryptographicException))]
105 #else
106                 [ExpectedException (typeof (NullReferenceException))]
107 #endif
108                 public void CreateDecryptor_KeyNull ()
109                 {
110                         ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, tdes.IV);
111                         byte[] data = new byte[encryptor.InputBlockSize];
112                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
113
114                         ICryptoTransform decryptor = tdes.CreateDecryptor (null, tdes.IV);
115                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
116                         // null key != SymmetricAlgorithm.Key
117
118                         // in about 1 out of 256 runs the exception will not be thrown because the padding will be
119                         // the same as expected - however this still won't produce the right results (next check)
120                         Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
121                         // so this case is ok too, throw the expected exception to make the unit test succeed
122                         throw new CryptographicException ("1/256");
123                 }
124
125                 [Test]
126                 public void CreateDecryptor_IvNull ()
127                 {
128                         ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, tdes.IV);
129                         byte[] data = new byte[encryptor.InputBlockSize];
130                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
131
132                         ICryptoTransform decryptor = tdes.CreateDecryptor (tdes.Key, null);
133                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
134                         Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
135                         // null iv != SymmetricAlgorithm.IV
136                 }
137
138                 [Test]
139                 public void CreateDecryptor_KeyIv ()
140                 {
141                         byte[] originalKey = tdes.Key;
142                         byte[] originalIV = tdes.IV;
143
144                         byte[] key = (byte[]) tdes.Key.Clone ();
145                         Array.Reverse (key);
146                         byte[] iv = (byte[]) tdes.IV.Clone ();
147                         Array.Reverse (iv);
148
149                         Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateDecryptor");
150
151                         Assert.AreEqual (originalKey, tdes.Key, "Key");
152                         Assert.AreEqual (originalIV, tdes.IV, "IV");
153                         // SymmetricAlgorithm Key and IV not changed by CreateDecryptor
154                 }
155
156                 // Setting the IV is more restrictive than supplying an IV to
157                 // CreateEncryptor and CreateDecryptor. See bug #76483
158
159                 private ICryptoTransform CreateEncryptor_IV (int size)
160                 {
161                         byte[] iv = (size == -1) ? null : new byte[size];
162                         return tdes.CreateEncryptor (tdes.Key, iv);
163                 }
164
165                 [Test]
166                 public void CreateEncryptor_IV_Null ()
167                 {
168                         int size = (tdes.BlockSize >> 3) - 1;
169                         CreateEncryptor_IV (-1);
170                 }
171
172                 [Test]
173 #if NET_2_0
174                 [ExpectedException (typeof (CryptographicException))]
175 #endif
176                 public void CreateEncryptor_IV_Zero ()
177                 {
178                         int size = (tdes.BlockSize >> 3) - 1;
179                         CreateEncryptor_IV (0);
180                 }
181
182                 [Test]
183 #if NET_2_0
184                 [ExpectedException (typeof (CryptographicException))]
185 #endif
186                 public void CreateEncryptor_IV_TooSmall ()
187                 {
188                         int size = (tdes.BlockSize >> 3) - 1;
189                         CreateEncryptor_IV (size);
190                 }
191
192                 [Test]
193                 public void CreateEncryptor_IV_BlockSize ()
194                 {
195                         int size = (tdes.BlockSize >> 3);
196                         CreateEncryptor_IV (size);
197                 }
198
199                 [Test]
200                 public void CreateEncryptor_IV_TooBig ()
201                 {
202                         int size = tdes.BlockSize; // 8 times too big
203                         CreateEncryptor_IV (size);
204                 }
205
206                 private ICryptoTransform CreateDecryptor_IV (int size)
207                 {
208                         byte[] iv = (size == -1) ? null : new byte[size];
209                         return tdes.CreateDecryptor (tdes.Key, iv);
210                 }
211
212                 [Test]
213                 public void CreateDecryptor_IV_Null ()
214                 {
215                         int size = (tdes.BlockSize >> 3) - 1;
216                         CreateDecryptor_IV (-1);
217                 }
218
219                 [Test]
220 #if NET_2_0
221                 [ExpectedException (typeof (CryptographicException))]
222 #endif
223                 public void CreateDecryptor_IV_Zero ()
224                 {
225                         int size = (tdes.BlockSize >> 3) - 1;
226                         CreateDecryptor_IV (0);
227                 }
228
229                 [Test]
230 #if NET_2_0
231                 [ExpectedException (typeof (CryptographicException))]
232 #endif
233                 public void CreateDecryptor_IV_TooSmall ()
234                 {
235                         int size = (tdes.BlockSize >> 3) - 1;
236                         CreateDecryptor_IV (size);
237                 }
238
239                 [Test]
240                 public void CreateDecryptor_IV_BlockSize ()
241                 {
242                         int size = (tdes.BlockSize >> 3);
243                         CreateDecryptor_IV (size);
244                 }
245
246                 [Test]
247                 public void CreateDecryptor_IV_TooBig ()
248                 {
249                         int size = tdes.BlockSize; // 8 times too big
250                         CreateDecryptor_IV (size);
251                 }
252                 
253                 [Test]
254                 public void TwoKeysTripleDes ()
255                 {
256                         byte[] key = new byte [16]; // 128 bits
257                         Buffer.BlockCopy (tdes.Key, 0, key, 0, 16);
258                         
259                         ICryptoTransform encryptor = tdes.CreateEncryptor (key, tdes.IV);
260                         byte[] data = new byte[encryptor.InputBlockSize];
261                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
262
263                         ICryptoTransform decryptor = tdes.CreateDecryptor (key, tdes.IV);
264                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
265                         Assert.IsTrue (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
266                 }
267         }
268 }