Search code examples
cencryptionopensslaes

OpenSSL AES Segmentation Fault on Encrypt Update


I have string with set size and trying to AES encrypt it, but I get Segmentation Fault at EVP_EncryptUpdate

size_t dec_len = 20;
char *dec = malloc(dec_len + 1);

//Fill dec
...
//Encrypt

EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
unsigned char *key = (unsigned char *)" no ";
EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, &key, NULL);
EVP_CIPHER_CTX_set_padding(ctx, 0);
unsigned char *ciphertext;
int ciphertext_len;
EVP_EncryptUpdate(ctx, ciphertext, &ciphertext_len, dec, dec_len);
EVP_EncryptFinal_ex(ctx, ciphertext + ciphertext_len, &ciphertext_len);
EVP_CIPHER_CTX_free(ctx);

I have no idea what's causing this. Thank you.


Solution

  • Solved by allocating ciphertext variable.