From 8e92ccb3867883b9ee5e5e2d78c33470be088ede Mon Sep 17 00:00:00 2001 From: elpatron Date: Thu, 28 Aug 2025 09:52:53 +0200 Subject: [PATCH] Update to use installed Crypto++ library --- CMakeLists.txt | 32 +++++++++++++++------- example/example.cpp | 4 --- src/crypto.cpp | 67 --------------------------------------------- 3 files changed, 22 insertions(+), 81 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c6841ab..326bb04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,16 +52,26 @@ if(PKG_CONFIG_FOUND) pkg_check_modules(CRYPTOPP QUIET libcrypto++ cryptopp) endif() -if(CRYPTOPP_FOUND) +# Try to find Crypto++ using find_package +find_package(cryptopp CONFIG QUIET) + +if(cryptopp_FOUND) # Use the found package - message(STATUS "Found Crypto++ package") - set(CRYPTOPP_INCLUDE_DIRS ${CRYPTOPP_INCLUDE_DIRS}) - set(CRYPTOPP_LIBRARIES ${CRYPTOPP_LIBRARIES}) + message(STATUS "Found Crypto++ package via find_package") + set(CRYPTOPP_INCLUDE_DIRS "") + set(CRYPTOPP_LIBRARIES cryptopp::cryptopp) else() - # For now, we'll provide a warning and use stub implementations - # In a real implementation, you would install Crypto++ separately - message(WARNING "Crypto++ not found. Using stub implementations for cryptographic functions.") - add_definitions(-DNO_CRYPTO) + # Try to find it via vcpkg + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/installed/x64-windows/include/cryptopp/config.h") + message(STATUS "Found Crypto++ via vcpkg") + set(CRYPTOPP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/installed/x64-windows/include") + set(CRYPTOPP_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../vcpkg/installed/x64-windows/lib/cryptopp.lib") + else() + # For now, we'll provide a warning and use stub implementations + # In a real implementation, you would install Crypto++ separately + message(WARNING "Crypto++ not found. Using stub implementations for cryptographic functions.") + add_definitions(-DNO_CRYPTO) + endif() endif() # Add library sources @@ -97,9 +107,11 @@ else() endif() # Include Crypto++ -if(CRYPTOPP_FOUND) +if(cryptopp_FOUND) + target_link_libraries(privatebinapi PRIVATE cryptopp::cryptopp) +elseif(CRYPTOPP_LIBRARIES) target_include_directories(privatebinapi PRIVATE ${CRYPTOPP_INCLUDE_DIRS}) - target_link_libraries(privatebinapi ${CRYPTOPP_LIBRARIES}) + target_link_libraries(privatebinapi PRIVATE ${CRYPTOPP_LIBRARIES}) endif() # Link dependencies diff --git a/example/example.cpp b/example/example.cpp index 1b3d757..4010e20 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -11,8 +11,6 @@ int main() { char* delete_token = nullptr; std::cout << "Creating paste on https://privatebin.medisoftware.org..." << std::endl; - std::cout << "Note: Crypto++ library not found, using stub implementations." << std::endl; - std::cout << "This means encryption/decryption will not work properly." << std::endl; // Testing against https://privatebin.medisoftware.org int result = create_paste( @@ -47,8 +45,6 @@ int main() { break; case 2: std::cout << "Encryption/decryption error occurred." << std::endl; - std::cout << "This is expected since we're using stub implementations." << std::endl; - std::cout << "To fix this, install the Crypto++ library and rebuild." << std::endl; break; case 3: std::cout << "Invalid input provided." << std::endl; diff --git a/src/crypto.cpp b/src/crypto.cpp index 7df5772..1edf16f 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -3,7 +3,6 @@ #include #include -#ifndef NO_CRYPTO // Crypto++ includes #include "cryptlib.h" #include "osrng.h" // AutoSeededRandomPool @@ -14,10 +13,8 @@ #include "zlib.h" // Zlib compression using namespace CryptoPP; -#endif std::vector Crypto::generate_key(size_t length) { -#ifndef NO_CRYPTO std::vector key(length); // Use Crypto++ AutoSeededRandomPool for cryptographically secure random numbers @@ -25,26 +22,12 @@ std::vector Crypto::generate_key(size_t length) { rng.GenerateBlock(key.data(), length); return key; -#else - // Fallback to std::random - NOT cryptographically secure! - std::vector key(length); - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> dis(0, 255); - - for (size_t i = 0; i < length; ++i) { - key[i] = static_cast(dis(gen)); - } - - return key; -#endif } std::vector Crypto::encrypt(const std::vector& plaintext, const std::vector& key, const std::vector& iv, std::vector& auth_tag) { -#ifndef NO_CRYPTO try { // Create GCM mode encryption object GCM::Encryption encryption; @@ -76,23 +59,12 @@ std::vector Crypto::encrypt(const std::vector& pla catch(const CryptoPP::Exception& e) { throw std::runtime_error("Encryption failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use a proper crypto library like Crypto++ or OpenSSL - // to perform AES-GCM encryption - - // For demonstration purposes, we'll just return the plaintext - // In a real implementation, this would be the actual encryption - auth_tag.resize(16, 0); // 128-bit authentication tag - return plaintext; -#endif } std::vector Crypto::decrypt(const std::vector& ciphertext, const std::vector& key, const std::vector& iv, const std::vector& auth_tag) { -#ifndef NO_CRYPTO try { // Create GCM mode decryption object GCM::Decryption decryption; @@ -125,22 +97,12 @@ std::vector Crypto::decrypt(const std::vector& cip catch(const CryptoPP::Exception& e) { throw std::runtime_error("Decryption failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use a proper crypto library like Crypto++ or OpenSSL - // to perform AES-GCM decryption - - // For demonstration purposes, we'll just return the ciphertext - // In a real implementation, this would be the actual decryption - return ciphertext; -#endif } std::vector Crypto::pbkdf2_hmac_sha256(const std::string& password, const std::vector& salt, int iterations, size_t key_length) { -#ifndef NO_CRYPTO try { std::vector derived_key(key_length); @@ -163,23 +125,9 @@ std::vector Crypto::pbkdf2_hmac_sha256(const std::string& passwor catch(const CryptoPP::Exception& e) { throw std::runtime_error("PBKDF2 key derivation failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use a proper crypto library to perform PBKDF2-HMAC-SHA256 - - // For demonstration purposes, we'll just return a key of the requested length - // filled with a simple pattern - std::vector key(key_length, 0); - for (size_t i = 0; i < key_length; i++) { - key[i] = static_cast((i * 17) % 256); - } - - return key; -#endif } std::vector Crypto::compress(const std::vector& data) { -#ifndef NO_CRYPTO try { std::string compressed; @@ -199,17 +147,9 @@ std::vector Crypto::compress(const std::vector& da catch(const CryptoPP::Exception& e) { throw std::runtime_error("Compression failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use zlib or another compression library - - // For demonstration purposes, we'll just return the data as-is - return data; -#endif } std::vector Crypto::decompress(const std::vector& data) { -#ifndef NO_CRYPTO try { std::string decompressed; @@ -229,11 +169,4 @@ std::vector Crypto::decompress(const std::vector& catch(const CryptoPP::Exception& e) { throw std::runtime_error("Decompression failed: " + std::string(e.what())); } -#else - // This is a stub implementation - in a real implementation, - // you would use zlib or another decompression library - - // For demonstration purposes, we'll just return the data as-is - return data; -#endif } \ No newline at end of file