2 // RSAPKCS1SignatureFormatterTest.cs - NUnit tests for PKCS#1 v.1.5 signature.
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
11 using NUnit.Framework;
13 using System.Security.Cryptography;
15 namespace MonoTests.System.Security.Cryptography {
18 public class RSAPKCS1SignatureFormatterTest : Assertion {
20 private static byte[] signatureRSASHA256 = { 0x0F, 0xE3, 0x15, 0x5B, 0x4D, 0xA1, 0xB4, 0x13, 0x93, 0x91, 0x1E, 0x17, 0xF9, 0x36, 0xB3, 0x2C, 0xAC, 0x51, 0x77, 0xBC, 0x86, 0x21, 0xB0, 0x69, 0x75, 0x57, 0xAF, 0xB0, 0xAD, 0xF9, 0x42, 0xF5, 0x58, 0xBC, 0xD5, 0x61, 0xD5, 0x14, 0x8E, 0xC6, 0xE0, 0xB3, 0xB5, 0x51, 0xCD, 0x17, 0x68, 0x58, 0x27, 0x74, 0x8A, 0xA7, 0x88, 0xB9, 0x24, 0xD6, 0xE4, 0xC4, 0x93, 0x82, 0x95, 0xB4, 0x36, 0x14, 0x48, 0xA7, 0xF6, 0x27, 0x87, 0xEB, 0xD8, 0xB9, 0x75, 0x14, 0x75, 0xFB, 0x6E, 0xA1, 0xF7, 0xAB, 0xA6, 0x78, 0x32, 0xEF, 0x1A, 0x23, 0x60, 0xD3, 0x0C, 0x8D, 0xFE, 0x89, 0x72, 0xB7, 0x93, 0x6D, 0x00, 0x25, 0xED, 0xF5, 0x55, 0x66, 0xA8, 0x52, 0x7F, 0x20, 0xFD, 0x77, 0xDA, 0x10, 0x77, 0xE9, 0xF0, 0x58, 0x8D, 0xE6, 0x3A, 0x5A, 0x00, 0x83, 0x64, 0x42, 0xA5, 0x15, 0x79, 0x3C, 0xB0, 0x8F };
21 private static byte[] signatureRSASHA384 = { 0x86, 0x20, 0x2A, 0xB6, 0xA8, 0x0F, 0x59, 0x42, 0xCA, 0x83, 0xC3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, 0xB0, 0x60, 0xA4, 0xBE, 0x1C, 0x9B, 0x05, 0x2A, 0xA9, 0x6A, 0x12, 0xFF, 0x73, 0x84, 0x5C, 0xA8, 0x74, 0x5B, 0x9C, 0xA2, 0x07, 0x9D, 0x73, 0xB8, 0xE3, 0x20, 0x16, 0x3C, 0x47, 0x8F, 0x27, 0x7A, 0x48, 0xAF, 0x01, 0x07, 0xA0, 0x6A, 0x2D, 0x71, 0xAD, 0xDD, 0x8B, 0x68, 0xC8, 0x32, 0x61, 0x95, 0x68, 0x22, 0x1B, 0x8B, 0xD9, 0x86, 0xA7, 0xBE, 0x60, 0x06, 0x70, 0x7C, 0xED, 0x51, 0x28, 0x66, 0x28, 0xF0, 0x65 };
24 private static RSA rsa;
25 private static DSA dsa;
27 private RSAPKCS1SignatureFormatter fmt;
34 rsa.ImportParameters (AllTests.GetRsaKey (true));
40 public void AssertEquals (string msg, byte[] array1, byte[] array2)
42 AllTests.AssertEquals (msg, array1, array2);
46 public void ConstructorEmpty ()
48 fmt = new RSAPKCS1SignatureFormatter ();
49 AssertNotNull ("RSAPKCS1SignatureFormatter()", fmt);
53 public void ConstructorNull ()
55 fmt = new RSAPKCS1SignatureFormatter (null);
56 AssertNotNull ("RSAPKCS1SignatureFormatter(null)", fmt);
60 public void ConstructorRSA ()
62 fmt = new RSAPKCS1SignatureFormatter (rsa);
63 AssertNotNull ("RSAPKCS1SignatureFormatter(rsa)", fmt);
67 [ExpectedException (typeof (InvalidCastException))]
68 public void ConstructorDSA ()
70 fmt = new RSAPKCS1SignatureFormatter (dsa);
74 public void SetKeyRSA ()
76 fmt = new RSAPKCS1SignatureFormatter ();
81 [ExpectedException (typeof (InvalidCastException))]
82 public void SetKeyDSA ()
84 fmt = new RSAPKCS1SignatureFormatter ();
89 public void SetKeyNull ()
91 fmt = new RSAPKCS1SignatureFormatter ();
96 public void SetHashAlgorithmSHA1 ()
98 fmt = new RSAPKCS1SignatureFormatter ();
99 fmt.SetHashAlgorithm ("SHA1");
103 public void SetHashAlgorithmMD5 ()
105 fmt = new RSAPKCS1SignatureFormatter ();
106 fmt.SetHashAlgorithm ("MD5");
110 public void SetHashAlgorithmSHA256 ()
112 fmt = new RSAPKCS1SignatureFormatter ();
113 fmt.SetHashAlgorithm ("SHA256");
117 public void SetHashAlgorithmSHA384 ()
119 fmt = new RSAPKCS1SignatureFormatter ();
120 fmt.SetHashAlgorithm ("SHA384");
124 public void SetHashAlgorithmSHA512 ()
126 fmt = new RSAPKCS1SignatureFormatter ();
127 fmt.SetHashAlgorithm ("SHA512");
131 [ExpectedException (typeof (ArgumentNullException))]
132 public void SetHashAlgorithmNull ()
134 fmt = new RSAPKCS1SignatureFormatter ();
135 fmt.SetHashAlgorithm (null);
138 // see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingsignatures.asp
141 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
142 public void CreateSignatureNullHash ()
144 fmt = new RSAPKCS1SignatureFormatter ();
147 byte[] signature = fmt.CreateSignature (hash);
151 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
152 public void CreateSignatureNoHashAlgorithm ()
154 fmt = new RSAPKCS1SignatureFormatter ();
156 byte[] hash = new byte [1];
157 byte[] signature = fmt.CreateSignature (hash);
161 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
162 public void CreateSignatureNoKey ()
164 fmt = new RSAPKCS1SignatureFormatter ();
166 fmt.SetHashAlgorithm ("SHA1");
167 byte[] hash = new byte [20];
168 byte[] signature = fmt.CreateSignature (hash);
172 public void CreateSignatureRSASHA1 ()
174 fmt = new RSAPKCS1SignatureFormatter ();
175 // we need the private key
178 fmt.SetHashAlgorithm ("SHA1");
179 byte[] hash = new byte [20];
180 byte[] signature = fmt.CreateSignature (hash);
181 AssertNotNull ("CreateSignature(SHA1)", signature);
185 [ExpectedException (typeof (CryptographicException))]
186 public void CreateSignatureRSASHA1BadLength ()
188 fmt = new RSAPKCS1SignatureFormatter ();
189 // we need the private key
192 fmt.SetHashAlgorithm ("SHA1");
193 byte[] hash = new byte [19];
194 byte[] signature = fmt.CreateSignature (hash);
198 public void CreateSignatureRSAMD5 ()
200 fmt = new RSAPKCS1SignatureFormatter ();
201 // we need the private key
204 fmt.SetHashAlgorithm ("MD5");
205 byte[] hash = new byte [16];
206 byte[] signature = fmt.CreateSignature (hash);
207 AssertNotNull ("CreateSignature(MD5)", signature);
210 private byte[] CreateSignature (string hashName, int hashSize)
212 byte[] signature = null;
213 fmt = new RSAPKCS1SignatureFormatter ();
215 // we need the private key
216 RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
221 rsa.ImportParameters (AllTests.GetRsaKey (true));
224 HashAlgorithm ha = HashAlgorithm.Create (hashName);
225 byte[] data = new byte [ha.HashSize >> 3];
226 // this way we get the same results as CreateSignatureHash
227 data = ha.ComputeHash (data);
229 fmt.SetHashAlgorithm (hashName);
230 signature = fmt.CreateSignature (data);
232 Fail ("CreateSignature(" + hashName + ") Expected CryptographicException but got none");
234 catch (CryptographicException) {
238 catch (Exception e) {
240 Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
245 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
246 // supported by Mono::
248 public void CreateSignatureRSASHA256 ()
250 byte[] signature = CreateSignature ("SHA256", 32);
251 if (signature != null)
252 AssertEquals ("CreateSignature(SHA256)", signatureRSASHA256, signature);
255 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
256 // supported by Mono::
258 public void CreateSignatureRSASHA384 ()
260 byte[] signature = CreateSignature ("SHA384", 48);
261 if (signature != null)
262 AssertEquals ("CreateSignature(SHA384)", signatureRSASHA384, signature);
265 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
266 // supported by Mono::
268 public void CreateSignatureRSASHA512 ()
270 byte[] signature = CreateSignature ("SHA512", 64);
271 if (signature != null)
272 AssertEquals ("CreateSignature(SHA512)", signatureRSASHA512, signature);
276 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
277 public void CreateSignatureRSABadHash ()
279 fmt = new RSAPKCS1SignatureFormatter ();
280 // we need the private key
284 byte[] signature = fmt.CreateSignature (hash);
288 [ExpectedException (typeof (ArgumentNullException))]
289 public void CreateSignatureHashBadHash ()
291 fmt = new RSAPKCS1SignatureFormatter ();
292 HashAlgorithm hash = null;
293 byte[] data = new byte [20];
295 byte[] signature = fmt.CreateSignature (hash);
299 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
300 public void CreateSignatureHashNoKey ()
302 fmt = new RSAPKCS1SignatureFormatter ();
303 byte[] data = new byte [20];
305 HashAlgorithm hash = SHA1.Create ();
306 hash.ComputeHash (data);
307 byte[] signature = fmt.CreateSignature (hash);
311 public void CreateSignatureHashSHA1 ()
313 fmt = new RSAPKCS1SignatureFormatter ();
314 byte[] data = new byte [20];
315 // we need the private key
318 HashAlgorithm hash = SHA1.Create ();
319 hash.ComputeHash (data);
320 byte[] signature = fmt.CreateSignature (hash);
321 AssertNotNull ("CreateSignature(SHA1)", signature);
325 public void CreateSignatureHashMD5 ()
327 fmt = new RSAPKCS1SignatureFormatter ();
328 byte[] data = new byte [16];
329 // we need the private key
332 HashAlgorithm hash = MD5.Create ();
333 hash.ComputeHash (data);
334 byte[] signature = fmt.CreateSignature (hash);
335 AssertNotNull ("CreateSignature(MD5)", signature);
338 private byte[] CreateSignatureHash (string hashName)
340 byte[] signature = null;
341 fmt = new RSAPKCS1SignatureFormatter ();
343 // we need the private key
344 RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
349 rsa.ImportParameters (AllTests.GetRsaKey (true));
352 HashAlgorithm hash = HashAlgorithm.Create (hashName);
353 byte[] data = new byte [(hash.HashSize >> 3)];
354 hash.ComputeHash (data);
355 signature = fmt.CreateSignature (hash);
357 Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got none");
359 catch (CryptographicException) {
363 catch (Exception e) {
365 Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
370 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
371 // supported by Mono::
373 public void CreateSignatureHashSHA256 ()
375 byte[] signature = CreateSignatureHash ("SHA256");
376 if (signature != null)
377 AssertEquals ("CreateSignatureHash(SHA256)", signatureRSASHA256, signature);
380 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
381 // supported by Mono::
383 public void CreateSignatureHashSHA384 ()
385 byte[] signature = CreateSignatureHash ("SHA384");
386 if (signature != null)
387 AssertEquals ("CreateSignatureHash(SHA384)", signatureRSASHA384, signature);
390 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
391 // supported by Mono::
393 public void CreateSignatureHashSHA512 ()
395 byte[] signature = CreateSignatureHash ("SHA512");
396 if (signature != null)
397 AssertEquals ("CreateSignatureHash(SHA512)", signatureRSASHA512, signature);