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 {
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 Assert.AreEqual("<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 Assert.IsNull (keyex.Rng, "RSAPKCS1KeyExchangeFormatter.Rng");
66 Assert.AreEqual("System.Security.Cryptography.RSAPKCS1KeyExchangeFormatter", keyex.ToString ());
70 [ExpectedException (typeof (ArgumentNullException))]
71 public void KeyExchangeNull ()
73 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
74 byte[] EM = keyex.CreateKeyExchange (null);
77 // TestExchangeMin (1)
79 public void KeyExchangeMin ()
81 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
83 byte[] EM = keyex.CreateKeyExchange (M);
85 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
86 byte[] Mback = keyback.DecryptKeyExchange (EM);
87 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
90 // test with a message 128 bits (16 bytes) long
92 public void KeyExchange128bits ()
94 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
95 byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
96 byte[] EM = keyex.CreateKeyExchange (M, typeof (Rijndael));
98 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
99 byte[] Mback = keyback.DecryptKeyExchange (EM);
100 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
103 // test with a message 160 bits (20 bytes) long
105 public void KeyExchange160bits ()
107 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
108 byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49, 0x00, 0x00, 0x00, 0x00 };
109 byte[] EM = keyex.CreateKeyExchange (M);
111 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
112 byte[] Mback = keyback.DecryptKeyExchange (EM);
113 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
118 public void KeyExchangeMax()
120 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
121 byte[] M = new byte [(key.KeySize >> 3)- 11];
122 byte[] EM = keyex.CreateKeyExchange (M);
124 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
125 byte[] Mback = keyback.DecryptKeyExchange (EM);
126 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
129 // TestExchangeTooBig
131 [ExpectedException (typeof (CryptographicException))]
132 public void KeyExchangeTooBig()
134 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
135 byte[] M = new byte [(key.KeySize >> 3)- 10];
136 byte[] EM = keyex.CreateKeyExchange (M);
142 RSAPKCS1KeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
143 Assert.IsNull (keyex.Rng, "Rng 1");
144 keyex.Rng = RandomNumberGenerator.Create ();
145 Assert.IsNotNull (keyex.Rng, "Rng 2");
149 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
150 public void ExchangeNoKey ()
152 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();
153 byte[] M = keyex.CreateKeyExchange (new byte [16]);
157 [ExpectedException (typeof (InvalidCastException))]
158 public void ExchangeDSAKey ()
160 DSA dsa = DSA.Create ();
161 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (dsa);
165 [ExpectedException (typeof (ArgumentNullException))]
166 public void CreateWithNullKey ()
168 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (null);
172 [ExpectedException (typeof (ArgumentNullException))]
173 public void CreateAndSetNullKey ()
175 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();