From d5600e966a4d09d4c12a91f76c3cd19493f34eef Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 19 Dec 2020 18:14:38 +0100 Subject: [PATCH] LibTLS+LibCrypto: Remove all remaining uses of ByteBuffer::wrap() --- Libraries/LibCrypto/PK/PK.h | 4 ++-- Libraries/LibCrypto/PK/RSA.cpp | 18 +++++++++--------- Libraries/LibCrypto/PK/RSA.h | 8 ++++---- Libraries/LibTLS/ClientHandshake.cpp | 4 ++-- Libraries/LibTLS/Handshake.cpp | 4 ++-- Userland/test-crypto.cpp | 15 ++++++++++----- 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Libraries/LibCrypto/PK/PK.h b/Libraries/LibCrypto/PK/PK.h index 580b0336ba..4186eb0c91 100644 --- a/Libraries/LibCrypto/PK/PK.h +++ b/Libraries/LibCrypto/PK/PK.h @@ -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; diff --git a/Libraries/LibCrypto/PK/RSA.cpp b/Libraries/LibCrypto/PK/RSA.cpp index f144662c7a..5f1de4c039 100644 --- a/Libraries/LibCrypto/PK/RSA.cpp +++ b/Libraries/LibCrypto/PK/RSA.cpp @@ -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::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; } diff --git a/Libraries/LibCrypto/PK/RSA.h b/Libraries/LibCrypto/PK/RSA.h index 318857d81c..c33a90ec89 100644 --- a/Libraries/LibCrypto/PK/RSA.h +++ b/Libraries/LibCrypto/PK/RSA.h @@ -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; diff --git a/Libraries/LibTLS/ClientHandshake.cpp b/Libraries/LibTLS/ClientHandshake.cpp index 633e01f918..fecda66d17 100644 --- a/Libraries/LibTLS/ClientHandshake.cpp +++ b/Libraries/LibTLS/ClientHandshake.cpp @@ -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) diff --git a/Libraries/LibTLS/Handshake.cpp b/Libraries/LibTLS/Handshake.cpp index 88cca0a8ad..1fa0f01fca 100644 --- a/Libraries/LibTLS/Handshake.cpp +++ b/Libraries/LibTLS/Handshake.cpp @@ -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); diff --git a/Userland/test-crypto.cpp b/Userland/test-crypto.cpp index 1163f0967c..324e50b152 100644 --- a/Userland/test-crypto.cpp +++ b/Userland/test-crypto.cpp @@ -781,7 +781,7 @@ static void aes_ctr_test_name() PASS; } -#define AS_BB(x) (ByteBuffer::wrap((x), sizeof((x)) / sizeof((x)[0]))) +#define AS_BB(x) (ReadonlyBytes { (x), sizeof((x)) / sizeof((x)[0]) }) static void aes_ctr_test_encrypt() { auto test_it = [](auto key, auto ivec, auto in, auto out_expected) { @@ -1808,7 +1808,7 @@ static void rsa_test_encrypt() "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint, "65537"_bigint); u8 buffer[rsa.output_size()]; - auto buf = ByteBuffer::wrap(buffer, sizeof(buffer)); + auto buf = Bytes { buffer, sizeof(buffer) }; rsa.encrypt(data, buf); if (memcmp(result, buf.data(), buf.size())) { FAIL(Invalid encryption result); @@ -1825,7 +1825,7 @@ static void rsa_test_encrypt() "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint, "65537"_bigint); u8 buffer[rsa.output_size()]; - auto buf = ByteBuffer::wrap(buffer, sizeof(buffer)); + auto buf = Bytes { buffer, sizeof(buffer) }; rsa.encrypt(data, buf); rsa.decrypt(buf, buf); @@ -2000,8 +2000,13 @@ static void rsa_test_encrypt_decrypt() "39542231845947188736992321577701849924317746648774438832456325878966594812143638244746284968851807975097653255909707366086606867657273809465195392910913"_bigint, "65537"_bigint); dbg() << "Output size: " << rsa.output_size(); - auto dec = ByteBuffer::create_zeroed(rsa.output_size()); - auto enc = ByteBuffer::create_zeroed(rsa.output_size()); + + u8 enc_buffer[rsa.output_size()]; + u8 dec_buffer[rsa.output_size()]; + + auto enc = Bytes { enc_buffer, rsa.output_size() }; + auto dec = Bytes { dec_buffer, rsa.output_size() }; + enc.overwrite(0, "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64); rsa.encrypt(enc, dec);