mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-28 05:07:35 +00:00
LibCrypto: Use OpenSSL for SECPxxxr1 point computation
This commit is contained in:
parent
aefffa9455
commit
fd2014f5c2
Notes:
github-actions[bot]
2025-01-27 11:26:21 +00:00
Author: https://github.com/devgianlu
Commit: fd2014f5c2
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3374
Reviewed-by: https://github.com/alimpfard ✅
3 changed files with 35 additions and 30 deletions
|
@ -286,33 +286,40 @@ public:
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> compute_coordinate(ReadonlyBytes scalar_bytes, ReadonlyBytes point_bytes) override
|
ErrorOr<ByteBuffer> compute_coordinate(ReadonlyBytes scalar_bytes, ReadonlyBytes point_bytes) override
|
||||||
{
|
{
|
||||||
AK::FixedMemoryStream scalar_stream { scalar_bytes };
|
auto scalar = UnsignedBigInteger::import_data(scalar_bytes);
|
||||||
AK::FixedMemoryStream point_stream { point_bytes };
|
auto point = TRY(SECPxxxr1Point::from_uncompressed(point_bytes));
|
||||||
|
auto result = TRY(compute_coordinate_point(scalar, { point.x, point.y, KEY_BYTE_SIZE }));
|
||||||
StorageType scalar = TRY(scalar_stream.read_value<BigEndian<StorageType>>());
|
return result.to_uncompressed();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<SECPxxxr1Point> compute_coordinate_point(UnsignedBigInteger scalar, SECPxxxr1Point point)
|
ErrorOr<SECPxxxr1Point> compute_coordinate_point(UnsignedBigInteger scalar, SECPxxxr1Point point)
|
||||||
{
|
{
|
||||||
auto scalar_int = unsigned_big_integer_to_storage_type(scalar);
|
auto* group = EC_GROUP_new_by_curve_name(EC_curve_nist2nid(CURVE_PARAMETERS.name));
|
||||||
auto point_x_int = unsigned_big_integer_to_storage_type(point.x);
|
ScopeGuard const free_group = [&] { EC_GROUP_free(group); };
|
||||||
auto point_y_int = unsigned_big_integer_to_storage_type(point.y);
|
|
||||||
|
|
||||||
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 {
|
return SECPxxxr1Point {
|
||||||
storage_type_to_unsigned_big_integer(result_point.x),
|
TRY(openssl_bignum_to_unsigned_big_integer(rx)),
|
||||||
storage_type_to_unsigned_big_integer(result_point.y),
|
TRY(openssl_bignum_to_unsigned_big_integer(ry)),
|
||||||
KEY_BYTE_SIZE,
|
KEY_BYTE_SIZE,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,12 @@ Harness status: OK
|
||||||
|
|
||||||
Found 40 tests
|
Found 40 tests
|
||||||
|
|
||||||
36 Pass
|
40 Pass
|
||||||
4 Fail
|
|
||||||
Pass setup - define tests
|
Pass setup - define tests
|
||||||
Fail P-521 good parameters
|
Pass P-521 good parameters
|
||||||
Fail P-521 mixed case parameters
|
Pass P-521 mixed case parameters
|
||||||
Fail P-521 short result
|
Pass P-521 short result
|
||||||
Fail P-521 non-multiple of 8 bits
|
Pass P-521 non-multiple of 8 bits
|
||||||
Pass P-521 missing public curve
|
Pass P-521 missing public curve
|
||||||
Pass P-521 public property of algorithm is not a CryptoKey
|
Pass P-521 public property of algorithm is not a CryptoKey
|
||||||
Pass P-521 mismatched curves
|
Pass P-521 mismatched curves
|
||||||
|
|
|
@ -2,11 +2,10 @@ Harness status: OK
|
||||||
|
|
||||||
Found 31 tests
|
Found 31 tests
|
||||||
|
|
||||||
29 Pass
|
31 Pass
|
||||||
2 Fail
|
|
||||||
Pass setup - define tests
|
Pass setup - define tests
|
||||||
Fail P-521 good parameters
|
Pass P-521 good parameters
|
||||||
Fail P-521 mixed case parameters
|
Pass P-521 mixed case parameters
|
||||||
Pass P-521 missing public curve
|
Pass P-521 missing public curve
|
||||||
Pass P-521 public property of algorithm is not a CryptoKey
|
Pass P-521 public property of algorithm is not a CryptoKey
|
||||||
Pass P-521 mismatched curves
|
Pass P-521 mismatched curves
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue