AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
BigNumber Class Reference

#include "BigNumber.h"

Public Member Functions

 BigNumber ()
 
 BigNumber (BigNumber const &bn)
 
 BigNumber (uint32 v)
 
 BigNumber (int32 v)
 
 BigNumber (std::string const &v)
 
template<size_t Size>
 BigNumber (std::array< uint8, Size > const &v, bool littleEndian=true)
 
 ~BigNumber ()
 
void SetDword (int32)
 
void SetDword (uint32)
 
void SetQword (uint64)
 
void SetBinary (uint8 const *bytes, int32 len, bool littleEndian=true)
 
template<typename Container >
auto SetBinary (Container const &c, bool littleEndian=true) -> std::enable_if_t<!std::is_pointer_v< std::decay_t< Container > > >
 
bool SetHexStr (char const *str)
 
bool SetHexStr (std::string const &str)
 
void SetRand (int32 numbits)
 
BigNumberoperator= (BigNumber const &bn)
 
BigNumberoperator+= (BigNumber const &bn)
 
BigNumber operator+ (BigNumber const &bn) const
 
BigNumberoperator-= (BigNumber const &bn)
 
BigNumber operator- (BigNumber const &bn) const
 
BigNumberoperator*= (BigNumber const &bn)
 
BigNumber operator* (BigNumber const &bn) const
 
BigNumberoperator/= (BigNumber const &bn)
 
BigNumber operator/ (BigNumber const &bn) const
 
BigNumberoperator%= (BigNumber const &bn)
 
BigNumber operator% (BigNumber const &bn) const
 
BigNumberoperator<<= (int n)
 
BigNumber operator<< (int n) const
 
int CompareTo (BigNumber const &bn) const
 
bool operator<= (BigNumber const &bn) const
 
bool operator== (BigNumber const &bn) const
 
bool operator>= (BigNumber const &bn) const
 
bool operator< (BigNumber const &bn) const
 
bool operator> (BigNumber const &bn) const
 
bool IsZero () const
 
bool IsNegative () const
 
BigNumber ModExp (BigNumber const &bn1, BigNumber const &bn2) const
 
BigNumber Exp (BigNumber const &) const
 
int32 GetNumBytes () const
 
struct bignum_st * BN ()
 
struct bignum_st const * BN () const
 
uint32 AsDword () const
 
void GetBytes (uint8 *buf, size_t bufsize, bool littleEndian=true) const
 
std::vector< uint8ToByteVector (int32 minSize=0, bool littleEndian=true) const
 
template<std::size_t Size>
std::array< uint8, Size > ToByteArray (bool littleEndian=true) const
 
std::string AsHexStr () const
 
std::string AsDecStr () const
 

Private Attributes

struct bignum_st * _bn
 

Detailed Description

Constructor & Destructor Documentation

◆ BigNumber() [1/6]

BigNumber::BigNumber ( )
26 : _bn(BN_new())
27{ }
struct bignum_st * _bn
Definition: BigNumber.h:134

◆ BigNumber() [2/6]

BigNumber::BigNumber ( BigNumber const &  bn)
30 : _bn(BN_dup(bn.BN()))
31{ }

◆ BigNumber() [3/6]

BigNumber::BigNumber ( uint32  v)
inline
33: BigNumber() { SetDword(v); }
BigNumber()
Definition: BigNumber.cpp:25
void SetDword(int32)
Definition: BigNumber.cpp:38

◆ BigNumber() [4/6]

BigNumber::BigNumber ( int32  v)
inline
34: BigNumber() { SetDword(v); }

◆ BigNumber() [5/6]

BigNumber::BigNumber ( std::string const &  v)
inline
35: BigNumber() { SetHexStr(v); }
bool SetHexStr(char const *str)
Definition: BigNumber.cpp:78

◆ BigNumber() [6/6]

template<size_t Size>
BigNumber::BigNumber ( std::array< uint8, Size > const &  v,
bool  littleEndian = true 
)
inline
38: BigNumber() { SetBinary(v.data(), Size, littleEndian); }
void SetBinary(uint8 const *bytes, int32 len, bool littleEndian=true)
Definition: BigNumber.cpp:57

◆ ~BigNumber()

BigNumber::~BigNumber ( )
34{
35 BN_free(_bn);
36}

References _bn.

Member Function Documentation

◆ AsDecStr()

std::string BigNumber::AsDecStr ( ) const
241{
242 char* ch = BN_bn2dec(_bn);
243 std::string ret = ch;
244 OPENSSL_free(ch);
245 return ret;
246}

References _bn.

◆ AsDword()

uint32 BigNumber::AsDword ( ) const
184{
185 return (uint32)BN_get_word(_bn);
186}
std::uint32_t uint32
Definition: Define.h:108

References _bn.

◆ AsHexStr()

std::string BigNumber::AsHexStr ( ) const
233{
234 char* ch = BN_bn2hex(_bn);
235 std::string ret = ch;
236 OPENSSL_free(ch);
237 return ret;
238}

References _bn.

◆ BN() [1/2]

struct bignum_st * BigNumber::BN ( )
inline
114{ return _bn; }

◆ BN() [2/2]

struct bignum_st const * BigNumber::BN ( ) const
inline
115{ return _bn; }

◆ CompareTo()

int BigNumber::CompareTo ( BigNumber const &  bn) const
150{
151 return BN_cmp(_bn, bn._bn);
152}

References _bn.

◆ Exp()

BigNumber BigNumber::Exp ( BigNumber const &  bn) const
155{
156 BigNumber ret;
157 BN_CTX *bnctx;
158
159 bnctx = BN_CTX_new();
160 BN_exp(ret._bn, _bn, bn._bn, bnctx);
161 BN_CTX_free(bnctx);
162
163 return ret;
164}
Definition: BigNumber.h:29

References _bn.

◆ GetBytes()

void BigNumber::GetBytes ( uint8 buf,
size_t  bufsize,
bool  littleEndian = true 
) const
199{
200#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
201 int nBytes = GetNumBytes();
202 ASSERT(nBytes >= 0, "Bignum has negative number of bytes ({}).", nBytes);
203 std::size_t numBytes = static_cast<std::size_t>(nBytes);
204
205 // too large to store
206 ASSERT(numBytes <= bufsize, "Buffer of size {} is too small to hold bignum with {} bytes.\n", bufsize, numBytes);
207
208 // If we need more bytes than length of BigNumber set the rest to 0
209 if (numBytes < bufsize)
210 memset((void*)buf, 0, bufsize);
211
212 BN_bn2bin(_bn, buf + (bufsize - numBytes));
213
214 // openssl's BN stores data internally in big endian format, reverse if little endian desired
215 if (littleEndian)
216 std::reverse(buf, buf + bufsize);
217#else
218 int res = littleEndian ? BN_bn2lebinpad(_bn, buf, bufsize) : BN_bn2binpad(_bn, buf, bufsize);
219 ASSERT(res > 0, "Buffer of size {} is too small to hold bignum with {} bytes.\n", bufsize, BN_num_bytes(_bn));
220#endif
221}
#define ASSERT
Definition: Errors.h:68
int32 GetNumBytes() const
Definition: BigNumber.cpp:178

References _bn, ASSERT, and GetNumBytes().

Referenced by ToByteVector().

◆ GetNumBytes()

int32 BigNumber::GetNumBytes ( ) const
179{
180 return BN_num_bytes(_bn);
181}

References _bn.

Referenced by GetBytes(), GetCheckPacketSize(), and ToByteVector().

◆ IsNegative()

bool BigNumber::IsNegative ( ) const
194{
195 return BN_is_negative(_bn);
196}

References _bn.

◆ IsZero()

bool BigNumber::IsZero ( ) const
189{
190 return BN_is_zero(_bn);
191}

References _bn.

◆ ModExp()

BigNumber BigNumber::ModExp ( BigNumber const &  bn1,
BigNumber const &  bn2 
) const
167{
168 BigNumber ret;
169 BN_CTX *bnctx;
170
171 bnctx = BN_CTX_new();
172 BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
173 BN_CTX_free(bnctx);
174
175 return ret;
176}

References _bn.

Referenced by Acore::Crypto::SRP6::_B(), Acore::Crypto::SRP6::CalculateVerifier(), and Acore::Crypto::SRP6::VerifyChallengeResponse().

◆ operator%()

BigNumber BigNumber::operator% ( BigNumber const &  bn) const
inline
87 {
88 BigNumber t(*this);
89 return t %= bn;
90 }

◆ operator%=()

BigNumber & BigNumber::operator%= ( BigNumber const &  bn)
133{
134 BN_CTX *bnctx;
135
136 bnctx = BN_CTX_new();
137 BN_mod(_bn, _bn, bn._bn, bnctx);
138 BN_CTX_free(bnctx);
139
140 return *this;
141}

References _bn.

◆ operator*()

BigNumber BigNumber::operator* ( BigNumber const &  bn) const
inline
73 {
74 BigNumber t(*this);
75 return t *= bn;
76 }

◆ operator*=()

BigNumber & BigNumber::operator*= ( BigNumber const &  bn)
111{
112 BN_CTX *bnctx;
113
114 bnctx = BN_CTX_new();
115 BN_mul(_bn, _bn, bn._bn, bnctx);
116 BN_CTX_free(bnctx);
117
118 return *this;
119}

References _bn.

◆ operator+()

BigNumber BigNumber::operator+ ( BigNumber const &  bn) const
inline
59 {
60 BigNumber t(*this);
61 return t += bn;
62 }

◆ operator+=()

BigNumber & BigNumber::operator+= ( BigNumber const &  bn)
99{
100 BN_add(_bn, _bn, bn._bn);
101 return *this;
102}

References _bn.

◆ operator-()

BigNumber BigNumber::operator- ( BigNumber const &  bn) const
inline
66 {
67 BigNumber t(*this);
68 return t -= bn;
69 }

◆ operator-=()

BigNumber & BigNumber::operator-= ( BigNumber const &  bn)
105{
106 BN_sub(_bn, _bn, bn._bn);
107 return *this;
108}

References _bn.

◆ operator/()

BigNumber BigNumber::operator/ ( BigNumber const &  bn) const
inline
80 {
81 BigNumber t(*this);
82 return t /= bn;
83 }

◆ operator/=()

BigNumber & BigNumber::operator/= ( BigNumber const &  bn)
122{
123 BN_CTX *bnctx;
124
125 bnctx = BN_CTX_new();
126 BN_div(_bn, nullptr, _bn, bn._bn, bnctx);
127 BN_CTX_free(bnctx);
128
129 return *this;
130}

References _bn.

◆ operator<()

bool BigNumber::operator< ( BigNumber const &  bn) const
inline
103{ return (CompareTo(bn) < 0); }
int CompareTo(BigNumber const &bn) const
Definition: BigNumber.cpp:149

◆ operator<<()

BigNumber BigNumber::operator<< ( int  n) const
inline
94 {
95 BigNumber t(*this);
96 return t <<= n;
97 }

◆ operator<<=()

BigNumber & BigNumber::operator<<= ( int  n)
144{
145 BN_lshift(_bn, _bn, n);
146 return *this;
147}

References _bn.

◆ operator<=()

bool BigNumber::operator<= ( BigNumber const &  bn) const
inline
100{ return (CompareTo(bn) <= 0); }

◆ operator=()

BigNumber & BigNumber::operator= ( BigNumber const &  bn)
90{
91 if (this == &bn)
92 return *this;
93
94 BN_copy(_bn, bn._bn);
95 return *this;
96}

References _bn.

◆ operator==()

bool BigNumber::operator== ( BigNumber const &  bn) const
inline
101{ return (CompareTo(bn) == 0); }

◆ operator>()

bool BigNumber::operator> ( BigNumber const &  bn) const
inline
104{ return (CompareTo(bn) > 0); }

◆ operator>=()

bool BigNumber::operator>= ( BigNumber const &  bn) const
inline
102{ return (CompareTo(bn) >= 0); }

◆ SetBinary() [1/2]

template<typename Container >
auto BigNumber::SetBinary ( Container const &  c,
bool  littleEndian = true 
) -> std::enable_if_t<!std::is_pointer_v<std::decay_t<Container>>>
inline
48{ SetBinary(std::data(c), std::size(c), littleEndian); }

References SetBinary().

Referenced by SetBinary().

◆ SetBinary() [2/2]

void BigNumber::SetBinary ( uint8 const *  bytes,
int32  len,
bool  littleEndian = true 
)
58{
59 if (littleEndian)
60 {
61#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
62 uint8* array = new uint8[len];
63
64 for (int i = 0; i < len; i++)
65 array[i] = bytes[len - 1 - i];
66
67 BN_bin2bn(array, len, _bn);
68
69 delete[] array;
70#else
71 BN_lebin2bn(bytes, len, _bn);
72#endif
73 }
74 else
75 BN_bin2bn(bytes, len, _bn);
76}
std::uint8_t uint8
Definition: Define.h:110

References _bn.

Referenced by AuthSession::HandleReconnectProof().

◆ SetDword() [1/2]

void BigNumber::SetDword ( int32  val)
39{
40 SetDword(uint32(std::abs(val)));
41 if (val < 0)
42 BN_set_negative(_bn, 1);
43}

References _bn, and SetDword().

Referenced by SetDword().

◆ SetDword() [2/2]

void BigNumber::SetDword ( uint32  val)
46{
47 BN_set_word(_bn, val);
48}

References _bn.

◆ SetHexStr() [1/2]

bool BigNumber::SetHexStr ( char const *  str)
79{
80 int n = BN_hex2bn(&_bn, str);
81 return (n > 0);
82}

References _bn.

Referenced by GetHexFromConfig(), and WardenCheckMgr::LoadWardenChecks().

◆ SetHexStr() [2/2]

bool BigNumber::SetHexStr ( std::string const &  str)
inline
51{ return SetHexStr(str.c_str()); }

References SetHexStr().

Referenced by SetHexStr().

◆ SetQword()

void BigNumber::SetQword ( uint64  val)
51{
52 BN_set_word(_bn, (uint32)(val >> 32));
53 BN_lshift(_bn, _bn, 32);
54 BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
55}

References _bn.

◆ SetRand()

void BigNumber::SetRand ( int32  numbits)
85{
86 BN_rand(_bn, numbits, 0, 1);
87}

References _bn.

Referenced by SecretMgr::AttemptTransition(), and main().

◆ ToByteArray()

template<std::size_t Size>
std::array< uint8, Size > BigNumber::ToByteArray ( bool  littleEndian = true) const
inline
124 {
125 std::array<uint8, Size> buf;
126 GetBytes(buf.data(), Size, littleEndian);
127 return buf;
128 }
void GetBytes(uint8 *buf, size_t bufsize, bool littleEndian=true) const
Definition: BigNumber.cpp:198

Referenced by Acore::Crypto::AEDecrypt(), Acore::Crypto::AEEncryptWithRandomIV(), Acore::Crypto::SRP6::CalculateVerifier(), WardenWin::HandleData(), and AuthSession::HandleReconnectProof().

◆ ToByteVector()

std::vector< uint8 > BigNumber::ToByteVector ( int32  minSize = 0,
bool  littleEndian = true 
) const
224{
225 std::size_t length = std::max(GetNumBytes(), minSize);
226 std::vector<uint8> v;
227 v.resize(length);
228 GetBytes(v.data(), length, littleEndian);
229 return v;
230}

References GetBytes(), and GetNumBytes().

Referenced by WardenWin::HandleData(), Acore::Crypto::Argon2::Hash(), and WardenWin::RequestChecks().

Member Data Documentation

◆ _bn