Skip to content

Commit

Permalink
Merge pull request #298 from tlsfuzzer/curve-by-name
Browse files Browse the repository at this point in the history
Selecting curve by name
  • Loading branch information
tomato42 committed Jun 10, 2022
2 parents 522f480 + 66a5150 commit 5242773
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/ecdsa/curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"NIST521p",
"curves",
"find_curve",
"curve_by_name",
"SECP256k1",
"BRAINPOOLP160r1",
"BRAINPOOLP192r1",
Expand Down Expand Up @@ -469,10 +470,44 @@ def from_pem(cls, string, valid_encodings=None):


def find_curve(oid_curve):
"""Select a curve based on its OID
:param tuple[int,...] oid_curve: ASN.1 Object Identifier of the
curve to return, like ``(1, 2, 840, 10045, 3, 1, 7)`` for ``NIST256p``.
:raises UnknownCurveError: When the oid doesn't match any of the supported
curves
:rtype: ~ecdsa.curves.Curve
"""
for c in curves:
if c.oid == oid_curve:
return c
raise UnknownCurveError(
"I don't know about the curve with oid %s."
"I only know about these: %s" % (oid_curve, [c.name for c in curves])
)


def curve_by_name(name):
"""Select a curve based on its name.
Returns a :py:class:`~ecdsa.curves.Curve` object with a ``name`` name.
Note that ``name`` is case-sensitve.
:param str name: Name of the curve to return, like ``NIST256p`` or
``prime256v1``
:raises UnknownCurveError: When the name doesn't match any of the supported
curves
:rtype: ~ecdsa.curves.Curve
"""
for c in curves:
if name == c.name or (c.openssl_name and name == c.openssl_name):
return c
raise UnknownCurveError(
"Curve with name {0!r} unknown, only curves supported: {1}".format(
name, [c.name for c in curves]
)
)
39 changes: 38 additions & 1 deletion src/ecdsa/test_curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

import base64
import pytest
from .curves import Curve, NIST256p, curves, UnknownCurveError, PRIME_FIELD_OID
from .curves import (
Curve,
NIST256p,
curves,
UnknownCurveError,
PRIME_FIELD_OID,
curve_by_name,
)
from .ellipticcurve import CurveFp, PointJacobi, CurveEdTw
from . import der
from .util import number_to_string
Expand Down Expand Up @@ -288,6 +295,36 @@ def test_decode_malformed_garbage_after_prime(self):
self.assertIn("Prime-p element", str(e.exception))


class TestCurveSearching(unittest.TestCase):
def test_correct_name(self):
c = curve_by_name("NIST256p")
self.assertIs(c, NIST256p)

def test_openssl_name(self):
c = curve_by_name("prime256v1")
self.assertIs(c, NIST256p)

def test_unknown_curve(self):
with self.assertRaises(UnknownCurveError) as e:
curve_by_name("foo bar")

self.assertIn(
"name 'foo bar' unknown, only curves supported: "
"['NIST192p', 'NIST224p'",
str(e.exception),
)

def test_with_None_as_parameter(self):
with self.assertRaises(UnknownCurveError) as e:
curve_by_name(None)

self.assertIn(
"name None unknown, only curves supported: "
"['NIST192p', 'NIST224p'",
str(e.exception),
)


@pytest.mark.parametrize("curve", curves, ids=[i.name for i in curves])
def test_curve_params_encode_decode_named(curve):
ret = Curve.from_der(curve.to_der("named_curve"))
Expand Down

0 comments on commit 5242773

Please sign in to comment.