diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp index a476ba7140a..f47c7aa6336 100644 --- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp +++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp @@ -3,6 +3,7 @@ * Copyright (c) 2024, stelar7 * Copyright (c) 2024, Jelle Raaijmakers * Copyright (c) 2024, Andreas Kling + * Copyright (c) 2024, Altomani Gianluca * * SPDX-License-Identifier: BSD-2-Clause */ @@ -2265,17 +2266,16 @@ WebIDL::ExceptionOr, GC::Ref>> ECDSA:: // 2. If the namedCurve member of normalizedAlgorithm is "P-256", "P-384" or "P-521": // Generate an Elliptic Curve key pair, as defined in [RFC6090] // with domain parameters for the curve identified by the namedCurve member of normalizedAlgorithm. - Variant curve; + Variant curve; if (normalized_algorithm.named_curve.is_one_of("P-256"sv, "P-384"sv, "P-521"sv)) { if (normalized_algorithm.named_curve == "P-256") curve = ::Crypto::Curves::SECP256r1 {}; - - if (normalized_algorithm.named_curve == "P-384") + else if (normalized_algorithm.named_curve == "P-384") curve = ::Crypto::Curves::SECP384r1 {}; - - // FIXME: Support P-521 - if (normalized_algorithm.named_curve == "P-521") - return WebIDL::NotSupportedError::create(m_realm, "'P-521' is not supported yet"_string); + else if (normalized_algorithm.named_curve == "P-521") + curve = ::Crypto::Curves::SECP521r1 {}; + else + VERIFY_NOT_REACHED(); } else { // If the namedCurve member of normalizedAlgorithm is a value specified in an applicable specification: // Perform the ECDSA generation steps specified in that specification, @@ -2399,17 +2399,16 @@ WebIDL::ExceptionOr> ECDSA::sign(AlgorithmParams const& // 6. If the namedCurve attribute of the [[algorithm]] internal slot of key is "P-256", "P-384" or "P-521": if (named_curve.is_one_of("P-256"sv, "P-384"sv, "P-521"sv)) { size_t coord_size; - Variant curve; + Variant curve; if (named_curve == "P-256") { - coord_size = 32; + coord_size = 256 / 8; curve = ::Crypto::Curves::SECP256r1 {}; } else if (named_curve == "P-384") { - coord_size = 48; + coord_size = 384 / 8; curve = ::Crypto::Curves::SECP384r1 {}; } else if (named_curve == "P-521") { - // FIXME: Support P-521 - coord_size = 66; - return WebIDL::NotSupportedError::create(m_realm, "'P-521' is not supported yet"_string); + coord_size = ceil_div(521, 8); + curve = ::Crypto::Curves::SECP521r1 {}; } else { VERIFY_NOT_REACHED(); } @@ -2492,17 +2491,16 @@ WebIDL::ExceptionOr ECDSA::verify(AlgorithmParams const& params, GC:: auto result = false; - Variant curve; + Variant curve; if (named_curve.is_one_of("P-256"sv, "P-384"sv, "P-521"sv)) { if (named_curve == "P-256") curve = ::Crypto::Curves::SECP256r1 {}; - - if (named_curve == "P-384") + else if (named_curve == "P-384") curve = ::Crypto::Curves::SECP384r1 {}; - - // FIXME: Support P-521 - if (named_curve == "P-521") - return WebIDL::NotSupportedError::create(m_realm, "'P-521' is not supported yet"_string); + else if (named_curve == "P-521") + curve = ::Crypto::Curves::SECP521r1 {}; + else + VERIFY_NOT_REACHED(); // Perform the ECDSA verifying process, as specified in [RFC6090], Section 5.3, // with M as the received message, signature as the received signature @@ -2810,11 +2808,11 @@ WebIDL::ExceptionOr> ECDSA::import_key(AlgorithmParams const& size_t coord_size; if (named_curve == "P-256"sv) - coord_size = 32; + coord_size = 256 / 8; else if (named_curve == "P-384"sv) - coord_size = 48; + coord_size = 384 / 8; else if (named_curve == "P-521"sv) - coord_size = 66; + coord_size = ceil_div(521, 8); else VERIFY_NOT_REACHED(); @@ -3145,16 +3143,16 @@ WebIDL::ExceptionOr> ECDSA::export_key(Bindings::KeyFormat f }, [&](::Crypto::PK::ECPrivateKey<> const& private_key) -> ErrorOr { size_t coord_size; - Variant curve; + Variant curve; if (algorithm.named_curve() == "P-256"sv) { curve = ::Crypto::Curves::SECP256r1 {}; - coord_size = 256 / 8; + coord_size = 32; } else if (algorithm.named_curve() == "P-384"sv) { curve = ::Crypto::Curves::SECP384r1 {}; - coord_size = 384 / 8; + coord_size = 48; } else if (algorithm.named_curve() == "P-521"sv) { - // FIXME: Support P-521 - return {}; + curve = ::Crypto::Curves::SECP521r1 {}; + coord_size = 66; } else { VERIFY_NOT_REACHED(); } @@ -3284,17 +3282,16 @@ WebIDL::ExceptionOr, GC::Ref>> ECDH::g // 2. If the namedCurve member of normalizedAlgorithm is "P-256", "P-384" or "P-521": // Generate an Elliptic Curve key pair, as defined in [RFC6090] // with domain parameters for the curve identified by the namedCurve member of normalizedAlgorithm. - Variant curve; + Variant curve; if (normalized_algorithm.named_curve.is_one_of("P-256"sv, "P-384"sv, "P-521"sv)) { if (normalized_algorithm.named_curve == "P-256") curve = ::Crypto::Curves::SECP256r1 {}; - - if (normalized_algorithm.named_curve == "P-384") + else if (normalized_algorithm.named_curve == "P-384") curve = ::Crypto::Curves::SECP384r1 {}; - - // FIXME: Support P-521 - if (normalized_algorithm.named_curve == "P-521") - return WebIDL::NotSupportedError::create(m_realm, "'P-521' is not supported yet"_string); + else if (normalized_algorithm.named_curve == "P-521") + curve = ::Crypto::Curves::SECP521r1 {}; + else + VERIFY_NOT_REACHED(); } else { // If the namedCurve member of normalizedAlgorithm is a value specified in an applicable specification // that specifies the use of that value with ECDH: @@ -3415,15 +3412,15 @@ WebIDL::ExceptionOr> ECDH::derive_bits(AlgorithmParams auto private_key_data = key->handle().get<::Crypto::PK::ECPrivateKey<>>(); auto public_key_data = public_key->handle().get<::Crypto::PK::ECPublicKey<>>(); - Variant curve; - if (internal_algorithm.named_curve() == "P-256"sv) { + Variant curve; + if (internal_algorithm.named_curve() == "P-256"sv) curve = ::Crypto::Curves::SECP256r1 {}; - } else if (internal_algorithm.named_curve() == "P-384"sv) { + else if (internal_algorithm.named_curve() == "P-384"sv) curve = ::Crypto::Curves::SECP384r1 {}; - } else if (internal_algorithm.named_curve() == "P-521"sv) { - // TODO: Support P-521 - return WebIDL::NotSupportedError::create(m_realm, "'P-521' is not supported yet"_string); - } + else if (internal_algorithm.named_curve() == "P-521"sv) + curve = ::Crypto::Curves::SECP521r1 {}; + else + VERIFY_NOT_REACHED(); auto maybe_secret = curve.visit( [](Empty const&) -> ErrorOr<::Crypto::Curves::SECPxxxr1Point> { return Error::from_string_literal("noop error"); }, @@ -3713,11 +3710,11 @@ WebIDL::ExceptionOr> ECDH::import_key(AlgorithmParams const& if (named_curve.is_one_of("P-256"sv, "P-384"sv, "P-521"sv)) { size_t coord_size; if (named_curve == "P-256"sv) - coord_size = 32; + coord_size = 256 / 8; else if (named_curve == "P-384"sv) - coord_size = 48; + coord_size = 384 / 8; else if (named_curve == "P-521"sv) - coord_size = 66; + coord_size = ceil_div(521, 8); else VERIFY_NOT_REACHED(); @@ -4039,16 +4036,16 @@ WebIDL::ExceptionOr> ECDH::export_key(Bindings::KeyFormat fo }, [&](::Crypto::PK::ECPrivateKey<> const& private_key) -> ErrorOr { size_t coord_size; - Variant curve; + Variant curve; if (algorithm.named_curve() == "P-256"sv) { curve = ::Crypto::Curves::SECP256r1 {}; - coord_size = 256 / 8; + coord_size = 32; } else if (algorithm.named_curve() == "P-384"sv) { curve = ::Crypto::Curves::SECP384r1 {}; - coord_size = 384 / 8; + coord_size = 48; } else if (algorithm.named_curve() == "P-521"sv) { - // FIXME: Support P-521 - return {}; + curve = ::Crypto::Curves::SECP521r1 {}; + coord_size = 66; } else { VERIFY_NOT_REACHED(); } diff --git a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_bits.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_bits.https.any.txt index 2c4c5732ee0..b291b3b2ce3 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_bits.https.any.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_bits.https.any.txt @@ -2,8 +2,8 @@ Harness status: OK Found 40 tests -30 Pass -10 Fail +31 Pass +9 Fail Pass setup - define tests Fail P-521 good parameters Fail P-521 mixed case parameters @@ -17,7 +17,7 @@ Pass P-521 no deriveBits usage for base key Pass P-521 base key is not a private key Pass P-521 public property value is a private key Pass P-521 public property value is a secret key -Fail P-521 asking for too many bits +Pass P-521 asking for too many bits Pass P-256 good parameters Pass P-256 mixed case parameters Pass P-256 short result diff --git a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/sign_verify/ecdsa.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/sign_verify/ecdsa.https.any.txt index c01fdac6a98..9461ed38b45 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/sign_verify/ecdsa.https.any.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/sign_verify/ecdsa.https.any.txt @@ -2,8 +2,8 @@ Harness status: OK Found 253 tests -193 Pass -60 Fail +205 Pass +48 Fail Pass setup Pass ECDSA P-256 with SHA-1 verification Pass ECDSA P-256 with SHA-256 verification @@ -233,27 +233,27 @@ Fail ECDSA P-521 with SHA-1 - The signature was truncated by 1 byte verification Fail ECDSA P-521 with SHA-1 - The signature was made using SHA-1, however verification is being done using SHA-256 verification Fail ECDSA P-521 with SHA-1 - The signature was made using SHA-1, however verification is being done using SHA-384 verification Fail ECDSA P-521 with SHA-1 - The signature was made using SHA-1, however verification is being done using SHA-512 verification -Fail ECDSA P-521 with SHA-1 - Signature has excess padding verification -Fail ECDSA P-521 with SHA-1 - The signature is empty verification -Fail ECDSA P-521 with SHA-1 - The signature is all zeroes verification +Pass ECDSA P-521 with SHA-1 - Signature has excess padding verification +Pass ECDSA P-521 with SHA-1 - The signature is empty verification +Pass ECDSA P-521 with SHA-1 - The signature is all zeroes verification Fail ECDSA P-521 with SHA-256 - The signature was truncated by 1 byte verification Fail ECDSA P-521 with SHA-256 - The signature was made using SHA-256, however verification is being done using SHA-1 verification Fail ECDSA P-521 with SHA-256 - The signature was made using SHA-256, however verification is being done using SHA-384 verification Fail ECDSA P-521 with SHA-256 - The signature was made using SHA-256, however verification is being done using SHA-512 verification -Fail ECDSA P-521 with SHA-256 - Signature has excess padding verification -Fail ECDSA P-521 with SHA-256 - The signature is empty verification -Fail ECDSA P-521 with SHA-256 - The signature is all zeroes verification +Pass ECDSA P-521 with SHA-256 - Signature has excess padding verification +Pass ECDSA P-521 with SHA-256 - The signature is empty verification +Pass ECDSA P-521 with SHA-256 - The signature is all zeroes verification Fail ECDSA P-521 with SHA-384 - The signature was truncated by 1 byte verification Fail ECDSA P-521 with SHA-384 - The signature was made using SHA-384, however verification is being done using SHA-1 verification Fail ECDSA P-521 with SHA-384 - The signature was made using SHA-384, however verification is being done using SHA-256 verification Fail ECDSA P-521 with SHA-384 - The signature was made using SHA-384, however verification is being done using SHA-512 verification -Fail ECDSA P-521 with SHA-384 - Signature has excess padding verification -Fail ECDSA P-521 with SHA-384 - The signature is empty verification -Fail ECDSA P-521 with SHA-384 - The signature is all zeroes verification +Pass ECDSA P-521 with SHA-384 - Signature has excess padding verification +Pass ECDSA P-521 with SHA-384 - The signature is empty verification +Pass ECDSA P-521 with SHA-384 - The signature is all zeroes verification Fail ECDSA P-521 with SHA-512 - The signature was truncated by 1 byte verification Fail ECDSA P-521 with SHA-512 - The signature was made using SHA-512, however verification is being done using SHA-1 verification Fail ECDSA P-521 with SHA-512 - The signature was made using SHA-512, however verification is being done using SHA-256 verification Fail ECDSA P-521 with SHA-512 - The signature was made using SHA-512, however verification is being done using SHA-384 verification -Fail ECDSA P-521 with SHA-512 - Signature has excess padding verification -Fail ECDSA P-521 with SHA-512 - The signature is empty verification -Fail ECDSA P-521 with SHA-512 - The signature is all zeroes verification \ No newline at end of file +Pass ECDSA P-521 with SHA-512 - Signature has excess padding verification +Pass ECDSA P-521 with SHA-512 - The signature is empty verification +Pass ECDSA P-521 with SHA-512 - The signature is all zeroes verification \ No newline at end of file