2 // RSAPKCS1KeyExchangeFormatterTest.cs - NUnit Test Cases for RSAPKCS1KeyExchangeFormatter
5 // Sebastien Pouliot (sebastien@ximian.com)
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.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.
30 using NUnit.Framework;
32 using System.Security.Cryptography;
34 namespace MonoTests.System.Security.Cryptography {
37 public class RSAPKCS1KeyExchangeFormatterTest : Assertion {
39 protected static RSA key;
44 // generating a keypair is REALLY long and the framework
45 // makes sure that we generate one (even if create an object
46 // to import an exsting key)
49 key.ImportParameters (AllTests.GetRsaKey (true));
53 public void AssertEquals (string msg, byte[] array1, byte[] array2)
55 AllTests.AssertEquals (msg, array1, array2);
59 public void Properties ()
61 RSAPKCS1KeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();
63 AssertEquals("RSAPKCS1KeyExchangeFormatter.Parameters", "<enc:KeyEncryptionMethod enc:Algorithm=\"http://www.microsoft.com/xml/security/algorithm/PKCS1-v1.5-KeyEx\" xmlns:enc=\"http://www.microsoft.com/xml/security/encryption/v1.0\" />", keyex.Parameters);
65 AssertNull("RSAPKCS1KeyExchangeFormatter.Rng", keyex.Rng);
66 AssertEquals("RSAPKCS1KeyExchangeFormatter.ToString()", "System.Security.Cryptography.RSAPKCS1KeyExchangeFormatter", keyex.ToString ());
70 [ExpectedException (typeof (ArgumentNullException))]
72 [Category ("NotDotNet")] // Sometime causes System.ExecutionEngineException on MS implementation
74 public void KeyExchangeNull ()
76 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
77 byte[] EM = keyex.CreateKeyExchange (null);
80 // TestExchangeMin (1)
82 public void KeyExchangeMin ()
84 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
86 byte[] EM = keyex.CreateKeyExchange (M);
88 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
89 byte[] Mback = keyback.DecryptKeyExchange (EM);
90 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
93 // test with a message 128 bits (16 bytes) long
95 public void KeyExchange128bits ()
97 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
98 byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
99 byte[] EM = keyex.CreateKeyExchange (M, typeof (Rijndael));
101 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
102 byte[] Mback = keyback.DecryptKeyExchange (EM);
103 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
106 // test with a message 160 bits (20 bytes) long
108 public void KeyExchange160bits ()
110 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
111 byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49, 0x00, 0x00, 0x00, 0x00 };
112 byte[] EM = keyex.CreateKeyExchange (M);
114 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
115 byte[] Mback = keyback.DecryptKeyExchange (EM);
116 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
121 public void KeyExchangeMax()
123 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
124 byte[] M = new byte [(key.KeySize >> 3)- 11];
125 byte[] EM = keyex.CreateKeyExchange (M);
127 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
128 byte[] Mback = keyback.DecryptKeyExchange (EM);
129 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
132 // TestExchangeTooBig
134 [ExpectedException (typeof (CryptographicException))]
135 public void KeyExchangeTooBig()
137 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
138 byte[] M = new byte [(key.KeySize >> 3)- 10];
139 byte[] EM = keyex.CreateKeyExchange (M);
145 RSAPKCS1KeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
146 AssertNull ("Rng", keyex.Rng);
147 keyex.Rng = RandomNumberGenerator.Create ();
148 AssertNotNull ("Rng", keyex.Rng);
153 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
155 [ExpectedException (typeof (NullReferenceException))]
157 public void ExchangeNoKey ()
159 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();
160 byte[] M = keyex.CreateKeyExchange (new byte [16]);
164 [ExpectedException (typeof (InvalidCastException))]
165 public void ExchangeDSAKey ()
167 DSA dsa = DSA.Create ();
168 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (dsa);
172 [ExpectedException (typeof (ArgumentNullException))]
173 public void CreateWithNullKey ()
175 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (null);
179 [ExpectedException (typeof (ArgumentNullException))]
180 public void CreateAndSetNullKey ()
182 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();