[bcl] Remove more NET_2_0 checks from class libs
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / CipherModeTest.cs
1 //
2 // CipherModeTest.cs - NUnit Test Cases for CipherMode
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Security.Cryptography;
31
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Security.Cryptography {
35
36         [TestFixture]
37         public class CipherModeTest {
38
39                 // Enum tests
40
41                 [Test]
42                 public void CipherModeEnum ()
43                 {
44                         Assert.AreEqual (1, (int)CipherMode.CBC, "CBC");
45                         Assert.AreEqual (4, (int)CipherMode.CFB, "CFB");
46                         Assert.AreEqual (5, (int)CipherMode.CTS, "CTS");
47                         Assert.AreEqual (2, (int)CipherMode.ECB, "ECB");
48                         Assert.AreEqual (3, (int)CipherMode.OFB, "OFB");
49                 }
50
51                 // SymmetricAlgorithm tests
52
53                 private byte[] GetKey (SymmetricAlgorithm sa)
54                 {
55                         byte[] key = new byte [sa.KeySize >> 3];
56                         // no weak key this way (DES, TripleDES)
57                         for (byte i = 0; i < key.Length; i++)
58                                 key [i] = i;
59                         return key;
60                 }
61
62                 private byte[] GetIV (SymmetricAlgorithm sa)
63                 {
64                         return new byte [sa.BlockSize >> 3];
65                 }
66
67                 private string Roundtrip (SymmetricAlgorithm sa, CipherMode mode) 
68                 {
69                         sa.Key = GetKey (sa);
70                         sa.IV = GetIV (sa);
71                         sa.Mode = mode;
72
73                         // two full blocks
74                         int bs = (sa.BlockSize >> 3) * 2;
75                         byte[] data = new byte [bs]; // in bytes
76                         ICryptoTransform enc = sa.CreateEncryptor ();
77                         byte[] encdata = enc.TransformFinalBlock (data, 0, data.Length);
78                         string result = BitConverter.ToString (encdata);
79
80                         ICryptoTransform dec = sa.CreateDecryptor ();
81                         byte[] decdata = dec.TransformFinalBlock (encdata, 0, encdata.Length);
82
83                         for (int i = 0; i < bs; i++)
84                                 Assert.AreEqual (data [i], decdata [i], i.ToString ());
85
86                         return result;
87                 }
88
89                 [Test]
90                 public void DES_ECB () 
91                 {
92                         Assert.AreEqual ("A5-17-3A-D5-95-7B-43-70-A5-17-3A-D5-95-7B-43-70-E4-81-A8-D3-97-14-D0-DE",
93                                 Roundtrip (DES.Create (), CipherMode.ECB), "Encrypted data");
94                 }
95
96                 [Test]
97                 public void DES_CBC ()
98                 {
99                         Assert.AreEqual ("A5-17-3A-D5-95-7B-43-70-79-6F-FD-B4-90-21-70-9D-FF-C8-76-01-24-7C-C3-82",
100                                 Roundtrip (DES.Create (), CipherMode.CBC), "Encrypted data");
101                 }
102
103                 [Test]
104                 public void DES_CFB ()
105                 {
106                         Assert.AreEqual ("A5-AA-9B-16-02-77-16-A1-86-BC-38-B6-E5-BA-53-4C-A5-F9-49-21-A9-8E-84-A0",
107                                 Roundtrip (DES.Create (), CipherMode.CFB), "Encrypted data");
108                 }
109
110                 [Test]
111                 [ExpectedException (typeof (CryptographicException))]
112                 public void DES_OFB ()
113                 {
114                         Assert.AreEqual ("not implemented in any released framework",
115                                 Roundtrip (DES.Create (), CipherMode.OFB), "Encrypted data");
116                 }
117
118                 [Test]
119                 [ExpectedException (typeof (CryptographicException))]
120                 public void DES_CTS ()
121                 {
122                         Assert.AreEqual ("not implemented in any released framework", 
123                                 Roundtrip (DES.Create (), CipherMode.CTS), "Encrypted data");
124                 }
125
126                 [Test]
127                 public void RC2_ECB ()
128                 {
129                         Assert.AreEqual ("9C-4B-FE-6D-FE-73-9C-2B-9C-4B-FE-6D-FE-73-9C-2B-AB-C5-6E-FB-C4-0E-63-34",
130                                 Roundtrip (RC2.Create (), CipherMode.ECB), "Encrypted data");
131                 }
132
133                 [Test]
134                 public void RC2_CBC ()
135                 {
136                         Assert.AreEqual ("9C-4B-FE-6D-FE-73-9C-2B-52-8F-C8-47-2B-66-F9-70-B2-67-CF-23-D7-D7-6D-A6",
137                                 Roundtrip (RC2.Create (), CipherMode.CBC), "Encrypted data");
138                 }
139
140                 [Test]
141                 public void RC2_CFB ()
142                 {
143                         Assert.AreEqual ("9C-5A-41-95-9A-15-12-C2-54-1C-9C-6C-4B-65-A0-36-DD-7F-2B-0D-D5-D2-C0-CD",
144                                 Roundtrip (RC2.Create (), CipherMode.CFB), "Encrypted data");
145                 }
146
147                 [Test]
148                 [ExpectedException (typeof (CryptographicException))]
149                 public void RC2_OFB ()
150                 {
151                         Assert.AreEqual ("not implemented in any released framework",
152                                 Roundtrip (RC2.Create (), CipherMode.OFB), "Encrypted data");
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (CryptographicException))]
157                 public void RC2_CTS ()
158                 {
159                         Assert.AreEqual ("not implemented in any released framework",
160                                 Roundtrip (RC2.Create (), CipherMode.CTS), "Encrypted data");
161                 }
162
163                 [Test]
164                 public void Rijndael_ECB ()
165                 {
166                         Assert.AreEqual ("F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-9F-3B-75-04-92-6F-8B-D3-6E-31-18-E9-03-A4-CD-4A",
167                                 Roundtrip (Rijndael.Create (), CipherMode.ECB), "Encrypted data");
168                 }
169
170                 [Test]
171                 public void Rijndael_CBC ()
172                 {
173                         Assert.AreEqual ("F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-D4-E9-69-25-C0-BF-CF-FB-52-F8-A1-87-EE-77-4A-AB-C3-28-91-ED-46-6E-6F-98-C1-4D-65-14-ED-9D-1E-5B",
174                                 Roundtrip (Rijndael.Create (), CipherMode.CBC), "Encrypted data");
175                 }
176                 [Test]
177                 public void Rijndael_CFB ()
178                 {
179                         Assert.AreEqual ("F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-D4-E9-69-25-C0-BF-CF-FB-52-F8-A1-87-EE-77-4A-AB-26-03-7F-B7-B5-88-AE-0A-F8-AF-1E-CF-9C-F5-3A-8A",
180                                 Roundtrip (Rijndael.Create (), CipherMode.CFB), "Encrypted data");
181                 }
182                 [Test]
183                 [ExpectedException (typeof (CryptographicException))]
184                 public void Rijndael_OFB ()
185                 {
186                         Assert.AreEqual ("not implemented in any released framework",
187                                 Roundtrip (Rijndael.Create (), CipherMode.OFB), "Encrypted data");
188                 }
189
190                 [Test]
191                 [ExpectedException (typeof (CryptographicException))]
192                 public void Rijndael_CTS ()
193                 {
194                         Assert.AreEqual ("not implemented in any released framework",
195                                 Roundtrip (Rijndael.Create (), CipherMode.CTS), "Encrypted data");
196                 }
197
198                 [Test]
199                 public void TripleDES_ECB ()
200                 {
201                         Assert.AreEqual ("89-4B-C3-08-54-26-A4-41-89-4B-C3-08-54-26-A4-41-A3-CF-6E-C8-8B-D9-7D-73",
202                                 Roundtrip (TripleDES.Create (), CipherMode.ECB), "Encrypted data");
203                 }
204
205                 [Test]
206                 public void TripleDES_CBC ()
207                 {
208                         Assert.AreEqual ("89-4B-C3-08-54-26-A4-41-06-8E-DF-B5-F0-23-AB-B4-76-40-68-9A-26-7D-8D-6E",
209                                 Roundtrip (TripleDES.Create (), CipherMode.CBC), "Encrypted data");
210                 }
211
212                 [Test]
213                 public void TripleDES_CFB ()
214                 {
215                         Assert.AreEqual ("89-9F-00-9D-26-BB-21-59-85-6D-A2-BF-15-FE-73-53-1F-CE-35-26-5B-DF-43-26",
216                                 Roundtrip (TripleDES.Create (), CipherMode.CFB), "Encrypted data");
217                 }
218
219                 [Test]
220                 [ExpectedException (typeof (CryptographicException))]
221                 public void TripleDES_OFB ()
222                 {
223                         Assert.AreEqual ("not implemented in any released framework",
224                                 Roundtrip (TripleDES.Create (), CipherMode.OFB), "Encrypted data");
225                 }
226
227                 [Test]
228                 [ExpectedException (typeof (CryptographicException))]
229                 public void TripleDES_CTS ()
230                 {
231                         Assert.AreEqual ("not implemented in any released framework",
232                                 Roundtrip (TripleDES.Create (), CipherMode.CTS), "Encrypted data");
233                 }
234         }
235 }