- EVP_CIPHER_CTX_init(crypto_context->encryption_context);
- EVP_CIPHER_CTX_init(crypto_context->decryption_context);
- if (EVP_EncryptInit_ex(crypto_context->encryption_context, EVP_aes_256_cbc(), NULL, key, aes_iv)
- != 1
- || EVP_DecryptInit_ex(crypto_context->decryption_context, EVP_aes_256_cbc(), NULL, key,
- aes_iv)
- != 1)
+ aes_key_t *aes_ctx = (aes_key_t *) malloc(sizeof(aes_key_t));
+
+ if (!(aes_ctx->encryption_context = EVP_CIPHER_CTX_new())) {
+ return NULL;
+ }
+ if (!(aes_ctx->decryption_context = EVP_CIPHER_CTX_new())) {
+ EVP_CIPHER_CTX_free(aes_ctx->encryption_context);
+ return NULL;
+ }
+
+ EVP_CIPHER_CTX_init(aes_ctx->encryption_context);
+ EVP_CIPHER_CTX_init(aes_ctx->decryption_context);
+ if (EVP_EncryptInit_ex(aes_ctx->encryption_context, EVP_aes_256_cbc(), NULL, ukey, aes_iv) != 1
+ || EVP_DecryptInit_ex(aes_ctx->decryption_context, EVP_aes_256_cbc(), NULL, ukey, aes_iv) != 1)