1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:37:35 +00:00

LibTLS+LibCrypto: Remove all remaining uses of ByteBuffer::wrap()

This commit is contained in:
Andreas Kling 2020-12-19 18:14:38 +01:00
parent 050eb5afa8
commit d5600e966a
6 changed files with 29 additions and 24 deletions

View file

@ -49,8 +49,8 @@ public:
{
}
virtual void encrypt(ReadonlyBytes in, ByteBuffer& out) = 0;
virtual void decrypt(ReadonlyBytes in, ByteBuffer& out) = 0;
virtual void encrypt(ReadonlyBytes in, Bytes& out) = 0;
virtual void decrypt(ReadonlyBytes in, Bytes& out) = 0;
virtual void sign(ReadonlyBytes in, Bytes& out) = 0;
virtual void verify(ReadonlyBytes in, Bytes& out) = 0;

View file

@ -113,7 +113,7 @@ RSA::KeyPairType RSA::parse_rsa_key(ReadonlyBytes in)
return keypair;
}
void RSA::encrypt(ReadonlyBytes in, ByteBuffer& out)
void RSA::encrypt(ReadonlyBytes in, Bytes& out)
{
#ifdef CRYPTO_DEBUG
dbg() << "in size: " << in.size();
@ -121,7 +121,7 @@ void RSA::encrypt(ReadonlyBytes in, ByteBuffer& out)
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
if (!(in_integer < m_public_key.modulus())) {
dbg() << "value too large for key";
out.clear();
out = {};
return;
}
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
@ -133,7 +133,7 @@ void RSA::encrypt(ReadonlyBytes in, ByteBuffer& out)
}
}
void RSA::decrypt(ReadonlyBytes in, ByteBuffer& out)
void RSA::decrypt(ReadonlyBytes in, Bytes& out)
{
// FIXME: Actually use the private key properly
@ -228,7 +228,7 @@ VerificationConsistency RSA_EMSA_PSS<HashFunction>::verify(ReadonlyBytes in)
return m_emsa_pss.verify(in, EM, mod_bytes * 8 - 1);
}
void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, ByteBuffer& out)
void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, Bytes& out)
{
auto mod_len = (m_public_key.modulus().trimmed_length() * sizeof(u32) * 8 + 7) / 8;
#ifdef CRYPTO_DEBUG
@ -236,7 +236,7 @@ void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, ByteBuffer& out)
#endif
if (in.size() > mod_len - 11) {
dbg() << "message too long :(";
out.trim(0);
out = out.trim(0);
return;
}
if (out.size() < mod_len) {
@ -263,7 +263,7 @@ void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, ByteBuffer& out)
out.overwrite(2, ps, ps_length);
out.overwrite(2 + ps_length, paddings, 1);
out.overwrite(3 + ps_length, in.data(), in.size());
out.trim(3 + ps_length + in.size()); // should be a single block
out = out.trim(3 + ps_length + in.size()); // should be a single block
#ifdef CRYPTO_DEBUG
dbg() << "padded output size: " << 3 + ps_length + in.size() << " buffer size: " << out.size();
@ -271,12 +271,12 @@ void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, ByteBuffer& out)
RSA::encrypt(out, out);
}
void RSA_PKCS1_EME::decrypt(ReadonlyBytes in, ByteBuffer& out)
void RSA_PKCS1_EME::decrypt(ReadonlyBytes in, Bytes& out)
{
auto mod_len = (m_public_key.modulus().trimmed_length() * sizeof(u32) * 8 + 7) / 8;
if (in.size() != mod_len) {
dbg() << "decryption error: wrong amount of data: " << in.size();
out.trim(0);
out = out.trim(0);
return;
}
@ -284,7 +284,7 @@ void RSA_PKCS1_EME::decrypt(ReadonlyBytes in, ByteBuffer& out)
if (out.size() < RSA::output_size()) {
dbg() << "decryption error: not enough data after decryption: " << out.size();
out.trim(0);
out = out.trim(0);
return;
}

View file

@ -178,8 +178,8 @@ public:
m_private_key = pair.private_key;
}
virtual void encrypt(ReadonlyBytes in, ByteBuffer& out) override;
virtual void decrypt(ReadonlyBytes in, ByteBuffer& out) override;
virtual void encrypt(ReadonlyBytes in, Bytes& out) override;
virtual void decrypt(ReadonlyBytes in, Bytes& out) override;
virtual void sign(ReadonlyBytes in, Bytes& out) override;
virtual void verify(ReadonlyBytes in, Bytes& out) override;
@ -222,8 +222,8 @@ public:
~RSA_PKCS1_EME() { }
virtual void encrypt(ReadonlyBytes in, ByteBuffer& out) override;
virtual void decrypt(ReadonlyBytes in, ByteBuffer& out) override;
virtual void encrypt(ReadonlyBytes in, Bytes& out) override;
virtual void decrypt(ReadonlyBytes in, Bytes& out) override;
virtual void sign(ReadonlyBytes, Bytes&) override;
virtual void verify(ReadonlyBytes, Bytes&) override;

View file

@ -290,7 +290,7 @@ void TLSv12::build_random(PacketBuilder& builder)
Crypto::PK::RSA_PKCS1_EME rsa(certificate.public_key.modulus(), 0, certificate.public_key.public_exponent());
u8 out[rsa.output_size()];
auto outbuf = ByteBuffer::wrap(out, rsa.output_size());
auto outbuf = Bytes { out, rsa.output_size() };
rsa.encrypt(m_context.premaster_key, outbuf);
#ifdef TLS_DEBUG
@ -305,7 +305,7 @@ void TLSv12::build_random(PacketBuilder& builder)
builder.append_u24(outbuf.size() + 2);
builder.append((u16)outbuf.size());
builder.append(outbuf.bytes());
builder.append(outbuf);
}
ssize_t TLSv12::handle_payload(ReadonlyBytes vbuffer)

View file

@ -153,14 +153,14 @@ ByteBuffer TLSv12::build_finished()
builder.append_u24(out_size);
u8 out[out_size];
auto outbuffer = ByteBuffer::wrap(out, out_size);
auto outbuffer = Bytes { out, out_size };
auto dummy = ByteBuffer::create_zeroed(0);
auto digest = m_context.handshake_hash.digest();
auto hashbuf = ReadonlyBytes { digest.immutable_data(), m_context.handshake_hash.digest_size() };
pseudorandom_function(outbuffer, m_context.master_key, (const u8*)"client finished", 15, hashbuf, dummy);
builder.append(outbuffer.bytes());
builder.append(outbuffer);
auto packet = builder.build();
update_packet(packet);