summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Stapleton <alexs@prol.etari.at>2014-09-28 15:38:06 +0100
committerAlex Stapleton <alexs@prol.etari.at>2014-09-28 15:38:06 +0100
commit2d2ee522a2bc038b996573d6c0fb6b95a0560041 (patch)
tree1f2df442de25e54df1c0b772591d9562feeddbbf
parent9c2227b97ff7b3aabe0f0a957a92c7628c447da1 (diff)
downloadcryptography-2d2ee522a2bc038b996573d6c0fb6b95a0560041.tar.gz
Move _ec_key_curve_sn to openssl/ec.py
-rw-r--r--cryptography/hazmat/backends/openssl/backend.py22
-rw-r--r--cryptography/hazmat/backends/openssl/ec.py32
-rw-r--r--tests/hazmat/backends/test_openssl.py3
3 files changed, 30 insertions, 27 deletions
diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py
index 2540a51f6..9a36674a1 100644
--- a/cryptography/hazmat/backends/openssl/backend.py
+++ b/cryptography/hazmat/backends/openssl/backend.py
@@ -1065,28 +1065,6 @@ class Backend(object):
)
return curve_nid
- def _ec_key_curve_sn(self, ec_key):
- group = self._lib.EC_KEY_get0_group(ec_key)
- assert group != self._ffi.NULL
-
- nid = self._lib.EC_GROUP_get_curve_name(group)
- assert nid != self._lib.NID_undef
-
- curve_name = self._lib.OBJ_nid2sn(nid)
- assert curve_name != self._ffi.NULL
-
- sn = self._ffi.string(curve_name).decode('ascii')
- return sn
-
- def _sn_to_elliptic_curve(self, sn):
- try:
- return ec._CURVE_TYPES[sn]()
- except KeyError:
- raise UnsupportedAlgorithm(
- "{0} is not a supported elliptic curve".format(sn),
- _Reasons.UNSUPPORTED_ELLIPTIC_CURVE
- )
-
@contextmanager
def _tmp_bn_ctx(self):
bn_ctx = self._lib.BN_CTX_new()
diff --git a/cryptography/hazmat/backends/openssl/ec.py b/cryptography/hazmat/backends/openssl/ec.py
index 7b0fd9d47..9371a9a95 100644
--- a/cryptography/hazmat/backends/openssl/ec.py
+++ b/cryptography/hazmat/backends/openssl/ec.py
@@ -63,6 +63,30 @@ def _truncate_digest_for_ecdsa(ec_key_cdata, digest, backend):
return digest
+def _ec_key_curve_sn(backend, ec_key):
+ group = backend._lib.EC_KEY_get0_group(ec_key)
+ assert group != backend._ffi.NULL
+
+ nid = backend._lib.EC_GROUP_get_curve_name(group)
+ assert nid != backend._lib.NID_undef
+
+ curve_name = backend._lib.OBJ_nid2sn(nid)
+ assert curve_name != backend._ffi.NULL
+
+ sn = backend._ffi.string(curve_name).decode('ascii')
+ return sn
+
+
+def _sn_to_elliptic_curve(backend, sn):
+ try:
+ return ec._CURVE_TYPES[sn]()
+ except KeyError:
+ raise UnsupportedAlgorithm(
+ "{0} is not a supported elliptic curve".format(sn),
+ _Reasons.UNSUPPORTED_ELLIPTIC_CURVE
+ )
+
+
@utils.register_interface(interfaces.AsymmetricSignatureContext)
class _ECDSASignatureContext(object):
def __init__(self, backend, private_key, algorithm):
@@ -135,8 +159,8 @@ class _EllipticCurvePrivateKey(object):
self._backend = backend
self._ec_key = ec_key_cdata
- sn = backend._ec_key_curve_sn(ec_key_cdata)
- self._curve = backend._sn_to_elliptic_curve(sn)
+ sn = _ec_key_curve_sn(backend, ec_key_cdata)
+ self._curve = _sn_to_elliptic_curve(backend, sn)
@property
def curve(self):
@@ -189,8 +213,8 @@ class _EllipticCurvePublicKey(object):
self._backend = backend
self._ec_key = ec_key_cdata
- sn = backend._ec_key_curve_sn(ec_key_cdata)
- self._curve = backend._sn_to_elliptic_curve(sn)
+ sn = _ec_key_curve_sn(backend, ec_key_cdata)
+ self._curve = _sn_to_elliptic_curve(backend, sn)
@property
def curve(self):
diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py
index bfe6040ef..b00543fe6 100644
--- a/tests/hazmat/backends/test_openssl.py
+++ b/tests/hazmat/backends/test_openssl.py
@@ -27,6 +27,7 @@ from cryptography.exceptions import InternalError, _Reasons
from cryptography.hazmat.backends.openssl.backend import (
Backend, backend
)
+from cryptography.hazmat.backends.openssl.ec import _sn_to_elliptic_curve
from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives.asymmetric import dsa, ec, padding, rsa
from cryptography.hazmat.primitives.ciphers import Cipher
@@ -509,7 +510,7 @@ class TestOpenSSLEllipticCurve(object):
def test_sn_to_elliptic_curve_not_supported(self):
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_ELLIPTIC_CURVE):
- backend._sn_to_elliptic_curve(b"fake")
+ _sn_to_elliptic_curve(backend, b"fake")
class TestDeprecatedRSABackendMethods(object):