Hi to everyone, I'm writing a little program that encrypt/decrypt some text but I've found some troubles using the OpenSSL libraries... When I use between the encrypt() function and the decrypt() function I receive some strange errors or segmentation faults.. I copy my source here so I can explain better:
-----------------CODE--------------------- #include <string.h> #include <openssl/evp.h> #include <openssl/pem.h> #include <openssl/rsa.h> void printbyte(char b) { char c; c = b; c = c >> 4; c = c & 15; fprintf(stderr,"%X", c); c = b; c = c & 15; fprintf(stderr,"%X:", c); } void read_shared_key(char* kab) { int n; FILE* f = NULL; strcpy(kab,"30A75E9E5D962371"); //f = fopen("key.k", "rb"); //n = fread(kab, sizeof(kab), 256, f); //fclose(f); } int encrypt(char* key, char* plain, char* crypto) { char *msg = (char*) malloc(strlen(plain)); strcpy(msg,plain); char *plaintext, *ciphertext; char k[256]; /* chiave di cifratura */ int nc, /* numero di byte [de]crittati ad ogni passo*/ nctot, /* numero totale di byte crittati */ i, /* indice */ ct_len, /* lunghezza del buffer */ ct_ptr, msg_ptr, /* puntatore alla prima posizione libera del buffer */ msg_len, /* lunghezza del messaggio */ n, /* numero di byte che si cifra di volta in volta*/ ret = 0; strcpy(crypto, ""); /* allocazione del contesto */ EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)malloc(sizeof(EVP_CIPHER_CTX)); /* inizializzazione del contesto */ EVP_CIPHER_CTX_init(ctx); /* setup del contesto per la cifratura */ EVP_EncryptInit(ctx, EVP_des_ecb(), NULL, NULL); /* selezione random della chiave */ strcpy(k,key); /* impostazione della chiave */ EVP_EncryptInit(ctx, NULL, k, NULL); /* allocazione del buffer per ciphertext */ msg_len = strlen(msg)+1; ct_len = msg_len + EVP_CIPHER_CTX_block_size(ctx); ciphertext = (char *)malloc(ct_len); /* cifratura di n byte alla volta */ n = 10; nc = 0; nctot = 0; ct_ptr = 0; msg_ptr =0; for (i = 0; i < msg_len / n; i++) { EVP_EncryptUpdate(ctx, &ciphertext[ct_ptr], &nc, &msg[msg_ptr], n); ct_ptr += nc; msg_ptr += n; nctot += nc; } if ( msg_len % n ) { EVP_EncryptUpdate(ctx, &ciphertext[ct_ptr], &nc, &msg[msg_ptr], msg_len % n); ct_ptr += nc; msg_ptr += msg_len % n; nctot += nc; } //EVP_EncryptUpdate(ctx, &ciphertext[ct_ptr], &nc, &msg[msg_ptr], msg_len); ret = EVP_EncryptFinal(ctx, &ciphertext[ct_ptr], &nc); //fprintf(stderr,"\nENC RET: = %d\n", ret); nctot += nc; //strcpy(crypto, ciphertext); memcpy(crypto, ciphertext, nctot); //fprintf(stderr,"\nENCRYPT\nP: %s \nC: %s\nK: %s",plain, crypto, k); //fprintf(stderr,"\nCT_LEN: = %d\n", msg_len + EVP_CIPHER_CTX_block_size(ctx)); int j = 0; for (j = 0; j < ct_len - 1; j++) printbyte(ciphertext[j]); printbyte(ciphertext[ct_len-1]); printf("\n"); return nctot; } int decrypt(char* key, char* plain, char* crypto) { char *plaintext, *ciphertext; strcpy(plain, ""); char k[256]; /* chiave di cifratura */ int nc, /* numero di byte [de]crittati ad ogni passo*/ nctot, /* numero totale di byte crittati */ i, /* indice */ ct_len, /* lunghezza del buffer */ ct_ptr, msg_ptr, /* puntatore alla prima posizione libera del buffer */ msg_len, /* lunghezza del messaggio */ n, /* numero di byte che si cifra di volta in volta*/ ret; ct_len = strlen(crypto); /* allocazione del contesto */ EVP_CIPHER_CTX *ctxd = (EVP_CIPHER_CTX *)malloc(sizeof(EVP_CIPHER_CTX)); /* inizializzazione del contesto */ EVP_CIPHER_CTX_init(ctxd); /* setup del contesto per la cifratura */ EVP_DecryptInit(ctxd, EVP_des_ecb(), NULL, NULL); /* selezione random della chiave */ strcpy(k,key); /* impostazione della chiave */ EVP_DecryptInit(ctxd, NULL, k, NULL); plaintext = (char *)malloc(ct_len); bzero(plaintext, ct_len); /* Inizializzazione contesto decifratura */ EVP_CIPHER_CTX_init(ctxd); EVP_DecryptInit(ctxd, EVP_des_ecb(), k, NULL); /*decifratura */ nc = 0; ct_ptr = 0; ct_len = 256; fprintf(stderr,"OK"); EVP_DecryptUpdate(ctxd, &plaintext[ct_ptr], &nc, &ciphertext[ct_ptr], ct_len); fprintf(stderr,"OK"); if (ERR_peek_error()) { ERR_load_crypto_strings(); ERR_print_errors_fp(stderr); //ERR_free_strings(); } ret = EVP_DecryptFinal(ctxd, &plaintext[ct_ptr], &nc); //fprintf(stderr,"\nDEC RET: = %d\n", ret); //strcpy(plain, plaintext); memcpy(plain, plaintext, strlen(plaintext)); //fprintf(stderr,"\nDECRYPT\nP: %s \nC: %s\nK: %s\n",plain, crypto, k); //fprintf(stderr,"Cipher: "); int j = 0; for (j = 0; j < strlen(plain); j++) printf("%c:", plaintext[j]); printf("%c\n", plaintext[strlen(plain)]); printf("\n"); return nc; } int main() { char plain[256]; char crypto[256]; char kab[256]; strcpy(plain, "Test"); read_shared_key(kab); encrypt(kab, plain, crypto); printf("ERROR????"); // <<<<----------------- decrypt(kab, plain, crypto); } -----------------------//CODE---------------------- The original project was a server/client architecture that was giving the same error invoking the "send()" on server-side, crashing with segfault on the client (decrypt) side and a child termination on the server (encrypt) side... Now, in this case, if I don't put anything between "encrypt(kab, plain, crypto);" and "decrypt(kab, plain, crypto);", all goes well, giving to the screen the right thing; if I put anything (in this case a simple printf) it give me a segfault...I really don't understand... Thanks to anyone, Cheers, -Spike -- View this message in context: http://www.nabble.com/DecryptUpdate-%22Segmentation-Fault%22-tp25335141p25335141.html Sent from the OpenSSL - User mailing list archive at Nabble.com. ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager majord...@openssl.org