aboutsummaryrefslogtreecommitdiff
path: root/src/common/Cryptography/BigNumber.cpp
diff options
context:
space:
mode:
authorTreeston <treeston.mmoc@gmail.com>2019-08-10 21:34:51 +0200
committerGitHub <noreply@github.com>2019-08-10 21:34:51 +0200
commit4211645834c467a03c60248e80818d3607be9ea7 (patch)
tree673a1695581503b6ea3e49da5c3e0d06bf5d892e /src/common/Cryptography/BigNumber.cpp
parent3d356b97d4cc4c7ec4c641487241eae6dcc0558e (diff)
[3.3.5] Core/Authserver: TOTP rewrite: (PR #23633)
- Proper management commands (.account 2fa) - Secrets can now be encrypted (set TOTPTokenSecret in .conf) - Secret now stored in binary - Argon2 and AES primitives - Base32/64 support
Diffstat (limited to 'src/common/Cryptography/BigNumber.cpp')
-rw-r--r--src/common/Cryptography/BigNumber.cpp67
1 files changed, 46 insertions, 21 deletions
diff --git a/src/common/Cryptography/BigNumber.cpp b/src/common/Cryptography/BigNumber.cpp
index 96f9144181e..19de13572bf 100644
--- a/src/common/Cryptography/BigNumber.cpp
+++ b/src/common/Cryptography/BigNumber.cpp
@@ -17,6 +17,7 @@
*/
#include "Cryptography/BigNumber.h"
+#include "Errors.h"
#include <openssl/bn.h>
#include <cstring>
#include <algorithm>
@@ -65,9 +66,10 @@ void BigNumber::SetBinary(uint8 const* bytes, int32 len)
delete[] array;
}
-void BigNumber::SetHexStr(char const* str)
+bool BigNumber::SetHexStr(char const* str)
{
- BN_hex2bn(&_bn, str);
+ int n = BN_hex2bn(&_bn, str);
+ return (n > 0);
}
void BigNumber::SetRand(int32 numbits)
@@ -84,19 +86,19 @@ BigNumber& BigNumber::operator=(BigNumber const& bn)
return *this;
}
-BigNumber BigNumber::operator+=(BigNumber const& bn)
+BigNumber& BigNumber::operator+=(BigNumber const& bn)
{
BN_add(_bn, _bn, bn._bn);
return *this;
}
-BigNumber BigNumber::operator-=(BigNumber const& bn)
+BigNumber& BigNumber::operator-=(BigNumber const& bn)
{
BN_sub(_bn, _bn, bn._bn);
return *this;
}
-BigNumber BigNumber::operator*=(BigNumber const& bn)
+BigNumber& BigNumber::operator*=(BigNumber const& bn)
{
BN_CTX *bnctx;
@@ -107,7 +109,7 @@ BigNumber BigNumber::operator*=(BigNumber const& bn)
return *this;
}
-BigNumber BigNumber::operator/=(BigNumber const& bn)
+BigNumber& BigNumber::operator/=(BigNumber const& bn)
{
BN_CTX *bnctx;
@@ -118,7 +120,7 @@ BigNumber BigNumber::operator/=(BigNumber const& bn)
return *this;
}
-BigNumber BigNumber::operator%=(BigNumber const& bn)
+BigNumber& BigNumber::operator%=(BigNumber const& bn)
{
BN_CTX *bnctx;
@@ -129,7 +131,18 @@ BigNumber BigNumber::operator%=(BigNumber const& bn)
return *this;
}
-BigNumber BigNumber::Exp(BigNumber const& bn)
+BigNumber& BigNumber::operator<<=(int n)
+{
+ BN_lshift(_bn, _bn, n);
+ return *this;
+}
+
+int BigNumber::CompareTo(BigNumber const& bn) const
+{
+ return BN_cmp(_bn, bn._bn);
+}
+
+BigNumber BigNumber::Exp(BigNumber const& bn) const
{
BigNumber ret;
BN_CTX *bnctx;
@@ -141,7 +154,7 @@ BigNumber BigNumber::Exp(BigNumber const& bn)
return ret;
}
-BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2)
+BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2) const
{
BigNumber ret;
BN_CTX *bnctx;
@@ -153,12 +166,12 @@ BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2)
return ret;
}
-int32 BigNumber::GetNumBytes(void)
+int32 BigNumber::GetNumBytes() const
{
return BN_num_bytes(_bn);
}
-uint32 BigNumber::AsDword()
+uint32 BigNumber::AsDword() const
{
return (uint32)BN_get_word(_bn);
}
@@ -173,25 +186,37 @@ bool BigNumber::IsNegative() const
return BN_is_negative(_bn);
}
-std::unique_ptr<uint8[]> BigNumber::AsByteArray(int32 minSize, bool littleEndian)
+bool BigNumber::AsByteArray(uint8* buf, std::size_t bufsize, bool littleEndian) const
{
- int numBytes = GetNumBytes();
- int length = (minSize >= numBytes) ? minSize : numBytes;
+ int nBytes = GetNumBytes();
+ ASSERT(!(nBytes < 0));
+ std::size_t numBytes = static_cast<std::size_t>(nBytes);
- uint8* array = new uint8[length];
+ // too large to store
+ if (bufsize < numBytes)
+ return false;
// If we need more bytes than length of BigNumber set the rest to 0
- if (length > numBytes)
- memset((void*)array, 0, length);
+ if (numBytes < bufsize)
+ memset((void*)buf, 0, bufsize);
- BN_bn2bin(_bn, array + (length-numBytes));
+ 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(array, array + length);
+ std::reverse(buf, buf + bufsize);
- std::unique_ptr<uint8[]> ret(array);
- return ret;
+ return true;
+}
+
+std::unique_ptr<uint8[]> BigNumber::AsByteArray(int32 minSize, bool littleEndian) const
+{
+ std::size_t length = std::max(GetNumBytes(), minSize);
+ uint8* array = new uint8[length];
+ bool success = AsByteArray(array, length, littleEndian);
+ ASSERT(success);
+
+ return std::unique_ptr<uint8[]>(array);
}
std::string BigNumber::AsHexStr() const