summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Tests/LibCrypto/TestCurves.cpp86
-rw-r--r--Userland/Libraries/LibCrypto/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibCrypto/Curves/X448.cpp356
-rw-r--r--Userland/Libraries/LibCrypto/Curves/X448.h40
4 files changed, 483 insertions, 0 deletions
diff --git a/Tests/LibCrypto/TestCurves.cpp b/Tests/LibCrypto/TestCurves.cpp
index 6356f3f0e7..4017bba52b 100644
--- a/Tests/LibCrypto/TestCurves.cpp
+++ b/Tests/LibCrypto/TestCurves.cpp
@@ -6,6 +6,7 @@
#include <AK/ByteBuffer.h>
#include <LibCrypto/Curves/X25519.h>
+#include <LibCrypto/Curves/X448.h>
#include <LibTest/TestCase.h>
TEST_CASE(test_x25519)
@@ -74,3 +75,88 @@ TEST_CASE(test_x25519)
EXPECT_EQ(shared_alice, shared_bob);
}
+
+TEST_CASE(test_x448)
+{
+ // https://datatracker.ietf.org/doc/html/rfc7748#section-6.1
+ u8 alice_private_key_data[56] {
+ 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57,
+ 0x75, 0xcf, 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4,
+ 0xee, 0x9e, 0xe8, 0xba, 0xe8, 0xbc, 0x55, 0x65,
+ 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9, 0xba, 0xf5,
+ 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91,
+ 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d,
+ 0x9a, 0xc2, 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b
+ };
+
+ u8 alice_public_key_data[56] {
+ 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6,
+ 0x7d, 0x22, 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a,
+ 0x27, 0x3b, 0xd2, 0xb8, 0x3d, 0xe0,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x74, 0xd1,
+ 0x2d, 0xa2, 0x32, 0xce, 0x40, 0xa7, 0x75, 0x52,
+ 0x28, 0x1d, 0x28, 0x2b, 0xb6, 0x0c, 0x0b, 0x56,
+ 0xfd, 0x24, 0x64, 0xc3, 0x35, 0x54, 0x39, 0x36,
+ 0x52, 0x1c, 0x24, 0x40, 0x30, 0x85, 0xd5, 0x9a,
+ 0x44, 0x9a, 0x50, 0x37, 0x51, 0x4a, 0x87, 0x9d
+ };
+
+ u8 coordinate_data[56] {
+ 0x05, 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,
+ 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, 56 };
+ ReadonlyBytes alice_public_key { alice_public_key_data, 56 };
+ ReadonlyBytes alice_private_key { alice_private_key_data, 56 };
+ ReadonlyBytes bob_public_key { bob_public_key_data, 56 };
+ ReadonlyBytes bob_private_key { bob_private_key_data, 56 };
+ ReadonlyBytes shared_secret { shared_secret_data, 56 };
+
+ auto generated_alice_public = MUST(Crypto::Curves::X448::compute_coordinate(alice_private_key, coordinate));
+ EXPECT_EQ(alice_public_key, generated_alice_public);
+
+ auto generated_bob_public = MUST(Crypto::Curves::X448::compute_coordinate(bob_private_key, coordinate));
+ EXPECT_EQ(bob_public_key, generated_bob_public);
+
+ auto shared_alice = MUST(Crypto::Curves::X448::compute_coordinate(alice_private_key, bob_public_key));
+ EXPECT_EQ(shared_alice, shared_secret);
+
+ auto shared_bob = MUST(Crypto::Curves::X448::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 e40e8b3dd4..ec703e4623 100644
--- a/Userland/Libraries/LibCrypto/CMakeLists.txt
+++ b/Userland/Libraries/LibCrypto/CMakeLists.txt
@@ -18,6 +18,7 @@ set(SOURCES
Checksum/CRC32.cpp
Cipher/AES.cpp
Curves/X25519.cpp
+ Curves/X448.cpp
Hash/MD5.cpp
Hash/SHA1.cpp
Hash/SHA2.cpp
diff --git a/Userland/Libraries/LibCrypto/Curves/X448.cpp b/Userland/Libraries/LibCrypto/Curves/X448.cpp
new file mode 100644
index 0000000000..a590141bfa
--- /dev/null
+++ b/Userland/Libraries/LibCrypto/Curves/X448.cpp
@@ -0,0 +1,356 @@
+/*
+ * Copyright (c) 2022, stelar7 <dudedbz@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ByteReader.h>
+#include <AK/Endian.h>
+#include <LibCrypto/Curves/X448.h>
+
+namespace Crypto::Curves {
+
+void X448::import_state(u32* state, ReadonlyBytes data)
+{
+ for (auto i = 0; i < X448::WORDS; i++) {
+ u32 value = ByteReader::load32(data.offset_pointer(sizeof(u32) * i));
+ state[i] = AK::convert_between_host_and_little_endian(value);
+ }
+}
+
+ErrorOr<ByteBuffer> X448::export_state(u32* data)
+{
+ auto buffer = TRY(ByteBuffer::create_uninitialized(X448::BYTES));
+
+ for (auto i = 0; i < X448::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 X448::select(u32* state, u32* a, u32* b, u32 condition)
+{
+ // If B < (2^448 - 2^224 + 1) then R = B, else R = A
+ u32 mask = condition - 1;
+
+ for (auto i = 0; i < X448::WORDS; i++) {
+ state[i] = (a[i] & mask) | (b[i] & ~mask);
+ }
+}
+
+void X448::set(u32* state, u32 value)
+{
+ state[0] = value;
+
+ for (auto i = 1; i < X448::WORDS; i++) {
+ state[i] = 0;
+ }
+}
+
+void X448::copy(u32* state, u32* value)
+{
+ for (auto i = 0; i < X448::WORDS; i++) {
+ state[i] = value[i];
+ }
+}
+
+void X448::conditional_swap(u32* first, u32* second, u32 condition)
+{
+ u32 mask = ~condition + 1;
+ for (auto i = 0; i < X448::WORDS; i++) {
+ u32 temp = mask & (first[i] ^ second[i]);
+ first[i] ^= temp;
+ second[i] ^= temp;
+ }
+}
+
+void X448::modular_multiply_single(u32* state, u32* first, u32 second)
+{
+ // Compute R = (A * B) mod p
+ u64 temp = 0;
+ u64 carry = 0;
+ u32 output[X448::WORDS];
+
+ for (auto i = 0; i < X448::WORDS; i++) {
+ temp += (u64)first[i] * second;
+ output[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ // Fast modular reduction
+ carry = temp;
+ for (auto i = 0; i < X448::WORDS / 2; i++) {
+ temp += output[i];
+ output[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ temp += carry;
+ for (auto i = X448::WORDS / 2; i < X448::WORDS; i++) {
+ temp += output[i];
+ output[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ modular_reduce(state, output, (u32)temp);
+}
+
+void X448::modular_square(u32* state, u32* value)
+{
+ // Compute R = (A ^ 2) mod p
+ modular_multiply(state, value, value);
+}
+
+void X448::modular_multiply(u32* state, u32* first, u32* second)
+{
+ // Compute R = (A * B) mod p
+
+ u64 temp = 0;
+ u64 carry = 0;
+ u32 output[X448::WORDS * 2];
+
+ // Comba's method
+ for (auto i = 0; i < X448::WORDS * 2; i++) {
+ if (i < 14) {
+ for (auto j = 0; j <= i; j++) {
+ temp += (u64)first[j] * second[i - j];
+ carry += temp >> 32;
+ temp &= 0xFFFFFFFF;
+ }
+ } else {
+ for (auto j = i - 13; j < X448::WORDS; j++) {
+ temp += (u64)first[j] * second[i - j];
+ carry += temp >> 32;
+ temp &= 0xFFFFFFFF;
+ }
+ }
+
+ output[i] = temp & 0xFFFFFFFF;
+ temp = carry & 0xFFFFFFFF;
+ carry >>= 32;
+ }
+
+ // Fast modular reduction (first pass)
+ temp = 0;
+ for (auto i = 0; i < X448::WORDS / 2; i++) {
+ temp += output[i];
+ temp += output[i + 14];
+ temp += output[i + 21];
+ output[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ for (auto i = X448::WORDS / 2; i < X448::WORDS; i++) {
+ temp += output[i];
+ temp += output[i + 7];
+ temp += output[i + 14];
+ temp += output[i + 14];
+ output[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ // Fast modular reduction (second pass)
+ carry = temp;
+ for (auto i = 0; i < X448::WORDS / 2; i++) {
+ temp += output[i];
+ output[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ temp += carry;
+ for (auto i = X448::WORDS / 2; i < X448::WORDS; i++) {
+ temp += output[i];
+ output[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ modular_reduce(state, output, (u32)temp);
+}
+
+void X448::modular_add(u32* state, u32* first, u32* second)
+{
+ u64 temp = 0;
+
+ // Compute R = A + B
+ for (auto i = 0; i < X448::WORDS; i++) {
+ temp += first[i];
+ temp += second[i];
+ state[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ modular_reduce(state, state, (u32)temp);
+}
+
+void X448::modular_subtract(u32* state, u32* first, u32* second)
+{
+ i64 temp = -1;
+
+ // Compute R = A + (2^448 - 2^224 - 1) - B
+ for (auto i = 0; i < 7; i++) {
+ temp += first[i];
+ temp -= second[i];
+ state[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ temp -= 1;
+
+ for (auto i = 7; i < 14; i++) {
+ temp += first[i];
+ temp -= second[i];
+ state[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ temp += 1;
+
+ modular_reduce(state, state, (u32)temp);
+}
+
+void X448::modular_reduce(u32* state, u32* data, u32 a_high)
+{
+ u64 temp = 1;
+ u32 other[X448::WORDS];
+
+ // Compute B = A - (2^448 - 2^224 - 1)
+ for (auto i = 0; i < X448::WORDS / 2; i++) {
+ temp += data[i];
+ other[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ temp += 1;
+
+ for (auto i = 7; i < X448::WORDS; i++) {
+ temp += data[i];
+ other[i] = temp & 0xFFFFFFFF;
+ temp >>= 32;
+ }
+
+ auto condition = (a_high + (u32)temp - 1) & 1;
+ select(state, other, data, condition);
+}
+
+void X448::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 X448::modular_multiply_inverse(u32* state, u32* value)
+{
+ // Compute R = A^-1 mod p
+ u32 u[X448::WORDS];
+ u32 v[X448::WORDS];
+
+ 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(v, u, v);
+ to_power_of_2n(u, v, 6);
+ modular_multiply(u, u, v);
+ modular_square(u, u);
+ modular_multiply(v, u, value);
+ to_power_of_2n(u, v, 13);
+ modular_multiply(u, u, v);
+ modular_square(u, u);
+ modular_multiply(v, u, value);
+ to_power_of_2n(u, v, 27);
+ modular_multiply(u, u, v);
+ modular_square(u, u);
+ modular_multiply(v, u, value);
+ to_power_of_2n(u, v, 55);
+ modular_multiply(u, u, v);
+ modular_square(u, u);
+ modular_multiply(v, u, value);
+ to_power_of_2n(u, v, 111);
+ modular_multiply(v, u, v);
+ modular_square(u, v);
+ modular_multiply(u, u, value);
+ to_power_of_2n(u, u, 223);
+ modular_multiply(u, u, v);
+ modular_square(u, u);
+ modular_square(u, u);
+ modular_multiply(state, u, value);
+}
+
+// https://datatracker.ietf.org/doc/html/rfc7748#section-5
+ErrorOr<ByteBuffer> X448::compute_coordinate(ReadonlyBytes input_k, ReadonlyBytes input_u)
+{
+ u32 k[X448::WORDS] {};
+ u32 u[X448::WORDS] {};
+ u32 x1[X448::WORDS] {};
+ u32 x2[X448::WORDS] {};
+ u32 z1[X448::WORDS] {};
+ u32 z2[X448::WORDS] {};
+ u32 t1[X448::WORDS] {};
+ u32 t2[X448::WORDS] {};
+
+ // Copy input to internal state
+ import_state(k, input_k);
+
+ // Set the two least significant bits of the first byte to 0, and the most significant bit of the last byte to 1
+ k[0] &= 0xFFFFFFFC;
+ k[13] |= 0x80000000;
+
+ // Copy coordinate to internal state
+ import_state(u, input_u);
+
+ // Implementations MUST accept non-canonical values and process them as
+ // if they had been reduced modulo the field prime.
+ modular_reduce(u, u, 0);
+
+ set(x1, 1);
+ set(z1, 0);
+ copy(x2, u);
+ set(z2, 1);
+
+ // Montgomery ladder
+ u32 swap = 0;
+ for (auto i = X448::BITS - 1; i >= 0; i--) {
+ u32 b = (k[i / 32] >> (i % 32)) & 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/X448.h b/Userland/Libraries/LibCrypto/Curves/X448.h
new file mode 100644
index 0000000000..b2c9a1f8ac
--- /dev/null
+++ b/Userland/Libraries/LibCrypto/Curves/X448.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 X448 {
+
+ static constexpr u16 BITS = 448;
+ static constexpr u8 BYTES = 56;
+ static constexpr u8 WORDS = 14;
+ static constexpr u32 A24 = 39082;
+
+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, u32 data_high);
+ static void to_power_of_2n(u32* state, u32* value, u8 n);
+ static void modular_multiply_inverse(u32* state, u32* value);
+};
+
+}