Browse Source

Use OpenSSL's bignum library

Michael Hansen 14 years ago
parent
commit
562b6fcfd0
  1. 3
      CMakeLists.txt
  2. 3
      Sources/Plasma/CoreLibExe/CMakeLists.txt
  3. 5
      Sources/Plasma/FeatureLib/pfAnimation/CMakeLists.txt
  4. 1
      Sources/Plasma/FeatureLib/pfAudio/CMakeLists.txt
  5. 2
      Sources/Plasma/FeatureLib/pfCamera/CMakeLists.txt
  6. 2
      Sources/Plasma/FeatureLib/pfCharacter/CMakeLists.txt
  7. 1
      Sources/Plasma/FeatureLib/pfConsole/CMakeLists.txt
  8. 16
      Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleCommandsCore.cpp
  9. 2
      Sources/Plasma/FeatureLib/pfGameMgr/CMakeLists.txt
  10. 2
      Sources/Plasma/FeatureLib/pfJournalBook/CMakeLists.txt
  11. 1
      Sources/Plasma/FeatureLib/pfPython/CMakeLists.txt
  12. 9
      Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp
  13. 2
      Sources/Plasma/NucleusLib/pnUtils/CMakeLists.txt
  14. 1358
      Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBigNum.cpp
  15. 201
      Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBigNum.h
  16. 2
      Sources/Plasma/PubUtilLib/plAgeDescription/CMakeLists.txt
  17. 2
      Sources/Plasma/PubUtilLib/plAgeLoader/CMakeLists.txt
  18. 1
      Sources/Plasma/PubUtilLib/plAvatar/CMakeLists.txt
  19. 2
      Sources/Plasma/PubUtilLib/plEncryption/CMakeLists.txt
  20. 2
      Sources/Plasma/PubUtilLib/plGLight/CMakeLists.txt
  21. 1
      Sources/Plasma/PubUtilLib/plInputCore/CMakeLists.txt
  22. 1
      Sources/Plasma/PubUtilLib/plModifier/CMakeLists.txt
  23. 2
      Sources/Plasma/PubUtilLib/plNetClient/CMakeLists.txt
  24. 2
      Sources/Plasma/PubUtilLib/plNetClientComm/CMakeLists.txt
  25. 2
      Sources/Plasma/PubUtilLib/plNetTransport/CMakeLists.txt
  26. 1
      Sources/Plasma/PubUtilLib/plPhysX/CMakeLists.txt
  27. 2
      Sources/Plasma/PubUtilLib/plStatGather/CMakeLists.txt
  28. 1
      Sources/Tools/MaxMain/CMakeLists.txt

3
CMakeLists.txt

@ -82,5 +82,8 @@ if(MSVC)
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
endif(MSVC)
#TODO: Make the OpenSSL includes less promiscuous so this isn't needed
include_directories(${OPENSSL_INCLUDE_DIR})
add_subdirectory(Sources/Plasma)
add_subdirectory(Sources/Tools)

3
Sources/Plasma/CoreLibExe/CMakeLists.txt

@ -1,6 +1,7 @@
include_directories("../../Plasma/CoreLib")
add_definitions(-D_LIB)
include_directories("../../Plasma/CoreLib")
set(CoreLibExe_SOURCES
hsExeError.cpp
hsExeMalloc.cpp

5
Sources/Plasma/FeatureLib/pfAnimation/CMakeLists.txt

@ -3,11 +3,6 @@ include_directories("../../NucleusLib")
include_directories("../../NucleusLib/inc")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
# this is in the vcproj, but it seems silly for animation...
# find_package(OpenSSL REQUIRED)
set(pfAnimation_SOURCES
pfObjectFlocker.cpp
plAnimDebugList.cpp

1
Sources/Plasma/FeatureLib/pfAudio/CMakeLists.txt

@ -5,7 +5,6 @@ include_directories(../../NucleusLib/inc)
include_directories(../../PubUtilLib)
#include_directories(../../PubUtilLib/inc)
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENAL_INCLUDE_DIR})
set(pfAudio_SOURCES

2
Sources/Plasma/FeatureLib/pfCamera/CMakeLists.txt

@ -4,8 +4,6 @@ include_directories(../../NucleusLib)
include_directories(../../NucleusLib/inc)
include_directories(../../PubUtilLib)
include_directories(${OPENSSL_INCLUDE_DIR})
set(pfCamera_SOURCES
pfCameraProxy.cpp
plCameraBrain.cpp

2
Sources/Plasma/FeatureLib/pfCharacter/CMakeLists.txt

@ -4,8 +4,6 @@ include_directories(../../NucleusLib)
include_directories(../../NucleusLib/inc)
include_directories(../../PubUtilLib)
include_directories(${OPENSSL_INCLUDE_DIR})
set(pfCharacter_SOURCES
pfMarkerInfo.cpp
pfMarkerMgr.cpp

1
Sources/Plasma/FeatureLib/pfConsole/CMakeLists.txt

@ -4,7 +4,6 @@ include_directories(../../NucleusLib)
include_directories(../../NucleusLib/inc)
include_directories(../../PubUtilLib)
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENAL_INCLUDE_DIR})
include_directories(${PYTHON_INCLUDE_DIR})

16
Sources/Plasma/FeatureLib/pfConsoleCore/pfConsoleCommandsCore.cpp

@ -61,14 +61,6 @@ void PrintStringF(void pfun(const char *),const char * fmt, ...)
*
***/
//TODO: Fix Plasma to use OpenSSL's byte order (or better yet, to use OpenSSL),
// so this hack isn't needed
static void swap_key_bytes(byte keyData[])
{
for (size_t i = 0; i < (kNetDiffieHellmanKeyBits / 16); ++i)
std::swap(keyData[i], keyData[ (kNetDiffieHellmanKeyBits / 8) - i - 1 ]);
}
//============================================================================
// Server group
PF_CONSOLE_GROUP(Server)
@ -159,7 +151,6 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kAuthDhNData);
swap_key_bytes(kAuthDhNData);
}
//============================================================================
@ -178,7 +169,6 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kAuthDhXData);
swap_key_bytes(kAuthDhXData);
}
@ -214,7 +204,6 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kCsrDhNData);
swap_key_bytes(kCsrDhNData);
}
//============================================================================
@ -233,7 +222,6 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kCsrDhXData);
swap_key_bytes(kCsrDhXData);
}
@ -257,7 +245,6 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kGameDhNData);
swap_key_bytes(kGameDhNData);
}
//============================================================================
@ -276,7 +263,6 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kGameDhXData);
swap_key_bytes(kGameDhXData);
}
@ -312,7 +298,6 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kGateKeeperDhNData);
swap_key_bytes(kGateKeeperDhNData);
}
//============================================================================
@ -331,5 +316,4 @@ PF_CONSOLE_CMD(
Base64Decode(hsStrlen((const char *)params[0]), (const char *)params[0],
kNetDiffieHellmanKeyBits / 8, kGateKeeperDhXData);
swap_key_bytes(kGateKeeperDhXData);
}

2
Sources/Plasma/FeatureLib/pfGameMgr/CMakeLists.txt

@ -4,8 +4,6 @@ include_directories(../../NucleusLib)
include_directories(../../NucleusLib/inc)
include_directories(../../PubUtilLib)
include_directories(${OPENSSL_INCLUDE_DIR})
set(pfGameMgr_SOURCES
pfGameMgr.cpp
BlueSpiral/pfGmBlueSpiral.cpp

2
Sources/Plasma/FeatureLib/pfJournalBook/CMakeLists.txt

@ -4,8 +4,6 @@ include_directories(../../NucleusLib)
include_directories(../../NucleusLib/inc)
include_directories(../../PubUtilLib)
include_directories(${OPENSSL_INCLUDE_DIR})
set(pfJournalBook_SOURCES
pfJournalBook.cpp
)

1
Sources/Plasma/FeatureLib/pfPython/CMakeLists.txt

@ -7,7 +7,6 @@ include_directories(../../)
include_directories(${PYTHON_INCLUDE_DIR})
include_directories(${OPENAL_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIR})
set(pfPython_SOURCES
cyAccountManagement.cpp

9
Sources/Plasma/NucleusLib/pnNetCli/pnNcCli.cpp

@ -589,20 +589,22 @@ static void ClientConnect (NetCli * cli) {
{
ZERO(cli->seed);
unsigned bytes;
const void * data = clientSeed.GetData(&bytes);
unsigned char * data = clientSeed.GetData_LE(&bytes);
MemCopy(cli->seed, data, min(bytes, sizeof(cli->seed)));
delete [] data;
}
// Send server seed
if (cli->sock) {
unsigned bytes;
NetCli_Cli2Srv_Connect msg;
const void * data = serverSeed.GetData(&bytes);
unsigned char * data = serverSeed.GetData_LE(&bytes);
ASSERTMSG(bytes <= sizeof(msg.dh_y_data), "4");
msg.message = kNetCliCli2SrvConnect;
msg.length = (byte) (sizeof(msg) - sizeof(msg.dh_y_data) + bytes);
MemCopy(msg.dh_y_data, data, bytes);
AsyncSocketSend(cli->sock, &msg, msg.length);
delete [] data;
}
}
@ -643,8 +645,9 @@ static bool ServerRecvConnect (
ZERO(clientSeed);
unsigned bytes;
const void * data = clientSeedValue.GetData(&bytes);
unsigned char * data = clientSeedValue.GetData_LE(&bytes);
MemCopy(clientSeed, data, min(bytes, sizeof(clientSeed)));
delete [] data;
}
// Create the symmetric key from a combination

2
Sources/Plasma/NucleusLib/pnUtils/CMakeLists.txt

@ -1,8 +1,6 @@
include_directories("../../CoreLib")
include_directories("../../NucleusLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(pnUtils_HEADERS
Intern.h
Pch.h

1358
Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBigNum.cpp

File diff suppressed because it is too large Load Diff

201
Sources/Plasma/NucleusLib/pnUtils/Private/pnUtBigNum.h

@ -41,70 +41,169 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
*
***/
class BigNum : private ARRAY(dword) {
public:
typedef dword Val; // must match base array
typedef qword DVal; // must be twice as large as Val
#include <openssl/bn.h>
class BigNum {
private:
bool m_isTemp;
void DivNormalized (const BigNum & a, const BigNum & b, BigNum * remainder);
void ModNormalized (const BigNum & a, const BigNum & b);
inline static DVal Mul (Val a, Val b);
BIGNUM m_number;
mutable BN_CTX * m_context;
inline void SetVal (unsigned index, Val value);
inline void SetVal (unsigned index, DVal value, Val * carry);
inline void Trim (unsigned count);
inline BigNum * UseTempAlloc (Val * ptr, unsigned count);
BN_CTX * GetContext () const
{
if (!m_context)
m_context = BN_CTX_new();
return m_context;
}
public:
BigNum ();
BigNum (const BigNum & a);
BigNum (unsigned a);
BigNum (unsigned bytes, const void * data);
BigNum (const wchar str[], Val radix);
BigNum (unsigned bytes, const void * data, bool le=false);
~BigNum ();
BigNum & operator= (const BigNum & a)
{
BN_copy(&m_number, &a.m_number);
return *this;
}
// Constant parameters need not be distinct from the destination or from
// each other
void Add (const BigNum & a, Val b);
void Add (const BigNum & a, const BigNum & b);
int Compare (Val a) const;
int Compare (const BigNum & a) const;
void Div (const BigNum & a, Val b, Val * remainder);
void Div (const BigNum & a, const BigNum & b, BigNum * remainder);
void FromData (unsigned bytes, const void * data);
void FromStr (const wchar str[], Val radix);
void Gcd (const BigNum & a, const BigNum & b);
const void * GetData (unsigned * bytes) const;
unsigned HighBitPos () const;
bool InverseMod (const BigNum & a, const BigNum & b);
bool IsMultiple (Val a) const;
bool IsOdd () const;
bool IsPrime () const;
unsigned LowBitPos () const;
void Mod (const BigNum & a, const BigNum & b);
void Mul (const BigNum & a, Val b);
void Mul (const BigNum & a, const BigNum & b);
void MulMod (const BigNum & a, const BigNum & b, const BigNum & c);
void PowMod (Val a, const BigNum & b, const BigNum & c);
void PowMod (const BigNum & a, const BigNum & b, const BigNum & c);
void Rand (const BigNum & a, BigNum * seed);
void Add (const BigNum & a, dword b)
{
// this = a + b
BN_copy(&m_number, &a.m_number);
BN_add_word(&m_number, b);
}
void Add (const BigNum & a, const BigNum & b)
{
// this = a + b
BN_add(&m_number, &a.m_number, &b.m_number);
}
int Compare (dword a) const;
int Compare (const BigNum & a) const
{
return BN_cmp(&m_number, &a.m_number);
}
void Div (const BigNum & a, dword b, dword * remainder)
{
// this = a / b, remainder = a % b
BN_copy(&m_number, &a.m_number);
*remainder = (dword)BN_div_word(&m_number, b);
}
void Div (const BigNum & a, const BigNum & b, BigNum * remainder)
{
// this = a / b, remainder = a % b
// either this or remainder may be nil
BN_div(this ? &m_number : nil, remainder ? &remainder->m_number : nil,
&a.m_number, &b.m_number, GetContext());
}
void FromData_BE (unsigned bytes, const void * data)
{
BN_bin2bn((const unsigned char *)data, bytes, &m_number);
}
void FromData_LE (unsigned bytes, const void * data);
unsigned char * GetData_BE (unsigned * bytes) const;
unsigned char * GetData_LE (unsigned * bytes) const;
bool IsPrime () const
{
// Cyan's code uses 3 checks, so we'll follow suit.
// This provides an accurate answer to p < 0.015625
return BN_is_prime_fasttest(&m_number, 3, nil, GetContext(), nil, 1) > 0;
}
void Mod (const BigNum & a, const BigNum & b)
{
// this = a % b
BN_div(nil, &m_number, &a.m_number, &b.m_number, GetContext());
}
void Mul (const BigNum & a, dword b)
{
// this = a * b
BN_copy(&m_number, &a.m_number);
BN_mul_word(&m_number, b);
}
void Mul (const BigNum & a, const BigNum & b)
{
// this = a * b
BN_mul(&m_number, &a.m_number, &b.m_number, GetContext());
}
void PowMod (dword a, const BigNum & b, const BigNum & c)
{
// this = a ^ b % c
PowMod(BigNum(a), b, c);
}
void PowMod (const BigNum & a, const BigNum & b, const BigNum & c)
{
// this = a ^ b % c
BN_mod_exp(&m_number, &a.m_number, &b.m_number, &c.m_number, GetContext());
}
void Rand (const BigNum & a, BigNum * seed)
{
// this = random number less than a
unsigned bits = BN_num_bits(&a.m_number);
do
Rand(bits, seed);
while (Compare(a) >= 0);
}
void Rand (unsigned bits, BigNum * seed);
void RandPrime (unsigned bits, BigNum * seed);
void Set (const BigNum & a);
void Set (unsigned a);
void SetBits (unsigned setBitsOffset, unsigned setBitsCount);
void SetOne ();
void SetZero ();
void Shl (const BigNum & a, unsigned b);
void Shr (const BigNum & a, unsigned b);
void Square (const BigNum & a);
void Sub (const BigNum & a, Val b);
void Sub (const BigNum & a, const BigNum & b);
void ToStr (BigNum * buffer, Val radix) const;
void RandPrime (unsigned bits, BigNum * seed)
{
BN_generate_prime(&m_number, bits, 1, nil, nil, nil, nil);
}
void Set (const BigNum & a)
{
BN_copy(&m_number, &a.m_number);
}
void Set (unsigned a)
{
BN_set_word(&m_number, a);
}
void SetOne () { Set(1); }
void SetZero () { Set(0); }
void Shl (const BigNum & a, unsigned b)
{
// this = a << b
BN_lshift(&m_number, &a.m_number, b);
}
void Shr (const BigNum & a, unsigned b)
{
// this = a >> b
BN_rshift(&m_number, &a.m_number, b);
}
void Sub (const BigNum & a, dword b)
{
// this = a - b
BN_copy(&m_number, &a.m_number);
BN_sub_word(&m_number, b);
}
void Sub (const BigNum & a, const BigNum & b)
{
// this = a - b
BN_sub(&m_number, &a.m_number, &b.m_number);
}
};

2
Sources/Plasma/PubUtilLib/plAgeDescription/CMakeLists.txt

@ -3,8 +3,6 @@ include_directories("../../FeatureLib")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plAgeDescription_SOURCES
plAgeDescription.cpp
plAgeManifest.cpp

2
Sources/Plasma/PubUtilLib/plAgeLoader/CMakeLists.txt

@ -4,8 +4,6 @@ include_directories("../../NucleusLib/inc")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plAgeLoader_SOURCES
plAgeLoader.cpp
plAgeLoaderPaging.cpp

1
Sources/Plasma/PubUtilLib/plAvatar/CMakeLists.txt

@ -4,7 +4,6 @@ include_directories("../../NucleusLib/inc")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENAL_INCLUDE_DIR})
set(plAvatar_SOURCES

2
Sources/Plasma/PubUtilLib/plEncryption/CMakeLists.txt

@ -2,8 +2,6 @@ include_directories("../../CoreLib")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plEncryption_SOURCES
plChecksum.cpp
)

2
Sources/Plasma/PubUtilLib/plGLight/CMakeLists.txt

@ -3,8 +3,6 @@ include_directories("../../NucleusLib/inc")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plGLight_SOURCES
plDirectShadowMaster.cpp
plLightInfo.cpp

1
Sources/Plasma/PubUtilLib/plInputCore/CMakeLists.txt

@ -4,7 +4,6 @@ include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${DirectX_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENAL_INCLUDE_DIR})
set(plInputCore_SOURCES

1
Sources/Plasma/PubUtilLib/plModifier/CMakeLists.txt

@ -6,7 +6,6 @@ include_directories("../../PubUtilLib")
include_directories(${PHYSX_INCLUDE_DIRS})
include_directories(${OPENAL_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIR})
set(plModifier_SOURCES
plAnimEventModifier.cpp

2
Sources/Plasma/PubUtilLib/plNetClient/CMakeLists.txt

@ -4,8 +4,6 @@ include_directories("../../NucleusLib/inc")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plNetClient_SOURCES
plLinkEffectsMgr.cpp
plNetCliAgeJoiner.cpp

2
Sources/Plasma/PubUtilLib/plNetClientComm/CMakeLists.txt

@ -4,8 +4,6 @@ include_directories("../../NucleusLib/inc")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plNetClientComm_SOURCES
plNetClientComm.cpp
)

2
Sources/Plasma/PubUtilLib/plNetTransport/CMakeLists.txt

@ -3,8 +3,6 @@ include_directories("../../NucleusLib/inc")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plNetTransport_SOURCES
plNetTransport.cpp
plNetTransportMember.cpp

1
Sources/Plasma/PubUtilLib/plPhysX/CMakeLists.txt

@ -4,7 +4,6 @@ include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${PHYSX_INCLUDE_DIRS})
include_directories(${OPENSSL_INCLUDE_DIR})
set(plPhysX_SOURCES
plLOSDispatch.cpp

2
Sources/Plasma/PubUtilLib/plStatGather/CMakeLists.txt

@ -3,8 +3,6 @@ include_directories("../../NucleusLib/inc")
include_directories("../../NucleusLib")
include_directories("../../PubUtilLib")
include_directories(${OPENSSL_INCLUDE_DIR})
set(plStatGather_SOURCES
plAutoProfile.cpp
plCalculatedProfiles.cpp

1
Sources/Tools/MaxMain/CMakeLists.txt

@ -7,7 +7,6 @@ include_directories("../../Plasma/NucleusLib/inc")
include_directories("../../Plasma/PubUtilLib")
include_directories("../../Plasma/PubUtilLib/inc")
include_directories(${3dsm_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${OPENAL_INCLUDE_DIR})
include_directories(${PHYSX_INCLUDE_DIRS})
include_directories(${PYTHON_INCLUDE_DIR})

Loading…
Cancel
Save