Legends-of-Azeroth-Pandaria-5.4.8

Форк
0
261 строка · 5.8 Кб
1
/*
2
* This file is part of the Pandaria 5.4.8 Project. See THANKS file for Copyright information
3
*
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License as published by the
6
* Free Software Foundation; either version 2 of the License, or (at your
7
* option) any later version.
8
*
9
* This program is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License along
15
* with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17

18
#include <cstring>
19
#include "Cryptography/BigNumber.h"
20
#include <openssl/bn.h>
21
#include <openssl/crypto.h>
22
#include <algorithm>
23

24
BigNumber::BigNumber()
25
    : _bn(BN_new())
26
{ }
27

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

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

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

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

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

56
void BigNumber::SetBinary(uint8 const* bytes, int32 len, bool littleEndian)
57
{
58
    if (littleEndian)
59
    {
60
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
61
        uint8* array = new uint8[len];
62

63
        for (int i = 0; i < len; i++)
64
            array[i] = bytes[len - 1 - i];
65

66
        BN_bin2bn(array, len, _bn);
67

68
        delete[] array;
69
#else
70
        BN_lebin2bn(bytes, len, _bn);
71
#endif
72
    }
73
    else
74
        BN_bin2bn(bytes, len, _bn);
75
}
76

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

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

88
BigNumber& BigNumber::operator=(BigNumber const& bn)
89
{
90
    if (this == &bn)
91
        return *this;
92

93
    BN_copy(_bn, bn._bn);
94
    return *this;
95
}
96

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

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

109
BigNumber& BigNumber::operator*=(BigNumber const& bn)
110
{
111
    BN_CTX *bnctx;
112

113
    bnctx = BN_CTX_new();
114
    BN_mul(_bn, _bn, bn._bn, bnctx);
115
    BN_CTX_free(bnctx);
116

117
    return *this;
118
}
119

120
BigNumber& BigNumber::operator/=(BigNumber const& bn)
121
{
122
    BN_CTX *bnctx;
123

124
    bnctx = BN_CTX_new();
125
    BN_div(_bn, nullptr, _bn, bn._bn, bnctx);
126
    BN_CTX_free(bnctx);
127

128
    return *this;
129
}
130

131
BigNumber& BigNumber::operator%=(BigNumber const& bn)
132
{
133
    BN_CTX *bnctx;
134

135
    bnctx = BN_CTX_new();
136
    BN_mod(_bn, _bn, bn._bn, bnctx);
137
    BN_CTX_free(bnctx);
138

139
    return *this;
140
}
141

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

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

153
BigNumber BigNumber::Exp(BigNumber const& bn) const
154
{
155
    BigNumber ret;
156
    BN_CTX *bnctx;
157

158
    bnctx = BN_CTX_new();
159
    BN_exp(ret._bn, _bn, bn._bn, bnctx);
160
    BN_CTX_free(bnctx);
161

162
    return ret;
163
}
164

165
BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2) const
166
{
167
    BigNumber ret;
168
    BN_CTX *bnctx;
169

170
    bnctx = BN_CTX_new();
171
    BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
172
    BN_CTX_free(bnctx);
173

174
    return ret;
175
}
176

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

182
uint32 BigNumber::AsDword() const
183
{
184
    return (uint32)BN_get_word(_bn);
185
}
186

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

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

197
uint8* BigNumber::AsByteArray(int32 minSize, bool littleEndian)
198
{
199
    int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
200

201
    uint8* array = new uint8[length];
202

203
    // If we need more bytes than length of BigNumber set the rest to 0
204
    if (length > GetNumBytes())
205
        std::memset((void*)array, 0, length);
206

207
    int paddingOffset = length - GetNumBytes();
208

209
    BN_bn2bin(_bn, (unsigned char *)array + paddingOffset);
210

211
    // openssl's BN stores data internally in big endian format, reverse if little endian desired
212
    if (littleEndian)
213
        std::reverse(array, array + length);
214

215
    uint8* ret(array);
216
    return ret;
217
}
218

219
void BigNumber::GetBytes(uint8* buf, size_t bufsize, bool littleEndian) const
220
{
221
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
222
    int nBytes = GetNumBytes();
223
    ASSERT(nBytes >= 0, "Bignum has negative number of bytes (%d).", nBytes);
224
    std::size_t numBytes = static_cast<std::size_t>(nBytes);
225

226
    // too large to store
227
    ASSERT(numBytes <= bufsize, "Buffer of size %zu is too small to hold bignum with %zu bytes.\n", bufsize, numBytes);
228

229
    // If we need more bytes than length of BigNumber set the rest to 0
230
    if (numBytes < bufsize)
231
        memset((void*)buf, 0, bufsize);
232

233
    BN_bn2bin(_bn, buf + (bufsize - numBytes));
234

235
    // openssl's BN stores data internally in big endian format, reverse if little endian desired
236
    if (littleEndian)
237
        std::reverse(buf, buf + bufsize);
238
#else
239
    int res = littleEndian ? BN_bn2lebinpad(_bn, buf, bufsize) : BN_bn2binpad(_bn, buf, bufsize);
240
    ASSERT(res > 0, "Buffer of size %zu is too small to hold bignum with %d bytes.\n", bufsize, BN_num_bytes(_bn));
241
#endif
242
}
243

244
std::vector<uint8> BigNumber::ToByteVector(int32 minSize, bool littleEndian) const
245
{
246
    std::size_t length = std::max(GetNumBytes(), minSize);
247
    std::vector<uint8> v;
248
    v.resize(length);
249
    GetBytes(v.data(), length, littleEndian);
250
    return v;
251
}
252

253
char * BigNumber::AsHexStr() const
254
{
255
    return BN_bn2hex(_bn);
256
}
257

258
char * BigNumber::AsDecStr() const
259
{
260
    return BN_bn2dec(_bn);
261
}
262

263

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.