AzeorthCore 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 ( )
12 : _bn(BN_new())
13{ }
struct bignum_st * _bn
Definition: BigNumber.h:123

◆ BigNumber() [2/6]

BigNumber::BigNumber ( BigNumber const &  bn)
16 : _bn(BN_dup(bn.BN()))
17{ }

◆ BigNumber() [3/6]

BigNumber::BigNumber ( uint32  v)
inline
22: BigNumber() { SetDword(v); }
BigNumber()
Definition: BigNumber.cpp:11
void SetDword(int32)
Definition: BigNumber.cpp:24

◆ BigNumber() [4/6]

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

◆ BigNumber() [5/6]

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

◆ BigNumber() [6/6]

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

◆ ~BigNumber()

BigNumber::~BigNumber ( )
20{
21 BN_free(_bn);
22}

References _bn.

Member Function Documentation

◆ AsDecStr()

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

References _bn.

◆ AsDword()

uint32 BigNumber::AsDword ( ) const
178{
179 return (uint32)BN_get_word(_bn);
180}
std::uint32_t uint32
Definition: Define.h:108

References _bn.

◆ AsHexStr()

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

References _bn.

◆ BN() [1/2]

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

◆ BN() [2/2]

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

◆ CompareTo()

int BigNumber::CompareTo ( BigNumber const &  bn) const
144{
145 return BN_cmp(_bn, bn._bn);
146}

References _bn.

◆ Exp()

BigNumber BigNumber::Exp ( BigNumber const &  bn) const
149{
150 BigNumber ret;
151 BN_CTX* bnctx;
152
153 bnctx = BN_CTX_new();
154 BN_exp(ret._bn, _bn, bn._bn, bnctx);
155 BN_CTX_free(bnctx);
156
157 return ret;
158}
Definition: BigNumber.h:18

References _bn.

◆ GetBytes()

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

References _bn, ASSERT, and GetNumBytes().

Referenced by ToByteVector().

◆ GetNumBytes()

int32 BigNumber::GetNumBytes ( ) const
173{
174 return BN_num_bytes(_bn);
175}

References _bn.

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

◆ IsNegative()

bool BigNumber::IsNegative ( ) const
188{
189 return BN_is_negative(_bn);
190}

References _bn.

◆ IsZero()

bool BigNumber::IsZero ( ) const
183{
184 return BN_is_zero(_bn);
185}

References _bn.

◆ ModExp()

BigNumber BigNumber::ModExp ( BigNumber const &  bn1,
BigNumber const &  bn2 
) const
161{
162 BigNumber ret;
163 BN_CTX* bnctx;
164
165 bnctx = BN_CTX_new();
166 BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
167 BN_CTX_free(bnctx);
168
169 return ret;
170}

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
76 {
77 BigNumber t(*this);
78 return t %= bn;
79 }

◆ operator%=()

BigNumber & BigNumber::operator%= ( BigNumber const &  bn)
127{
128 BN_CTX* bnctx;
129
130 bnctx = BN_CTX_new();
131 BN_mod(_bn, _bn, bn._bn, bnctx);
132 BN_CTX_free(bnctx);
133
134 return *this;
135}

References _bn.

◆ operator*()

BigNumber BigNumber::operator* ( BigNumber const &  bn) const
inline
62 {
63 BigNumber t(*this);
64 return t *= bn;
65 }

◆ operator*=()

BigNumber & BigNumber::operator*= ( BigNumber const &  bn)
105{
106 BN_CTX* bnctx;
107
108 bnctx = BN_CTX_new();
109 BN_mul(_bn, _bn, bn._bn, bnctx);
110 BN_CTX_free(bnctx);
111
112 return *this;
113}

References _bn.

◆ operator+()

BigNumber BigNumber::operator+ ( BigNumber const &  bn) const
inline
48 {
49 BigNumber t(*this);
50 return t += bn;
51 }

◆ operator+=()

BigNumber & BigNumber::operator+= ( BigNumber const &  bn)
93{
94 BN_add(_bn, _bn, bn._bn);
95 return *this;
96}

References _bn.

◆ operator-()

BigNumber BigNumber::operator- ( BigNumber const &  bn) const
inline
55 {
56 BigNumber t(*this);
57 return t -= bn;
58 }

◆ operator-=()

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

References _bn.

◆ operator/()

BigNumber BigNumber::operator/ ( BigNumber const &  bn) const
inline
69 {
70 BigNumber t(*this);
71 return t /= bn;
72 }

◆ operator/=()

BigNumber & BigNumber::operator/= ( BigNumber const &  bn)
116{
117 BN_CTX* bnctx;
118
119 bnctx = BN_CTX_new();
120 BN_div(_bn, nullptr, _bn, bn._bn, bnctx);
121 BN_CTX_free(bnctx);
122
123 return *this;
124}

References _bn.

◆ operator<()

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

◆ operator<<()

BigNumber BigNumber::operator<< ( int  n) const
inline
83 {
84 BigNumber t(*this);
85 return t <<= n;
86 }

◆ operator<<=()

BigNumber & BigNumber::operator<<= ( int  n)
138{
139 BN_lshift(_bn, _bn, n);
140 return *this;
141}

References _bn.

◆ operator<=()

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

◆ operator=()

BigNumber & BigNumber::operator= ( BigNumber const &  bn)
82{
83 if (this == &bn)
84 {
85 return *this;
86 }
87
88 BN_copy(_bn, bn._bn);
89 return *this;
90}

References _bn.

◆ operator==()

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

◆ operator>()

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

◆ operator>=()

bool BigNumber::operator>= ( BigNumber const &  bn) const
inline
91{ 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
37{ 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 
)
46{
47 if (littleEndian)
48 {
49#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
50 uint8* array = new uint8[len];
51
52 for (int i = 0; i < len; i++)
53 {
54 array[i] = bytes[len - 1 - i];
55 }
56
57 BN_bin2bn(array, len, _bn);
58
59 delete[] array;
60#else
61 BN_lebin2bn(bytes, len, _bn);
62#endif
63 }
64 else
65 {
66 BN_bin2bn(bytes, len, _bn);
67 }
68}
std::uint8_t uint8
Definition: Define.h:110

References _bn.

Referenced by AuthSession::HandleReconnectProof().

◆ SetDword() [1/2]

void BigNumber::SetDword ( int32  val)
25{
26 SetDword(uint32(std::abs(val)));
27 if (val < 0)
28 {
29 BN_set_negative(_bn, 1);
30 }
31}

References _bn, and SetDword().

Referenced by SetDword().

◆ SetDword() [2/2]

void BigNumber::SetDword ( uint32  val)
34{
35 BN_set_word(_bn, val);
36}

References _bn.

◆ SetHexStr() [1/2]

bool BigNumber::SetHexStr ( char const *  str)
71{
72 int n = BN_hex2bn(&_bn, str);
73 return (n > 0);
74}

References _bn.

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

◆ SetHexStr() [2/2]

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

References SetHexStr().

Referenced by SetHexStr().

◆ SetQword()

void BigNumber::SetQword ( uint64  val)
39{
40 BN_set_word(_bn, (uint32)(val >> 32));
41 BN_lshift(_bn, _bn, 32);
42 BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
43}

References _bn.

◆ SetRand()

void BigNumber::SetRand ( int32  numbits)
77{
78 BN_rand(_bn, numbits, 0, 1);
79}

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
113 {
114 std::array<uint8, Size> buf;
115 GetBytes(buf.data(), Size, littleEndian);
116 return buf;
117 }
void GetBytes(uint8 *buf, size_t bufsize, bool littleEndian=true) const
Definition: BigNumber.cpp:192

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
222{
223 std::size_t length = std::max(GetNumBytes(), minSize);
224 std::vector<uint8> v;
225 v.resize(length);
226 GetBytes(v.data(), length, littleEndian);
227 return v;
228}

References GetBytes(), and GetNumBytes().

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

Member Data Documentation

◆ _bn