RsaDigitalSignatureTest.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. using Renci.SshNet.Security;
  6. using Renci.SshNet.Security.Cryptography;
  7. using Renci.SshNet.Tests.Common;
  8. namespace Renci.SshNet.Tests.Classes.Security.Cryptography
  9. {
  10. /// <summary>
  11. /// Implements RSA digital signature algorithm.
  12. /// </summary>
  13. [TestClass]
  14. public class RsaDigitalSignatureTest : TestBase
  15. {
  16. [TestMethod]
  17. public void Sha1_SignAndVerify()
  18. {
  19. byte[] data = Encoding.UTF8.GetBytes("hello world");
  20. RsaKey rsaKey = GetRsaKey();
  21. var digitalSignature = new RsaDigitalSignature(rsaKey); // Verify SHA-1 is the default
  22. byte[] signedBytes = digitalSignature.Sign(data);
  23. byte[] expectedSignedBytes = new byte[]
  24. {
  25. // echo -n 'hello world' | openssl dgst -sha1 -sign Key.RSA.txt -out test.signed
  26. 0x41, 0x50, 0x12, 0x14, 0xd3, 0x7c, 0xe0, 0x40, 0x50, 0x65, 0xfb, 0x33, 0xd9, 0x17, 0x89, 0xbf,
  27. 0xb2, 0x4b, 0x85, 0x15, 0xbf, 0x9e, 0x57, 0x3b, 0x01, 0x15, 0x2b, 0x99, 0xfa, 0x62, 0x9b, 0x2a,
  28. 0x05, 0xa0, 0x73, 0xc7, 0xb7, 0x5b, 0xd9, 0x01, 0xaa, 0x56, 0x73, 0x95, 0x13, 0x41, 0x33, 0x0d,
  29. 0x7f, 0x83, 0x8a, 0x60, 0x4d, 0x19, 0xdc, 0x9b, 0xba, 0x8e, 0x61, 0xed, 0xd0, 0x8a, 0x3e, 0x38,
  30. 0x71, 0xee, 0x34, 0xc3, 0x55, 0x0f, 0x55, 0x65, 0x89, 0xbb, 0x3e, 0x41, 0xee, 0xdf, 0xf5, 0x2f,
  31. 0xab, 0x9e, 0x89, 0x37, 0x68, 0x1f, 0x9f, 0x38, 0x00, 0x81, 0x29, 0x93, 0xeb, 0x61, 0x37, 0xad,
  32. 0x8d, 0x35, 0xf1, 0x3d, 0x4b, 0x9b, 0x99, 0x74, 0x7b, 0xeb, 0xf4, 0xfb, 0x76, 0xb4, 0xb6, 0xb4,
  33. 0x09, 0x33, 0x5c, 0xfa, 0x6a, 0xad, 0x1e, 0xed, 0x1c, 0xe1, 0xb4, 0x4d, 0xf2, 0xa5, 0xc3, 0x64,
  34. 0x9a, 0x45, 0x81, 0xee, 0x1b, 0xa6, 0x1d, 0x01, 0x3c, 0x4d, 0xb5, 0x62, 0x9e, 0xff, 0x8e, 0xff,
  35. 0x6c, 0x18, 0xed, 0xe9, 0x8e, 0x03, 0x2c, 0xc5, 0x94, 0x81, 0xca, 0x8b, 0x18, 0x3f, 0x25, 0xcd,
  36. 0xe5, 0x42, 0x49, 0x43, 0x23, 0x1f, 0xdc, 0x3f, 0xa2, 0x43, 0xbc, 0xbd, 0x42, 0xf5, 0x60, 0xfb,
  37. 0x01, 0xd3, 0x67, 0x0d, 0x8d, 0x85, 0x7b, 0x51, 0x14, 0xec, 0x26, 0x53, 0x00, 0x61, 0x25, 0x16,
  38. 0x19, 0x10, 0x3c, 0x86, 0x16, 0x59, 0x84, 0x08, 0xd1, 0xf9, 0x1e, 0x05, 0x88, 0xbd, 0x4a, 0x01,
  39. 0x43, 0x4e, 0xec, 0x76, 0x0b, 0xd7, 0x2c, 0xe9, 0x98, 0xb1, 0x4c, 0x0a, 0x13, 0xc6, 0x95, 0xf9,
  40. 0x8f, 0x95, 0x5c, 0x98, 0x4c, 0x8f, 0x97, 0x4a, 0xad, 0x0d, 0xfe, 0x84, 0xf0, 0x56, 0xc3, 0x29,
  41. 0x73, 0x75, 0x55, 0x3c, 0xd9, 0x5e, 0x5b, 0x6f, 0xf9, 0x81, 0xbc, 0xbc, 0x50, 0x75, 0x7d, 0xa8
  42. };
  43. CollectionAssert.AreEqual(expectedSignedBytes, signedBytes);
  44. // Also verify RsaKey uses SHA-1 by default
  45. CollectionAssert.AreEqual(expectedSignedBytes, rsaKey.Sign(data));
  46. // The following fails due to the _isPrivate decision in RsaCipher.Transform. Is that really correct?
  47. //Assert.IsTrue(digitalSignature.Verify(data, signedBytes));
  48. // 'Workaround': use a key with no private key information
  49. var digitalSignaturePublic = new RsaDigitalSignature(new RsaKey()
  50. {
  51. Public = rsaKey.Public
  52. });
  53. Assert.IsTrue(digitalSignaturePublic.Verify(data, signedBytes));
  54. }
  55. [TestMethod]
  56. public void Sha256_SignAndVerify()
  57. {
  58. byte[] data = Encoding.UTF8.GetBytes("hello world");
  59. RsaKey rsaKey = GetRsaKey();
  60. var digitalSignature = new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA256);
  61. byte[] signedBytes = digitalSignature.Sign(data);
  62. CollectionAssert.AreEqual(new byte[]
  63. {
  64. // echo -n 'hello world' | openssl dgst -sha256 -sign Key.RSA.txt -out test.signed
  65. 0x2e, 0xef, 0x01, 0x49, 0x5c, 0x66, 0x37, 0x56, 0xc2, 0xfb, 0x7b, 0xfa, 0x80, 0x2f, 0xdb, 0xaa,
  66. 0x0d, 0x15, 0xd9, 0x8d, 0xa9, 0xad, 0x81, 0x4f, 0x09, 0x2e, 0x53, 0x9e, 0xce, 0x5d, 0x68, 0x07,
  67. 0xae, 0xb9, 0xc0, 0x45, 0xfa, 0x30, 0xd0, 0xf7, 0xd6, 0xa6, 0x8d, 0x19, 0x24, 0x3a, 0xea, 0x91,
  68. 0x3e, 0xa2, 0x4a, 0x42, 0x2e, 0x21, 0xf1, 0x48, 0x57, 0xca, 0x2b, 0x6c, 0x9f, 0x79, 0x54, 0x91,
  69. 0x3e, 0x3a, 0x4d, 0xd1, 0x70, 0x87, 0x3d, 0xbe, 0x22, 0x97, 0xc9, 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x05, 0xc7, 0xed, 0xc7, 0x52, 0x4a
  81. }, signedBytes);
  82. // The following fails due to the _isPrivate decision in RsaCipher.Transform. Is that really correct?
  83. //Assert.IsTrue(digitalSignature.Verify(data, signedBytes));
  84. // 'Workaround': use a key with no private key information
  85. var digitalSignaturePublic = new RsaDigitalSignature(new RsaKey()
  86. {
  87. Public = rsaKey.Public
  88. }, HashAlgorithmName.SHA256);
  89. Assert.IsTrue(digitalSignaturePublic.Verify(data, signedBytes));
  90. }
  91. [TestMethod]
  92. public void Sha512_SignAndVerify()
  93. {
  94. byte[] data = Encoding.UTF8.GetBytes("hello world");
  95. RsaKey rsaKey = GetRsaKey();
  96. var digitalSignature = new RsaDigitalSignature(rsaKey, HashAlgorithmName.SHA512);
  97. byte[] signedBytes = digitalSignature.Sign(data);
  98. CollectionAssert.AreEqual(new byte[]
  99. {
  100. // echo -n 'hello world' | openssl dgst -sha512 -sign Key.RSA.txt -out test.signed
  101. 0x69, 0x70, 0xb5, 0x9f, 0x32, 0x86, 0x3b, 0xae, 0xc0, 0x79, 0x6e, 0xdb, 0x35, 0xd5, 0xa6, 0x22,
  102. 0xcd, 0x2b, 0x4b, 0xd2, 0x68, 0x1a, 0x65, 0x41, 0xa6, 0xd9, 0x20, 0x54, 0x31, 0x9a, 0xb1, 0x44,
  103. 0x6e, 0x8f, 0x56, 0x4b, 0xfc, 0x27, 0x7f, 0x3f, 0xe7, 0x47, 0xcb, 0x78, 0x03, 0x05, 0x79, 0x8a,
  104. 0x16, 0x7b, 0x12, 0x01, 0x3a, 0xa2, 0xd5, 0x0d, 0x2b, 0x16, 0x38, 0xef, 0x84, 0x6b, 0xd7, 0x19,
  105. 0xeb, 0xac, 0x54, 0x01, 0x9d, 0xa6, 0x80, 0x74, 0x43, 0xa8, 0x6e, 0x5e, 0x33, 0x05, 0x06, 0x1d,
  106. 0x6d, 0xfe, 0x32, 0x4f, 0xe3, 0xcb, 0x3e, 0x2d, 0x4e, 0xe1, 0x47, 0x03, 0x69, 0xb4, 0x59, 0x80,
  107. 0x59, 0x05, 0x15, 0xa0, 0x11, 0x34, 0x47, 0x58, 0xd7, 0x93, 0x2d, 0x40, 0xf2, 0x2c, 0x37, 0x48,
  108. 0x6b, 0x3c, 0xd3, 0x03, 0x09, 0x32, 0x74, 0xa0, 0x2d, 0x33, 0x11, 0x99, 0x10, 0xb4, 0x09, 0x31,
  109. 0xec, 0xa3, 0x2c, 0x63, 0xba, 0x50, 0xd1, 0x02, 0x45, 0xae, 0xb5, 0x75, 0x7e, 0xfa, 0xfc, 0x06,
  110. 0xb6, 0x6a, 0xb2, 0xa1, 0x73, 0x14, 0xa5, 0xaa, 0x17, 0x88, 0x03, 0x19, 0x14, 0x9b, 0xe1, 0x10,
  111. 0xf8, 0x2f, 0x73, 0x01, 0xc7, 0x8d, 0x37, 0xef, 0x98, 0x69, 0xc2, 0xe2, 0x7a, 0x11, 0xd5, 0xb8,
  112. 0xc9, 0x35, 0x45, 0xcb, 0x56, 0x4b, 0x92, 0x4a, 0xe0, 0x4c, 0xd6, 0x82, 0xae, 0xad, 0x5b, 0xe9,
  113. 0x40, 0x7e, 0x2a, 0x48, 0x7d, 0x57, 0xc5, 0xfd, 0xe9, 0x98, 0xe0, 0xbb, 0x09, 0xa1, 0xf5, 0x48,
  114. 0x45, 0xcb, 0xee, 0xb9, 0x99, 0x81, 0x44, 0x15, 0x2e, 0x50, 0x39, 0x64, 0x58, 0x4c, 0x34, 0x86,
  115. 0xf8, 0x81, 0x9e, 0x1d, 0xb6, 0x97, 0xe0, 0xce, 0x16, 0xca, 0x20, 0x46, 0xe9, 0x49, 0x8f, 0xe6,
  116. 0xa0, 0x23, 0x08, 0x80, 0xa6, 0x37, 0x70, 0x06, 0xcc, 0x8f, 0xf4, 0xa0, 0x74, 0x53, 0x26, 0x38
  117. }, signedBytes);
  118. // The following fails due to the _isPrivate decision in RsaCipher.Transform. Is that really correct?
  119. //Assert.IsTrue(digitalSignature.Verify(data, signedBytes));
  120. // 'Workaround': use a key with no private key information
  121. var digitalSignaturePublic = new RsaDigitalSignature(new RsaKey()
  122. {
  123. Public = rsaKey.Public
  124. }, HashAlgorithmName.SHA512);
  125. Assert.IsTrue(digitalSignaturePublic.Verify(data, signedBytes));
  126. }
  127. [TestMethod]
  128. public void Constructor_InvalidHashAlgorithm_ThrowsArgumentException()
  129. {
  130. ArgumentException exception = Assert.ThrowsException<ArgumentException>(
  131. () => new RsaDigitalSignature(new RsaKey(), new HashAlgorithmName("invalid")));
  132. Assert.AreEqual("hashAlgorithmName", exception.ParamName);
  133. }
  134. private static RsaKey GetRsaKey()
  135. {
  136. using (var stream = GetData("Key.RSA.txt"))
  137. {
  138. return (RsaKey) new PrivateKeyFile(stream).Key;
  139. }
  140. }
  141. }
  142. }