Skip to content

Commit

Permalink
Merge pull request #579 from r-a-sattarov/xd_dev
Browse files Browse the repository at this point in the history
CMake: added option to turn ON/OFF of using Crypto++ Library
  • Loading branch information
Xottab-DUTY authored Apr 23, 2020
2 parents 023b510 + 9fbc811 commit ced5ae1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ if(NOT WIN32)
endif()

option ( TBB_PARALLEL "Use tbb::parallel for prarticle and skinning acceleration on SMP." ON )
option ( USE_CRYPTOPP "Use Crypto++ Library (REQUIRED FOR MULTIPLAYER)" ON )

# Memory allocator option
set ( MEMORY_ALLOCATOR "mimalloc" CACHE STRING "Use specific memory allocator (mimalloc/tbb/standard)" )
Expand Down Expand Up @@ -157,6 +158,10 @@ if( TBB_PARALLEL )
add_definitions ( -DUSE_TBB_PARALLEL )
endif()

if( USE_CRYPTOPP )
add_definitions ( -DUSE_CRYPTOPP )
endif()

if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
add_definitions(-DDEBUG -DMIXED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Og")
Expand Down Expand Up @@ -184,7 +189,9 @@ if (NOT WIN32)
find_package(FreeImage REQUIRED)
find_package(LockFile REQUIRED)
find_package(OpenAL REQUIRED)
find_package(Crypto++ REQUIRED)
if (USE_CRYPTOPP)
find_package(Crypto++ REQUIRED)
endif()
find_package(Theora REQUIRED)
find_package(OGG REQUIRED)
find_package(SDL2 REQUIRED)
Expand Down
26 changes: 24 additions & 2 deletions src/xrCore/Crypto/xr_dsa.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#include "stdafx.h"
#include "xr_dsa.h"

#ifdef USE_CRYPTOPP
#include <cryptopp/dsa.h>
#include <cryptopp/integer.h>
#endif

namespace crypto
{
xr_dsa::xr_dsa(u8 const p[public_key_length], u8 const q[private_key_length], u8 const g[public_key_length])
{
#ifdef USE_CRYPTOPP
CryptoPP::Integer p_number(p, public_key_length);
CryptoPP::Integer q_number(q, private_key_length);
CryptoPP::Integer g_number(g, public_key_length);

m_dsa.Initialize(p_number, q_number, g_number);
#endif // USE_CRYPTOPP
}

xr_dsa::~xr_dsa() {}
shared_str xr_dsa::sign(private_key_t const& priv_key, u8 const* data, u32 const data_size)
{
#ifdef USE_CRYPTOPP
CryptoPP::Integer exp(priv_key.m_value, sizeof(priv_key.m_value));
CryptoPP::DSA::PrivateKey private_key;
private_key.Initialize(m_dsa, exp);
Expand All @@ -28,10 +34,17 @@ shared_str xr_dsa::sign(private_key_t const& priv_key, u8 const* data, u32 const
); // StringSource

return shared_str(signature.c_str());
#else // USE_CRYPTOPP
UNUSED(priv_key);
UNUSED(data);
UNUSED(data_size);
return shared_str("(null signature)");
#endif // USE_CRYPTOPP
}

bool xr_dsa::verify(public_key_t const& pub_key, u8 const* data, u32 const data_size, shared_str const& dsign)
{
#ifdef USE_CRYPTOPP
CryptoPP::Integer exp(pub_key.m_value, sizeof(pub_key.m_value));
CryptoPP::DSA::PublicKey public_key;
public_key.Initialize(m_dsa, exp);
Expand All @@ -43,10 +56,17 @@ bool xr_dsa::verify(public_key_t const& pub_key, u8 const* data, u32 const data_
CryptoPP::StringSource(signature + message, true, new CryptoPP::Redirector(svf));

return svf.GetLastResult();
#else
UNUSED(pub_key);
UNUSED(data);
UNUSED(data_size);
UNUSED(dsign);
return true;
#endif // USE_CRYPTOPP
}

#ifdef DEBUG

#ifdef USE_CRYPTOPP
void print_big_number(CryptoPP::Integer big_num, u32 max_columns = 8)
{
u8 bin_buff[xr_dsa::public_key_length]; // public_key_length is the max
Expand Down Expand Up @@ -132,7 +152,9 @@ void xr_dsa::generate_params()
CryptoPP::StringSource(signature + debug_bad_digest, true, new CryptoPP::Redirector(svf));
VERIFY(svf.GetLastResult() == false);
}

#else // USE_CRYPTOPP
void xr_dsa::generate_params() {}
#endif // USE_CRYPTOPP
#endif //#ifdef DEBUG

} // namespace crypto
4 changes: 4 additions & 0 deletions src/xrCore/Crypto/xr_dsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "xrCore/xrCore.h"

#ifdef USE_CRYPTOPP
#include <cryptopp/dsa.h>
#include <cryptopp/sha.h>
#include <cryptopp/osrng.h>
#endif

namespace crypto
{
Expand Down Expand Up @@ -38,8 +40,10 @@ class XRCORE_API xr_dsa
#endif

private:
#ifdef USE_CRYPTOPP
CryptoPP::DL_GroupParameters_DSA m_dsa;
CryptoPP::AutoSeededRandomPool m_rng;
#endif // USE_CRYPTOPP
}; // class xr_dsa

} // namespace crypto
Expand Down
2 changes: 0 additions & 2 deletions src/xrCore/Crypto/xr_dsa_signer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#ifndef XR_DSA_SIGNER_INCLUDED
#define XR_DSA_SIGNER_INCLUDED

#include <cryptopp/dsa.h>

#include "xr_dsa.h"
#include "xr_sha.h"

Expand Down
2 changes: 0 additions & 2 deletions src/xrCore/Crypto/xr_dsa_verifyer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include <optional>

#include <cryptopp/dsa.h>

#include "xr_dsa.h"
#include "xr_sha.h"

Expand Down
18 changes: 17 additions & 1 deletion src/xrCore/Crypto/xr_sha.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
#pragma once
#include "xrCore/xrCore.h"
#include <cryptopp/sha.h>

#include <optional>
#include <array>

#ifdef USE_CRYPTOPP
#include <cryptopp/sha.h>
#else // hack
namespace CryptoPP
{
class SHA1
{
public:
static constexpr size_t DIGESTSIZE = 20;
static constexpr size_t BLOCKSIZE = 64;

static void Update(const u8* /*input*/, size_t /*len*/) {}
static void Final(u8* /*digest*/) {}
};
}
#endif

namespace crypto
{
// TODO: Yielder must be moved to the separate module
Expand Down

0 comments on commit ced5ae1

Please sign in to comment.