mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 15:27: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:
parent
5c045b6934
commit
15532df83d
20 changed files with 37 additions and 39 deletions
|
@ -30,7 +30,7 @@ namespace Core {
|
|||
static DeprecatedString get_salt()
|
||||
{
|
||||
char random_data[12];
|
||||
fill_with_random(random_data, sizeof(random_data));
|
||||
fill_with_random({ random_data, sizeof(random_data) });
|
||||
|
||||
StringBuilder builder;
|
||||
builder.append("$5$"sv);
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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 };
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ namespace TLS {
|
|||
|
||||
ByteBuffer TLSv12::build_hello()
|
||||
{
|
||||
fill_with_random(&m_context.local_random, 32);
|
||||
fill_with_random(m_context.local_random);
|
||||
|
||||
auto packet_version = (u16)m_context.options.version;
|
||||
auto version = (u16)m_context.options.version;
|
||||
|
|
|
@ -157,7 +157,7 @@ void TLSv12::build_rsa_pre_master_secret(PacketBuilder& builder)
|
|||
u8 random_bytes[48];
|
||||
size_t bytes = 48;
|
||||
|
||||
fill_with_random(random_bytes, bytes);
|
||||
fill_with_random(random_bytes);
|
||||
|
||||
// remove zeros from the random bytes
|
||||
for (size_t i = 0; i < bytes; ++i) {
|
||||
|
|
|
@ -159,7 +159,7 @@ void TLSv12::update_packet(ByteBuffer& packet)
|
|||
u8 iv[16];
|
||||
Bytes iv_bytes { iv, 16 };
|
||||
Bytes { m_context.crypto.local_aead_iv, 4 }.copy_to(iv_bytes);
|
||||
fill_with_random(iv_bytes.offset(4), 8);
|
||||
fill_with_random(iv_bytes.slice(4, 8));
|
||||
memset(iv_bytes.offset(12), 0, 4);
|
||||
|
||||
// write the random part of the iv out
|
||||
|
@ -207,7 +207,7 @@ void TLSv12::update_packet(ByteBuffer& packet)
|
|||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
auto iv = iv_buffer_result.release_value();
|
||||
fill_with_random(iv.data(), iv.size());
|
||||
fill_with_random(iv);
|
||||
|
||||
// write it into the ciphertext portion of the message
|
||||
ct.overwrite(header_size, iv.data(), iv.size());
|
||||
|
|
|
@ -61,7 +61,7 @@ WebIDL::ExceptionOr<JS::Value> Crypto::get_random_values(JS::Value array) const
|
|||
// FIXME: Handle SharedArrayBuffers
|
||||
|
||||
// 3. Overwrite all elements of array with cryptographically strong random values of the appropriate type.
|
||||
fill_with_random(typed_array.viewed_array_buffer()->buffer().data(), typed_array.viewed_array_buffer()->buffer().size());
|
||||
fill_with_random(typed_array.viewed_array_buffer()->buffer());
|
||||
|
||||
// 4. Return array.
|
||||
return array;
|
||||
|
@ -88,7 +88,7 @@ ErrorOr<String> generate_random_uuid()
|
|||
u8 bytes[16];
|
||||
|
||||
// 2. Fill bytes with cryptographically secure random bytes.
|
||||
fill_with_random(bytes, 16);
|
||||
fill_with_random(bytes);
|
||||
|
||||
// 3. Set the 4 most significant bits of bytes[6], which represent the UUID version, to 0100.
|
||||
bytes[6] &= ~(1 << 7);
|
||||
|
|
|
@ -184,7 +184,7 @@ void WebSocket::send_client_handshake()
|
|||
|
||||
// 7. 16-byte nonce encoded as Base64
|
||||
u8 nonce_data[16];
|
||||
fill_with_random(nonce_data, 16);
|
||||
fill_with_random(nonce_data);
|
||||
// FIXME: change to TRY() and make method fallible
|
||||
m_websocket_key = MUST(encode_base64({ nonce_data, 16 })).to_deprecated_string();
|
||||
builder.appendff("Sec-WebSocket-Key: {}\r\n", m_websocket_key);
|
||||
|
@ -579,7 +579,7 @@ void WebSocket::send_frame(WebSocket::OpCode op_code, ReadonlyBytes payload, boo
|
|||
// > Clients MUST choose a new masking key for each frame, using an algorithm
|
||||
// > that cannot be predicted by end applications that provide data
|
||||
u8 masking_key[4];
|
||||
fill_with_random(masking_key, 4);
|
||||
fill_with_random(masking_key);
|
||||
m_impl->send(ReadonlyBytes(masking_key, 4));
|
||||
// don't try to send empty payload
|
||||
if (payload.size() == 0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue