Commit graph

164 commits

Author SHA1 Message Date
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
devgianlu
c23765c8f2 LibCrypto: Add Ed448 OID to know algorithm identifiers 2025-01-11 11:13:06 +01:00
devgianlu
27b4bae78b LibCrypto: Implement Ed448
Implement the Ed448 curve for signing and verifying using OpenSSL.

The methods could be all made static, but all other curves are not.
I think this is material for further refactoring.
2025-01-11 11:13:06 +01:00
devgianlu
4817ca489f LibCrypto: Add useful macros and classes for working with OpenSSL
Add a couple of macros to aid error handling with OpenSSL and some RAII
classes that manage the lifetime of some OpenSSL objects.
2025-01-11 11:13:06 +01:00
Timothy Flynn
27478ec7d4 Everywhere: Run clang-format
The following command was used to clang-format these files:

    clang-format-19 -i $(find . \
        -not \( -path "./\.*" -prune \) \
        -not \( -path "./Build/*" -prune \) \
        -not \( -path "./Toolchain/*" -prune \) \
        -type f -name "*.cpp" -o -name "*.mm" -o -name "*.h")
2024-12-28 05:39:32 -08:00
rmg-x
f55f507e56 Meta+LibCrypto: Add SecureRandom and replace PRNG usage with it
This adds a thin wrapper to LibCrypto for generating cryptographically
secure random values and replaces current usages of PRNG within
LibCrypto as well.
2024-12-24 17:54:52 +01:00
devgianlu
89061dd3c4 LibCrypto: Replace all hashes implementation with OpenSSL
This required multiple changes:
- Make hashes non-copiable because they contain a heap allocated pointer
- Reference classes via `NonnullOwnPtr` only (they are non-copiable)
- Drop all existing hashes implementations
- Use the `OpenSSLHashFunction` base class to implement the same hashes

I was not able to come up with a way to divide this commit into multiple
without increasing the amount of changes.

Nothing breaks with this commit!
2024-12-22 18:53:45 +01:00
devgianlu
2d799727e8 LibCrypto: Introduce utility class for OpenSSL backed hashes
This abstract class allows implementing hashes backed by OpenSSL with
very few lines of code, see next commit.
2024-12-22 18:53:45 +01:00
devgianlu
002a93a33c LibCrypto: Link with OpenSSL
Add OpenSSL with vcpkg and link with LibCrypto using CMake.

Also added a placeholder GN setup.
2024-12-22 18:53:45 +01:00
Timothy Flynn
edd3b14ddf LibCrypto: Protect the SignedBigInteger ctor against integer overflow
In particular, if given a value of -2147483648, we would invoke signed
integer overflow (which is UB).
2024-12-19 23:37:30 +01:00
devgianlu
8620a2af47 LibCrypto: Ensure RSA decryption with CRT works for all inputs
Ensure becomes `m1` greater than `m2` even when smaller by more than
one `p`. Since the next operations on `m1` are modulus `p` we can add it
as many times as it's needed.
2024-12-19 18:43:23 +01:00
devgianlu
1d94d678b3 LibCrypto: Implement AES-KW
Add the AES-KW (Key Wrap) implementation as of
https://www.rfc-editor.org/rfc/rfc3394#section-4.2.

Tests are taken from section 4 of RFC3394.
2024-12-17 11:00:14 +01:00
devgianlu
1ae28324bd LibCrypto: Accept correct IV sizes for AES-GCM
AES-GCM should accept 96-bits keys as is. Any other key should be
preprocessed with GHASH.
2024-12-16 13:27:53 +01:00
devgianlu
3167d4f06b LibCrypto: Move GHash hashing routine to separate function
This allows for the function to be used outside `GHash`. In particular,
it'll be used for IV preparation in AES-GCM.
2024-12-16 13:27:53 +01:00
devgianlu
08af878466 LibCrypto+LibWeb: Allow serializing key info without params
Previously, if `nullptr` was passed as params for
`wrap_in_private_key_info` or `wrap_in_subject_public_key_info` an ASN1
null was serialized. This was not the intended behaviour for many.

The issue was discovered while implementing `wrapKey` and `unwrapKey` in
the next commits.
2024-12-16 11:35:00 +01:00
devgianlu
57cc248883 LibCrypto: Add optimized RSA decryption with CRT method
The textbook RSA decryption method of `c^d % n` is quite slow. If the
necessary parameters are present, the CRT variant will be used.
Performing RSA decryption this way is ~3 times faster.
2024-12-15 23:31:49 +01:00
devgianlu
ec990d620f LibCrypto: Cleanup Crypto::PK::RSA constructors to avoid pitfalls
- Removed the constructor taking a (n, d, e) tuple and moved
  it to `RSAPrivateKey`
- Removed default constructor with key generation because it was always
  misused and the default key size is quite small
- Added utility constructors to accept a key pair, public key, private
  key or both
- Made constructor parameters const
- Updated test to use generated random keys where possible
2024-12-15 23:31:49 +01:00
devgianlu
f49a55d089 LibCrypto: Update ModularInverse implementation to use extended GCD
The previous implementation of `ModularInverse` was flaky and did not
compute the correct value in many occasions, especially with big numbers
like in RSA.

Also added a bunch of tests with big numbers.
2024-12-15 23:31:49 +01:00
devgianlu
b35764da0e LibCrypto: Add extended GCD algorithm 2024-12-15 23:31:49 +01:00
devgianlu
a74ef5df3d LibCrypto: Reset cached trimmed length after add_into_accumulator
The trimmed cache length of the `UnsignedBigInteger` was not reset after
an `add_into_accumulator_without_allocation` operation because the
function manipulates the words directly.

This meant that if the trimmed length was calculated before this
operation it would be wrong after.
2024-12-15 23:31:49 +01:00
devgianlu
9240d38273 LibCrypto+LibTLS+LibWeb: Store EC key size + refactor serialization
In order for public/private key serialization to work correctly we must
store the size of the key because P-521 cannot be stored as full words
inside `UnsignedBigInteger` and therefore is exported as the wrong
length (68 instead of 66).

This makes it also possible to refactor some methods and cleanup
constants scattered around.

Gets almost all import/export tests, expect the JWK ones that calculate
the public key on export. The `SECPxxxr1` implementation currently fails
to do calculations for P-521.
2024-12-14 01:52:16 +01:00
devgianlu
c3aa8af514 LibCrypto: Define SECP521r1
Define SECP521r1 with its constants. Since the parameters cannot be
represented as full bytes, a slight modification has been added to the
byte size.

The current implementation of SECPxxxr1 does not work with this curve.
2024-12-14 01:52:16 +01:00
R-Goc
e2b6ab4a69 LibCrypto: DER.cpp use uz literals 2024-12-08 17:18:12 -07:00
R-Goc
3e69794c7d LibCrypto: Use size_t integer literal 2024-12-08 17:18:12 -07:00
devgianlu
c7a1287a4f LibCrypto: Remove ASN1 encoding/decoding from SECPxxxr1
Little refactoring to remove the last bits of ASN1 decoding/encoding
from within the `SECPxxxr1` class. It was a bit confusing for the
`SECPxxxr1` methods to handle ASN1 internally implicitly. Some explicit
methods are available to achieve the same functionality on the data
structures.
2024-12-07 19:08:40 +01:00
devgianlu
bce2893638 LibCrypto: Add utility functions to SECPxxxr1
This allows to move ASN1 logic from inside the `SECPxxxr1` curve
itself to the data structures. It makes more sense to have dedicated and
explicit methods to handle transformation between formats.
2024-12-07 19:08:40 +01:00
Timothy Flynn
912e38c5fb LibCrypto: Add a BigFraction::is_zero helper 2024-12-04 08:01:35 -05:00
Timothy Flynn
e6c96ce57e LibCrypto: Convert BigFraction stringification to String 2024-12-04 08:01:35 -05:00
Jonne Ransijn
d7596a0a61 AK: Don't implicitly convert Optional<T&> to Optional<T>
C++ will jovially select the implicit conversion operator, even if it's
complete bogus, such as for unknown-size types or non-destructible
types. Therefore, all such conversions (which incur a copy) must
(unfortunately) be explicit so that non-copyable types continue to work.

NOTE: We make an exception for trivially copyable types, since they
are, well, trivially copyable.

Co-authored-by: kleines Filmröllchen <filmroellchen@serenityos.org>
2024-12-04 01:58:22 +01:00
devgianlu
dace140b0d LibCrypto+LibWeb: Implement ECDSA.sign
Gained ~20 tests, failing only on P-521.
2024-12-03 13:20:51 +01:00
devgianlu
1d11448f00 LibCrypto+LibWeb: Refactor integer conversions in SECPxxxr1
Little effort to refactor the chaos of integers / bytes / ASN.1 that
is inside `SECPxxxr1`. More love is needed.
2024-12-03 13:20:51 +01:00
devgianlu
0c60f7c995 LibWeb: Migrate ECDSA.generateKey and ECDSA.verify away from ByteBuffer
Use instances of `ECPublicKey` and `ECPrivateKey` instead of
`ByteBuffer` for ECDSA. Fixes another ~200 tests.
2024-12-03 13:20:51 +01:00
devgianlu
399b3d2430 LibCrypto+LibWeb: Parse EC public key in parse_subject_public_key_info
Replicate what we are doing with RSA and parse both the private and
public key when parsing the ASN1.

The only thing that changed in the tests is the error message.
2024-11-30 11:17:44 +01:00
devgianlu
1f7586ce14 LibCrypto: Use ASN1 macros for RSA key parsing
Improve error handling in `RSA::parse_rsa_key` by using ASN1 macros and
generalizing the parsing to both private and public keys.
2024-11-30 11:17:44 +01:00
devgianlu
ee50a8c0f7 LibCrypto: Do not parse PKCS#8 in RSA::parse_rsa_key
The decoding inside `RSA::parse_rsa_key` is quite complex because it
tries to understand if it's decoding PKCS#8 or PKCS#1. Simplify the code
 by moving the burden to the PEM decoder.
2024-11-30 11:17:44 +01:00