1
0
mirror of synced 2025-01-29 19:17:28 +01:00

Switch from openssl/libcrypto to mbedtls

This commit is contained in:
WerWolv 2021-02-02 23:11:23 +01:00
parent fa352b6917
commit 785ecb8a78
14 changed files with 124 additions and 128 deletions

View File

@ -32,7 +32,7 @@ jobs:
libglfw3-dev \
libglm-dev \
libmagic-dev \
libssl-dev \
libmbedtls-dev \
libcapstone-dev \
python3-dev \
libfreetype-dev \
@ -92,7 +92,7 @@ jobs:
mingw-w64-${{ matrix.arch }}-glfw
mingw-w64-${{ matrix.arch }}-glm
mingw-w64-${{ matrix.arch }}-file
mingw-w64-${{ matrix.arch }}-openssl
mingw-w64-${{ matrix.arch }}-mbedtls
mingw-w64-${{ matrix.arch }}-polly
mingw-w64-${{ matrix.arch }}-python
mingw-w64-${{ matrix.arch }}-dlfcn
@ -155,7 +155,14 @@ jobs:
run: |
mkdir build
cd build
CC=$(brew --prefix llvm)/bin/clang CXX=$(brew --prefix llvm)/bin/clang++ PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig":"$(brew --prefix)/lib/pkgconfig" cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCREATE_BUNDLE=ON -DCREATE_PACKAGE=ON ..
CC=$(brew --prefix llvm)/bin/clang \
CXX=$(brew --prefix llvm)/bin/clang++ \
PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig":"$(brew --prefix)/lib/pkgconfig" \
cmake \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCREATE_BUNDLE=ON \
-DCREATE_PACKAGE=ON \
..
make -j 4 package
- name: 📦 Upload DMG

View File

@ -29,7 +29,7 @@ add_subdirectory(external/llvm)
add_subdirectory(plugins/libimhex)
# Add include directories
include_directories(include ${CRYPTO_INCLUDE_DIRS} ${CAPSTONE_INCLUDE_DIRS} ${MAGIC_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
include_directories(include ${MBEDTLS_INCLUDE_DIRS} ${CAPSTONE_INCLUDE_DIRS} ${MAGIC_INCLUDE_DIRS} ${Python_INCLUDE_DIRS})
addVersionDefines()
configurePackageCreation()
@ -66,12 +66,12 @@ add_executable(imhex ${application_type}
)
set_target_properties(imhex PROPERTIES CXX_VISIBILITY_PRESET hidden)
target_link_directories(imhex PRIVATE ${CRYPTO_LIBRARY_DIRS} ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS})
target_link_directories(imhex PRIVATE ${MBEDTLS_LIBRARY_DIRS} ${CAPSTONE_LIBRARY_DIRS} ${MAGIC_LIBRARY_DIRS})
if (WIN32)
target_link_libraries(imhex libdl.a libmagic.a libgnurx.a libtre.a libintl.a libiconv.a libshlwapi.a libcrypto.a libcapstone.a LLVMDemangle libimhex ${Python_LIBRARIES} wsock32 ws2_32)
target_link_libraries(imhex libdl.a libmagic.a libgnurx.a libtre.a libintl.a libiconv.a libshlwapi.a libmbedx509.a libmbedcrypto.a libcapstone.a LLVMDemangle libimhex ${Python_LIBRARIES} wsock32 ws2_32)
elseif (UNIX)
target_link_libraries(imhex magic crypto ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} dl)
target_link_libraries(imhex magic mbedtls ${CMAKE_DL_LIBS} capstone LLVMDemangle libimhex ${Python_LIBRARIES} dl)
endif()
createPackage()

View File

@ -36,11 +36,11 @@ macro(findLibraries)
# Find packages
find_package(PkgConfig REQUIRED)
pkg_search_module(CRYPTO libcrypto)
if(NOT CRYPTO_FOUND)
find_library(CRYPTO crypto REQUIRED)
pkg_search_module(MBEDTLS libmbedtls)
if(NOT MBEDTLS_FOUND)
find_library(MBEDTLS mbedtls REQUIRED)
else()
set(CRYPTO_INCLUDE_DIRS ${CRYPTO_INCLUDEDIR})
set(MBEDTLS_INCLUDE_DIRS ${MBEDTLS_INCLUDEDIR})
endif()
pkg_search_module(CAPSTONE REQUIRED capstone)

2
dist/Brewfile vendored
View File

@ -1,5 +1,5 @@
brew "glfw3"
brew "openssl@1.1"
brew "mbedtls"
brew "capstone"
brew "nlohmann-json"
brew "glm"

View File

@ -25,7 +25,7 @@ RDEPEND="${DEPEND}
${PYTHON_DEPS}
media-libs/glfw
sys-apps/file
dev-libs/openssl
dev-libs/mbedtls
dev-libs/capstone
sys-devel/llvm
dev-cpp/nlohmann_json

View File

@ -5,7 +5,7 @@ pacman -S --needed \
gcc \
glfw \
file \
openssl \
mbedtls \
capstone \
glm \
python3 \

View File

@ -19,7 +19,7 @@ apt install \
libglfw3-dev \
libglm-dev \
libjsoncpp-dev \
libssl-dev \
libmbedtls-dev \
libstdc++-10-dev \
python3-dev \
libfreetype-dev

View File

@ -8,6 +8,6 @@ dnf install \
glfw-devel \
glm-devel \
mesa-libGL-devel \
openssl-devel \
mbedtls-devel \
python-devel \
freetype-devel

View File

@ -8,7 +8,7 @@ pacman -S --needed \
mingw-w64-x86_64-glfw \
mingw-w64-x86_64-glm \
mingw-w64-x86_64-file \
mingw-w64-x86_64-openssl \
mingw-w64-x86_64-mbedtls \
mingw-w64-x86_64-polly \
mingw-w64-x86_64-python \
mingw-w64-x86_64-freetype \

View File

@ -7,20 +7,22 @@
#include <string>
#include <vector>
namespace hex {
namespace hex::prv { class Provider; }
namespace prv { class Provider; }
namespace hex::crypt {
void initialize();
void exit();
u16 crc16(prv::Provider* &data, u64 offset, size_t size, u16 polynomial, u16 init);
u32 crc32(prv::Provider* &data, u64 offset, size_t size, u32 polynomial, u32 init);
std::array<u32, 4> md4(prv::Provider* &data, u64 offset, size_t size);
std::array<u32, 4> md5(prv::Provider* &data, u64 offset, size_t size);
std::array<u32, 5> sha1(prv::Provider* &data, u64 offset, size_t size);
std::array<u32, 7> sha224(prv::Provider* &data, u64 offset, size_t size);
std::array<u32, 8> sha256(prv::Provider* &data, u64 offset, size_t size);
std::array<u32, 12> sha384(prv::Provider* &data, u64 offset, size_t size);
std::array<u32, 16> sha512(prv::Provider* &data, u64 offset, size_t size);
std::array<u8, 16> md5(prv::Provider* &data, u64 offset, size_t size);
std::array<u8, 20> sha1(prv::Provider* &data, u64 offset, size_t size);
std::array<u8, 28> sha224(prv::Provider* &data, u64 offset, size_t size);
std::array<u8, 32> sha256(prv::Provider* &data, u64 offset, size_t size);
std::array<u8, 48> sha384(prv::Provider* &data, u64 offset, size_t size);
std::array<u8, 64> sha512(prv::Provider* &data, u64 offset, size_t size);
std::vector<u8> decode64(const std::vector<u8> &input);
std::vector<u8> encode64(const std::vector<u8> &input);

View File

@ -22,7 +22,7 @@ namespace hex {
u64 m_hashRegion[2] = { 0 };
bool m_shouldMatchSelection = false;
static constexpr const char* HashFunctionNames[] = { "CRC16", "CRC32", "MD4", "MD5", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512" };
static constexpr const char* HashFunctionNames[] = { "CRC16", "CRC32", "MD5", "SHA-1", "SHA-224", "SHA-256", "SHA-384", "SHA-512" };
};
}

View File

@ -2,16 +2,16 @@
#include <hex/providers/provider.hpp>
#include <openssl/md4.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <mbedtls/base64.h>
#include <mbedtls/md5.h>
#include <mbedtls/sha1.h>
#include <mbedtls/sha256.h>
#include <mbedtls/sha512.h>
#include <array>
#include <span>
namespace hex {
namespace hex::crypt {
u16 crc16(prv::Provider* &data, u64 offset, size_t size, u16 polynomial, u16 init) {
const auto table = [polynomial] {
@ -85,134 +85,134 @@ namespace hex {
return ~c;
}
std::array<u32, 4> md4(prv::Provider* &data, u64 offset, size_t size) {
std::array<u32, 4> result = { 0 };
std::array<u8, 16> md5(prv::Provider* &data, u64 offset, size_t size) {
std::array<u8, 16> result = { 0 };
MD4_CTX ctx;
mbedtls_md5_context ctx;
mbedtls_md5_init(&ctx);
MD4_Init(&ctx);
mbedtls_md5_starts_ret(&ctx);
std::array<u8, 512> buffer = { 0 };
for (u64 bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
const u64 readSize = std::min(u64(buffer.size()), size - bufferOffset);
data->read(offset + bufferOffset, buffer.data(), readSize);
MD4_Update(&ctx, buffer.data(), readSize);
mbedtls_md5_update_ret(&ctx, buffer.data(), readSize);
}
MD4_Final(reinterpret_cast<u8*>(result.data()), &ctx);
mbedtls_md5_finish_ret(&ctx, result.data());
mbedtls_md5_free(&ctx);
return result;
}
std::array<u32, 4> md5(prv::Provider* &data, u64 offset, size_t size) {
std::array<u32, 4> result = { 0 };
std::array<u8, 20> sha1(prv::Provider* &data, u64 offset, size_t size) {
std::array<u8, 20> result = { 0 };
MD5_CTX ctx;
mbedtls_sha1_context ctx;
mbedtls_sha1_init(&ctx);
MD5_Init(&ctx);
mbedtls_sha1_starts_ret(&ctx);
std::array<u8, 512> buffer = { 0 };
for (u64 bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
const u64 readSize = std::min(u64(buffer.size()), size - bufferOffset);
data->read(offset + bufferOffset, buffer.data(), readSize);
MD5_Update(&ctx, buffer.data(), readSize);
mbedtls_sha1_update_ret(&ctx, buffer.data(), readSize);
}
MD5_Final(reinterpret_cast<u8*>(result.data()), &ctx);
mbedtls_sha1_finish_ret(&ctx, result.data());
mbedtls_sha1_free(&ctx);
return result;
}
std::array<u32, 5> sha1(prv::Provider* &data, u64 offset, size_t size) {
std::array<u32, 5> result = { 0 };
std::array<u8, 28> sha224(prv::Provider* &data, u64 offset, size_t size) {
std::array<u8, 28> result = { 0 };
SHA_CTX ctx;
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
mbedtls_sha256_starts_ret(&ctx, true);
SHA1_Init(&ctx);
std::array<u8, 512> buffer = { 0 };
for (u64 bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
const u64 readSize = std::min(u64(buffer.size()), size - bufferOffset);
data->read(offset + bufferOffset, buffer.data(), readSize);
SHA1_Update(&ctx, buffer.data(), readSize);
mbedtls_sha256_update_ret(&ctx, buffer.data(), readSize);
}
SHA1_Final(reinterpret_cast<u8*>(result.data()), &ctx);
mbedtls_sha256_finish_ret(&ctx, result.data());
mbedtls_sha256_free(&ctx);
return result;
}
std::array<u32, 7> sha224(prv::Provider* &data, u64 offset, size_t size) {
std::array<u32, 7> result = { 0 };
std::array<u8, 32> sha256(prv::Provider* &data, u64 offset, size_t size) {
std::array<u8, 32> result = { 0 };
SHA256_CTX ctx;
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
SHA224_Init(&ctx);
mbedtls_sha256_starts_ret(&ctx, false);
std::array<u8, 512> buffer = { 0 };
for (u64 bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
const u64 readSize = std::min(u64(buffer.size()), size - bufferOffset);
data->read(offset + bufferOffset, buffer.data(), readSize);
SHA224_Update(&ctx, buffer.data(), readSize);
mbedtls_sha256_update_ret(&ctx, buffer.data(), readSize);
}
SHA224_Final(reinterpret_cast<u8*>(result.data()), &ctx);
mbedtls_sha256_finish_ret(&ctx, result.data());
mbedtls_sha256_free(&ctx);
return result;
}
std::array<u32, 8> sha256(prv::Provider* &data, u64 offset, size_t size) {
std::array<u32, 8> result = { 0 };
std::array<u8, 48> sha384(prv::Provider* &data, u64 offset, size_t size) {
std::array<u8, 48> result = { 0 };
SHA256_CTX ctx;
mbedtls_sha512_context ctx;
mbedtls_sha512_init(&ctx);
SHA256_Init(&ctx);
mbedtls_sha512_starts_ret(&ctx, true);
std::array<u8, 512> buffer = { 0 };
for (u64 bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
const u64 readSize = std::min(u64(buffer.size()), size - bufferOffset);
data->read(offset + bufferOffset, buffer.data(), readSize);
SHA256_Update(&ctx, buffer.data(), readSize);
mbedtls_sha512_update_ret(&ctx, buffer.data(), readSize);
}
SHA256_Final(reinterpret_cast<u8*>(result.data()), &ctx);
mbedtls_sha512_finish_ret(&ctx, result.data());
mbedtls_sha512_free(&ctx);
return result;
}
std::array<u32, 12> sha384(prv::Provider* &data, u64 offset, size_t size) {
std::array<u32, 12> result = { 0 };
std::array<u8, 64> sha512(prv::Provider* &data, u64 offset, size_t size) {
std::array<u8, 64> result = { 0 };
SHA512_CTX ctx;
mbedtls_sha512_context ctx;
mbedtls_sha512_init(&ctx);
SHA384_Init(&ctx);
mbedtls_sha512_starts_ret(&ctx, false);
std::array<u8, 512> buffer = { 0 };
for (u64 bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
const u64 readSize = std::min(u64(buffer.size()), size - bufferOffset);
data->read(offset + bufferOffset, buffer.data(), readSize);
SHA384_Update(&ctx, buffer.data(), readSize);
mbedtls_sha512_update_ret(&ctx, buffer.data(), readSize);
}
SHA384_Final(reinterpret_cast<u8*>(result.data()), &ctx);
mbedtls_sha512_finish_ret(&ctx, result.data());
return result;
}
std::array<u32, 16> sha512(prv::Provider* &data, u64 offset, size_t size) {
std::array<u32, 16> result = { 0 };
SHA512_CTX ctx;
SHA512_Init(&ctx);
std::array<u8, 512> buffer = { 0 };
for (u64 bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
const u64 readSize = std::min(u64(buffer.size()), size - bufferOffset);
data->read(offset + bufferOffset, buffer.data(), readSize);
SHA512_Update(&ctx, buffer.data(), readSize);
}
SHA512_Final(reinterpret_cast<u8*>(result.data()), &ctx);
mbedtls_sha512_free(&ctx);
return result;
}
@ -221,7 +221,8 @@ namespace hex {
size_t outputSize = (3 * input.size()) / 4;
std::vector<u8> output(outputSize + 1, 0x00);
if (EVP_DecodeBlock(output.data(), reinterpret_cast<const unsigned char *>(input.data()), input.size()) != outputSize)
size_t written = 0;
if (mbedtls_base64_decode(output.data(), output.size(), &written, reinterpret_cast<const unsigned char *>(input.data()), input.size()))
return { };
return output;
@ -231,7 +232,8 @@ namespace hex {
size_t outputSize = 4 * ((input.size() + 2) / 3);
std::vector<u8> output(outputSize + 1, 0x00);
if (EVP_EncodeBlock(output.data(), reinterpret_cast<const unsigned char *>(input.data()), input.size()) != outputSize)
size_t written = 0;
if (mbedtls_base64_encode(output.data(), output.size(), &written, reinterpret_cast<const unsigned char *>(input.data()), input.size()))
return { };
return output;

View File

@ -32,9 +32,10 @@ namespace hex {
}
static void formatBigHexInt(auto dataArray, char *buffer, size_t bufferSize) {
template<size_t Size>
static void formatBigHexInt(std::array<u8, Size> dataArray, char *buffer, size_t bufferSize) {
for (int i = 0; i < dataArray.size(); i++)
snprintf(buffer + 8 * i, bufferSize - 8 * i, "%08X", hex::changeEndianess(dataArray[i], std::endian::big));
snprintf(buffer + 2 * i, bufferSize - 2 * i, "%02X", dataArray[i]);
}
void ViewHashes::drawContent() {
@ -83,7 +84,7 @@ namespace hex {
static u16 result = 0;
if (this->m_shouldInvalidate)
result = crc16(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
result = crypt::crc16(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
char buffer[sizeof(result) * 2 + 1];
snprintf(buffer, sizeof(buffer), "%04X", result);
@ -108,7 +109,7 @@ namespace hex {
static u32 result = 0;
if (this->m_shouldInvalidate)
result = crc32(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
result = crypt::crc32(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1, polynomial, init);
char buffer[sizeof(result) * 2 + 1];
snprintf(buffer, sizeof(buffer), "%08X", result);
@ -118,12 +119,12 @@ namespace hex {
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case 2: // MD4
case 2: // MD5
{
static std::array<u32, 4> result;
static std::array<u8, 16> result = { 0 };
if (this->m_shouldInvalidate)
result = md4(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
result = crypt::md5(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
@ -134,12 +135,12 @@ namespace hex {
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case 3: // MD5
case 3: // SHA-1
{
static std::array<u32, 4> result = { 0 };
static std::array<u8, 20> result = { 0 };
if (this->m_shouldInvalidate)
result = md5(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
result = crypt::sha1(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
@ -150,12 +151,12 @@ namespace hex {
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case 4: // SHA-1
case 4: // SHA-224
{
static std::array<u32, 5> result = { 0 };
static std::array<u8, 28> result = { 0 };
if (this->m_shouldInvalidate)
result = sha1(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
result = crypt::sha224(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
@ -166,12 +167,12 @@ namespace hex {
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case 5: // SHA-224
case 5: // SHA-256
{
static std::array<u32, 7> result = { 0 };
static std::array<u8, 32> result;
if (this->m_shouldInvalidate)
result = sha224(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
result = crypt::sha256(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
@ -182,12 +183,12 @@ namespace hex {
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case 6: // SHA-256
case 6: // SHA-384
{
static std::array<u32, 8> result;
static std::array<u8, 48> result;
if (this->m_shouldInvalidate)
result = sha256(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
result = crypt::sha384(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
@ -198,28 +199,12 @@ namespace hex {
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case 7: // SHA-384
case 7: // SHA-512
{
static std::array<u32, 12> result;
static std::array<u8, 64> result;
if (this->m_shouldInvalidate)
result = sha384(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));
ImGui::NewLine();
ImGui::TextUnformatted("Result");
ImGui::Separator();
ImGui::InputText("##nolabel", buffer, ImGuiInputTextFlags_ReadOnly);
}
break;
case 8: // SHA-512
{
static std::array<u32, 16> result;
if (this->m_shouldInvalidate)
result = sha512(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
result = crypt::sha512(provider, this->m_hashRegion[0], this->m_hashRegion[1] - this->m_hashRegion[0] + 1);
char buffer[sizeof(result) * 2 + 1];
formatBigHexInt(result, buffer, sizeof(buffer));

View File

@ -367,7 +367,7 @@ namespace hex {
this->loadFromFile(path, base64);
if (!base64.empty()) {
this->m_dataToSave = decode64(base64);
this->m_dataToSave = crypt::decode64(base64);
if (this->m_dataToSave.empty())
View::showErrorPopup("File is not in a valid Base64 format!");