From 49d3fe65a3f69a2ccf7c87d9ab4de6e85c351d68 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 9 Aug 2023 20:01:29 +0200 Subject: [PATCH] fix: Allow converting very long hex strings to bytes --- lib/libimhex/source/helpers/crypto.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/libimhex/source/helpers/crypto.cpp b/lib/libimhex/source/helpers/crypto.cpp index 9d9498c0b..df3725f90 100644 --- a/lib/libimhex/source/helpers/crypto.cpp +++ b/lib/libimhex/source/helpers/crypto.cpp @@ -390,16 +390,22 @@ namespace hex::crypt { std::vector decode16(const std::string &input) { std::vector output(input.length() / 2, 0x00); + mbedtls_mpi ctx; mbedtls_mpi_init(&ctx); ON_SCOPE_EXIT { mbedtls_mpi_free(&ctx); }; - if (mbedtls_mpi_read_string(&ctx, 16, input.c_str())) - return {}; + constexpr static auto BufferSize = 0x100; + for (size_t offset = 0; offset < input.size(); offset += BufferSize) { + std::string inputPart = input.substr(offset, std::min(BufferSize, input.size() - offset)); + if (mbedtls_mpi_read_string(&ctx, 16, inputPart.c_str())) + return {}; - if (mbedtls_mpi_write_binary(&ctx, output.data(), output.size())) - return {}; + auto size = std::min(BufferSize / 2, input.size() - offset); + if (mbedtls_mpi_write_binary(&ctx, output.data() + offset / 2, size) != 0) + return {}; + } return output; }