2 // TestSuite.System.Security.Cryptography.RC2Test.cs
5 // Andrew Birkett (andy@nobugs.org)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // Copyright (C) 2004 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 RC2Test : Assertion {
41 public void DefaultProperties ()
43 RC2 algo = RC2.Create ();
44 AssertEquals ("Key Size", 128, algo.KeySize);
45 AssertEquals ("Key Length", 16, algo.Key.Length);
46 AssertEquals ("IV Length", 8, algo.IV.Length);
47 AssertEquals ("BlockSize", 64, algo.BlockSize);
48 AssertEquals ("FeedbackSize", 8, algo.FeedbackSize);
49 AssertEquals ("Mode", CipherMode.CBC, algo.Mode);
50 AssertEquals ("Padding", PaddingMode.PKCS7, algo.Padding);
51 AssertEquals ("LegalBlockSizes", 1, algo.LegalBlockSizes.Length);
52 AssertEquals ("LegalBlockSizes.MaxSize", 64, algo.LegalBlockSizes [0].MaxSize);
53 AssertEquals ("LegalBlockSizes.MinSize", 64, algo.LegalBlockSizes [0].MinSize);
54 AssertEquals ("LegalBlockSizes.SkipSize", 0, algo.LegalBlockSizes [0].SkipSize);
55 AssertEquals ("LegalKeySizes", 1, algo.LegalKeySizes.Length);
56 AssertEquals ("LegalKeySizes.MaxSize", 128, algo.LegalKeySizes [0].MaxSize);
57 AssertEquals ("LegalKeySizes.MinSize", 40, algo.LegalKeySizes [0].MinSize);
58 AssertEquals ("LegalKeySizes.SkipSize", 8, algo.LegalKeySizes [0].SkipSize);
61 private void CheckECB (int effective_bits, byte[] key, byte[] pt, byte[] expected)
63 RC2 c = RC2.Create ();
64 c.Mode = CipherMode.ECB;
65 c.Padding = PaddingMode.Zeros;
67 AssertEquals ("KeySize", key.Length * 8, c.KeySize);
68 c.EffectiveKeySize = effective_bits;
70 ICryptoTransform encryptor = c.CreateEncryptor ();
71 ICryptoTransform decryptor = c.CreateDecryptor ();
73 byte[] ct = new byte [pt.Length];
74 int n = encryptor.TransformBlock (pt, 0, pt.Length, ct, 0);
75 AssertEquals ("EncryptLen", n, pt.Length);
76 for (int i=0; i < n; i++) {
77 AssertEquals ("Encrypt" + i, ct [i], expected [i]);
80 byte[] rt = new byte [ct.Length];
81 n = decryptor.TransformBlock (ct, 0, ct.Length, rt, 0);
82 AssertEquals ("DecryptLen", n, ct.Length);
83 for (int i=0; i < n; i++) {
84 AssertEquals ("Decrypt" + i, rt [i], pt [i]);
90 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
92 [ExpectedException (typeof (CryptographicException))]
94 public void RFC2268Vector_1 ()
96 byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
97 byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
98 byte[] ct = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
100 // we don't support EffectiveKeySize != KeySize to match MS implementation
101 CheckECB (63, key, pt, ct);
105 public void RFC2268Vector_2 ()
107 byte[] key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
108 byte[] pt = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
109 byte[] ct = { 0x27, 0x8b, 0x27, 0xe4, 0x2e, 0x2f, 0x0d, 0x49 };
111 CheckECB (64, key, pt, ct);
115 public void RFC2268Vector_3 ()
117 byte[] key = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
118 byte[] pt = { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
119 byte[] ct = { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 };
121 CheckECB (64, key, pt, ct);
125 [ExpectedException (typeof (CryptographicException))]
126 public void RFC2268Vector_4 ()
128 byte[] key = { 0x88 };
129 byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
130 byte[] ct = { 0x61, 0xa8, 0xa2, 0x44, 0xad, 0xac, 0xcc, 0xf0 };
132 // we don't support KeySize < 40 to match MS implementation
133 CheckECB (64, key, pt, ct);
138 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
140 [ExpectedException (typeof (CryptographicException))]
142 public void RFC2268Vector_5 ()
144 byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a };
145 byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
146 byte[] ct = { 0x6c, 0xcf, 0x43, 0x08, 0x97, 0x4c, 0x26, 0x7f };
148 // we don't support EffectiveKeySize != KeySize to match MS implementation
149 CheckECB (64, key, pt, ct);
154 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
156 [ExpectedException (typeof (CryptographicException))]
158 public void RFC2268Vector_6 ()
160 byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
161 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 };
162 byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
163 byte[] ct = { 0x1a, 0x80, 0x7d, 0x27, 0x2b, 0xbe, 0x5d, 0xb1 };
165 // we don't support EffectiveKeySize != KeySize to match MS implementation
166 CheckECB (64, key, pt, ct);
170 public void RFC2268Vector_7 ()
172 byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
173 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 };
174 byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
175 byte[] ct = { 0x22, 0x69, 0x55, 0x2a, 0xb0, 0xf8, 0x5c, 0xa6 };
177 CheckECB (128, key, pt, ct);
181 [ExpectedException (typeof (CryptographicException))]
182 public void RFC2268Vector_8 ()
184 byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
185 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2,
186 0x16, 0xf8, 0x0a, 0x6f, 0x85, 0x92, 0x05, 0x84,
187 0xc4, 0x2f, 0xce, 0xb0, 0xbe, 0x25, 0x5d, 0xaf, 0x1e };
188 byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
189 byte[] ct = { 0x5b, 0x78, 0xd3, 0xa4, 0x3d, 0xff, 0xf1, 0xf1 };
191 // we don't support KeySize > 128 to match MS implementation
192 CheckECB (129, key, pt, ct);