aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/Cryptography/BigNumber.cpp
diff options
context:
space:
mode:
authorXTZGZoReX <none@none>2010-06-06 23:18:57 +0200
committerXTZGZoReX <none@none>2010-06-06 23:18:57 +0200
commit57f64f1f5a8eccff388211e0d08bf9d630d48ada (patch)
treed2136a7d198b667d9cbda7c845da112c4aa577db /src/server/shared/Cryptography/BigNumber.cpp
parent51121d0769619b1690fb140079244c121a39afc8 (diff)
* Restructuring shared.
--HG-- branch : trunk
Diffstat (limited to 'src/server/shared/Cryptography/BigNumber.cpp')
-rw-r--r--src/server/shared/Cryptography/BigNumber.cpp201
1 files changed, 201 insertions, 0 deletions
diff --git a/src/server/shared/Cryptography/BigNumber.cpp b/src/server/shared/Cryptography/BigNumber.cpp
new file mode 100644
index 00000000000..64e9ef21ccc
--- /dev/null
+++ b/src/server/shared/Cryptography/BigNumber.cpp
@@ -0,0 +1,201 @@
+/*
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "Auth/BigNumber.h"
+#include <openssl/bn.h>
+#include <algorithm>
+
+BigNumber::BigNumber()
+{
+ _bn = BN_new();
+ _array = NULL;
+}
+
+BigNumber::BigNumber(const BigNumber &bn)
+{
+ _bn = BN_dup(bn._bn);
+ _array = NULL;
+}
+
+BigNumber::BigNumber(uint32 val)
+{
+ _bn = BN_new();
+ BN_set_word(_bn, val);
+ _array = NULL;
+}
+
+BigNumber::~BigNumber()
+{
+ BN_free(_bn);
+ if(_array) delete[] _array;
+}
+
+void BigNumber::SetDword(uint32 val)
+{
+ BN_set_word(_bn, val);
+}
+
+void BigNumber::SetQword(uint64 val)
+{
+ BN_add_word(_bn, (uint32)(val >> 32));
+ BN_lshift(_bn, _bn, 32);
+ BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
+}
+
+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);
+}
+
+void BigNumber::SetHexStr(const char *str)
+{
+ BN_hex2bn(&_bn, str);
+}
+
+void BigNumber::SetRand(int numbits)
+{
+ BN_rand(_bn, numbits, 0, 1);
+}
+
+BigNumber BigNumber::operator=(const BigNumber &bn)
+{
+ BN_copy(_bn, bn._bn);
+ return *this;
+}
+
+BigNumber BigNumber::operator+=(const BigNumber &bn)
+{
+ BN_add(_bn, _bn, bn._bn);
+ return *this;
+}
+
+BigNumber BigNumber::operator-=(const BigNumber &bn)
+{
+ BN_sub(_bn, _bn, bn._bn);
+ return *this;
+}
+
+BigNumber BigNumber::operator*=(const BigNumber &bn)
+{
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_mul(_bn, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return *this;
+}
+
+BigNumber BigNumber::operator/=(const BigNumber &bn)
+{
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_div(_bn, NULL, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return *this;
+}
+
+BigNumber BigNumber::operator%=(const BigNumber &bn)
+{
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_mod(_bn, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return *this;
+}
+
+BigNumber BigNumber::Exp(const BigNumber &bn)
+{
+ BigNumber ret;
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_exp(ret._bn, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return ret;
+}
+
+BigNumber BigNumber::ModExp(const BigNumber &bn1, const BigNumber &bn2)
+{
+ BigNumber ret;
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return ret;
+}
+
+int BigNumber::GetNumBytes(void)
+{
+ return BN_num_bytes(_bn);
+}
+
+uint32 BigNumber::AsDword()
+{
+ return (uint32)BN_get_word(_bn);
+}
+
+bool BigNumber::isZero() const
+{
+ return BN_is_zero(_bn)!=0;
+}
+
+uint8 *BigNumber::AsByteArray(int minSize, bool reverse)
+{
+ int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes();
+
+ if (_array)
+ {
+ delete[] _array;
+ _array = NULL;
+ }
+ _array = new uint8[length];
+
+ // If we need more bytes than length of BigNumber set the rest to 0
+ if (length > GetNumBytes())
+ memset((void*)_array, 0, length);
+
+ BN_bn2bin(_bn, (unsigned char *)_array);
+
+ if (reverse)
+ std::reverse(_array, _array + length);
+
+ return _array;
+}
+
+const char *BigNumber::AsHexStr()
+{
+ return BN_bn2hex(_bn);
+}
+
+const char *BigNumber::AsDecStr()
+{
+ return BN_bn2dec(_bn);
+}
+