New test.
[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
65                 [Test]
66                 public void CreateEncryptor_IvNull ()
67                 {
68                         ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, null);
69                         byte[] data = new byte[encryptor.InputBlockSize];
70                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
71
72                         ICryptoTransform decryptor = tdes.CreateDecryptor (tdes.Key, tdes.IV);
73                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
74                         Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
75                         // null iv != SymmetricAlgorithm.IV
76                 }
77
78                 [Test]
79                 public void CreateEncryptor_KeyIv ()
80                 {
81                         byte[] originalKey = tdes.Key;
82                         byte[] originalIV = tdes.IV;
83
84                         byte[] key = (byte[]) tdes.Key.Clone ();
85                         Array.Reverse (key);
86                         byte[] iv = (byte[]) tdes.IV.Clone ();
87                         Array.Reverse (iv);
88
89                         Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateEncryptor");
90
91                         Assert.AreEqual (originalKey, tdes.Key, "Key");
92                         Assert.AreEqual (originalIV, tdes.IV, "IV");
93                         // SymmetricAlgorithm Key and IV not changed by CreateEncryptor
94                 }
95
96                 [Test]
97 #if NET_2_0
98                 [ExpectedException (typeof (CryptographicException))]
99 #else
100                 [ExpectedException (typeof (NullReferenceException))]
101 #endif
102                 public void CreateDecryptor_KeyNull ()
103                 {
104                         ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, tdes.IV);
105                         byte[] data = new byte[encryptor.InputBlockSize];
106                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
107
108                         ICryptoTransform decryptor = tdes.CreateDecryptor (null, tdes.IV);
109                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
110                         // null key != SymmetricAlgorithm.Key
111                 }
112
113                 [Test]
114                 public void CreateDecryptor_IvNull ()
115                 {
116                         ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, tdes.IV);
117                         byte[] data = new byte[encryptor.InputBlockSize];
118                         byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
119
120                         ICryptoTransform decryptor = tdes.CreateDecryptor (tdes.Key, null);
121                         byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
122                         Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
123                         // null iv != SymmetricAlgorithm.IV
124                 }
125
126                 [Test]
127                 public void CreateDecryptor_KeyIv ()
128                 {
129                         byte[] originalKey = tdes.Key;
130                         byte[] originalIV = tdes.IV;
131
132                         byte[] key = (byte[]) tdes.Key.Clone ();
133                         Array.Reverse (key);
134                         byte[] iv = (byte[]) tdes.IV.Clone ();
135                         Array.Reverse (iv);
136
137                         Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateDecryptor");
138
139                         Assert.AreEqual (originalKey, tdes.Key, "Key");
140                         Assert.AreEqual (originalIV, tdes.IV, "IV");
141                         // SymmetricAlgorithm Key and IV not changed by CreateDecryptor
142                 }
143
144                 // Setting the IV is more restrictive than supplying an IV to
145                 // CreateEncryptor and CreateDecryptor. See bug #76483
146
147                 private ICryptoTransform CreateEncryptor_IV (int size)
148                 {
149                         byte[] iv = (size == -1) ? null : new byte[size];
150                         return tdes.CreateEncryptor (tdes.Key, iv);
151                 }
152
153                 [Test]
154                 public void CreateEncryptor_IV_Null ()
155                 {
156                         int size = (tdes.BlockSize >> 3) - 1;
157                         CreateEncryptor_IV (-1);
158                 }
159
160                 [Test]
161 #if NET_2_0
162                 [ExpectedException (typeof (CryptographicException))]
163 #endif
164                 public void CreateEncryptor_IV_Zero ()
165                 {
166                         int size = (tdes.BlockSize >> 3) - 1;
167                         CreateEncryptor_IV (0);
168                 }
169
170                 [Test]
171 #if NET_2_0
172                 [ExpectedException (typeof (CryptographicException))]
173 #endif
174                 public void CreateEncryptor_IV_TooSmall ()
175                 {
176                         int size = (tdes.BlockSize >> 3) - 1;
177                         CreateEncryptor_IV (size);
178                 }
179
180                 [Test]
181                 public void CreateEncryptor_IV_BlockSize ()
182                 {
183                         int size = (tdes.BlockSize >> 3);
184                         CreateEncryptor_IV (size);
185                 }
186
187                 [Test]
188                 public void CreateEncryptor_IV_TooBig ()
189                 {
190                         int size = tdes.BlockSize; // 8 times too big
191                         CreateEncryptor_IV (size);
192                 }
193
194                 private ICryptoTransform CreateDecryptor_IV (int size)
195                 {
196                         byte[] iv = (size == -1) ? null : new byte[size];
197                         return tdes.CreateDecryptor (tdes.Key, iv);
198                 }
199
200                 [Test]
201                 public void CreateDecryptor_IV_Null ()
202                 {
203                         int size = (tdes.BlockSize >> 3) - 1;
204                         CreateDecryptor_IV (-1);
205                 }
206
207                 [Test]
208 #if NET_2_0
209                 [ExpectedException (typeof (CryptographicException))]
210 #endif
211                 public void CreateDecryptor_IV_Zero ()
212                 {
213                         int size = (tdes.BlockSize >> 3) - 1;
214                         CreateDecryptor_IV (0);
215                 }
216
217                 [Test]
218 #if NET_2_0
219                 [ExpectedException (typeof (CryptographicException))]
220 #endif
221                 public void CreateDecryptor_IV_TooSmall ()
222                 {
223                         int size = (tdes.BlockSize >> 3) - 1;
224                         CreateDecryptor_IV (size);
225                 }
226
227                 [Test]
228                 public void CreateDecryptor_IV_BlockSize ()
229                 {
230                         int size = (tdes.BlockSize >> 3);
231                         CreateDecryptor_IV (size);
232                 }
233
234                 [Test]
235                 public void CreateDecryptor_IV_TooBig ()
236                 {
237                         int size = tdes.BlockSize; // 8 times too big
238                         CreateDecryptor_IV (size);
239                 }
240         }
241 }