Browse Source

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

Andreas Kling 4 years ago
parent
commit
d5600e966a

+ 2 - 2
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 sign(ReadonlyBytes in, Bytes& out) = 0;
     virtual void verify(ReadonlyBytes in, Bytes& out) = 0;
     virtual void verify(ReadonlyBytes in, Bytes& out) = 0;

+ 9 - 9
Libraries/LibCrypto/PK/RSA.cpp

@@ -113,7 +113,7 @@ RSA::KeyPairType RSA::parse_rsa_key(ReadonlyBytes in)
     return keypair;
     return keypair;
 }
 }
 
 
-void RSA::encrypt(ReadonlyBytes in, ByteBuffer& out)
+void RSA::encrypt(ReadonlyBytes in, Bytes& out)
 {
 {
 #ifdef CRYPTO_DEBUG
 #ifdef CRYPTO_DEBUG
     dbg() << "in size: " << in.size();
     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());
     auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
     if (!(in_integer < m_public_key.modulus())) {
     if (!(in_integer < m_public_key.modulus())) {
         dbg() << "value too large for key";
         dbg() << "value too large for key";
-        out.clear();
+        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());
@@ -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
     // 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);
     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;
     auto mod_len = (m_public_key.modulus().trimmed_length() * sizeof(u32) * 8 + 7) / 8;
 #ifdef CRYPTO_DEBUG
 #ifdef CRYPTO_DEBUG
@@ -236,7 +236,7 @@ void RSA_PKCS1_EME::encrypt(ReadonlyBytes in, ByteBuffer& out)
 #endif
 #endif
     if (in.size() > mod_len - 11) {
     if (in.size() > mod_len - 11) {
         dbg() << "message too long :(";
         dbg() << "message too long :(";
-        out.trim(0);
+        out = out.trim(0);
         return;
         return;
     }
     }
     if (out.size() < mod_len) {
     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, ps_length);
     out.overwrite(2 + ps_length, paddings, 1);
     out.overwrite(2 + ps_length, paddings, 1);
     out.overwrite(3 + ps_length, in.data(), in.size());
     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
 #ifdef CRYPTO_DEBUG
     dbg() << "padded output size: " << 3 + ps_length + in.size() << " buffer size: " << out.size();
     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);
     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;
     auto mod_len = (m_public_key.modulus().trimmed_length() * sizeof(u32) * 8 + 7) / 8;
     if (in.size() != mod_len) {
     if (in.size() != mod_len) {
         dbg() << "decryption error: wrong amount of data: " << in.size();
         dbg() << "decryption error: wrong amount of data: " << in.size();
-        out.trim(0);
+        out = out.trim(0);
         return;
         return;
     }
     }
 
 
@@ -284,7 +284,7 @@ void RSA_PKCS1_EME::decrypt(ReadonlyBytes in, ByteBuffer& out)
 
 
     if (out.size() < RSA::output_size()) {
     if (out.size() < RSA::output_size()) {
         dbg() << "decryption error: not enough data after decryption: " << out.size();
         dbg() << "decryption error: not enough data after decryption: " << out.size();
-        out.trim(0);
+        out = out.trim(0);
         return;
         return;
     }
     }
 
 

+ 4 - 4
Libraries/LibCrypto/PK/RSA.h

@@ -178,8 +178,8 @@ public:
         m_private_key = pair.private_key;
         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 sign(ReadonlyBytes in, Bytes& out) override;
     virtual void verify(ReadonlyBytes in, Bytes& out) override;
     virtual void verify(ReadonlyBytes in, Bytes& out) override;
@@ -222,8 +222,8 @@ public:
 
 
     ~RSA_PKCS1_EME() { }
     ~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 sign(ReadonlyBytes, Bytes&) override;
     virtual void verify(ReadonlyBytes, Bytes&) override;
     virtual void verify(ReadonlyBytes, Bytes&) override;

+ 2 - 2
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());
     Crypto::PK::RSA_PKCS1_EME rsa(certificate.public_key.modulus(), 0, certificate.public_key.public_exponent());
 
 
     u8 out[rsa.output_size()];
     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);
     rsa.encrypt(m_context.premaster_key, outbuf);
 
 
 #ifdef TLS_DEBUG
 #ifdef TLS_DEBUG
@@ -305,7 +305,7 @@ void TLSv12::build_random(PacketBuilder& builder)
 
 
     builder.append_u24(outbuf.size() + 2);
     builder.append_u24(outbuf.size() + 2);
     builder.append((u16)outbuf.size());
     builder.append((u16)outbuf.size());
-    builder.append(outbuf.bytes());
+    builder.append(outbuf);
 }
 }
 
 
 ssize_t TLSv12::handle_payload(ReadonlyBytes vbuffer)
 ssize_t TLSv12::handle_payload(ReadonlyBytes vbuffer)

+ 2 - 2
Libraries/LibTLS/Handshake.cpp

@@ -153,14 +153,14 @@ ByteBuffer TLSv12::build_finished()
     builder.append_u24(out_size);
     builder.append_u24(out_size);
 
 
     u8 out[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 dummy = ByteBuffer::create_zeroed(0);
 
 
     auto digest = m_context.handshake_hash.digest();
     auto digest = m_context.handshake_hash.digest();
     auto hashbuf = ReadonlyBytes { digest.immutable_data(), m_context.handshake_hash.digest_size() };
     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);
     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();
     auto packet = builder.build();
     update_packet(packet);
     update_packet(packet);
 
 

+ 10 - 5
Userland/test-crypto.cpp

@@ -781,7 +781,7 @@ static void aes_ctr_test_name()
         PASS;
         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()
 static void aes_ctr_test_encrypt()
 {
 {
     auto test_it = [](auto key, auto ivec, auto in, auto out_expected) {
     auto test_it = [](auto key, auto ivec, auto in, auto out_expected) {
@@ -1808,7 +1808,7 @@ static void rsa_test_encrypt()
             "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint,
             "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint,
             "65537"_bigint);
             "65537"_bigint);
         u8 buffer[rsa.output_size()];
         u8 buffer[rsa.output_size()];
-        auto buf = ByteBuffer::wrap(buffer, sizeof(buffer));
+        auto buf = Bytes { buffer, sizeof(buffer) };
         rsa.encrypt(data, buf);
         rsa.encrypt(data, buf);
         if (memcmp(result, buf.data(), buf.size())) {
         if (memcmp(result, buf.data(), buf.size())) {
             FAIL(Invalid encryption result);
             FAIL(Invalid encryption result);
@@ -1825,7 +1825,7 @@ static void rsa_test_encrypt()
             "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint,
             "4234603516465654167360850580101327813936403862038934287300450163438938741499875303761385527882335478349599685406941909381269804396099893549838642251053393"_bigint,
             "65537"_bigint);
             "65537"_bigint);
         u8 buffer[rsa.output_size()];
         u8 buffer[rsa.output_size()];
-        auto buf = ByteBuffer::wrap(buffer, sizeof(buffer));
+        auto buf = Bytes { buffer, sizeof(buffer) };
         rsa.encrypt(data, buf);
         rsa.encrypt(data, buf);
         rsa.decrypt(buf, buf);
         rsa.decrypt(buf, buf);
 
 
@@ -2000,8 +2000,13 @@ static void rsa_test_encrypt_decrypt()
         "39542231845947188736992321577701849924317746648774438832456325878966594812143638244746284968851807975097653255909707366086606867657273809465195392910913"_bigint,
         "39542231845947188736992321577701849924317746648774438832456325878966594812143638244746284968851807975097653255909707366086606867657273809465195392910913"_bigint,
         "65537"_bigint);
         "65537"_bigint);
     dbg() << "Output size: " << rsa.output_size();
     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);
     enc.overwrite(0, "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64);
 
 
     rsa.encrypt(enc, dec);
     rsa.encrypt(enc, dec);