You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.9 KiB
146 lines
3.9 KiB
14 years ago
|
/*==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/>.
|
||
|
|
||
13 years ago
|
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.
|
||
|
|
||
14 years ago
|
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==*/
|
||
|
|
||
13 years ago
|
#include "plBigNum.h"
|
||
14 years ago
|
#include <openssl/rand.h>
|
||
|
#include <algorithm>
|
||
14 years ago
|
|
||
13 years ago
|
static inline void byteswap(size_t size, uint8_t* data)
|
||
14 years ago
|
{
|
||
|
for (size_t i = 0; i < (size / 2); ++i)
|
||
|
std::swap(data[i], data[size - i - 1]);
|
||
14 years ago
|
}
|
||
|
|
||
|
/****************************************************************************
|
||
|
*
|
||
13 years ago
|
* plBigNum public methods
|
||
14 years ago
|
*
|
||
|
***/
|
||
|
|
||
13 years ago
|
plBigNum::plBigNum () : m_context(nil)
|
||
14 years ago
|
{
|
||
14 years ago
|
BN_init(&m_number);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
plBigNum::plBigNum(const plBigNum& a) : m_context(nil)
|
||
14 years ago
|
{
|
||
14 years ago
|
BN_init(&m_number);
|
||
|
BN_copy(&m_number, &a.m_number);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
plBigNum::plBigNum(unsigned a) : m_context(nil)
|
||
14 years ago
|
{
|
||
14 years ago
|
BN_init(&m_number);
|
||
|
BN_set_word(&m_number, a);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
plBigNum::plBigNum(unsigned bytes, const void* data, bool le) : m_context(nil)
|
||
14 years ago
|
{
|
||
14 years ago
|
BN_init(&m_number);
|
||
|
if (le)
|
||
|
FromData_LE(bytes, data);
|
||
|
else
|
||
|
FromData_BE(bytes, data);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
plBigNum::~plBigNum ()
|
||
14 years ago
|
{
|
||
14 years ago
|
if (m_context)
|
||
|
BN_CTX_free(m_context);
|
||
|
BN_free(&m_number);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
int plBigNum::Compare(uint32_t a) const
|
||
|
{
|
||
14 years ago
|
// -1 if (this < a)
|
||
|
// 0 if (this == a)
|
||
|
// 1 if (this > a)
|
||
|
|
||
14 years ago
|
if (BN_is_word(&m_number, a))
|
||
|
return 0;
|
||
14 years ago
|
|
||
13 years ago
|
// This returns 0xFFFFFFFFL if the number is bigger than one uint16_t, so
|
||
14 years ago
|
// it doesn't need any size check
|
||
|
if (BN_get_word(&m_number) < a)
|
||
14 years ago
|
return -1;
|
||
|
|
||
14 years ago
|
// Not less or equal, must be greater
|
||
|
return 1;
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
void plBigNum::FromData_LE(uint32_t bytes, const void* data)
|
||
14 years ago
|
{
|
||
13 years ago
|
uint8_t* buffer = new uint8_t[bytes];
|
||
14 years ago
|
memcpy(buffer, data, bytes);
|
||
|
byteswap(bytes, buffer);
|
||
|
BN_bin2bn(buffer, bytes, &m_number);
|
||
13 years ago
|
delete[] buffer;
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
uint8_t* plBigNum::GetData_BE(uint32_t* bytes) const
|
||
14 years ago
|
{
|
||
|
*bytes = BN_num_bytes(&m_number);
|
||
13 years ago
|
uint8_t* data = new uint8_t[*bytes];
|
||
14 years ago
|
BN_bn2bin(&m_number, data);
|
||
|
return data;
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
uint8_t* plBigNum::GetData_LE(uint32_t* bytes) const
|
||
14 years ago
|
{
|
||
|
*bytes = BN_num_bytes(&m_number);
|
||
13 years ago
|
uint8_t* data = new uint8_t[*bytes];
|
||
14 years ago
|
BN_bn2bin(&m_number, data);
|
||
|
byteswap(*bytes, data);
|
||
|
return data;
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
void plBigNum::Rand(uint32_t bits, plBigNum* seed)
|
||
14 years ago
|
{
|
||
14 years ago
|
// this = random number with bits or fewer bits
|
||
|
|
||
13 years ago
|
uint32_t seedbytes;
|
||
|
uint8_t* seedData = seed->GetData_BE(&seedbytes);
|
||
13 years ago
|
RAND_seed(seedData, seedbytes);
|
||
14 years ago
|
BN_rand(&m_number, bits, 0, 0);
|
||
13 years ago
|
delete[] seedData;
|
||
14 years ago
|
}
|