mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-18 11:19:10 +00:00
Move pnUtBigNum to pnEncryption/plBigNum.
This commit is contained in:
@ -3,11 +3,13 @@ include_directories("../../NucleusLib")
|
||||
include_directories(${OPENSSL_INCLUDE_DIR})
|
||||
|
||||
set(pnEncryption_SOURCES
|
||||
plBigNum.cpp
|
||||
plChallengeHash.cpp
|
||||
plChecksum.cpp
|
||||
)
|
||||
|
||||
set(pnEncryption_HEADERS
|
||||
plBigNum.h
|
||||
plChallengeHash.h
|
||||
plChecksum.h
|
||||
plRandom.h
|
||||
|
145
Sources/Plasma/NucleusLib/pnEncryption/plBigNum.cpp
Normal file
145
Sources/Plasma/NucleusLib/pnEncryption/plBigNum.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
|
||||
#include "plBigNum.h"
|
||||
#include <openssl/rand.h>
|
||||
#include <algorithm>
|
||||
|
||||
static inline void byteswap(size_t size, uint8_t* data)
|
||||
{
|
||||
for (size_t i = 0; i < (size / 2); ++i)
|
||||
std::swap(data[i], data[size - i - 1]);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
*
|
||||
* plBigNum public methods
|
||||
*
|
||||
***/
|
||||
|
||||
plBigNum::plBigNum () : m_context(nil)
|
||||
{
|
||||
BN_init(&m_number);
|
||||
}
|
||||
|
||||
plBigNum::plBigNum(const plBigNum& a) : m_context(nil)
|
||||
{
|
||||
BN_init(&m_number);
|
||||
BN_copy(&m_number, &a.m_number);
|
||||
}
|
||||
|
||||
plBigNum::plBigNum(unsigned a) : m_context(nil)
|
||||
{
|
||||
BN_init(&m_number);
|
||||
BN_set_word(&m_number, a);
|
||||
}
|
||||
|
||||
plBigNum::plBigNum(unsigned bytes, const void* data, bool le) : m_context(nil)
|
||||
{
|
||||
BN_init(&m_number);
|
||||
if (le)
|
||||
FromData_LE(bytes, data);
|
||||
else
|
||||
FromData_BE(bytes, data);
|
||||
}
|
||||
|
||||
plBigNum::~plBigNum ()
|
||||
{
|
||||
if (m_context)
|
||||
BN_CTX_free(m_context);
|
||||
BN_free(&m_number);
|
||||
}
|
||||
|
||||
int plBigNum::Compare(uint32_t a) const
|
||||
{
|
||||
// -1 if (this < a)
|
||||
// 0 if (this == a)
|
||||
// 1 if (this > a)
|
||||
|
||||
if (BN_is_word(&m_number, a))
|
||||
return 0;
|
||||
|
||||
// This returns 0xFFFFFFFFL if the number is bigger than one uint16_t, so
|
||||
// it doesn't need any size check
|
||||
if (BN_get_word(&m_number) < a)
|
||||
return -1;
|
||||
|
||||
// Not less or equal, must be greater
|
||||
return 1;
|
||||
}
|
||||
|
||||
void plBigNum::FromData_LE(uint32_t bytes, const void* data)
|
||||
{
|
||||
uint8_t* buffer = new uint8_t[bytes];
|
||||
memcpy(buffer, data, bytes);
|
||||
byteswap(bytes, buffer);
|
||||
BN_bin2bn(buffer, bytes, &m_number);
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
uint8_t* plBigNum::GetData_BE(uint32_t* bytes) const
|
||||
{
|
||||
*bytes = BN_num_bytes(&m_number);
|
||||
uint8_t* data = new uint8_t[*bytes];
|
||||
BN_bn2bin(&m_number, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
uint8_t* plBigNum::GetData_LE(uint32_t* bytes) const
|
||||
{
|
||||
*bytes = BN_num_bytes(&m_number);
|
||||
uint8_t* data = new uint8_t[*bytes];
|
||||
BN_bn2bin(&m_number, data);
|
||||
byteswap(*bytes, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
void plBigNum::Rand(uint32_t bits, plBigNum* seed)
|
||||
{
|
||||
// this = random number with bits or fewer bits
|
||||
|
||||
uint32_t seedbytes;
|
||||
uint8_t* seedData = seed->GetData_BE(&seedbytes);
|
||||
RAND_seed(seedData, seedbytes);
|
||||
BN_rand(&m_number, bits, 0, 0);
|
||||
delete[] seedData;
|
||||
}
|
226
Sources/Plasma/NucleusLib/pnEncryption/plBigNum.h
Normal file
226
Sources/Plasma/NucleusLib/pnEncryption/plBigNum.h
Normal file
@ -0,0 +1,226 @@
|
||||
/*==LICENSE==*
|
||||
|
||||
CyanWorlds.com Engine - MMOG client, server and tools
|
||||
Copyright (C) 2011 Cyan Worlds, Inc.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Additional permissions under GNU GPL version 3 section 7
|
||||
|
||||
If you modify this Program, or any covered work, by linking or
|
||||
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||||
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||||
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||||
(or a modified version of those libraries),
|
||||
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||||
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||||
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||||
licensors of this Program grant you additional
|
||||
permission to convey the resulting work. Corresponding Source for a
|
||||
non-source form of such a combination shall include the source code for
|
||||
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||||
work.
|
||||
|
||||
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
or by snail mail at:
|
||||
Cyan Worlds, Inc.
|
||||
14617 N Newport Hwy
|
||||
Mead, WA 99021
|
||||
|
||||
*==LICENSE==*/
|
||||
|
||||
#ifndef plBigNum_inc
|
||||
#define plBigNum_inc
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* plBigNum class
|
||||
*
|
||||
***/
|
||||
|
||||
#include "HeadSpin.h"
|
||||
#include <openssl/bn.h>
|
||||
|
||||
class plBigNum
|
||||
{
|
||||
private:
|
||||
BIGNUM m_number;
|
||||
mutable BN_CTX* m_context;
|
||||
|
||||
BN_CTX* GetContext() const
|
||||
{
|
||||
if (!m_context)
|
||||
m_context = BN_CTX_new();
|
||||
return m_context;
|
||||
}
|
||||
|
||||
public:
|
||||
plBigNum();
|
||||
plBigNum(const plBigNum& a);
|
||||
plBigNum(uint32_t a);
|
||||
plBigNum(uint32_t bytess, const void* data, bool le=false);
|
||||
~plBigNum();
|
||||
|
||||
plBigNum& operator=(const plBigNum& 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 plBigNum& a, uint32_t b)
|
||||
{
|
||||
// this = a + b
|
||||
BN_copy(&m_number, &a.m_number);
|
||||
BN_add_word(&m_number, b);
|
||||
}
|
||||
|
||||
void Add(const plBigNum& a, const plBigNum& b)
|
||||
{
|
||||
// this = a + b
|
||||
BN_add(&m_number, &a.m_number, &b.m_number);
|
||||
}
|
||||
|
||||
int Compare(uint32_t a) const;
|
||||
|
||||
int Compare(const plBigNum& a) const
|
||||
{
|
||||
return BN_cmp(&m_number, &a.m_number);
|
||||
}
|
||||
|
||||
bool isZero() const
|
||||
{
|
||||
return BN_is_zero(&m_number);
|
||||
}
|
||||
|
||||
void Div(const plBigNum& a, uint32_t b, uint32_t* remainder)
|
||||
{
|
||||
// this = a / b, remainder = a % b
|
||||
BN_copy(&m_number, &a.m_number);
|
||||
*remainder = (uint32_t)BN_div_word(&m_number, b);
|
||||
}
|
||||
|
||||
void Div(const plBigNum& a, const plBigNum& b, plBigNum* 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(uint32_t bytess, const void* data)
|
||||
{
|
||||
BN_bin2bn((const uint8_t*)data, bytess, &m_number);
|
||||
}
|
||||
|
||||
void FromData_LE(uint32_t bytess, const void* data);
|
||||
|
||||
uint8_t* GetData_BE(uint32_t* bytess) const;
|
||||
uint8_t* GetData_LE(uint32_t* bytess) 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 plBigNum& a, const plBigNum& b)
|
||||
{
|
||||
// this = a % b
|
||||
BN_div(nil, &m_number, &a.m_number, &b.m_number, GetContext());
|
||||
}
|
||||
|
||||
void Mul(const plBigNum& a, uint32_t b)
|
||||
{
|
||||
// this = a * b
|
||||
BN_copy(&m_number, &a.m_number);
|
||||
BN_mul_word(&m_number, b);
|
||||
}
|
||||
|
||||
void Mul(const plBigNum& a, const plBigNum& b)
|
||||
{
|
||||
// this = a * b
|
||||
BN_mul(&m_number, &a.m_number, &b.m_number, GetContext());
|
||||
}
|
||||
|
||||
void PowMod(uint32_t a, const plBigNum& b, const plBigNum& c)
|
||||
{
|
||||
// this = a ^ b % c
|
||||
PowMod(plBigNum(a), b, c);
|
||||
}
|
||||
|
||||
void PowMod(const plBigNum& a, const plBigNum& b, const plBigNum& c)
|
||||
{
|
||||
// this = a ^ b % c
|
||||
BN_mod_exp(&m_number, &a.m_number, &b.m_number, &c.m_number, GetContext());
|
||||
}
|
||||
|
||||
void Rand(const plBigNum& a, plBigNum* seed)
|
||||
{
|
||||
// this = random number less than a
|
||||
int bits = BN_num_bits(&a.m_number);
|
||||
do
|
||||
Rand(bits, seed);
|
||||
while (Compare(a) >= 0);
|
||||
}
|
||||
|
||||
void Rand(uint32_t bits, plBigNum* seed);
|
||||
|
||||
void RandPrime(uint32_t bits, plBigNum* seed)
|
||||
{
|
||||
BN_generate_prime(&m_number, bits, 1, nil, nil, nil, nil);
|
||||
}
|
||||
|
||||
void Set(const plBigNum& a)
|
||||
{
|
||||
BN_copy(&m_number, &a.m_number);
|
||||
}
|
||||
|
||||
void Set(uint32_t a)
|
||||
{
|
||||
BN_set_word(&m_number, a);
|
||||
}
|
||||
|
||||
void SetOne() { Set(1); }
|
||||
void SetZero() { Set(0); }
|
||||
|
||||
void Shl(const plBigNum& a, uint32_t b)
|
||||
{
|
||||
// this = a << b
|
||||
BN_lshift(&m_number, &a.m_number, b);
|
||||
}
|
||||
|
||||
void Shr(const plBigNum& a, uint32_t b)
|
||||
{
|
||||
// this = a >> b
|
||||
BN_rshift(&m_number, &a.m_number, b);
|
||||
}
|
||||
|
||||
void Sub(const plBigNum& a, uint32_t b)
|
||||
{
|
||||
// this = a - b
|
||||
BN_copy(&m_number, &a.m_number);
|
||||
BN_sub_word(&m_number, b);
|
||||
}
|
||||
|
||||
void Sub(const plBigNum& a, const plBigNum& b)
|
||||
{
|
||||
// this = a - b
|
||||
BN_sub(&m_number, &a.m_number, &b.m_number);
|
||||
}
|
||||
};
|
||||
#endif // plBigNum_inc
|
@ -41,7 +41,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||||
*==LICENSE==*/
|
||||
|
||||
#include "plChallengeHash.h"
|
||||
#include "pnUtils/pnUtils.h"
|
||||
|
||||
ShaDigest fSeed;
|
||||
|
||||
|
Reference in New Issue
Block a user