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
66 public void CreateEncryptor_IvNull ()
68 ICryptoTransform encryptor = tdes.CreateEncryptor (tdes.Key, null);
69 byte[] data = new byte[encryptor.InputBlockSize];
70 byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
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
79 public void CreateEncryptor_KeyIv ()
81 byte[] originalKey = tdes.Key;
82 byte[] originalIV = tdes.IV;
84 byte[] key = (byte[]) tdes.Key.Clone ();
86 byte[] iv = (byte[]) tdes.IV.Clone ();
89 Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateEncryptor");
91 Assert.AreEqual (originalKey, tdes.Key, "Key");
92 Assert.AreEqual (originalIV, tdes.IV, "IV");
93 // SymmetricAlgorithm Key and IV not changed by CreateEncryptor
98 [ExpectedException (typeof (CryptographicException))]
100 [ExpectedException (typeof (NullReferenceException))]
102 public void CreateDecryptor_KeyNull ()
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);
108 ICryptoTransform decryptor = tdes.CreateDecryptor (null, tdes.IV);
109 byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
110 // null key != SymmetricAlgorithm.Key
114 public void CreateDecryptor_IvNull ()
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);
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
127 public void CreateDecryptor_KeyIv ()
129 byte[] originalKey = tdes.Key;
130 byte[] originalIV = tdes.IV;
132 byte[] key = (byte[]) tdes.Key.Clone ();
134 byte[] iv = (byte[]) tdes.IV.Clone ();
137 Assert.IsNotNull (tdes.CreateEncryptor (key, iv), "CreateDecryptor");
139 Assert.AreEqual (originalKey, tdes.Key, "Key");
140 Assert.AreEqual (originalIV, tdes.IV, "IV");
141 // SymmetricAlgorithm Key and IV not changed by CreateDecryptor
144 // Setting the IV is more restrictive than supplying an IV to
145 // CreateEncryptor and CreateDecryptor. See bug #76483
147 private ICryptoTransform CreateEncryptor_IV (int size)
149 byte[] iv = (size == -1) ? null : new byte[size];
150 return tdes.CreateEncryptor (tdes.Key, iv);
154 public void CreateEncryptor_IV_Null ()
156 int size = (tdes.BlockSize >> 3) - 1;
157 CreateEncryptor_IV (-1);
162 [ExpectedException (typeof (CryptographicException))]
164 public void CreateEncryptor_IV_Zero ()
166 int size = (tdes.BlockSize >> 3) - 1;
167 CreateEncryptor_IV (0);
172 [ExpectedException (typeof (CryptographicException))]
174 public void CreateEncryptor_IV_TooSmall ()
176 int size = (tdes.BlockSize >> 3) - 1;
177 CreateEncryptor_IV (size);
181 public void CreateEncryptor_IV_BlockSize ()
183 int size = (tdes.BlockSize >> 3);
184 CreateEncryptor_IV (size);
188 public void CreateEncryptor_IV_TooBig ()
190 int size = tdes.BlockSize; // 8 times too big
191 CreateEncryptor_IV (size);
194 private ICryptoTransform CreateDecryptor_IV (int size)
196 byte[] iv = (size == -1) ? null : new byte[size];
197 return tdes.CreateDecryptor (tdes.Key, iv);
201 public void CreateDecryptor_IV_Null ()
203 int size = (tdes.BlockSize >> 3) - 1;
204 CreateDecryptor_IV (-1);
209 [ExpectedException (typeof (CryptographicException))]
211 public void CreateDecryptor_IV_Zero ()
213 int size = (tdes.BlockSize >> 3) - 1;
214 CreateDecryptor_IV (0);
219 [ExpectedException (typeof (CryptographicException))]
221 public void CreateDecryptor_IV_TooSmall ()
223 int size = (tdes.BlockSize >> 3) - 1;
224 CreateDecryptor_IV (size);
228 public void CreateDecryptor_IV_BlockSize ()
230 int size = (tdes.BlockSize >> 3);
231 CreateDecryptor_IV (size);
235 public void CreateDecryptor_IV_TooBig ()
237 int size = tdes.BlockSize; // 8 times too big
238 CreateDecryptor_IV (size);