forked from LadybirdBrowser/ladybird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibCrypto+LibTLS+LibWeb: Store EC key size + refactor serialization
In order for public/private key serialization to work correctly we must store the size of the key because P-521 cannot be stored as full words inside `UnsignedBigInteger` and therefore is exported as the wrong length (68 instead of 66). This makes it also possible to refactor some methods and cleanup constants scattered around. Gets almost all import/export tests, expect the JWK ones that calculate the public key on export. The `SECPxxxr1` implementation currently fails to do calculations for P-521.
- Loading branch information
Showing
9 changed files
with
140 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* | ||
* Copyright (c) 2023, Michiel Visser <[email protected]> | ||
* Copyright (c) 2024, Altomani Gianluca <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
@@ -36,6 +37,16 @@ struct SECPxxxr1CurveParameters { | |
struct SECPxxxr1Point { | ||
UnsignedBigInteger x; | ||
UnsignedBigInteger y; | ||
size_t size; | ||
|
||
static ErrorOr<ByteBuffer> scalar_to_bytes(UnsignedBigInteger const& a, size_t size) | ||
{ | ||
auto a_bytes = TRY(ByteBuffer::create_uninitialized(a.byte_length())); | ||
auto a_size = a.export_data(a_bytes.span()); | ||
VERIFY(a_size >= size); | ||
|
||
return a_bytes.slice(a_size - size, size); | ||
} | ||
|
||
static ErrorOr<SECPxxxr1Point> from_uncompressed(ReadonlyBytes data) | ||
{ | ||
|
@@ -46,16 +57,30 @@ struct SECPxxxr1Point { | |
return SECPxxxr1Point { | ||
UnsignedBigInteger::import_data(data.slice(1, half_size)), | ||
UnsignedBigInteger::import_data(data.slice(1 + half_size, half_size)), | ||
half_size, | ||
}; | ||
} | ||
|
||
ErrorOr<ByteBuffer> x_bytes() const | ||
{ | ||
return scalar_to_bytes(x, size); | ||
} | ||
|
||
ErrorOr<ByteBuffer> y_bytes() const | ||
{ | ||
return scalar_to_bytes(y, size); | ||
} | ||
|
||
ErrorOr<ByteBuffer> to_uncompressed() const | ||
{ | ||
auto bytes = TRY(ByteBuffer::create_uninitialized(1 + x.byte_length() + y.byte_length())); | ||
auto x = TRY(x_bytes()); | ||
auto y = TRY(y_bytes()); | ||
|
||
auto bytes = TRY(ByteBuffer::create_uninitialized(1 + (size * 2))); | ||
bytes[0] = 0x04; // uncompressed | ||
auto x_size = x.export_data(bytes.span().slice(1)); | ||
auto y_size = y.export_data(bytes.span().slice(1 + x_size)); | ||
return bytes.slice(0, 1 + x_size + y_size); | ||
memcpy(bytes.data() + 1, x.data(), size); | ||
memcpy(bytes.data() + 1 + size, y.data(), size); | ||
return bytes; | ||
} | ||
}; | ||
|
||
|
@@ -218,7 +243,11 @@ class SECPxxxr1 : public EllipticCurve { | |
{ | ||
VERIFY(scalar.byte_length() >= KEY_BYTE_SIZE); | ||
|
||
return compute_coordinate_point(scalar, SECPxxxr1Point { UnsignedBigInteger::import_data(GENERATOR_POINT.data() + 1, KEY_BYTE_SIZE), UnsignedBigInteger::import_data(GENERATOR_POINT.data() + 1 + KEY_BYTE_SIZE, KEY_BYTE_SIZE) }); | ||
return compute_coordinate_point(scalar, SECPxxxr1Point { | ||
UnsignedBigInteger::import_data(GENERATOR_POINT.data() + 1, KEY_BYTE_SIZE), | ||
UnsignedBigInteger::import_data(GENERATOR_POINT.data() + 1 + KEY_BYTE_SIZE, KEY_BYTE_SIZE), | ||
KEY_BYTE_SIZE, | ||
}); | ||
} | ||
|
||
ErrorOr<ByteBuffer> compute_coordinate(ReadonlyBytes scalar_bytes, ReadonlyBytes point_bytes) override | ||
|
@@ -248,8 +277,9 @@ class SECPxxxr1 : public EllipticCurve { | |
auto result_point = TRY(compute_coordinate_internal(scalar_int, JacobianPoint { point_x_int, point_y_int, 1u })); | ||
|
||
return SECPxxxr1Point { | ||
.x = storage_type_to_unsigned_big_integer(result_point.x), | ||
.y = storage_type_to_unsigned_big_integer(result_point.y), | ||
storage_type_to_unsigned_big_integer(result_point.x), | ||
storage_type_to_unsigned_big_integer(result_point.y), | ||
KEY_BYTE_SIZE, | ||
}; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.