geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

350 lines
10 KiB

  1. /* Copyright (c) 2009, 2010 Simon Josefsson <simon@josefsson.org>
  2. * Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms,
  6. * with or without modification, are permitted provided
  7. * that the following conditions are met:
  8. *
  9. * Redistributions of source code must retain the above
  10. * copyright notice, this list of conditions and the
  11. * following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. *
  18. * Neither the name of the copyright holder nor the names
  19. * of any other contributors may be used to endorse or
  20. * promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  24. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  33. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. */
  38. #include "libssh2_priv.h"
  39. #ifdef LIBSSH2_CRYPT_NONE
  40. /* crypt_none_crypt
  41. * Minimalist cipher: VERY secure *wink*
  42. */
  43. static int
  44. crypt_none_crypt(LIBSSH2_SESSION * session, unsigned char *buf,
  45. void **abstract)
  46. {
  47. /* Do nothing to the data! */
  48. return 0;
  49. }
  50. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_none = {
  51. "none",
  52. "DEK-Info: NONE",
  53. 8, /* blocksize (SSH2 defines minimum blocksize as 8) */
  54. 0, /* iv_len */
  55. 0, /* secret_len */
  56. 0, /* flags */
  57. NULL,
  58. crypt_none_crypt,
  59. NULL
  60. };
  61. #endif /* LIBSSH2_CRYPT_NONE */
  62. struct crypt_ctx
  63. {
  64. int encrypt;
  65. _libssh2_cipher_type(algo);
  66. _libssh2_cipher_ctx h;
  67. };
  68. static int
  69. crypt_init(LIBSSH2_SESSION * session,
  70. const LIBSSH2_CRYPT_METHOD * method,
  71. unsigned char *iv, int *free_iv,
  72. unsigned char *secret, int *free_secret,
  73. int encrypt, void **abstract)
  74. {
  75. struct crypt_ctx *ctx = LIBSSH2_ALLOC(session,
  76. sizeof(struct crypt_ctx));
  77. if(!ctx)
  78. return LIBSSH2_ERROR_ALLOC;
  79. ctx->encrypt = encrypt;
  80. ctx->algo = method->algo;
  81. if(_libssh2_cipher_init(&ctx->h, ctx->algo, iv, secret, encrypt)) {
  82. LIBSSH2_FREE(session, ctx);
  83. return -1;
  84. }
  85. *abstract = ctx;
  86. *free_iv = 1;
  87. *free_secret = 1;
  88. return 0;
  89. }
  90. static int
  91. crypt_encrypt(LIBSSH2_SESSION * session, unsigned char *block,
  92. size_t blocksize, void **abstract)
  93. {
  94. struct crypt_ctx *cctx = *(struct crypt_ctx **) abstract;
  95. (void) session;
  96. return _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block,
  97. blocksize);
  98. }
  99. static int
  100. crypt_dtor(LIBSSH2_SESSION * session, void **abstract)
  101. {
  102. struct crypt_ctx **cctx = (struct crypt_ctx **) abstract;
  103. if(cctx && *cctx) {
  104. _libssh2_cipher_dtor(&(*cctx)->h);
  105. LIBSSH2_FREE(session, *cctx);
  106. *abstract = NULL;
  107. }
  108. return 0;
  109. }
  110. #if LIBSSH2_AES_CTR
  111. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes128_ctr = {
  112. "aes128-ctr",
  113. "",
  114. 16, /* blocksize */
  115. 16, /* initial value length */
  116. 16, /* secret length -- 16*8 == 128bit */
  117. 0, /* flags */
  118. &crypt_init,
  119. &crypt_encrypt,
  120. &crypt_dtor,
  121. _libssh2_cipher_aes128ctr
  122. };
  123. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes192_ctr = {
  124. "aes192-ctr",
  125. "",
  126. 16, /* blocksize */
  127. 16, /* initial value length */
  128. 24, /* secret length -- 24*8 == 192bit */
  129. 0, /* flags */
  130. &crypt_init,
  131. &crypt_encrypt,
  132. &crypt_dtor,
  133. _libssh2_cipher_aes192ctr
  134. };
  135. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes256_ctr = {
  136. "aes256-ctr",
  137. "",
  138. 16, /* blocksize */
  139. 16, /* initial value length */
  140. 32, /* secret length -- 32*8 == 256bit */
  141. 0, /* flags */
  142. &crypt_init,
  143. &crypt_encrypt,
  144. &crypt_dtor,
  145. _libssh2_cipher_aes256ctr
  146. };
  147. #endif
  148. #if LIBSSH2_AES
  149. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes128_cbc = {
  150. "aes128-cbc",
  151. "DEK-Info: AES-128-CBC",
  152. 16, /* blocksize */
  153. 16, /* initial value length */
  154. 16, /* secret length -- 16*8 == 128bit */
  155. 0, /* flags */
  156. &crypt_init,
  157. &crypt_encrypt,
  158. &crypt_dtor,
  159. _libssh2_cipher_aes128
  160. };
  161. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes192_cbc = {
  162. "aes192-cbc",
  163. "DEK-Info: AES-192-CBC",
  164. 16, /* blocksize */
  165. 16, /* initial value length */
  166. 24, /* secret length -- 24*8 == 192bit */
  167. 0, /* flags */
  168. &crypt_init,
  169. &crypt_encrypt,
  170. &crypt_dtor,
  171. _libssh2_cipher_aes192
  172. };
  173. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes256_cbc = {
  174. "aes256-cbc",
  175. "DEK-Info: AES-256-CBC",
  176. 16, /* blocksize */
  177. 16, /* initial value length */
  178. 32, /* secret length -- 32*8 == 256bit */
  179. 0, /* flags */
  180. &crypt_init,
  181. &crypt_encrypt,
  182. &crypt_dtor,
  183. _libssh2_cipher_aes256
  184. };
  185. /* rijndael-cbc@lysator.liu.se == aes256-cbc */
  186. static const LIBSSH2_CRYPT_METHOD
  187. libssh2_crypt_method_rijndael_cbc_lysator_liu_se = {
  188. "rijndael-cbc@lysator.liu.se",
  189. "DEK-Info: AES-256-CBC",
  190. 16, /* blocksize */
  191. 16, /* initial value length */
  192. 32, /* secret length -- 32*8 == 256bit */
  193. 0, /* flags */
  194. &crypt_init,
  195. &crypt_encrypt,
  196. &crypt_dtor,
  197. _libssh2_cipher_aes256
  198. };
  199. #endif /* LIBSSH2_AES */
  200. #if LIBSSH2_BLOWFISH
  201. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_blowfish_cbc = {
  202. "blowfish-cbc",
  203. "",
  204. 8, /* blocksize */
  205. 8, /* initial value length */
  206. 16, /* secret length */
  207. 0, /* flags */
  208. &crypt_init,
  209. &crypt_encrypt,
  210. &crypt_dtor,
  211. _libssh2_cipher_blowfish
  212. };
  213. #endif /* LIBSSH2_BLOWFISH */
  214. #if LIBSSH2_RC4
  215. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_arcfour = {
  216. "arcfour",
  217. "DEK-Info: RC4",
  218. 8, /* blocksize */
  219. 8, /* initial value length */
  220. 16, /* secret length */
  221. 0, /* flags */
  222. &crypt_init,
  223. &crypt_encrypt,
  224. &crypt_dtor,
  225. _libssh2_cipher_arcfour
  226. };
  227. static int
  228. crypt_init_arcfour128(LIBSSH2_SESSION * session,
  229. const LIBSSH2_CRYPT_METHOD * method,
  230. unsigned char *iv, int *free_iv,
  231. unsigned char *secret, int *free_secret,
  232. int encrypt, void **abstract)
  233. {
  234. int rc;
  235. rc = crypt_init(session, method, iv, free_iv, secret, free_secret,
  236. encrypt, abstract);
  237. if(rc == 0) {
  238. struct crypt_ctx *cctx = *(struct crypt_ctx **) abstract;
  239. unsigned char block[8];
  240. size_t discard = 1536;
  241. for(; discard; discard -= 8)
  242. _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block,
  243. method->blocksize);
  244. }
  245. return rc;
  246. }
  247. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_arcfour128 = {
  248. "arcfour128",
  249. "",
  250. 8, /* blocksize */
  251. 8, /* initial value length */
  252. 16, /* secret length */
  253. 0, /* flags */
  254. &crypt_init_arcfour128,
  255. &crypt_encrypt,
  256. &crypt_dtor,
  257. _libssh2_cipher_arcfour
  258. };
  259. #endif /* LIBSSH2_RC4 */
  260. #if LIBSSH2_CAST
  261. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_cast128_cbc = {
  262. "cast128-cbc",
  263. "",
  264. 8, /* blocksize */
  265. 8, /* initial value length */
  266. 16, /* secret length */
  267. 0, /* flags */
  268. &crypt_init,
  269. &crypt_encrypt,
  270. &crypt_dtor,
  271. _libssh2_cipher_cast5
  272. };
  273. #endif /* LIBSSH2_CAST */
  274. #if LIBSSH2_3DES
  275. static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_3des_cbc = {
  276. "3des-cbc",
  277. "DEK-Info: DES-EDE3-CBC",
  278. 8, /* blocksize */
  279. 8, /* initial value length */
  280. 24, /* secret length */
  281. 0, /* flags */
  282. &crypt_init,
  283. &crypt_encrypt,
  284. &crypt_dtor,
  285. _libssh2_cipher_3des
  286. };
  287. #endif
  288. static const LIBSSH2_CRYPT_METHOD *_libssh2_crypt_methods[] = {
  289. #if LIBSSH2_AES_CTR
  290. &libssh2_crypt_method_aes128_ctr,
  291. &libssh2_crypt_method_aes192_ctr,
  292. &libssh2_crypt_method_aes256_ctr,
  293. #endif /* LIBSSH2_AES */
  294. #if LIBSSH2_AES
  295. &libssh2_crypt_method_aes256_cbc,
  296. &libssh2_crypt_method_rijndael_cbc_lysator_liu_se, /* == aes256-cbc */
  297. &libssh2_crypt_method_aes192_cbc,
  298. &libssh2_crypt_method_aes128_cbc,
  299. #endif /* LIBSSH2_AES */
  300. #if LIBSSH2_BLOWFISH
  301. &libssh2_crypt_method_blowfish_cbc,
  302. #endif /* LIBSSH2_BLOWFISH */
  303. #if LIBSSH2_RC4
  304. &libssh2_crypt_method_arcfour128,
  305. &libssh2_crypt_method_arcfour,
  306. #endif /* LIBSSH2_RC4 */
  307. #if LIBSSH2_CAST
  308. &libssh2_crypt_method_cast128_cbc,
  309. #endif /* LIBSSH2_CAST */
  310. #if LIBSSH2_3DES
  311. &libssh2_crypt_method_3des_cbc,
  312. #endif /* LIBSSH2_DES */
  313. #ifdef LIBSSH2_CRYPT_NONE
  314. &libssh2_crypt_method_none,
  315. #endif
  316. NULL
  317. };
  318. /* Expose to kex.c */
  319. const LIBSSH2_CRYPT_METHOD **
  320. libssh2_crypt_methods(void)
  321. {
  322. return _libssh2_crypt_methods;
  323. }