LibCrypto: Use OpenSSL for SECPxxxr1 point computation

This commit is contained in:
devgianlu 2025-01-26 20:49:28 +01:00 committed by Ali Mohammad Pur
commit fd2014f5c2
Notes: github-actions[bot] 2025-01-27 11:26:21 +00:00
3 changed files with 35 additions and 30 deletions

View file

@ -286,33 +286,40 @@ public:
ErrorOr<ByteBuffer> 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<BigEndian<StorageType>>());
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<u8>(0x04));
TRY(buf_stream.write_value<BigEndian<StorageType>>(result.x));
TRY(buf_stream.write_value<BigEndian<StorageType>>(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<SECPxxxr1Point> 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,
};
}

View file

@ -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

View file

@ -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