diff options
-rw-r--r-- | Tests/LibCrypto/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Tests/LibCrypto/TestCurves.cpp | 76 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/Curves/X25519.cpp | 337 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/Curves/X25519.h | 40 |
5 files changed, 455 insertions, 0 deletions
diff --git a/Tests/LibCrypto/CMakeLists.txt b/Tests/LibCrypto/CMakeLists.txt index cc14627b0a..5a7bdd7bea 100644 --- a/Tests/LibCrypto/CMakeLists.txt +++ b/Tests/LibCrypto/CMakeLists.txt @@ -2,6 +2,7 @@ set(TEST_SOURCES TestAES.cpp TestBigInteger.cpp TestChecksum.cpp + TestCurves.cpp TestHash.cpp TestHMAC.cpp TestRSA.cpp diff --git a/Tests/LibCrypto/TestCurves.cpp b/Tests/LibCrypto/TestCurves.cpp new file mode 100644 index 0000000000..6356f3f0e7 --- /dev/null +++ b/Tests/LibCrypto/TestCurves.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2022, stelar7 <dudedbz@gmail.com> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/ByteBuffer.h> +#include <LibCrypto/Curves/X25519.h> +#include <LibTest/TestCase.h> + +TEST_CASE(test_x25519) +{ + // https://datatracker.ietf.org/doc/html/rfc7748#section-6.1 + u8 alice_private_key_data[32] { + 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, + 0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, + 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a, + 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a + }; + + u8 alice_public_key_data[32] { + 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, + 0x74, 0x8b, 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, + 0x0d, 0xbf, 0x3a, 0x0d, 0x26, 0x38, 0x1a, 0xf4, + 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, 0x4e, 0x6a + }; + + u8 bob_private_key_data[32] { + 0x5d, 0xab, 0x08, 0x7e, 0x62, 0x4a, 0x8a, 0x4b, + 0x79, 0xe1, 0x7f, 0x8b, 0x83, 0x80, 0x0e, 0xe6, + 0x6f, 0x3b, 0xb1, 0x29, 0x26, 0x18, 0xb6, 0xfd, + 0x1c, 0x2f, 0x8b, 0x27, 0xff, 0x88, 0xe0, 0xeb + }; + + u8 bob_public_key_data[32] { + 0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4, + 0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37, + 0x3f, 0x83, 0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d, + 0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f + }; + + u8 shared_secret_data[32] { + 0x4a, 0x5d, 0x9d, 0x5b, 0xa4, 0xce, 0x2d, 0xe1, + 0x72, 0x8e, 0x3b, 0xf4, 0x80, 0x35, 0x0f, 0x25, + 0xe0, 0x7e, 0x21, 0xc9, 0x47, 0xd1, 0x9e, 0x33, + 0x76, 0xf0, 0x9b, 0x3c, 0x1e, 0x16, 0x17, 0x42 + }; + + u8 coordinate_data[32] { + 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + ReadonlyBytes coordinate { coordinate_data, 32 }; + ReadonlyBytes alice_public_key { alice_public_key_data, 32 }; + ReadonlyBytes alice_private_key { alice_private_key_data, 32 }; + ReadonlyBytes bob_public_key { bob_public_key_data, 32 }; + ReadonlyBytes bob_private_key { bob_private_key_data, 32 }; + ReadonlyBytes shared_secret { shared_secret_data, 32 }; + + auto generated_alice_public = MUST(Crypto::Curves::X25519::compute_coordinate(alice_private_key, coordinate)); + EXPECT_EQ(alice_public_key, generated_alice_public); + + auto generated_bob_public = MUST(Crypto::Curves::X25519::compute_coordinate(bob_private_key, coordinate)); + EXPECT_EQ(bob_public_key, generated_bob_public); + + auto shared_alice = MUST(Crypto::Curves::X25519::compute_coordinate(alice_private_key, bob_public_key)); + EXPECT_EQ(shared_alice, shared_secret); + + auto shared_bob = MUST(Crypto::Curves::X25519::compute_coordinate(bob_private_key, alice_public_key)); + EXPECT_EQ(shared_bob, shared_secret); + + EXPECT_EQ(shared_alice, shared_bob); +} diff --git a/Userland/Libraries/LibCrypto/CMakeLists.txt b/Userland/Libraries/LibCrypto/CMakeLists.txt index 19f24aef09..e40e8b3dd4 100644 --- a/Userland/Libraries/LibCrypto/CMakeLists.txt +++ b/Userland/Libraries/LibCrypto/CMakeLists.txt @@ -17,6 +17,7 @@ set(SOURCES Checksum/Adler32.cpp Checksum/CRC32.cpp Cipher/AES.cpp + Curves/X25519.cpp Hash/MD5.cpp Hash/SHA1.cpp Hash/SHA2.cpp diff --git a/Userland/Libraries/LibCrypto/Curves/X25519.cpp b/Userland/Libraries/LibCrypto/Curves/X25519.cpp new file mode 100644 index 0000000000..60240952f4 --- /dev/null +++ b/Userland/Libraries/LibCrypto/Curves/X25519.cpp @@ -0,0 +1,337 @@ +/* + * Copyright (c) 2022, stelar7 <dudedbz@gmail.com> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/ByteReader.h> +#include <AK/Endian.h> +#include <LibCrypto/Curves/X25519.h> + +namespace Crypto::Curves { + +void X25519::import_state(u32* state, ReadonlyBytes data) +{ + for (auto i = 0; i < X25519::WORDS; i++) { + u32 value = ByteReader::load32(data.offset_pointer(sizeof(u32) * i)); + state[i] = AK::convert_between_host_and_little_endian(value); + } +} + +ErrorOr<ByteBuffer> X25519::export_state(u32* data) +{ + auto buffer = TRY(ByteBuffer::create_uninitialized(X25519::BYTES)); + + for (auto i = 0; i < X25519::WORDS; i++) { + u32 value = AK::convert_between_host_and_little_endian(data[i]); + ByteReader::store(buffer.offset_pointer(sizeof(u32) * i), value); + } + + return buffer; +} + +void X25519::select(u32* state, u32* a, u32* b, u32 condition) +{ + // If B < (2^255 - 19) then R = B, else R = A + u32 mask = condition - 1; + + for (auto i = 0; i < X25519::WORDS; i++) { + state[i] = (a[i] & mask) | (b[i] & ~mask); + } +} + +void X25519::set(u32* state, u32 value) +{ + state[0] = value; + + for (auto i = 1; i < X25519::WORDS; i++) { + state[i] = 0; + } +} + +void X25519::copy(u32* state, u32* value) +{ + for (auto i = 0; i < X25519::WORDS; i++) { + state[i] = value[i]; + } +} + +void X25519::conditional_swap(u32* first, u32* second, u32 condition) +{ + u32 mask = ~condition + 1; + for (auto i = 0; i < X25519::WORDS; i++) { + u32 temp = mask & (first[i] ^ second[i]); + first[i] ^= temp; + second[i] ^= temp; + } +} + +void X25519::modular_multiply_single(u32* state, u32* first, u32 second) +{ + // Compute R = (A * B) mod p + u64 temp = 0; + u32 output[X25519::WORDS]; + + for (auto i = 0; i < X25519::WORDS; i++) { + temp += (u64)first[i] * second; + output[i] = temp & 0xFFFFFFFF; + temp >>= 32; + } + + // Reduce bit 256 (2^256 = 38 mod p) + temp *= 38; + // Reduce bit 255 (2^255 = 19 mod p) + temp += (output[7] >> 31) * 19; + // Mask the most significant bit + output[7] &= 0x7FFFFFFF; + + // Fast modular reduction + for (auto i = 0; i < X25519::WORDS; i++) { + temp += output[i]; + output[i] = temp & 0xFFFFFFFF; + temp >>= 32; + } + + modular_reduce(state, output); +} + +void X25519::modular_square(u32* state, u32* value) +{ + // Compute R = (A ^ 2) mod p + modular_multiply(state, value, value); +} + +void X25519::modular_multiply(u32* state, u32* first, u32* second) +{ + // Compute R = (A * B) mod p + u64 temp = 0; + u64 carry = 0; + u32 output[X25519::WORDS * 2]; + + // Comba's method + for (auto i = 0; i < 16; i++) { + if (i < X25519::WORDS) { + for (auto j = 0; j <= i; j++) { + temp += (u64)first[j] * second[i - j]; + carry += temp >> 32; + temp &= 0xFFFFFFFF; + } + } else { + for (auto j = i - 7; j < X25519::WORDS; j++) { + temp += (u64)first[j] * second[i - j]; + carry += temp >> 32; + temp &= 0xFFFFFFFF; + } + } + + output[i] = temp & 0xFFFFFFFF; + temp = carry & 0xFFFFFFFF; + carry >>= 32; + } + + // Reduce bit 255 (2^255 = 19 mod p) + temp = (output[7] >> 31) * 19; + // Mask the most significant bit + output[7] &= 0x7FFFFFFF; + + // Fast modular reduction 1st pass + for (auto i = 0; i < X25519::WORDS; i++) { + temp += output[i]; + temp += (u64)output[i + 8] * 38; + output[i] = temp & 0xFFFFFFFF; + temp >>= 32; + } + + // Reduce bit 256 (2^256 = 38 mod p) + temp *= 38; + // Reduce bit 255 (2^255 = 19 mod p) + temp += (output[7] >> 31) * 19; + // Mask the most significant bit + output[7] &= 0x7FFFFFFF; + + // Fast modular reduction 2nd pass + for (auto i = 0; i < X25519::WORDS; i++) { + temp += output[i]; + output[i] = temp & 0xFFFFFFFF; + temp >>= 32; + } + + modular_reduce(state, output); +} + +void X25519::modular_add(u32* state, u32* first, u32* second) +{ + // R = (A + B) mod p + u64 temp = 0; + for (auto i = 0; i < X25519::WORDS; i++) { + temp += first[i]; + temp += second[i]; + state[i] = temp & 0xFFFFFFFF; + temp >>= 32; + } + + modular_reduce(state, state); +} + +void X25519::modular_subtract(u32* state, u32* first, u32* second) +{ + // R = (A - B) mod p + i64 temp = -19; + for (auto i = 0; i < X25519::WORDS; i++) { + temp += first[i]; + temp -= second[i]; + state[i] = temp & 0xFFFFFFFF; + temp >>= 32; + } + + // Compute R = A + (2^255 - 19) - B + state[7] += 0x80000000; + + modular_reduce(state, state); +} + +void X25519::modular_reduce(u32* state, u32* data) +{ + // R = A mod p + u64 temp = 19; + u32 other[X25519::WORDS]; + + for (auto i = 0; i < X25519::WORDS; i++) { + temp += data[i]; + other[i] = temp & 0xFFFFFFFF; + temp >>= 32; + } + + // Compute B = A - (2^255 - 19) + other[7] -= 0x80000000; + + u32 mask = (other[7] & 0x80000000) >> 31; + select(state, other, data, mask); +} + +void X25519::to_power_of_2n(u32* state, u32* value, u8 n) +{ + // compute R = (A ^ (2^n)) mod p + modular_square(state, value); + for (auto i = 1; i < n; i++) { + modular_square(state, state); + } +} + +void X25519::modular_multiply_inverse(u32* state, u32* value) +{ + // Compute R = A^-1 mod p + u32 u[X25519::WORDS]; + u32 v[X25519::WORDS]; + + // Fermat's little theorem + modular_square(u, value); + modular_multiply(u, u, value); + modular_square(u, u); + modular_multiply(v, u, value); + to_power_of_2n(u, v, 3); + modular_multiply(u, u, v); + modular_square(u, u); + modular_multiply(v, u, value); + to_power_of_2n(u, v, 7); + modular_multiply(u, u, v); + modular_square(u, u); + modular_multiply(v, u, value); + to_power_of_2n(u, v, 15); + modular_multiply(u, u, v); + modular_square(u, u); + modular_multiply(v, u, value); + to_power_of_2n(u, v, 31); + modular_multiply(v, u, v); + to_power_of_2n(u, v, 62); + modular_multiply(u, u, v); + modular_square(u, u); + modular_multiply(v, u, value); + to_power_of_2n(u, v, 125); + modular_multiply(u, u, v); + modular_square(u, u); + modular_square(u, u); + modular_multiply(u, u, value); + modular_square(u, u); + modular_square(u, u); + modular_multiply(u, u, value); + modular_square(u, u); + modular_multiply(state, u, value); +} + +// https://datatracker.ietf.org/doc/html/rfc7748#section-5 +ErrorOr<ByteBuffer> X25519::compute_coordinate(ReadonlyBytes input_k, ReadonlyBytes input_u) +{ + u32 k[X25519::WORDS] {}; + u32 u[X25519::WORDS] {}; + u32 x1[X25519::WORDS] {}; + u32 x2[X25519::WORDS] {}; + u32 z1[X25519::WORDS] {}; + u32 z2[X25519::WORDS] {}; + u32 t1[X25519::WORDS] {}; + u32 t2[X25519::WORDS] {}; + + // Copy input to internal state + import_state(k, input_k); + + // Set the three least significant bits of the first byte and the most significant bit of the last to zero, + // set the second most significant bit of the last byte to 1 + k[0] &= 0xFFFFFFF8; + k[7] &= 0x7FFFFFFF; + k[7] |= 0x40000000; + + // Copy coordinate to internal state + import_state(u, input_u); + // mask the most significant bit in the final byte. + u[7] &= 0x7FFFFFFF; + + // Implementations MUST accept non-canonical values and process them as + // if they had been reduced modulo the field prime. + modular_reduce(u, u); + + set(x1, 1); + set(z1, 0); + copy(x2, u); + set(z2, 1); + + // Montgomery ladder + u32 swap = 0; + for (auto i = X25519::BITS - 1; i >= 0; i--) { + u32 b = (k[i / X25519::BYTES] >> (i % X25519::BYTES)) & 1; + + conditional_swap(x1, x2, swap ^ b); + conditional_swap(z1, z2, swap ^ b); + + swap = b; + + modular_add(t1, x2, z2); + modular_subtract(x2, x2, z2); + modular_add(z2, x1, z1); + modular_subtract(x1, x1, z1); + modular_multiply(t1, t1, x1); + modular_multiply(x2, x2, z2); + modular_square(z2, z2); + modular_square(x1, x1); + modular_subtract(t2, z2, x1); + modular_multiply_single(z1, t2, A24); + modular_add(z1, z1, x1); + modular_multiply(z1, z1, t2); + modular_multiply(x1, x1, z2); + modular_subtract(z2, t1, x2); + modular_square(z2, z2); + modular_multiply(z2, z2, u); + modular_add(x2, x2, t1); + modular_square(x2, x2); + } + + conditional_swap(x1, x2, swap); + conditional_swap(z1, z2, swap); + + // Retrieve affine representation + modular_multiply_inverse(u, z1); + modular_multiply(u, u, x1); + + // Encode state for export + return export_state(u); +} +} diff --git a/Userland/Libraries/LibCrypto/Curves/X25519.h b/Userland/Libraries/LibCrypto/Curves/X25519.h new file mode 100644 index 0000000000..b0342211d2 --- /dev/null +++ b/Userland/Libraries/LibCrypto/Curves/X25519.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022, stelar7 <dudedbz@gmail.com> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/ByteBuffer.h> + +namespace Crypto::Curves { + +class X25519 { + + static constexpr u8 BITS = 255; + static constexpr u8 BYTES = 32; + static constexpr u8 WORDS = 8; + static constexpr u32 A24 = 121666; + +public: + static ErrorOr<ByteBuffer> compute_coordinate(ReadonlyBytes a, ReadonlyBytes b); + +private: + static void import_state(u32* state, ReadonlyBytes data); + static ErrorOr<ByteBuffer> export_state(u32* data); + static void select(u32* state, u32* a, u32* b, u32 condition); + static void set(u32* state, u32 value); + static void copy(u32* state, u32* value); + static void conditional_swap(u32* first, u32* second, u32 condition); + static void modular_multiply_single(u32* state, u32* first, u32 second); + static void modular_square(u32* state, u32* value); + static void modular_multiply(u32* state, u32* first, u32* second); + static void modular_add(u32* state, u32* first, u32* second); + static void modular_subtract(u32* state, u32* first, u32* second); + static void modular_reduce(u32* state, u32* data); + static void to_power_of_2n(u32* state, u32* value, u8 n); + static void modular_multiply_inverse(u32* state, u32* value); +}; + +} |