Update to use installed Crypto++ library

This commit is contained in:
2025-08-28 09:52:53 +02:00
parent 42c00c3452
commit 8e92ccb386
3 changed files with 22 additions and 81 deletions

View File

@ -52,17 +52,27 @@ 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()
# 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
set(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

View File

@ -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;

View File

@ -3,7 +3,6 @@
#include <stdexcept>
#include <cstring>
#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<unsigned char> Crypto::generate_key(size_t length) {
#ifndef NO_CRYPTO
std::vector<unsigned char> key(length);
// Use Crypto++ AutoSeededRandomPool for cryptographically secure random numbers
@ -25,26 +22,12 @@ std::vector<unsigned char> Crypto::generate_key(size_t length) {
rng.GenerateBlock(key.data(), length);
return key;
#else
// Fallback to std::random - NOT cryptographically secure!
std::vector<unsigned char> 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<unsigned char>(dis(gen));
}
return key;
#endif
}
std::vector<unsigned char> Crypto::encrypt(const std::vector<unsigned char>& plaintext,
const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
std::vector<unsigned char>& auth_tag) {
#ifndef NO_CRYPTO
try {
// Create GCM mode encryption object
GCM<AES>::Encryption encryption;
@ -76,23 +59,12 @@ std::vector<unsigned char> Crypto::encrypt(const std::vector<unsigned char>& 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<unsigned char> Crypto::decrypt(const std::vector<unsigned char>& ciphertext,
const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& auth_tag) {
#ifndef NO_CRYPTO
try {
// Create GCM mode decryption object
GCM<AES>::Decryption decryption;
@ -125,22 +97,12 @@ std::vector<unsigned char> Crypto::decrypt(const std::vector<unsigned char>& 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<unsigned char> Crypto::pbkdf2_hmac_sha256(const std::string& password,
const std::vector<unsigned char>& salt,
int iterations,
size_t key_length) {
#ifndef NO_CRYPTO
try {
std::vector<unsigned char> derived_key(key_length);
@ -163,23 +125,9 @@ std::vector<unsigned char> 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<unsigned char> key(key_length, 0);
for (size_t i = 0; i < key_length; i++) {
key[i] = static_cast<unsigned char>((i * 17) % 256);
}
return key;
#endif
}
std::vector<unsigned char> Crypto::compress(const std::vector<unsigned char>& data) {
#ifndef NO_CRYPTO
try {
std::string compressed;
@ -199,17 +147,9 @@ std::vector<unsigned char> Crypto::compress(const std::vector<unsigned char>& 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<unsigned char> Crypto::decompress(const std::vector<unsigned char>& data) {
#ifndef NO_CRYPTO
try {
std::string decompressed;
@ -229,11 +169,4 @@ std::vector<unsigned char> Crypto::decompress(const std::vector<unsigned char>&
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
}