RSAPKCS1KeyExchangeFormatterTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // RSAPKCS1KeyExchangeFormatterTest.cs - NUnit Test Cases for RSAPKCS1KeyExchangeFormatter
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security.Cryptography;
  12. namespace MonoTests.System.Security.Cryptography
  13. {
  14. public class RSAPKCS1KeyExchangeFormatterTest : TestCase
  15. {
  16. protected static RSA key;
  17. protected override void SetUp ()
  18. {
  19. // generating a keypair is REALLY long and the framework
  20. // makes sure that we generate one (even if create an object
  21. // to import an exsting key)
  22. if (key == null) {
  23. key = RSA.Create ();
  24. key.ImportParameters (AllTests.GetRsaKey (true));
  25. }
  26. }
  27. protected override void TearDown () {}
  28. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  29. {
  30. AllTests.AssertEquals (msg, array1, array2);
  31. }
  32. public void TestProperties ()
  33. {
  34. RSAPKCS1KeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter ();
  35. keyex.SetKey (key);
  36. 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);
  37. // null (default)
  38. AssertNull("RSAPKCS1KeyExchangeFormatter.Rng", keyex.Rng);
  39. AssertEquals("RSAPKCS1KeyExchangeFormatter.ToString()", "System.Security.Cryptography.RSAPKCS1KeyExchangeFormatter", keyex.ToString ());
  40. }
  41. // TestExchangeMin (1)
  42. public void TestExchangeMin()
  43. {
  44. AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
  45. byte[] M = { 0x01 };
  46. byte[] EM = keyex.CreateKeyExchange (M);
  47. AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
  48. byte[] Mback = keyback.DecryptKeyExchange (EM);
  49. AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
  50. }
  51. // TestExchange64, 128, 192, 256
  52. // test with a message 128 bits (16 bytes) long
  53. public void TestExchange128()
  54. {
  55. AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
  56. byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
  57. byte[] EM = keyex.CreateKeyExchange (M);
  58. AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
  59. byte[] Mback = keyback.DecryptKeyExchange (EM);
  60. AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
  61. }
  62. // test with a message 160 bits (20 bytes) long
  63. public void TestExchange192()
  64. {
  65. AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
  66. byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49, 0x00, 0x00, 0x00, 0x00 };
  67. byte[] EM = keyex.CreateKeyExchange (M);
  68. AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
  69. byte[] Mback = keyback.DecryptKeyExchange (EM);
  70. AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
  71. }
  72. // Max = k - m - 11
  73. public void TestExchangeMax()
  74. {
  75. AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
  76. byte[] M = new byte [(key.KeySize >> 3)- 11];
  77. byte[] EM = keyex.CreateKeyExchange (M);
  78. AsymmetricKeyExchangeDeformatter keyback = new RSAPKCS1KeyExchangeDeformatter (key);
  79. byte[] Mback = keyback.DecryptKeyExchange (EM);
  80. AssertEquals ("RSAPKCS1KeyExchangeFormatter 1", M, Mback);
  81. }
  82. // TestExchangeTooBig
  83. public void TestExchangeTooBig()
  84. {
  85. AsymmetricKeyExchangeFormatter keyex = new RSAPKCS1KeyExchangeFormatter (key);
  86. byte[] M = new byte [(key.KeySize >> 3)- 10];
  87. try {
  88. byte[] EM = keyex.CreateKeyExchange (M);
  89. Fail ("Expected CryptographicException but got none");
  90. }
  91. catch (CryptographicException) {
  92. // this is what we expect
  93. }
  94. catch (Exception e) {
  95. Fail ("Expected CryptographicException but got : " + e.ToString ());
  96. }
  97. }
  98. }
  99. }