Skip to content

Truly independent web browser

LibCrypto: Keep only the signature checks LibDNS needs

Shannon Booth6a0ce5267f78
diff --git a/Libraries/LibCrypto/Curves/EdwardsCurve.cpp b/Libraries/LibCrypto/Curves/EdwardsCurve.cpp
index 5e7ffe21839..004b1fbe5ba 100644
--- a/Libraries/LibCrypto/Curves/EdwardsCurve.cpp
+++ b/Libraries/LibCrypto/Curves/EdwardsCurve.cpp
@@ -8,78 +8,10 @@
#include <LibCrypto/Curves/EdwardsCurve.h>
#include <LibCrypto/OpenSSL.h>
-#include <openssl/core_names.h>
#include <openssl/evp.h>
namespace Crypto::Curves {
-char const* EdwardsCurve::curve_type_to_openssl_name(EdwardsCurveType curve_type)
-{
- switch (curve_type) {
- case EdwardsCurveType::Ed25519:
- return "ED25519";
- case EdwardsCurveType::Ed448:
- return "ED448";
- case EdwardsCurveType::X25519:
- return "X25519";
- case EdwardsCurveType::X448:
- return "X448";
- }
- VERIFY_NOT_REACHED();
-}
-
-ErrorOr<ByteBuffer> EdwardsCurve::generate_private_key()
-{
- auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_Q_keygen(nullptr, nullptr, curve_type_to_openssl_name(m_curve_type))));
-
- size_t key_size = 0;
- OPENSSL_TRY(EVP_PKEY_get_raw_private_key(key.ptr(), nullptr, &key_size));
-
- auto buf = TRY(ByteBuffer::create_uninitialized(key_size));
- OPENSSL_TRY(EVP_PKEY_get_raw_private_key(key.ptr(), buf.data(), &key_size));
-
- return buf;
-}
-
-ErrorOr<ByteBuffer> EdwardsCurve::generate_public_key(ReadonlyBytes private_key)
-{
- auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_private_key_ex(nullptr, curve_type_to_openssl_name(m_curve_type), nullptr, private_key.data(), private_key.size())));
-
- size_t key_size = 0;
- OPENSSL_TRY(EVP_PKEY_get_raw_public_key(key.ptr(), nullptr, &key_size));
-
- auto buf = TRY(ByteBuffer::create_uninitialized(key_size));
- OPENSSL_TRY(EVP_PKEY_get_raw_public_key(key.ptr(), buf.data(), &key_size));
-
- return buf;
-}
-
-ErrorOr<ByteBuffer> SignatureEdwardsCurve::sign(ReadonlyBytes private_key, ReadonlyBytes message, ReadonlyBytes context)
-{
- auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_private_key_ex(nullptr, curve_type_to_openssl_name(m_curve_type), nullptr, private_key.data(), private_key.size())));
-
- auto ctx = TRY(OpenSSL_MD_CTX::create());
-
- OSSL_PARAM params[2] = {
- OSSL_PARAM_END,
- OSSL_PARAM_END
- };
-
- if (!context.is_null()) {
- params[0] = OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, const_cast<u8*>(context.data()), context.size());
- }
-
- OPENSSL_TRY(EVP_DigestSignInit_ex(ctx.ptr(), nullptr, nullptr, nullptr, nullptr, key.ptr(), params));
-
- size_t sig_len = 0;
- OPENSSL_TRY(EVP_DigestSign(ctx.ptr(), nullptr, &sig_len, message.data(), message.size()));
-
- auto sig = TRY(ByteBuffer::create_uninitialized(sig_len));
- OPENSSL_TRY(EVP_DigestSign(ctx.ptr(), sig.data(), &sig_len, message.data(), message.size()));
-
- return sig;
-}
-
static bool is_small_order_ed25519_point(ReadonlyBytes public_key)
{
// Ed25519 public keys are 32 bytes
@@ -88,7 +20,7 @@ static bool is_small_order_ed25519_point(ReadonlyBytes public_key)
// Known small-order points for Ed25519 curve
// These points have order 1, 2, 4, or 8 and should be rejected for security
- static constexpr Array<Array<u8, 32>, 12> small_order_points = { { // Identity point (order 1)
+ static constexpr Array<Array<u8, 32>, 11> small_order_points = { { // Identity point (order 1)
{ 0x01, 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 },
// Point of order 2 (canonical)
@@ -115,10 +47,7 @@ static bool is_small_order_ed25519_point(ReadonlyBytes public_key)
{ 0xc7, 0x17, 0x6a, 0x70, 0x3d, 0x4d, 0xd8, 0x4f, 0xba, 0x3c, 0x0b, 0x76, 0x0d, 0x10, 0x67, 0x0f,
0x2a, 0x20, 0x53, 0xfa, 0x2c, 0x39, 0xcc, 0xc6, 0x4e, 0xc7, 0xfd, 0x77, 0x92, 0xac, 0x03, 0xfa }, // pubKeys[0]
{ 0xf7, 0xba, 0xde, 0xc5, 0xb8, 0xab, 0xea, 0xf6, 0x99, 0x58, 0x39, 0x92, 0x21, 0x9b, 0x7b, 0x22,
- 0x3f, 0x1d, 0xf3, 0xfb, 0xbe, 0xa9, 0x19, 0x84, 0x4e, 0x3f, 0x7c, 0x55, 0x4a, 0x43, 0xdd, 0x43 }, // pubKeys[1] (actually part of a signature, but used as small-order)
- // pubKeys[5] - same as pubKeys[4] but with different case, still the same small-order point
- { 0xEC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F } } };
+ 0x3f, 0x1d, 0xf3, 0xfb, 0xbe, 0xa9, 0x19, 0x84, 0x4e, 0x3f, 0x7c, 0x55, 0x4a, 0x43, 0xdd, 0x43 } } }; // pubKeys[1] (actually part of a signature, but used as small-order)
for (auto const& small_order_point : small_order_points) {
if (public_key == small_order_point)
@@ -128,60 +57,28 @@ static bool is_small_order_ed25519_point(ReadonlyBytes public_key)
return false;
}
-ErrorOr<bool> SignatureEdwardsCurve::verify(ReadonlyBytes public_key, ReadonlyBytes signature, ReadonlyBytes message, ReadonlyBytes context)
+ErrorOr<bool> ed25519_verify(ReadonlyBytes public_key, ReadonlyBytes signature, ReadonlyBytes message)
{
// For Ed25519, reject small-order points for security
// This is required by RFC 8032 and the Web Crypto API specification
- if (m_curve_type == EdwardsCurveType::Ed25519) {
- if (is_small_order_ed25519_point(public_key))
- return false;
-
- // Also check the R point in the signature (first 32 bytes) for small-order
- if (signature.size() >= 32 && is_small_order_ed25519_point(signature.slice(0, 32)))
- return false;
- }
+ if (is_small_order_ed25519_point(public_key))
+ return false;
- auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_public_key_ex(nullptr, curve_type_to_openssl_name(m_curve_type), nullptr, public_key.data(), public_key.size())));
+ // Also check the R point in the signature (first 32 bytes) for small-order
+ if (signature.size() >= 32 && is_small_order_ed25519_point(signature.slice(0, 32)))
+ return false;
+ auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_public_key_ex(nullptr, "ED25519", nullptr, public_key.data(), public_key.size())));
auto ctx = TRY(OpenSSL_MD_CTX::create());
+ OPENSSL_TRY(EVP_DigestVerifyInit_ex(ctx.ptr(), nullptr, nullptr, nullptr, nullptr, key.ptr(), nullptr));
- OSSL_PARAM params[2] = {
- OSSL_PARAM_END,
- OSSL_PARAM_END
- };
-
- if (!context.is_null()) {
- params[0] = OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, const_cast<u8*>(context.data()), context.size());
- }
-
- OPENSSL_TRY(EVP_DigestVerifyInit_ex(ctx.ptr(), nullptr, nullptr, nullptr, nullptr, key.ptr(), params));
-
- auto res = EVP_DigestVerify(ctx.ptr(), signature.data(), signature.size(), message.data(), message.size());
- if (res == 1)
+ auto result = EVP_DigestVerify(ctx.ptr(), signature.data(), signature.size(), message.data(), message.size());
+ if (result == 1)
return true;
- if (res == 0)
+ if (result == 0)
return false;
- OPENSSL_TRY(res);
+ OPENSSL_TRY(result);
VERIFY_NOT_REACHED();
}
-ErrorOr<ByteBuffer> ExchangeEdwardsCurve::compute_coordinate(ReadonlyBytes private_key, ReadonlyBytes public_key)
-{
- auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_private_key_ex(nullptr, curve_type_to_openssl_name(m_curve_type), nullptr, private_key.data(), private_key.size())));
- auto peerkey = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_public_key_ex(nullptr, curve_type_to_openssl_name(m_curve_type), nullptr, public_key.data(), public_key.size())));
-
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new(key.ptr(), nullptr)));
-
- OPENSSL_TRY(EVP_PKEY_derive_init(ctx.ptr()));
- OPENSSL_TRY(EVP_PKEY_derive_set_peer(ctx.ptr(), peerkey.ptr()));
-
- size_t key_size = 0;
- OPENSSL_TRY(EVP_PKEY_derive(ctx.ptr(), nullptr, &key_size));
-
- auto buf = TRY(ByteBuffer::create_uninitialized(key_size));
- OPENSSL_TRY(EVP_PKEY_derive(ctx.ptr(), buf.data(), &key_size));
-
- return buf;
-}
-
}
diff --git a/Libraries/LibCrypto/Curves/EdwardsCurve.h b/Libraries/LibCrypto/Curves/EdwardsCurve.h
index 8eb2619c9b1..113d3dee04c 100644
--- a/Libraries/LibCrypto/Curves/EdwardsCurve.h
+++ b/Libraries/LibCrypto/Curves/EdwardsCurve.h
@@ -6,86 +6,11 @@
#pragma once
-#include <AK/ByteBuffer.h>
+#include <AK/Error.h>
+#include <AK/Span.h>
namespace Crypto::Curves {
-enum class EdwardsCurveType : u8 {
- Ed25519,
- Ed448,
- X25519,
- X448
-};
-
-class EdwardsCurve {
-public:
- ErrorOr<ByteBuffer> generate_private_key();
- ErrorOr<ByteBuffer> generate_public_key(ReadonlyBytes private_key);
-
-protected:
- EdwardsCurve(EdwardsCurveType curve_type)
- : m_curve_type(curve_type)
- {
- }
-
- static char const* curve_type_to_openssl_name(EdwardsCurveType curve_type);
-
- EdwardsCurveType m_curve_type;
-};
-
-class SignatureEdwardsCurve : public EdwardsCurve {
-public:
- ErrorOr<ByteBuffer> sign(ReadonlyBytes private_key, ReadonlyBytes message, ReadonlyBytes context = {});
- ErrorOr<bool> verify(ReadonlyBytes public_key, ReadonlyBytes signature, ReadonlyBytes message, ReadonlyBytes context = {});
-
-protected:
- explicit SignatureEdwardsCurve(EdwardsCurveType curve_type)
- : EdwardsCurve(curve_type)
- {
- }
-};
-
-class ExchangeEdwardsCurve : public EdwardsCurve {
-public:
- ErrorOr<ByteBuffer> compute_coordinate(ReadonlyBytes private_key, ReadonlyBytes public_key);
-
-protected:
- explicit ExchangeEdwardsCurve(EdwardsCurveType curve_type)
- : EdwardsCurve(curve_type)
- {
- }
-};
-
-class Ed448 : public SignatureEdwardsCurve {
-public:
- Ed448()
- : SignatureEdwardsCurve(EdwardsCurveType::Ed448)
- {
- }
-};
-
-class X448 : public ExchangeEdwardsCurve {
-public:
- X448()
- : ExchangeEdwardsCurve(EdwardsCurveType::X448)
- {
- }
-};
-
-class Ed25519 : public SignatureEdwardsCurve {
-public:
- Ed25519()
- : SignatureEdwardsCurve(EdwardsCurveType::Ed25519)
- {
- }
-};
-
-class X25519 : public ExchangeEdwardsCurve {
-public:
- X25519()
- : ExchangeEdwardsCurve(EdwardsCurveType::X25519)
- {
- }
-};
+ErrorOr<bool> ed25519_verify(ReadonlyBytes public_key, ReadonlyBytes signature, ReadonlyBytes message);
}
diff --git a/Libraries/LibCrypto/OpenSSL.cpp b/Libraries/LibCrypto/OpenSSL.cpp
index 1535197a0ff..b36f3097171 100644
--- a/Libraries/LibCrypto/OpenSSL.cpp
+++ b/Libraries/LibCrypto/OpenSSL.cpp
@@ -11,47 +11,6 @@
namespace Crypto {
-ErrorOr<OpenSSL_PKEY> OpenSSL_PKEY::create()
-{
- return OpenSSL_PKEY(OPENSSL_TRY_PTR(EVP_PKEY_new()));
-}
-
-OpenSSL_SharedPKEY::OpenSSL_SharedPKEY(OpenSSL_PKEY&& key)
- : m_ptr(key.leak_ptr())
-{
-}
-
-OpenSSL_SharedPKEY::OpenSSL_SharedPKEY(OpenSSL_SharedPKEY const& other)
- : m_ptr(other.m_ptr)
-{
- if (m_ptr)
- VERIFY(EVP_PKEY_up_ref(m_ptr) == 1);
-}
-
-OpenSSL_SharedPKEY::OpenSSL_SharedPKEY(OpenSSL_SharedPKEY&& other)
- : m_ptr(exchange(other.m_ptr, nullptr))
-{
-}
-
-OpenSSL_SharedPKEY& OpenSSL_SharedPKEY::operator=(OpenSSL_SharedPKEY const& other)
-{
- OpenSSL_SharedPKEY copy(other);
- swap(m_ptr, copy.m_ptr);
- return *this;
-}
-
-OpenSSL_SharedPKEY& OpenSSL_SharedPKEY::operator=(OpenSSL_SharedPKEY&& other)
-{
- OpenSSL_SharedPKEY moved(move(other));
- swap(m_ptr, moved.m_ptr);
- return *this;
-}
-
-OpenSSL_SharedPKEY::~OpenSSL_SharedPKEY()
-{
- EVP_PKEY_free(m_ptr);
-}
-
ErrorOr<OpenSSL_MD_CTX> OpenSSL_MD_CTX::create()
{
return OpenSSL_MD_CTX(OPENSSL_TRY_PTR(EVP_MD_CTX_new()));
diff --git a/Libraries/LibCrypto/OpenSSL.h b/Libraries/LibCrypto/OpenSSL.h
index 63ede93b22a..6f874456055 100644
--- a/Libraries/LibCrypto/OpenSSL.h
+++ b/Libraries/LibCrypto/OpenSSL.h
@@ -87,30 +87,6 @@ class OpenSSL_X509 {
class OpenSSL_PKEY {
OPENSSL_WRAPPER_CLASS(OpenSSL_PKEY, EVP_PKEY, EVP_PKEY);
-
-public:
- static ErrorOr<OpenSSL_PKEY> create();
-
- friend class OpenSSL_SharedPKEY;
-};
-
-// OpenSSL never changes a key once it is built, so copies share one key.
-class OpenSSL_SharedPKEY {
-public:
- OpenSSL_SharedPKEY() = default;
- explicit OpenSSL_SharedPKEY(OpenSSL_PKEY&&);
-
- OpenSSL_SharedPKEY(OpenSSL_SharedPKEY const&);
- OpenSSL_SharedPKEY(OpenSSL_SharedPKEY&&);
- OpenSSL_SharedPKEY& operator=(OpenSSL_SharedPKEY const&);
- OpenSSL_SharedPKEY& operator=(OpenSSL_SharedPKEY&&);
- ~OpenSSL_SharedPKEY();
-
- // NB: OpenSSL takes a non-const key even to read from it.
- EVP_PKEY* ptr() const { return m_ptr; }
-
-private:
- EVP_PKEY* m_ptr { nullptr };
};
class OpenSSL_PKEY_CTX {
diff --git a/Libraries/LibCrypto/PK/EC.cpp b/Libraries/LibCrypto/PK/EC.cpp
index 50608c9c900..7f57d71f154 100644
--- a/Libraries/LibCrypto/PK/EC.cpp
+++ b/Libraries/LibCrypto/PK/EC.cpp
@@ -5,12 +5,9 @@
*/
#include <AK/ByteBuffer.h>
-#include <AK/Memory.h>
#include <AK/ScopeGuard.h>
-#include <AK/StringView.h>
#include <LibCrypto/OpenSSL.h>
#include <LibCrypto/PK/EC.h>
-
#include <openssl/core_names.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
@@ -32,103 +29,44 @@ static ErrorOr<char const*> curve_name(size_t scalar_size)
}
}
-static ErrorOr<OpenSSL_SharedPKEY> key_from_parameters(int selection, OSSL_PARAM_BLD* builder)
-{
- auto* params = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_to_param(builder));
- ScopeGuard const free_params = [&] { OSSL_PARAM_free(params); };
-
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)));
- OPENSSL_TRY(EVP_PKEY_fromdata_init(ctx.ptr()));
-
- EVP_PKEY* key = nullptr;
- OPENSSL_TRY(EVP_PKEY_fromdata(ctx.ptr(), &key, selection, params));
- return OpenSSL_SharedPKEY { TRY(OpenSSL_PKEY::wrap(key)) };
-}
-
-static ErrorOr<ByteBuffer> integer_parameter(EVP_PKEY* key, char const* name, size_t size)
-{
- BIGNUM* value = nullptr;
- OPENSSL_TRY(EVP_PKEY_get_bn_param(key, name, &value));
- ScopeGuard const free_value = [&] { BN_clear_free(value); };
-
- auto bytes = TRY(ByteBuffer::create_uninitialized(size));
- if (BN_bn2binpad(value, bytes.data(), size) < 0)
- return Error::from_string_literal("EC key component is too large");
- return bytes;
-}
-
-ErrorOr<ECPublicKey> ECPublicKey::from_octets(ReadonlyBytes data)
-{
- if (data.is_empty())
- return Error::from_string_literal("Empty EC point");
-
- size_t scalar_size = 0;
- if (data[0] == 0x04 && data.size() % 2 == 1)
- scalar_size = (data.size() - 1) / 2;
- else if (data[0] == 0x02 || data[0] == 0x03)
- scalar_size = data.size() - 1;
- else
- return Error::from_string_literal("Unsupported EC point format");
-
- auto const* group = TRY(curve_name(scalar_size));
-
- auto* builder = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_new());
- ScopeGuard const free_builder = [&] { OSSL_PARAM_BLD_free(builder); };
- OPENSSL_TRY(OSSL_PARAM_BLD_push_utf8_string(builder, OSSL_PKEY_PARAM_GROUP_NAME, group, 0));
- OPENSSL_TRY(OSSL_PARAM_BLD_push_octet_string(builder, OSSL_PKEY_PARAM_PUB_KEY, data.data(), data.size()));
- auto key = key_from_parameters(EVP_PKEY_PUBLIC_KEY, builder);
- if (key.is_error())
- return Error::from_string_literal("Invalid EC public key");
-
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.value().ptr(), nullptr)));
- if (EVP_PKEY_public_check(ctx.ptr()) != 1)
- return Error::from_string_literal("Invalid EC public key");
-
- return ECPublicKey { key.release_value(), scalar_size };
-}
-
-ErrorOr<ECPublicKey> ECPublicKey::from_coordinates(ReadonlyBytes x, ReadonlyBytes y)
+static ErrorOr<OpenSSL_PKEY> public_key_from_coordinates(ReadonlyBytes x, ReadonlyBytes y)
{
if (x.size() != y.size())
return Error::from_string_literal("EC coordinates have different sizes");
-
+ auto const* group = TRY(curve_name(x.size()));
auto octets = TRY(ByteBuffer::create_uninitialized(1 + x.size() + y.size()));
octets[0] = 0x04;
octets.overwrite(1, x.data(), x.size());
octets.overwrite(1 + x.size(), y.data(), y.size());
- return from_octets(octets);
-}
-ErrorOr<ByteBuffer> ECPublicKey::x_bytes() const
-{
- return integer_parameter(m_key.ptr(), OSSL_PKEY_PARAM_EC_PUB_X, m_scalar_size);
-}
-
-ErrorOr<ByteBuffer> ECPublicKey::y_bytes() const
-{
- return integer_parameter(m_key.ptr(), OSSL_PKEY_PARAM_EC_PUB_Y, m_scalar_size);
-}
+ auto* builder = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_new());
+ ScopeGuard const free_builder = [&] { OSSL_PARAM_BLD_free(builder); };
+ OPENSSL_TRY(OSSL_PARAM_BLD_push_utf8_string(builder, OSSL_PKEY_PARAM_GROUP_NAME, group, 0));
+ OPENSSL_TRY(OSSL_PARAM_BLD_push_octet_string(builder, OSSL_PKEY_PARAM_PUB_KEY, octets.data(), octets.size()));
+ auto* params = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_to_param(builder));
+ ScopeGuard const free_params = [&] { OSSL_PARAM_free(params); };
-ErrorOr<ByteBuffer> ECPublicKey::to_uncompressed() const
-{
- auto x = TRY(x_bytes());
- auto y = TRY(y_bytes());
+ auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)));
+ OPENSSL_TRY(EVP_PKEY_fromdata_init(ctx.ptr()));
+ EVP_PKEY* key_pointer = nullptr;
+ if (EVP_PKEY_fromdata(ctx.ptr(), &key_pointer, EVP_PKEY_PUBLIC_KEY, params) != 1)
+ return Error::from_string_literal("Invalid EC public key");
+ auto key = TRY(OpenSSL_PKEY::wrap(key_pointer));
- auto bytes = TRY(ByteBuffer::create_uninitialized(1 + x.size() + y.size()));
- bytes[0] = 0x04;
- bytes.overwrite(1, x.data(), x.size());
- bytes.overwrite(1 + x.size(), y.data(), y.size());
- return bytes;
+ auto check_ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr)));
+ if (EVP_PKEY_public_check(check_ctx.ptr()) != 1)
+ return Error::from_string_literal("Invalid EC public key");
+ return key;
}
-ErrorOr<bool> ecdsa_verify(ECPublicKey const& public_key, ReadonlyBytes digest, ReadonlyBytes r, ReadonlyBytes s)
+ErrorOr<bool> ecdsa_verify(ReadonlyBytes x, ReadonlyBytes y, ReadonlyBytes digest, ReadonlyBytes r, ReadonlyBytes s)
{
- if (r.size() != public_key.scalar_size() || s.size() != public_key.scalar_size())
+ auto key = TRY(public_key_from_coordinates(x, y));
+ if (r.size() != x.size() || s.size() != x.size())
return false;
auto* signature_object = OPENSSL_TRY_PTR(ECDSA_SIG_new());
ScopeGuard const free_signature_object = [&] { ECDSA_SIG_free(signature_object); };
-
auto* r_value = OPENSSL_TRY_PTR(BN_bin2bn(r.data(), r.size(), nullptr));
auto* s_value = BN_bin2bn(s.data(), s.size(), nullptr);
if (!s_value || ECDSA_SIG_set0(signature_object, r_value, s_value) != 1) {
@@ -143,9 +81,8 @@ ErrorOr<bool> ecdsa_verify(ECPublicKey const& public_key, ReadonlyBytes digest,
if (signature_size <= 0)
return Error::from_string_literal("Failed to encode the ECDSA signature");
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, public_key.openssl_key(), nullptr)));
+ auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr)));
OPENSSL_TRY(EVP_PKEY_verify_init(ctx.ptr()));
-
auto result = EVP_PKEY_verify(ctx.ptr(), signature, signature_size, digest.data(), digest.size());
if (result == 1)
return true;
diff --git a/Libraries/LibCrypto/PK/EC.h b/Libraries/LibCrypto/PK/EC.h
index fef68589ecb..947202c7f6c 100644
--- a/Libraries/LibCrypto/PK/EC.h
+++ b/Libraries/LibCrypto/PK/EC.h
@@ -6,41 +6,11 @@
#pragma once
-#include <AK/ByteBuffer.h>
-#include <AK/Optional.h>
-#include <AK/StringView.h>
-#include <LibCrypto/OpenSSL.h>
+#include <AK/Error.h>
+#include <AK/Span.h>
-// NB: Each key's curve is the NIST prime curve whose coordinates have the key's scalar size: 32 bytes for P-256, 48 for
-// P-384 and 66 for P-521.
namespace Crypto::PK {
-class ECPublicKey {
-public:
- ECPublicKey() = default;
-
- static ErrorOr<ECPublicKey> from_octets(ReadonlyBytes);
- static ErrorOr<ECPublicKey> from_coordinates(ReadonlyBytes x, ReadonlyBytes y);
-
- size_t scalar_size() const { return m_scalar_size; }
-
- ErrorOr<ByteBuffer> x_bytes() const;
- ErrorOr<ByteBuffer> y_bytes() const;
- ErrorOr<ByteBuffer> to_uncompressed() const;
-
- EVP_PKEY* openssl_key() const { return m_key.ptr(); }
-
-private:
- ECPublicKey(OpenSSL_SharedPKEY key, size_t scalar_size)
- : m_key(move(key))
- , m_scalar_size(scalar_size)
- {
- }
-
- OpenSSL_SharedPKEY m_key;
- size_t m_scalar_size { 0 };
-};
-
-ErrorOr<bool> ecdsa_verify(ECPublicKey const&, ReadonlyBytes digest, ReadonlyBytes r, ReadonlyBytes s);
+ErrorOr<bool> ecdsa_verify(ReadonlyBytes x, ReadonlyBytes y, ReadonlyBytes digest, ReadonlyBytes r, ReadonlyBytes s);
}
diff --git a/Libraries/LibCrypto/PK/RSA.cpp b/Libraries/LibCrypto/PK/RSA.cpp
index fe47b8a90bf..510ccea37a9 100644
--- a/Libraries/LibCrypto/PK/RSA.cpp
+++ b/Libraries/LibCrypto/PK/RSA.cpp
@@ -5,13 +5,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/ByteBuffer.h>
-#include <AK/Memory.h>
#include <AK/ScopeGuard.h>
-#include <AK/Vector.h>
#include <LibCrypto/OpenSSL.h>
#include <LibCrypto/PK/RSA.h>
-
#include <openssl/bn.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
@@ -19,106 +15,55 @@
namespace Crypto::PK {
-// NB: The builder reads each BIGNUM when it builds the parameters, so the BIGNUMs must outlive the builder.
-class ParameterBuilder {
- AK_MAKE_NONCOPYABLE(ParameterBuilder);
- AK_MAKE_NONMOVABLE(ParameterBuilder);
-
-public:
- ParameterBuilder() = default;
-
- ~ParameterBuilder()
- {
- OSSL_PARAM_BLD_free(m_builder);
- for (auto* integer : m_integers)
- BN_clear_free(integer);
- }
-
- ErrorOr<void> push_integer(char const* name, ReadonlyBytes bytes, bool secret)
- {
- if (!m_builder)
- m_builder = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_new());
-
- auto* integer = OPENSSL_TRY_PTR(secret ? BN_secure_new() : BN_new());
- TRY(m_integers.try_append(integer));
- OPENSSL_TRY_PTR(BN_bin2bn(bytes.data(), bytes.size(), integer));
- OPENSSL_TRY(OSSL_PARAM_BLD_push_BN(m_builder, name, integer));
- return {};
- }
-
- OSSL_PARAM_BLD* ptr() { return m_builder; }
-
-private:
- OSSL_PARAM_BLD* m_builder { nullptr };
- Vector<BIGNUM*, 8> m_integers;
-};
-
-static ErrorOr<OpenSSL_SharedPKEY> key_from_parameters(int selection, ParameterBuilder& builder)
-{
- auto* params = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_to_param(builder.ptr()));
- ScopeGuard const free_params = [&] { OSSL_PARAM_free(params); };
-
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "RSA", nullptr)));
- OPENSSL_TRY(EVP_PKEY_fromdata_init(ctx.ptr()));
-
- EVP_PKEY* key = nullptr;
- OPENSSL_TRY(EVP_PKEY_fromdata(ctx.ptr(), &key, selection, params));
- return OpenSSL_SharedPKEY { TRY(OpenSSL_PKEY::wrap(key)) };
-}
-
// https://www.rfc-editor.org/rfc/rfc8017#section-3.1
// [...] the RSA public exponent e is an integer between 3 and n - 1 [...]
// NB: An odd e above 1 is at least 3.
-static ErrorOr<bool> public_exponent_is_valid(EVP_PKEY* key)
+static bool public_exponent_is_valid(BIGNUM const* n, BIGNUM const* e)
{
- BIGNUM* n = nullptr;
- BIGNUM* e = nullptr;
- ScopeGuard const free_values = [&] {
- BN_free(n);
- BN_free(e);
- };
- OPENSSL_TRY(EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_RSA_N, &n));
- OPENSSL_TRY(EVP_PKEY_get_bn_param(key, OSSL_PKEY_PARAM_RSA_E, &e));
return BN_is_odd(e) && !BN_is_one(e) && BN_cmp(e, n) < 0;
}
-RSAPublicKey::RSAPublicKey(OpenSSL_SharedPKEY key)
- : m_key(move(key))
- , m_length(EVP_PKEY_get_size(m_key.ptr()))
-{
-}
-
-ErrorOr<RSAPublicKey> RSAPublicKey::from_components(ReadonlyBytes modulus, ReadonlyBytes public_exponent)
-{
- ParameterBuilder builder;
- TRY(builder.push_integer(OSSL_PKEY_PARAM_RSA_N, modulus, false));
- TRY(builder.push_integer(OSSL_PKEY_PARAM_RSA_E, public_exponent, false));
- return RSAPublicKey { TRY(key_from_parameters(EVP_PKEY_PUBLIC_KEY, builder)) };
-}
+static OSSL_PARAM const pkcs1_parameters[] {
+ OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, const_cast<char*>(OSSL_PKEY_RSA_PAD_MODE_PKCSV15), sizeof(OSSL_PKEY_RSA_PAD_MODE_PKCSV15) - 1),
+ OSSL_PARAM_END,
+};
-ErrorOr<bool> RSAPublicKey::is_valid() const
+ErrorOr<bool> rsa_pkcs1_verify(ReadonlyBytes modulus, ReadonlyBytes public_exponent, Hash::HashKind hash_kind, ReadonlyBytes message, ReadonlyBytes signature)
{
- if (!TRY(public_exponent_is_valid(m_key.ptr())))
+ // NB: The builder reads each BIGNUM when it builds the parameters, so the BIGNUMs must outlive it.
+ BIGNUM* n = nullptr;
+ BIGNUM* e = nullptr;
+ OSSL_PARAM_BLD* builder = nullptr;
+ OSSL_PARAM* params = nullptr;
+ ScopeGuard const free_parameters = [&] {
+ OSSL_PARAM_free(params);
+ OSSL_PARAM_BLD_free(builder);
+ BN_free(n);
+ BN_free(e);
+ };
+ n = OPENSSL_TRY_PTR(BN_bin2bn(modulus.data(), modulus.size(), nullptr));
+ e = OPENSSL_TRY_PTR(BN_bin2bn(public_exponent.data(), public_exponent.size(), nullptr));
+ builder = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_new());
+ OPENSSL_TRY(OSSL_PARAM_BLD_push_BN(builder, OSSL_PKEY_PARAM_RSA_N, n));
+ OPENSSL_TRY(OSSL_PARAM_BLD_push_BN(builder, OSSL_PKEY_PARAM_RSA_E, e));
+ params = OPENSSL_TRY_PTR(OSSL_PARAM_BLD_to_param(builder));
+
+ auto key_ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "RSA", nullptr)));
+ OPENSSL_TRY(EVP_PKEY_fromdata_init(key_ctx.ptr()));
+ EVP_PKEY* key_pointer = nullptr;
+ OPENSSL_TRY(EVP_PKEY_fromdata(key_ctx.ptr(), &key_pointer, EVP_PKEY_PUBLIC_KEY, params));
+ auto key = TRY(OpenSSL_PKEY::wrap(key_pointer));
+
+ if (!public_exponent_is_valid(n, e))
return false;
-
- auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, m_key.ptr(), nullptr)));
- return EVP_PKEY_public_check(ctx.ptr()) == 1;
-}
-
-static ErrorOr<bool> verify(RSAPublicKey const& public_key, Hash::HashKind hash_kind, OSSL_PARAM const* params, ReadonlyBytes message, ReadonlyBytes signature)
-{
- if (signature.size() != public_key.length())
+ auto check_ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_pkey(nullptr, key.ptr(), nullptr)));
+ if (EVP_PKEY_public_check(check_ctx.ptr()) != 1)
return false;
-
- // NB: Keys are checked in full when they are imported, so this only keeps the rule on e, which is cheap.
- if (!TRY(public_exponent_is_valid(public_key.openssl_key())))
+ if (signature.size() != static_cast<size_t>(EVP_PKEY_get_size(key.ptr())))
return false;
- auto* key = public_key.openssl_key();
auto ctx = TRY(OpenSSL_MD_CTX::create());
-
- OPENSSL_TRY(EVP_DigestVerifyInit_ex(ctx.ptr(), nullptr, TRY(Hash::openssl_digest_name(hash_kind)), nullptr, nullptr, key, params));
-
+ OPENSSL_TRY(EVP_DigestVerifyInit_ex(ctx.ptr(), nullptr, TRY(Hash::openssl_digest_name(hash_kind)), nullptr, nullptr, key.ptr(), pkcs1_parameters));
auto result = EVP_DigestVerify(ctx.ptr(), signature.data(), signature.size(), message.data(), message.size());
if (result == 1)
return true;
@@ -128,14 +73,4 @@ static ErrorOr<bool> verify(RSAPublicKey const& public_key, Hash::HashKind hash_
VERIFY_NOT_REACHED();
}
-static OSSL_PARAM const pkcs1_parameters[] {
- OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, const_cast<char*>(OSSL_PKEY_RSA_PAD_MODE_PKCSV15), sizeof(OSSL_PKEY_RSA_PAD_MODE_PKCSV15) - 1),
- OSSL_PARAM_END,
-};
-
-ErrorOr<bool> rsa_pkcs1_verify(RSAPublicKey const& public_key, Hash::HashKind hash_kind, ReadonlyBytes message, ReadonlyBytes signature)
-{
- return verify(public_key, hash_kind, pkcs1_parameters, message, signature);
-}
-
}
diff --git a/Libraries/LibCrypto/PK/RSA.h b/Libraries/LibCrypto/PK/RSA.h
index fc39bb4b3af..dcbb901f4d0 100644
--- a/Libraries/LibCrypto/PK/RSA.h
+++ b/Libraries/LibCrypto/PK/RSA.h
@@ -8,32 +8,12 @@
#pragma once
-#include <AK/ByteBuffer.h>
+#include <AK/Error.h>
+#include <AK/Span.h>
#include <LibCrypto/Hash/Hash.h>
-#include <LibCrypto/OpenSSL.h>
-// NB: RSA key components are big-endian unsigned integers.
namespace Crypto::PK {
-class RSAPublicKey {
-public:
- RSAPublicKey() = default;
-
- static ErrorOr<RSAPublicKey> from_components(ReadonlyBytes modulus, ReadonlyBytes public_exponent);
-
- size_t length() const { return m_length; }
-
- ErrorOr<bool> is_valid() const;
-
- EVP_PKEY* openssl_key() const { return m_key.ptr(); }
-
-private:
- RSAPublicKey(OpenSSL_SharedPKEY key);
-
- OpenSSL_SharedPKEY m_key;
- size_t m_length { 0 };
-};
-
-ErrorOr<bool> rsa_pkcs1_verify(RSAPublicKey const&, Hash::HashKind, ReadonlyBytes message, ReadonlyBytes signature);
+ErrorOr<bool> rsa_pkcs1_verify(ReadonlyBytes modulus, ReadonlyBytes public_exponent, Hash::HashKind, ReadonlyBytes message, ReadonlyBytes signature);
}
diff --git a/Libraries/LibDNS/Validator.cpp b/Libraries/LibDNS/Validator.cpp
index ec32d73f865..f919926555e 100644
--- a/Libraries/LibDNS/Validator.cpp
+++ b/Libraries/LibDNS/Validator.cpp
@@ -121,10 +121,7 @@ static ErrorOr<bool> verify_signature(Records::DNSKEY const& key, ReadonlyBytes
: key.algorithm == Algorithm::RSASHA512 ? Crypto::Hash::HashKind::SHA512
: Crypto::Hash::HashKind::SHA1;
auto components = TRY(key.rsa_public_key_components());
- auto public_key = TRY(Crypto::PK::RSAPublicKey::from_components(components.modulus, components.exponent));
- if (!TRY(public_key.is_valid()))
- return false;
- return Crypto::PK::rsa_pkcs1_verify(public_key, hash_kind, signed_data, signature);
+ return Crypto::PK::rsa_pkcs1_verify(components.modulus, components.exponent, hash_kind, signed_data, signature);
}
case Algorithm::ECDSAP256SHA256:
case Algorithm::ECDSAP384SHA384: {
@@ -136,14 +133,11 @@ static ErrorOr<bool> verify_signature(Records::DNSKEY const& key, ReadonlyBytes
size_t scalar_size = is_p256 ? 32 : 48;
if (key.public_key.size() != scalar_size * 2 || signature.size() != scalar_size * 2)
return false;
- auto public_key = TRY(Crypto::PK::ECPublicKey::from_coordinates(key.public_key.bytes().slice(0, scalar_size), key.public_key.bytes().slice(scalar_size, scalar_size)));
auto digest = Crypto::Hash::digest(is_p256 ? Crypto::Hash::HashKind::SHA256 : Crypto::Hash::HashKind::SHA384, signed_data);
- return Crypto::PK::ecdsa_verify(public_key, digest.bytes(), signature.slice(0, scalar_size), signature.slice(scalar_size, scalar_size));
- }
- case Algorithm::ED25519: {
- Crypto::Curves::Ed25519 ed25519;
- return ed25519.verify(key.public_key.bytes(), signature, signed_data);
+ return Crypto::PK::ecdsa_verify(key.public_key.bytes().slice(0, scalar_size), key.public_key.bytes().slice(scalar_size, scalar_size), digest.bytes(), signature.slice(0, scalar_size), signature.slice(scalar_size, scalar_size));
}
+ case Algorithm::ED25519:
+ return Crypto::Curves::ed25519_verify(key.public_key.bytes(), signature, signed_data);
default:
return false;
}
diff --git a/Tests/LibCrypto/CMakeLists.txt b/Tests/LibCrypto/CMakeLists.txt
index dece03e8fc5..751674b582e 100644
--- a/Tests/LibCrypto/CMakeLists.txt
+++ b/Tests/LibCrypto/CMakeLists.txt
@@ -1,9 +1,7 @@
ladybird_test_suite(LibCrypto)
set(TEST_SOURCES
- TestCurves.cpp
TestEd25519.cpp
- TestEd448.cpp
TestHash.cpp
TestHMAC.cpp
TestRSA.cpp
diff --git a/Tests/LibCrypto/TestCurves.cpp b/Tests/LibCrypto/TestCurves.cpp
deleted file mode 100644
index bd910cc2cc4..00000000000
--- a/Tests/LibCrypto/TestCurves.cpp
+++ /dev/null
@@ -1,226 +0,0 @@
-/*
- * Copyright (c) 2022, stelar7 <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/ByteBuffer.h>
-#include <LibCrypto/Curves/EdwardsCurve.h>
-#include <LibCrypto/PK/EC.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
- };
-
- 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 };
-
- Crypto::Curves::X25519 curve;
-
- auto generated_alice_public = MUST(curve.generate_public_key(alice_private_key));
- EXPECT_EQ(alice_public_key, generated_alice_public);
-
- auto generated_bob_public = MUST(curve.generate_public_key(bob_private_key));
- EXPECT_EQ(bob_public_key, generated_bob_public);
-
- auto shared_alice = MUST(curve.compute_coordinate(alice_private_key, bob_public_key));
- EXPECT_EQ(shared_alice, shared_secret);
-
- auto shared_bob = MUST(curve.compute_coordinate(bob_private_key, alice_public_key));
- EXPECT_EQ(shared_bob, shared_secret);
-
- 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, 0x9c, 0x63,
- 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5, 0xd9, 0xbb,
- 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4,
- 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53,
- 0x17, 0x7f, 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0
- };
-
- u8 bob_private_key_data[56] {
- 0x1c, 0x30, 0x6a, 0x7a, 0xc2, 0xa0, 0xe2, 0xe0,
- 0x99, 0x0b, 0x29, 0x44, 0x70, 0xcb, 0xa3, 0x39,
- 0xe6, 0x45, 0x37, 0x72, 0xb0, 0x75, 0x81, 0x1d,
- 0x8f, 0xad, 0x0d, 0x1d, 0x69, 0x27, 0xc1, 0x20,
- 0xbb, 0x5e, 0xe8, 0x97, 0x2b, 0x0d, 0x3e, 0x21,
- 0x37, 0x4c, 0x9c, 0x92, 0x1b, 0x09, 0xd1, 0xb0,
- 0x36, 0x6f, 0x10, 0xb6, 0x51, 0x73, 0x99, 0x2d
- };
-
- u8 bob_public_key_data[56] {
- 0x3e, 0xb7, 0xa8, 0x29, 0xb0, 0xcd, 0x20, 0xf5,
- 0xbc, 0xfc, 0x0b, 0x59, 0x9b, 0x6f, 0xec, 0xcf,
- 0x6d, 0xa4, 0x62, 0x71, 0x07, 0xbd, 0xb0, 0xd4,
- 0xf3, 0x45, 0xb4, 0x30, 0x27, 0xd8, 0xb9, 0x72,
- 0xfc, 0x3e, 0x34, 0xfb, 0x42, 0x32, 0xa1, 0x3c,
- 0xa7, 0x06, 0xdc, 0xb5, 0x7a, 0xec, 0x3d, 0xae,
- 0x07, 0xbd, 0xc1, 0xc6, 0x7b, 0xf3, 0x36, 0x09
- };
-
- u8 shared_secret_data[56] {
- 0x07, 0xff, 0xf4, 0x18, 0x1a, 0xc6, 0xcc, 0x95,
- 0xec, 0x1c, 0x16, 0xa9, 0x4a, 0x0f, 0x74, 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
- };
-
- 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 };
-
- Crypto::Curves::X448 curve;
-
- auto generated_alice_public = MUST(curve.generate_public_key(alice_private_key));
- EXPECT_EQ(alice_public_key, generated_alice_public);
-
- auto generated_bob_public = MUST(curve.generate_public_key(bob_private_key));
- EXPECT_EQ(bob_public_key, generated_bob_public);
-
- auto shared_alice = MUST(curve.compute_coordinate(alice_private_key, bob_public_key));
- EXPECT_EQ(shared_alice, shared_secret);
-
- auto shared_bob = MUST(curve.compute_coordinate(bob_private_key, alice_public_key));
- EXPECT_EQ(shared_bob, shared_secret);
-
- EXPECT_EQ(shared_alice, shared_bob);
-}
-
-// Test vectors from WebCryptoAPI WPT ec_importKey.https.any.js
-TEST_CASE(test_ec_compressed_point_p256)
-{
- // clang-format off
- u8 raw_data[] {
- 0x04,
- 0xD2, 0x10, 0xB0, 0xA6, 0xF9, 0xD9, 0xF0, 0x12, 0x86, 0x80, 0x58, 0xB4, 0x3F, 0xA4, 0xF4, 0x71,
- 0x01, 0x85, 0x43, 0xBB, 0xA0, 0x0C, 0x92, 0x50, 0xDF, 0x92, 0x57, 0xC2, 0xAC, 0xAE, 0x5D, 0xD1,
- 0xCE, 0x03, 0x75, 0x52, 0xD4, 0x81, 0x45, 0x0C, 0xE3, 0x9B, 0x4D, 0x10, 0x95, 0x70, 0x1B, 0x17,
- 0x5B, 0xFA, 0xB3, 0x4B, 0x8E, 0x6C, 0x09, 0x9E, 0x18, 0xF1, 0xC1, 0x98, 0x35, 0x83, 0x61, 0xE8,
- };
- u8 compressed_data[] {
- 0x02,
- 0xD2, 0x10, 0xB0, 0xA6, 0xF9, 0xD9, 0xF0, 0x12, 0x86, 0x80, 0x58, 0xB4, 0x3F, 0xA4, 0xF4, 0x71,
- 0x01, 0x85, 0x43, 0xBB, 0xA0, 0x0C, 0x92, 0x50, 0xDF, 0x92, 0x57, 0xC2, 0xAC, 0xAE, 0x5D, 0xD1,
- };
-
- auto uncompressed = TRY_OR_FAIL(Crypto::PK::ECPublicKey::from_octets(raw_data));
- auto compressed = TRY_OR_FAIL(Crypto::PK::ECPublicKey::from_octets(compressed_data));
-
- EXPECT_EQ(TRY_OR_FAIL(compressed.to_uncompressed()), TRY_OR_FAIL(uncompressed.to_uncompressed()));
-}
-
-TEST_CASE(test_ec_compressed_point_p384)
-{
- // clang-format off
- u8 raw_data[] {
- 0x04,
- 0x21, 0x9C, 0x14, 0xD6, 0x66, 0x17, 0xB3, 0x6E, 0xC6, 0xD8, 0x85, 0x6B, 0x38, 0x5B, 0x73, 0xA7,
- 0x4D, 0x34, 0x4F, 0xD8, 0xAE, 0x75, 0xEF, 0x04, 0x64, 0x35, 0xDD, 0xA5, 0x4E, 0x3B, 0x44, 0xBD,
- 0x5F, 0xBD, 0xEB, 0xD1, 0xD0, 0x8D, 0xD6, 0x9E, 0x2D, 0x7D, 0xC1, 0xDC, 0x21, 0x8C, 0xB4, 0x35,
- 0xBD, 0x28, 0x13, 0x8C, 0xC7, 0x78, 0x33, 0x7A, 0x84, 0x2F, 0x6B, 0xD6, 0x1B, 0x24, 0x0E, 0x74,
- 0x24, 0x9F, 0x24, 0x66, 0x7C, 0x2A, 0x58, 0x10, 0xA7, 0x6B, 0xFC, 0x28, 0xE0, 0x33, 0x5F, 0x88,
- 0xA6, 0x50, 0x1D, 0xEC, 0x01, 0x97, 0x6D, 0xA8, 0x5A, 0xFB, 0x00, 0x86, 0x9C, 0xB6, 0xAC, 0xE8,
- };
- u8 compressed_data[] {
- 0x02,
- 0x21, 0x9C, 0x14, 0xD6, 0x66, 0x17, 0xB3, 0x6E, 0xC6, 0xD8, 0x85, 0x6B, 0x38, 0x5B, 0x73, 0xA7,
- 0x4D, 0x34, 0x4F, 0xD8, 0xAE, 0x75, 0xEF, 0x04, 0x64, 0x35, 0xDD, 0xA5, 0x4E, 0x3B, 0x44, 0xBD,
- 0x5F, 0xBD, 0xEB, 0xD1, 0xD0, 0x8D, 0xD6, 0x9E, 0x2D, 0x7D, 0xC1, 0xDC, 0x21, 0x8C, 0xB4, 0x35,
- };
-
- auto uncompressed = TRY_OR_FAIL(Crypto::PK::ECPublicKey::from_octets(raw_data));
- auto compressed = TRY_OR_FAIL(Crypto::PK::ECPublicKey::from_octets(compressed_data));
-
- EXPECT_EQ(TRY_OR_FAIL(compressed.to_uncompressed()), TRY_OR_FAIL(uncompressed.to_uncompressed()));
-}
-
-TEST_CASE(test_ec_compressed_point_p521)
-{
- // clang-format off
- u8 raw_data[] {
- 0x04,
- 0x01, 0x56, 0xF4, 0x79, 0xF8, 0xDF, 0x1E, 0x20, 0xA7, 0xFF, 0xC0, 0x4C, 0xE4, 0x20, 0xC3, 0xE1,
- 0x54, 0xAE, 0x25, 0x19, 0x96, 0xBE, 0xE4, 0x2F, 0x03, 0x4B, 0x84, 0xD4, 0x1B, 0x74, 0x3F, 0x34,
- 0xE4, 0x5F, 0x31, 0x1B, 0x81, 0x3A, 0x9C, 0xDE, 0xC8, 0xCD, 0xA5, 0x9B, 0xBB, 0xBD, 0x31, 0xD4,
- 0x60, 0xB3, 0x29, 0x25, 0x21, 0xE7, 0xC1, 0xB7, 0x22, 0xE5, 0x66, 0x7C, 0x03, 0xDB, 0x2F, 0xAE,
- 0x75, 0x3F,
- 0x01, 0x50, 0x17, 0x36, 0xCF, 0xE2, 0x47, 0x39, 0x43, 0x20, 0xD8, 0xE4, 0xAF, 0xC2, 0xFD, 0x39,
- 0xB5, 0xA9, 0x33, 0x10, 0x61, 0xB8, 0x1E, 0x22, 0x41, 0x28, 0x2B, 0x9E, 0x17, 0x89, 0x18, 0x22,
- 0xB5, 0xB7, 0x9E, 0x05, 0x2F, 0x45, 0x97, 0xB5, 0x96, 0x43, 0xFD, 0x39, 0x37, 0x9C, 0x51, 0xBD,
- 0x51, 0x25, 0xC4, 0xF4, 0x8B, 0xC3, 0xF0, 0x25, 0xCE, 0x3C, 0xD3, 0x69, 0x53, 0x28, 0x6C, 0xCB,
- 0x38, 0xFB,
- };
- u8 compressed_data[] {
- 0x03,
- 0x01, 0x56, 0xF4, 0x79, 0xF8, 0xDF, 0x1E, 0x20, 0xA7, 0xFF, 0xC0, 0x4C, 0xE4, 0x20, 0xC3, 0xE1,
- 0x54, 0xAE, 0x25, 0x19, 0x96, 0xBE, 0xE4, 0x2F, 0x03, 0x4B, 0x84, 0xD4, 0x1B, 0x74, 0x3F, 0x34,
- 0xE4, 0x5F, 0x31, 0x1B, 0x81, 0x3A, 0x9C, 0xDE, 0xC8, 0xCD, 0xA5, 0x9B, 0xBB, 0xBD, 0x31, 0xD4,
- 0x60, 0xB3, 0x29, 0x25, 0x21, 0xE7, 0xC1, 0xB7, 0x22, 0xE5, 0x66, 0x7C, 0x03, 0xDB, 0x2F, 0xAE,
- 0x75, 0x3F,
- };
-
- auto uncompressed = TRY_OR_FAIL(Crypto::PK::ECPublicKey::from_octets(raw_data));
- auto compressed = TRY_OR_FAIL(Crypto::PK::ECPublicKey::from_octets(compressed_data));
-
- EXPECT_EQ(TRY_OR_FAIL(compressed.to_uncompressed()), TRY_OR_FAIL(uncompressed.to_uncompressed()));
-}
diff --git a/Tests/LibCrypto/TestEd25519.cpp b/Tests/LibCrypto/TestEd25519.cpp
index 5e60326c9e9..d9ecf73ec6f 100644
--- a/Tests/LibCrypto/TestEd25519.cpp
+++ b/Tests/LibCrypto/TestEd25519.cpp
@@ -11,13 +11,6 @@
// https://datatracker.ietf.org/doc/html/rfc8032#section-7.1
TEST_CASE(TEST_1)
{
- u8 private_bytes[32] = {
- 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60,
- 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4,
- 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19,
- 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60
- };
-
u8 public_bytes[32] = {
0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7,
0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a,
@@ -38,23 +31,16 @@ TEST_CASE(TEST_1)
0x65, 0x51, 0x41, 0x43, 0x8e, 0x7a, 0x10, 0x0b
};
- ReadonlyBytes private_key { private_bytes, 32 };
ReadonlyBytes public_key { public_bytes, 32 };
ReadonlyBytes message { message_bytes, 0 };
ReadonlyBytes expected_signature { signature_bytes, 64 };
- Crypto::Curves::Ed25519 curve;
-
- auto generated_signature = TRY_OR_FAIL(curve.sign(private_key, message));
- EXPECT_EQ(generated_signature, expected_signature);
- EXPECT(TRY_OR_FAIL(curve.verify(public_key, expected_signature, message)));
+ EXPECT(TRY_OR_FAIL(Crypto::Curves::ed25519_verify(public_key, expected_signature, message)));
}
// Test case for small-order point rejection
TEST_CASE(small_order_point_rejection)
{
- Crypto::Curves::Ed25519 curve;
-
// Test message
u8 message_bytes[5] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f }; // "hello"
ReadonlyBytes message { message_bytes, 5 };
@@ -100,27 +86,25 @@ TEST_CASE(small_order_point_rejection)
ReadonlyBytes signature { dummy_signature, 64 };
// All of these should return false (verification failure due to small-order point)
- auto result1 = TRY_OR_FAIL(curve.verify(ReadonlyBytes { identity_point, 32 }, signature, message));
+ auto result1 = TRY_OR_FAIL(Crypto::Curves::ed25519_verify(ReadonlyBytes { identity_point, 32 }, signature, message));
EXPECT(!result1);
- auto result2 = TRY_OR_FAIL(curve.verify(ReadonlyBytes { order_2_point, 32 }, signature, message));
+ auto result2 = TRY_OR_FAIL(Crypto::Curves::ed25519_verify(ReadonlyBytes { order_2_point, 32 }, signature, message));
EXPECT(!result2);
- auto result3 = TRY_OR_FAIL(curve.verify(ReadonlyBytes { order_4_point, 32 }, signature, message));
+ auto result3 = TRY_OR_FAIL(Crypto::Curves::ed25519_verify(ReadonlyBytes { order_4_point, 32 }, signature, message));
EXPECT(!result3);
- auto result4 = TRY_OR_FAIL(curve.verify(ReadonlyBytes { order_8_point, 32 }, signature, message));
+ auto result4 = TRY_OR_FAIL(Crypto::Curves::ed25519_verify(ReadonlyBytes { order_8_point, 32 }, signature, message));
EXPECT(!result4);
- auto result5 = TRY_OR_FAIL(curve.verify(ReadonlyBytes { wpt_small_order_point, 32 }, signature, message));
+ auto result5 = TRY_OR_FAIL(Crypto::Curves::ed25519_verify(ReadonlyBytes { wpt_small_order_point, 32 }, signature, message));
EXPECT(!result5);
}
// Test case for small-order R point in signature rejection
TEST_CASE(small_order_signature_r_point_rejection)
{
- Crypto::Curves::Ed25519 curve;
-
// Test message
u8 message_bytes[5] = { 0x68, 0x65, 0x6c, 0x6c, 0x6f }; // "hello"
ReadonlyBytes message { message_bytes, 5 };
@@ -147,6 +131,6 @@ TEST_CASE(small_order_signature_r_point_rejection)
ReadonlyBytes signature { signature_with_small_order_r, 64 };
// This should return false (verification failure due to small-order R point in signature)
- auto result = TRY_OR_FAIL(curve.verify(public_key, signature, message));
+ auto result = TRY_OR_FAIL(Crypto::Curves::ed25519_verify(public_key, signature, message));
EXPECT(!result);
}
diff --git a/Tests/LibCrypto/TestEd448.cpp b/Tests/LibCrypto/TestEd448.cpp
deleted file mode 100644
index 80b42129cf6..00000000000
--- a/Tests/LibCrypto/TestEd448.cpp
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Copyright (c) 2022, stelar7 <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/ByteBuffer.h>
-#include <LibCrypto/Curves/EdwardsCurve.h>
-#include <LibTest/TestCase.h>
-
-// https://datatracker.ietf.org/doc/html/rfc8032#section-7.4
-TEST_CASE(TEST_BLANK)
-{
- u8 private_bytes[57] = {
- 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
- 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
- 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
- 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
- 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
- 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
- 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
- 0x5b
- };
-
- u8 public_bytes[57] = {
- 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
- 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
- 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
- 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
- 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
- 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
- 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
- 0x80
- };
-
- u8 message_bytes[0] = {};
-
- u8 signature_bytes[114] = {
- 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
- 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
- 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
- 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
- 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
- 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
- 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
- 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
- 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
- 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
- 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
- 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
- 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
- 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
- 0x26, 0x00
- };
-
- ReadonlyBytes private_key { private_bytes, 57 };
- ReadonlyBytes public_key { public_bytes, 57 };
- ReadonlyBytes message { message_bytes, 0 };
- ReadonlyBytes expected_signature { signature_bytes, 114 };
-
- Crypto::Curves::Ed448 curve;
-
- auto generated_signature = TRY_OR_FAIL(curve.sign(private_key, message));
- EXPECT_EQ(generated_signature, expected_signature);
- EXPECT(TRY_OR_FAIL(curve.verify(public_key, expected_signature, message)));
-}
-
-// https://datatracker.ietf.org/doc/html/rfc8032#section-7.4
-TEST_CASE(TEST_1_OCTET_WITH_CTX)
-{
- // https://datatracker.ietf.org/doc/html/rfc8032#section-7.1
- u8 private_bytes[57] = {
- 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
- 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
- 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
- 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
- 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
- 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
- 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
- 0x4e
- };
-
- u8 public_bytes[57] = {
- 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
- 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
- 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
- 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
- 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
- 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
- 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
- 0x80
- };
-
- u8 message_bytes[1] = { 0x03 };
-
- u8 context_bytes[3] = { 0x66, 0x6f, 0x6f };
-
- u8 signature_bytes[114] = {
- 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
- 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
- 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
- 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
- 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
- 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
- 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
- 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
- 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
- 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
- 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
- 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
- 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
- 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
- 0x3c, 0x00
- };
-
- ReadonlyBytes private_key { private_bytes, 57 };
- ReadonlyBytes public_key { public_bytes, 57 };
- ReadonlyBytes message { message_bytes, 1 };
- ReadonlyBytes context { context_bytes, 3 };
- ReadonlyBytes expected_signature { signature_bytes, 114 };
-
- Crypto::Curves::Ed448 curve;
-
- auto generated_signature = TRY_OR_FAIL(curve.sign(private_key, message, context));
- EXPECT_EQ(generated_signature, expected_signature);
- EXPECT(TRY_OR_FAIL(curve.verify(public_key, expected_signature, message, context)));
-}
diff --git a/Tests/LibCrypto/TestRSA.cpp b/Tests/LibCrypto/TestRSA.cpp
index b1594bbb48b..ed42c9a03aa 100644
--- a/Tests/LibCrypto/TestRSA.cpp
+++ b/Tests/LibCrypto/TestRSA.cpp
@@ -31,6 +31,5 @@ TEST_CASE(test_RSA_rejects_invalid_public_key)
__builtin_memcpy(encoded_message.data() + separator + 1 + digest_info_prefix.size(), digest.bytes().data(), digest.bytes().size());
u8 const exponent_one[] { 1 };
- auto invalid_key = TRY_OR_FAIL(Crypto::PK::RSAPublicKey::from_components(modulus, exponent_one));
- EXPECT(!TRY_OR_FAIL(Crypto::PK::rsa_pkcs1_verify(invalid_key, Crypto::Hash::HashKind::SHA256, message, encoded_message)));
+ EXPECT(!TRY_OR_FAIL(Crypto::PK::rsa_pkcs1_verify(modulus, exponent_one, Crypto::Hash::HashKind::SHA256, message, encoded_message)));
}
diff --git a/Tests/LibDNS/TestDNSSEC.cpp b/Tests/LibDNS/TestDNSSEC.cpp
index b8aa962cabc..4e033482bd7 100644
--- a/Tests/LibDNS/TestDNSSEC.cpp
+++ b/Tests/LibDNS/TestDNSSEC.cpp
@@ -12,13 +12,14 @@
#include <LibCore/EventLoop.h>
#include <LibCore/Socket.h>
#include <LibCore/UDPServer.h>
-#include <LibCrypto/Curves/EdwardsCurve.h>
#include <LibCrypto/Hash/Hash.h>
#include <LibDNS/Resolver.h>
#include <LibTest/TestCase.h>
#include <AK/Windows.h>
+#include <openssl/evp.h>
+
using namespace DNS::Messages;
namespace {
@@ -73,6 +74,46 @@ static int compare_buffers(ByteBuffer const& a, ByteBuffer const& b)
return a.size() == b.size() ? 0 : (a.size() < b.size() ? -1 : 1);
}
+// NB: LibCrypto verifies Ed25519 signatures but does not make them, so these tests sign their zones with OpenSSL.
+static ByteBuffer raw_ed25519_key(EVP_PKEY* key, int (*get)(EVP_PKEY const*, unsigned char*, size_t*))
+{
+ size_t size = 0;
+ VERIFY(get(key, nullptr, &size) == 1);
+ auto bytes = MUST(ByteBuffer::create_uninitialized(size));
+ VERIFY(get(key, bytes.data(), &size) == 1);
+ return bytes;
+}
+
+struct Ed25519KeyPair {
+ ByteBuffer private_key;
+ ByteBuffer public_key;
+};
+
+static Ed25519KeyPair generate_ed25519_key_pair()
+{
+ auto* key = EVP_PKEY_Q_keygen(nullptr, nullptr, "ED25519");
+ VERIFY(key);
+ auto private_key = raw_ed25519_key(key, EVP_PKEY_get_raw_private_key);
+ auto public_key = raw_ed25519_key(key, EVP_PKEY_get_raw_public_key);
+ EVP_PKEY_free(key);
+ return { move(private_key), move(public_key) };
+}
+
+static ByteBuffer ed25519_sign(ReadonlyBytes private_key, ReadonlyBytes message)
+{
+ auto* key = EVP_PKEY_new_raw_private_key_ex(nullptr, "ED25519", nullptr, private_key.data(), private_key.size());
+ VERIFY(key);
+ auto* context = EVP_MD_CTX_new();
+ VERIFY(EVP_DigestSignInit_ex(context, nullptr, nullptr, nullptr, nullptr, key, nullptr) == 1);
+ size_t size = 0;
+ VERIFY(EVP_DigestSign(context, nullptr, &size, message.data(), message.size()) == 1);
+ auto signature = MUST(ByteBuffer::create_uninitialized(size));
+ VERIFY(EVP_DigestSign(context, signature.data(), &size, message.data(), message.size()) == 1);
+ EVP_MD_CTX_free(context);
+ EVP_PKEY_free(key);
+ return signature;
+}
+
struct Zone {
DomainName apex;
bool signed_ { true };
@@ -96,9 +137,8 @@ struct Zone {
Zone zone;
zone.apex = DomainName::from_string(apex_name);
zone.use_nsec3 = use_nsec3;
- Crypto::Curves::Ed25519 ed25519;
- zone.private_key = MUST(ed25519.generate_private_key());
- auto public_key = MUST(ed25519.generate_public_key(zone.private_key));
+ auto [private_key, public_key] = generate_ed25519_key_pair();
+ zone.private_key = move(private_key);
zone.dnskey = Records::DNSKEY { 257, 3, DNSSEC::Algorithm::ED25519, public_key, 0 };
zone.dnskey.calculated_key_tag = key_tag_for(zone.dnskey);
zone.add(zone.apex, ResourceType::DNSKEY, zone.dnskey, 3600);
@@ -229,8 +269,7 @@ struct Zone {
for (auto const& buffer : encoded)
MUST(signed_data.try_append(buffer));
- Crypto::Curves::Ed25519 ed25519;
- rrsig.signature = MUST(ed25519.sign(private_key, signed_data));
+ rrsig.signature = ed25519_sign(private_key, signed_data);
if (corrupt_signatures)
rrsig.signature[0] ^= 0x01;
return ResourceRecord { rrset.first().name, ResourceType::RRSIG, Class::IN, rrset.first().ttl, move(rrsig), {} };