Skip to content

Truly independent web browser

LibCrypto: Enforce SECP point widths

Kyle Elliottba2d40839130
diff --git a/Libraries/LibCrypto/Curves/SECPxxxr1.cpp b/Libraries/LibCrypto/Curves/SECPxxxr1.cpp
index 49c21f2d0c0..a37962ee341 100644
--- a/Libraries/LibCrypto/Curves/SECPxxxr1.cpp
+++ b/Libraries/LibCrypto/Curves/SECPxxxr1.cpp
@@ -94,6 +94,9 @@ ErrorOr<SECPxxxr1Point> SECPxxxr1::generate_public_key(UnsignedBigInteger scalar
ErrorOr<SECPxxxr1Point> SECPxxxr1::compute_coordinate(UnsignedBigInteger scalar, SECPxxxr1Point point)
{
+ if (point.size != m_scalar_size)
+ return Error::from_string_literal("Invalid point size for curve");
+
auto* group = EC_GROUP_new_by_curve_name(EC_curve_nist2nid(m_curve_name));
ScopeGuard const free_group = [&] { EC_GROUP_free(group); };
@@ -230,6 +233,9 @@ ErrorOr<SECPxxxr1Signature> SECPxxxr1::sign(ReadonlyBytes hash, UnsignedBigInteg
ErrorOr<bool> SECPxxxr1::is_valid_point(SECPxxxr1Point pubkey, Optional<UnsignedBigInteger> private_key)
{
+ if (pubkey.size != m_scalar_size)
+ return false;
+
auto* group = OPENSSL_TRY_PTR(EC_GROUP_new_by_curve_name(EC_curve_nist2nid(m_curve_name)));
ScopeGuard const free_group = [&] { EC_GROUP_free(group); };
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-point-width.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-point-width.txt
new file mode 100644
index 00000000000..1350e1598cf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-ECDH-point-width.txt
@@ -0,0 +1,2 @@
+trigger=OperationError
+control=OperationError
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-point-width.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-point-width.html
new file mode 100644
index 00000000000..455b5ad8434
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-ECDH-point-width.html
@@ -0,0 +1,33 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const algorithm = { name: "ECDH", namedCurve: "P-256" };
+ const local = await crypto.subtle.generateKey(algorithm, false, ["deriveBits"]);
+ const peer = await crypto.subtle.generateKey(algorithm, true, ["deriveBits"]);
+ const expected = new Uint8Array(
+ await crypto.subtle.deriveBits({ name: "ECDH", public: peer.publicKey }, local.privateKey, 256)
+ );
+ const canonical = new Uint8Array(await crypto.subtle.exportKey("raw", peer.publicKey));
+
+ async function derive(firstPaddingByte, name) {
+ const encoded = new Uint8Array(67);
+ encoded[0] = 0x04;
+ encoded.set(canonical.slice(1, 33), 2);
+ encoded.set(canonical.slice(33), 35);
+ encoded[1] = firstPaddingByte;
+ try {
+ const publicKey = await crypto.subtle.importKey("raw", encoded, algorithm, false, []);
+ const actual = new Uint8Array(
+ await crypto.subtle.deriveBits({ name: "ECDH", public: publicKey }, local.privateKey, 256)
+ );
+ println(`${name}=derived:${actual.every((byte, i) => byte === expected[i])}`);
+ } catch (error) {
+ println(`${name}=${error.name}`);
+ }
+ }
+
+ await derive(0, "trigger");
+ await derive(1, "control");
+ });
+</script>