1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #include "Auth/BigNumber.h" |
---|
20 | #include <openssl/bn.h> |
---|
21 | #include <algorithm> |
---|
22 | |
---|
23 | BigNumber::BigNumber() |
---|
24 | { |
---|
25 | _bn = BN_new(); |
---|
26 | _array = NULL; |
---|
27 | } |
---|
28 | |
---|
29 | BigNumber::BigNumber(const BigNumber &bn) |
---|
30 | { |
---|
31 | _bn = BN_dup(bn._bn); |
---|
32 | _array = NULL; |
---|
33 | } |
---|
34 | |
---|
35 | BigNumber::BigNumber(uint32 val) |
---|
36 | { |
---|
37 | _bn = BN_new(); |
---|
38 | BN_set_word(_bn, val); |
---|
39 | _array = NULL; |
---|
40 | } |
---|
41 | |
---|
42 | BigNumber::~BigNumber() |
---|
43 | { |
---|
44 | BN_free(_bn); |
---|
45 | if(_array) delete[] _array; |
---|
46 | } |
---|
47 | |
---|
48 | void BigNumber::SetDword(uint32 val) |
---|
49 | { |
---|
50 | BN_set_word(_bn, val); |
---|
51 | } |
---|
52 | |
---|
53 | void BigNumber::SetQword(uint64 val) |
---|
54 | { |
---|
55 | BN_add_word(_bn, (uint32)(val >> 32)); |
---|
56 | BN_lshift(_bn, _bn, 32); |
---|
57 | BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF)); |
---|
58 | } |
---|
59 | |
---|
60 | void BigNumber::SetBinary(const uint8 *bytes, int len) |
---|
61 | { |
---|
62 | uint8 t[1000]; |
---|
63 | for (int i = 0; i < len; i++) t[i] = bytes[len - 1 - i]; |
---|
64 | BN_bin2bn(t, len, _bn); |
---|
65 | } |
---|
66 | |
---|
67 | void BigNumber::SetHexStr(const char *str) |
---|
68 | { |
---|
69 | BN_hex2bn(&_bn, str); |
---|
70 | } |
---|
71 | |
---|
72 | void BigNumber::SetRand(int numbits) |
---|
73 | { |
---|
74 | BN_rand(_bn, numbits, 0, 1); |
---|
75 | } |
---|
76 | |
---|
77 | BigNumber BigNumber::operator=(const BigNumber &bn) |
---|
78 | { |
---|
79 | BN_copy(_bn, bn._bn); |
---|
80 | return *this; |
---|
81 | } |
---|
82 | |
---|
83 | BigNumber BigNumber::operator+=(const BigNumber &bn) |
---|
84 | { |
---|
85 | BN_add(_bn, _bn, bn._bn); |
---|
86 | return *this; |
---|
87 | } |
---|
88 | |
---|
89 | BigNumber BigNumber::operator-=(const BigNumber &bn) |
---|
90 | { |
---|
91 | BN_sub(_bn, _bn, bn._bn); |
---|
92 | return *this; |
---|
93 | } |
---|
94 | |
---|
95 | BigNumber BigNumber::operator*=(const BigNumber &bn) |
---|
96 | { |
---|
97 | BN_CTX *bnctx; |
---|
98 | |
---|
99 | bnctx = BN_CTX_new(); |
---|
100 | BN_mul(_bn, _bn, bn._bn, bnctx); |
---|
101 | BN_CTX_free(bnctx); |
---|
102 | |
---|
103 | return *this; |
---|
104 | } |
---|
105 | |
---|
106 | BigNumber BigNumber::operator/=(const BigNumber &bn) |
---|
107 | { |
---|
108 | BN_CTX *bnctx; |
---|
109 | |
---|
110 | bnctx = BN_CTX_new(); |
---|
111 | BN_div(_bn, NULL, _bn, bn._bn, bnctx); |
---|
112 | BN_CTX_free(bnctx); |
---|
113 | |
---|
114 | return *this; |
---|
115 | } |
---|
116 | |
---|
117 | BigNumber BigNumber::operator%=(const BigNumber &bn) |
---|
118 | { |
---|
119 | BN_CTX *bnctx; |
---|
120 | |
---|
121 | bnctx = BN_CTX_new(); |
---|
122 | BN_mod(_bn, _bn, bn._bn, bnctx); |
---|
123 | BN_CTX_free(bnctx); |
---|
124 | |
---|
125 | return *this; |
---|
126 | } |
---|
127 | |
---|
128 | BigNumber BigNumber::Exp(const BigNumber &bn) |
---|
129 | { |
---|
130 | BigNumber ret; |
---|
131 | BN_CTX *bnctx; |
---|
132 | |
---|
133 | bnctx = BN_CTX_new(); |
---|
134 | BN_exp(ret._bn, _bn, bn._bn, bnctx); |
---|
135 | BN_CTX_free(bnctx); |
---|
136 | |
---|
137 | return ret; |
---|
138 | } |
---|
139 | |
---|
140 | BigNumber BigNumber::ModExp(const BigNumber &bn1, const BigNumber &bn2) |
---|
141 | { |
---|
142 | BigNumber ret; |
---|
143 | BN_CTX *bnctx; |
---|
144 | |
---|
145 | bnctx = BN_CTX_new(); |
---|
146 | BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx); |
---|
147 | BN_CTX_free(bnctx); |
---|
148 | |
---|
149 | return ret; |
---|
150 | } |
---|
151 | |
---|
152 | int BigNumber::GetNumBytes(void) |
---|
153 | { |
---|
154 | return BN_num_bytes(_bn); |
---|
155 | } |
---|
156 | |
---|
157 | uint32 BigNumber::AsDword() |
---|
158 | { |
---|
159 | return (uint32)BN_get_word(_bn); |
---|
160 | } |
---|
161 | |
---|
162 | uint8 *BigNumber::AsByteArray(int minSize) |
---|
163 | { |
---|
164 | int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes(); |
---|
165 | |
---|
166 | if (_array) |
---|
167 | { |
---|
168 | delete[] _array; |
---|
169 | _array = NULL; |
---|
170 | } |
---|
171 | _array = new uint8[length]; |
---|
172 | |
---|
173 | // If we need more bytes than length of BigNumber set the rest to 0 |
---|
174 | if (length > GetNumBytes()) |
---|
175 | memset((void*)_array, 0, length); |
---|
176 | |
---|
177 | BN_bn2bin(_bn, (unsigned char *)_array); |
---|
178 | |
---|
179 | std::reverse(_array, _array + length); |
---|
180 | |
---|
181 | return _array; |
---|
182 | } |
---|
183 | |
---|
184 | ByteBuffer BigNumber::AsByteBuffer() |
---|
185 | { |
---|
186 | ByteBuffer ret(GetNumBytes()); |
---|
187 | ret.append(AsByteArray(), GetNumBytes()); |
---|
188 | return ret; |
---|
189 | } |
---|
190 | |
---|
191 | std::vector<uint8> BigNumber::AsByteVector() |
---|
192 | { |
---|
193 | std::vector<uint8> ret; |
---|
194 | ret.resize(GetNumBytes()); |
---|
195 | memcpy(&ret[0], AsByteArray(), GetNumBytes()); |
---|
196 | return ret; |
---|
197 | } |
---|
198 | |
---|
199 | const char *BigNumber::AsHexStr() |
---|
200 | { |
---|
201 | return BN_bn2hex(_bn); |
---|
202 | } |
---|
203 | |
---|
204 | const char *BigNumber::AsDecStr() |
---|
205 | { |
---|
206 | return BN_bn2dec(_bn); |
---|
207 | } |
---|