2 // CipherModeTest.cs - NUnit Test Cases for CipherMode
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Security.Cryptography;
32 using NUnit.Framework;
34 namespace MonoTests.System.Security.Cryptography {
37 public class CipherModeTest {
42 public void CipherModeEnum ()
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");
51 // SymmetricAlgorithm tests
53 private byte[] GetKey (SymmetricAlgorithm sa)
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++)
62 private byte[] GetIV (SymmetricAlgorithm sa)
64 return new byte [sa.BlockSize >> 3];
67 private string Roundtrip (SymmetricAlgorithm sa, CipherMode mode)
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);
80 ICryptoTransform dec = sa.CreateDecryptor ();
81 byte[] decdata = dec.TransformFinalBlock (encdata, 0, encdata.Length);
83 for (int i = 0; i < bs; i++)
84 Assert.AreEqual (data [i], decdata [i], i.ToString ());
90 public void DES_ECB ()
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");
97 public void DES_CBC ()
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");
104 public void DES_CFB ()
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");
111 [ExpectedException (typeof (CryptographicException))]
112 public void DES_OFB ()
114 Assert.AreEqual ("not implemented in any released framework",
115 Roundtrip (DES.Create (), CipherMode.OFB), "Encrypted data");
119 [ExpectedException (typeof (CryptographicException))]
120 public void DES_CTS ()
122 Assert.AreEqual ("not implemented in any released framework",
123 Roundtrip (DES.Create (), CipherMode.CTS), "Encrypted data");
127 public void RC2_ECB ()
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");
134 public void RC2_CBC ()
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");
141 public void RC2_CFB ()
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");
148 [ExpectedException (typeof (CryptographicException))]
149 public void RC2_OFB ()
151 Assert.AreEqual ("not implemented in any released framework",
152 Roundtrip (RC2.Create (), CipherMode.OFB), "Encrypted data");
156 [ExpectedException (typeof (CryptographicException))]
157 public void RC2_CTS ()
159 Assert.AreEqual ("not implemented in any released framework",
160 Roundtrip (RC2.Create (), CipherMode.CTS), "Encrypted data");
164 public void Rijndael_ECB ()
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");
171 public void Rijndael_CBC ()
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");
178 public void Rijndael_CFB ()
180 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",
181 Roundtrip (Rijndael.Create (), CipherMode.CFB), "Encrypted data");
184 // CFB was confused with OFB in Fx 1.0 and 1.1 (and wasn't supported because of that)
185 // However Mono does support it (because the same code is executed for all managed ciphers).
187 public void Rijndael_CFB ()
190 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",
191 Roundtrip (Rijndael.Create (), CipherMode.CFB), "Encrypted data");
193 catch (CryptographicException) {
194 // we assume this is the bugged MS implementation
199 [ExpectedException (typeof (CryptographicException))]
200 public void Rijndael_OFB ()
202 Assert.AreEqual ("not implemented in any released framework",
203 Roundtrip (Rijndael.Create (), CipherMode.OFB), "Encrypted data");
207 [ExpectedException (typeof (CryptographicException))]
208 public void Rijndael_CTS ()
210 Assert.AreEqual ("not implemented in any released framework",
211 Roundtrip (Rijndael.Create (), CipherMode.CTS), "Encrypted data");
215 public void TripleDES_ECB ()
217 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",
218 Roundtrip (TripleDES.Create (), CipherMode.ECB), "Encrypted data");
222 public void TripleDES_CBC ()
224 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",
225 Roundtrip (TripleDES.Create (), CipherMode.CBC), "Encrypted data");
229 public void TripleDES_CFB ()
231 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",
232 Roundtrip (TripleDES.Create (), CipherMode.CFB), "Encrypted data");
236 [ExpectedException (typeof (CryptographicException))]
237 public void TripleDES_OFB ()
239 Assert.AreEqual ("not implemented in any released framework",
240 Roundtrip (TripleDES.Create (), CipherMode.OFB), "Encrypted data");
244 [ExpectedException (typeof (CryptographicException))]
245 public void TripleDES_CTS ()
247 Assert.AreEqual ("not implemented in any released framework",
248 Roundtrip (TripleDES.Create (), CipherMode.CTS), "Encrypted data");