diff --git a/src/libgcrypt.c b/src/libgcrypt.c index f1c3cbd..fa852a4 100644 --- a/src/libgcrypt.c +++ b/src/libgcrypt.c @@ -524,16 +524,14 @@ _libssh2_cipher_init(_libssh2_cipher_ctx * h, _libssh2_cipher_type(algo), unsigned char *iv, unsigned char *secret, int encrypt) { - int mode = 0, ret; - int keylen = gcry_cipher_get_algo_keylen(algo); + int ret; + int cipher = _libssh2_gcry_cipher (algo); + int mode = _libssh2_gcry_mode (algo); + int keylen = gcry_cipher_get_algo_keylen(cipher); (void) encrypt; - if (algo != GCRY_CIPHER_ARCFOUR) { - mode = GCRY_CIPHER_MODE_CBC; - } - - ret = gcry_cipher_open(h, algo, mode, 0); + ret = gcry_cipher_open(h, cipher, mode, 0); if (ret) { return -1; } @@ -544,8 +542,8 @@ _libssh2_cipher_init(_libssh2_cipher_ctx * h, return -1; } - if (algo != GCRY_CIPHER_ARCFOUR) { - int blklen = gcry_cipher_get_algo_blklen(algo); + if (mode != GCRY_CIPHER_MODE_STREAM) { + int blklen = gcry_cipher_get_algo_blklen(cipher); ret = gcry_cipher_setiv(*h, iv, blklen); if (ret) { gcry_cipher_close(*h); @@ -561,8 +559,10 @@ _libssh2_cipher_crypt(_libssh2_cipher_ctx * ctx, _libssh2_cipher_type(algo), int encrypt, unsigned char *block) { - size_t blklen = gcry_cipher_get_algo_blklen(algo); + int cipher = _libssh2_gcry_cipher (algo); + size_t blklen = gcry_cipher_get_algo_blklen(cipher); int ret; + if (blklen == 1) { /* Hack for arcfour. */ blklen = 8; diff --git a/src/libgcrypt.h b/src/libgcrypt.h index 52c4ca0..8bf84cc 100644 --- a/src/libgcrypt.h +++ b/src/libgcrypt.h @@ -1,5 +1,6 @@ -/* Copyright (C) 2006, 2007, The Written Word, Inc. - * Copyright (C) 2008, Simon Josefsson +/* + * Copyright (C) 2008, 2009 Simon Josefsson + * Copyright (C) 2006, 2007, The Written Word, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, @@ -155,13 +156,24 @@ int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx * dsactx, #define _libssh2_cipher_type(name) int name #define _libssh2_cipher_ctx gcry_cipher_hd_t -#define _libssh2_cipher_aes256 GCRY_CIPHER_AES256 -#define _libssh2_cipher_aes192 GCRY_CIPHER_AES192 -#define _libssh2_cipher_aes128 GCRY_CIPHER_AES128 -#define _libssh2_cipher_blowfish GCRY_CIPHER_BLOWFISH -#define _libssh2_cipher_arcfour GCRY_CIPHER_ARCFOUR -#define _libssh2_cipher_cast5 GCRY_CIPHER_CAST5 -#define _libssh2_cipher_3des GCRY_CIPHER_3DES +#define _libssh2_gcry_ciphermode(c,m) ((c << 8) | m) +#define _libssh2_gcry_cipher(c) (c >> 8) +#define _libssh2_gcry_mode(m) (m & 0xFF) + +#define _libssh2_cipher_aes256 \ + _libssh2_gcry_ciphermode(GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC) +#define _libssh2_cipher_aes192 \ + _libssh2_gcry_ciphermode(GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC) +#define _libssh2_cipher_aes128 \ + _libssh2_gcry_ciphermode(GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC) +#define _libssh2_cipher_blowfish \ + _libssh2_gcry_ciphermode(GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC) +#define _libssh2_cipher_arcfour \ + _libssh2_gcry_ciphermode(GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM) +#define _libssh2_cipher_cast5 \ + _libssh2_gcry_ciphermode(GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC) +#define _libssh2_cipher_3des \ + _libssh2_gcry_ciphermode(GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC) int _libssh2_cipher_init(_libssh2_cipher_ctx * h, _libssh2_cipher_type(algo),