2 // RSAOAEPKeyExchangeFormatterTest.cs - NUnit Test Cases for RSAOAEPKeyExchangeFormatter
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 RSAOAEPKeyExchangeFormatterTest : Assertion {
39 protected static RSA key;
44 // generating a keypair is REALLY long and the MS framework
45 // makes sure that we generate one (even if create an object
46 // to import an existing key). Mono is smarter in this case
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 RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter ();
63 Assertion.AssertNull ("RSAOAEPKeyExchangeFormatter.Parameter", keyex.Parameter);
64 Assertion.AssertNull ("RSAOAEPKeyExchangeFormatter.Parameters", keyex.Parameters);
65 Assertion.AssertNull ("RSAOAEPKeyExchangeFormatter.Rng", keyex.Rng);
66 Assertion.AssertEquals ("RSAOAEPKeyExchangeFormatter.ToString()", "System.Security.Cryptography.RSAOAEPKeyExchangeFormatter", keyex.ToString ());
71 public void ExchangeMin()
73 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
76 byte[] EM = keyex.CreateKeyExchange (M);
77 AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
78 byte[] Mback = keyback.DecryptKeyExchange (EM);
79 AssertEquals ("RSAOAEPKeyExchangeFormatter Min", M, Mback);
81 catch (CryptographicException ce) {
82 // not supported by every version of Windows - Minimum: Windows XP
83 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
87 // test with a message 128 bits (16 bytes) long
89 public void Exchange128()
91 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
92 byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
94 byte[] EM = keyex.CreateKeyExchange (M, typeof (Rijndael));
95 AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
96 byte[] Mback = keyback.DecryptKeyExchange (EM);
97 AssertEquals ("RSAOAEPKeyExchangeFormatter 128", M, Mback);
99 catch (CryptographicException ce) {
100 // not supported by every version of Windows - Minimum: Windows XP
101 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
105 // test with a message 160 bits (20 bytes) long
107 public void Exchange192()
109 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
110 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);
113 AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
114 byte[] Mback = keyback.DecryptKeyExchange (EM);
115 AssertEquals ("RSAOAEPKeyExchangeFormatter 192", M, Mback);
117 catch (CryptographicException ce) {
118 // not supported by every version of Windows - Minimum: Windows XP
119 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
123 // Max = (key size in bytes) - 2 * (hash length) - 2
125 public void ExchangeMax()
127 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
128 // use SHA1 internaly
129 byte[] M = new byte [(key.KeySize >> 3) - 2 * 20 - 2];
131 byte[] EM = keyex.CreateKeyExchange (M);
132 AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
133 byte[] Mback = keyback.DecryptKeyExchange (EM);
134 AssertEquals ("RSAOAEPKeyExchangeFormatter Max", M, Mback);
136 catch (CryptographicException ce) {
137 // not supported by every version of Windows - Minimum: Windows XP
138 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
142 // TestExchangeTooBig
144 [ExpectedException (typeof (CryptographicException))]
145 public void ExchangeTooBig()
147 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
148 byte[] M = new byte [(key.KeySize >> 3)- 10];
149 byte[] EM = keyex.CreateKeyExchange (M);
153 public void Parameter ()
155 RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
156 keyex.Parameter = new byte [1];
157 AssertEquals ("Parameter", 1, keyex.Parameter.Length);
163 RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
164 AssertNull ("Rng", keyex.Rng);
165 keyex.Rng = RandomNumberGenerator.Create ();
166 AssertNotNull ("Rng", keyex.Rng);
171 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
173 [ExpectedException (typeof (NullReferenceException))]
175 public void ExchangeNoKey ()
177 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter ();
178 byte[] M = keyex.CreateKeyExchange (new byte [16]);
182 [ExpectedException (typeof (InvalidCastException))]
183 public void ExchangeDSAKey ()
185 DSA dsa = DSA.Create ();
186 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (dsa);