2 // TripleDESCryptoServiceProviderTest.cs - Unit tests for
3 // System.Security.Cryptography.TripleDESCryptoServiceProvider
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System.Security.Cryptography;
33 using NUnit.Framework;
35 namespace MonoTests.System.Security.Cryptography {
38 public class TripleDESCryptoServiceProviderTest {
40 private TripleDESCryptoServiceProvider tdes;
45 tdes = new TripleDESCryptoServiceProvider ();
50 [ExpectedException (typeof (CryptographicException))]
52 [ExpectedException (typeof (NullReferenceException))]
54 public void CreateEncryptor_KeyNull ()
56 ICryptoTransform encryptor = tdes.CreateEncryptor (null, tdes.IV);
57 byte[] data = new byte[encryptor.InputBlockSize];
58 byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
60 ICryptoTransform decryptor = tdes.CreateDecryptor (tdes.Key, tdes.IV);
61 byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
62 // null key != SymmetricAlgorithm.Key
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");
72 public void CreateEncryptor_IvNull ()
74 ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, null);
75 byte[] data = new byte[encryptor.InputBlockSize];
76 byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
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
85 public void CreateEncryptor_KeyIv ()
87 byte[] originalKey = tdes.Key;
88 byte[] originalIV = tdes.IV;
90 byte[] key = (byte[]) tdes.Key.Clone ();
92 byte[] iv = (byte[]) tdes.IV.Clone ();
95 Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateEncryptor");
97 Assert.AreEqual (originalKey, tdes.Key, "Key");
98 Assert.AreEqual (originalIV, tdes.IV, "IV");
99 // SymmetricAlgorithm Key and IV not changed by CreateEncryptor
104 [ExpectedException (typeof (CryptographicException))]
106 [ExpectedException (typeof (NullReferenceException))]
108 public void CreateDecryptor_KeyNull ()
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);
114 ICryptoTransform decryptor = tdes.CreateDecryptor (null, tdes.IV);
115 byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
116 // null key != SymmetricAlgorithm.Key
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");
126 public void CreateDecryptor_IvNull ()
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);
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
139 public void CreateDecryptor_KeyIv ()
141 byte[] originalKey = tdes.Key;
142 byte[] originalIV = tdes.IV;
144 byte[] key = (byte[]) tdes.Key.Clone ();
146 byte[] iv = (byte[]) tdes.IV.Clone ();
149 Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateDecryptor");
151 Assert.AreEqual (originalKey, tdes.Key, "Key");
152 Assert.AreEqual (originalIV, tdes.IV, "IV");
153 // SymmetricAlgorithm Key and IV not changed by CreateDecryptor
156 // Setting the IV is more restrictive than supplying an IV to
157 // CreateEncryptor and CreateDecryptor. See bug #76483
159 private ICryptoTransform CreateEncryptor_IV (int size)
161 byte[] iv = (size == -1) ? null : new byte[size];
162 return tdes.CreateEncryptor (tdes.Key, iv);
166 public void CreateEncryptor_IV_Null ()
168 int size = (tdes.BlockSize >> 3) - 1;
169 CreateEncryptor_IV (-1);
174 [ExpectedException (typeof (CryptographicException))]
176 public void CreateEncryptor_IV_Zero ()
178 int size = (tdes.BlockSize >> 3) - 1;
179 CreateEncryptor_IV (0);
184 [ExpectedException (typeof (CryptographicException))]
186 public void CreateEncryptor_IV_TooSmall ()
188 int size = (tdes.BlockSize >> 3) - 1;
189 CreateEncryptor_IV (size);
193 public void CreateEncryptor_IV_BlockSize ()
195 int size = (tdes.BlockSize >> 3);
196 CreateEncryptor_IV (size);
200 public void CreateEncryptor_IV_TooBig ()
202 int size = tdes.BlockSize; // 8 times too big
203 CreateEncryptor_IV (size);
206 private ICryptoTransform CreateDecryptor_IV (int size)
208 byte[] iv = (size == -1) ? null : new byte[size];
209 return tdes.CreateDecryptor (tdes.Key, iv);
213 public void CreateDecryptor_IV_Null ()
215 int size = (tdes.BlockSize >> 3) - 1;
216 CreateDecryptor_IV (-1);
221 [ExpectedException (typeof (CryptographicException))]
223 public void CreateDecryptor_IV_Zero ()
225 int size = (tdes.BlockSize >> 3) - 1;
226 CreateDecryptor_IV (0);
231 [ExpectedException (typeof (CryptographicException))]
233 public void CreateDecryptor_IV_TooSmall ()
235 int size = (tdes.BlockSize >> 3) - 1;
236 CreateDecryptor_IV (size);
240 public void CreateDecryptor_IV_BlockSize ()
242 int size = (tdes.BlockSize >> 3);
243 CreateDecryptor_IV (size);
247 public void CreateDecryptor_IV_TooBig ()
249 int size = tdes.BlockSize; // 8 times too big
250 CreateDecryptor_IV (size);
254 public void TwoKeysTripleDes ()
256 byte[] key = new byte [16]; // 128 bits
257 Buffer.BlockCopy (tdes.Key, 0, key, 0, 16);
259 ICryptoTransform encryptor = tdes.CreateEncryptor (key, tdes.IV);
260 byte[] data = new byte[encryptor.InputBlockSize];
261 byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
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");