Commit Graph

264 Commits

Author SHA1 Message Date
Sergey Bugaev
e720eadd9e LibCrypto: Skip the check against 2^32 on 32-bit
We can't have a length that large there. This was causing a build error
about shifting a 32-bit value by 32 bits.
2024-05-02 07:46:53 -06:00
Daniel Bertalan
cc92c3f551 LibCrypto+LibGfx: Fix GCC 14 compile errors
The C++ standard does not allow specifying the template parameters in
constructor declarations, see
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97202#c8.

Converting constructors have a higher priority that user-defined
conversion functions; let's constrain `Gfx::Size<T>(Gfx::Size<U>)` to
only be considered when `U` is convertible to `T`. This lets us fall
back to conversion operators in the case of `UISize` -> `IntSize`, for
instance. Clang is still okay without this, but MSVC would error out
similarly: https://godbolt.org/z/PTbeYPM7s

Note that a not-yet-committed patch is required for full compilation:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114784#c3
2024-04-20 15:52:29 -06:00
Dan Klishch
5ed7cd6e32 Everywhere: Use east const in more places
These changes are compatible with clang-format 16 and will be mandatory
when we eventually bump clang-format version. So, since there are no
real downsides, let's commit them now.
2024-04-19 06:31:19 -04:00
stelar7
0e53b87261 LibCrypto: Add OAEP 2024-04-08 09:34:49 -06:00
stelar7
73a534494c LibCrypto: Add MGF1 2024-04-08 09:34:49 -06:00
Lucas CHOLLET
de6507ef94 LibCrypto: Remove simple-template-id from constructors
This is disallowed as from C++20. GCC 14 gives an error for these two.

See:
https://cplusplus.github.io/CWG/issues/2237.html
https://stackoverflow.com/questions/71978335/class-templates-constructor-declaration-doesnt-compile-for-c20-but-compiles
2024-04-07 21:33:46 +02:00
stelar7
b3e112fa51 LibCrypto: Fix off-by-one error when bit count is a multiple of 256 2024-04-05 21:53:12 +02:00
stelar7
f9dd028119 LibWeb: Add RSAOAEP.exportKey for the spki format 2024-04-05 21:53:12 +02:00
stelar7
54a3ffcd42 LibCrypto: Add early exit to SECP.verify if signature is invalid 2024-04-03 13:10:01 -06:00
Ali Mohammad Pur
ea692338c2 LibCrypto: Allow CMS padding to span an entire block
This is in line with what the spec states, the previous implementation
excluded the case where the original message's length is a multiple of
block_size, which would lead to a full block of padding.
2024-03-26 19:39:03 +00:00
Hendiadyoin1
70cfa60f56 LibCrypto: Remove now unused temporary BigInt buffers
Plus 1 drive-by division->shift optimization
2024-03-25 14:26:29 -06:00
Hendiadyoin1
c96d44e9cf LibCrypto: Use a right shift for division in NumberTheory::Power 2024-03-25 14:26:29 -06:00
Hendiadyoin1
4339868296 LibCrypto: Use Knuths algorithm D for BigInt division 2024-03-25 14:26:29 -06:00
Hendiadyoin1
1af9fa1968 LibCrypto: Implement arbitrarily sized right shifts
Previously we could only shift by words at a time
2024-03-25 14:26:29 -06:00
stelar7
0359e8848a LibCrypto: Add roundtrip test for parsing RSA private keys 2024-03-25 14:06:19 -06:00
stelar7
1be411cc73 LibCrypto: Adjust DER length encoding to follow the spec
It says to always use the smallest possible length representation
2024-03-25 14:06:19 -06:00
Ali Mohammad Pur
0994aa91dc LibCrypto: Remove unused Crypto::PK::EMSA_PSS class
This is not used, and its implementation is not actually correct
regardless.
2024-03-16 01:17:02 -06:00
Ali Mohammad Pur
15836cc865 LibCrypto: Parse and store all RSA private key components 2024-03-16 01:17:02 -06:00
Ali Mohammad Pur
dc1180d6b2 LibCrypto: Use UnsignedBigInteger::is_odd() instead of manually checking
The previous implementation ignored "empty" bigints that represented
zero, leading to random crashes.
2024-03-16 01:17:02 -06:00
Ali Mohammad Pur
d451f84f31 LibCrypto: Add a minimal DER encoder
Progress towards #23562.
2024-03-16 01:17:02 -06:00
Andrew Kaster
008c89edde LibCrypto: Add observers for the *byte* length of UnsignedBigInteger
When calling the export_data method, it's a bit of a hassle to remember
that the caller's buffer needs to be the length() * Word.
2024-03-13 15:31:00 -06:00
Andrew Kaster
139ff3552c LibCrypto: Allow callers to pass exponent to RSA::generate_key_pair 2024-03-13 15:31:00 -06:00
Timothy Flynn
928287b782 LibCrypto: Store ASN1 certificate timestamps as UnixDateTime
We are currently using Core::DateTime, which is meant to represent local
time. However, we are doing no conversion between the parsed time in UTC
and local time, so we end up comparing time stamps from different time
zones.

Instead, store the parsed times as UnixDateTime, which is UTC. Then we
can always compare the parsed times against the current UTC time.

This also lets us store parsed milliseconds.
2024-03-08 00:41:23 +01:00
Dan Klishch
86d54a8684 JSSpecCompiler: Parse arbitrarily large rational numbers in xspec mode 2024-02-24 15:03:08 -07:00
Dan Klishch
2a2e31f2ed LibCrypto: Avoid UB in BigFraction::to_byte_string for 0/x fractions 2024-02-24 15:03:08 -07:00
implicitfield
05ee5ffa36 LibCrypto: Add support for the POSIX cksum algorithm 2024-01-18 18:01:26 +03:30
Tim Ledbetter
d545fb2b60 LibCrypto: Parse negative input correctly in BigFraction::from_string()
Previously, when calling `BigFraction::from_string()`, the fractional
part of the number was always treated as positive. This led to an
incorrect result if the input string was negative.
2024-01-14 20:15:15 +01:00
Tim Ledbetter
48a3a02238 LibCrypto: Make constructing a BigInteger from string fallible
Previously, constructing a `UnsignedBigInteger::from_base()` could
produce an incorrect result if the input string contained a valid
Base36 digit that was out of range of the given base. The same method
would also crash if the input string contained an invalid Base36 digit.
An error is now returned in both these cases.

Constructing a BigFraction from string is now also fallible, so that we
can handle the case where we are given an input string with invalid
digits.
2024-01-13 19:01:35 -07:00
Tim Ledbetter
0b0c7693e2 LibCrypto: Prefer operator when converting string literal to BigInteger 2024-01-13 19:01:35 -07:00
kleines Filmröllchen
eada4f2ee8 AK: Remove ByteString from GenericLexer
A bunch of users used consume_specific with a constant ByteString
literal, which can be replaced by an allocation-free StringView literal.

The generic consume_while overload gains a requires clause so that
consume_specific("abc") causes a more understandable and actionable
error.
2024-01-12 17:03:53 -07:00
Ali Mohammad Pur
b1297a267c LibCrypto: Avoid branching in galois_multiply()
This makes GHash a little more than twice as fast.
2024-01-03 14:59:59 +01:00
Shannon Booth
e2e7c4d574 Everywhere: Use to_number<T> instead of to_{int,uint,float,double}
In a bunch of cases, this actually ends up simplifying the code as
to_number will handle something such as:

```
Optional<I> opt;
if constexpr (IsSigned<I>)
    opt = view.to_int<I>();
else
    opt = view.to_uint<I>();
```

For us.

The main goal here however is to have a single generic number conversion
API between all of the String classes.
2023-12-23 20:41:07 +01:00
Ali Mohammad Pur
5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30
Michiel Visser
000f0274e2 LibCrypto: Fix SECP384r1 verification when hash is SHA256
Some websites actually provide a SECP384 certificate which is signed
using a SHA256 hash. We assumed that SECP384 always used a SHA384 hash,
but this is not the case.
2023-11-29 18:17:14 +03:30
Michiel Visser
bc9cdd4394 LibCrypto+LibTLS: Switch to the generic SECPxxxr1 implementation 2023-11-27 09:43:07 +03:30
Michiel Visser
8a487f2c26 LibCrypto: Add generic SECPxxxr1 elliptic curve implementation
This implementation can be used to provide the SECP256r1 and SECP384r1
elliptic curves, while only requiring a single implementation.
2023-11-27 09:43:07 +03:30
Michiel Visser
0db6e0449e LibCrypto: Add method to copy Crypto::Hash::Manager 2023-11-27 09:37:25 +03:30
Michiel Visser
6322d68b1b LibCrypto: Add SECP384r1 implementation
This implementation is basically a copy-paste of the SECP256r1
implementation with all "256" replaced with "384".

In the future it might be nice to make this generic, instead of having
two almost identical copies of code.
2023-11-11 14:40:10 +03:30
Michiel Visser
6b5c6e7c03 LibCrypto: Move JacobianPoint into the curve cpp file 2023-11-11 14:40:10 +03:30
Michiel Visser
c6b2a07326 LibCrypto: Add static_assert to check that A = -3 mod p
This is required for some optimization made in the file. While this
should always be the case for the SECP256r1 curve, it is good to check
it anyway.
2023-11-10 09:54:00 -07:00
Michiel Visser
4f4034d1a4 LibCrypto: Also check Z when checking if point is on the curve 2023-11-10 09:54:00 -07:00
Michiel Visser
399dc211fa LibCrypto: Use simplified u256 constructor when reading big endian data 2023-11-10 09:54:00 -07:00
Michiel Visser
153cd3ecf0 LibCrypto: Use square-and-multiply loop for modular_inverse
Instead of using the hand unrolled version of the square-and-multiply
algorithm, we now simply use a loop.
2023-11-10 09:54:00 -07:00
Michiel Visser
5b658c341c LibCrypto: Change modular_add_order to match modular_add 2023-11-10 09:54:00 -07:00
Michiel Visser
d746c01716 LibCrypto: Simplify modular addition and subtraction
Instead of building the REDUCE_PRIME constant on the fly from the carry
flag, we now simply use the constant in combination with select. This
improves the readablility of the functions significantly.
2023-11-10 09:54:00 -07:00
Michiel Visser
5da070ba5e LibCrypto: Remove many magic constants and calculate them instead
Instead of having a large list of magical constants, we now only have
the curve prime, a, b, and order, which are all taken from the
specification. All the other helper constants are now calculated from
the curve paramters.
2023-11-10 09:54:00 -07:00
Michiel Visser
caf533bddf LibCrypto: Add ECDSA signature checking with secp256r1 2023-10-30 10:17:39 -06:00
stelar7
73ef102b01 LibCrypto: Add Chacha20Poly1305 2023-09-26 13:22:04 +03:30
implicitfield
1b3ad1c721 LibCrypto: Add support for BLAKE2b 2023-09-17 16:49:35 +03:30
kleines Filmröllchen
fb37587efe LibCrypto: Implement a generic 16-bit CRC
This is mostly a 16-bit version of the 8-bit CRC, using the same general
byte-LUT algorithm.
2023-08-12 12:25:26 -06:00