fix: AES ECB mode in Data processor not working at all (#1986)

Fix the AES ECB mode in the data processor along with some other misc
fixes:
- Fixed nullpointer node not working
- Fixed crypto module incorrectly using mbedtls api
- Fixed crypto module ignoring mbedtls errors
- Fixed silently ignoring of errors in AES node
This commit is contained in:
Justus Garbe
2024-12-05 20:56:43 +01:00
committed by GitHub
parent 8acdc19be4
commit 3c73f88a52
4 changed files with 82 additions and 22 deletions

View File

@@ -1,9 +1,13 @@
#include <algorithm>
#include <hex/api/content_registry.hpp>
#include <hex/api/localization_manager.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/data_processor/node.hpp>
#include <mbedtls/cipher.h>
#include <mbedtls/error.h>
#include <nlohmann/json.hpp>
namespace hex::plugin::builtin {
@@ -25,6 +29,9 @@ namespace hex::plugin::builtin {
}
void process() override {
const auto mode = static_cast<crypt::AESMode>(m_mode);
const auto keyLength = static_cast<crypt::KeyLength>(m_keyLength);
const auto &key = this->getBufferOnInput(0);
const auto &iv = this->getBufferOnInput(1);
const auto &nonce = this->getBufferOnInput(2);
@@ -38,12 +45,34 @@ namespace hex::plugin::builtin {
std::array<u8, 8> ivData = { 0 }, nonceData = { 0 };
std::copy(iv.begin(), iv.end(), ivData.begin());
std::copy(nonce.begin(), nonce.end(), nonceData.begin());
if (mode != crypt::AESMode::ECB) {
if (iv.empty())
throwNodeError("IV cannot be empty");
auto output = crypt::aesDecrypt(static_cast<crypt::AESMode>(m_mode), static_cast<crypt::KeyLength>(m_keyLength), key, nonceData, ivData, input);
if (nonce.empty())
throwNodeError("Nonce cannot be empty");
this->setBufferOnOutput(4, output);
std::ranges::copy(iv, ivData.begin());
std::ranges::copy(nonce, nonceData.begin());
}
auto output = crypt::aesDecrypt(mode, keyLength, key, nonceData, ivData, input);
if (!output) {
switch (output.error()) {
case CRYPTO_ERROR_INVALID_KEY_LENGTH:
throwNodeError("Invalid key length");
case CRYPTO_ERROR_INVALID_MODE:
throwNodeError("Invalid mode");
default: {
std::array<char, 128> errorBuffer = { 0 };
mbedtls_strerror(output.error(), errorBuffer.data(), errorBuffer.size());
throwNodeError(std::string(errorBuffer.data()));
}
}
}
this->setBufferOnOutput(4, output.value());
}
void store(nlohmann::json &j) const override {