mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 20:52:45 +00:00 
			
		
		
		
	LibTLS+LibCrypto: Remove all remaining uses of ByteBuffer::wrap()
This commit is contained in:
		
							parent
							
								
									050eb5afa8
								
							
						
					
					
						commit
						d5600e966a
					
				
					 6 changed files with 29 additions and 24 deletions
				
			
		|  | @ -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; | ||||
|  |  | |||
|  | @ -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; | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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); | ||||
| 
 | ||||
|  |  | |||
|  | @ -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); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling