mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-10 18:28:10 +09:00
Improve packet_encrypt().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@480 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
@@ -81,26 +81,37 @@ int packet_decrypt(SSH_SESSION *session, void *data,u32 len) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char * packet_encrypt(SSH_SESSION *session,void *data,u32 len){
|
unsigned char *packet_encrypt(SSH_SESSION *session, void *data, u32 len) {
|
||||||
struct crypto_struct *crypto;
|
struct crypto_struct *crypto = NULL;
|
||||||
HMACCTX ctx;
|
HMACCTX ctx = NULL;
|
||||||
char *out;
|
char *out = NULL;
|
||||||
unsigned int finallen;
|
unsigned int finallen;
|
||||||
u32 seq=ntohl(session->send_seq);
|
u32 seq;
|
||||||
if(!session->current_crypto)
|
|
||||||
|
if (!session->current_crypto) {
|
||||||
return NULL; /* nothing to do here */
|
return NULL; /* nothing to do here */
|
||||||
crypto= session->current_crypto->out_cipher;
|
}
|
||||||
ssh_log(session,SSH_LOG_PACKET,"encrypting packet with seq num: %d, len: %d",session->send_seq,len);
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
|
||||||
crypto->set_encrypt_key(crypto,session->current_crypto->encryptkey,session->current_crypto->encryptIV);
|
|
||||||
#elif defined HAVE_LIBCRYPTO
|
|
||||||
crypto->set_encrypt_key(crypto,session->current_crypto->encryptkey);
|
|
||||||
#endif
|
|
||||||
out = malloc(len);
|
out = malloc(len);
|
||||||
if (out == NULL) {
|
if (out == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if(session->version==2){
|
|
||||||
|
seq = ntohl(session->send_seq);
|
||||||
|
crypto = session->current_crypto->out_cipher;
|
||||||
|
|
||||||
|
ssh_log(session, SSH_LOG_PACKET,
|
||||||
|
"Encrypting packet with seq num: %d, len: %d",
|
||||||
|
session->send_seq,len);
|
||||||
|
|
||||||
|
#ifdef HAVE_LIBGCRYPT
|
||||||
|
crypto->set_encrypt_key(crypto, session->current_crypto->encryptkey,
|
||||||
|
session->current_crypto->encryptIV);
|
||||||
|
#elif defined HAVE_LIBCRYPTO
|
||||||
|
crypto->set_encrypt_key(crypto, session->current_crypto->encryptkey);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (session->version == 2) {
|
||||||
ctx = hmac_init(session->current_crypto->encryptMAC,20,HMAC_SHA1);
|
ctx = hmac_init(session->current_crypto->encryptMAC,20,HMAC_SHA1);
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
SAFE_FREE(out);
|
SAFE_FREE(out);
|
||||||
@@ -110,23 +121,29 @@ unsigned char * packet_encrypt(SSH_SESSION *session,void *data,u32 len){
|
|||||||
hmac_update(ctx,data,len);
|
hmac_update(ctx,data,len);
|
||||||
hmac_final(ctx,session->current_crypto->hmacbuf,&finallen);
|
hmac_final(ctx,session->current_crypto->hmacbuf,&finallen);
|
||||||
#ifdef DEBUG_CRYPTO
|
#ifdef DEBUG_CRYPTO
|
||||||
ssh_print_hexa("mac :",data,len);
|
ssh_print_hexa("mac: ",data,len);
|
||||||
if(finallen!=20)
|
if (finallen != 20) {
|
||||||
printf("Final len is %d\n",finallen);
|
printf("Final len is %d\n",finallen);
|
||||||
ssh_print_hexa("packet hmac",session->current_crypto->hmacbuf,20);
|
}
|
||||||
|
ssh_print_hexa("Packet hmac", session->current_crypto->hmacbuf, 20);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
crypto->cbc_encrypt(crypto,data,out,len);
|
crypto->cbc_encrypt(crypto, data, out, len);
|
||||||
#elif defined HAVE_LIBCRYPTO
|
#elif defined HAVE_LIBCRYPTO
|
||||||
crypto->cbc_encrypt(crypto,data,out,len,session->current_crypto->encryptIV);
|
crypto->cbc_encrypt(crypto, data, out, len,
|
||||||
|
session->current_crypto->encryptIV);
|
||||||
#endif
|
#endif
|
||||||
memcpy(data,out,len);
|
|
||||||
memset(out,0,len);
|
memcpy(data, out, len);
|
||||||
free(out);
|
memset(out, 0, len);
|
||||||
if(session->version==2)
|
SAFE_FREE(out);
|
||||||
|
|
||||||
|
if (session->version == 2) {
|
||||||
return session->current_crypto->hmacbuf;
|
return session->current_crypto->hmacbuf;
|
||||||
else
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user