RSATest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // RSATest.cs - NUnit Test Cases for RSA
  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 NonAbstractRSAForUnitTests : RSA {
  14. protected RSAParameters rsaParams;
  15. // not tested here - but we must implemented all abstract properties
  16. public override string KeyExchangeAlgorithm {
  17. get { return null; }
  18. }
  19. // not tested here - but we must implemented all abstract properties
  20. public override string SignatureAlgorithm {
  21. get { return null; }
  22. }
  23. // not tested here - but we must implemented all abstract methods
  24. public override byte[] DecryptValue (byte[] rgb)
  25. {
  26. return null;
  27. }
  28. // not tested here - but we must implemented all abstract methods
  29. public override byte[] EncryptValue (byte[] rgb) {
  30. return null;
  31. }
  32. // basic implementation for tests
  33. public override RSAParameters ExportParameters (bool includePrivateParameters)
  34. {
  35. if (includePrivateParameters)
  36. return rsaParams;
  37. else {
  38. RSAParameters rsaPublicParams = rsaParams;
  39. rsaPublicParams.D = null;
  40. rsaPublicParams.DP = null;
  41. rsaPublicParams.DQ = null;
  42. rsaPublicParams.P = null;
  43. rsaPublicParams.Q = null;
  44. rsaPublicParams.InverseQ = null;
  45. return rsaPublicParams;
  46. }
  47. }
  48. // basic implementation for tests
  49. public override void ImportParameters (RSAParameters parameters)
  50. {
  51. rsaParams = parameters;
  52. }
  53. // not tested here - but we must implemented all abstract methods
  54. protected override void Dispose (bool disposing) {}
  55. }
  56. public class RSATest : TestCase {
  57. protected RSA rsa;
  58. static byte[] rsaModulus = { 0xbb, 0xf8, 0x2f, 0x09, 0x06, 0x82, 0xce, 0x9c, 0x23, 0x38, 0xac, 0x2b, 0x9d, 0xa8, 0x71, 0xf7,
  59. 0x36, 0x8d, 0x07, 0xee, 0xd4, 0x10, 0x43, 0xa4, 0x40, 0xd6, 0xb6, 0xf0, 0x74, 0x54, 0xf5, 0x1f,
  60. 0xb8, 0xdf, 0xba, 0xaf, 0x03, 0x5c, 0x02, 0xab, 0x61, 0xea, 0x48, 0xce, 0xeb, 0x6f, 0xcd, 0x48,
  61. 0x76, 0xed, 0x52, 0x0d, 0x60, 0xe1, 0xec, 0x46, 0x19, 0x71, 0x9d, 0x8a, 0x5b, 0x8b, 0x80, 0x7f,
  62. 0xaf, 0xb8, 0xe0, 0xa3, 0xdf, 0xc7, 0x37, 0x72, 0x3e, 0xe6, 0xb4, 0xb7, 0xd9, 0x3a, 0x25, 0x84,
  63. 0xee, 0x6a, 0x64, 0x9d, 0x06, 0x09, 0x53, 0x74, 0x88, 0x34, 0xb2, 0x45, 0x45, 0x98, 0x39, 0x4e,
  64. 0xe0, 0xaa, 0xb1, 0x2d, 0x7b, 0x61, 0xa5, 0x1f, 0x52, 0x7a, 0x9a, 0x41, 0xf6, 0xc1, 0x68, 0x7f,
  65. 0xe2, 0x53, 0x72, 0x98, 0xca, 0x2a, 0x8f, 0x59, 0x46, 0xf8, 0xe5, 0xfd, 0x09, 0x1d, 0xbd, 0xcb };
  66. static byte[] rsaExponent = { 0x11 };
  67. static byte[] rsaP = { 0xee, 0xcf, 0xae, 0x81, 0xb1, 0xb9, 0xb3, 0xc9, 0x08, 0x81, 0x0b, 0x10, 0xa1, 0xb5, 0x60, 0x01,
  68. 0x99, 0xeb, 0x9f, 0x44, 0xae, 0xf4, 0xfd, 0xa4, 0x93, 0xb8, 0x1a, 0x9e, 0x3d, 0x84, 0xf6, 0x32,
  69. 0x12, 0x4e, 0xf0, 0x23, 0x6e, 0x5d, 0x1e, 0x3b, 0x7e, 0x28, 0xfa, 0xe7, 0xaa, 0x04, 0x0a, 0x2d,
  70. 0x5b, 0x25, 0x21, 0x76, 0x45, 0x9d, 0x1f, 0x39, 0x75, 0x41, 0xba, 0x2a, 0x58, 0xfb, 0x65, 0x99 };
  71. static byte[] rsaQ = { 0xc9, 0x7f, 0xb1, 0xf0, 0x27, 0xf4, 0x53, 0xf6, 0x34, 0x12, 0x33, 0xea, 0xaa, 0xd1, 0xd9, 0x35,
  72. 0x3f, 0x6c, 0x42, 0xd0, 0x88, 0x66, 0xb1, 0xd0, 0x5a, 0x0f, 0x20, 0x35, 0x02, 0x8b, 0x9d, 0x86,
  73. 0x98, 0x40, 0xb4, 0x16, 0x66, 0xb4, 0x2e, 0x92, 0xea, 0x0d, 0xa3, 0xb4, 0x32, 0x04, 0xb5, 0xcf,
  74. 0xce, 0x33, 0x52, 0x52, 0x4d, 0x04, 0x16, 0xa5, 0xa4, 0x41, 0xe7, 0x00, 0xaf, 0x46, 0x15, 0x03 };
  75. static byte[] rsaDP = { 0x54, 0x49, 0x4c, 0xa6, 0x3e, 0xba, 0x03, 0x37, 0xe4, 0xe2, 0x40, 0x23, 0xfc, 0xd6, 0x9a, 0x5a,
  76. 0xeb, 0x07, 0xdd, 0xdc, 0x01, 0x83, 0xa4, 0xd0, 0xac, 0x9b, 0x54, 0xb0, 0x51, 0xf2, 0xb1, 0x3e,
  77. 0xd9, 0x49, 0x09, 0x75, 0xea, 0xb7, 0x74, 0x14, 0xff, 0x59, 0xc1, 0xf7, 0x69, 0x2e, 0x9a, 0x2e,
  78. 0x20, 0x2b, 0x38, 0xfc, 0x91, 0x0a, 0x47, 0x41, 0x74, 0xad, 0xc9, 0x3c, 0x1f, 0x67, 0xc9, 0x81 };
  79. static byte[] rsaDQ = { 0x47, 0x1e, 0x02, 0x90, 0xff, 0x0a, 0xf0, 0x75, 0x03, 0x51, 0xb7, 0xf8, 0x78, 0x86, 0x4c, 0xa9,
  80. 0x61, 0xad, 0xbd, 0x3a, 0x8a, 0x7e, 0x99, 0x1c, 0x5c, 0x05, 0x56, 0xa9, 0x4c, 0x31, 0x46, 0xa7,
  81. 0xf9, 0x80, 0x3f, 0x8f, 0x6f, 0x8a, 0xe3, 0x42, 0xe9, 0x31, 0xfd, 0x8a, 0xe4, 0x7a, 0x22, 0x0d,
  82. 0x1b, 0x99, 0xa4, 0x95, 0x84, 0x98, 0x07, 0xfe, 0x39, 0xf9, 0x24, 0x5a, 0x98, 0x36, 0xda, 0x3d };
  83. static byte[] rsaInverseQ = { 0xb0, 0x6c, 0x4f, 0xda, 0xbb, 0x63, 0x01, 0x19, 0x8d, 0x26, 0x5b, 0xdb, 0xae, 0x94, 0x23, 0xb3,
  84. 0x80, 0xf2, 0x71, 0xf7, 0x34, 0x53, 0x88, 0x50, 0x93, 0x07, 0x7f, 0xcd, 0x39, 0xe2, 0x11, 0x9f,
  85. 0xc9, 0x86, 0x32, 0x15, 0x4f, 0x58, 0x83, 0xb1, 0x67, 0xa9, 0x67, 0xbf, 0x40, 0x2b, 0x4e, 0x9e,
  86. 0x2e, 0x0f, 0x96, 0x56, 0xe6, 0x98, 0xea, 0x36, 0x66, 0xed, 0xfb, 0x25, 0x79, 0x80, 0x39, 0xf7 };
  87. static byte[] rsaD = { 0xa5, 0xda, 0xfc, 0x53, 0x41, 0xfa, 0xf2, 0x89, 0xc4, 0xb9, 0x88, 0xdb, 0x30, 0xc1, 0xcd, 0xf8,
  88. 0x3f, 0x31, 0x25, 0x1e, 0x06, 0x68, 0xb4, 0x27, 0x84, 0x81, 0x38, 0x01, 0x57, 0x96, 0x41, 0xb2,
  89. 0x94, 0x10, 0xb3, 0xc7, 0x99, 0x8d, 0x6b, 0xc4, 0x65, 0x74, 0x5e, 0x5c, 0x39, 0x26, 0x69, 0xd6,
  90. 0x87, 0x0d, 0xa2, 0xc0, 0x82, 0xa9, 0x39, 0xe3, 0x7f, 0xdc, 0xb8, 0x2e, 0xc9, 0x3e, 0xda, 0xc9,
  91. 0x7f, 0xf3, 0xad, 0x59, 0x50, 0xac, 0xcf, 0xbc, 0x11, 0x1c, 0x76, 0xf1, 0xa9, 0x52, 0x94, 0x44,
  92. 0xe5, 0x6a, 0xaf, 0x68, 0xc5, 0x6c, 0x09, 0x2c, 0xd3, 0x8d, 0xc3, 0xbe, 0xf5, 0xd2, 0x0a, 0x93,
  93. 0x99, 0x26, 0xed, 0x4f, 0x74, 0xa1, 0x3e, 0xdd, 0xfb, 0xe1, 0xa1, 0xce, 0xcc, 0x48, 0x94, 0xaf,
  94. 0x94, 0x28, 0xc2, 0xb7, 0xb8, 0x88, 0x3f, 0xe4, 0x46, 0x3a, 0x4b, 0xc8, 0x5b, 0x1c, 0xb3, 0xc1 };
  95. static string xmlPrivate = "<RSAKeyValue><Modulus>u/gvCQaCzpwjOKwrnahx9zaNB+7UEEOkQNa28HRU9R+437qvA1wCq2HqSM7rb81Idu1SDWDh7EYZcZ2KW4uAf6+44KPfxzdyPua0t9k6JYTuamSdBglTdIg0skVFmDlO4KqxLXthpR9SeppB9sFof+JTcpjKKo9ZRvjl/Qkdvcs=</Modulus><Exponent>EQ==</Exponent><P>7s+ugbG5s8kIgQsQobVgAZnrn0Su9P2kk7ganj2E9jISTvAjbl0eO34o+ueqBAotWyUhdkWdHzl1QboqWPtlmQ==</P><Q>yX+x8Cf0U/Y0EjPqqtHZNT9sQtCIZrHQWg8gNQKLnYaYQLQWZrQukuoNo7QyBLXPzjNSUk0EFqWkQecAr0YVAw==</Q><DP>VElMpj66Azfk4kAj/NaaWusH3dwBg6TQrJtUsFHysT7ZSQl16rd0FP9ZwfdpLpouICs4/JEKR0F0rck8H2fJgQ==</DP><DQ>Rx4CkP8K8HUDUbf4eIZMqWGtvTqKfpkcXAVWqUwxRqf5gD+Pb4rjQukx/YrkeiING5mklYSYB/45+SRamDbaPQ==</DQ><InverseQ>sGxP2rtjARmNJlvbrpQjs4Dycfc0U4hQkwd/zTniEZ/JhjIVT1iDsWepZ79AK06eLg+WVuaY6jZm7fsleYA59w==</InverseQ><D>pdr8U0H68onEuYjbMMHN+D8xJR4GaLQnhIE4AVeWQbKUELPHmY1rxGV0Xlw5JmnWhw2iwIKpOeN/3LguyT7ayX/zrVlQrM+8ERx28alSlETlaq9oxWwJLNONw7710gqTmSbtT3ShPt374aHOzEiUr5Qowre4iD/kRjpLyFscs8E=</D></RSAKeyValue>";
  96. static string xmlPublic = "<RSAKeyValue><Modulus>u/gvCQaCzpwjOKwrnahx9zaNB+7UEEOkQNa28HRU9R+437qvA1wCq2HqSM7rb81Idu1SDWDh7EYZcZ2KW4uAf6+44KPfxzdyPua0t9k6JYTuamSdBglTdIg0skVFmDlO4KqxLXthpR9SeppB9sFof+JTcpjKKo9ZRvjl/Qkdvcs=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue>";
  97. protected override void SetUp ()
  98. {
  99. rsa = new NonAbstractRSAForUnitTests ();
  100. }
  101. protected override void TearDown () {}
  102. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  103. {
  104. AllTests.AssertEquals (msg, array1, array2);
  105. }
  106. // may also help for RSA descendants
  107. public void AssertEquals (string message, RSAParameters expectedKey, RSAParameters actualKey, bool checkPrivateKey)
  108. {
  109. if (checkPrivateKey) {
  110. AssertEquals( message + " D", expectedKey.D, actualKey.D );
  111. AssertEquals( message + " DP", expectedKey.DP, actualKey.DP );
  112. AssertEquals( message + " DQ", expectedKey.DQ, actualKey.DQ );
  113. AssertEquals( message + " P", expectedKey.P, actualKey.P );
  114. AssertEquals( message + " Q", expectedKey.Q, actualKey.Q );
  115. AssertEquals( message + " InverseQ", expectedKey.InverseQ, actualKey.InverseQ );
  116. }
  117. AssertEquals( message + " Modulus", expectedKey.Modulus, actualKey.Modulus );
  118. AssertEquals( message + " Exponent", expectedKey.Exponent, actualKey.Exponent );
  119. }
  120. public RSAParameters GetKey (bool includePrivateKey)
  121. {
  122. RSAParameters p = new RSAParameters();
  123. if (includePrivateKey) {
  124. p.D = rsaD;
  125. p.DP = rsaDP;
  126. p.DQ = rsaDQ;
  127. p.P = rsaP;
  128. p.Q = rsaQ;
  129. p.InverseQ = rsaInverseQ;
  130. }
  131. else {
  132. p.D = null;
  133. p.DP = null;
  134. p.DQ = null;
  135. p.P = null;
  136. p.Q = null;
  137. p.InverseQ = null;
  138. }
  139. p.Modulus = rsaModulus;
  140. p.Exponent = rsaExponent;
  141. return p;
  142. }
  143. // importing RSA keypair and exporting a RSA keypair
  144. public void TestRSAImportPrivateExportPrivate()
  145. {
  146. RSAParameters input = GetKey (true);
  147. rsa.ImportParameters (input);
  148. string xmlRSA = rsa.ToXmlString (true);
  149. rsa.FromXmlString (xmlRSA);
  150. AssertEquals ("RSA Import Private Export Private (xml)", xmlPrivate, xmlRSA);
  151. RSAParameters output = rsa.ExportParameters (true);
  152. AssertEquals ("RSA Import Private Export Private (binary)", input, output, true);
  153. }
  154. // importing RSA keypair and exporting a RSA public key
  155. public void TestRSAImportPrivateExportPublic()
  156. {
  157. RSAParameters input = GetKey (true);
  158. rsa.ImportParameters (input);
  159. string xmlRSA = rsa.ToXmlString (false);
  160. rsa.FromXmlString (xmlRSA);
  161. AssertEquals ("RSA Import Private Export Public (xml)", xmlPublic, xmlRSA);
  162. RSAParameters output = rsa.ExportParameters (false);
  163. AssertEquals ("RSA Import Private Export Public (binary)", input, output, false);
  164. }
  165. // importing RSA public key and exporting a RSA keypair (including private key!)
  166. public void TestRSAImportPublicExportPrivate()
  167. {
  168. RSAParameters input = GetKey (false);
  169. rsa.ImportParameters (input);
  170. string xmlRSA = null;
  171. try {
  172. xmlRSA = rsa.ToXmlString (true);
  173. Fail ("Expected ArgumentNullException but got no exception");
  174. }
  175. catch (ArgumentNullException) {
  176. // expected as we cannot get a private key
  177. // from a public key (at least not in our lifetime)
  178. }
  179. catch (Exception e) {
  180. Fail ("Expected ArgumentNullException but got " + e.ToString ());
  181. }
  182. //rsa.FromXmlString (xmlRSA);
  183. //RSAParameters output = rsa.ExportParameters (true);
  184. //AssertEquals ("RSA Import Public Export Private", input, output, true);
  185. }
  186. // importing RSA public key and exporting a RSA public key
  187. public void TestRSAImportPublicExportPublic()
  188. {
  189. RSAParameters input = GetKey (false);
  190. rsa.ImportParameters (input);
  191. string xmlRSA = rsa.ToXmlString (false);
  192. rsa.FromXmlString (xmlRSA);
  193. AssertEquals ("RSA Import Public Export Public (xml)", xmlPublic, xmlRSA);
  194. RSAParameters output = rsa.ExportParameters (false);
  195. AssertEquals ("RSA Import Public Export Public (binary)", input, output, true);
  196. }
  197. }
  198. }