Files
TCPlayerbotCore/src/server/shared/Cryptography/BigNumber.cpp
T

201 lines
3.9 KiB
C++
Raw Normal View History

/*
2011-01-01 15:01:13 +01:00
* Copyright (C) 2008-2011 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* 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 2 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/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2010-06-07 07:43:20 -06:00
#include "Cryptography/BigNumber.h"
2008-10-02 16:23:55 -05:00
#include <openssl/bn.h>
2011-10-18 14:59:23 +02:00
#include <openssl/crypto.h>
2008-10-02 16:23:55 -05:00
#include <algorithm>
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber::BigNumber()
2011-10-18 14:59:23 +02:00
: _bn(BN_new())
, _array(NULL)
{ }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber::BigNumber(const BigNumber &bn)
2011-10-18 14:59:23 +02:00
: _bn(BN_dup(bn._bn))
, _array(NULL)
{ }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber::BigNumber(uint32 val)
2011-10-18 14:59:23 +02:00
: _bn(BN_new())
, _array(NULL)
2008-10-02 16:23:55 -05:00
{
BN_set_word(_bn, val);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber::~BigNumber()
{
BN_free(_bn);
2011-10-18 14:59:23 +02:00
delete[] _array;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void BigNumber::SetDword(uint32 val)
{
BN_set_word(_bn, val);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void BigNumber::SetQword(uint64 val)
{
BN_add_word(_bn, (uint32)(val >> 32));
BN_lshift(_bn, _bn, 32);
BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void BigNumber::SetBinary(const uint8 *bytes, int len)
{
uint8 t[1000];
for (int i = 0; i < len; i++) t[i] = bytes[len - 1 - i];
BN_bin2bn(t, len, _bn);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void BigNumber::SetHexStr(const char *str)
{
BN_hex2bn(&_bn, str);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void BigNumber::SetRand(int numbits)
{
BN_rand(_bn, numbits, 0, 1);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::operator=(const BigNumber &bn)
{
2011-10-18 14:59:23 +02:00
if (this == &bn)
return *this;
2008-10-02 16:23:55 -05:00
BN_copy(_bn, bn._bn);
return *this;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::operator+=(const BigNumber &bn)
{
BN_add(_bn, _bn, bn._bn);
return *this;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::operator-=(const BigNumber &bn)
{
BN_sub(_bn, _bn, bn._bn);
return *this;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::operator*=(const BigNumber &bn)
{
BN_CTX *bnctx;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bnctx = BN_CTX_new();
BN_mul(_bn, _bn, bn._bn, bnctx);
BN_CTX_free(bnctx);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return *this;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::operator/=(const BigNumber &bn)
{
BN_CTX *bnctx;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bnctx = BN_CTX_new();
BN_div(_bn, NULL, _bn, bn._bn, bnctx);
BN_CTX_free(bnctx);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return *this;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::operator%=(const BigNumber &bn)
{
BN_CTX *bnctx;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bnctx = BN_CTX_new();
BN_mod(_bn, _bn, bn._bn, bnctx);
BN_CTX_free(bnctx);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return *this;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::Exp(const BigNumber &bn)
{
BigNumber ret;
BN_CTX *bnctx;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bnctx = BN_CTX_new();
BN_exp(ret._bn, _bn, bn._bn, bnctx);
BN_CTX_free(bnctx);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return ret;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BigNumber BigNumber::ModExp(const BigNumber &bn1, const BigNumber &bn2)
{
BigNumber ret;
BN_CTX *bnctx;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bnctx = BN_CTX_new();
BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
BN_CTX_free(bnctx);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return ret;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
int BigNumber::GetNumBytes(void)
{
return BN_num_bytes(_bn);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
uint32 BigNumber::AsDword()
{
return (uint32)BN_get_word(_bn);
}
2009-10-17 15:51:44 -07:00
bool BigNumber::isZero() const
{
return BN_is_zero(_bn)!=0;
}
2009-10-17 15:51:44 -07:00
uint8 *BigNumber::AsByteArray(int minSize, bool reverse)
2008-10-02 16:23:55 -05:00
{
int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if (_array)
{
delete[] _array;
_array = NULL;
}
_array = new uint8[length];
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// If we need more bytes than length of BigNumber set the rest to 0
if (length > GetNumBytes())
memset((void*)_array, 0, length);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
BN_bn2bin(_bn, (unsigned char *)_array);
2009-10-17 15:51:44 -07:00
2010-04-11 20:20:38 +02:00
if (reverse)
std::reverse(_array, _array + length);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return _array;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
const char *BigNumber::AsHexStr()
{
return BN_bn2hex(_bn);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
const char *BigNumber::AsDecStr()
{
return BN_bn2dec(_bn);
}
2009-02-17 20:07:49 -06:00