Merge pull request #323 from crazyjncsu/master
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RSAOAEPKeyExchangeFormatterTest.cs
1 //
2 // RSAOAEPKeyExchangeFormatterTest.cs - NUnit Test Cases for RSAOAEPKeyExchangeFormatter
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Security.Cryptography;
33
34 namespace MonoTests.System.Security.Cryptography {
35
36         [TestFixture]
37         public class RSAOAEPKeyExchangeFormatterTest {
38
39                 protected static RSA key;
40
41                 [SetUp]
42                 public void SetUp () 
43                 {
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
47                         if (key == null) {
48                                 key = RSA.Create ();
49                                 key.ImportParameters (AllTests.GetRsaKey (true));
50                         }
51                 }
52
53                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
54                 {
55                         AllTests.AssertEquals (msg, array1, array2);
56                 }
57
58                 [Test]
59                 public void Properties () 
60                 {
61                         RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter ();
62                         keyex.SetKey (key);
63                         Assert.IsNull (keyex.Parameter, "RSAOAEPKeyExchangeFormatter.Parameter");
64                         Assert.IsNull (keyex.Parameters, "RSAOAEPKeyExchangeFormatter.Parameters");
65                         Assert.IsNull (keyex.Rng, "RSAOAEPKeyExchangeFormatter.Rng");
66                         Assert.AreEqual ("System.Security.Cryptography.RSAOAEPKeyExchangeFormatter", keyex.ToString ());
67                 }
68
69                 // ExchangeMin (1)
70                 [Test]
71                 public void ExchangeMin() 
72                 {
73                         AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
74                         byte[] M = { 0x01 };
75                         try {
76                                 byte[] EM = keyex.CreateKeyExchange (M);
77                                 AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
78                                 byte[] Mback = keyback.DecryptKeyExchange (EM);
79                                 AssertEquals ("RSAOAEPKeyExchangeFormatter Min", M, Mback);
80                         }
81                         catch (CryptographicException ce) {
82                                 // not supported by every version of Windows - Minimum: Windows XP
83                                 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
84                         }
85                 }
86
87                 // test with a message 128 bits (16 bytes) long
88                 [Test]
89                 public void Exchange128() 
90                 {
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 };
93                         try {
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);
98                         }
99                         catch (CryptographicException ce) {
100                                 // not supported by every version of Windows - Minimum: Windows XP
101                                 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
102                         }
103                 }
104
105                 // test with a message 160 bits (20 bytes) long
106                 [Test]
107                 public void Exchange192() 
108                 {
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 };
111                         try {
112                                 byte[] EM = keyex.CreateKeyExchange (M);
113                                 AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
114                                 byte[] Mback = keyback.DecryptKeyExchange (EM);
115                                 AssertEquals ("RSAOAEPKeyExchangeFormatter 192", M, Mback);
116                         }
117                         catch (CryptographicException ce) {
118                                 // not supported by every version of Windows - Minimum: Windows XP
119                                 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
120                         }
121                 }
122
123                 // Max = (key size in bytes) - 2 * (hash length) - 2
124                 [Test]
125                 public void ExchangeMax() 
126                 {
127                         AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
128                         // use SHA1 internaly
129                         byte[] M = new byte [(key.KeySize >> 3) - 2 * 20 - 2];
130                         try {
131                                 byte[] EM = keyex.CreateKeyExchange (M);
132                                 AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
133                                 byte[] Mback = keyback.DecryptKeyExchange (EM);
134                                 AssertEquals ("RSAOAEPKeyExchangeFormatter Max", M, Mback);
135                         }
136                         catch (CryptographicException ce) {
137                                 // not supported by every version of Windows - Minimum: Windows XP
138                                 Console.WriteLine (ce.Message + " (" + Environment.OSVersion.ToString () + ")");
139                         }
140                 }
141
142                 // TestExchangeTooBig
143                 [Test]
144                 [ExpectedException (typeof (CryptographicException))]
145                 public void ExchangeTooBig() 
146                 {
147                         AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
148                         byte[] M = new byte [(key.KeySize >> 3)- 10];
149                         byte[] EM = keyex.CreateKeyExchange (M);
150                 }
151
152                 [Test]
153                 public void Parameter () 
154                 {
155                         RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
156                         keyex.Parameter = new byte [1];
157                         Assert.AreEqual (1, keyex.Parameter.Length);
158                 }
159
160                 [Test]
161                 public void Rng () 
162                 {
163                         RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
164                         Assert.IsNull (keyex.Rng, "Rng");
165                         keyex.Rng = RandomNumberGenerator.Create ();
166                         Assert.IsNotNull (keyex.Rng, "Rng 2");
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
171                 public void ExchangeNoKey () 
172                 {
173                         AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter ();
174                         byte[] M = keyex.CreateKeyExchange (new byte [16]);
175                 }
176
177                 [Test]
178                 [ExpectedException (typeof (InvalidCastException))]
179                 public void ExchangeDSAKey () 
180                 {
181                         DSA dsa = DSA.Create ();
182                         AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (dsa);
183                 }
184         }
185 }