Commit Graph

70 Commits

Author SHA1 Message Date
Timothy Flynn
02e3633b7f AK: Move FormatParser definition from header to implementation file
This is primarily to be able to remove the GenericLexer include out of
Format.h as well. A subsequent commit will add AK::Result to
GenericLexer, which will cause naming conflicts with other structures
named Result. This can be avoided (for now) by preventing nearly every
file in the system from implicitly including GenericLexer.

Other changes in this commit are to add the GenericLexer include to
files where it is missing.
2021-08-19 23:49:25 +02:00
Idan Horowitz
ff6ca0f02d LibCrypto: Add operator<= and operator>= to SignedBigInteger 2021-07-19 09:11:20 +01:00
Idan Horowitz
75d1ffea00 LibCrypto: Add the >= operator to UnsignedBigInteger 2021-07-12 19:05:17 +01:00
Linus Groh
a216ea4c8d LibCrypto: Add missing implementation of SBI::divided_by(USBI) 2021-07-09 13:20:51 +01:00
Linus Groh
3e8574a9a8 LibCrypto: Add missing implementation of SBI::multiplied_by(USBI)
This only had a declaration and was creating linker errors when used.
Easily fixed!
2021-07-08 01:25:49 +01:00
Linus Groh
89641d90db LibCrypto: Add operator>() to UnsignedBigInteger and SignedBigInteger
Piggybacking on operator!=() and operator<().
2021-07-07 19:00:42 +01:00
Gal Horowitz
3872c31b08 LibCrypto: Replace incorrect operator in SignedBigInteger::bitwise_and 2021-07-01 11:37:16 +02:00
Gal Horowitz
38e9e35380 LibCrypto: Replace use of negate() in SignedBigInteger::bitwise_or
Calling negate() on a big integer does not make it negative, but
rather flips its sign, so this was not actually acting as an OR.
2021-07-01 11:37:16 +02:00
Idan Horowitz
005d75656e LibCrypto: Replace from_base{2,8,10,16}() & to_base10 with from_base(N)
This allows us to support parsing and serializing BigIntegers to and
from any base N (such that 2 <= N <= 36).
2021-06-29 16:55:54 +01:00
Andrew Kaster
97444f0a25 LibCrypto: Avoid unaligned reads in GHash constructor
The fact that this always reads 16 bytes from the input byte stream
for the key data is still a bit on the suspicious side, but at least
it won't crash UBSAN anymore.
2021-06-29 07:17:34 +04:30
Andreas Kling
49018553d3 LibJS+LibCrypto: Allow '_' as a numeric literal separator :^)
This patch adds support for the NumericLiteralSeparator concept from
the ECMAScript grammar.
2021-06-26 16:30:35 +02:00
DexesTTP
b205c9814a LibCrypto: Fix Hash::MD5's movability
Because MD5 stored a "Bytes {}" wrapper to its internal data buffer,
it was not actually movable. However, its use in several parts of
the system (such as HashManager) assumed it was, leading to crashes.

Fixes #8135
2021-06-19 01:29:21 +04:30
Idan Horowitz
2ad2e055e2 LibCrypto: Add {Signed,Unsigned}BigInteger::from_base{2, 8, 16} helpers
These can be used to create BigInteger instances from non-decimal
number strings.
2021-06-14 01:45:04 +01:00
Idan Horowitz
d7a70eb77c LibJS: Add all of the DataView.prototype.set* methods 2021-06-14 01:45:04 +01:00
Idan Horowitz
c54b9a6920 LibJS: Add all of the DataView.prototype.get* methods 2021-06-14 01:45:04 +01:00
Idan Horowitz
b17a282b4b LibCrypto: Add hash methods to {Signed, Unsigned}BigInteger
These just use hash the underlying bytes that make up the integer words
2021-06-09 11:48:04 +01:00
Gunnar Beutner
5f18cf75c5 AK: Replace ByteBuffer::grow with resize()/ensure_capacity()
Previously ByteBuffer::grow() behaved like Vector<T>::resize().
However the function name was somewhat ambiguous - and so this patch
updates ByteBuffer to behave more like Vector<T> by replacing grow()
with resize() and adding an ensure_capacity() method.

This also lets the user change the buffer's capacity without affecting
the size which was not previously possible.

Additionally this patch makes the capacity() method public (again).
2021-05-31 14:49:00 +04:30
Ben Wiederhake
05d49cc0cb LibCrypto: Fix bound checks when reading bitmaps
This only affects malformed RSA keys. Instead of accepting and
continuing with potentially broken pointers (and in ASAN, crashing), we
now consider bitmaps malformed, and stop parsing.

Found by OSS Fuzz: #31698, long-standing-bug:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=31698

Fun fact: The "if" only exists because of OSS Fuzz.
8cc279ed74
2021-05-31 00:47:43 +04:30
Ali Mohammad Pur
c94440860e LibCrypto: Use the new return-from-Variant::visit() mechanism
And simplify the code _even further_!
2021-05-19 22:45:34 +02:00
DexesTTP
3d27550ab7 LibCrypto: Make GCM movable 2021-05-19 09:18:45 +02:00
DexesTTP
f11f629731 LibCrypto: Use AK::Variant in HashManager 2021-05-19 09:18:45 +02:00
DexesTTP
73f585ceb4 LibCrypto: Use AK::Variant in MultiHashDigestVariant 2021-05-19 09:18:45 +02:00
DexesTTP
2c1916dd8d LibCrypto: Add the SHA-384 hash algorithm
This is a truncated version of SHA-512, so it was fairly trivial.
2021-05-19 09:18:45 +02:00
Lenny Maiorani
6bc3ed6266 LibCrypto: Change static constexpr array to function local constexpr
Problem:
- Static variables take memory and can be subject to less optimization
  (https://serenityos.godbolt.org/z/7EYebr1aa)
- This static variable is only used in 1 place.

Solution:
- Move the variable into the function and make it non-static.
2021-05-17 19:37:56 +01:00
Linus Groh
0aab774343 Everywhere: Fix a bunch of typos 2021-05-17 17:48:55 +01:00
Lenny Maiorani
adbf555e64 LibCrypto: Fix incorrectly constexpr variable
Problem:
- Clang ToT reports an error because `digest_size` cannot be evaluated
  at compile-time.

Solution:
- Change from using the member function to the `static` shadow of the
  NTTP.
2021-05-17 08:16:46 +02:00
Jean-Baptiste Boric
090936e424 Userland: Replace arc4random() with get_random<u32>() 2021-05-14 22:24:02 +02:00
DexesTTP
36a56871c0 LibCrypto: Prevent a signed overflow during BigInt Modular Power
The algorithm isn't explicit about what type this needs to be. But this
passes all of the tests, so that's probably fine.
2021-05-14 11:36:39 +02:00
Ali Mohammad Pur
df515e1d85 LibCrypto+LibTLS: Avoid unaligned reads and writes
This adds an `AK::ByteReader` to help with that so we don't duplicate
the logic all over the place.
No more `*(const u16*)` and `*(const u32*)` for anyone.
This should help a little with #7060.
2021-05-14 08:39:29 +01:00
Ali Mohammad Pur
a4e20a87d5 LibCrypto: Do not assume that the passed in IV is as long as a block
Just take ReadonlyBytes instead of a raw pointer.
Fixes #7072 (tested with the ASAN build fixed by #7060).
2021-05-14 08:39:29 +01:00
DexesTTP
fd80e7a339 LibCrypto: Reduce the UnsignedBigInteger inline size to 32 words
We never really needed the 512 words in the first place, and this does
reduce the stack allocations in montgomery modular power from 32Kb to
a more manageable 2Kb :^)

Note that the 32 words size doesn't provide any performance benefits or
drawbacks compared to other values. All values seem to have equivalent
performances (the tested values were 1, 2, 4, ..., 512). But since the
previous value of 512 was definitely too big, let's reduce it for now!
2021-05-13 19:18:07 +01:00
DexesTTP
485adb5e29 LibCrypto: Add the montgomery modular power algorithm
This algorithm allows for much faster computations of modular powers
(around a 5x-10x speedup of the Crypto test). However, it is only valid
for odd modulo values, and therefore the old algorithm must be kept for
computations involving even modulo values.
2021-05-13 19:18:07 +01:00
DexesTTP
5071989545 LibCrypto: Add a += operation to UnsignedBigIntegerAlgorithms
This new operation is immediately used in several existing algorithms.
2021-05-13 19:18:07 +01:00
DexesTTP
f4e6f58cc6 LibCrypto: Add the UnsignedBigInteger::Word alias
This makes it clearer which variables are operating on words instead
of directly operating on raw values.
2021-05-13 19:18:07 +01:00
DexesTTP
5963f6f9ff LibCrypto: Split BigInteger operations into an Algorithms class
Since the operations are already complicated and will become even more
so soon, let's split them into their own files. We can also integrate
the NumberTheory operations that would better fit there into this class
as well.

This commit doesn't change behaviors, but moves the allocation of some
variables into caller classes.
2021-05-13 19:18:07 +01:00
DexesTTP
0853d98420 LibCrypto: Fix an out-of-bounds access in UnsignedBigInteger
This is working fine for TLS because we have a big enough inline
capacity, but in theory we could have crashed at any time even with
our 512 words of inline capacity.
2021-05-13 19:18:07 +01:00
Ali Mohammad Pur
abda20e25e LibCrypto: Enable -Wvla for LibCrypto
Resolves part of #7071.
2021-05-13 17:53:32 +02:00
Ali Mohammad Pur
b05beb79d4 LibCrypto: Remove all uses of VLAs
This removes all uses of VLAs with either Vectors with inline capacity
for the expected soft upper bound, or the occasional heap allocation.
2021-05-13 17:53:32 +02:00
Andreas Kling
05dbf3891a LibCrypto: Fix a mistake in appendff() conversion 2021-05-07 21:57:29 +02:00
Andreas Kling
e76956f712 LibCrypto: Convert StringBuilder::appendf() => AK::Format 2021-05-07 21:12:09 +02:00
Gunnar Beutner
6cf59b6ae9 Everywhere: Turn #if *_DEBUG into dbgln_if/if constexpr 2021-05-01 21:25:06 +02:00
Andreas Kling
3d4afe7614 Everywhere: "indexes" => "indices"
I've wasted a silly amount of time in the past fretting over which
of these words to use. Let's just choose one and use it everywhere. :^)
2021-04-29 22:23:52 +02:00
Linus Groh
649d2faeab Everywhere: Use "the SerenityOS developers." in copyright headers
We had some inconsistencies before:

- Sometimes "The", sometimes "the"
- Sometimes trailing ".", sometimes no trailing "."

I picked the most common one (lowecase "the", trailing ".") and applied
it to all copyright headers.

By using the exact same string everywhere we can ensure nothing gets
missed during a global search (and replace), and that these
inconsistencies are not spread any further (as copyright headers are
commonly copied to new files).
2021-04-29 00:59:26 +02:00
Linus Groh
dbe72fd962 Everywhere: Remove empty line after function body opening curly brace 2021-04-25 20:20:00 +02:00
Ali Mohammad Pur
38418fdfdf AK+Userland: Use mpfard@serenityos.org for my copyright headers 2021-04-22 22:19:09 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Lenny Maiorani
ece8aeaaf4 Everywhere: Remove redundant inline keyword with constexpr
Problem:
- `constexpr` functions are additionally decorated with `inline`
  keyword. This is redundant since `constexpr` implies `inline`.

Solution:
- Remove redundancies.
2021-04-21 20:49:49 +02:00
AnotherTest
ae49171755 LibCrypto: Avoid creating bools from anything except bools 2021-04-18 14:18:16 +02:00
AnotherTest
13abbc5ea8 LibCrypto: Implement UTCTime and GeneralizedTime parsers 2021-04-18 14:18:16 +02:00
AnotherTest
ed28008d78 LibCrypo: Add an ASN.1/DER pretty-printer
It's much easier to debug things when we can actually *see* them :P
2021-04-18 14:18:16 +02:00