mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-31 05:09:12 +00:00
LibCrypto: Make PKSystem
methods return a ByteBuffer
directly
It used to be that the caller would supply a buffer to write the output to. This created an anti-pattern in multiple places where the caller would allocate a `ByteBuffer` and then use `.bytes()` to provide it to the `PKSystem` method. Then the callee would resize the output buffer and reassign it, but because the resize was on `Bytes` and not on `ByteBuffer`, the caller using the latter would cause a bug. Additionally, in pretty much all cases the buffer was pre-allocated shortly before.
This commit is contained in:
parent
81eb66c6eb
commit
0fc02d4d00
Notes:
github-actions[bot]
2025-01-13 16:02:16 +00:00
Author: https://github.com/devgianlu
Commit: 0fc02d4d00
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3234
9 changed files with 69 additions and 112 deletions
|
@ -25,10 +25,7 @@ TEST_CASE(test_RSA_raw_encrypt)
|
|||
"8126832723025844890518845777858816391166654950553329127845898924164623511718747856014227624997335860970996746552094406240834082304784428582653994490504519"_bigint,
|
||||
"65537"_bigint,
|
||||
});
|
||||
ByteBuffer buffer = {};
|
||||
buffer.resize(rsa.output_size());
|
||||
auto buf = buffer.bytes();
|
||||
TRY_OR_FAIL(rsa.encrypt(data, buf));
|
||||
auto buf = TRY_OR_FAIL(rsa.encrypt(data));
|
||||
EXPECT(memcmp(result, buf.data(), buf.size()) == 0);
|
||||
}
|
||||
|
||||
|
@ -39,13 +36,10 @@ TEST_CASE(test_RSA_PKCS_1_encrypt)
|
|||
|
||||
auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));
|
||||
Crypto::PK::RSA_PKCS1_EME rsa(keypair);
|
||||
ByteBuffer buffer = {};
|
||||
buffer.resize(rsa.output_size());
|
||||
auto buf = buffer.bytes();
|
||||
TRY_OR_FAIL(rsa.encrypt(data, buf));
|
||||
TRY_OR_FAIL(rsa.decrypt(buf, buf));
|
||||
auto enc = TRY_OR_FAIL(rsa.encrypt(data));
|
||||
auto dec = TRY_OR_FAIL(rsa.decrypt(enc));
|
||||
|
||||
EXPECT(memcmp(buf.data(), "hellohellohellohellohellohellohellohellohello123-", 49) == 0);
|
||||
EXPECT(memcmp(dec.data(), "hellohellohellohellohellohellohellohellohello123-", 49) == 0);
|
||||
}
|
||||
|
||||
// RSA | ASN1 PKCS1 DER / PEM encoded Key import
|
||||
|
@ -138,19 +132,14 @@ c8yGzl89pYST
|
|||
|
||||
EXPECT_EQ(keypem, StringView(priv_pem));
|
||||
|
||||
ByteBuffer enc_buffer = {};
|
||||
enc_buffer.resize(rsa_from_pair.output_size());
|
||||
ByteBuffer msg_buffer = {};
|
||||
msg_buffer.resize(rsa_from_pair.output_size());
|
||||
|
||||
ByteBuffer dec_buffer = {};
|
||||
dec_buffer.resize(rsa_from_pair.output_size());
|
||||
auto msg = msg_buffer.bytes();
|
||||
msg.overwrite(0, "WellHelloFriends", 16);
|
||||
|
||||
auto enc = enc_buffer.bytes();
|
||||
auto dec = dec_buffer.bytes();
|
||||
|
||||
dec.overwrite(0, "WellHelloFriends", 16);
|
||||
|
||||
TRY_OR_FAIL(rsa_from_pair.encrypt(dec, enc));
|
||||
TRY_OR_FAIL(rsa_from_pem.decrypt(enc, dec));
|
||||
auto enc = TRY_OR_FAIL(rsa_from_pair.encrypt(msg));
|
||||
auto dec = TRY_OR_FAIL(rsa_from_pem.decrypt(enc));
|
||||
|
||||
EXPECT_EQ(memcmp(dec.data(), "WellHelloFriends", 16), 0);
|
||||
}
|
||||
|
@ -160,19 +149,14 @@ TEST_CASE(test_RSA_encrypt_decrypt)
|
|||
auto keypair = TRY_OR_FAIL(Crypto::PK::RSA::generate_key_pair(1024));
|
||||
Crypto::PK::RSA rsa(keypair);
|
||||
|
||||
ByteBuffer enc_buffer = {};
|
||||
enc_buffer.resize(rsa.output_size());
|
||||
ByteBuffer msg_buffer = {};
|
||||
msg_buffer.resize(rsa.output_size());
|
||||
|
||||
ByteBuffer dec_buffer = {};
|
||||
dec_buffer.resize(rsa.output_size());
|
||||
auto msg = msg_buffer.bytes();
|
||||
msg.overwrite(0, "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64);
|
||||
|
||||
auto enc = enc_buffer.bytes();
|
||||
auto dec = dec_buffer.bytes();
|
||||
auto enc = TRY_OR_FAIL(rsa.encrypt(msg));
|
||||
auto dec = TRY_OR_FAIL(rsa.decrypt(enc));
|
||||
|
||||
enc.overwrite(0, "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64);
|
||||
|
||||
TRY_OR_FAIL(rsa.encrypt(enc, dec));
|
||||
TRY_OR_FAIL(rsa.decrypt(dec, enc));
|
||||
|
||||
EXPECT(memcmp(enc.data(), "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64) == 0);
|
||||
EXPECT(memcmp(dec.data(), "WellHelloFriendsWellHelloFriendsWellHelloFriendsWellHelloFriends", 64) == 0);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue