1 //
2 // RC2CryptoServiceProviderTest.cs - Unit tests for
3 //	System.Security.Cryptography.RC2CryptoServiceProvider
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 RC2CryptoServiceProviderTest {
39 
40 		private RC2CryptoServiceProvider rc2;
41 
42 		[SetUp]
SetUp()43 		public void SetUp ()
44 		{
45 			rc2 = new RC2CryptoServiceProvider ();
46 		}
47 
48 		[Test]
49 		[ExpectedException (typeof (CryptographicException))]
CreateEncryptor_KeyNull()50 		public void CreateEncryptor_KeyNull ()
51 		{
52 			ICryptoTransform encryptor = rc2.CreateEncryptor (null, rc2.IV);
53 			byte[] data = new byte[encryptor.InputBlockSize];
54 			byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
55 
56 			ICryptoTransform decryptor = rc2.CreateDecryptor (rc2.Key, rc2.IV);
57 			byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
58 			// null key != SymmetricAlgorithm.Key
59 
60 			// in about 1 out of 256 runs the exception will not be thrown because the padding will be
61 			// the same as expected - however this still won't produce the right results (next check)
62 			Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
63 			// so this case is ok too, throw the expected exception to make the unit test succeed
64 			throw new CryptographicException ("1/256");
65 		}
66 
67 		[Test]
CreateEncryptor_IvNull()68 		public void CreateEncryptor_IvNull ()
69 		{
70 			ICryptoTransform encryptor = rc2.CreateEncryptor (rc2.Key, null);
71 			byte[] data = new byte[encryptor.InputBlockSize];
72 			byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
73 
74 			ICryptoTransform decryptor = rc2.CreateDecryptor (rc2.Key, rc2.IV);
75 			byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
76 			Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
77 			// null iv != SymmetricAlgorithm.IV
78 		}
79 
80 		[Test]
CreateEncryptor_KeyIv()81 		public void CreateEncryptor_KeyIv ()
82 		{
83 			byte[] originalKey = rc2.Key;
84 			byte[] originalIV = rc2.IV;
85 
86 			byte[] key = (byte[])rc2.Key.Clone ();
87 			Array.Reverse (key);
88 			byte[] iv = (byte[])rc2.IV.Clone ();
89 			Array.Reverse (iv);
90 
91 			Assert.IsNotNull (rc2.CreateEncryptor (key, iv), "CreateEncryptor");
92 
93 			Assert.AreEqual (originalKey, rc2.Key, "Key");
94 			Assert.AreEqual (originalIV, rc2.IV, "IV");
95 			// SymmetricAlgorithm Key and IV not changed by CreateEncryptor
96 		}
97 
98 		[Test]
99 		[ExpectedException (typeof (CryptographicException))]
CreateDecryptor_KeyNull()100 		public void CreateDecryptor_KeyNull ()
101 		{
102 			ICryptoTransform encryptor = rc2.CreateEncryptor (rc2.Key, rc2.IV);
103 			byte[] data = new byte[encryptor.InputBlockSize];
104 			byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
105 
106 			ICryptoTransform decryptor = rc2.CreateDecryptor (null, rc2.IV);
107 			byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
108 			// null key != SymmetricAlgorithm.Key
109 
110 			// in about 1 out of 256 runs the exception will not be thrown because the padding will be
111 			// the same as expected - however this still won't produce the right results (next check)
112 			Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
113 			// so this case is ok too, throw the expected exception to make the unit test succeed
114 			throw new CryptographicException ("1/256");
115 		}
116 
117 		[Test]
CreateDecryptor_IvNull()118 		public void CreateDecryptor_IvNull ()
119 		{
120 			ICryptoTransform encryptor = rc2.CreateEncryptor (rc2.Key, rc2.IV);
121 			byte[] data = new byte[encryptor.InputBlockSize];
122 			byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
123 
124 			ICryptoTransform decryptor = rc2.CreateDecryptor (rc2.Key, null);
125 			byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
126 			Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
127 			// null iv != SymmetricAlgorithm.IV
128 		}
129 
130 		[Test]
CreateDecryptor_KeyIv()131 		public void CreateDecryptor_KeyIv ()
132 		{
133 			byte[] originalKey = rc2.Key;
134 			byte[] originalIV = rc2.IV;
135 
136 			byte[] key = (byte[]) rc2.Key.Clone ();
137 			Array.Reverse (key);
138 			byte[] iv = (byte[]) rc2.IV.Clone ();
139 			Array.Reverse (iv);
140 
141 			Assert.IsNotNull (rc2.CreateEncryptor (key, iv), "CreateDecryptor");
142 
143 			Assert.AreEqual (originalKey, rc2.Key, "Key");
144 			Assert.AreEqual (originalIV, rc2.IV, "IV");
145 			// SymmetricAlgorithm Key and IV not changed by CreateDecryptor
146 		}
147 
148 		// Setting the IV is more restrictive than supplying an IV to
149 		// CreateEncryptor and CreateDecryptor. See bug #76483
150 
CreateEncryptor_IV(int size)151 		private ICryptoTransform CreateEncryptor_IV (int size)
152 		{
153 			byte[] iv = (size == -1) ? null : new byte[size];
154 			return rc2.CreateEncryptor (rc2.Key, iv);
155 		}
156 
157 		[Test]
CreateEncryptor_IV_Null()158 		public void CreateEncryptor_IV_Null ()
159 		{
160 			int size = (rc2.BlockSize >> 3) - 1;
161 			CreateEncryptor_IV (-1);
162 		}
163 
164 		[Test]
165 		[ExpectedException (typeof (CryptographicException))]
CreateEncryptor_IV_Zero()166 		public void CreateEncryptor_IV_Zero ()
167 		{
168 			int size = (rc2.BlockSize >> 3) - 1;
169 			CreateEncryptor_IV (0);
170 		}
171 
172 		[Test]
173 		[ExpectedException (typeof (CryptographicException))]
CreateEncryptor_IV_TooSmall()174 		public void CreateEncryptor_IV_TooSmall ()
175 		{
176 			int size = (rc2.BlockSize >> 3) - 1;
177 			CreateEncryptor_IV (size);
178 		}
179 
180 		[Test]
CreateEncryptor_IV_BlockSize()181 		public void CreateEncryptor_IV_BlockSize ()
182 		{
183 			int size = (rc2.BlockSize >> 3);
184 			CreateEncryptor_IV (size);
185 		}
186 
187 		[Test]
CreateEncryptor_IV_TooBig()188 		public void CreateEncryptor_IV_TooBig ()
189 		{
190 			int size = rc2.BlockSize; // 8 times too big
191 			CreateEncryptor_IV (size);
192 		}
193 
CreateDecryptor_IV(int size)194 		private ICryptoTransform CreateDecryptor_IV (int size)
195 		{
196 			byte[] iv = (size == -1) ? null : new byte[size];
197 			return rc2.CreateDecryptor (rc2.Key, iv);
198 		}
199 
200 		[Test]
CreateDecryptor_IV_Null()201 		public void CreateDecryptor_IV_Null ()
202 		{
203 			int size = (rc2.BlockSize >> 3) - 1;
204 			CreateDecryptor_IV (-1);
205 		}
206 
207 		[Test]
208 		[ExpectedException (typeof (CryptographicException))]
CreateDecryptor_IV_Zero()209 		public void CreateDecryptor_IV_Zero ()
210 		{
211 			int size = (rc2.BlockSize >> 3) - 1;
212 			CreateDecryptor_IV (0);
213 		}
214 
215 		[Test]
216 		[ExpectedException (typeof (CryptographicException))]
CreateDecryptor_IV_TooSmall()217 		public void CreateDecryptor_IV_TooSmall ()
218 		{
219 			int size = (rc2.BlockSize >> 3) - 1;
220 			CreateDecryptor_IV (size);
221 		}
222 
223 		[Test]
CreateDecryptor_IV_BlockSize()224 		public void CreateDecryptor_IV_BlockSize ()
225 		{
226 			int size = (rc2.BlockSize >> 3);
227 			CreateDecryptor_IV (size);
228 		}
229 
230 		[Test]
CreateDecryptor_IV_TooBig()231 		public void CreateDecryptor_IV_TooBig ()
232 		{
233 			int size = rc2.BlockSize; // 8 times too big
234 			CreateDecryptor_IV (size);
235 		}
236 	}
237 }
238