diff --git a/CMakeLists.txt b/CMakeLists.txt index a3475be..8b4fccc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ -cmake_minimum_required(VERSION 3.24) # Requires 3.24+ for URL-based FetchContent binaries -project(OpenSSLExample CXX) +cmake_minimum_required(VERSION 3.24) +project(CryptoPDiddy CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -9,7 +9,6 @@ include(FetchContent) if(WIN32) # Automatically downloads pre-built Windows OpenSSL binaries. - # The original placeholder URL "https://github.com" cannot be unpacked by FetchContent. FetchContent_Declare( openssl URL "https://github.com/TaurusTLS-Developers/OpenSSL-Distribution/releases/download/v3.5.6/openssl-3.5.6-Windows-x64.zip" @@ -132,22 +131,20 @@ else() set(OPENSSL_SSL_LIB OpenSSL::SSL) endif() -add_executable(crypto_app main.cpp) +add_executable(EpstProject main.cpp CryptEpstein.h) -# Link against the downloaded binaries -target_include_directories(crypto_app PRIVATE "${OPENSSL_INCLUDE_DIR}") -target_link_libraries(crypto_app PRIVATE "${OPENSSL_CRYPTO_LIB}" "${OPENSSL_SSL_LIB}") +target_include_directories(EpstProject PRIVATE "${OPENSSL_INCLUDE_DIR}") +target_link_libraries(EpstProject PRIVATE "${OPENSSL_CRYPTO_LIB}" "${OPENSSL_SSL_LIB}") -# Copy DLL files to output directory so the executable can run if(WIN32) if(NOT OPENSSL_CRYPTO_DLL OR NOT OPENSSL_SSL_DLL) message(FATAL_ERROR "Downloaded OpenSSL package does not contain the expected DLL files.") endif() - add_custom_command(TARGET crypto_app POST_BUILD + add_custom_command(TARGET EpstProject POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENSSL_CRYPTO_DLL}" "${OPENSSL_SSL_DLL}" - $ + $ ) endif() diff --git a/CryptEpstein.h b/CryptEpstein.h new file mode 100644 index 0000000..b08e18c --- /dev/null +++ b/CryptEpstein.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +// RAII +struct PKEYDeleter { void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); } }; +struct CTXDeleter { void operator()(EVP_PKEY_CTX* p) const { EVP_PKEY_CTX_free(p); } }; +struct CipherDeleter { void operator()(EVP_CIPHER_CTX* ctx) const { EVP_CIPHER_CTX_free(ctx); } }; +struct BIODeleter { void operator()(BIO* b) const { BIO_free_all(b); } }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 9be02c8..553b013 100644 --- a/main.cpp +++ b/main.cpp @@ -1,31 +1,14 @@ -#include -#include -#include -#include -#include -#include -#include -#include +#include "CryptEpstein.h" -#include -#include -#include -#include +using PRIVATE_KEY = std::unique_ptr; +using PUBLIC_KEY_CONTEXT = std::unique_ptr; +using KEY_BIO = std::unique_ptr; +using CIPHER_CONTEXT = std::unique_ptr; #define DATA_WRITE(data) reinterpret_cast(data) #define DATA_READ(data) reinterpret_cast(data) constexpr size_t BUFFER_SIZE = 4096; -// RAII -struct PKEYDeleter { void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); } }; -struct CTXDeleter { void operator()(EVP_PKEY_CTX* p) const { EVP_PKEY_CTX_free(p); } }; -struct CipherDeleter { void operator()(EVP_CIPHER_CTX* ctx) const { EVP_CIPHER_CTX_free(ctx); } }; -struct BIODeleter { void operator()(BIO* b) const { BIO_free_all(b); } }; - -typedef std::unique_ptr PRIVATE_KEY; -typedef std::unique_ptr PUBLIC_KEY_CONTEXT; -typedef std::unique_ptr KEY_BIO; -typedef std::unique_ptr CIPTHER_CONTEXT; void generate_rsa_keypair(const std::string& private_key_path, const std::string& public_key_path) { // Initialize the context for key generation @@ -130,7 +113,7 @@ void hybrid_encrypt(const std::string& input_path, const std::string& output_pat out_file.write(DATA_WRITE(iv), sizeof(iv)); // Stream encrypt the actual file data via AES - CIPTHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new()); + CIPHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new()); if (!aes_ctx || EVP_EncryptInit_ex(aes_ctx.get(), EVP_aes_256_cbc(), nullptr, aes_key, iv) != 1) { throw std::runtime_error("AES init failed."); } @@ -188,7 +171,7 @@ void hybrid_decrypt(const std::string& input_path, const std::string& output_pat aes_key.resize(aes_key_len); // Stream decrypt the file data using the recovered AES key - CIPTHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new()); + CIPHER_CONTEXT aes_ctx(EVP_CIPHER_CTX_new()); if (!aes_ctx || EVP_DecryptInit_ex(aes_ctx.get(), EVP_aes_256_cbc(), nullptr, aes_key.data(), iv) != 1) { throw std::runtime_error("AES decrypt init failed."); }