Commit graph

198 commits

Author SHA1 Message Date
Timothy Flynn
f070264800 Everywhere: Remove sv suffix from format string literals
This prevents the compile-time checks that would catch errors in the
format invocation (which would usually lead to a runtime crash).
2025-04-08 20:00:18 -04:00
Manuel Zahariev
4ed8e9e596 LibCrypto: Improve precision of Crypto::BigFraction::to_double()
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Before:
    - FIXME: very naive implementation
    - was preventing passing some Temporal tests
    - https://github.com/tc39/test262
    - https://github.com/LadybirdBrowser/libjs-test262

Bonus: Unrelated formatting change (Line 249) that unblocks the CI
lint check.
2025-03-23 19:33:25 +01:00
Manuel Zahariev
d2ea77c099 LibCrypto: Improve efficiency of UnsignedBigInteger::shift_left
Before:
- a separate Word element allocation of the underlying Vector<Word> was
necessary for every new word in a multi-word shift
- two additional temporary UnsignedBigInteger buffers were allocated
and passed through, including in downstream calls (e.g. Multiplication)
- an additional allocation and word shift for the carry
- FIXME note seems to point to some of these issues

After:
- main change is in LibCrypto/BigInt/Algorithms/BitwiseOperations.cpp
- one single allocation per call, using shift_left_by_n_words
- only the input "number" and "output" need to be allocated by the
  caller
- downstream calls are adapted not to allocate or pass temporary
  buffers
- noticeable performance improvement when running TestBigInteger:
  0.41-0.42s (before) to 0.28-0.29s (after) Intel Core i9 laptop

Bonus: remove unused variables from UnsignedBigInteger::divided_by
- These were likely cut-and-paste artifacts from
  UnsignedBigInteger::multiplied_by; not caught by "unused-varible".

NOTE: making this change in a separate commit than shift_right, even if
it touches the same file BitwiseOperations.cpp since:
- it is a "bonus" addition: not necessary for fixing the shift_right
  bug, but logically unrelated to the shift_right code
- it brings a chain of downstream interface modifications (7 files),
  unrelated to shift_right
2025-03-23 19:33:25 +01:00
Manuel Zahariev
05cfbdd6fb LibCrypto: Add support for shift_right more than one word
- Before: UnsignedBigInteger::shift_right( n ) trigger index
  verification error for n>31. An assumption of
  num_bits<UnsignedBigInteger::BITS_IN_WORD was being made
- After: shift_right( n ) works correctly for n>31.

NOTE: "bonus" change; not necessary for fixing BigFraction::to_double
2025-03-23 19:33:25 +01:00
Jess
12cbefbee7 LibJS+LibCrypto: Use a bitwise approach for BigInt's as*IntN methods
This speeds up expressions such as `BigInt.asIntN(0x4000000000000, 1n)`
(#3615). And those involving very large bigints.
2025-03-20 09:44:12 +01:00
devgianlu
2b3934e34e LibCrypto: Remove leftover checksum helpers 2025-03-19 13:46:50 +01:00
devgianlu
f3631d6517 LibCrypto: Remove unused cksum class 2025-03-19 13:46:50 +01:00
devgianlu
6b2515657c LibCrypto: Remove unused CRC32, CRC16 and CRC8 classes 2025-03-19 13:46:50 +01:00
devgianlu
0561d130f3 LibCrypto: Remove unused Adler32 class 2025-03-19 13:46:50 +01:00
devgianlu
40c71ff3c0 LibCrypto: Remove unused GHash class 2025-03-02 15:11:38 +01:00
devgianlu
05f3b1f361 LibCrypto+LibWeb: Refactor AES implementation with OpenSSL 2025-03-02 15:11:38 +01:00
devgianlu
75841f5920 LibCrypto: Do not print OpenSSL errors with redundant newline 2025-03-02 15:11:38 +01:00
devgianlu
80fe259dab LibCrypto: Refactor HMAC implementations with OpenSSL 2025-03-02 15:11:38 +01:00
devgianlu
e90d2a5713 LibCrypto+LibWeb: Refactor HKDF and PBKDF2 classes with OpenSSL 2025-02-24 11:11:05 +01:00
devgianlu
60dcf3e023 LibCrypto: Refactor Edwards-curves implementation with OpenSSL 2025-02-24 11:11:05 +01:00
Jess
8fda05d8b7 LibCrypto: Introduce a falible API for SignedBigInteger::shift_left 2025-02-19 09:00:59 -05:00
devgianlu
f630ca7cd0 LibCrypto: Rename and remove unused methods from SECPxxxr1 class 2025-02-18 00:02:26 +01:00
devgianlu
048d6b8012 LibCrypto: Remove unused constructors from RSA class 2025-02-18 00:02:26 +01:00
devgianlu
3431b3235c LibCrypto: Prevent exporting invalid DER RSAPrivateKey 2025-02-18 00:02:26 +01:00
devgianlu
7180c5f13b LibCrypto: Remove unused EllipticCurve abstract class
This was required only to support our custom TLS implementation, but
does not serve any purpose other than forcing improper APIs.
2025-02-18 00:02:26 +01:00
devgianlu
8ae01f81c9 LibCrypto: Remove unused MGF class 2025-02-18 00:02:26 +01:00
devgianlu
510aa223c2 LibCrypto: Remove unused ChaCha20 class 2025-02-18 00:02:26 +01:00
devgianlu
7fb15ef936 LibCrypto: Remove unused Poly1305 class 2025-02-18 00:02:26 +01:00
devgianlu
ce3e79a402 LibCrypto: Remove unused ChaCha20Poly1305 class 2025-02-18 00:02:26 +01:00
devgianlu
bc0bb0d535 LibCrypto: Remove unused Certificate class 2025-02-18 00:02:26 +01:00
devgianlu
b6ec31b590 LibCrypto: Move OPENSSL_TRY helper function outside namespace
This allows using `OPENSSL_TRY` and `OPENSSL_TRY_PTR` outside of the
`Crypto` namespace.
2025-02-17 19:52:43 +01:00
devgianlu
12ca074671 LibCrypto: Always handle SECPxxxr1 scalars with leading zeros
It may happen that the scalars used by SECPxxxr1 turn out to be slightly
smaller than their actual size when serialized to `UnsignedBigInteger`,
especially for P521. Handle this case by serializing zeros instead of
failing.

Originally discovered as a flaky WPT test.
2025-01-29 12:46:46 +01:00
devgianlu
1d207aa0d8 LibCrypto: De-templetize and move SECPxxxr1 methods out of line 2025-01-27 12:24:48 +01:00
devgianlu
cceb7110fc LibCrypto: Remove old SECPxxxr1 math 2025-01-27 12:24:48 +01:00
devgianlu
cf5ce8277f LibCrypto: Use OpenSSL for SECPxxxr1 sign/verify operations 2025-01-27 12:24:48 +01:00
devgianlu
f2e530ec14 LibCrypto: Make SECPxxxr1Signature carry the scalar size
Our `UnsignedBigInteger` implementation cannot handle numbers whose
size is not a multiple of 4. For this reason we need to carry the real
size around for P-521 support.
2025-01-27 12:24:48 +01:00
devgianlu
fd2014f5c2 LibCrypto: Use OpenSSL for SECPxxxr1 point computation 2025-01-27 12:24:48 +01:00
devgianlu
aefffa9455 LibCrypto: Use OpenSSL for SECPxxxr1 key generation 2025-01-27 12:24:48 +01:00
devgianlu
de6f77e7e3 LibCrypto: Print all descriptive OpenSSL errors
The code was printing one error message only, but multiple can be
generated in one call. Additionally, using this builtin produces
a much more descriptive output.
2025-01-27 12:24:48 +01:00
devgianlu
3eeb35e787 LibCrypto: Implement RSA_PSS_EMSA with OpenSSL 2025-01-17 12:43:03 +01:00
devgianlu
24e374d7e1 LibCrypto: Remove old unused padding schemes and codes 2025-01-13 17:00:18 +01:00
devgianlu
a59b48cffc LibCrypto+LibWeb: Replace RSA_OAEP-EME implementation
This replaces the old `OAEP` implementation with one backed by OpenSSL.
The changes also include some added modularity to the RSA class by
making the `RSA_EME` and `RSA_EMSE` for encryption/decryption and
signing/verifying respectively.
2025-01-13 17:00:18 +01:00
devgianlu
70bc26e32a LibCrypto+LibTLS: Replace RSA_PKCS1-EMSA implementation
This commit replaces the old implementation of `EMSA_PKCS1_V1_5` with
one backed by OpenSSL. In doing so, the `sign` and `verify` methods of
RSA have been modified to behave like expected and not just be
encryption and decryption.

I was not able to split this commit because the changes to `verify` and
`sign` break pretty much everything.
2025-01-13 17:00:18 +01:00
devgianlu
6e721110f9 LibCrypto: Make RSA class easily configurable
This is a small change to allow subclasses of `RSA` to configure the
`EVP_PKEY_CTX` without rewriting everything.
2025-01-13 17:00:18 +01:00
devgianlu
91c393ea98 LibCrypto: Use OpenSSL for RSA encryption and decryption 2025-01-13 17:00:18 +01:00
devgianlu
daa81c9b32 LibCrypto: Remove OpenSSL as a public header dependency
Add a forwarding header for OpenSSL types so that we can build without
propagating the OpenSSL dependency.
2025-01-13 17:00:18 +01:00
devgianlu
559c5a7311 LibCrypto: Move OpenSSL RAII helper methods out of line 2025-01-13 17:00:18 +01:00
devgianlu
977af95b5b LibCrypto: Move hash constructors out of line 2025-01-13 17:00:18 +01:00
devgianlu
0fc02d4d00 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.
2025-01-13 17:00:18 +01:00
devgianlu
fef1f62ecc LibCrypto: Use OpenSSL to generate RSA keys
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.
2025-01-12 01:13:19 +01:00
devgianlu
130f890497 LibCrypto: Add methods to convert OpenSSL BN <-> UnsignedBigInteger
These methods allow to convert between OpenSSL big numbers and ours.
2025-01-12 01:13:19 +01:00
devgianlu
7b38923144 LibCrypto: Refactor OpenSSL RAII wrappers to a macro 2025-01-12 01:13:19 +01:00
devgianlu
df05cc8478 LibCrypto: Make PKSystem methods return ErrorOr
Make `encrypt`, `decrypt`, `sign` and `verify` return `ErrorOr` for
better error propagation.
2025-01-12 01:13:19 +01:00
devgianlu
6ba627b047 LibCrypto: Remove default key size for RSA::generate_key_pair
The current default is unsafe, but determining a safe value is not easy.
Leave it up to the caller to decide.
2025-01-12 01:13:19 +01:00
devgianlu
9e08f71fd9 LibCrypto: Make RSA::generate_key_pair return ErrorOr
Not currently needed as it cannot fail, but useful for future commits.
2025-01-12 01:13:19 +01:00