2004-12-06 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RC2Test.cs
1 //
2 // TestSuite.System.Security.Cryptography.RC2Test.cs
3 //
4 // Authors:
5 //      Andrew Birkett (andy@nobugs.org)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // Copyright (C) 2004 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 RC2Test : Assertion {
39
40                 [Test]
41                 public void DefaultProperties ()
42                 {
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);
59                 }
60
61                 private void CheckECB (int effective_bits, byte[] key, byte[] pt, byte[] expected)
62                 {
63                         RC2 c = RC2.Create ();
64                         c.Mode = CipherMode.ECB;
65                         c.Padding = PaddingMode.Zeros;
66                         c.Key = key;
67                         AssertEquals ("KeySize", key.Length * 8, c.KeySize);
68                         c.EffectiveKeySize = effective_bits;
69
70                         ICryptoTransform encryptor = c.CreateEncryptor ();
71                         ICryptoTransform decryptor = c.CreateDecryptor ();
72
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]);
78                         }
79
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]);
85                         }
86                 }
87
88                 [Test]
89 #if NET_1_1
90                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
91 #else
92                 [ExpectedException (typeof (CryptographicException))]
93 #endif
94                 public void RFC2268Vector_1 ()
95                 {
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 };
99
100                         // we don't support EffectiveKeySize != KeySize to match MS implementation
101                         CheckECB (63, key, pt, ct);
102                 }
103
104                 [Test]
105                 public void RFC2268Vector_2 ()
106                 {
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 };
110
111                         CheckECB (64, key, pt, ct);
112                 }
113         
114                 [Test]
115                 public void RFC2268Vector_3 ()
116                 {
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 };
120
121                         CheckECB (64, key, pt, ct);
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (CryptographicException))]
126                 public void RFC2268Vector_4 ()
127                 {
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 };
131
132                         // we don't support KeySize < 40 to match MS implementation
133                         CheckECB (64, key, pt, ct);
134                 }
135
136                 [Test]
137 #if NET_1_1
138                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
139 #else
140                 [ExpectedException (typeof (CryptographicException))]
141 #endif
142                 public void RFC2268Vector_5 ()
143                 {
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 };
147
148                         // we don't support EffectiveKeySize != KeySize to match MS implementation
149                         CheckECB (64, key, pt, ct);
150                 }
151
152                 [Test]
153 #if NET_1_1
154                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
155 #else
156                 [ExpectedException (typeof (CryptographicException))]
157 #endif
158                 public void RFC2268Vector_6 ()
159                 {
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 };
164
165                         // we don't support EffectiveKeySize != KeySize to match MS implementation
166                         CheckECB (64, key, pt, ct);
167                 }
168
169                 [Test]
170                 public void RFC2268Vector_7 ()
171                 {
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 };
176
177                         CheckECB (128, key, pt, ct);
178                 }
179
180                 [Test]
181                 [ExpectedException (typeof (CryptographicException))]
182                 public void RFC2268Vector_8 ()
183                 {
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 };
190
191                         // we don't support KeySize > 128 to match MS implementation
192                         CheckECB (129, key, pt, ct);
193                 }
194         }
195 }