1 // 2 // RSAPKCS1KeyExchangeFormatterTest.cs - NUnit Test Cases for RSAPKCS1KeyExchangeFormatter 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 RSAPKCS1KeyExchangeFormatterTest { 38 39 protected static RSA key; 40 41 [SetUp] SetUp()42 public void SetUp () 43 { 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) 47 if (key == null) { 48 key = RSA.Create (); 49 key.ImportParameters (AllTests.GetRsaKey (true)); 50 } 51 } 52 AssertEquals(string msg, byte[] array1, byte[] array2)53 public void AssertEquals (string msg, byte[] array1, byte[] array2) 54 { 55 AllTests.AssertEquals (msg, array1, array2); 56 } 57 58 [Test] Properties()59 public void Properties () 60 { 61 RSAPKCS1KeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (); 62 keyex.SetKey (key); 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); 64 // null (default) 65 Assert.IsNull (keyex.Rng, "RSAPKCS1KeyExchangeFormatter.Rng"); 66 Assert.AreEqual("System.Security.Cryptography.RSAPKCS1KeyExchangeFormatter", keyex.ToString ()); 67 } 68 69 [Test] 70 [ExpectedException (typeof (ArgumentNullException))] KeyExchangeNull()71 public void KeyExchangeNull () 72 { 73 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key); 74 byte[] EM = keyex.CreateKeyExchange (null); 75 } 76 77 // TestExchangeMin (1) 78 [Test] KeyExchangeMin()79 public void KeyExchangeMin () 80 { 81 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key); 82 byte[] M = { 0x01 }; 83 byte[] EM = keyex.CreateKeyExchange (M); 84 85 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key); 86 byte[] Mback = keyback.DecryptKeyExchange (EM); 87 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback); 88 } 89 90 // test with a message 128 bits (16 bytes) long 91 [Test] KeyExchange128bits()92 public void KeyExchange128bits () 93 { 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)); 97 98 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key); 99 byte[] Mback = keyback.DecryptKeyExchange (EM); 100 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback); 101 } 102 103 // test with a message 160 bits (20 bytes) long 104 [Test] KeyExchange160bits()105 public void KeyExchange160bits () 106 { 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); 110 111 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key); 112 byte[] Mback = keyback.DecryptKeyExchange (EM); 113 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback); 114 } 115 116 // Max = k - m - 11 117 [Test] KeyExchangeMax()118 public void KeyExchangeMax() 119 { 120 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key); 121 byte[] M = new byte [(key.KeySize >> 3)- 11]; 122 byte[] EM = keyex.CreateKeyExchange (M); 123 124 AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key); 125 byte[] Mback = keyback.DecryptKeyExchange (EM); 126 AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback); 127 } 128 129 // TestExchangeTooBig 130 [Test] 131 [ExpectedException (typeof (CryptographicException))] KeyExchangeTooBig()132 public void KeyExchangeTooBig() 133 { 134 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key); 135 byte[] M = new byte [(key.KeySize >> 3)- 10]; 136 byte[] EM = keyex.CreateKeyExchange (M); 137 } 138 139 [Test] Rng()140 public void Rng () 141 { 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"); 146 } 147 148 [Test] 149 [ExpectedException (typeof (CryptographicUnexpectedOperationException))] ExchangeNoKey()150 public void ExchangeNoKey () 151 { 152 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (); 153 byte[] M = keyex.CreateKeyExchange (new byte [16]); 154 } 155 156 [Test] 157 [ExpectedException (typeof (InvalidCastException))] ExchangeDSAKey()158 public void ExchangeDSAKey () 159 { 160 DSA dsa = DSA.Create (); 161 AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (dsa); 162 } 163 164 [Test] 165 [ExpectedException (typeof (ArgumentNullException))] CreateWithNullKey()166 public void CreateWithNullKey () 167 { 168 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (null); 169 } 170 171 [Test] 172 [ExpectedException (typeof (ArgumentNullException))] CreateAndSetNullKey()173 public void CreateAndSetNullKey () 174 { 175 AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (); 176 keyex.SetKey (null); 177 } 178 } 179 180 } 181