diff --git a/Libraries/LibCrypto/Curves/SECPxxxr1.h b/Libraries/LibCrypto/Curves/SECPxxxr1.h index 593a8a8e14c..a5802af5122 100644 --- a/Libraries/LibCrypto/Curves/SECPxxxr1.h +++ b/Libraries/LibCrypto/Curves/SECPxxxr1.h @@ -286,33 +286,40 @@ public: ErrorOr compute_coordinate(ReadonlyBytes scalar_bytes, ReadonlyBytes point_bytes) override { - AK::FixedMemoryStream scalar_stream { scalar_bytes }; - AK::FixedMemoryStream point_stream { point_bytes }; - - StorageType scalar = TRY(scalar_stream.read_value>()); - JacobianPoint point = TRY(read_uncompressed_point(point_stream)); - JacobianPoint result = TRY(compute_coordinate_internal(scalar, point)); - - // Export the values into an output buffer - auto buf = TRY(ByteBuffer::create_uninitialized(POINT_BYTE_SIZE)); - AK::FixedMemoryStream buf_stream { buf.bytes() }; - TRY(buf_stream.write_value(0x04)); - TRY(buf_stream.write_value>(result.x)); - TRY(buf_stream.write_value>(result.y)); - return buf; + auto scalar = UnsignedBigInteger::import_data(scalar_bytes); + auto point = TRY(SECPxxxr1Point::from_uncompressed(point_bytes)); + auto result = TRY(compute_coordinate_point(scalar, { point.x, point.y, KEY_BYTE_SIZE })); + return result.to_uncompressed(); } ErrorOr compute_coordinate_point(UnsignedBigInteger scalar, SECPxxxr1Point point) { - auto scalar_int = unsigned_big_integer_to_storage_type(scalar); - auto point_x_int = unsigned_big_integer_to_storage_type(point.x); - auto point_y_int = unsigned_big_integer_to_storage_type(point.y); + auto* group = EC_GROUP_new_by_curve_name(EC_curve_nist2nid(CURVE_PARAMETERS.name)); + ScopeGuard const free_group = [&] { EC_GROUP_free(group); }; - auto result_point = TRY(compute_coordinate_internal(scalar_int, JacobianPoint { point_x_int, point_y_int, 1u })); + auto scalar_int = TRY(unsigned_big_integer_to_openssl_bignum(scalar)); + + auto qx = TRY(unsigned_big_integer_to_openssl_bignum(point.x)); + auto qy = TRY(unsigned_big_integer_to_openssl_bignum(point.y)); + + auto* q = EC_POINT_new(group); + ScopeGuard const free_q = [&] { EC_POINT_free(q); }; + + OPENSSL_TRY(EC_POINT_set_affine_coordinates(group, q, qx.ptr(), qy.ptr(), nullptr)); + + auto* r = EC_POINT_new(group); + ScopeGuard const free_r = [&] { EC_POINT_free(r); }; + + OPENSSL_TRY(EC_POINT_mul(group, r, nullptr, q, scalar_int.ptr(), nullptr)); + + auto rx = TRY(OpenSSL_BN::create()); + auto ry = TRY(OpenSSL_BN::create()); + + OPENSSL_TRY(EC_POINT_get_affine_coordinates(group, r, rx.ptr(), ry.ptr(), nullptr)); return SECPxxxr1Point { - storage_type_to_unsigned_big_integer(result_point.x), - storage_type_to_unsigned_big_integer(result_point.y), + TRY(openssl_bignum_to_unsigned_big_integer(rx)), + TRY(openssl_bignum_to_unsigned_big_integer(ry)), KEY_BYTE_SIZE, }; } 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 f692baf5159..236b1bbcc12 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,13 +2,12 @@ Harness status: OK Found 40 tests -36 Pass -4 Fail +40 Pass Pass setup - define tests -Fail P-521 good parameters -Fail P-521 mixed case parameters -Fail P-521 short result -Fail P-521 non-multiple of 8 bits +Pass P-521 good parameters +Pass P-521 mixed case parameters +Pass P-521 short result +Pass P-521 non-multiple of 8 bits Pass P-521 missing public curve Pass P-521 public property of algorithm is not a CryptoKey Pass P-521 mismatched curves diff --git a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_keys.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_keys.https.any.txt index 340876a8a79..226bf5ac66f 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_keys.https.any.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/WebCryptoAPI/derive_bits_keys/ecdh_keys.https.any.txt @@ -2,11 +2,10 @@ Harness status: OK Found 31 tests -29 Pass -2 Fail +31 Pass Pass setup - define tests -Fail P-521 good parameters -Fail P-521 mixed case parameters +Pass P-521 good parameters +Pass P-521 mixed case parameters Pass P-521 missing public curve Pass P-521 public property of algorithm is not a CryptoKey Pass P-521 mismatched curves