Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibCrypto: RSA improvements and OpenSSL #3225

Merged
merged 6 commits into from
Jan 12, 2025

Conversation

devgianlu
Copy link
Contributor

@devgianlu devgianlu commented Jan 11, 2025

Continuing the saga of improving the crypto suite with OpenSSL, this PR brings some improvements:

  • Refactor PKSystem and RSA::generate_key_pair to return ErrorOr instead of failing and printing to stderr
  • Remove unsafe default RSA key size (not used anyways)
  • Refactor OpenSSL RAII wrappers
  • Finally, use OpenSSL to generate RSA keys

The last step allows us to complete more WPT tests by not timing out because we are too slow.

Not currently needed as it cannot fail, but useful for future commits.
The current default is unsafe, but determining a safe value is not easy.
Leave it up to the caller to decide.
Make `encrypt`, `decrypt`, `sign` and `verify` return `ErrorOr` for
better error propagation.
@devgianlu devgianlu requested a review from alimpfard as a code owner January 11, 2025 11:19
@devgianlu devgianlu force-pushed the rsa-openssl branch 5 times, most recently from d00e453 to b605460 Compare January 11, 2025 17:10
@devgianlu
Copy link
Contributor Author

CI is finally ok, had to restart it a couple of times because of flaky tests :(

@tcl3
Copy link
Member

tcl3 commented Jan 11, 2025

I don't have any comment on the code, but here are the results I got from running
./Meta/WPT.sh run WebCryptoAPI/generateKey locally:

Before:

Ran 98 tests finished in 65.4 seconds.
  • 68 ran as expected. 0 tests skipped.
  • 30 tests had unexpected subtest results

After

Ran 98 tests finished in 5.5 seconds.
  • 70 ran as expected. 0 tests skipped.
  • 28 tests had unexpected subtest results

Which is about 12x faster! 🚀

@devgianlu
Copy link
Contributor Author

Which is about 12x faster! 🚀

Didn't think about doing a benchmark, this is awesome!

@tcl3
Copy link
Member

tcl3 commented Jan 11, 2025

Which is about 12x faster! 🚀

Didn't think about doing a benchmark, this is awesome!

Actually, it's better than that, since the "before" run includes some tests that timed out!

Libraries/LibCrypto/OpenSSL.h Show resolved Hide resolved
Libraries/LibCrypto/PK/RSA.cpp Outdated Show resolved Hide resolved
These methods allow to convert between OpenSSL big numbers and ours.
Replace our slow, possibly incorrect RSA key generation with OpenSSL.

This should fix many WPT tests that are timing out because we were too
slow at computing keys.
@gmta gmta enabled auto-merge (rebase) January 11, 2025 22:34
IntegerType p;
IntegerType q;
IntegerType lambda;
auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "RSA", nullptr)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I don't quite understand what the difference between TRY(OpenSSL_FOO::wrap(operation)) and OPENSSL_TRY(operation) is supposed to be, is the first one just making a smart pointer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the first one just making a smart pointer?

Yes, that is something that was discussed in my previous PR: instead of using a bunch of scope guards to free memory we introduced some RAII wrappers. However for stuff that is lightly used, I haven't made wrappers (e.g. params builder).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 why not OwnPtr with a custom deleter then?
It also feels the wrong way around, I'd expect OpenSSL_FOO::wrap(OPENSSL_TRY(operation))?

Copy link
Contributor Author

@devgianlu devgianlu Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not OwnPtr with a custom deleter then?

We were unsure whether at some it might be useful to have methods on the wrappers to simplify the most common OpenSSL patterns.

It also feels the wrong way around

I've put it that way so that you cannot misuse it and not call OPENSSL_TRY because it is called internally in ::wrap.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for clarifying!

@gmta gmta disabled auto-merge January 11, 2025 22:41
@alimpfard alimpfard enabled auto-merge (rebase) January 11, 2025 23:12
@alimpfard alimpfard merged commit fef1f62 into LadybirdBrowser:master Jan 12, 2025
8 checks passed
@devgianlu devgianlu deleted the rsa-openssl branch January 12, 2025 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants