AlgorithmSupport.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:9k
源码类别:

Telnet服务器

开发平台:

C#

  1. /*
  2.  Copyright (c) 2005 Poderosa Project, All Rights Reserved.
  3.  This file is a part of the Granados SSH Client Library that is subject to
  4.  the license included in the distributed package.
  5.  You may not use this file except in compliance with the license.
  6. * $Id: AlgorithmSupport.cs,v 1.2 2005/04/20 08:58:50 okajima Exp $
  7. */
  8. using System;
  9. using HMACSHA1 = System.Security.Cryptography.HMACSHA1;
  10. using Granados.Crypto;
  11. namespace Granados.SSHC
  12. {
  13. /*
  14.  * Cipher
  15.  *  The numbers at the tail of the class names indicates the version of SSH protocol.
  16.  *  The difference between V1 and V2 is the CBC procedure
  17.  */
  18. internal interface Cipher {
  19. void Encrypt(byte[] data, int offset, int len, byte[] result, int result_offset);
  20. void Decrypt(byte[] data, int offset, int len, byte[] result, int result_offset);
  21. int BlockSize { get; }
  22. }
  23. internal class BlowfishCipher1 : Cipher {
  24. private Blowfish _bf;
  25. public BlowfishCipher1(byte[] key) {
  26. _bf = new Blowfish();
  27. _bf.initializeKey(key);
  28. }
  29. public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  30. _bf.encryptSSH1Style(data, offset, len, result, ro);
  31. }
  32. public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  33. _bf.decryptSSH1Style(data, offset, len, result, ro);
  34. }
  35. public int BlockSize { get { return 8; } } 
  36. }
  37. internal class BlowfishCipher2 : Cipher {
  38. private Blowfish _bf;
  39. public BlowfishCipher2(byte[] key) {
  40. _bf = new Blowfish();
  41. _bf.initializeKey(key);
  42. }
  43. public BlowfishCipher2(byte[] key, byte[] iv) {
  44. _bf = new Blowfish();
  45. _bf.SetIV(iv);
  46. _bf.initializeKey(key);
  47. }
  48. public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  49. _bf.encryptCBC(data, offset, len, result, ro);
  50. }
  51. public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  52. _bf.decryptCBC(data, offset, len, result, ro);
  53. }
  54. public int BlockSize { get { return 8; } }
  55. }
  56. internal class TripleDESCipher1 : Cipher {
  57. private DES _DESCipher1;
  58. private DES _DESCipher2;
  59. private DES _DESCipher3;
  60. public TripleDESCipher1(byte[] key) {
  61. _DESCipher1 = new DES();
  62. _DESCipher2 = new DES();
  63. _DESCipher3 = new DES();
  64. _DESCipher1.InitializeKey(key, 0);
  65. _DESCipher2.InitializeKey(key, 8);
  66. _DESCipher3.InitializeKey(key,16);
  67. }
  68. public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  69. byte[] buf1 = new byte[len];
  70. _DESCipher1.EncryptCBC(data, offset, len, result, ro);
  71. _DESCipher2.DecryptCBC(result, ro, buf1.Length, buf1, 0);
  72. _DESCipher3.EncryptCBC(buf1, 0, buf1.Length, result, ro);
  73. }
  74. public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  75. byte[] buf1 = new byte[len];
  76. _DESCipher3.DecryptCBC(data, offset, len, result, ro);
  77. _DESCipher2.EncryptCBC(result, ro, buf1.Length, buf1, 0);
  78. _DESCipher1.DecryptCBC(buf1, 0, buf1.Length, result, ro);
  79. }
  80. public int BlockSize { get { return 8; } } 
  81. }
  82. internal class TripleDESCipher2 : Cipher {
  83. private DES _DESCipher1;
  84. private DES _DESCipher2;
  85. private DES _DESCipher3;
  86. public TripleDESCipher2(byte[] key) {
  87. _DESCipher1 = new DES();
  88. _DESCipher2 = new DES();
  89. _DESCipher3 = new DES();
  90. _DESCipher1.InitializeKey(key, 0);
  91. _DESCipher2.InitializeKey(key, 8);
  92. _DESCipher3.InitializeKey(key,16);
  93. }
  94. public TripleDESCipher2(byte[] key, byte[] iv) {
  95. _DESCipher1 = new DES();
  96. _DESCipher1.SetIV(iv);
  97. _DESCipher2 = new DES();
  98. _DESCipher2.SetIV(iv);
  99. _DESCipher3 = new DES();
  100. _DESCipher3.SetIV(iv);
  101. _DESCipher1.InitializeKey(key, 0);
  102. _DESCipher2.InitializeKey(key, 8);
  103. _DESCipher3.InitializeKey(key,16);
  104. }
  105. public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  106. byte[] buf1 = new byte[8];
  107. int n = 0;
  108. while(n < len) {
  109. _DESCipher1.EncryptCBC(data, offset+n, 8, result, ro+n);
  110. _DESCipher2.DecryptCBC(result, ro+n, 8, buf1, 0);
  111. _DESCipher3.EncryptCBC(buf1, 0, 8, result, ro+n);
  112. _DESCipher1.SetIV(result, ro+n);
  113. _DESCipher2.SetIV(result, ro+n);
  114. _DESCipher3.SetIV(result, ro+n);
  115. n += 8;
  116. }
  117. }
  118. public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  119. byte[] buf1 = new byte[8];
  120. int n = 0;
  121. while(n < len) {
  122. _DESCipher3.DecryptCBC(data, offset+n, 8, result, ro+n);
  123. _DESCipher2.EncryptCBC(result, ro+n, 8, buf1, 0);
  124. _DESCipher1.DecryptCBC(buf1, 0, 8, result, ro+n);
  125. _DESCipher3.SetIV(data, offset+n);
  126. _DESCipher2.SetIV(data, offset+n);
  127. _DESCipher1.SetIV(data, offset+n);
  128. n += 8;
  129. }
  130. }
  131. public int BlockSize { get { return 8; } } 
  132. }
  133. internal class RijindaelCipher2 : Cipher {
  134. private Rijndael _rijindael;
  135. public RijindaelCipher2(byte[] key, byte[] iv) {
  136. _rijindael = new Rijndael();
  137. _rijindael.SetIV(iv);
  138. _rijindael.InitializeKey(key);
  139. }
  140. public void Encrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  141. _rijindael.encryptCBC(data, offset, len, result, ro);
  142. }
  143. public void Decrypt(byte[] data, int offset, int len, byte[] result, int ro) {
  144. _rijindael.decryptCBC(data, offset, len, result, ro);
  145. }
  146. public int BlockSize { get { return _rijindael.GetBlockSize(); } }
  147. }
  148. /// <summary>
  149. /// Creates a cipher from given parameters
  150. /// </summary>
  151. internal class CipherFactory {
  152. public static Cipher CreateCipher(SSHProtocol protocol, CipherAlgorithm algorithm, byte[] key) {
  153. if(protocol==SSHProtocol.SSH1) {
  154. switch(algorithm) {
  155. case CipherAlgorithm.TripleDES:
  156. return new TripleDESCipher1(key);
  157. case CipherAlgorithm.Blowfish:
  158. return new BlowfishCipher1(key);
  159. default:
  160. throw new SSHException("unknown algorithm " + algorithm);
  161. }
  162. }
  163. else {
  164. switch(algorithm) {
  165. case CipherAlgorithm.TripleDES:
  166. return new TripleDESCipher2(key);
  167. case CipherAlgorithm.Blowfish:
  168. return new BlowfishCipher2(key);
  169. default:
  170. throw new SSHException("unknown algorithm " + algorithm);
  171. }
  172. }
  173. }
  174. public static Cipher CreateCipher(SSHProtocol protocol, CipherAlgorithm algorithm, byte[] key, byte[] iv) {
  175. if(protocol==SSHProtocol.SSH1) {
  176. return CreateCipher(protocol, algorithm, key);
  177. }
  178. else {
  179. switch(algorithm) {
  180. case CipherAlgorithm.TripleDES:
  181. return new TripleDESCipher2(key, iv);
  182. case CipherAlgorithm.Blowfish:
  183. return new BlowfishCipher2(key, iv);
  184. case CipherAlgorithm.AES128:
  185. return new RijindaelCipher2(key, iv);
  186. default:
  187. throw new SSHException("unknown algorithm " + algorithm);
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// returns necessary key size from Algorithm in bytes
  193. /// </summary>
  194. public static int GetKeySize(CipherAlgorithm algorithm) {
  195. switch(algorithm) {
  196. case CipherAlgorithm.TripleDES:
  197. return 24;
  198. case CipherAlgorithm.Blowfish:
  199. case CipherAlgorithm.AES128:
  200. return 16;
  201. default:
  202. throw new SSHException("unknown algorithm " + algorithm);
  203. }
  204. }
  205. /// <summary>
  206. /// returns the block size from Algorithm in bytes
  207. /// </summary>
  208. public static int GetBlockSize(CipherAlgorithm algorithm) {
  209. switch(algorithm) {
  210. case CipherAlgorithm.TripleDES:
  211. case CipherAlgorithm.Blowfish:
  212. return 8;
  213. case CipherAlgorithm.AES128:
  214. return 16;
  215. default:
  216. throw new SSHException("unknown algorithm " + algorithm);
  217. }
  218. }
  219. public static string AlgorithmToSSH2Name(CipherAlgorithm algorithm) {
  220. switch(algorithm) {
  221. case CipherAlgorithm.TripleDES:
  222. return "3des-cbc";
  223. case CipherAlgorithm.Blowfish:
  224. return "blowfish-cbc";
  225. case CipherAlgorithm.AES128:
  226. return "aes128-cbc";
  227. default:
  228. throw new SSHException("unknown algorithm " + algorithm);
  229. }
  230. }
  231. public static CipherAlgorithm SSH2NameToAlgorithm(string name) {
  232. if(name=="3des-cbc")
  233. return CipherAlgorithm.TripleDES;
  234. else if(name=="blowfish-cbc")
  235. return CipherAlgorithm.Blowfish;
  236. else if(name=="aes128-cbc")
  237. return CipherAlgorithm.AES128;
  238. else
  239. throw new SSHException("Unknown algorithm "+name);
  240. }
  241. }
  242.  
  243. /**********        MAC        ***********/
  244. interface MAC {
  245. byte[] Calc(byte[] data);
  246. int Size { get; }
  247. }
  248. internal class MACSHA1 : MAC {
  249. private HMACSHA1 _algorithm;
  250. public MACSHA1(byte[] key) {
  251. _algorithm = new HMACSHA1(key);
  252. }
  253. public byte[] Calc(byte[] data) {
  254. _algorithm.Initialize();
  255. return _algorithm.ComputeHash(data);
  256. }
  257. public int Size { get { return 20; } }
  258. }
  259. internal class MACFactory {
  260. public static MAC CreateMAC(MACAlgorithm algorithm, byte[] key) {
  261. if(algorithm==MACAlgorithm.HMACSHA1)
  262. return new MACSHA1(key);
  263. else
  264. throw new SSHException("unknown algorithm"+algorithm);
  265. }
  266. public static int GetSize(MACAlgorithm algorithm) {
  267. if(algorithm==MACAlgorithm.HMACSHA1)
  268. return 20;
  269. else
  270. throw new SSHException("unknown algorithm"+algorithm);
  271. }
  272. }
  273. }