feat(Core/Crypto): add support OpenSSL 3.0 (#13354)

This commit is contained in:
Winfidonarleyan
2022-11-23 21:12:20 +03:00
committed by GitHub
parent 4a2964e10a
commit a1a1528cb4
37 changed files with 1099 additions and 507 deletions

View File

@@ -1,11 +1,25 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 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 Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Cryptography/BigNumber.h"
#include "BigNumber.h"
#include "Errors.h"
#include <algorithm>
#include <cstring>
#include <memory>
#include <openssl/bn.h>
BigNumber::BigNumber()
@@ -25,9 +39,7 @@ void BigNumber::SetDword(int32 val)
{
SetDword(uint32(std::abs(val)));
if (val < 0)
{
BN_set_negative(_bn, 1);
}
}
void BigNumber::SetDword(uint32 val)
@@ -50,9 +62,7 @@ void BigNumber::SetBinary(uint8 const* bytes, int32 len, bool littleEndian)
uint8* array = new uint8[len];
for (int i = 0; i < len; i++)
{
array[i] = bytes[len - 1 - i];
}
BN_bin2bn(array, len, _bn);
@@ -62,9 +72,7 @@ void BigNumber::SetBinary(uint8 const* bytes, int32 len, bool littleEndian)
#endif
}
else
{
BN_bin2bn(bytes, len, _bn);
}
}
bool BigNumber::SetHexStr(char const* str)
@@ -81,9 +89,7 @@ void BigNumber::SetRand(int32 numbits)
BigNumber& BigNumber::operator=(BigNumber const& bn)
{
if (this == &bn)
{
return *this;
}
BN_copy(_bn, bn._bn);
return *this;
@@ -103,7 +109,7 @@ BigNumber& BigNumber::operator-=(BigNumber const& bn)
BigNumber& BigNumber::operator*=(BigNumber const& bn)
{
BN_CTX* bnctx;
BN_CTX *bnctx;
bnctx = BN_CTX_new();
BN_mul(_bn, _bn, bn._bn, bnctx);
@@ -114,7 +120,7 @@ BigNumber& BigNumber::operator*=(BigNumber const& bn)
BigNumber& BigNumber::operator/=(BigNumber const& bn)
{
BN_CTX* bnctx;
BN_CTX *bnctx;
bnctx = BN_CTX_new();
BN_div(_bn, nullptr, _bn, bn._bn, bnctx);
@@ -125,7 +131,7 @@ BigNumber& BigNumber::operator/=(BigNumber const& bn)
BigNumber& BigNumber::operator%=(BigNumber const& bn)
{
BN_CTX* bnctx;
BN_CTX *bnctx;
bnctx = BN_CTX_new();
BN_mod(_bn, _bn, bn._bn, bnctx);
@@ -148,7 +154,7 @@ int BigNumber::CompareTo(BigNumber const& bn) const
BigNumber BigNumber::Exp(BigNumber const& bn) const
{
BigNumber ret;
BN_CTX* bnctx;
BN_CTX *bnctx;
bnctx = BN_CTX_new();
BN_exp(ret._bn, _bn, bn._bn, bnctx);
@@ -160,7 +166,7 @@ BigNumber BigNumber::Exp(BigNumber const& bn) const
BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2) const
{
BigNumber ret;
BN_CTX* bnctx;
BN_CTX *bnctx;
bnctx = BN_CTX_new();
BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
@@ -201,17 +207,13 @@ void BigNumber::GetBytes(uint8* buf, size_t bufsize, bool littleEndian) const
// If we need more bytes than length of BigNumber set the rest to 0
if (numBytes < bufsize)
{
memset((void*)buf, 0, bufsize);
}
BN_bn2bin(_bn, buf + (bufsize - numBytes));
// openssl's BN stores data internally in big endian format, reverse if little endian desired
if (littleEndian)
{
std::reverse(buf, buf + bufsize);
}
#else
int res = littleEndian ? BN_bn2lebinpad(_bn, buf, bufsize) : BN_bn2binpad(_bn, buf, bufsize);
ASSERT(res > 0, "Buffer of size {} is too small to hold bignum with {} bytes.\n", bufsize, BN_num_bytes(_bn));