summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCrypto
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-02-06 13:17:34 +0000
committerLinus Groh <mail@linusgroh.de>2022-02-06 15:49:54 +0000
commitb0d6399f60760e25a55ec9e8e95a1ad322b74b22 (patch)
treee31fa3275b81bfa9c4fe6376cb533526596a8d68 /Userland/Libraries/LibCrypto
parent4d785b9aa0cbca1f6ce108a87d3232a58a2069bb (diff)
downloadserenity-b0d6399f60760e25a55ec9e8e95a1ad322b74b22.zip
LibCrypto: Do not allow signed big integers to be negative zero
If a big integer were to become negative zero, set the sign to instead be positive. This prevents odd scenarios where users of signed big ints would falsely think the result of some big int arithmetic is negative.
Diffstat (limited to 'Userland/Libraries/LibCrypto')
-rw-r--r--Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h20
-rw-r--r--Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp10
-rw-r--r--Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h1
3 files changed, 29 insertions, 2 deletions
diff --git a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h
index c8850fd5ab..5b5468a728 100644
--- a/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h
+++ b/Userland/Libraries/LibCrypto/BigInt/SignedBigInteger.h
@@ -25,6 +25,7 @@ public:
: m_sign(sign)
, m_unsigned_data(move(unsigned_data))
{
+ ensure_sign_is_valid();
}
explicit SignedBigInteger(UnsignedBigInteger unsigned_data)
@@ -72,9 +73,18 @@ public:
const Vector<u32, STARTING_WORD_SIZE> words() const { return m_unsigned_data.words(); }
bool is_negative() const { return m_sign; }
- void negate() { m_sign = !m_sign; }
+ void negate()
+ {
+ if (!m_unsigned_data.is_zero())
+ m_sign = !m_sign;
+ }
+
+ void set_to_0()
+ {
+ m_unsigned_data.set_to_0();
+ m_sign = false;
+ }
- void set_to_0() { m_unsigned_data.set_to_0(); }
void set_to(i32 other)
{
m_unsigned_data.set_to((u32)other);
@@ -129,6 +139,12 @@ public:
bool operator>(const UnsignedBigInteger& other) const;
private:
+ void ensure_sign_is_valid()
+ {
+ if (m_sign && m_unsigned_data.is_zero())
+ m_sign = false;
+ }
+
bool m_sign { false };
UnsignedBigInteger m_unsigned_data;
};
diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
index ef56015395..a8082721d1 100644
--- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
+++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
@@ -145,6 +145,16 @@ void UnsignedBigInteger::set_to(const UnsignedBigInteger& other)
m_cached_hash = 0;
}
+bool UnsignedBigInteger::is_zero() const
+{
+ for (size_t i = 0; i < length(); ++i) {
+ if (m_words[i] != 0)
+ return false;
+ }
+
+ return true;
+}
+
size_t UnsignedBigInteger::trimmed_length() const
{
if (!m_cached_trimmed_length.has_value()) {
diff --git a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
index 14c9a90b4c..3f91aa8d51 100644
--- a/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
+++ b/Userland/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
@@ -72,6 +72,7 @@ public:
m_cached_hash = 0;
}
+ bool is_zero() const;
bool is_odd() const { return m_words.size() && (m_words[0] & 1); }
bool is_invalid() const { return m_is_invalid; }