RSAOAEPKeyExchangeFormatterTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // RSAOAEPKeyExchangeFormatterTest.cs - NUnit Test Cases for RSAOAEPKeyExchangeFormatter
  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. public class RSAOAEPKeyExchangeFormatterTest : TestCase {
  14. protected static RSA key;
  15. protected override void SetUp ()
  16. {
  17. // generating a keypair is REALLY long and the framework
  18. // makes sure that we generate one (even if create an object
  19. // to import an exsting key)
  20. if (key == null) {
  21. key = RSA.Create ();
  22. key.ImportParameters (AllTests.GetRsaKey (true));
  23. }
  24. }
  25. protected override void TearDown () {}
  26. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  27. {
  28. AllTests.AssertEquals (msg, array1, array2);
  29. }
  30. public void TestProperties ()
  31. {
  32. RSAOAEPKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter ();
  33. keyex.SetKey (key);
  34. AssertEquals("RSAOAEPKeyExchangeFormatter.Parameters", null, keyex.Parameters);
  35. // null (default)
  36. AssertNull("RSAOAEPKeyExchangeFormatter.Rng", keyex.Rng);
  37. AssertEquals("RSAOAEPKeyExchangeFormatter.ToString()", "System.Security.Cryptography.RSAOAEPKeyExchangeFormatter", keyex.ToString ());
  38. }
  39. // TestExchangeMin (1)
  40. public void TestExchangeMin()
  41. {
  42. AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
  43. byte[] M = { 0x01 };
  44. try {
  45. byte[] EM = keyex.CreateKeyExchange (M);
  46. AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
  47. byte[] Mback = keyback.DecryptKeyExchange (EM);
  48. AssertEquals ("RSAOAEPKeyExchangeFormatter Min", M, Mback);
  49. }
  50. catch (CryptographicException) {
  51. // not supported by every version of Windows
  52. // Minimum: Windows 2000 + High Encryption Pack
  53. }
  54. }
  55. // test with a message 128 bits (16 bytes) long
  56. public void TestExchange128()
  57. {
  58. AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
  59. byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
  60. try {
  61. byte[] EM = keyex.CreateKeyExchange (M);
  62. AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
  63. byte[] Mback = keyback.DecryptKeyExchange (EM);
  64. AssertEquals ("RSAOAEPKeyExchangeFormatter 128", M, Mback);
  65. }
  66. catch (CryptographicException) {
  67. // not supported by every version of Windows
  68. // Minimum: Windows 2000 + High Encryption Pack
  69. }
  70. }
  71. // test with a message 160 bits (20 bytes) long
  72. public void TestExchange192()
  73. {
  74. AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
  75. byte[] M = { 0xd4, 0x36, 0xe9, 0x95, 0x69, 0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49, 0x00, 0x00, 0x00, 0x00 };
  76. try {
  77. byte[] EM = keyex.CreateKeyExchange (M);
  78. AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
  79. byte[] Mback = keyback.DecryptKeyExchange (EM);
  80. AssertEquals ("RSAOAEPKeyExchangeFormatter 192", M, Mback);
  81. }
  82. catch (CryptographicException) {
  83. // not supported by every version of Windows
  84. // Minimum: Windows 2000 + High Encryption Pack
  85. }
  86. }
  87. // Max = (key size in bytes) - 2 * (hash length) - 2
  88. public void TestExchangeMax()
  89. {
  90. AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
  91. // use SHA1 internaly
  92. byte[] M = new byte [(key.KeySize >> 3) - 2 * 20 - 2];
  93. try {
  94. byte[] EM = keyex.CreateKeyExchange (M);
  95. AsymmetricKeyExchangeDeformatter keyback = new RSAOAEPKeyExchangeDeformatter (key);
  96. byte[] Mback = keyback.DecryptKeyExchange (EM);
  97. AssertEquals ("RSAOAEPKeyExchangeFormatter Max", M, Mback);
  98. }
  99. catch (CryptographicException) {
  100. // not supported by every version of Windows
  101. // Minimum: Windows 2000 + High Encryption Pack
  102. }
  103. }
  104. // TestExchangeTooBig
  105. public void TestExchangeTooBig()
  106. {
  107. AsymmetricKeyExchangeFormatter keyex = new RSAOAEPKeyExchangeFormatter (key);
  108. byte[] M = new byte [(key.KeySize >> 3)- 10];
  109. try {
  110. byte[] EM = keyex.CreateKeyExchange (M);
  111. Fail ("Expected CryptographicException but got none");
  112. }
  113. catch (CryptographicException) {
  114. // this is what we expect
  115. }
  116. catch (Exception e) {
  117. Fail ("Expected CryptographicException but got : " + e.ToString ());
  118. }
  119. }
  120. }
  121. }