mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:27:35 +00:00
LibCrypto: Change [XXX]BigInteger::export_data() to use Span.
This commit is contained in:
parent
abe925e4b0
commit
4709b700bd
6 changed files with 16 additions and 26 deletions
|
@ -36,10 +36,10 @@ SignedBigInteger SignedBigInteger::import_data(const u8* ptr, size_t length)
|
||||||
return { move(unsigned_data), sign };
|
return { move(unsigned_data), sign };
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t SignedBigInteger::export_data(AK::ByteBuffer& data) const
|
size_t SignedBigInteger::export_data(Bytes data) const
|
||||||
{
|
{
|
||||||
data[0] = m_sign;
|
data[0] = m_sign;
|
||||||
auto bytes_view = data.slice_view(1, data.size() - 1);
|
auto bytes_view = data.slice(1, data.size() - 1);
|
||||||
return m_unsigned_data.export_data(bytes_view) + 1;
|
return m_unsigned_data.export_data(bytes_view) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Span.h>
|
||||||
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
|
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
|
||||||
|
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
|
@ -66,13 +67,7 @@ public:
|
||||||
static SignedBigInteger import_data(const AK::StringView& data) { return import_data((const u8*)data.characters_without_null_termination(), data.length()); }
|
static SignedBigInteger import_data(const AK::StringView& data) { return import_data((const u8*)data.characters_without_null_termination(), data.length()); }
|
||||||
static SignedBigInteger import_data(const u8* ptr, size_t length);
|
static SignedBigInteger import_data(const u8* ptr, size_t length);
|
||||||
|
|
||||||
size_t export_data(AK::ByteBuffer& data) const;
|
size_t export_data(Bytes) const;
|
||||||
size_t export_data(u8* ptr, size_t length) const
|
|
||||||
{
|
|
||||||
// Note: ByteBuffer::wrap() does a const_cast!
|
|
||||||
auto buffer = ByteBuffer::wrap(ptr, length);
|
|
||||||
return export_data(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
static SignedBigInteger from_base10(StringView str);
|
static SignedBigInteger from_base10(StringView str);
|
||||||
String to_base10() const;
|
String to_base10() const;
|
||||||
|
|
|
@ -55,7 +55,7 @@ UnsignedBigInteger UnsignedBigInteger::create_invalid()
|
||||||
return invalid;
|
return invalid;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t UnsignedBigInteger::export_data(AK::ByteBuffer& data) const
|
size_t UnsignedBigInteger::export_data(Bytes data) const
|
||||||
{
|
{
|
||||||
size_t word_count = trimmed_length();
|
size_t word_count = trimmed_length();
|
||||||
size_t out = 0;
|
size_t out = 0;
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
#include <AK/ByteBuffer.h>
|
#include <AK/ByteBuffer.h>
|
||||||
#include <AK/LogStream.h>
|
#include <AK/LogStream.h>
|
||||||
|
#include <AK/Span.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
@ -58,13 +59,7 @@ public:
|
||||||
return UnsignedBigInteger(ptr, length);
|
return UnsignedBigInteger(ptr, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t export_data(AK::ByteBuffer& data) const;
|
size_t export_data(Bytes) const;
|
||||||
size_t export_data(u8* ptr, size_t length) const
|
|
||||||
{
|
|
||||||
// Note: ByteBuffer::wrap() does a const_cast!
|
|
||||||
auto buffer = ByteBuffer::wrap(ptr, length);
|
|
||||||
return export_data(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
static UnsignedBigInteger from_base10(const String& str);
|
static UnsignedBigInteger from_base10(const String& str);
|
||||||
String to_base10() const;
|
String to_base10() const;
|
||||||
|
|
|
@ -125,7 +125,7 @@ void RSA::encrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
|
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
|
||||||
auto size = exp.export_data(out);
|
auto size = exp.export_data(out.span());
|
||||||
// FIXME: We should probably not do this...
|
// FIXME: We should probably not do this...
|
||||||
if (size != out.size())
|
if (size != out.size())
|
||||||
out = out.slice(out.size() - size, size);
|
out = out.slice(out.size() - size, size);
|
||||||
|
@ -137,7 +137,7 @@ void RSA::decrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||||
|
|
||||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||||
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
|
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
|
||||||
auto size = exp.export_data(out);
|
auto size = exp.export_data(out.span());
|
||||||
|
|
||||||
auto align = m_private_key.length();
|
auto align = m_private_key.length();
|
||||||
auto aligned_size = (size + align - 1) / align * align;
|
auto aligned_size = (size + align - 1) / align * align;
|
||||||
|
@ -151,7 +151,7 @@ void RSA::sign(const ByteBuffer& in, ByteBuffer& out)
|
||||||
{
|
{
|
||||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||||
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
|
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
|
||||||
auto size = exp.export_data(out);
|
auto size = exp.export_data(out.span());
|
||||||
out = out.slice(out.size() - size, size);
|
out = out.slice(out.size() - size, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ void RSA::verify(const ByteBuffer& in, ByteBuffer& out)
|
||||||
{
|
{
|
||||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||||
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
|
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
|
||||||
auto size = exp.export_data(out);
|
auto size = exp.export_data(out.span());
|
||||||
out = out.slice(out.size() - size, size);
|
out = out.slice(out.size() - size, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1560,7 +1560,7 @@ void bigint_import_export()
|
||||||
u8 target_buffer[128];
|
u8 target_buffer[128];
|
||||||
AK::fill_with_random(random_bytes, 128);
|
AK::fill_with_random(random_bytes, 128);
|
||||||
auto encoded = Crypto::UnsignedBigInteger::import_data(random_bytes, 128);
|
auto encoded = Crypto::UnsignedBigInteger::import_data(random_bytes, 128);
|
||||||
encoded.export_data(target_buffer, 128);
|
encoded.export_data({ target_buffer, 128 });
|
||||||
if (memcmp(target_buffer, random_bytes, 128) != 0)
|
if (memcmp(target_buffer, random_bytes, 128) != 0)
|
||||||
FAIL(Could not roundtrip);
|
FAIL(Could not roundtrip);
|
||||||
else
|
else
|
||||||
|
@ -1570,7 +1570,7 @@ void bigint_import_export()
|
||||||
I_TEST((BigInteger | BigEndian Encode / Decode roundtrip));
|
I_TEST((BigInteger | BigEndian Encode / Decode roundtrip));
|
||||||
u8 target_buffer[128];
|
u8 target_buffer[128];
|
||||||
auto encoded = "12345678901234567890"_bigint;
|
auto encoded = "12345678901234567890"_bigint;
|
||||||
auto size = encoded.export_data(target_buffer, 128);
|
auto size = encoded.export_data({ target_buffer, 128 });
|
||||||
auto decoded = Crypto::UnsignedBigInteger::import_data(target_buffer, size);
|
auto decoded = Crypto::UnsignedBigInteger::import_data(target_buffer, size);
|
||||||
if (encoded != decoded)
|
if (encoded != decoded)
|
||||||
FAIL(Could not roundtrip);
|
FAIL(Could not roundtrip);
|
||||||
|
@ -1590,7 +1590,7 @@ void bigint_import_export()
|
||||||
I_TEST((BigInteger | BigEndian Export));
|
I_TEST((BigInteger | BigEndian Export));
|
||||||
auto number = "448378203247"_bigint;
|
auto number = "448378203247"_bigint;
|
||||||
char exported[8] { 0 };
|
char exported[8] { 0 };
|
||||||
auto exported_length = number.export_data((u8*)exported, 8);
|
auto exported_length = number.export_data({ exported, 8 });
|
||||||
if (exported_length == 5 && memcmp(exported + 3, "hello", 5) == 0) {
|
if (exported_length == 5 && memcmp(exported + 3, "hello", 5) == 0) {
|
||||||
PASS;
|
PASS;
|
||||||
} else {
|
} else {
|
||||||
|
@ -1877,7 +1877,7 @@ void bigint_signed_import_export()
|
||||||
random_bytes[0] = 1;
|
random_bytes[0] = 1;
|
||||||
AK::fill_with_random(random_bytes + 1, 128);
|
AK::fill_with_random(random_bytes + 1, 128);
|
||||||
auto encoded = Crypto::SignedBigInteger::import_data(random_bytes, 129);
|
auto encoded = Crypto::SignedBigInteger::import_data(random_bytes, 129);
|
||||||
encoded.export_data(target_buffer, 129);
|
encoded.export_data({ target_buffer, 129 });
|
||||||
if (memcmp(target_buffer, random_bytes, 129) != 0)
|
if (memcmp(target_buffer, random_bytes, 129) != 0)
|
||||||
FAIL(Could not roundtrip);
|
FAIL(Could not roundtrip);
|
||||||
else
|
else
|
||||||
|
@ -1887,7 +1887,7 @@ void bigint_signed_import_export()
|
||||||
I_TEST((Signed BigInteger | BigEndian Encode / Decode roundtrip));
|
I_TEST((Signed BigInteger | BigEndian Encode / Decode roundtrip));
|
||||||
u8 target_buffer[128];
|
u8 target_buffer[128];
|
||||||
auto encoded = "-12345678901234567890"_sbigint;
|
auto encoded = "-12345678901234567890"_sbigint;
|
||||||
auto size = encoded.export_data(target_buffer, 128);
|
auto size = encoded.export_data({ target_buffer, 128 });
|
||||||
auto decoded = Crypto::SignedBigInteger::import_data(target_buffer, size);
|
auto decoded = Crypto::SignedBigInteger::import_data(target_buffer, size);
|
||||||
if (encoded != decoded)
|
if (encoded != decoded)
|
||||||
FAIL(Could not roundtrip);
|
FAIL(Could not roundtrip);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue