1 /* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
4 * Redistribution and use in source and binary forms,
5 * with or without modification, are permitted provided
6 * that the following conditions are met:
8 * Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * Neither the name of the copyright holder nor the names
18 * of any other contributors may be used to endorse or
19 * promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 #include "libssh2_priv.h"
40 #ifdef LIBSSH2_CRYPT_NONE
41 /* {{{ libssh2_crypt_none_crypt
42 * Minimalist cipher: VERY secure *wink*
45 libssh2_crypt_none_crypt(LIBSSH2_SESSION
* session
, unsigned char *buf
,
48 /* Do nothing to the data! */
54 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_none
= {
56 8, /* blocksize (SSH2 defines minimum blocksize as 8) */
61 libssh2_crypt_none_crypt
,
64 #endif /* LIBSSH2_CRYPT_NONE */
69 _libssh2_cipher_type(algo
);
70 _libssh2_cipher_ctx h
;
74 _libssh2_init(LIBSSH2_SESSION
* session
,
75 const LIBSSH2_CRYPT_METHOD
* method
,
76 unsigned char *iv
, int *free_iv
,
77 unsigned char *secret
, int *free_secret
,
78 int encrypt
, void **abstract
)
80 struct crypt_ctx
*ctx
= LIBSSH2_ALLOC(session
,
81 sizeof(struct crypt_ctx
));
85 ctx
->encrypt
= encrypt
;
86 ctx
->algo
= method
->algo
;
87 if (_libssh2_cipher_init(&ctx
->h
, ctx
->algo
, iv
, secret
, encrypt
)) {
88 LIBSSH2_FREE(session
, ctx
);
98 _libssh2_encrypt(LIBSSH2_SESSION
* session
, unsigned char *block
,
101 struct crypt_ctx
*cctx
= *(struct crypt_ctx
**) abstract
;
103 return _libssh2_cipher_crypt(&cctx
->h
, cctx
->algo
, cctx
->encrypt
, block
);
107 _libssh2_dtor(LIBSSH2_SESSION
* session
, void **abstract
)
109 struct crypt_ctx
**cctx
= (struct crypt_ctx
**) abstract
;
111 _libssh2_cipher_dtor(&(*cctx
)->h
);
112 LIBSSH2_FREE(session
, *cctx
);
119 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes128_cbc
= {
122 16, /* initial value length */
123 16, /* secret length -- 16*8 == 128bit */
128 _libssh2_cipher_aes128
131 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes192_cbc
= {
134 16, /* initial value length */
135 24, /* secret length -- 24*8 == 192bit */
140 _libssh2_cipher_aes192
143 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes256_cbc
= {
146 16, /* initial value length */
147 32, /* secret length -- 32*8 == 256bit */
152 _libssh2_cipher_aes256
155 /* rijndael-cbc@lysator.liu.se == aes256-cbc */
156 static const LIBSSH2_CRYPT_METHOD
157 libssh2_crypt_method_rijndael_cbc_lysator_liu_se
= {
158 "rijndael-cbc@lysator.liu.se",
160 16, /* initial value length */
161 32, /* secret length -- 32*8 == 256bit */
166 _libssh2_cipher_aes256
168 #endif /* LIBSSH2_AES */
171 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_blowfish_cbc
= {
174 8, /* initial value length */
175 16, /* secret length */
180 _libssh2_cipher_blowfish
182 #endif /* LIBSSH2_BLOWFISH */
185 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_arcfour
= {
188 8, /* initial value length */
189 16, /* secret length */
194 _libssh2_cipher_arcfour
196 #endif /* LIBSSH2_RC4 */
199 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_cast128_cbc
= {
202 8, /* initial value length */
203 16, /* secret length */
208 _libssh2_cipher_cast5
210 #endif /* LIBSSH2_CAST */
213 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_3des_cbc
= {
216 8, /* initial value length */
217 24, /* secret length */
226 static const LIBSSH2_CRYPT_METHOD
*_libssh2_crypt_methods
[] = {
228 &libssh2_crypt_method_aes256_cbc
,
229 &libssh2_crypt_method_rijndael_cbc_lysator_liu_se
, /* == aes256-cbc */
230 &libssh2_crypt_method_aes192_cbc
,
231 &libssh2_crypt_method_aes128_cbc
,
232 #endif /* LIBSSH2_AES */
234 &libssh2_crypt_method_blowfish_cbc
,
235 #endif /* LIBSSH2_BLOWFISH */
237 &libssh2_crypt_method_arcfour
,
238 #endif /* LIBSSH2_RC4 */
240 &libssh2_crypt_method_cast128_cbc
,
241 #endif /* LIBSSH2_CAST */
243 &libssh2_crypt_method_3des_cbc
,
244 #endif /* LIBSSH2_DES */
245 #ifdef LIBSSH2_CRYPT_NONE
246 &libssh2_crypt_method_none
,
251 /* Expose to kex.c */
252 const LIBSSH2_CRYPT_METHOD
**
253 libssh2_crypt_methods(void)
255 return _libssh2_crypt_methods
;