1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 18:17:34 +00:00

AK+Everywhere: Change AK::fill_with_random to accept a Bytes object

Rather than the very C-like API we currently have, accepting a void* and
a length, let's take a Bytes object instead. In almost all existing
cases, the compiler figures out the length.
This commit is contained in:
Timothy Flynn 2023-04-02 13:08:43 -04:00 committed by Andreas Kling
parent 5c045b6934
commit 15532df83d
20 changed files with 37 additions and 39 deletions

View file

@ -19,7 +19,7 @@ ErrorOr<ByteBuffer> Ed25519::generate_private_key()
// about randomness.
auto buffer = TRY(ByteBuffer::create_uninitialized(key_size()));
fill_with_random(buffer.data(), buffer.size());
fill_with_random(buffer);
return buffer;
};

View file

@ -357,7 +357,7 @@ static bool is_point_on_curve(JacobianPoint const& point)
ErrorOr<ByteBuffer> SECP256r1::generate_private_key()
{
auto buffer = TRY(ByteBuffer::create_uninitialized(32));
fill_with_random(buffer.data(), buffer.size());
fill_with_random(buffer);
return buffer;
}

View file

@ -30,7 +30,7 @@ static void conditional_swap(u32* first, u32* second, u32 condition)
ErrorOr<ByteBuffer> X25519::generate_private_key()
{
auto buffer = TRY(ByteBuffer::create_uninitialized(BYTES));
fill_with_random(buffer.data(), buffer.size());
fill_with_random(buffer);
return buffer;
}

View file

@ -291,7 +291,7 @@ static void modular_multiply_inverse(u32* state, u32* value)
ErrorOr<ByteBuffer> X448::generate_private_key()
{
auto buffer = TRY(ByteBuffer::create_uninitialized(BYTES));
fill_with_random(buffer.data(), buffer.size());
fill_with_random(buffer);
return buffer;
}

View file

@ -168,7 +168,7 @@ UnsignedBigInteger random_number(UnsignedBigInteger const& min, UnsignedBigInteg
auto buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
auto* buf = buffer.data();
fill_with_random(buf, size);
fill_with_random(buffer);
UnsignedBigInteger random { buf, size };
// At this point, `random` is a large number, in the range [0, 256^size).
// To get down to the actual range, we could just compute random % range.

View file

@ -39,7 +39,7 @@ public:
auto em_length = (em_bits + 7) / 8;
u8 salt[SaltLength];
fill_with_random(salt, SaltLength);
fill_with_random(salt);
if (em_length < hash_length + SaltLength + 2) {
dbgln("Ooops...encoding error");

View file

@ -343,12 +343,13 @@ void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, Bytes& out)
Vector<u8, 8096> ps;
ps.resize(ps_length);
fill_with_random(ps.data(), ps_length);
fill_with_random(ps);
// since fill_with_random can create zeros (shocking!)
// we have to go through and un-zero the zeros
for (size_t i = 0; i < ps_length; ++i)
for (size_t i = 0; i < ps_length; ++i) {
while (!ps[i])
fill_with_random(ps.span().offset(i), 1);
ps[i] = get_random<u8>();
}
u8 paddings[] { 0x00, 0x02 };