Skip to content

Check the order and cofactor of explicit curve parameters - #392

Open
mathewOracle wants to merge 1 commit into
tlsfuzzer:masterfrom
mathewOracle:fix-explicit-params-order-cofactor
Open

mathewOracle wants to merge 1 commit into
tlsfuzzer:masterfrom
mathewOracle:fix-explicit-params-order-cofactor

Conversation

@mathewOracle

Copy link
Copy Markdown

Fixes #390.

Curve.from_der() matches explicit parameters to a well-known curve with Curve.__eq__, which only compares the curve equation and the generator. So parameters with order = 0, or cofactor = n, were matched to the named curve and accepted.

This change:

  • rejects an order below 2 and a cofactor of 0 (negative values are already rejected by remove_integer)
  • when the parameters match a well-known curve, requires the order, and the cofactor if it's present, to equal that curve's. A missing cofactor is still accepted, since the field is optional.

I left Curve.__eq__ alone, because changing equality semantics seemed riskier than checking in the parser.

New tests in test_curves.py build NIST P-256 explicit parameters with order 0, 1 and n-1, and with cofactor 0, 2 and n. All six fail on master and pass here. Matching parameters with cofactor 1 or no cofactor still decode to NIST256p. The full suite passes, and black --line-length 79 and flake8 are clean.

Wycheproof check. I ran testvectors_v1 ecdh_* for brainpoolP224r1 through P512r1 and secp224r1 through secp521r1 (9 files) through ECDH.load_received_public_key_der() and generate_sharedsecret_bytes().

  • On master, 27 invalid vectors are accepted: 9 "order = 0", 9 "cofactor = n" and 9 "no cofactor".
  • With this change, the first 18 are rejected, and no valid vector changes result.
  • The 9 "no cofactor" vectors marked invalid are still accepted. In every one of these files, that vector has the same public, private and shared values as a "no cofactor" vector marked acceptable, so no implementation can satisfy both. Accepting it matches the field being optional.
  • The "acceptable" vectors with order 1, a different order or cofactor 2 are now rejected, which Wycheproof allows.

I used an AI assistant to help write and test this change. I've reviewed the diff and the test results.

Curve equality only compares the curve equation and the generator, so
explicit parameters with a wrong order or cofactor were matched to a
well-known curve and accepted. Reject an order below 2 and a cofactor
of 0, and require the order and the cofactor (when present) to match
the well-known curve.

Fixes tlsfuzzer#390
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ECDH accepts peer keys whose explicit curve parameters are malformed (order = 0, cofactor = n, no cofactor)

1 participant