summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-04-10 01:00:37 +0430
committerAndreas Kling <kling@serenityos.org>2020-05-02 12:24:10 +0200
commit6b742c69bd407cb2178b5915c2269f7ecc64b503 (patch)
tree09dbb0f329e4ccf72a38ae542c03baa336491cc5 /Libraries
parentc52d3e65b90a166741ca5e1fb6c9cb36b5db305a (diff)
downloadserenity-6b742c69bd407cb2178b5915c2269f7ecc64b503.zip
LibCrypto: Add ::import_data() and ::export_data() to UnsignedBigInteger
These functions allow conversion to-and-from big-endian buffers This commit also adds a ""_bigint operator for easy bigint use
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp28
-rw-r--r--Libraries/LibCrypto/BigInt/UnsignedBigInteger.h17
2 files changed, 45 insertions, 0 deletions
diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
index b682704a2e..5cdff360a5 100644
--- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
+++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.cpp
@@ -312,4 +312,32 @@ UnsignedBigInteger UnsignedBigInteger::create_invalid()
invalid.invalidate();
return invalid;
}
+
+// FIXME: in great need of optimisation
+UnsignedBigInteger UnsignedBigInteger::import_data(const u8* ptr, size_t length)
+{
+ UnsignedBigInteger integer { 0 };
+
+ for (size_t i = 0; i < length; ++i) {
+ auto part = UnsignedBigInteger { ptr[length - i - 1] }.shift_left(8 * i);
+ integer = integer.add(part);
+ }
+
+ return integer;
+}
+
+size_t UnsignedBigInteger::export_data(AK::ByteBuffer& data)
+{
+ UnsignedBigInteger copy { *this };
+
+ size_t size = trimmed_length() * sizeof(u32);
+ size_t i = 0;
+ for (; i < size; ++i) {
+ if (copy.length() == 0)
+ break;
+ data[size - i - 1] = copy.m_words[0] & 0xff;
+ copy = copy.divide(256).quotient;
+ }
+ return i;
+}
}
diff --git a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
index 660b234583..dbecbf0735 100644
--- a/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
+++ b/Libraries/LibCrypto/BigInt/UnsignedBigInteger.h
@@ -25,6 +25,7 @@
*/
#pragma once
+#include <AK/ByteBuffer.h>
#include <AK/LogStream.h>
#include <AK/String.h>
#include <AK/Types.h>
@@ -48,6 +49,16 @@ public:
static UnsignedBigInteger from_base10(const String& str);
static UnsignedBigInteger create_invalid();
+ static UnsignedBigInteger import_data(const AK::StringView& data) { return import_data((const u8*)data.characters_without_null_termination(), data.length()); }
+ static UnsignedBigInteger import_data(const u8* ptr, size_t length);
+
+ size_t export_data(AK::ByteBuffer& data);
+ size_t export_data(const u8* ptr, size_t length)
+ {
+ auto buffer = ByteBuffer::wrap(ptr, length);
+ return export_data(buffer);
+ }
+
const AK::Vector<u32>& words() const { return m_words; }
UnsignedBigInteger add(const UnsignedBigInteger& other) const;
@@ -103,3 +114,9 @@ operator<<(const LogStream& stream, const Crypto::UnsignedBigInteger value)
}
return stream;
}
+
+inline Crypto::UnsignedBigInteger
+operator""_bigint(const char* string, size_t length)
+{
+ return Crypto::UnsignedBigInteger::from_base10({ string, length });
+}