1
1

[crypto] Removed ugly ifdefs on gcrypt/libcrypto

Этот коммит содержится в:
Aris Adamantiadis 2011-06-13 14:47:17 +02:00
родитель a3c28f2558
Коммит 2653b31af0
3 изменённых файлов: 48 добавлений и 64 удалений

Просмотреть файл

@ -87,9 +87,9 @@ struct crypto_struct {
gcry_cipher_hd_t *key; gcry_cipher_hd_t *key;
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
void *key; /* a key buffer allocated for the algo */ void *key; /* a key buffer allocated for the algo */
void *IV;
#endif #endif
unsigned int keysize; /* bytes of key used. != keylen */ unsigned int keysize; /* bytes of key used. != keylen */
#ifdef HAVE_LIBGCRYPT
/* sets the new key for immediate use */ /* sets the new key for immediate use */
int (*set_encrypt_key)(struct crypto_struct *cipher, void *key, void *IV); int (*set_encrypt_key)(struct crypto_struct *cipher, void *key, void *IV);
int (*set_decrypt_key)(struct crypto_struct *cipher, void *key, void *IV); int (*set_decrypt_key)(struct crypto_struct *cipher, void *key, void *IV);
@ -97,15 +97,6 @@ struct crypto_struct {
unsigned long len); unsigned long len);
void (*cbc_decrypt)(struct crypto_struct *cipher, void *in, void *out, void (*cbc_decrypt)(struct crypto_struct *cipher, void *in, void *out,
unsigned long len); unsigned long len);
#elif defined HAVE_LIBCRYPTO
/* sets the new key for immediate use */
int (*set_encrypt_key)(struct crypto_struct *cipher, void *key);
int (*set_decrypt_key)(struct crypto_struct *cipher, void *key);
void (*cbc_encrypt)(struct crypto_struct *cipher, void *in, void *out,
unsigned long len, void *IV);
void (*cbc_decrypt)(struct crypto_struct *cipher, void *in, void *out,
unsigned long len, void *IV);
#endif
}; };
/* vim: set ts=2 sw=2 et cindent: */ /* vim: set ts=2 sw=2 et cindent: */

Просмотреть файл

@ -74,20 +74,13 @@ int packet_decrypt(ssh_session session, void *data,uint32_t len) {
ssh_log(session,SSH_LOG_PACKET, "Decrypting %d bytes", len); ssh_log(session,SSH_LOG_PACKET, "Decrypting %d bytes", len);
#ifdef HAVE_LIBGCRYPT
if (crypto->set_decrypt_key(crypto, session->current_crypto->decryptkey, if (crypto->set_decrypt_key(crypto, session->current_crypto->decryptkey,
session->current_crypto->decryptIV) < 0) { session->current_crypto->decryptIV) < 0) {
SAFE_FREE(out); SAFE_FREE(out);
return -1; return -1;
} }
crypto->cbc_decrypt(crypto,data,out,len); crypto->cbc_decrypt(crypto,data,out,len);
#elif defined HAVE_LIBCRYPTO
if (crypto->set_decrypt_key(crypto, session->current_crypto->decryptkey) < 0) {
SAFE_FREE(out);
return -1;
}
crypto->cbc_decrypt(crypto,data,out,len,session->current_crypto->decryptIV);
#endif
memcpy(data,out,len); memcpy(data,out,len);
memset(out,0,len); memset(out,0,len);
@ -122,18 +115,11 @@ unsigned char *packet_encrypt(ssh_session session, void *data, uint32_t len) {
"Encrypting packet with seq num: %d, len: %d", "Encrypting packet with seq num: %d, len: %d",
session->send_seq,len); session->send_seq,len);
#ifdef HAVE_LIBGCRYPT
if (crypto->set_encrypt_key(crypto, session->current_crypto->encryptkey, if (crypto->set_encrypt_key(crypto, session->current_crypto->encryptkey,
session->current_crypto->encryptIV) < 0) { session->current_crypto->encryptIV) < 0) {
SAFE_FREE(out); SAFE_FREE(out);
return NULL; return NULL;
} }
#elif defined HAVE_LIBCRYPTO
if (crypto->set_encrypt_key(crypto, session->current_crypto->encryptkey) < 0) {
SAFE_FREE(out);
return NULL;
}
#endif
if (session->version == 2) { if (session->version == 2) {
ctx = hmac_init(session->current_crypto->encryptMAC,20,SSH_HMAC_SHA1); ctx = hmac_init(session->current_crypto->encryptMAC,20,SSH_HMAC_SHA1);
@ -153,12 +139,7 @@ unsigned char *packet_encrypt(ssh_session session, void *data, uint32_t len) {
#endif #endif
} }
#ifdef HAVE_LIBGCRYPT
crypto->cbc_encrypt(crypto, data, out, len); crypto->cbc_encrypt(crypto, data, out, len);
#elif defined HAVE_LIBCRYPTO
crypto->cbc_encrypt(crypto, data, out, len,
session->current_crypto->encryptIV);
#endif
memcpy(data, out, len); memcpy(data, out, len);
memset(out, 0, len); memset(out, 0, len);

Просмотреть файл

@ -234,30 +234,31 @@ void hmac_final(HMACCTX ctx, unsigned char *hashmacbuf, unsigned int *len) {
#ifdef HAS_BLOWFISH #ifdef HAS_BLOWFISH
/* the wrapper functions for blowfish */ /* the wrapper functions for blowfish */
static int blowfish_set_key(struct crypto_struct *cipher, void *key){ static int blowfish_set_key(struct crypto_struct *cipher, void *key, void *IV){
if (cipher->key == NULL) { if (cipher->key == NULL) {
if (alloc_key(cipher) < 0) { if (alloc_key(cipher) < 0) {
return -1; return -1;
} }
BF_set_key(cipher->key, 16, key); BF_set_key(cipher->key, 16, key);
} }
cipher->IV = IV;
return 0; return 0;
} }
static void blowfish_encrypt(struct crypto_struct *cipher, void *in, static void blowfish_encrypt(struct crypto_struct *cipher, void *in,
void *out, unsigned long len, void *IV) { void *out, unsigned long len) {
BF_cbc_encrypt(in, out, len, cipher->key, IV, BF_ENCRYPT); BF_cbc_encrypt(in, out, len, cipher->key, cipher->IV, BF_ENCRYPT);
} }
static void blowfish_decrypt(struct crypto_struct *cipher, void *in, static void blowfish_decrypt(struct crypto_struct *cipher, void *in,
void *out, unsigned long len, void *IV) { void *out, unsigned long len) {
BF_cbc_encrypt(in, out, len, cipher->key, IV, BF_DECRYPT); BF_cbc_encrypt(in, out, len, cipher->key, cipher->IV, BF_DECRYPT);
} }
#endif /* HAS_BLOWFISH */ #endif /* HAS_BLOWFISH */
#ifdef HAS_AES #ifdef HAS_AES
static int aes_set_encrypt_key(struct crypto_struct *cipher, void *key) { static int aes_set_encrypt_key(struct crypto_struct *cipher, void *key,
void *IV) {
if (cipher->key == NULL) { if (cipher->key == NULL) {
if (alloc_key(cipher) < 0) { if (alloc_key(cipher) < 0) {
return -1; return -1;
@ -267,10 +268,11 @@ static int aes_set_encrypt_key(struct crypto_struct *cipher, void *key) {
return -1; return -1;
} }
} }
cipher->IV=IV;
return 0; return 0;
} }
static int aes_set_decrypt_key(struct crypto_struct *cipher, void *key) { static int aes_set_decrypt_key(struct crypto_struct *cipher, void *key,
void *IV) {
if (cipher->key == NULL) { if (cipher->key == NULL) {
if (alloc_key(cipher) < 0) { if (alloc_key(cipher) < 0) {
return -1; return -1;
@ -280,18 +282,18 @@ static int aes_set_decrypt_key(struct crypto_struct *cipher, void *key) {
return -1; return -1;
} }
} }
cipher->IV=IV;
return 0; return 0;
} }
static void aes_encrypt(struct crypto_struct *cipher, void *in, void *out, static void aes_encrypt(struct crypto_struct *cipher, void *in, void *out,
unsigned long len, void *IV) { unsigned long len) {
AES_cbc_encrypt(in, out, len, cipher->key, IV, AES_ENCRYPT); AES_cbc_encrypt(in, out, len, cipher->key, cipher->IV, AES_ENCRYPT);
} }
static void aes_decrypt(struct crypto_struct *cipher, void *in, void *out, static void aes_decrypt(struct crypto_struct *cipher, void *in, void *out,
unsigned long len, void *IV) { unsigned long len) {
AES_cbc_encrypt(in, out, len, cipher->key, IV, AES_DECRYPT); AES_cbc_encrypt(in, out, len, cipher->key, cipher->IV, AES_DECRYPT);
} }
#ifndef BROKEN_AES_CTR #ifndef BROKEN_AES_CTR
@ -305,7 +307,7 @@ static void aes_decrypt(struct crypto_struct *cipher, void *in, void *out,
* @param len[in] must be a multiple of AES128 block size. * @param len[in] must be a multiple of AES128 block size.
*/ */
static void aes_ctr128_encrypt(struct crypto_struct *cipher, void *in, void *out, static void aes_ctr128_encrypt(struct crypto_struct *cipher, void *in, void *out,
unsigned long len, void *IV) { unsigned long len) {
unsigned char tmp_buffer[128/8]; unsigned char tmp_buffer[128/8];
unsigned int num=0; unsigned int num=0;
/* Some things are special with ctr128 : /* Some things are special with ctr128 :
@ -314,13 +316,13 @@ static void aes_ctr128_encrypt(struct crypto_struct *cipher, void *in, void *out
* Same for num, which is being used to store the current offset in blocksize in CTR * Same for num, which is being used to store the current offset in blocksize in CTR
* function. * function.
*/ */
AES_ctr128_encrypt(in, out, len, cipher->key, IV, tmp_buffer, &num); AES_ctr128_encrypt(in, out, len, cipher->key, cipher->IV, tmp_buffer, &num);
} }
#endif /* BROKEN_AES_CTR */ #endif /* BROKEN_AES_CTR */
#endif /* HAS_AES */ #endif /* HAS_AES */
#ifdef HAS_DES #ifdef HAS_DES
static int des3_set_key(struct crypto_struct *cipher, void *key) { static int des3_set_key(struct crypto_struct *cipher, void *key,void *IV) {
if (cipher->key == NULL) { if (cipher->key == NULL) {
if (alloc_key(cipher) < 0) { if (alloc_key(cipher) < 0) {
return -1; return -1;
@ -333,55 +335,55 @@ static int des3_set_key(struct crypto_struct *cipher, void *key) {
DES_set_key_unchecked((void*)((uint8_t*)key + 8), (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule))); DES_set_key_unchecked((void*)((uint8_t*)key + 8), (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)));
DES_set_key_unchecked((void*)((uint8_t*)key + 16), (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule))); DES_set_key_unchecked((void*)((uint8_t*)key + 16), (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)));
} }
cipher->IV=IV;
return 0; return 0;
} }
static void des3_encrypt(struct crypto_struct *cipher, void *in, static void des3_encrypt(struct crypto_struct *cipher, void *in,
void *out, unsigned long len, void *IV) { void *out, unsigned long len) {
DES_ede3_cbc_encrypt(in, out, len, cipher->key, DES_ede3_cbc_encrypt(in, out, len, cipher->key,
(void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)), (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)),
(void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)), (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)),
IV, 1); cipher->IV, 1);
} }
static void des3_decrypt(struct crypto_struct *cipher, void *in, static void des3_decrypt(struct crypto_struct *cipher, void *in,
void *out, unsigned long len, void *IV) { void *out, unsigned long len) {
DES_ede3_cbc_encrypt(in, out, len, cipher->key, DES_ede3_cbc_encrypt(in, out, len, cipher->key,
(void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)), (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)),
(void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)), (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)),
IV, 0); cipher->IV, 0);
} }
static void des3_1_encrypt(struct crypto_struct *cipher, void *in, static void des3_1_encrypt(struct crypto_struct *cipher, void *in,
void *out, unsigned long len, void *IV) { void *out, unsigned long len) {
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
ssh_print_hexa("Encrypt IV before", IV, 24); ssh_print_hexa("Encrypt IV before", cipher->IV, 24);
#endif #endif
DES_ncbc_encrypt(in, out, len, cipher->key, IV, 1); DES_ncbc_encrypt(in, out, len, cipher->key, cipher->IV, 1);
DES_ncbc_encrypt(out, in, len, (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)), DES_ncbc_encrypt(out, in, len, (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)),
(void*)((uint8_t*)IV + 8), 0); (void*)((uint8_t*)cipher->IV + 8), 0);
DES_ncbc_encrypt(in, out, len, (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)), DES_ncbc_encrypt(in, out, len, (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)),
(void*)((uint8_t*)IV + 16), 1); (void*)((uint8_t*)cipher->IV + 16), 1);
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
ssh_print_hexa("Encrypt IV after", IV, 24); ssh_print_hexa("Encrypt IV after", cipher->IV, 24);
#endif #endif
} }
static void des3_1_decrypt(struct crypto_struct *cipher, void *in, static void des3_1_decrypt(struct crypto_struct *cipher, void *in,
void *out, unsigned long len, void *IV) { void *out, unsigned long len) {
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
ssh_print_hexa("Decrypt IV before", IV, 24); ssh_print_hexa("Decrypt IV before", cipher->IV, 24);
#endif #endif
DES_ncbc_encrypt(in, out, len, (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)), DES_ncbc_encrypt(in, out, len, (void*)((uint8_t*)cipher->key + 2 * sizeof(DES_key_schedule)),
IV, 0); cipher->IV, 0);
DES_ncbc_encrypt(out, in, len, (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)), DES_ncbc_encrypt(out, in, len, (void*)((uint8_t*)cipher->key + sizeof(DES_key_schedule)),
(void*)((uint8_t*)IV + 8), 1); (void*)((uint8_t*)cipher->IV + 8), 1);
DES_ncbc_encrypt(in, out, len, cipher->key, (void*)((uint8_t*)IV + 16), 0); DES_ncbc_encrypt(in, out, len, cipher->key, (void*)((uint8_t*)cipher->IV + 16), 0);
#ifdef DEBUG_CRYPTO #ifdef DEBUG_CRYPTO
ssh_print_hexa("Decrypt IV after", IV, 24); ssh_print_hexa("Decrypt IV after", cipher->IV, 24);
#endif #endif
} }
@ -400,6 +402,7 @@ static struct crypto_struct ssh_ciphertab[] = {
8, 8,
sizeof (BF_KEY), sizeof (BF_KEY),
NULL, NULL,
NULL,
128, 128,
blowfish_set_key, blowfish_set_key,
blowfish_set_key, blowfish_set_key,
@ -414,6 +417,7 @@ static struct crypto_struct ssh_ciphertab[] = {
16, 16,
sizeof(AES_KEY), sizeof(AES_KEY),
NULL, NULL,
NULL,
128, 128,
aes_set_encrypt_key, aes_set_encrypt_key,
aes_set_encrypt_key, aes_set_encrypt_key,
@ -425,6 +429,7 @@ static struct crypto_struct ssh_ciphertab[] = {
16, 16,
sizeof(AES_KEY), sizeof(AES_KEY),
NULL, NULL,
NULL,
192, 192,
aes_set_encrypt_key, aes_set_encrypt_key,
aes_set_encrypt_key, aes_set_encrypt_key,
@ -436,6 +441,7 @@ static struct crypto_struct ssh_ciphertab[] = {
16, 16,
sizeof(AES_KEY), sizeof(AES_KEY),
NULL, NULL,
NULL,
256, 256,
aes_set_encrypt_key, aes_set_encrypt_key,
aes_set_encrypt_key, aes_set_encrypt_key,
@ -448,6 +454,7 @@ static struct crypto_struct ssh_ciphertab[] = {
16, 16,
sizeof(AES_KEY), sizeof(AES_KEY),
NULL, NULL,
NULL,
128, 128,
aes_set_encrypt_key, aes_set_encrypt_key,
aes_set_decrypt_key, aes_set_decrypt_key,
@ -459,6 +466,7 @@ static struct crypto_struct ssh_ciphertab[] = {
16, 16,
sizeof(AES_KEY), sizeof(AES_KEY),
NULL, NULL,
NULL,
192, 192,
aes_set_encrypt_key, aes_set_encrypt_key,
aes_set_decrypt_key, aes_set_decrypt_key,
@ -470,6 +478,7 @@ static struct crypto_struct ssh_ciphertab[] = {
16, 16,
sizeof(AES_KEY), sizeof(AES_KEY),
NULL, NULL,
NULL,
256, 256,
aes_set_encrypt_key, aes_set_encrypt_key,
aes_set_decrypt_key, aes_set_decrypt_key,
@ -483,6 +492,7 @@ static struct crypto_struct ssh_ciphertab[] = {
8, 8,
sizeof(DES_key_schedule) * 3, sizeof(DES_key_schedule) * 3,
NULL, NULL,
NULL,
192, 192,
des3_set_key, des3_set_key,
des3_set_key, des3_set_key,
@ -494,6 +504,7 @@ static struct crypto_struct ssh_ciphertab[] = {
8, 8,
sizeof(DES_key_schedule) * 3, sizeof(DES_key_schedule) * 3,
NULL, NULL,
NULL,
192, 192,
des3_set_key, des3_set_key,
des3_set_key, des3_set_key,
@ -506,6 +517,7 @@ static struct crypto_struct ssh_ciphertab[] = {
0, 0,
0, 0,
NULL, NULL,
NULL,
0, 0,
NULL, NULL,
NULL, NULL,