This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / Rfc2898DeriveBytesTest.cs
1 //
2 // Rfc2898DeriveBytesTest.cs - NUnit Test Cases for Rfc2898DeriveBytes
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 #if NET_2_0
11
12 using NUnit.Framework;
13
14 using System;
15 using System.Security.Cryptography;
16
17 namespace MonoTests.System.Security.Cryptography {
18
19         // References:
20         // a.   PKCS#5: Password-Based Cryptography Standard 
21         //      http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html
22         // b.   RFC 3211 - Password-based Encryption for CMS
23         //      http://www.faqs.org/rfcs/rfc3211.html
24
25         [TestFixture]
26         public class Rfc2898DeriveBytesTest : Assertion {
27
28                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
29                 {
30                         AllTests.AssertEquals (msg, array1, array2);
31                 }
32
33                 static private byte[] salt = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
34
35                 [Test]
36                 public void ConstructorPasswordSalt () 
37                 {
38                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
39                         AssertEquals ("IterationCount", 1000, pkcs5.IterationCount);
40                         AssertEquals ("Salt", salt, pkcs5.Salt);
41                 }
42
43                 [Test]
44                 [ExpectedException (typeof (ArgumentNullException))]
45                 public void ConstructorPasswordNullSalt () 
46                 {
47                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes (null, salt);
48                 }
49
50                 [Test]
51                 [ExpectedException (typeof (ArgumentNullException))]
52                 public void ConstructorPasswordSaltNull () 
53                 {
54                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", null);
55                 }
56
57                 [Test]
58                 public void ConstructorPasswordSaltIterations () 
59                 {
60                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
61                         AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
62                         AssertEquals ("Salt", salt, pkcs5.Salt);
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (ArgumentNullException))]
67                 public void ConstructorPasswordNullSaltIterations () 
68                 {
69                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes (null, salt, 5);
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (ArgumentNullException))]
74                 public void ConstructorPasswordSaltNullIterations () 
75                 {
76                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", null, 5);
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
81                 public void ConstructorPasswordSaltIterationsZero () 
82                 {
83                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 0);
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
88                 public void ConstructorPasswordSaltIterationsNegative () 
89                 {
90                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, Int32.MinValue);
91                 }
92
93                 [Test]
94                 public void ConstructorPasswordSaltLength () 
95                 {
96                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8);
97                         AssertEquals ("IterationCount", 1000, pkcs5.IterationCount);
98                         AssertEquals ("Salt", 8, pkcs5.Salt.Length);
99                 }
100
101                 [Test]
102                 [ExpectedException (typeof (ArgumentNullException))]
103                 public void ConstructorPasswordNullSaltLength () 
104                 {
105                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes (null, 8);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (ArgumentException))]
110                 public void ConstructorPasswordSaltLengthZero () 
111                 {
112                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 0);
113                 }
114
115                 [Test]
116                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
117                 public void ConstructorPasswordSaltLengthNegative () 
118                 {
119                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", Int32.MinValue);
120                 }
121
122                 [Test]
123                 public void ConstructorPasswordSaltLengthIterations () 
124                 {
125                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8, 5);
126                         AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
127                         AssertEquals ("Salt", 8, pkcs5.Salt.Length);
128                 }
129
130                 [Test]
131                 [ExpectedException (typeof (ArgumentNullException))]
132                 public void ConstructorPasswordNullSaltLengthIterations () 
133                 {
134                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes (null, 8, 5);
135                 }
136
137                 [Test]
138                 [ExpectedException (typeof (ArgumentException))]
139                 public void ConstructorPasswordSaltLengthZeroIterations () 
140                 {
141                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 0, 5);
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
146                 public void ConstructorPasswordSaltLengthNegativeIterations () 
147                 {
148                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", Int32.MinValue, 5);
149                 }
150
151                 [Test]
152                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
153                 public void ConstructorPasswordSaltLengthIterationsZero () 
154                 {
155                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8, 0);
156                 }
157
158                 [Test]
159                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
160                 public void ConstructorPasswordSaltLengthIterationsNegative () 
161                 {
162                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", 8, Int32.MinValue);
163                 }
164
165                 [Test]
166                 public void IterationCount () 
167                 {
168                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
169                         AssertEquals ("IterationCount", 5, pkcs5.IterationCount);
170                         pkcs5.IterationCount = Int32.MaxValue;
171                         AssertEquals ("IterationCount", Int32.MaxValue, pkcs5.IterationCount);
172                 }
173
174                 [Test]
175                 public void SaltNew () 
176                 {
177                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
178                         AssertEquals ("Salt", salt, pkcs5.Salt);
179                         byte[] newSalt = (byte[]) salt.Clone ();
180                         newSalt [0] = 0xFF;
181                         pkcs5.Salt = newSalt;
182                         AssertEquals ("Salt(new)", newSalt, pkcs5.Salt);
183                 }
184
185                 [Test]
186                 public void SaltCantModifyInternal () 
187                 {
188                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
189                         AssertEquals ("Salt", salt, pkcs5.Salt);
190                         byte[] modSalt = (byte[]) salt.Clone ();
191                         modSalt [0] = 0xFF;
192                         Assert ("Can't modify internal salt", (BitConverter.ToString(pkcs5.Salt) != BitConverter.ToString(modSalt)));
193                 }
194
195                 [Test]
196                 [ExpectedException (typeof (ArgumentNullException))]
197                 public void SaltNull () 
198                 {
199                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
200                         pkcs5.Salt = null;
201                 }
202
203                 [Test]
204                 [ExpectedException (typeof (ArgumentException))]
205                 public void SaltTooSmall () 
206                 {
207                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt);
208                         byte[] smallSalt = { 0x01, 0x02, 0x03, 0x04, 0x05 };
209                         pkcs5.Salt = smallSalt;
210                 }
211
212                 [Test]
213                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
214                 public void GetBytesZero ()
215                 {
216                         byte[] expected = { 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6 };
217                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
218                         byte[] key = pkcs5.GetBytes (0);
219                         AssertEquals ("GetBytesZero", 0, key.Length);
220                 }
221
222                 [Test]
223                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
224                 public void GetBytesNegative () 
225                 {
226                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
227                         byte[] key = pkcs5.GetBytes (Int32.MinValue);
228                 }
229
230                 [Test]
231                 public void RFC3211_TC1 () 
232                 {
233                         byte[] expected = { 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6 };
234                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("password", salt, 5);
235                         byte[] key = pkcs5.GetBytes (8);
236                         AssertEquals ("RFC3211_TC1", expected, key);
237                 }
238
239                 [Test]
240                 public void RFC3211_TC2 () 
241                 {
242                         byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
243                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
244                         byte[] key = pkcs5.GetBytes (16);
245                         AssertEquals ("RFC3211_TC2", expected, key);
246                 }
247
248                 [Test]
249                 public void RFC3211_TC2_TwoBlocks () 
250                 {
251                         byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86, 0x07, 0x12, 0x63, 0x80, 0xcc, 0x47, 0xab, 0x2d, 0xa6, 0xcc, 0xda, 0xfb, 0x26, 0x83, 0xdf, 0xe8 };
252                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
253                         byte[] key = pkcs5.GetBytes (32);
254                         AssertEquals ("RFC3211_TC2_TwoBlocks", expected, key);
255                 }
256
257                 [Test]
258                 public void RFC3211_TC2_Splitted_OneBlock () 
259                 {
260                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
261                         byte[] key1 = pkcs5.GetBytes (8);
262                         byte[] expected_part1 = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
263                         AssertEquals ("RFC3211_TC2_Splitted_OneBlock-1", expected_part1, key1);
264                         byte[] expected_part2 = { 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
265                         byte[] key2 = pkcs5.GetBytes (8);
266                         AssertEquals ("RFC3211_TC2_Splitted_OneBlock-2", expected_part2, key2);
267                 }
268
269                 [Test]
270                 public void RFC3211_TC2_Splitted_TwoBlocks () 
271                 {
272                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
273                         byte[] key1 = pkcs5.GetBytes (16);
274                         byte[] expected_part1 = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE, 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86 };
275                         AssertEquals ("RFC3211_TC2_Splitted_TwoBlocks-1", expected_part1, key1);
276                         byte[] expected_part2 = { 0x07, 0x12, 0x63, 0x80, 0xcc, 0x47, 0xab, 0x2d, 0xa6, 0xcc, 0xda, 0xfb, 0x26, 0x83, 0xdf, 0xe8 };
277                         byte[] key2 = pkcs5.GetBytes (16);
278                         AssertEquals ("RFC3211_TC2_Splitted_TwoBlocks-2", expected_part2, key2);
279                 }
280
281                 [Test]
282                 public void RFC3211_TC2_Reset () 
283                 {
284                         byte[] expected = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE };
285                         Rfc2898DeriveBytes pkcs5 = new Rfc2898DeriveBytes ("All n-entities must communicate with other n-entities via n-1 entiteeheehees", salt, 500);
286                         byte[] key1 = pkcs5.GetBytes (8);
287                         AssertEquals ("RFC3211_TC2_part1", expected, key1);
288                         pkcs5.Reset ();
289                         byte[] key2 = pkcs5.GetBytes (8);
290                         AssertEquals ("RFC3211_TC2_part2", expected, key2);
291                 }
292         }
293 }
294
295 #endif