Commit graph

49 commits

Author SHA1 Message Date
Peter Elliott 2e8cfe5435 LibCrypto: Add CTR cipher mode
Kernel: Changed fortuna implementation to use CTR mode instead of
manually implementing a counter.
2020-06-25 21:05:40 +02:00
Peter Elliott 3de32f0a55 LibCrypto: Add missing <AK/StringView.h> include 2020-06-25 21:05:40 +02:00
AnotherTest 02c53fd1f9 LibCrypto: Add bitwise operations (and/or/xor) 2020-06-07 19:29:40 +02:00
Linus Groh fbb1d9afe5 LibCrypto: Make ModularFunctions.h compile as part of Lagom
Compiler was complaining about two ambiguous overloads.

Also make some functions "inline" to fix warnings about unused
functions.
2020-06-07 19:29:40 +02:00
Linus Groh 75b4cc13a0 LibCrypto: Fix to_base10() for zero-value BigIntegers
All the magic is happening in a "while != 0" loop, so we ended up with
an empty string for zero-value BigIntegers. Now we just check that
upfront and return early.
2020-06-07 19:29:40 +02:00
AnotherTest d8208fd37c LibCrypto: Add a simple SignedBigInteger
This patchset adds a simple SignedBigInteger that is entirely defined in
terms of UnsignedBigInteger.

It also adds a NumberTheory::Power function, which is terribly
inefficient, but since the use of exponentiation is very much
discouraged for large inputs, no particular attempts were made
to make it more performant.
2020-06-05 13:29:44 +02:00
AnotherTest 63cc2f58ea LibCrypto: Correctly pad blocks with FinalBlockSize < size < BlockSize
This fixes #2488
2020-06-04 15:58:04 +02:00
AnotherTest 2a241a11bb LibCrypto: Fix some issues preventing compilation w/ clang 10 2020-05-29 20:17:12 +02:00
AnotherTest d497521d2b LibCrypto: Format all files with clang-format 10 2020-05-29 20:17:12 +02:00
Emanuele Torre 937d0be762 Meta: Add a script check the presence of "#pragma once" in header files
.. and make travis run it.

I renamed check-license-headers.sh to check-style.sh and expanded it so
that it now also checks for the presence of "#pragma once" in .h files.

It also checks the presence of a (single) blank line above and below the
"#pragma once" line.

I also added "#pragma once" to all the files that need it: even the ones
we are not check.
I also added/removed blank lines in order to make the script not fail.

I also ran clang-format on the files I modified.
2020-05-29 07:59:45 +02:00
Andreas Kling c1dd67e792 LibCrypto+LibTLS: Use AK/Random.h
This makes it possible to build both of these on Linux.
2020-05-27 12:28:17 +02:00
Andreas Kling b0eca4023f LibCrypto: Put some debug spam behind CRYPTO_DEBUG 2020-05-26 23:46:17 +02:00
Sergey Bugaev 450a2a0f9c Build: Switch to CMake :^)
Closes https://github.com/SerenityOS/serenity/issues/2080
2020-05-14 20:15:18 +02:00
Andreas Kling 444b6c8407 LibCrypto: Cache the "trimmed length" of UnsignedBigIntegers
This avoids repeated traversals of the underlying words and gives a
30% speed-up on "test-crypto -t pk" :^)
2020-05-07 12:23:09 +02:00
Andreas Kling e6922c0d40 LibCrypto: Manage vector sizing manually in performance-critical places
Use Vector::resize_and_keep_capacity() to resize BigInt vectors to just
the right size without risking deallocation. Then do direct indexed
accesses to the underlying words (or use memset/memcpy.)

This gives a ~40% speed-up on the RSA tests in "test-crypto -t pk" :^)
2020-05-03 19:14:50 +02:00
DexesTTP 8aeccf4f02 LibCrypto: Added BigInteger 'division by u16' operator 2020-05-03 14:31:26 +02:00
DexesTTP 0efd58bf6d LibCrypto: Changed ModularFunctions to use non-allocating operations
This change leads to between 10% and 35% performance improvement when executing
the RSA decryption method.

The main impact is to drastically reduce the number of allocations done in this
method from around 50% of the profile hits to less than 2%.
2020-05-03 14:31:26 +02:00
DexesTTP 28ea347e55 LibCrypto: Added static non-allocating UnsignedBigInteger operators
This changes the plus, minus, etc... operators from UnsignedBigInteger to use a
static helper method. The static methods do not allocate any variables, instead
all the required BigInteger output and temporary variables are required on call
as parameters.

This change already optimizes the number of allocations in complex operations
such as multiply or divide, by having a single allocation per call (instead of
one per loop).

This new API also provides a way to limit the number of allocations for complex
computations in other parts of the code. This is done by using these helpers in
any place that currently makes use of the standard operators.
2020-05-03 14:31:26 +02:00
DexesTTP d008a38f93 LibCrypto: Small fixes in BigInteger & test-crypto 2020-05-03 14:31:26 +02:00
AnotherTest 3b432eed98 LibCrypto: Correct RFC5246 un-padding behaviour
The decrypted data is legally allowed to have any amount of padding, so
long as it is block-aligned, we should not assume that padding bytes
fall inside the same block, or that an entire block cannot be padding.

Fixes #2072
2020-05-03 11:46:40 +02:00
AnotherTest c9321b4f00 LibCrypto: Make UnsignedBigInteger as fast as architecturally possible
This commit attempts to make UnsignedBigInteger as fast as possible
without changing the underlaying architecture.
This effort involves
- Preallocating space for vector operations
- Avoiding calls to computationally expensive functions
- Inlining or flattening functions (sensibly)
2020-05-02 12:24:10 +02:00
AnotherTest 4d932ce701 LibCrypto: Tweak ::prune_padding() to be more intuitive with loop bounds 2020-05-02 12:24:10 +02:00
AnotherTest adab43987d LibCrypto: Rename UnsignedBigInteger APIs to match their actions 2020-05-02 12:24:10 +02:00
AnotherTest e366416d51 LibCrypto: Preallocate capacity and cache trimmed_length() in UnsignedBigInteger 2020-05-02 12:24:10 +02:00
AnotherTest bb46e5f608 LibTLS: Switch to Hash::Manager for hashing and add SHA1
Now we can talk to google.com
2020-05-02 12:24:10 +02:00
AnotherTest 43a49f5fff LibCrypto: Add a Hash::Manager that can act as any one of the hashes 2020-05-02 12:24:10 +02:00
AnotherTest e997661e26 LibCrypto: Implement SHA1 Hash Function 2020-05-02 12:24:10 +02:00
AnotherTest e015ffd5f0 LibCrypto: Ensure that EME padding does not contain zeros
With this fix, we can now reliably open TLS connections!
2020-05-02 12:24:10 +02:00
AnotherTest 05e2c7d9cf LibCrypto+LibTLS: Reformat everything
I have no idea how I'll squash _this_ one...
2020-05-02 12:24:10 +02:00
AnotherTest a1e1570552 LibCrypto+LibTLS: Generalise the use of IV length
This is in preparation for the upcoming Galois/Counter mode, which
conventionally has 12 bytes of IV as opposed to CBC's 16 bytes.

...Also fixes a lot of style issues, since the author finally found the
project's clang config file in the repository root :^)
2020-05-02 12:24:10 +02:00
AnotherTest 8d20a526e5 LibCrypto: Preallocate 128 words of space for UnsignedBigInteger
This shaves off 1 second of runtime
2020-05-02 12:24:10 +02:00
AnotherTest 2247036acf LibTLS: Implement a preliminary version of the TLS protocol
TLS::TLSv12 is a Core::Socket, however, I think splitting that into a
TLS::Socket would probably be beneficial
2020-05-02 12:24:10 +02:00
AnotherTest f1578d7e9e LibCrypto: Fix issues in the Crypto stack
This commit fixes up the following:
- HMAC should not reuse a single hasher when successively updating
- AES Key should not assume its user key is valid signed char*
- Mode should have a virtual destructor
And adds a RFC5246 padding mode, which is required for TLS
2020-05-02 12:24:10 +02:00
AnotherTest 7adb93ede9 LibCrypto: Implement RSA in terms of UnsignedBigInteger
This commit also adds enough ASN.1/DER to parse RSA keys
2020-05-02 12:24:10 +02:00
AnotherTest 6b742c69bd LibCrypto: Add ::import_data() and ::export_data() to UnsignedBigInteger
These functions allow conversion to-and-from big-endian buffers
This commit also adds a ""_bigint operator for easy bigint use
2020-05-02 12:24:10 +02:00
Itamar c52d3e65b9 LibCrypto: Cleanup UnsignedBigInteger a bit
- Add missing 'explicit' to the constructor
- Remove unneeded 'AK::' in AK::Vector
- Avoid copying 'words' in constructor
2020-05-02 12:24:10 +02:00
Itamar 2125a4debb LibCrypto: Add base-10 string de/serialization methods for bigint 2020-05-02 12:24:10 +02:00
Itamar 709c691f38 LibCrypto: Fix bug in big int subtraction
A regression test was added to the suite.

This commit also generally simplifies the subtraction method.
2020-05-02 12:24:10 +02:00
Itamar 0d2777752e LibCrypto: Add UnsignedBigInteger division
The division operation returns both the quotient and the remainder.
2020-05-02 12:24:10 +02:00
Itamar 2959c4a5e9 LibCrypto: Add UnsignedBigInteger multiplication
Also added documentation for the runtime complexity of some operations.
2020-05-02 12:24:10 +02:00
Itamar 2843dce498 LibCrypto: Fix a bug in big int addition
There was a bug when dealing with a carry when the addition
result for the current word was UINT32_MAX.

This commit also adds a regression test for the bug.
2020-05-02 12:24:10 +02:00
Itamar e0cf40518c LibCrypto: Add UnsignedBigInteger subtraction and comparison 2020-05-02 12:24:10 +02:00
Itamar 6201f741d4 LibCrypto: Add UnsignedBigInteger and implement addition
UnsignedBigInteger stores an unsigned ainteger of arbitrary length.

A big integer is represented  as a vector of word. Each
word is an unsigned int.
2020-05-02 12:24:10 +02:00
AnotherTest 8c645916b4 LibCrypto: Add SHA512
There is quite a bit of avoidable duplication, however, I could not get
the compiler to be happy about SHA2<Size> (see FIXMEs)
2020-05-02 12:24:10 +02:00
AnotherTest ca097b093b LibCrypto: Add SHA256 hash function 2020-05-02 12:24:10 +02:00
AnotherTest f2cd004d11 LibCrypto: Implement HMAC 2020-05-02 12:24:10 +02:00
AnotherTest 4f89a377a4 LibCrypto: Move each subsection into its own namespace 2020-05-02 12:24:10 +02:00
AnotherTest bffb2c7542 LibCrypto: Add HashFunction and implement MD5 2020-05-02 12:24:10 +02:00
AnotherTest 899ca245ae LibCrypto: Implement Cipher and AES_CBC
Also adds a test program to userland
2020-05-02 12:24:10 +02:00