AesCipherTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Security.Cryptography.Ciphers;
  3. using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
  4. using Renci.SshNet.Tests.Common;
  5. using Renci.SshNet.Tests.Properties;
  6. using System.Linq;
  7. namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. [TestClass]
  13. public class AesCipherTest : TestBase
  14. {
  15. [TestMethod]
  16. public void Test_Cipher_AES_128_CBC()
  17. {
  18. var input = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0x30, 0x9e, 0xe0, 0x9c, 0x12, 0xee, 0x3a, 0x30, 0x03, 0x52, 0x1c, 0x1a, 0xe7, 0x3e, 0x0b, 0x9a, 0xcf, 0x9a, 0x57, 0x42, 0x0b, 0x4f, 0x4a, 0x15, 0xa0, 0xf5 };
  19. var key = new byte[] { 0xe4, 0x94, 0xf9, 0xb1, 0x00, 0x4f, 0x16, 0x2a, 0x80, 0x11, 0xea, 0x73, 0x0d, 0xb9, 0xbf, 0x64 };
  20. var iv = new byte[] { 0x74, 0x8b, 0x4f, 0xe6, 0xc1, 0x29, 0xb3, 0x54, 0xec, 0x77, 0x92, 0xf3, 0x15, 0xa0, 0x41, 0xa8 };
  21. var output = new byte[] { 0x19, 0x7f, 0x80, 0xd8, 0xc9, 0x89, 0xc4, 0xa7, 0xc6, 0xc6, 0x3f, 0x9f, 0x1e, 0x00, 0x1f, 0x72, 0xa7, 0x5e, 0xde, 0x40, 0x88, 0xa2, 0x72, 0xf2, 0xed, 0x3f, 0x81, 0x45, 0xb6, 0xbd, 0x45, 0x87, 0x15, 0xa5, 0x10, 0x92, 0x4a, 0x37, 0x9e, 0xa9, 0x80, 0x1c, 0x14, 0x83, 0xa3, 0x39, 0x45, 0x28 };
  22. var testCipher = new Renci.SshNet.Security.Cryptography.Ciphers.AesCipher(key, new Renci.SshNet.Security.Cryptography.Ciphers.Modes.CbcCipherMode(iv), null);
  23. var r = testCipher.Encrypt(input);
  24. if (!r.SequenceEqual(output))
  25. Assert.Fail("Invalid encryption");
  26. }
  27. [TestMethod]
  28. public void Test_Cipher_AES_128_CTR()
  29. {
  30. var input = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0xb0, 0x74, 0x21, 0x87, 0x16, 0xb9, 0x69, 0x48, 0x33, 0xce, 0xb3, 0xe7, 0xdc, 0x3f, 0x50, 0xdc, 0xcc, 0xd5, 0x27, 0xb7, 0xfe, 0x7a, 0x78, 0x22, 0xae, 0xc8 };
  31. var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
  32. var iv = new byte[] { 0xe6, 0x65, 0x36, 0x0d, 0xdd, 0xd7, 0x50, 0xc3, 0x48, 0xdb, 0x48, 0x07, 0xa1, 0x30, 0xd2, 0x38 };
  33. var output = new byte[] { 0xca, 0xfb, 0x1c, 0x49, 0xbf, 0x82, 0x2a, 0xbb, 0x1c, 0x52, 0xc7, 0x86, 0x22, 0x8a, 0xe5, 0xa4, 0xf3, 0xda, 0x4e, 0x1c, 0x3a, 0x87, 0x41, 0x1c, 0xd2, 0x6e, 0x76, 0xdc, 0xc2, 0xe9, 0xc2, 0x0e, 0xf5, 0xc7, 0xbd, 0x12, 0x85, 0xfa, 0x0e, 0xda, 0xee, 0x50, 0xd7, 0xfd, 0x81, 0x34, 0x25, 0x6d };
  34. var testCipher = new Renci.SshNet.Security.Cryptography.Ciphers.AesCipher(key, new Renci.SshNet.Security.Cryptography.Ciphers.Modes.CtrCipherMode(iv), null);
  35. var r = testCipher.Encrypt(input);
  36. if (!r.SequenceEqual(output))
  37. Assert.Fail("Invalid encryption");
  38. }
  39. [TestMethod]
  40. [Owner("olegkap")]
  41. [TestCategory("Cipher")]
  42. [TestCategory("integration")]
  43. public void Test_Cipher_AEes128CBC_Connection()
  44. {
  45. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  46. connectionInfo.Encryptions.Clear();
  47. connectionInfo.Encryptions.Add("aes128-cbc", new CipherInfo(128, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), null); }));
  48. using (var client = new SshClient(connectionInfo))
  49. {
  50. client.Connect();
  51. client.Disconnect();
  52. }
  53. }
  54. [TestMethod]
  55. [Owner("olegkap")]
  56. [TestCategory("Cipher")]
  57. [TestCategory("integration")]
  58. public void Test_Cipher_Aes192CBC_Connection()
  59. {
  60. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  61. connectionInfo.Encryptions.Clear();
  62. connectionInfo.Encryptions.Add("aes192-cbc", new CipherInfo(192, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), null); }));
  63. using (var client = new SshClient(connectionInfo))
  64. {
  65. client.Connect();
  66. client.Disconnect();
  67. }
  68. }
  69. [TestMethod]
  70. [Owner("olegkap")]
  71. [TestCategory("Cipher")]
  72. [TestCategory("integration")]
  73. public void Test_Cipher_Aes256CBC_Connection()
  74. {
  75. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  76. connectionInfo.Encryptions.Clear();
  77. connectionInfo.Encryptions.Add("aes256-cbc", new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), null); }));
  78. using (var client = new SshClient(connectionInfo))
  79. {
  80. client.Connect();
  81. client.Disconnect();
  82. }
  83. }
  84. [TestMethod]
  85. [Owner("olegkap")]
  86. [TestCategory("Cipher")]
  87. [TestCategory("integration")]
  88. public void Test_Cipher_Aes128CTR_Connection()
  89. {
  90. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  91. connectionInfo.Encryptions.Clear();
  92. connectionInfo.Encryptions.Add("aes128-ctr", new CipherInfo(128, (key, iv) => { return new AesCipher(key, new CtrCipherMode(iv), null); }));
  93. using (var client = new SshClient(connectionInfo))
  94. {
  95. client.Connect();
  96. client.Disconnect();
  97. }
  98. }
  99. [TestMethod]
  100. [Owner("olegkap")]
  101. [TestCategory("Cipher")]
  102. [TestCategory("integration")]
  103. public void Test_Cipher_Aes192CTR_Connection()
  104. {
  105. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  106. connectionInfo.Encryptions.Clear();
  107. connectionInfo.Encryptions.Add("aes192-ctr", new CipherInfo(192, (key, iv) => { return new AesCipher(key, new CtrCipherMode(iv), null); }));
  108. using (var client = new SshClient(connectionInfo))
  109. {
  110. client.Connect();
  111. client.Disconnect();
  112. }
  113. }
  114. [TestMethod]
  115. [Owner("olegkap")]
  116. [TestCategory("Cipher")]
  117. [TestCategory("integration")]
  118. public void Test_Cipher_Aes256CTR_Connection()
  119. {
  120. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  121. connectionInfo.Encryptions.Clear();
  122. connectionInfo.Encryptions.Add("aes256-ctr", new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CtrCipherMode(iv), null); }));
  123. using (var client = new SshClient(connectionInfo))
  124. {
  125. client.Connect();
  126. client.Disconnect();
  127. }
  128. }
  129. [TestMethod]
  130. [Owner("olegkap")]
  131. [TestCategory("Cipher")]
  132. [TestCategory("integration")]
  133. public void Test_Cipher_Arcfour_Connection()
  134. {
  135. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, int.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD);
  136. connectionInfo.Encryptions.Clear();
  137. connectionInfo.Encryptions.Add("arcfour", new CipherInfo(128, (key, iv) => { return new Arc4Cipher(key, false); }));
  138. using (var client = new SshClient(connectionInfo))
  139. {
  140. client.Connect();
  141. client.Disconnect();
  142. }
  143. }
  144. /// <summary>
  145. ///A test for DecryptBlock
  146. ///</summary>
  147. [TestMethod]
  148. [Ignore] // placeholder for actual test
  149. public void DecryptBlockTest()
  150. {
  151. byte[] key = null; // TODO: Initialize to an appropriate value
  152. CipherMode mode = null; // TODO: Initialize to an appropriate value
  153. CipherPadding padding = null; // TODO: Initialize to an appropriate value
  154. AesCipher target = new AesCipher(key, mode, padding); // TODO: Initialize to an appropriate value
  155. byte[] inputBuffer = null; // TODO: Initialize to an appropriate value
  156. int inputOffset = 0; // TODO: Initialize to an appropriate value
  157. int inputCount = 0; // TODO: Initialize to an appropriate value
  158. byte[] outputBuffer = null; // TODO: Initialize to an appropriate value
  159. int outputOffset = 0; // TODO: Initialize to an appropriate value
  160. int expected = 0; // TODO: Initialize to an appropriate value
  161. int actual;
  162. actual = target.DecryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  163. Assert.AreEqual(expected, actual);
  164. Assert.Inconclusive("Verify the correctness of this test method.");
  165. }
  166. /// <summary>
  167. ///A test for AesCipher Constructor
  168. ///</summary>
  169. [TestMethod]
  170. [Ignore] // placeholder for actual test
  171. public void AesCipherConstructorTest()
  172. {
  173. byte[] key = null; // TODO: Initialize to an appropriate value
  174. CipherMode mode = null; // TODO: Initialize to an appropriate value
  175. CipherPadding padding = null; // TODO: Initialize to an appropriate value
  176. AesCipher target = new AesCipher(key, mode, padding);
  177. Assert.Inconclusive("TODO: Implement code to verify target");
  178. }
  179. /// <summary>
  180. ///A test for EncryptBlock
  181. ///</summary>
  182. [TestMethod]
  183. [Ignore] // placeholder for actual test
  184. public void EncryptBlockTest()
  185. {
  186. byte[] key = null; // TODO: Initialize to an appropriate value
  187. CipherMode mode = null; // TODO: Initialize to an appropriate value
  188. CipherPadding padding = null; // TODO: Initialize to an appropriate value
  189. AesCipher target = new AesCipher(key, mode, padding); // TODO: Initialize to an appropriate value
  190. byte[] inputBuffer = null; // TODO: Initialize to an appropriate value
  191. int inputOffset = 0; // TODO: Initialize to an appropriate value
  192. int inputCount = 0; // TODO: Initialize to an appropriate value
  193. byte[] outputBuffer = null; // TODO: Initialize to an appropriate value
  194. int outputOffset = 0; // TODO: Initialize to an appropriate value
  195. int expected = 0; // TODO: Initialize to an appropriate value
  196. int actual;
  197. actual = target.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  198. Assert.AreEqual(expected, actual);
  199. Assert.Inconclusive("Verify the correctness of this test method.");
  200. }
  201. }
  202. }