1 /* Copyright (c) 2009, 2010 Simon Josefsson <simon@josefsson.org>
2 * Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
5 * Redistribution and use in source and binary forms,
6 * with or without modification, are permitted provided
7 * that the following conditions are met:
9 * Redistributions of source code must retain the above
10 * copyright notice, this list of conditions and the
11 * following disclaimer.
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.
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.
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
39 #include "libssh2_priv.h"
41 #ifdef LIBSSH2_CRYPT_NONE
44 * Minimalist cipher: VERY secure *wink*
47 crypt_none_crypt(LIBSSH2_SESSION
* session
, unsigned char *buf
,
50 /* Do nothing to the data! */
54 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_none
= {
56 8, /* blocksize (SSH2 defines minimum blocksize as 8) */
64 #endif /* LIBSSH2_CRYPT_NONE */
69 _libssh2_cipher_type(algo
);
70 _libssh2_cipher_ctx h
;
74 crypt_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
));
83 return LIBSSH2_ERROR_ALLOC
;
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 crypt_encrypt(LIBSSH2_SESSION
* session
, unsigned char *block
,
99 size_t blocksize
, void **abstract
)
101 struct crypt_ctx
*cctx
= *(struct crypt_ctx
**) abstract
;
103 return _libssh2_cipher_crypt(&cctx
->h
, cctx
->algo
, cctx
->encrypt
, block
,
108 crypt_dtor(LIBSSH2_SESSION
* session
, void **abstract
)
110 struct crypt_ctx
**cctx
= (struct crypt_ctx
**) abstract
;
112 _libssh2_cipher_dtor(&(*cctx
)->h
);
113 LIBSSH2_FREE(session
, *cctx
);
120 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes128_ctr
= {
123 16, /* initial value length */
124 16, /* secret length -- 16*8 == 128bit */
129 _libssh2_cipher_aes128ctr
132 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes192_ctr
= {
135 16, /* initial value length */
136 24, /* secret length -- 24*8 == 192bit */
141 _libssh2_cipher_aes192ctr
144 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes256_ctr
= {
147 16, /* initial value length */
148 32, /* secret length -- 32*8 == 256bit */
153 _libssh2_cipher_aes256ctr
158 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes128_cbc
= {
161 16, /* initial value length */
162 16, /* secret length -- 16*8 == 128bit */
167 _libssh2_cipher_aes128
170 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes192_cbc
= {
173 16, /* initial value length */
174 24, /* secret length -- 24*8 == 192bit */
179 _libssh2_cipher_aes192
182 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_aes256_cbc
= {
185 16, /* initial value length */
186 32, /* secret length -- 32*8 == 256bit */
191 _libssh2_cipher_aes256
194 /* rijndael-cbc@lysator.liu.se == aes256-cbc */
195 static const LIBSSH2_CRYPT_METHOD
196 libssh2_crypt_method_rijndael_cbc_lysator_liu_se
= {
197 "rijndael-cbc@lysator.liu.se",
199 16, /* initial value length */
200 32, /* secret length -- 32*8 == 256bit */
205 _libssh2_cipher_aes256
207 #endif /* LIBSSH2_AES */
210 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_blowfish_cbc
= {
213 8, /* initial value length */
214 16, /* secret length */
219 _libssh2_cipher_blowfish
221 #endif /* LIBSSH2_BLOWFISH */
224 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_arcfour
= {
227 8, /* initial value length */
228 16, /* secret length */
233 _libssh2_cipher_arcfour
237 crypt_init_arcfour128(LIBSSH2_SESSION
* session
,
238 const LIBSSH2_CRYPT_METHOD
* method
,
239 unsigned char *iv
, int *free_iv
,
240 unsigned char *secret
, int *free_secret
,
241 int encrypt
, void **abstract
)
245 rc
= crypt_init (session
, method
, iv
, free_iv
, secret
, free_secret
,
248 struct crypt_ctx
*cctx
= *(struct crypt_ctx
**) abstract
;
249 unsigned char block
[8];
250 size_t discard
= 1536;
251 for (; discard
; discard
-= 8)
252 _libssh2_cipher_crypt(&cctx
->h
, cctx
->algo
, cctx
->encrypt
, block
,
259 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_arcfour128
= {
262 8, /* initial value length */
263 16, /* secret length */
265 &crypt_init_arcfour128
,
268 _libssh2_cipher_arcfour
270 #endif /* LIBSSH2_RC4 */
273 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_cast128_cbc
= {
276 8, /* initial value length */
277 16, /* secret length */
282 _libssh2_cipher_cast5
284 #endif /* LIBSSH2_CAST */
287 static const LIBSSH2_CRYPT_METHOD libssh2_crypt_method_3des_cbc
= {
290 8, /* initial value length */
291 24, /* secret length */
300 static const LIBSSH2_CRYPT_METHOD
*_libssh2_crypt_methods
[] = {
302 &libssh2_crypt_method_aes128_ctr
,
303 &libssh2_crypt_method_aes192_ctr
,
304 &libssh2_crypt_method_aes256_ctr
,
305 #endif /* LIBSSH2_AES */
307 &libssh2_crypt_method_aes256_cbc
,
308 &libssh2_crypt_method_rijndael_cbc_lysator_liu_se
, /* == aes256-cbc */
309 &libssh2_crypt_method_aes192_cbc
,
310 &libssh2_crypt_method_aes128_cbc
,
311 #endif /* LIBSSH2_AES */
313 &libssh2_crypt_method_blowfish_cbc
,
314 #endif /* LIBSSH2_BLOWFISH */
316 &libssh2_crypt_method_arcfour128
,
317 &libssh2_crypt_method_arcfour
,
318 #endif /* LIBSSH2_RC4 */
320 &libssh2_crypt_method_cast128_cbc
,
321 #endif /* LIBSSH2_CAST */
323 &libssh2_crypt_method_3des_cbc
,
324 #endif /* LIBSSH2_DES */
325 #ifdef LIBSSH2_CRYPT_NONE
326 &libssh2_crypt_method_none
,
331 /* Expose to kex.c */
332 const LIBSSH2_CRYPT_METHOD
**
333 libssh2_crypt_methods(void)
335 return _libssh2_crypt_methods
;