| 75 | } |
| 76 | |
| 77 | GANDIVA_EXPORT |
| 78 | int32_t aes_decrypt(const char* ciphertext, int32_t ciphertext_len, const char* key, |
| 79 | int32_t key_len, unsigned char* plaintext) { |
| 80 | int32_t plaintext_len = 0; |
| 81 | int32_t len = 0; |
| 82 | EVP_CIPHER_CTX* de_ctx = EVP_CIPHER_CTX_new(); |
| 83 | const EVP_CIPHER* cipher_algo = get_cipher_algo(key_len); |
| 84 | |
| 85 | if (!de_ctx) { |
| 86 | throw std::runtime_error("could not create a new evp cipher ctx for decryption"); |
| 87 | } |
| 88 | |
| 89 | if (!EVP_DecryptInit_ex(de_ctx, cipher_algo, nullptr, |
| 90 | reinterpret_cast<const unsigned char*>(key), nullptr)) { |
| 91 | throw std::runtime_error("could not initialize evp cipher ctx for decryption"); |
| 92 | } |
| 93 | |
| 94 | if (!EVP_DecryptUpdate(de_ctx, plaintext, &len, |
| 95 | reinterpret_cast<const unsigned char*>(ciphertext), |
| 96 | ciphertext_len)) { |
| 97 | throw std::runtime_error("could not update evp cipher ctx for decryption"); |
| 98 | } |
| 99 | |
| 100 | plaintext_len += len; |
| 101 | |
| 102 | if (!EVP_DecryptFinal_ex(de_ctx, plaintext + len, &len)) { |
| 103 | throw std::runtime_error("could not finish evp cipher ctx for decryption"); |
| 104 | } |
| 105 | |
| 106 | plaintext_len += len; |
| 107 | |
| 108 | EVP_CIPHER_CTX_free(de_ctx); |
| 109 | return plaintext_len; |
| 110 | } |
| 111 | |
| 112 | } // namespace gandiva |